blob: 5bf5d9043d6f821e761db5265fa3cc050877d21f [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>
Chia-I Wu5b66aa52015-04-16 22:02:10 +080045#include <vk_wsi_lunarg.h>
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -060046#include "vk_debug_report_lunarg.h"
Chia-I Wuc19795a2014-09-13 11:12:55 +080047
Cody Northropd4e020a2015-03-17 14:54:35 -060048#include "icd-spv.h"
Courtney Goeltzenleuchter3f2606d2014-10-13 17:51:58 -060049
Chia-I Wuc19795a2014-09-13 11:12:55 +080050#define DEMO_BUFFER_COUNT 2
Chia-I Wub043fe32014-10-06 15:30:33 +080051#define DEMO_TEXTURE_COUNT 1
Courtney Goeltzenleuchterf5cdad02015-03-31 16:36:30 -060052#define VERTEX_BUFFER_BIND_ID 0
Ian Elliott4e19ed02015-04-28 10:52:52 -060053#define APP_SHORT_NAME "tri"
54#define APP_LONG_NAME "The Vulkan Triangle Demo Program"
Chia-I Wuc19795a2014-09-13 11:12:55 +080055
Tony Barbour22a30862015-04-22 09:02:32 -060056#if defined(NDEBUG) && defined(__GNUC__)
57#define U_ASSERT_ONLY __attribute__((unused))
58#else
59#define U_ASSERT_ONLY
60#endif
61
Ian Elliottcaa9f272015-04-28 11:35:02 -060062#ifdef _WIN32
63#define ERR_EXIT(err_msg, err_class) \
64 do { \
65 MessageBox(NULL, err_msg, err_class, MB_OK); \
66 exit(1); \
67 } while (0)
68
69// NOTE: If the following values (copied from "loader_platform.h") change, they
70// need to change here as well:
71#define LAYER_NAMES_ENV "VK_LAYER_NAMES"
72#define LAYER_NAMES_REGISTRY_VALUE "VK_LAYER_NAMES"
73#else // _WIN32
74
75#define ERR_EXIT(err_msg, err_class) \
76 do { \
77 printf(err_msg); \
78 fflush(stdout); \
79 exit(1); \
80 } while (0)
81#endif // _WIN32
Tony Barbour22a30862015-04-22 09:02:32 -060082
Ian Elliott1b6de092015-06-22 15:07:49 -060083#define GET_DEVICE_PROC_ADDR(dev, entrypoint) \
84{ \
Courtney Goeltzenleuchtera4c8c712015-07-12 14:35:22 -060085 demo->fp##entrypoint = (PFN_vk##entrypoint) vkGetDeviceProcAddr(dev, "vk"#entrypoint); \
Ian Elliott1b6de092015-06-22 15:07:49 -060086 if (demo->fp##entrypoint == NULL) { \
87 ERR_EXIT("vkGetDeviceProcAddr failed to find vk"#entrypoint, \
88 "vkGetDeviceProcAddr Failure"); \
89 } \
90}
91
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -060092struct texture_object {
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -060093 VkSampler sampler;
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -070094
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -060095 VkImage image;
96 VkImageLayout imageLayout;
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -060097
Mark Lobodzinski23182612015-05-29 09:32:35 -050098 VkDeviceMemory mem;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -060099 VkImageView view;
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700100 int32_t tex_width, tex_height;
101};
102
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -0600103void dbgFunc(
Tony Barbourde4124d2015-07-03 10:33:54 -0600104 VkFlags msgFlags,
105 VkDbgObjectType objType,
106 uint64_t srcObject,
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -0600107 size_t location,
108 int32_t msgCode,
109 const char* pLayerPrefix,
110 const char* pMsg,
111 void* pUserData)
112{
113 char *message = (char *) malloc(strlen(pMsg)+100);
114
115 assert (message);
116
117 if (msgFlags & VK_DBG_REPORT_ERROR_BIT) {
118 sprintf(message,"ERROR: [%s] Code %d : %s", pLayerPrefix, msgCode, pMsg);
119 } else if (msgFlags & VK_DBG_REPORT_WARN_BIT) {
120 sprintf(message,"WARNING: [%s] Code %d : %s", pLayerPrefix, msgCode, pMsg);
121 } else {
122 return;
123 }
124
125#ifdef _WIN32
126 MessageBox(NULL, message, "Alert", MB_OK);
127#else
128 printf("%s\n",message);
129 fflush(stdout);
130#endif
131 free(message);
132}
133
Chia-I Wuc19795a2014-09-13 11:12:55 +0800134struct demo {
Ian Elliotte14e9f92015-04-16 15:23:05 -0600135#ifdef _WIN32
136#define APP_NAME_STR_LEN 80
137 HINSTANCE connection; // hInstance - Windows Instance
138 char name[APP_NAME_STR_LEN]; // Name to put on the window/icon
139 HWND window; // hWnd - window handle
140#else // _WIN32
Chia-I Wuc19795a2014-09-13 11:12:55 +0800141 xcb_connection_t *connection;
142 xcb_screen_t *screen;
Chia-I Wu5b66aa52015-04-16 22:02:10 +0800143 xcb_window_t window;
144 xcb_intern_atom_reply_t *atom_wm_delete_window;
Ian Elliotte14e9f92015-04-16 15:23:05 -0600145#endif // _WIN32
Jon Ashburn8a399e92015-04-24 09:46:24 -0700146 bool prepared;
Ian Elliotte14e9f92015-04-16 15:23:05 -0600147 bool use_staging_buffer;
Cody Northrop75db0322015-05-28 11:27:16 -0600148 bool use_glsl;
Chia-I Wuc19795a2014-09-13 11:12:55 +0800149
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600150 VkInstance inst;
Tony Barbour8205d902015-04-16 15:59:00 -0600151 VkPhysicalDevice gpu;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600152 VkDevice device;
153 VkQueue queue;
Tony Barbour426b9052015-06-24 16:06:58 -0600154 VkPhysicalDeviceProperties gpu_props;
Tony Barbour8205d902015-04-16 15:59:00 -0600155 VkPhysicalDeviceQueueProperties *queue_props;
Courtney Goeltzenleuchterf3168062015-03-05 18:09:39 -0700156 uint32_t graphics_queue_node_index;
Chia-I Wuc19795a2014-09-13 11:12:55 +0800157
158 int width, height;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600159 VkFormat format;
Chia-I Wuc19795a2014-09-13 11:12:55 +0800160
Jon Ashburncedc15f2015-05-21 18:13:33 -0600161 PFN_vkCreateSwapChainWSI fpCreateSwapChainWSI;
162 PFN_vkDestroySwapChainWSI fpDestroySwapChainWSI;
163 PFN_vkGetSwapChainInfoWSI fpGetSwapChainInfoWSI;
164 PFN_vkQueuePresentWSI fpQueuePresentWSI;
165
Chia-I Wu5b66aa52015-04-16 22:02:10 +0800166 VkSwapChainWSI swap_chain;
Cody Northrop18ea11b2015-07-09 18:08:32 -0600167 VkCmdPool cmd_pool;
Chia-I Wuc19795a2014-09-13 11:12:55 +0800168 struct {
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600169 VkImage image;
Tony Barbour8205d902015-04-16 15:59:00 -0600170 VkDeviceMemory mem;
Chia-I Wuc19795a2014-09-13 11:12:55 +0800171
Chia-I Wuc278df82015-07-07 11:50:03 +0800172 VkAttachmentView view;
Chia-I Wuc19795a2014-09-13 11:12:55 +0800173 } buffers[DEMO_BUFFER_COUNT];
174
Chia-I Wub043fe32014-10-06 15:30:33 +0800175 struct {
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600176 VkFormat format;
Chia-I Wu9ae87c92014-10-07 14:15:01 +0800177
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600178 VkImage image;
Mark Lobodzinski23182612015-05-29 09:32:35 -0500179 VkDeviceMemory mem;
Chia-I Wuc278df82015-07-07 11:50:03 +0800180 VkAttachmentView view;
Chia-I Wu9ae87c92014-10-07 14:15:01 +0800181 } depth;
182
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600183 struct texture_object textures[DEMO_TEXTURE_COUNT];
Chia-I Wub043fe32014-10-06 15:30:33 +0800184
Chia-I Wu99621bc2014-10-08 11:52:22 +0800185 struct {
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600186 VkBuffer buf;
Mark Lobodzinski23182612015-05-29 09:32:35 -0500187 VkDeviceMemory mem;
Chia-I Wu8d29d022014-10-08 12:14:39 +0800188
Mark Lobodzinski0e0fb5c2015-06-23 15:11:57 -0600189 VkPipelineVertexInputStateCreateInfo vi;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600190 VkVertexInputBindingDescription vi_bindings[1];
191 VkVertexInputAttributeDescription vi_attrs[2];
Chia-I Wu99621bc2014-10-08 11:52:22 +0800192 } vertices;
193
Courtney Goeltzenleuchter633f9c52015-04-30 10:58:33 -0600194 VkCmdBuffer setup_cmd; // Command Buffer for initialization commands
195 VkCmdBuffer draw_cmd; // Command Buffer for drawing commands
Mark Lobodzinski556f7212015-04-17 14:11:39 -0500196 VkPipelineLayout pipeline_layout;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600197 VkDescriptorSetLayout desc_layout;
Jon Ashburn0d60d272015-07-09 15:02:25 -0600198 VkPipelineCache pipelineCache;
Chia-I Wu76cd4222015-07-08 13:34:24 +0800199 VkRenderPass render_pass;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600200 VkPipeline pipeline;
Chia-I Wuc19795a2014-09-13 11:12:55 +0800201
Tony Barbourde4124d2015-07-03 10:33:54 -0600202 VkDynamicViewportState viewport;
203 VkDynamicRasterState raster;
204 VkDynamicColorBlendState color_blend;
205 VkDynamicDepthStencilState depth_stencil;
Chia-I Wuc19795a2014-09-13 11:12:55 +0800206
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600207 VkDescriptorPool desc_pool;
208 VkDescriptorSet desc_set;
Chia-I Wuf8385062015-01-04 16:27:24 +0800209
Chia-I Wu76cd4222015-07-08 13:34:24 +0800210 VkFramebuffer framebuffers[DEMO_BUFFER_COUNT];
211
Mark Lobodzinski72346292015-07-02 16:49:40 -0600212 VkPhysicalDeviceMemoryProperties memory_properties;
213
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -0600214 bool validate;
215 PFN_vkDbgCreateMsgCallback dbgCreateMsgCallback;
216 VkDbgMsgCallback msg_callback;
217
Chia-I Wuc19795a2014-09-13 11:12:55 +0800218 bool quit;
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600219 uint32_t current_buffer;
Chia-I Wuc19795a2014-09-13 11:12:55 +0800220};
221
Mark Lobodzinski72346292015-07-02 16:49:40 -0600222static VkResult memory_type_from_properties(struct demo *demo, uint32_t typeBits, VkFlags properties, uint32_t *typeIndex)
223{
224 // Search memtypes to find first index with those properties
225 for (uint32_t i = 0; i < 32; i++) {
226 if ((typeBits & 1) == 1) {
227 // Type is available, does it match user properties?
228 if ((demo->memory_properties.memoryTypes[i].propertyFlags & properties) == properties) {
229 *typeIndex = i;
230 return VK_SUCCESS;
231 }
232 }
233 typeBits >>= 1;
234 }
235 // No memory types matched, return failure
236 return VK_UNSUPPORTED;
237}
238
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600239static void demo_flush_init_cmd(struct demo *demo)
240{
Tony Barbour22a30862015-04-22 09:02:32 -0600241 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600242
Courtney Goeltzenleuchter633f9c52015-04-30 10:58:33 -0600243 if (demo->setup_cmd == VK_NULL_HANDLE)
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600244 return;
245
Courtney Goeltzenleuchter633f9c52015-04-30 10:58:33 -0600246 err = vkEndCommandBuffer(demo->setup_cmd);
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600247 assert(!err);
248
Courtney Goeltzenleuchter633f9c52015-04-30 10:58:33 -0600249 const VkCmdBuffer cmd_bufs[] = { demo->setup_cmd };
Tony Barbourde4124d2015-07-03 10:33:54 -0600250 VkFence nullFence = {VK_NULL_HANDLE};
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600251
Tony Barbourde4124d2015-07-03 10:33:54 -0600252 err = vkQueueSubmit(demo->queue, 1, cmd_bufs, nullFence);
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600253 assert(!err);
254
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600255 err = vkQueueWaitIdle(demo->queue);
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600256 assert(!err);
257
Tony Barbourde4124d2015-07-03 10:33:54 -0600258 vkDestroyCommandBuffer(demo->device, demo->setup_cmd);
Courtney Goeltzenleuchter633f9c52015-04-30 10:58:33 -0600259 demo->setup_cmd = VK_NULL_HANDLE;
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600260}
261
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600262static void demo_set_image_layout(
263 struct demo *demo,
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600264 VkImage image,
malnasse4b8ba4d2015-06-03 17:28:38 -0400265 VkImageAspect aspect,
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600266 VkImageLayout old_image_layout,
267 VkImageLayout new_image_layout)
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600268{
Tony Barbour22a30862015-04-22 09:02:32 -0600269 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600270
Courtney Goeltzenleuchter633f9c52015-04-30 10:58:33 -0600271 if (demo->setup_cmd == VK_NULL_HANDLE) {
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600272 const VkCmdBufferCreateInfo cmd = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600273 .sType = VK_STRUCTURE_TYPE_CMD_BUFFER_CREATE_INFO,
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600274 .pNext = NULL,
Cody Northrop18ea11b2015-07-09 18:08:32 -0600275 .cmdPool = demo->cmd_pool,
Chia-I Wu88eaa3b2015-06-26 15:34:39 +0800276 .level = VK_CMD_BUFFER_LEVEL_PRIMARY,
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600277 .flags = 0,
278 };
279
Courtney Goeltzenleuchter633f9c52015-04-30 10:58:33 -0600280 err = vkCreateCommandBuffer(demo->device, &cmd, &demo->setup_cmd);
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600281 assert(!err);
282
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600283 VkCmdBufferBeginInfo cmd_buf_info = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600284 .sType = VK_STRUCTURE_TYPE_CMD_BUFFER_BEGIN_INFO,
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600285 .pNext = NULL,
Tony Barbour8205d902015-04-16 15:59:00 -0600286 .flags = VK_CMD_BUFFER_OPTIMIZE_SMALL_BATCH_BIT |
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600287 VK_CMD_BUFFER_OPTIMIZE_ONE_TIME_SUBMIT_BIT,
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600288 };
Courtney Goeltzenleuchter633f9c52015-04-30 10:58:33 -0600289 err = vkBeginCommandBuffer(demo->setup_cmd, &cmd_buf_info);
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600290 }
291
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600292 VkImageMemoryBarrier image_memory_barrier = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600293 .sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600294 .pNext = NULL,
295 .outputMask = 0,
296 .inputMask = 0,
297 .oldLayout = old_image_layout,
298 .newLayout = new_image_layout,
299 .image = image,
malnasse4b8ba4d2015-06-03 17:28:38 -0400300 .subresourceRange = { aspect, 0, 1, 0, 0 }
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600301 };
302
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600303 if (new_image_layout == VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL) {
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600304 /* Make sure anything that was copying from this image has completed */
Courtney Goeltzenleuchterad870812015-04-15 15:29:59 -0600305 image_memory_barrier.inputMask = VK_MEMORY_INPUT_TRANSFER_BIT;
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600306 }
307
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600308 if (new_image_layout == VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL) {
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600309 /* Make sure any Copy or CPU writes to image are flushed */
Courtney Goeltzenleuchtera569a502015-04-29 17:16:21 -0600310 image_memory_barrier.outputMask = VK_MEMORY_OUTPUT_TRANSFER_BIT | VK_MEMORY_OUTPUT_HOST_WRITE_BIT;
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600311 }
312
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600313 VkImageMemoryBarrier *pmemory_barrier = &image_memory_barrier;
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600314
Tony Barbourc2e987e2015-06-29 16:20:35 -0600315 VkPipelineStageFlags src_stages = VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT;
316 VkPipelineStageFlags dest_stages = VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT;
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600317
Courtney Goeltzenleuchter82b348f2015-07-12 13:07:46 -0600318 vkCmdPipelineBarrier(demo->setup_cmd, src_stages, dest_stages, false, 1, (const void * const*)&pmemory_barrier);
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600319}
320
Chia-I Wuc19795a2014-09-13 11:12:55 +0800321static void demo_draw_build_cmd(struct demo *demo)
322{
Chia-I Wu76cd4222015-07-08 13:34:24 +0800323 const VkCmdBufferBeginInfo cmd_buf_info = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600324 .sType = VK_STRUCTURE_TYPE_CMD_BUFFER_BEGIN_INFO,
Courtney Goeltzenleuchtere3b0f3a2015-04-03 15:25:24 -0600325 .pNext = NULL,
Tony Barbour8205d902015-04-16 15:59:00 -0600326 .flags = VK_CMD_BUFFER_OPTIMIZE_SMALL_BATCH_BIT |
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600327 VK_CMD_BUFFER_OPTIMIZE_ONE_TIME_SUBMIT_BIT,
Jon Ashburn53d27af2014-12-31 17:08:35 -0700328 };
Chia-I Wuc278df82015-07-07 11:50:03 +0800329 const VkClearValue clear_values[2] = {
330 [0] = { .color.f32 = { 0.2f, 0.2f, 0.2f, 0.2f } },
331 [1] = { .ds = { 0.9f, 0 } },
332 };
333 const VkRenderPassBeginInfo rp_begin = {
334 .sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO,
335 .pNext = NULL,
Chia-I Wu76cd4222015-07-08 13:34:24 +0800336 .renderPass = demo->render_pass,
337 .framebuffer = demo->framebuffers[demo->current_buffer],
Chia-I Wuc278df82015-07-07 11:50:03 +0800338 .renderArea.offset.x = 0,
339 .renderArea.offset.y = 0,
340 .renderArea.extent.width = demo->width,
341 .renderArea.extent.height = demo->height,
342 .attachmentCount = 2,
343 .pAttachmentClearValues = clear_values,
Jon Ashburn3325d6b2015-01-02 18:24:05 -0700344 };
Chia-I Wu76cd4222015-07-08 13:34:24 +0800345 VkResult U_ASSERT_ONLY err;
Chia-I Wuc19795a2014-09-13 11:12:55 +0800346
Courtney Goeltzenleuchter633f9c52015-04-30 10:58:33 -0600347 err = vkBeginCommandBuffer(demo->draw_cmd, &cmd_buf_info);
Chia-I Wuc19795a2014-09-13 11:12:55 +0800348 assert(!err);
349
Chia-I Wuc278df82015-07-07 11:50:03 +0800350 vkCmdBeginRenderPass(demo->draw_cmd, &rp_begin, VK_RENDER_PASS_CONTENTS_INLINE);
Courtney Goeltzenleuchter633f9c52015-04-30 10:58:33 -0600351 vkCmdBindPipeline(demo->draw_cmd, VK_PIPELINE_BIND_POINT_GRAPHICS,
Chia-I Wuc19795a2014-09-13 11:12:55 +0800352 demo->pipeline);
Mark Lobodzinskia65c4632015-06-15 13:21:21 -0600353 vkCmdBindDescriptorSets(demo->draw_cmd, VK_PIPELINE_BIND_POINT_GRAPHICS, demo->pipeline_layout,
Cody Northrop1a01b1d2015-04-16 13:41:56 -0600354 0, 1, & demo->desc_set, 0, NULL);
Chia-I Wuc19795a2014-09-13 11:12:55 +0800355
Tony Barbourde4124d2015-07-03 10:33:54 -0600356 vkCmdBindDynamicViewportState(demo->draw_cmd, demo->viewport);
357 vkCmdBindDynamicRasterState(demo->draw_cmd, demo->raster);
358 vkCmdBindDynamicColorBlendState(demo->draw_cmd, demo->color_blend);
359 vkCmdBindDynamicDepthStencilState(demo->draw_cmd, demo->depth_stencil);
Chia-I Wuc19795a2014-09-13 11:12:55 +0800360
Tony Barbour8205d902015-04-16 15:59:00 -0600361 VkDeviceSize offsets[1] = {0};
Courtney Goeltzenleuchter633f9c52015-04-30 10:58:33 -0600362 vkCmdBindVertexBuffers(demo->draw_cmd, VERTEX_BUFFER_BIND_ID, 1, &demo->vertices.buf, offsets);
Chia-I Wu3b04af52014-11-08 10:48:20 +0800363
Courtney Goeltzenleuchter633f9c52015-04-30 10:58:33 -0600364 vkCmdDraw(demo->draw_cmd, 0, 3, 0, 1);
Chia-I Wu88eaa3b2015-06-26 15:34:39 +0800365 vkCmdEndRenderPass(demo->draw_cmd);
Chia-I Wuc19795a2014-09-13 11:12:55 +0800366
Courtney Goeltzenleuchter633f9c52015-04-30 10:58:33 -0600367 err = vkEndCommandBuffer(demo->draw_cmd);
Chia-I Wuc19795a2014-09-13 11:12:55 +0800368 assert(!err);
369}
370
371static void demo_draw(struct demo *demo)
372{
Chia-I Wu5b66aa52015-04-16 22:02:10 +0800373 const VkPresentInfoWSI present = {
374 .sType = VK_STRUCTURE_TYPE_PRESENT_INFO_WSI,
375 .pNext = NULL,
376 .image = demo->buffers[demo->current_buffer].image,
377 .flipInterval = 0,
Chia-I Wuc19795a2014-09-13 11:12:55 +0800378 };
Tony Barbour22a30862015-04-22 09:02:32 -0600379 VkResult U_ASSERT_ONLY err;
Chia-I Wuc19795a2014-09-13 11:12:55 +0800380
381 demo_draw_build_cmd(demo);
Tony Barbourde4124d2015-07-03 10:33:54 -0600382 VkFence nullFence = { VK_NULL_HANDLE };
Chia-I Wuc19795a2014-09-13 11:12:55 +0800383
Tony Barbourde4124d2015-07-03 10:33:54 -0600384 err = vkQueueSubmit(demo->queue, 1, &demo->draw_cmd, nullFence);
Chia-I Wuc19795a2014-09-13 11:12:55 +0800385 assert(!err);
386
Jon Ashburncedc15f2015-05-21 18:13:33 -0600387 err = demo->fpQueuePresentWSI(demo->queue, &present);
Chia-I Wuc19795a2014-09-13 11:12:55 +0800388 assert(!err);
389
390 demo->current_buffer = (demo->current_buffer + 1) % DEMO_BUFFER_COUNT;
Chia-I Wu5b66aa52015-04-16 22:02:10 +0800391
392 err = vkQueueWaitIdle(demo->queue);
393 assert(err == VK_SUCCESS);
Chia-I Wuc19795a2014-09-13 11:12:55 +0800394}
395
396static void demo_prepare_buffers(struct demo *demo)
397{
Chia-I Wu5b66aa52015-04-16 22:02:10 +0800398 const VkSwapChainCreateInfoWSI swap_chain = {
399 .sType = VK_STRUCTURE_TYPE_SWAP_CHAIN_CREATE_INFO_WSI,
400 .pNext = NULL,
401 .pNativeWindowSystemHandle = demo->connection,
402 .pNativeWindowHandle = (void *) (intptr_t) demo->window,
Ian Elliott32536f92015-04-21 16:41:02 -0600403 .displayCount = 1,
Chia-I Wu5b66aa52015-04-16 22:02:10 +0800404 .imageCount = DEMO_BUFFER_COUNT,
405 .imageFormat = demo->format,
406 .imageExtent = {
Chia-I Wuc19795a2014-09-13 11:12:55 +0800407 .width = demo->width,
408 .height = demo->height,
409 },
Chia-I Wu5b66aa52015-04-16 22:02:10 +0800410 .imageArraySize = 1,
411 .imageUsageFlags = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT,
Chia-I Wuc19795a2014-09-13 11:12:55 +0800412 };
Chia-I Wu5b66aa52015-04-16 22:02:10 +0800413 VkSwapChainImageInfoWSI images[DEMO_BUFFER_COUNT];
414 size_t images_size = sizeof(images);
Tony Barbour22a30862015-04-22 09:02:32 -0600415 VkResult U_ASSERT_ONLY err;
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600416 uint32_t i;
Chia-I Wuc19795a2014-09-13 11:12:55 +0800417
Jon Ashburncedc15f2015-05-21 18:13:33 -0600418 err = demo->fpCreateSwapChainWSI(demo->device, &swap_chain, &demo->swap_chain);
Chia-I Wu5b66aa52015-04-16 22:02:10 +0800419 assert(!err);
420
Jon Ashburncedc15f2015-05-21 18:13:33 -0600421 err = demo->fpGetSwapChainInfoWSI(demo->swap_chain,
Chia-I Wu5b66aa52015-04-16 22:02:10 +0800422 VK_SWAP_CHAIN_INFO_TYPE_PERSISTENT_IMAGES_WSI,
423 &images_size, images);
424 assert(!err && images_size == sizeof(images));
425
Chia-I Wuc19795a2014-09-13 11:12:55 +0800426 for (i = 0; i < DEMO_BUFFER_COUNT; i++) {
Chia-I Wuc278df82015-07-07 11:50:03 +0800427 VkAttachmentViewCreateInfo color_attachment_view = {
428 .sType = VK_STRUCTURE_TYPE_ATTACHMENT_VIEW_CREATE_INFO,
Chia-I Wuc19795a2014-09-13 11:12:55 +0800429 .pNext = NULL,
430 .format = demo->format,
431 .mipLevel = 0,
432 .baseArraySlice = 0,
433 .arraySize = 1,
434 };
435
Chia-I Wu5b66aa52015-04-16 22:02:10 +0800436 demo->buffers[i].image = images[i].image;
437 demo->buffers[i].mem = images[i].memory;
Mark Lobodzinski97dcd042015-04-16 08:52:00 -0500438
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600439 demo_set_image_layout(demo, demo->buffers[i].image,
malnasse4b8ba4d2015-06-03 17:28:38 -0400440 VK_IMAGE_ASPECT_COLOR,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600441 VK_IMAGE_LAYOUT_UNDEFINED,
442 VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL);
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600443
Chia-I Wuc19795a2014-09-13 11:12:55 +0800444 color_attachment_view.image = demo->buffers[i].image;
445
Chia-I Wuc278df82015-07-07 11:50:03 +0800446 err = vkCreateAttachmentView(demo->device,
Chia-I Wuc19795a2014-09-13 11:12:55 +0800447 &color_attachment_view, &demo->buffers[i].view);
448 assert(!err);
449 }
Piers Daniell886be472015-02-23 16:23:13 -0700450
451 demo->current_buffer = 0;
Chia-I Wuc19795a2014-09-13 11:12:55 +0800452}
453
Chia-I Wu9ae87c92014-10-07 14:15:01 +0800454static void demo_prepare_depth(struct demo *demo)
455{
Tony Barbour8205d902015-04-16 15:59:00 -0600456 const VkFormat depth_format = VK_FORMAT_D16_UNORM;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600457 const VkImageCreateInfo image = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600458 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
Chia-I Wu9ae87c92014-10-07 14:15:01 +0800459 .pNext = NULL,
Tony Barbour8205d902015-04-16 15:59:00 -0600460 .imageType = VK_IMAGE_TYPE_2D,
Chia-I Wu9ae87c92014-10-07 14:15:01 +0800461 .format = depth_format,
462 .extent = { demo->width, demo->height, 1 },
463 .mipLevels = 1,
464 .arraySize = 1,
465 .samples = 1,
Tony Barbour8205d902015-04-16 15:59:00 -0600466 .tiling = VK_IMAGE_TILING_OPTIMAL,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600467 .usage = VK_IMAGE_USAGE_DEPTH_STENCIL_BIT,
Chia-I Wu9ae87c92014-10-07 14:15:01 +0800468 .flags = 0,
469 };
Courtney Goeltzenleuchterddcb6192015-04-14 18:48:46 -0600470 VkMemoryAllocInfo mem_alloc = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600471 .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOC_INFO,
Mark Lobodzinski97dcd042015-04-16 08:52:00 -0500472 .pNext = NULL,
Chia-I Wu9ae87c92014-10-07 14:15:01 +0800473 .allocationSize = 0,
Mark Lobodzinski72346292015-07-02 16:49:40 -0600474 .memoryTypeIndex = 0,
Chia-I Wu9ae87c92014-10-07 14:15:01 +0800475 };
Chia-I Wuc278df82015-07-07 11:50:03 +0800476 VkAttachmentViewCreateInfo view = {
477 .sType = VK_STRUCTURE_TYPE_ATTACHMENT_VIEW_CREATE_INFO,
Chia-I Wu9ae87c92014-10-07 14:15:01 +0800478 .pNext = NULL,
Tony Barbourde4124d2015-07-03 10:33:54 -0600479 .image.handle = VK_NULL_HANDLE,
Chia-I Wu9ae87c92014-10-07 14:15:01 +0800480 .mipLevel = 0,
481 .baseArraySlice = 0,
482 .arraySize = 1,
483 .flags = 0,
484 };
Jon Ashburna9ae3832015-01-16 09:37:43 -0700485
Mark Lobodzinski23182612015-05-29 09:32:35 -0500486 VkMemoryRequirements mem_reqs;
Tony Barbour22a30862015-04-22 09:02:32 -0600487 VkResult U_ASSERT_ONLY err;
Chia-I Wu9ae87c92014-10-07 14:15:01 +0800488
489 demo->depth.format = depth_format;
490
491 /* create image */
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600492 err = vkCreateImage(demo->device, &image,
Chia-I Wu9ae87c92014-10-07 14:15:01 +0800493 &demo->depth.image);
494 assert(!err);
495
Mark Lobodzinski72346292015-07-02 16:49:40 -0600496 /* get memory requirements for this object */
Tony Barbourde4124d2015-07-03 10:33:54 -0600497 err = vkGetImageMemoryRequirements(demo->device, demo->depth.image,
498 &mem_reqs);
Mark Lobodzinski72346292015-07-02 16:49:40 -0600499
500 /* select memory size and type */
Mark Lobodzinski23182612015-05-29 09:32:35 -0500501 mem_alloc.allocationSize = mem_reqs.size;
Mark Lobodzinski72346292015-07-02 16:49:40 -0600502 err = memory_type_from_properties(demo,
503 mem_reqs.memoryTypeBits,
504 VK_MEMORY_PROPERTY_DEVICE_ONLY,
505 &mem_alloc.memoryTypeIndex);
506 assert(!err);
Chia-I Wu9ae87c92014-10-07 14:15:01 +0800507
Mark Lobodzinski23182612015-05-29 09:32:35 -0500508 /* allocate memory */
509 err = vkAllocMemory(demo->device, &mem_alloc, &demo->depth.mem);
510 assert(!err);
Chia-I Wu9ae87c92014-10-07 14:15:01 +0800511
Mark Lobodzinski23182612015-05-29 09:32:35 -0500512 /* bind memory */
Tony Barbourde4124d2015-07-03 10:33:54 -0600513 err = vkBindImageMemory(demo->device, demo->depth.image,
514 demo->depth.mem, 0);
Mark Lobodzinski23182612015-05-29 09:32:35 -0500515 assert(!err);
Chia-I Wu9ae87c92014-10-07 14:15:01 +0800516
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600517 demo_set_image_layout(demo, demo->depth.image,
malnasse4b8ba4d2015-06-03 17:28:38 -0400518 VK_IMAGE_ASPECT_DEPTH,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600519 VK_IMAGE_LAYOUT_UNDEFINED,
520 VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL);
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600521
Chia-I Wu9ae87c92014-10-07 14:15:01 +0800522 /* create image view */
523 view.image = demo->depth.image;
Chia-I Wuc278df82015-07-07 11:50:03 +0800524 err = vkCreateAttachmentView(demo->device, &view, &demo->depth.view);
Chia-I Wu9ae87c92014-10-07 14:15:01 +0800525 assert(!err);
Chia-I Wu9ae87c92014-10-07 14:15:01 +0800526}
527
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700528static void demo_prepare_texture_image(struct demo *demo,
529 const uint32_t *tex_colors,
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600530 struct texture_object *tex_obj,
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600531 VkImageTiling tiling,
Courtney Goeltzenleuchtercb67a322015-04-21 09:31:23 -0600532 VkImageUsageFlags usage,
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600533 VkFlags mem_props)
Chia-I Wub043fe32014-10-06 15:30:33 +0800534{
Tony Barbour8205d902015-04-16 15:59:00 -0600535 const VkFormat tex_format = VK_FORMAT_B8G8R8A8_UNORM;
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600536 const int32_t tex_width = 2;
537 const int32_t tex_height = 2;
Tony Barbour22a30862015-04-22 09:02:32 -0600538 VkResult U_ASSERT_ONLY err;
Chia-I Wub043fe32014-10-06 15:30:33 +0800539
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600540 tex_obj->tex_width = tex_width;
541 tex_obj->tex_height = tex_height;
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700542
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600543 const VkImageCreateInfo image_create_info = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600544 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
Courtney Goeltzenleuchter3226a6f2015-02-11 18:17:22 -0700545 .pNext = NULL,
Tony Barbour8205d902015-04-16 15:59:00 -0600546 .imageType = VK_IMAGE_TYPE_2D,
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700547 .format = tex_format,
548 .extent = { tex_width, tex_height, 1 },
549 .mipLevels = 1,
550 .arraySize = 1,
551 .samples = 1,
552 .tiling = tiling,
Courtney Goeltzenleuchtercb67a322015-04-21 09:31:23 -0600553 .usage = usage,
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700554 .flags = 0,
555 };
Courtney Goeltzenleuchterddcb6192015-04-14 18:48:46 -0600556 VkMemoryAllocInfo mem_alloc = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600557 .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOC_INFO,
Mark Lobodzinski97dcd042015-04-16 08:52:00 -0500558 .pNext = NULL,
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700559 .allocationSize = 0,
Mark Lobodzinski72346292015-07-02 16:49:40 -0600560 .memoryTypeIndex = 0,
Courtney Goeltzenleuchter3226a6f2015-02-11 18:17:22 -0700561 };
562
Mark Lobodzinski23182612015-05-29 09:32:35 -0500563 VkMemoryRequirements mem_reqs;
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700564
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600565 err = vkCreateImage(demo->device, &image_create_info,
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600566 &tex_obj->image);
Courtney Goeltzenleuchter3226a6f2015-02-11 18:17:22 -0700567 assert(!err);
568
Tony Barbourde4124d2015-07-03 10:33:54 -0600569 err = vkGetImageMemoryRequirements(demo->device, tex_obj->image, &mem_reqs);
Mark Lobodzinski72346292015-07-02 16:49:40 -0600570
571 mem_alloc.allocationSize = mem_reqs.size;
572 err = memory_type_from_properties(demo, mem_reqs.memoryTypeBits, mem_props, &mem_alloc.memoryTypeIndex);
573 assert(!err);
Courtney Goeltzenleuchter3226a6f2015-02-11 18:17:22 -0700574
Mark Lobodzinski23182612015-05-29 09:32:35 -0500575 /* allocate memory */
576 err = vkAllocMemory(demo->device, &mem_alloc, &tex_obj->mem);
577 assert(!err);
Courtney Goeltzenleuchter3226a6f2015-02-11 18:17:22 -0700578
Mark Lobodzinski23182612015-05-29 09:32:35 -0500579 /* bind memory */
Tony Barbourde4124d2015-07-03 10:33:54 -0600580 err = vkBindImageMemory(demo->device, tex_obj->image,
Mark Lobodzinski23182612015-05-29 09:32:35 -0500581 tex_obj->mem, 0);
582 assert(!err);
Courtney Goeltzenleuchter3226a6f2015-02-11 18:17:22 -0700583
Tony Barbour8205d902015-04-16 15:59:00 -0600584 if (mem_props & VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT) {
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600585 const VkImageSubresource subres = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600586 .aspect = VK_IMAGE_ASPECT_COLOR,
Courtney Goeltzenleuchter3226a6f2015-02-11 18:17:22 -0700587 .mipLevel = 0,
588 .arraySlice = 0,
589 };
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600590 VkSubresourceLayout layout;
Courtney Goeltzenleuchter3226a6f2015-02-11 18:17:22 -0700591 void *data;
592 int32_t x, y;
593
Tony Barbour426b9052015-06-24 16:06:58 -0600594 err = vkGetImageSubresourceLayout(demo->device, tex_obj->image, &subres, &layout);
595 assert(!err);
Courtney Goeltzenleuchter3226a6f2015-02-11 18:17:22 -0700596
Mark Lobodzinski23182612015-05-29 09:32:35 -0500597 err = vkMapMemory(demo->device, tex_obj->mem, 0, 0, 0, &data);
Courtney Goeltzenleuchter3226a6f2015-02-11 18:17:22 -0700598 assert(!err);
599
600 for (y = 0; y < tex_height; y++) {
601 uint32_t *row = (uint32_t *) ((char *) data + layout.rowPitch * y);
602 for (x = 0; x < tex_width; x++)
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700603 row[x] = tex_colors[(x & 1) ^ (y & 1)];
Courtney Goeltzenleuchter3226a6f2015-02-11 18:17:22 -0700604 }
605
Mark Lobodzinski23182612015-05-29 09:32:35 -0500606 err = vkUnmapMemory(demo->device, tex_obj->mem);
Courtney Goeltzenleuchter3226a6f2015-02-11 18:17:22 -0700607 assert(!err);
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700608 }
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600609
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600610 tex_obj->imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600611 demo_set_image_layout(demo, tex_obj->image,
malnasse4b8ba4d2015-06-03 17:28:38 -0400612 VK_IMAGE_ASPECT_COLOR,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600613 VK_IMAGE_LAYOUT_UNDEFINED,
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600614 tex_obj->imageLayout);
615 /* 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 -0700616}
617
Mark Lobodzinskicf26e072015-04-16 11:44:05 -0500618static void demo_destroy_texture_image(struct demo *demo, struct texture_object *tex_obj)
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700619{
620 /* clean up staging resources */
Tony Barbourde4124d2015-07-03 10:33:54 -0600621 vkDestroyImage(demo->device, tex_obj->image);
Courtney Goeltzenleuchtera063d9b2015-06-10 16:16:22 -0600622 vkFreeMemory(demo->device, tex_obj->mem);
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700623}
624
625static void demo_prepare_textures(struct demo *demo)
626{
Tony Barbour8205d902015-04-16 15:59:00 -0600627 const VkFormat tex_format = VK_FORMAT_B8G8R8A8_UNORM;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600628 VkFormatProperties props;
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700629 const uint32_t tex_colors[DEMO_TEXTURE_COUNT][2] = {
630 { 0xffff0000, 0xff00ff00 },
631 };
Tony Barbour22a30862015-04-22 09:02:32 -0600632 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700633 uint32_t i;
634
Courtney Goeltzenleuchterab36aa62015-07-12 12:40:29 -0600635 err = vkGetPhysicalDeviceFormatProperties(demo->gpu, tex_format, &props);
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700636 assert(!err);
637
638 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Tony Barbour8205d902015-04-16 15:59:00 -0600639 if ((props.linearTilingFeatures & VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT) && !demo->use_staging_buffer) {
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700640 /* Device can texture using linear textures */
641 demo_prepare_texture_image(demo, tex_colors[i], &demo->textures[i],
Courtney Goeltzenleuchtercb67a322015-04-21 09:31:23 -0600642 VK_IMAGE_TILING_LINEAR, VK_IMAGE_USAGE_SAMPLED_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT);
Tony Barbour8205d902015-04-16 15:59:00 -0600643 } else if (props.optimalTilingFeatures & VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT){
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700644 /* Must use staging buffer to copy linear texture to optimized */
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600645 struct texture_object staging_texture;
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700646
647 memset(&staging_texture, 0, sizeof(staging_texture));
648 demo_prepare_texture_image(demo, tex_colors[i], &staging_texture,
Courtney Goeltzenleuchtercb67a322015-04-21 09:31:23 -0600649 VK_IMAGE_TILING_LINEAR, VK_IMAGE_USAGE_TRANSFER_SOURCE_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT);
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700650
651 demo_prepare_texture_image(demo, tex_colors[i], &demo->textures[i],
Courtney Goeltzenleuchtercb67a322015-04-21 09:31:23 -0600652 VK_IMAGE_TILING_OPTIMAL,
653 (VK_IMAGE_USAGE_TRANSFER_DESTINATION_BIT | VK_IMAGE_USAGE_SAMPLED_BIT),
654 VK_MEMORY_PROPERTY_DEVICE_ONLY);
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700655
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600656 demo_set_image_layout(demo, staging_texture.image,
malnasse4b8ba4d2015-06-03 17:28:38 -0400657 VK_IMAGE_ASPECT_COLOR,
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600658 staging_texture.imageLayout,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600659 VK_IMAGE_LAYOUT_TRANSFER_SOURCE_OPTIMAL);
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700660
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600661 demo_set_image_layout(demo, demo->textures[i].image,
malnasse4b8ba4d2015-06-03 17:28:38 -0400662 VK_IMAGE_ASPECT_COLOR,
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600663 demo->textures[i].imageLayout,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600664 VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL);
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700665
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600666 VkImageCopy copy_region = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600667 .srcSubresource = { VK_IMAGE_ASPECT_COLOR, 0, 0 },
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700668 .srcOffset = { 0, 0, 0 },
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600669 .destSubresource = { VK_IMAGE_ASPECT_COLOR, 0, 0 },
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700670 .destOffset = { 0, 0, 0 },
671 .extent = { staging_texture.tex_width, staging_texture.tex_height, 1 },
672 };
Courtney Goeltzenleuchter633f9c52015-04-30 10:58:33 -0600673 vkCmdCopyImage(demo->setup_cmd,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600674 staging_texture.image, VK_IMAGE_LAYOUT_TRANSFER_SOURCE_OPTIMAL,
675 demo->textures[i].image, VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL,
Courtney Goeltzenleuchter51cbf302015-03-25 11:25:10 -0600676 1, &copy_region);
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700677
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600678 demo_set_image_layout(demo, demo->textures[i].image,
malnasse4b8ba4d2015-06-03 17:28:38 -0400679 VK_IMAGE_ASPECT_COLOR,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600680 VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL,
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600681 demo->textures[i].imageLayout);
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700682
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600683 demo_flush_init_cmd(demo);
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700684
Courtney Goeltzenleuchter876629f2015-04-21 09:30:03 -0600685 demo_destroy_texture_image(demo, &staging_texture);
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700686 } else {
Tony Barbour8205d902015-04-16 15:59:00 -0600687 /* Can't support VK_FORMAT_B8G8R8A8_UNORM !? */
Piers Daniell886be472015-02-23 16:23:13 -0700688 assert(!"No support for B8G8R8A8_UNORM as texture image format");
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700689 }
Courtney Goeltzenleuchter3226a6f2015-02-11 18:17:22 -0700690
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600691 const VkSamplerCreateInfo sampler = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600692 .sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO,
Chia-I Wub043fe32014-10-06 15:30:33 +0800693 .pNext = NULL,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600694 .magFilter = VK_TEX_FILTER_NEAREST,
695 .minFilter = VK_TEX_FILTER_NEAREST,
Tony Barbour8205d902015-04-16 15:59:00 -0600696 .mipMode = VK_TEX_MIPMAP_MODE_BASE,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600697 .addressU = VK_TEX_ADDRESS_WRAP,
698 .addressV = VK_TEX_ADDRESS_WRAP,
699 .addressW = VK_TEX_ADDRESS_WRAP,
Chia-I Wub043fe32014-10-06 15:30:33 +0800700 .mipLodBias = 0.0f,
Courtney Goeltzenleuchterbc9c8162015-02-13 18:20:24 -0700701 .maxAnisotropy = 1,
Tony Barbour8205d902015-04-16 15:59:00 -0600702 .compareOp = VK_COMPARE_OP_NEVER,
Chia-I Wub043fe32014-10-06 15:30:33 +0800703 .minLod = 0.0f,
704 .maxLod = 0.0f,
Tony Barbour2c4e7c72015-06-25 16:56:44 -0600705 .borderColor = VK_BORDER_COLOR_FLOAT_OPAQUE_WHITE,
Chia-I Wub043fe32014-10-06 15:30:33 +0800706 };
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600707 VkImageViewCreateInfo view = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600708 .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
Chia-I Wub043fe32014-10-06 15:30:33 +0800709 .pNext = NULL,
Tony Barbourde4124d2015-07-03 10:33:54 -0600710 .image.handle = VK_NULL_HANDLE,
Tony Barbour8205d902015-04-16 15:59:00 -0600711 .viewType = VK_IMAGE_VIEW_TYPE_2D,
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700712 .format = tex_format,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600713 .channels = { VK_CHANNEL_SWIZZLE_R,
714 VK_CHANNEL_SWIZZLE_G,
715 VK_CHANNEL_SWIZZLE_B,
716 VK_CHANNEL_SWIZZLE_A, },
717 .subresourceRange = { VK_IMAGE_ASPECT_COLOR, 0, 1, 0, 1 },
Chia-I Wub043fe32014-10-06 15:30:33 +0800718 };
Jon Ashburna9ae3832015-01-16 09:37:43 -0700719
Chia-I Wub043fe32014-10-06 15:30:33 +0800720 /* create sampler */
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600721 err = vkCreateSampler(demo->device, &sampler,
Chia-I Wub043fe32014-10-06 15:30:33 +0800722 &demo->textures[i].sampler);
723 assert(!err);
724
Chia-I Wub043fe32014-10-06 15:30:33 +0800725 /* create image view */
726 view.image = demo->textures[i].image;
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600727 err = vkCreateImageView(demo->device, &view,
Courtney Goeltzenleuchter3226a6f2015-02-11 18:17:22 -0700728 &demo->textures[i].view);
Chia-I Wub043fe32014-10-06 15:30:33 +0800729 assert(!err);
Chia-I Wub043fe32014-10-06 15:30:33 +0800730 }
731}
732
Chia-I Wu99621bc2014-10-08 11:52:22 +0800733static void demo_prepare_vertices(struct demo *demo)
734{
735 const float vb[3][5] = {
736 /* position texcoord */
Chia-I Wue2504cb2015-04-22 14:20:52 +0800737 { -1.0f, -1.0f, 0.2f, 0.0f, 0.0f },
738 { 1.0f, -1.0f, 0.25f, 1.0f, 0.0f },
Chia-I Wu99621bc2014-10-08 11:52:22 +0800739 { 0.0f, 1.0f, 1.0f, 0.5f, 1.0f },
740 };
Courtney Goeltzenleuchterddcb6192015-04-14 18:48:46 -0600741 const VkBufferCreateInfo buf_info = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600742 .sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO,
Chia-I Wu714df452015-01-01 07:55:04 +0800743 .pNext = NULL,
744 .size = sizeof(vb),
Courtney Goeltzenleuchterad870812015-04-15 15:29:59 -0600745 .usage = VK_BUFFER_USAGE_VERTEX_BUFFER_BIT,
Chia-I Wu714df452015-01-01 07:55:04 +0800746 .flags = 0,
747 };
Courtney Goeltzenleuchterddcb6192015-04-14 18:48:46 -0600748 VkMemoryAllocInfo mem_alloc = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600749 .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOC_INFO,
Mark Lobodzinski97dcd042015-04-16 08:52:00 -0500750 .pNext = NULL,
Chia-I Wu714df452015-01-01 07:55:04 +0800751 .allocationSize = 0,
Mark Lobodzinski72346292015-07-02 16:49:40 -0600752 .memoryTypeIndex = 0,
Chia-I Wu99621bc2014-10-08 11:52:22 +0800753 };
Mark Lobodzinski23182612015-05-29 09:32:35 -0500754 VkMemoryRequirements mem_reqs;
Tony Barbour22a30862015-04-22 09:02:32 -0600755 VkResult U_ASSERT_ONLY err;
Chia-I Wu99621bc2014-10-08 11:52:22 +0800756 void *data;
757
758 memset(&demo->vertices, 0, sizeof(demo->vertices));
759
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600760 err = vkCreateBuffer(demo->device, &buf_info, &demo->vertices.buf);
Chia-I Wu714df452015-01-01 07:55:04 +0800761 assert(!err);
762
Tony Barbourde4124d2015-07-03 10:33:54 -0600763 err = vkGetBufferMemoryRequirements(demo->device,
764 demo->vertices.buf, &mem_reqs);
Chia-I Wu714df452015-01-01 07:55:04 +0800765
Mark Lobodzinski72346292015-07-02 16:49:40 -0600766 mem_alloc.allocationSize = mem_reqs.size;
767 err = memory_type_from_properties(demo,
768 mem_reqs.memoryTypeBits,
769 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT,
770 &mem_alloc.memoryTypeIndex);
771 assert(!err);
Chia-I Wu99621bc2014-10-08 11:52:22 +0800772
Mark Lobodzinski23182612015-05-29 09:32:35 -0500773 err = vkAllocMemory(demo->device, &mem_alloc, &demo->vertices.mem);
774 assert(!err);
Chia-I Wu99621bc2014-10-08 11:52:22 +0800775
Mark Lobodzinski23182612015-05-29 09:32:35 -0500776 err = vkMapMemory(demo->device, demo->vertices.mem, 0, 0, 0, &data);
777 assert(!err);
Chia-I Wu99621bc2014-10-08 11:52:22 +0800778
Mark Lobodzinski23182612015-05-29 09:32:35 -0500779 memcpy(data, vb, sizeof(vb));
Chia-I Wu99621bc2014-10-08 11:52:22 +0800780
Mark Lobodzinski23182612015-05-29 09:32:35 -0500781 err = vkUnmapMemory(demo->device, demo->vertices.mem);
782 assert(!err);
783
Tony Barbourde4124d2015-07-03 10:33:54 -0600784 err = vkBindBufferMemory(demo->device, demo->vertices.buf,
Mark Lobodzinski23182612015-05-29 09:32:35 -0500785 demo->vertices.mem, 0);
786 assert(!err);
Chia-I Wu714df452015-01-01 07:55:04 +0800787
Mark Lobodzinski0e0fb5c2015-06-23 15:11:57 -0600788 demo->vertices.vi.sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO;
Chia-I Wu8d29d022014-10-08 12:14:39 +0800789 demo->vertices.vi.pNext = NULL;
790 demo->vertices.vi.bindingCount = 1;
791 demo->vertices.vi.pVertexBindingDescriptions = demo->vertices.vi_bindings;
792 demo->vertices.vi.attributeCount = 2;
793 demo->vertices.vi.pVertexAttributeDescriptions = demo->vertices.vi_attrs;
794
Courtney Goeltzenleuchterf5cdad02015-03-31 16:36:30 -0600795 demo->vertices.vi_bindings[0].binding = VERTEX_BUFFER_BIND_ID;
Chia-I Wu8d29d022014-10-08 12:14:39 +0800796 demo->vertices.vi_bindings[0].strideInBytes = sizeof(vb[0]);
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600797 demo->vertices.vi_bindings[0].stepRate = VK_VERTEX_INPUT_STEP_RATE_VERTEX;
Chia-I Wu8d29d022014-10-08 12:14:39 +0800798
Courtney Goeltzenleuchterf5cdad02015-03-31 16:36:30 -0600799 demo->vertices.vi_attrs[0].binding = VERTEX_BUFFER_BIND_ID;
800 demo->vertices.vi_attrs[0].location = 0;
Tony Barbour8205d902015-04-16 15:59:00 -0600801 demo->vertices.vi_attrs[0].format = VK_FORMAT_R32G32B32_SFLOAT;
Chia-I Wu8d29d022014-10-08 12:14:39 +0800802 demo->vertices.vi_attrs[0].offsetInBytes = 0;
803
Courtney Goeltzenleuchterf5cdad02015-03-31 16:36:30 -0600804 demo->vertices.vi_attrs[1].binding = VERTEX_BUFFER_BIND_ID;
805 demo->vertices.vi_attrs[1].location = 1;
Tony Barbour8205d902015-04-16 15:59:00 -0600806 demo->vertices.vi_attrs[1].format = VK_FORMAT_R32G32_SFLOAT;
Chia-I Wu8d29d022014-10-08 12:14:39 +0800807 demo->vertices.vi_attrs[1].offsetInBytes = sizeof(float) * 3;
Chia-I Wu99621bc2014-10-08 11:52:22 +0800808}
809
Chia-I Wuf8385062015-01-04 16:27:24 +0800810static void demo_prepare_descriptor_layout(struct demo *demo)
Chia-I Wub043fe32014-10-06 15:30:33 +0800811{
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600812 const VkDescriptorSetLayoutBinding layout_binding = {
Courtney Goeltzenleuchterad870812015-04-15 15:29:59 -0600813 .descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
Chia-I Wud3114a22015-05-25 16:22:52 +0800814 .arraySize = DEMO_TEXTURE_COUNT,
Tony Barbour8205d902015-04-16 15:59:00 -0600815 .stageFlags = VK_SHADER_STAGE_FRAGMENT_BIT,
Chia-I Wu310eece2015-03-27 12:56:09 +0800816 .pImmutableSamplers = NULL,
Chia-I Wub043fe32014-10-06 15:30:33 +0800817 };
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600818 const VkDescriptorSetLayoutCreateInfo descriptor_layout = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600819 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO,
Chia-I Wufc9d9132015-03-26 15:04:41 +0800820 .pNext = NULL,
821 .count = 1,
822 .pBinding = &layout_binding,
823 };
Tony Barbour22a30862015-04-22 09:02:32 -0600824 VkResult U_ASSERT_ONLY err;
Chia-I Wub043fe32014-10-06 15:30:33 +0800825
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600826 err = vkCreateDescriptorSetLayout(demo->device,
Chia-I Wu7732cb22015-03-26 15:27:55 +0800827 &descriptor_layout, &demo->desc_layout);
828 assert(!err);
829
Mark Lobodzinski556f7212015-04-17 14:11:39 -0500830 const VkPipelineLayoutCreateInfo pPipelineLayoutCreateInfo = {
831 .sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO,
832 .pNext = NULL,
833 .descriptorSetCount = 1,
834 .pSetLayouts = &demo->desc_layout,
835 };
836
837 err = vkCreatePipelineLayout(demo->device,
838 &pPipelineLayoutCreateInfo,
839 &demo->pipeline_layout);
Chia-I Wub043fe32014-10-06 15:30:33 +0800840 assert(!err);
Chia-I Wub043fe32014-10-06 15:30:33 +0800841}
842
Chia-I Wu76cd4222015-07-08 13:34:24 +0800843static void demo_prepare_render_pass(struct demo *demo)
844{
Chia-I Wuc278df82015-07-07 11:50:03 +0800845 const VkAttachmentDescription attachments[2] = {
846 [0] = {
847 .sType = VK_STRUCTURE_TYPE_ATTACHMENT_DESCRIPTION,
848 .pNext = NULL,
849 .format = demo->format,
850 .samples = 1,
851 .loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR,
852 .storeOp = VK_ATTACHMENT_STORE_OP_STORE,
853 .stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE,
854 .stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE,
855 .initialLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
856 .finalLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
857 },
858 [1] = {
859 .sType = VK_STRUCTURE_TYPE_ATTACHMENT_DESCRIPTION,
860 .pNext = NULL,
861 .format = demo->depth.format,
862 .samples = 1,
863 .loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR,
864 .storeOp = VK_ATTACHMENT_STORE_OP_DONT_CARE,
865 .stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE,
866 .stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE,
867 .initialLayout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
868 .finalLayout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
869 },
Chia-I Wu76cd4222015-07-08 13:34:24 +0800870 };
Chia-I Wuc278df82015-07-07 11:50:03 +0800871 const VkAttachmentReference color_reference = {
872 .attachment = 0,
873 .layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
874 };
875 const VkSubpassDescription subpass = {
876 .sType = VK_STRUCTURE_TYPE_SUBPASS_DESCRIPTION,
877 .pNext = NULL,
878 .pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS,
879 .flags = 0,
880 .inputCount = 0,
881 .inputAttachments = NULL,
882 .colorCount = 1,
883 .colorAttachments = &color_reference,
884 .resolveAttachments = NULL,
885 .depthStencilAttachment = {
886 .attachment = 1,
887 .layout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
888 },
889 .preserveCount = 0,
890 .preserveAttachments = NULL,
891 };
Chia-I Wu76cd4222015-07-08 13:34:24 +0800892 const VkRenderPassCreateInfo rp_info = {
893 .sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO,
894 .pNext = NULL,
Chia-I Wuc278df82015-07-07 11:50:03 +0800895 .attachmentCount = 2,
896 .pAttachments = attachments,
897 .subpassCount = 1,
898 .pSubpasses = &subpass,
899 .dependencyCount = 0,
900 .pDependencies = NULL,
Chia-I Wu76cd4222015-07-08 13:34:24 +0800901 };
Chia-I Wuc278df82015-07-07 11:50:03 +0800902 VkResult U_ASSERT_ONLY err;
Chia-I Wu76cd4222015-07-08 13:34:24 +0800903
904 err = vkCreateRenderPass(demo->device, &rp_info, &demo->render_pass);
905 assert(!err);
906}
907
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600908static VkShader demo_prepare_shader(struct demo *demo,
Tony Barbour8205d902015-04-16 15:59:00 -0600909 VkShaderStage stage,
Chia-I Wuc19795a2014-09-13 11:12:55 +0800910 const void *code,
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600911 size_t size)
Chia-I Wuc19795a2014-09-13 11:12:55 +0800912{
Courtney Goeltzenleuchter2d034fd2015-06-28 13:01:17 -0600913 VkShaderModuleCreateInfo moduleCreateInfo;
914 VkShaderCreateInfo shaderCreateInfo;
915 VkShaderModule shaderModule;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600916 VkShader shader;
917 VkResult err;
Chia-I Wuc19795a2014-09-13 11:12:55 +0800918
Courtney Goeltzenleuchter2d034fd2015-06-28 13:01:17 -0600919
920 moduleCreateInfo.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
921 moduleCreateInfo.pNext = NULL;
922
923 shaderCreateInfo.sType = VK_STRUCTURE_TYPE_SHADER_CREATE_INFO;
924 shaderCreateInfo.pNext = NULL;
Courtney Goeltzenleuchter3f2606d2014-10-13 17:51:58 -0600925
Cody Northrop75db0322015-05-28 11:27:16 -0600926 if (!demo->use_glsl) {
Courtney Goeltzenleuchter2d034fd2015-06-28 13:01:17 -0600927 moduleCreateInfo.codeSize = size;
928 moduleCreateInfo.pCode = code;
929 moduleCreateInfo.flags = 0;
930 err = vkCreateShaderModule(demo->device, &moduleCreateInfo, &shaderModule);
931 if (err) {
932 free((void *) moduleCreateInfo.pCode);
933 }
Courtney Goeltzenleuchter3f2606d2014-10-13 17:51:58 -0600934
Courtney Goeltzenleuchter2d034fd2015-06-28 13:01:17 -0600935 shaderCreateInfo.flags = 0;
936 shaderCreateInfo.module = shaderModule;
937 err = vkCreateShader(demo->device, &shaderCreateInfo, &shader);
Cody Northrop75db0322015-05-28 11:27:16 -0600938 } else {
939 // Create fake SPV structure to feed GLSL
940 // to the driver "under the covers"
Courtney Goeltzenleuchter2d034fd2015-06-28 13:01:17 -0600941 moduleCreateInfo.codeSize = 3 * sizeof(uint32_t) + size + 1;
942 moduleCreateInfo.pCode = malloc(moduleCreateInfo.codeSize);
943 moduleCreateInfo.flags = 0;
Courtney Goeltzenleuchter3f2606d2014-10-13 17:51:58 -0600944
Cody Northrop75db0322015-05-28 11:27:16 -0600945 /* try version 0 first: VkShaderStage followed by GLSL */
Courtney Goeltzenleuchter2d034fd2015-06-28 13:01:17 -0600946 ((uint32_t *) moduleCreateInfo.pCode)[0] = ICD_SPV_MAGIC;
947 ((uint32_t *) moduleCreateInfo.pCode)[1] = 0;
948 ((uint32_t *) moduleCreateInfo.pCode)[2] = stage;
949 memcpy(((uint32_t *) moduleCreateInfo.pCode + 3), code, size + 1);
Cody Northrop75db0322015-05-28 11:27:16 -0600950
Courtney Goeltzenleuchter2d034fd2015-06-28 13:01:17 -0600951 err = vkCreateShaderModule(demo->device, &moduleCreateInfo, &shaderModule);
Cody Northrop75db0322015-05-28 11:27:16 -0600952 if (err) {
Courtney Goeltzenleuchter2d034fd2015-06-28 13:01:17 -0600953 free((void *) moduleCreateInfo.pCode);
Cody Northrop75db0322015-05-28 11:27:16 -0600954 }
Chia-I Wuc19795a2014-09-13 11:12:55 +0800955
Courtney Goeltzenleuchter2d034fd2015-06-28 13:01:17 -0600956 shaderCreateInfo.flags = 0;
957 shaderCreateInfo.module = shaderModule;
958 err = vkCreateShader(demo->device, &shaderCreateInfo, &shader);
959 }
Chia-I Wuc19795a2014-09-13 11:12:55 +0800960 return shader;
961}
962
Cody Northrop75db0322015-05-28 11:27:16 -0600963char *demo_read_spv(const char *filename, size_t *psize)
964{
965 long int size;
966 void *shader_code;
967
968 FILE *fp = fopen(filename, "rb");
969 if (!fp) return NULL;
970
971 fseek(fp, 0L, SEEK_END);
972 size = ftell(fp);
973
974 fseek(fp, 0L, SEEK_SET);
975
976 shader_code = malloc(size);
977 fread(shader_code, size, 1, fp);
978
979 *psize = size;
980
981 return shader_code;
982}
983
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600984static VkShader demo_prepare_vs(struct demo *demo)
Chia-I Wuc19795a2014-09-13 11:12:55 +0800985{
Cody Northrop75db0322015-05-28 11:27:16 -0600986 if (!demo->use_glsl) {
987 void *vertShaderCode;
988 size_t size;
989
990 vertShaderCode = demo_read_spv("tri-vert.spv", &size);
991
992 return demo_prepare_shader(demo, VK_SHADER_STAGE_VERTEX,
993 vertShaderCode, size);
994 } else {
995 static const char *vertShaderText =
Mark Lobodzinskiba4d2f02015-04-06 15:24:40 -0500996 "#version 140\n"
Courtney Goeltzenleuchterf5cdad02015-03-31 16:36:30 -0600997 "#extension GL_ARB_separate_shader_objects : enable\n"
998 "#extension GL_ARB_shading_language_420pack : enable\n"
999 "layout (location = 0) in vec4 pos;\n"
1000 "layout (location = 1) in vec2 attr;\n"
Chia-I Wuf5caeb02014-10-25 12:11:27 +08001001 "out vec2 texcoord;\n"
Courtney Goeltzenleuchter3f2606d2014-10-13 17:51:58 -06001002 "void main() {\n"
Chia-I Wuf5caeb02014-10-25 12:11:27 +08001003 " texcoord = attr;\n"
1004 " gl_Position = pos;\n"
Courtney Goeltzenleuchter3f2606d2014-10-13 17:51:58 -06001005 "}\n";
Courtney Goeltzenleuchteref7301b2014-09-17 13:17:12 -06001006
Cody Northrop75db0322015-05-28 11:27:16 -06001007 return demo_prepare_shader(demo, VK_SHADER_STAGE_VERTEX,
1008 (const void *) vertShaderText,
1009 strlen(vertShaderText));
1010 }
Chia-I Wuc19795a2014-09-13 11:12:55 +08001011}
1012
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001013static VkShader demo_prepare_fs(struct demo *demo)
Chia-I Wuc19795a2014-09-13 11:12:55 +08001014{
Cody Northrop75db0322015-05-28 11:27:16 -06001015 if (!demo->use_glsl) {
1016 void *fragShaderCode;
1017 size_t size;
Courtney Goeltzenleuchteref7301b2014-09-17 13:17:12 -06001018
Cody Northrop75db0322015-05-28 11:27:16 -06001019 fragShaderCode = demo_read_spv("tri-frag.spv", &size);
1020
1021 return demo_prepare_shader(demo, VK_SHADER_STAGE_FRAGMENT,
1022 fragShaderCode, size);
1023 } else {
1024 static const char *fragShaderText =
1025 "#version 140\n"
1026 "#extension GL_ARB_separate_shader_objects : enable\n"
1027 "#extension GL_ARB_shading_language_420pack : enable\n"
1028 "layout (binding = 0) uniform sampler2D tex;\n"
1029 "layout (location = 0) in vec2 texcoord;\n"
1030 "layout (location = 0) out vec4 uFragColor;\n"
1031 "void main() {\n"
1032 " uFragColor = texture(tex, texcoord);\n"
1033 "}\n";
1034
1035 return demo_prepare_shader(demo, VK_SHADER_STAGE_FRAGMENT,
1036 (const void *) fragShaderText,
1037 strlen(fragShaderText));
1038 }
Chia-I Wuc19795a2014-09-13 11:12:55 +08001039}
1040
1041static void demo_prepare_pipeline(struct demo *demo)
1042{
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001043 VkGraphicsPipelineCreateInfo pipeline;
Jon Ashburn0d60d272015-07-09 15:02:25 -06001044 VkPipelineCacheCreateInfo pipelineCache;
Mark Lobodzinski0e0fb5c2015-06-23 15:11:57 -06001045
Tony Barboure307f582015-07-10 15:29:03 -06001046 VkPipelineVertexInputStateCreateInfo vi;
1047 VkPipelineInputAssemblyStateCreateInfo ia;
1048 VkPipelineRasterStateCreateInfo rs;
1049 VkPipelineColorBlendStateCreateInfo cb;
1050 VkPipelineDepthStencilStateCreateInfo ds;
1051 VkPipelineViewportStateCreateInfo vp;
1052 VkPipelineMultisampleStateCreateInfo ms;
Mark Lobodzinski0e0fb5c2015-06-23 15:11:57 -06001053
Tony Barbour22a30862015-04-22 09:02:32 -06001054 VkResult U_ASSERT_ONLY err;
Chia-I Wuc19795a2014-09-13 11:12:55 +08001055
1056 memset(&pipeline, 0, sizeof(pipeline));
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001057 pipeline.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
Mark Lobodzinski556f7212015-04-17 14:11:39 -05001058 pipeline.layout = demo->pipeline_layout;
Chia-I Wuc19795a2014-09-13 11:12:55 +08001059
Chia-I Wu8d29d022014-10-08 12:14:39 +08001060 vi = demo->vertices.vi;
1061
Chia-I Wuc19795a2014-09-13 11:12:55 +08001062 memset(&ia, 0, sizeof(ia));
Tony Barboure307f582015-07-10 15:29:03 -06001063 ia.sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO;
Tony Barbour8205d902015-04-16 15:59:00 -06001064 ia.topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
Chia-I Wuc19795a2014-09-13 11:12:55 +08001065
1066 memset(&rs, 0, sizeof(rs));
Tony Barboure307f582015-07-10 15:29:03 -06001067 rs.sType = VK_STRUCTURE_TYPE_PIPELINE_RASTER_STATE_CREATE_INFO;
Tony Barbour8205d902015-04-16 15:59:00 -06001068 rs.fillMode = VK_FILL_MODE_SOLID;
Chia-I Wuc414ba82015-04-22 15:44:24 +08001069 rs.cullMode = VK_CULL_MODE_BACK;
1070 rs.frontFace = VK_FRONT_FACE_CW;
Chia-I Wue2504cb2015-04-22 14:20:52 +08001071 rs.depthClipEnable = VK_TRUE;
Chia-I Wuc19795a2014-09-13 11:12:55 +08001072
1073 memset(&cb, 0, sizeof(cb));
Tony Barboure307f582015-07-10 15:29:03 -06001074 cb.sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO;
1075 VkPipelineColorBlendAttachmentState att_state[1];
Tony Barbourfa6cac72015-01-16 14:27:35 -07001076 memset(att_state, 0, sizeof(att_state));
Tony Barbourfa6cac72015-01-16 14:27:35 -07001077 att_state[0].channelWriteMask = 0xf;
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001078 att_state[0].blendEnable = VK_FALSE;
Tony Barbourfa6cac72015-01-16 14:27:35 -07001079 cb.attachmentCount = 1;
1080 cb.pAttachments = att_state;
Chia-I Wuc19795a2014-09-13 11:12:55 +08001081
Tony Barbourfa6cac72015-01-16 14:27:35 -07001082 memset(&vp, 0, sizeof(vp));
Tony Barboure307f582015-07-10 15:29:03 -06001083 vp.sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO;
Tony Barbour8205d902015-04-16 15:59:00 -06001084 vp.viewportCount = 1;
Tony Barbourfa6cac72015-01-16 14:27:35 -07001085
1086 memset(&ds, 0, sizeof(ds));
Tony Barboure307f582015-07-10 15:29:03 -06001087 ds.sType = VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO;
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001088 ds.depthTestEnable = VK_TRUE;
1089 ds.depthWriteEnable = VK_TRUE;
Tony Barbour8205d902015-04-16 15:59:00 -06001090 ds.depthCompareOp = VK_COMPARE_OP_LESS_EQUAL;
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001091 ds.depthBoundsEnable = VK_FALSE;
1092 ds.back.stencilFailOp = VK_STENCIL_OP_KEEP;
1093 ds.back.stencilPassOp = VK_STENCIL_OP_KEEP;
Tony Barbour8205d902015-04-16 15:59:00 -06001094 ds.back.stencilCompareOp = VK_COMPARE_OP_ALWAYS;
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001095 ds.stencilTestEnable = VK_FALSE;
Tony Barbourfa6cac72015-01-16 14:27:35 -07001096 ds.front = ds.back;
Chia-I Wuc19795a2014-09-13 11:12:55 +08001097
Tony Barbourfa6cac72015-01-16 14:27:35 -07001098 memset(&ms, 0, sizeof(ms));
Tony Barboure307f582015-07-10 15:29:03 -06001099 ms.sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO;
Tony Barbourfa6cac72015-01-16 14:27:35 -07001100 ms.sampleMask = 1;
Tony Barboure094edf2015-06-26 10:18:34 -06001101 ms.rasterSamples = 1;
Chia-I Wub043fe32014-10-06 15:30:33 +08001102
Mark Lobodzinski0e0fb5c2015-06-23 15:11:57 -06001103 // Two stages: vs and fs
1104 pipeline.stageCount = 2;
1105 VkPipelineShaderStageCreateInfo shaderStages[2];
1106 memset(&shaderStages, 0, 2 * sizeof(VkPipelineShaderStageCreateInfo));
1107
1108 shaderStages[0].sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
1109 shaderStages[0].stage = VK_SHADER_STAGE_VERTEX;
1110 shaderStages[0].shader = demo_prepare_vs(demo);
Mark Lobodzinski0e0fb5c2015-06-23 15:11:57 -06001111
1112 shaderStages[1].sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
1113 shaderStages[1].stage = VK_SHADER_STAGE_FRAGMENT;
1114 shaderStages[1].shader = demo_prepare_fs(demo);
1115
Tony Barboure307f582015-07-10 15:29:03 -06001116 pipeline.pVertexInputState = &vi;
1117 pipeline.pInputAssemblyState = &ia;
1118 pipeline.pRasterState = &rs;
1119 pipeline.pColorBlendState = &cb;
1120 pipeline.pMultisampleState = &ms;
1121 pipeline.pViewportState = &vp;
1122 pipeline.pDepthStencilState = &ds;
1123 pipeline.pStages = shaderStages;
Chia-I Wuc19795a2014-09-13 11:12:55 +08001124
Jon Ashburn0d60d272015-07-09 15:02:25 -06001125 memset(&pipelineCache, 0, sizeof(pipelineCache));
1126 pipelineCache.sType = VK_STRUCTURE_TYPE_PIPELINE_CACHE_CREATE_INFO;
1127
1128 err = vkCreatePipelineCache(demo->device, &pipelineCache, &demo->pipelineCache);
1129 assert(!err);
1130 err = vkCreateGraphicsPipelines(demo->device, demo->pipelineCache, 1, &pipeline, &demo->pipeline);
Chia-I Wuc19795a2014-09-13 11:12:55 +08001131 assert(!err);
1132
Mark Lobodzinski0e0fb5c2015-06-23 15:11:57 -06001133 for (uint32_t i = 0; i < pipeline.stageCount; i++) {
Tony Barbourde4124d2015-07-03 10:33:54 -06001134 vkDestroyShader(demo->device, shaderStages[i].shader);
Mark Lobodzinski0e0fb5c2015-06-23 15:11:57 -06001135 }
Chia-I Wuc19795a2014-09-13 11:12:55 +08001136}
1137
1138static void demo_prepare_dynamic_states(struct demo *demo)
1139{
Tony Barbourde4124d2015-07-03 10:33:54 -06001140 VkDynamicViewportStateCreateInfo viewport_create;
1141 VkDynamicRasterStateCreateInfo raster;
1142 VkDynamicColorBlendStateCreateInfo color_blend;
1143 VkDynamicDepthStencilStateCreateInfo depth_stencil;
Tony Barbour22a30862015-04-22 09:02:32 -06001144 VkResult U_ASSERT_ONLY err;
Chia-I Wuc19795a2014-09-13 11:12:55 +08001145
Tony Barbourfa6cac72015-01-16 14:27:35 -07001146 memset(&viewport_create, 0, sizeof(viewport_create));
Tony Barbourde4124d2015-07-03 10:33:54 -06001147 viewport_create.sType = VK_STRUCTURE_TYPE_DYNAMIC_VIEWPORT_STATE_CREATE_INFO;
Courtney Goeltzenleuchterc6e32f92015-02-11 14:13:34 -07001148 viewport_create.viewportAndScissorCount = 1;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001149 VkViewport viewport;
Chia-I Wuc19795a2014-09-13 11:12:55 +08001150 memset(&viewport, 0, sizeof(viewport));
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -06001151 viewport.height = (float) demo->height;
Courtney Goeltzenleuchterc6e32f92015-02-11 14:13:34 -07001152 viewport.width = (float) demo->width;
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -06001153 viewport.minDepth = (float) 0.0f;
1154 viewport.maxDepth = (float) 1.0f;
Piers Daniell886be472015-02-23 16:23:13 -07001155 viewport_create.pViewports = &viewport;
Chris Forbes2951d7d2015-06-22 17:21:59 +12001156 VkRect2D scissor;
Piers Daniell886be472015-02-23 16:23:13 -07001157 memset(&scissor, 0, sizeof(scissor));
Courtney Goeltzenleuchterc6e32f92015-02-11 14:13:34 -07001158 scissor.extent.width = demo->width;
1159 scissor.extent.height = demo->height;
1160 scissor.offset.x = 0;
1161 scissor.offset.y = 0;
Courtney Goeltzenleuchterc6e32f92015-02-11 14:13:34 -07001162 viewport_create.pScissors = &scissor;
Chia-I Wuc19795a2014-09-13 11:12:55 +08001163
1164 memset(&raster, 0, sizeof(raster));
Tony Barbourde4124d2015-07-03 10:33:54 -06001165 raster.sType = VK_STRUCTURE_TYPE_DYNAMIC_RASTER_STATE_CREATE_INFO;
Piers Daniell886be472015-02-23 16:23:13 -07001166 raster.lineWidth = 1.0;
Chia-I Wuc19795a2014-09-13 11:12:55 +08001167
1168 memset(&color_blend, 0, sizeof(color_blend));
Tony Barbourde4124d2015-07-03 10:33:54 -06001169 color_blend.sType = VK_STRUCTURE_TYPE_DYNAMIC_COLOR_BLEND_STATE_CREATE_INFO;
Piers Daniell886be472015-02-23 16:23:13 -07001170 color_blend.blendConst[0] = 1.0f;
1171 color_blend.blendConst[1] = 1.0f;
1172 color_blend.blendConst[2] = 1.0f;
1173 color_blend.blendConst[3] = 1.0f;
Chia-I Wuc19795a2014-09-13 11:12:55 +08001174
1175 memset(&depth_stencil, 0, sizeof(depth_stencil));
Tony Barbourde4124d2015-07-03 10:33:54 -06001176 depth_stencil.sType = VK_STRUCTURE_TYPE_DYNAMIC_DEPTH_STENCIL_STATE_CREATE_INFO;
Mark Lobodzinski4405fbf2015-06-12 11:14:17 -06001177 depth_stencil.minDepthBounds = 0.0f;
1178 depth_stencil.maxDepthBounds = 1.0f;
Tony Barbourfa6cac72015-01-16 14:27:35 -07001179 depth_stencil.stencilBackRef = 0;
1180 depth_stencil.stencilFrontRef = 0;
1181 depth_stencil.stencilReadMask = 0xff;
1182 depth_stencil.stencilWriteMask = 0xff;
Chia-I Wuc19795a2014-09-13 11:12:55 +08001183
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001184 err = vkCreateDynamicViewportState(demo->device, &viewport_create, &demo->viewport);
Chia-I Wuc19795a2014-09-13 11:12:55 +08001185 assert(!err);
1186
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001187 err = vkCreateDynamicRasterState(demo->device, &raster, &demo->raster);
Chia-I Wuc19795a2014-09-13 11:12:55 +08001188 assert(!err);
1189
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001190 err = vkCreateDynamicColorBlendState(demo->device,
Chia-I Wuc19795a2014-09-13 11:12:55 +08001191 &color_blend, &demo->color_blend);
1192 assert(!err);
1193
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001194 err = vkCreateDynamicDepthStencilState(demo->device,
Chia-I Wuc19795a2014-09-13 11:12:55 +08001195 &depth_stencil, &demo->depth_stencil);
1196 assert(!err);
1197}
1198
Chia-I Wu8d24b3b2015-03-26 13:14:16 +08001199static void demo_prepare_descriptor_pool(struct demo *demo)
Chia-I Wuf8385062015-01-04 16:27:24 +08001200{
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001201 const VkDescriptorTypeCount type_count = {
Courtney Goeltzenleuchterad870812015-04-15 15:29:59 -06001202 .type = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
Chia-I Wuf8385062015-01-04 16:27:24 +08001203 .count = DEMO_TEXTURE_COUNT,
1204 };
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001205 const VkDescriptorPoolCreateInfo descriptor_pool = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001206 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO,
Chia-I Wuf8385062015-01-04 16:27:24 +08001207 .pNext = NULL,
1208 .count = 1,
1209 .pTypeCount = &type_count,
1210 };
Tony Barbour22a30862015-04-22 09:02:32 -06001211 VkResult U_ASSERT_ONLY err;
Chia-I Wuf8385062015-01-04 16:27:24 +08001212
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001213 err = vkCreateDescriptorPool(demo->device,
1214 VK_DESCRIPTOR_POOL_USAGE_ONE_SHOT, 1,
Chia-I Wu8d24b3b2015-03-26 13:14:16 +08001215 &descriptor_pool, &demo->desc_pool);
Chia-I Wuf8385062015-01-04 16:27:24 +08001216 assert(!err);
1217}
1218
1219static void demo_prepare_descriptor_set(struct demo *demo)
1220{
Chia-I Wu8cd8ecd2015-05-25 16:27:55 +08001221 VkDescriptorInfo tex_descs[DEMO_TEXTURE_COUNT];
1222 VkWriteDescriptorSet write;
Tony Barbour22a30862015-04-22 09:02:32 -06001223 VkResult U_ASSERT_ONLY err;
Chia-I Wuf8385062015-01-04 16:27:24 +08001224 uint32_t count;
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -06001225 uint32_t i;
Chia-I Wuf8385062015-01-04 16:27:24 +08001226
Mike Stroyan230e6252015-04-17 12:36:38 -06001227 err = vkAllocDescriptorSets(demo->device, demo->desc_pool,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001228 VK_DESCRIPTOR_SET_USAGE_STATIC,
Chia-I Wuf8385062015-01-04 16:27:24 +08001229 1, &demo->desc_layout,
1230 &demo->desc_set, &count);
1231 assert(!err && count == 1);
1232
Chia-I Wu8cd8ecd2015-05-25 16:27:55 +08001233 memset(&tex_descs, 0, sizeof(tex_descs));
1234 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
1235 tex_descs[i].sampler = demo->textures[i].sampler;
1236 tex_descs[i].imageView = demo->textures[i].view;
1237 tex_descs[i].imageLayout = VK_IMAGE_LAYOUT_GENERAL;
1238 }
1239
1240 memset(&write, 0, sizeof(write));
1241 write.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
1242 write.destSet = demo->desc_set;
1243 write.count = DEMO_TEXTURE_COUNT;
1244 write.descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
1245 write.pDescriptors = tex_descs;
1246
1247 err = vkUpdateDescriptorSets(demo->device, 1, &write, 0, NULL);
1248 assert(!err);
Chia-I Wuf8385062015-01-04 16:27:24 +08001249}
1250
Chia-I Wu76cd4222015-07-08 13:34:24 +08001251static void demo_prepare_framebuffers(struct demo *demo)
1252{
Chia-I Wuc278df82015-07-07 11:50:03 +08001253 VkAttachmentBindInfo attachments[2] = {
1254 [0] = {
Tony Barbourde4124d2015-07-03 10:33:54 -06001255 .view.handle = VK_NULL_HANDLE,
Chia-I Wuc278df82015-07-07 11:50:03 +08001256 .layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
1257 },
1258 [1] = {
1259 .view = demo->depth.view,
1260 .layout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
1261 },
Chia-I Wu76cd4222015-07-08 13:34:24 +08001262 };
1263 const VkFramebufferCreateInfo fb_info = {
1264 .sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO,
1265 .pNext = NULL,
Chia-I Wuc278df82015-07-07 11:50:03 +08001266 .renderPass = demo->render_pass,
1267 .attachmentCount = 2,
1268 .pAttachments = attachments,
Chia-I Wu76cd4222015-07-08 13:34:24 +08001269 .width = demo->width,
1270 .height = demo->height,
1271 .layers = 1,
1272 };
1273 VkResult U_ASSERT_ONLY err;
1274 uint32_t i;
1275
1276 for (i = 0; i < DEMO_BUFFER_COUNT; i++) {
Chia-I Wuc278df82015-07-07 11:50:03 +08001277 attachments[0].view = demo->buffers[i].view;
Chia-I Wu76cd4222015-07-08 13:34:24 +08001278 err = vkCreateFramebuffer(demo->device, &fb_info, &demo->framebuffers[i]);
1279 assert(!err);
1280 }
1281}
1282
Chia-I Wuc19795a2014-09-13 11:12:55 +08001283static void demo_prepare(struct demo *demo)
1284{
Cody Northrop18ea11b2015-07-09 18:08:32 -06001285 VkResult U_ASSERT_ONLY err;
1286
1287 const VkCmdPoolCreateInfo cmd_pool_info = {
1288 .sType = VK_STRUCTURE_TYPE_CMD_POOL_CREATE_INFO,
1289 .pNext = NULL,
1290 .queueFamilyIndex = demo->graphics_queue_node_index,
1291 .flags = 0,
1292 };
1293 err = vkCreateCommandPool(demo->device, &cmd_pool_info, &demo->cmd_pool);
1294 assert(!err);
1295
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001296 const VkCmdBufferCreateInfo cmd = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001297 .sType = VK_STRUCTURE_TYPE_CMD_BUFFER_CREATE_INFO,
Chia-I Wuc19795a2014-09-13 11:12:55 +08001298 .pNext = NULL,
Cody Northrop18ea11b2015-07-09 18:08:32 -06001299 .cmdPool = demo->cmd_pool,
Chia-I Wu88eaa3b2015-06-26 15:34:39 +08001300 .level = VK_CMD_BUFFER_LEVEL_PRIMARY,
Chia-I Wuc19795a2014-09-13 11:12:55 +08001301 .flags = 0,
1302 };
Courtney Goeltzenleuchter633f9c52015-04-30 10:58:33 -06001303 err = vkCreateCommandBuffer(demo->device, &cmd, &demo->draw_cmd);
1304 assert(!err);
1305
Chia-I Wuc19795a2014-09-13 11:12:55 +08001306 demo_prepare_buffers(demo);
Chia-I Wu9ae87c92014-10-07 14:15:01 +08001307 demo_prepare_depth(demo);
Chia-I Wub043fe32014-10-06 15:30:33 +08001308 demo_prepare_textures(demo);
Chia-I Wu99621bc2014-10-08 11:52:22 +08001309 demo_prepare_vertices(demo);
Chia-I Wuf8385062015-01-04 16:27:24 +08001310 demo_prepare_descriptor_layout(demo);
Chia-I Wu76cd4222015-07-08 13:34:24 +08001311 demo_prepare_render_pass(demo);
Chia-I Wuc19795a2014-09-13 11:12:55 +08001312 demo_prepare_pipeline(demo);
1313 demo_prepare_dynamic_states(demo);
1314
Chia-I Wu8d24b3b2015-03-26 13:14:16 +08001315 demo_prepare_descriptor_pool(demo);
Chia-I Wuf8385062015-01-04 16:27:24 +08001316 demo_prepare_descriptor_set(demo);
Chia-I Wu76cd4222015-07-08 13:34:24 +08001317
1318 demo_prepare_framebuffers(demo);
1319
Courtney Goeltzenleuchter633f9c52015-04-30 10:58:33 -06001320 demo->prepared = true;
Chia-I Wuc19795a2014-09-13 11:12:55 +08001321}
1322
Ian Elliotte14e9f92015-04-16 15:23:05 -06001323#ifdef _WIN32
1324static void demo_run(struct demo *demo)
1325{
Courtney Goeltzenleuchter633f9c52015-04-30 10:58:33 -06001326 if (!demo->prepared)
1327 return;
Ian Elliotte14e9f92015-04-16 15:23:05 -06001328 demo_draw(demo);
1329}
1330
1331// On MS-Windows, make this a global, so it's available to WndProc()
1332struct demo demo;
1333
1334// MS-Windows event handling function:
1335LRESULT CALLBACK WndProc(HWND hWnd,
1336 UINT uMsg,
1337 WPARAM wParam,
1338 LPARAM lParam)
1339{
Ian Elliott4e19ed02015-04-28 10:52:52 -06001340 char tmp_str[] = APP_LONG_NAME;
Ian Elliotte14e9f92015-04-16 15:23:05 -06001341
1342 switch(uMsg)
1343 {
1344 case WM_CREATE:
1345 return 0;
1346 case WM_CLOSE:
1347 PostQuitMessage(0);
1348 return 0;
1349 case WM_PAINT:
1350 demo_run(&demo);
1351 return 0;
1352 default:
1353 break;
1354 }
1355 return (DefWindowProc(hWnd, uMsg, wParam, lParam));
1356}
1357
1358static void demo_create_window(struct demo *demo)
1359{
1360 WNDCLASSEX win_class;
1361
1362 // Initialize the window class structure:
1363 win_class.cbSize = sizeof(WNDCLASSEX);
1364 win_class.style = CS_HREDRAW | CS_VREDRAW;
1365 win_class.lpfnWndProc = WndProc;
1366 win_class.cbClsExtra = 0;
1367 win_class.cbWndExtra = 0;
1368 win_class.hInstance = demo->connection; // hInstance
1369 win_class.hIcon = LoadIcon(NULL, IDI_APPLICATION);
1370 win_class.hCursor = LoadCursor(NULL, IDC_ARROW);
1371 win_class.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
1372 win_class.lpszMenuName = NULL;
1373 win_class.lpszClassName = demo->name;
1374 win_class.hIconSm = LoadIcon(NULL, IDI_WINLOGO);
1375 // Register window class:
1376 if (!RegisterClassEx(&win_class)) {
1377 // It didn't work, so try to give a useful error:
1378 printf("Unexpected error trying to start the application!\n");
1379 fflush(stdout);
1380 exit(1);
1381 }
1382 // Create window with the registered class:
Mike Stroyan7eef5742015-06-15 14:19:19 -06001383 RECT wr = { 0, 0, demo->width, demo->height };
1384 AdjustWindowRect(&wr, WS_OVERLAPPEDWINDOW, FALSE);
Ian Elliotte14e9f92015-04-16 15:23:05 -06001385 demo->window = CreateWindowEx(0,
1386 demo->name, // class name
1387 demo->name, // app name
1388 WS_OVERLAPPEDWINDOW | // window style
1389 WS_VISIBLE |
1390 WS_SYSMENU,
1391 100,100, // x/y coords
Mike Stroyan7eef5742015-06-15 14:19:19 -06001392 wr.right-wr.left, // width
1393 wr.bottom-wr.top, // height
Ian Elliotte14e9f92015-04-16 15:23:05 -06001394 NULL, // handle to parent
1395 NULL, // handle to menu
1396 demo->connection, // hInstance
1397 NULL); // no extra parameters
1398 if (!demo->window) {
1399 // It didn't work, so try to give a useful error:
1400 printf("Cannot create a window in which to draw!\n");
1401 fflush(stdout);
1402 exit(1);
1403 }
1404}
1405#else // _WIN32
1406
Chia-I Wuc19795a2014-09-13 11:12:55 +08001407static void demo_handle_event(struct demo *demo,
1408 const xcb_generic_event_t *event)
1409{
1410 switch (event->response_type & 0x7f) {
1411 case XCB_EXPOSE:
1412 demo_draw(demo);
1413 break;
Courtney Goeltzenleuchterca698052014-11-07 15:17:03 -07001414 case XCB_CLIENT_MESSAGE:
1415 if((*(xcb_client_message_event_t*)event).data.data32[0] ==
1416 (*demo->atom_wm_delete_window).atom) {
1417 demo->quit = true;
1418 }
1419 break;
Chia-I Wuc19795a2014-09-13 11:12:55 +08001420 case XCB_KEY_RELEASE:
1421 {
1422 const xcb_key_release_event_t *key =
1423 (const xcb_key_release_event_t *) event;
1424
1425 if (key->detail == 0x9)
1426 demo->quit = true;
1427 }
1428 break;
Courtney Goeltzenleuchterca698052014-11-07 15:17:03 -07001429 case XCB_DESTROY_NOTIFY:
1430 demo->quit = true;
1431 break;
Chia-I Wuc19795a2014-09-13 11:12:55 +08001432 default:
1433 break;
1434 }
1435}
1436
1437static void demo_run(struct demo *demo)
1438{
1439 xcb_flush(demo->connection);
1440
1441 while (!demo->quit) {
1442 xcb_generic_event_t *event;
1443
1444 event = xcb_wait_for_event(demo->connection);
1445 if (event) {
1446 demo_handle_event(demo, event);
1447 free(event);
1448 }
1449 }
1450}
1451
1452static void demo_create_window(struct demo *demo)
1453{
1454 uint32_t value_mask, value_list[32];
1455
1456 demo->window = xcb_generate_id(demo->connection);
1457
1458 value_mask = XCB_CW_BACK_PIXEL | XCB_CW_EVENT_MASK;
1459 value_list[0] = demo->screen->black_pixel;
1460 value_list[1] = XCB_EVENT_MASK_KEY_RELEASE |
Courtney Goeltzenleuchterca698052014-11-07 15:17:03 -07001461 XCB_EVENT_MASK_EXPOSURE |
1462 XCB_EVENT_MASK_STRUCTURE_NOTIFY;
Chia-I Wuc19795a2014-09-13 11:12:55 +08001463
1464 xcb_create_window(demo->connection,
1465 XCB_COPY_FROM_PARENT,
1466 demo->window, demo->screen->root,
1467 0, 0, demo->width, demo->height, 0,
1468 XCB_WINDOW_CLASS_INPUT_OUTPUT,
1469 demo->screen->root_visual,
1470 value_mask, value_list);
1471
Courtney Goeltzenleuchterca698052014-11-07 15:17:03 -07001472 /* Magic code that will send notification when window is destroyed */
1473 xcb_intern_atom_cookie_t cookie = xcb_intern_atom(demo->connection, 1, 12,
1474 "WM_PROTOCOLS");
1475 xcb_intern_atom_reply_t* reply = xcb_intern_atom_reply(demo->connection, cookie, 0);
1476
1477 xcb_intern_atom_cookie_t cookie2 = xcb_intern_atom(demo->connection, 0, 16, "WM_DELETE_WINDOW");
1478 demo->atom_wm_delete_window = xcb_intern_atom_reply(demo->connection, cookie2, 0);
1479
1480 xcb_change_property(demo->connection, XCB_PROP_MODE_REPLACE,
1481 demo->window, (*reply).atom, 4, 32, 1,
1482 &(*demo->atom_wm_delete_window).atom);
1483 free(reply);
1484
Chia-I Wuc19795a2014-09-13 11:12:55 +08001485 xcb_map_window(demo->connection, demo->window);
1486}
Ian Elliotte14e9f92015-04-16 15:23:05 -06001487#endif // _WIN32
Chia-I Wuc19795a2014-09-13 11:12:55 +08001488
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001489static void demo_init_vk(struct demo *demo)
Chia-I Wuc19795a2014-09-13 11:12:55 +08001490{
Tobin Ehlis3536b442015-04-16 18:04:57 -06001491 VkResult err;
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06001492 char *extension_names[64];
1493 char *layer_names[64];
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -06001494 VkExtensionProperties *instance_extensions;
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06001495 VkLayerProperties *instance_layers;
1496 VkLayerProperties *device_layers;
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -06001497 uint32_t instance_extension_count = 0;
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06001498 uint32_t instance_layer_count = 0;
1499 uint32_t enabled_extension_count = 0;
1500 uint32_t enabled_layer_count = 0;
1501
1502 /* Look for validation layers */
Courtney Goeltzenleuchter1f41f542015-07-09 11:44:38 -06001503 VkBool32 validation_found = 0;
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06001504 err = vkGetGlobalLayerProperties(&instance_layer_count, NULL);
Tobin Ehlis3536b442015-04-16 18:04:57 -06001505 assert(!err);
1506
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06001507 memset(layer_names, 0, sizeof(layer_names));
1508 instance_layers = malloc(sizeof(VkLayerProperties) * instance_layer_count);
1509 err = vkGetGlobalLayerProperties(&instance_layer_count, instance_layers);
1510 assert(!err);
1511 for (uint32_t i = 0; i < instance_layer_count; i++) {
1512 if (!validation_found && demo->validate && !strcmp("Validation", instance_layers[i].layerName)) {
1513 layer_names[enabled_layer_count++] = "Validation";
1514 validation_found = 1;
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -06001515 }
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06001516 assert(enabled_layer_count < 64);
1517 }
1518 if (demo->validate && !validation_found) {
1519 ERR_EXIT("vkGetGlobalLayerProperties failed to find any "
1520 "\"Validation\" layers.\n\n"
1521 "Please look at the Getting Started guide for additional "
1522 "information.\n",
1523 "vkCreateInstance Failure");
1524 }
1525
1526 err = vkGetGlobalExtensionProperties(NULL, &instance_extension_count, NULL);
1527 assert(!err);
1528
Courtney Goeltzenleuchter1f41f542015-07-09 11:44:38 -06001529 VkBool32 WSIextFound = 0;
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06001530 memset(extension_names, 0, sizeof(extension_names));
1531 instance_extensions = malloc(sizeof(VkExtensionProperties) * instance_extension_count);
1532 err = vkGetGlobalExtensionProperties(NULL, &instance_extension_count, instance_extensions);
1533 assert(!err);
1534 for (uint32_t i = 0; i < instance_extension_count; i++) {
1535 if (!strcmp(VK_WSI_LUNARG_EXTENSION_NAME, instance_extensions[i].extName)) {
1536 WSIextFound = 1;
1537 extension_names[enabled_extension_count++] = VK_WSI_LUNARG_EXTENSION_NAME;
1538 }
1539 if (!strcmp(DEBUG_REPORT_EXTENSION_NAME, instance_extensions[i].extName)) {
1540 if (demo->validate) {
1541 extension_names[enabled_extension_count++] = DEBUG_REPORT_EXTENSION_NAME;
1542 }
1543 }
1544 assert(enabled_extension_count < 64);
Tobin Ehlis3536b442015-04-16 18:04:57 -06001545 }
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -06001546 if (!WSIextFound) {
Tony Barbour426b9052015-06-24 16:06:58 -06001547 ERR_EXIT("vkGetGlobalExtensionProperties failed to find the "
Ian Elliott3b375cf2015-04-28 13:22:33 -06001548 "\"VK_WSI_LunarG\" extension.\n\nDo you have a compatible "
1549 "Vulkan installable client driver (ICD) installed?\nPlease "
1550 "look at the Getting Started guide for additional "
1551 "information.\n",
1552 "vkCreateInstance Failure");
1553 }
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001554 const VkApplicationInfo app = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001555 .sType = VK_STRUCTURE_TYPE_APPLICATION_INFO,
Chia-I Wuc19795a2014-09-13 11:12:55 +08001556 .pNext = NULL,
Ian Elliott4e19ed02015-04-28 10:52:52 -06001557 .pAppName = APP_SHORT_NAME,
Chia-I Wuc19795a2014-09-13 11:12:55 +08001558 .appVersion = 0,
Ian Elliott4e19ed02015-04-28 10:52:52 -06001559 .pEngineName = APP_SHORT_NAME,
Chia-I Wuc19795a2014-09-13 11:12:55 +08001560 .engineVersion = 0,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001561 .apiVersion = VK_API_VERSION,
Chia-I Wuc19795a2014-09-13 11:12:55 +08001562 };
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06001563 VkInstanceCreateInfo inst_info = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001564 .sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO,
Jon Ashburn29669a42015-04-04 14:52:07 -06001565 .pNext = NULL,
1566 .pAppInfo = &app,
1567 .pAllocCb = NULL,
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06001568 .layerCount = enabled_layer_count,
1569 .ppEnabledLayerNames = (const char *const*) layer_names,
1570 .extensionCount = enabled_extension_count,
1571 .ppEnabledExtensionNames = (const char *const*) extension_names,
Jon Ashburn29669a42015-04-04 14:52:07 -06001572 };
Courtney Goeltzenleuchterddcb6192015-04-14 18:48:46 -06001573 const VkDeviceQueueCreateInfo queue = {
Chris Forbesfa6d36e2015-07-11 19:11:39 +12001574 .queueFamilyIndex = 0,
Chia-I Wuc19795a2014-09-13 11:12:55 +08001575 .queueCount = 1,
1576 };
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06001577
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -06001578 uint32_t gpu_count;
1579 uint32_t i;
Courtney Goeltzenleuchterf3168062015-03-05 18:09:39 -07001580 uint32_t queue_count;
Chia-I Wuc19795a2014-09-13 11:12:55 +08001581
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001582 err = vkCreateInstance(&inst_info, &demo->inst);
Ian Elliottcaa9f272015-04-28 11:35:02 -06001583 if (err == VK_ERROR_INCOMPATIBLE_DRIVER) {
1584 ERR_EXIT("Cannot find a compatible Vulkan installable client driver "
Ian Elliott3b375cf2015-04-28 13:22:33 -06001585 "(ICD).\n\nPlease look at the Getting Started guide for "
Ian Elliottcaa9f272015-04-28 11:35:02 -06001586 "additional information.\n",
1587 "vkCreateInstance Failure");
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06001588 } else if (err == VK_ERROR_INVALID_EXTENSION) {
1589 ERR_EXIT("Cannot find a specified extension library"
1590 ".\nMake sure your layers path is set appropriately\n",
1591 "vkCreateInstance Failure");
Ian Elliottcaa9f272015-04-28 11:35:02 -06001592 } else if (err) {
Ian Elliott3b375cf2015-04-28 13:22:33 -06001593 ERR_EXIT("vkCreateInstance failed.\n\nDo you have a compatible Vulkan "
1594 "installable client driver (ICD) installed?\nPlease look at "
Ian Elliottcaa9f272015-04-28 11:35:02 -06001595 "the Getting Started guide for additional information.\n",
1596 "vkCreateInstance Failure");
Ian Elliottdfe55f72015-04-03 15:24:55 -06001597 }
Jon Ashburn29669a42015-04-04 14:52:07 -06001598
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06001599 free(instance_layers);
1600 free(instance_extensions);
1601
Jon Ashburn07b309a2015-04-15 11:31:12 -06001602 gpu_count = 1;
1603 err = vkEnumeratePhysicalDevices(demo->inst, &gpu_count, &demo->gpu);
Chia-I Wuc19795a2014-09-13 11:12:55 +08001604 assert(!err && gpu_count == 1);
1605
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06001606 /* Look for validation layers */
1607 validation_found = 0;
1608 enabled_layer_count = 0;
1609 uint32_t device_layer_count = 0;
1610 err = vkGetPhysicalDeviceLayerProperties(demo->gpu, &device_layer_count, NULL);
1611 assert(!err);
1612
1613 memset(layer_names, 0, sizeof(layer_names));
1614 device_layers = malloc(sizeof(VkLayerProperties) * device_layer_count);
1615 err = vkGetPhysicalDeviceLayerProperties(demo->gpu, &device_layer_count, device_layers);
1616 assert(!err);
1617 for (uint32_t i = 0; i < device_layer_count; i++) {
1618 if (!validation_found && demo->validate &&
1619 !strcmp("Validation", device_layers[i].layerName)) {
1620 layer_names[enabled_layer_count++] = "Validation";
1621 validation_found = 1;
1622 }
1623 assert(enabled_layer_count < 64);
1624 }
1625 if (demo->validate && !validation_found) {
1626 ERR_EXIT("vkGetGlobalLayerProperties failed to find any "
1627 "\"Validation\" layers.\n\n"
1628 "Please look at the Getting Started guide for additional "
1629 "information.\n",
1630 "vkCreateInstance Failure");
1631 }
1632
1633 /* Don't need any device extensions */
1634 /* TODO: WSI device extension will go here eventually */
1635
1636 VkDeviceCreateInfo device = {
1637 .sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO,
1638 .pNext = NULL,
1639 .queueRecordCount = 1,
1640 .pRequestedQueues = &queue,
1641 .layerCount = enabled_layer_count,
1642 .ppEnabledLayerNames = (const char*const*) layer_names,
1643 .extensionCount = 0,
1644 .ppEnabledExtensionNames = NULL,
1645 .flags = 0,
1646 };
1647
1648 if (demo->validate) {
Courtney Goeltzenleuchtera4c8c712015-07-12 14:35:22 -06001649 demo->dbgCreateMsgCallback = (PFN_vkDbgCreateMsgCallback) vkGetInstanceProcAddr(demo->inst, "vkDbgCreateMsgCallback");
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06001650 if (!demo->dbgCreateMsgCallback) {
1651 ERR_EXIT("GetProcAddr: Unable to find vkDbgCreateMsgCallback\n",
1652 "vkGetProcAddr Failure");
1653 }
1654 err = demo->dbgCreateMsgCallback(
1655 demo->inst,
1656 VK_DBG_REPORT_ERROR_BIT | VK_DBG_REPORT_WARN_BIT,
1657 dbgFunc, NULL,
1658 &demo->msg_callback);
1659 switch (err) {
1660 case VK_SUCCESS:
1661 break;
1662 case VK_ERROR_INVALID_POINTER:
1663 ERR_EXIT("dbgCreateMsgCallback: Invalid pointer\n",
1664 "dbgCreateMsgCallback Failure");
1665 break;
1666 case VK_ERROR_OUT_OF_HOST_MEMORY:
1667 ERR_EXIT("dbgCreateMsgCallback: out of host memory\n",
1668 "dbgCreateMsgCallback Failure");
1669 break;
1670 default:
1671 ERR_EXIT("dbgCreateMsgCallback: unknown failure\n",
1672 "dbgCreateMsgCallback Failure");
1673 break;
1674 }
1675 }
1676
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001677 err = vkCreateDevice(demo->gpu, &device, &demo->device);
Chia-I Wuc19795a2014-09-13 11:12:55 +08001678 assert(!err);
1679
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06001680 free(device_layers);
1681
Ian Elliott1b6de092015-06-22 15:07:49 -06001682 GET_DEVICE_PROC_ADDR(demo->device, CreateSwapChainWSI);
1683 GET_DEVICE_PROC_ADDR(demo->device, CreateSwapChainWSI);
1684 GET_DEVICE_PROC_ADDR(demo->device, DestroySwapChainWSI);
1685 GET_DEVICE_PROC_ADDR(demo->device, GetSwapChainInfoWSI);
1686 GET_DEVICE_PROC_ADDR(demo->device, QueuePresentWSI);
1687
Tony Barbour426b9052015-06-24 16:06:58 -06001688 err = vkGetPhysicalDeviceProperties(demo->gpu, &demo->gpu_props);
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -06001689 assert(!err);
1690
Tony Barbour426b9052015-06-24 16:06:58 -06001691 err = vkGetPhysicalDeviceQueueCount(demo->gpu, &queue_count);
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -06001692 assert(!err);
1693
Tony Barbour426b9052015-06-24 16:06:58 -06001694 demo->queue_props = (VkPhysicalDeviceQueueProperties *) malloc(queue_count * sizeof(VkPhysicalDeviceQueueProperties));
1695 err = vkGetPhysicalDeviceQueueProperties(demo->gpu, queue_count, demo->queue_props);
Courtney Goeltzenleuchterf3168062015-03-05 18:09:39 -07001696 assert(!err);
Courtney Goeltzenleuchterf3168062015-03-05 18:09:39 -07001697 assert(queue_count >= 1);
1698
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06001699 // Graphics queue and MemMgr queue can be separate.
1700 // TODO: Add support for separate queues, including synchronization,
1701 // and appropriate tracking for QueueSubmit
Courtney Goeltzenleuchterf3168062015-03-05 18:09:39 -07001702 for (i = 0; i < queue_count; i++) {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001703 if (demo->queue_props[i].queueFlags & VK_QUEUE_GRAPHICS_BIT)
Courtney Goeltzenleuchterf3168062015-03-05 18:09:39 -07001704 break;
1705 }
1706 assert(i < queue_count);
1707 demo->graphics_queue_node_index = i;
1708
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001709 err = vkGetDeviceQueue(demo->device, demo->graphics_queue_node_index,
Chia-I Wuc19795a2014-09-13 11:12:55 +08001710 0, &demo->queue);
1711 assert(!err);
Ian Elliott32536f92015-04-21 16:41:02 -06001712
Jon Ashburnba4a1952015-06-16 12:44:51 -06001713 // for now hardcode format till get WSI support
1714 demo->format = VK_FORMAT_B8G8R8A8_UNORM;
Ian Elliott32536f92015-04-21 16:41:02 -06001715
Mark Lobodzinski72346292015-07-02 16:49:40 -06001716 // Get Memory information and properties
1717 err = vkGetPhysicalDeviceMemoryProperties(demo->gpu, &demo->memory_properties);
1718 assert(!err);
Chia-I Wuc19795a2014-09-13 11:12:55 +08001719}
1720
1721static void demo_init_connection(struct demo *demo)
1722{
Ian Elliotte14e9f92015-04-16 15:23:05 -06001723#ifndef _WIN32
Chia-I Wuc19795a2014-09-13 11:12:55 +08001724 const xcb_setup_t *setup;
1725 xcb_screen_iterator_t iter;
1726 int scr;
1727
1728 demo->connection = xcb_connect(NULL, &scr);
Ian Elliottdfe55f72015-04-03 15:24:55 -06001729 if (demo->connection == NULL) {
1730 printf("Cannot find a compatible Vulkan installable client driver "
1731 "(ICD).\nExiting ...\n");
1732 fflush(stdout);
1733 exit(1);
1734 }
Chia-I Wuc19795a2014-09-13 11:12:55 +08001735
1736 setup = xcb_get_setup(demo->connection);
1737 iter = xcb_setup_roots_iterator(setup);
1738 while (scr-- > 0)
1739 xcb_screen_next(&iter);
1740
1741 demo->screen = iter.data;
Ian Elliotte14e9f92015-04-16 15:23:05 -06001742#endif // _WIN32
Chia-I Wuc19795a2014-09-13 11:12:55 +08001743}
1744
Ian Elliotte14e9f92015-04-16 15:23:05 -06001745#ifdef _WIN32
1746static void demo_init(struct demo *demo, HINSTANCE hInstance, LPSTR pCmdLine)
1747#else // _WIN32
Courtney Goeltzenleuchterf113a952015-02-25 11:46:58 -07001748static void demo_init(struct demo *demo, const int argc, const char *argv[])
Ian Elliotte14e9f92015-04-16 15:23:05 -06001749#endif // _WIN32
Chia-I Wuc19795a2014-09-13 11:12:55 +08001750{
Ian Elliotte14e9f92015-04-16 15:23:05 -06001751 bool argv_error = false;
1752
Chia-I Wuc19795a2014-09-13 11:12:55 +08001753 memset(demo, 0, sizeof(*demo));
1754
Ian Elliotte14e9f92015-04-16 15:23:05 -06001755#ifdef _WIN32
1756 demo->connection = hInstance;
Ian Elliott4e19ed02015-04-28 10:52:52 -06001757 strncpy(demo->name, APP_SHORT_NAME, APP_NAME_STR_LEN);
Ian Elliotte14e9f92015-04-16 15:23:05 -06001758
1759 if (strncmp(pCmdLine, "--use_staging", strlen("--use_staging")) == 0)
1760 demo->use_staging_buffer = true;
Cody Northrop75db0322015-05-28 11:27:16 -06001761 else if (strncmp(pCmdLine, "--use_glsl", strlen("--use_glsl")) == 0)
1762 demo->use_glsl = true;
Ian Elliotte14e9f92015-04-16 15:23:05 -06001763 else if (strlen(pCmdLine) != 0) {
1764 fprintf(stderr, "Do not recognize argument \"%s\".\n", pCmdLine);
1765 argv_error = true;
1766 }
1767#else // _WIN32
Courtney Goeltzenleuchterf113a952015-02-25 11:46:58 -07001768 for (int i = 0; i < argc; i++) {
1769 if (strncmp(argv[i], "--use_staging", strlen("--use_staging")) == 0)
1770 demo->use_staging_buffer = true;
Cody Northrop75db0322015-05-28 11:27:16 -06001771 else if (strncmp(argv[i], "--use_glsl", strlen("--use_glsl")) == 0)
1772 demo->use_glsl = true;
Courtney Goeltzenleuchterf113a952015-02-25 11:46:58 -07001773 }
Ian Elliotte14e9f92015-04-16 15:23:05 -06001774#endif // _WIN32
1775 if (argv_error) {
Ian Elliott4e19ed02015-04-28 10:52:52 -06001776 fprintf(stderr, "Usage:\n %s [--use_staging]\n", APP_SHORT_NAME);
Ian Elliotte14e9f92015-04-16 15:23:05 -06001777 fflush(stderr);
1778 exit(1);
1779 }
Courtney Goeltzenleuchterf113a952015-02-25 11:46:58 -07001780
Chia-I Wuc19795a2014-09-13 11:12:55 +08001781 demo_init_connection(demo);
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001782 demo_init_vk(demo);
Chia-I Wuc19795a2014-09-13 11:12:55 +08001783
1784 demo->width = 300;
1785 demo->height = 300;
Chia-I Wuc19795a2014-09-13 11:12:55 +08001786}
1787
1788static void demo_cleanup(struct demo *demo)
1789{
Mark Lobodzinski23182612015-05-29 09:32:35 -05001790 uint32_t i;
Chia-I Wuc19795a2014-09-13 11:12:55 +08001791
Tony Barbourde4124d2015-07-03 10:33:54 -06001792 for (i = 0; i < DEMO_BUFFER_COUNT; i++) {
1793 vkDestroyFramebuffer(demo->device, demo->framebuffers[i]);
1794 }
Tony Barbourb857d312015-07-10 10:50:45 -06001795 vkFreeDescriptorSets(demo->device, demo->desc_pool, 1, &demo->desc_set);
Tony Barbourde4124d2015-07-03 10:33:54 -06001796 vkDestroyDescriptorPool(demo->device, demo->desc_pool);
Chia-I Wuf8385062015-01-04 16:27:24 +08001797
Courtney Goeltzenleuchter633f9c52015-04-30 10:58:33 -06001798 if (demo->setup_cmd) {
Tony Barbourde4124d2015-07-03 10:33:54 -06001799 vkDestroyCommandBuffer(demo->device, demo->setup_cmd);
Courtney Goeltzenleuchter633f9c52015-04-30 10:58:33 -06001800 }
Tony Barbourde4124d2015-07-03 10:33:54 -06001801 vkDestroyCommandBuffer(demo->device, demo->draw_cmd);
Chia-I Wuc19795a2014-09-13 11:12:55 +08001802
Tony Barbourde4124d2015-07-03 10:33:54 -06001803 vkDestroyDynamicViewportState(demo->device, demo->viewport);
1804 vkDestroyDynamicRasterState(demo->device, demo->raster);
1805 vkDestroyDynamicColorBlendState(demo->device, demo->color_blend);
1806 vkDestroyDynamicDepthStencilState(demo->device, demo->depth_stencil);
Chia-I Wuc19795a2014-09-13 11:12:55 +08001807
Tony Barbourde4124d2015-07-03 10:33:54 -06001808 vkDestroyPipeline(demo->device, demo->pipeline);
1809 vkDestroyRenderPass(demo->device, demo->render_pass);
1810 vkDestroyPipelineLayout(demo->device, demo->pipeline_layout);
1811 vkDestroyDescriptorSetLayout(demo->device, demo->desc_layout);
Chia-I Wub043fe32014-10-06 15:30:33 +08001812
Tony Barbourde4124d2015-07-03 10:33:54 -06001813 vkDestroyBuffer(demo->device, demo->vertices.buf);
Mark Lobodzinski23182612015-05-29 09:32:35 -05001814 vkFreeMemory(demo->device, demo->vertices.mem);
Chia-I Wu99621bc2014-10-08 11:52:22 +08001815
Chia-I Wub043fe32014-10-06 15:30:33 +08001816 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Tony Barbourde4124d2015-07-03 10:33:54 -06001817 vkDestroyImageView(demo->device, demo->textures[i].view);
1818 vkDestroyImage(demo->device, demo->textures[i].image);
Mark Lobodzinski23182612015-05-29 09:32:35 -05001819 vkFreeMemory(demo->device, demo->textures[i].mem);
Tony Barbourde4124d2015-07-03 10:33:54 -06001820 vkDestroySampler(demo->device, demo->textures[i].sampler);
Chia-I Wub043fe32014-10-06 15:30:33 +08001821 }
1822
Chia-I Wuc19795a2014-09-13 11:12:55 +08001823 for (i = 0; i < DEMO_BUFFER_COUNT; i++) {
Tony Barbourde4124d2015-07-03 10:33:54 -06001824 vkDestroyAttachmentView(demo->device, demo->buffers[i].view);
Chia-I Wuc19795a2014-09-13 11:12:55 +08001825 }
Tony Barbourde4124d2015-07-03 10:33:54 -06001826
1827 vkDestroyAttachmentView(demo->device, demo->depth.view);
1828 vkDestroyImage(demo->device, demo->depth.image);
1829 vkFreeMemory(demo->device, demo->depth.mem);
1830
Jon Ashburncedc15f2015-05-21 18:13:33 -06001831 demo->fpDestroySwapChainWSI(demo->swap_chain);
Chia-I Wuc19795a2014-09-13 11:12:55 +08001832
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001833 vkDestroyDevice(demo->device);
1834 vkDestroyInstance(demo->inst);
Chia-I Wuc19795a2014-09-13 11:12:55 +08001835
Ian Elliotte14e9f92015-04-16 15:23:05 -06001836#ifndef _WIN32
Chia-I Wuc19795a2014-09-13 11:12:55 +08001837 xcb_destroy_window(demo->connection, demo->window);
1838 xcb_disconnect(demo->connection);
Ian Elliotte14e9f92015-04-16 15:23:05 -06001839#endif // _WIN32
Chia-I Wuc19795a2014-09-13 11:12:55 +08001840}
1841
Ian Elliotte14e9f92015-04-16 15:23:05 -06001842#ifdef _WIN32
1843int APIENTRY WinMain(HINSTANCE hInstance,
1844 HINSTANCE hPrevInstance,
1845 LPSTR pCmdLine,
1846 int nCmdShow)
1847{
1848 MSG msg; // message
1849 bool done; // flag saying when app is complete
1850
1851 demo_init(&demo, hInstance, pCmdLine);
1852 demo_create_window(&demo);
1853
1854 demo_prepare(&demo);
1855
1856 done = false; //initialize loop condition variable
1857 /* main message loop*/
1858 while(!done)
1859 {
Ian Elliott421107f2015-04-28 15:50:36 -06001860 PeekMessage(&msg, NULL, 0, 0, PM_REMOVE);
Ian Elliotte14e9f92015-04-16 15:23:05 -06001861 if (msg.message == WM_QUIT) //check for a quit message
1862 {
1863 done = true; //if found, quit app
1864 }
1865 else
1866 {
1867 /* Translate and dispatch to event queue*/
1868 TranslateMessage(&msg);
1869 DispatchMessage(&msg);
1870 }
1871 }
1872
1873 demo_cleanup(&demo);
1874
Tony Barboura938abb2015-04-22 11:36:22 -06001875 return (int) msg.wParam;
Ian Elliotte14e9f92015-04-16 15:23:05 -06001876}
1877#else // _WIN32
Courtney Goeltzenleuchterf113a952015-02-25 11:46:58 -07001878int main(const int argc, const char *argv[])
Chia-I Wuc19795a2014-09-13 11:12:55 +08001879{
1880 struct demo demo;
1881
Courtney Goeltzenleuchterf113a952015-02-25 11:46:58 -07001882 demo_init(&demo, argc, argv);
Chia-I Wu5b66aa52015-04-16 22:02:10 +08001883 demo_create_window(&demo);
Chia-I Wuc19795a2014-09-13 11:12:55 +08001884
1885 demo_prepare(&demo);
Chia-I Wuc19795a2014-09-13 11:12:55 +08001886 demo_run(&demo);
1887
1888 demo_cleanup(&demo);
1889
Chia-I Wuc19795a2014-09-13 11:12:55 +08001890 return 0;
1891}
Ian Elliotte14e9f92015-04-16 15:23:05 -06001892#endif // _WIN32