blob: ff1431b810d54de03c2fd5d5bc7b56396b82952b [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>
Chia-I Wuc19795a2014-09-13 11:12:55 +080046
Cody Northropd4e020a2015-03-17 14:54:35 -060047#include "icd-spv.h"
Courtney Goeltzenleuchter3f2606d2014-10-13 17:51:58 -060048
Chia-I Wuc19795a2014-09-13 11:12:55 +080049#define DEMO_BUFFER_COUNT 2
Chia-I Wub043fe32014-10-06 15:30:33 +080050#define DEMO_TEXTURE_COUNT 1
Courtney Goeltzenleuchterf5cdad02015-03-31 16:36:30 -060051#define VERTEX_BUFFER_BIND_ID 0
Ian Elliott4e19ed02015-04-28 10:52:52 -060052#define APP_SHORT_NAME "tri"
53#define APP_LONG_NAME "The Vulkan Triangle Demo Program"
Chia-I Wuc19795a2014-09-13 11:12:55 +080054
Tony Barbour22a30862015-04-22 09:02:32 -060055#if defined(NDEBUG) && defined(__GNUC__)
56#define U_ASSERT_ONLY __attribute__((unused))
57#else
58#define U_ASSERT_ONLY
59#endif
60
Ian Elliottcaa9f272015-04-28 11:35:02 -060061#ifdef _WIN32
62#define ERR_EXIT(err_msg, err_class) \
63 do { \
64 MessageBox(NULL, err_msg, err_class, MB_OK); \
65 exit(1); \
66 } while (0)
67
68// NOTE: If the following values (copied from "loader_platform.h") change, they
69// need to change here as well:
70#define LAYER_NAMES_ENV "VK_LAYER_NAMES"
71#define LAYER_NAMES_REGISTRY_VALUE "VK_LAYER_NAMES"
72#else // _WIN32
73
74#define ERR_EXIT(err_msg, err_class) \
75 do { \
76 printf(err_msg); \
77 fflush(stdout); \
78 exit(1); \
79 } while (0)
80#endif // _WIN32
Tony Barbour22a30862015-04-22 09:02:32 -060081
Ian Elliott1b6de092015-06-22 15:07:49 -060082#define GET_DEVICE_PROC_ADDR(dev, entrypoint) \
83{ \
84 demo->fp##entrypoint = vkGetDeviceProcAddr(dev, "vk"#entrypoint); \
85 if (demo->fp##entrypoint == NULL) { \
86 ERR_EXIT("vkGetDeviceProcAddr failed to find vk"#entrypoint, \
87 "vkGetDeviceProcAddr Failure"); \
88 } \
89}
90
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -060091struct texture_object {
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -060092 VkSampler sampler;
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -070093
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -060094 VkImage image;
95 VkImageLayout imageLayout;
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -060096
Mark Lobodzinski23182612015-05-29 09:32:35 -050097 VkDeviceMemory mem;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -060098 VkImageView view;
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -070099 int32_t tex_width, tex_height;
100};
101
Chia-I Wuc19795a2014-09-13 11:12:55 +0800102struct demo {
Ian Elliotte14e9f92015-04-16 15:23:05 -0600103#ifdef _WIN32
104#define APP_NAME_STR_LEN 80
105 HINSTANCE connection; // hInstance - Windows Instance
106 char name[APP_NAME_STR_LEN]; // Name to put on the window/icon
107 HWND window; // hWnd - window handle
108#else // _WIN32
Chia-I Wuc19795a2014-09-13 11:12:55 +0800109 xcb_connection_t *connection;
110 xcb_screen_t *screen;
Chia-I Wu5b66aa52015-04-16 22:02:10 +0800111 xcb_window_t window;
112 xcb_intern_atom_reply_t *atom_wm_delete_window;
Ian Elliotte14e9f92015-04-16 15:23:05 -0600113#endif // _WIN32
Jon Ashburn8a399e92015-04-24 09:46:24 -0700114 bool prepared;
Ian Elliotte14e9f92015-04-16 15:23:05 -0600115 bool use_staging_buffer;
Cody Northrop75db0322015-05-28 11:27:16 -0600116 bool use_glsl;
Chia-I Wuc19795a2014-09-13 11:12:55 +0800117
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600118 VkInstance inst;
Tony Barbour8205d902015-04-16 15:59:00 -0600119 VkPhysicalDevice gpu;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600120 VkDevice device;
121 VkQueue queue;
Tony Barbour426b9052015-06-24 16:06:58 -0600122 VkPhysicalDeviceProperties gpu_props;
Tony Barbour8205d902015-04-16 15:59:00 -0600123 VkPhysicalDeviceQueueProperties *queue_props;
Courtney Goeltzenleuchterf3168062015-03-05 18:09:39 -0700124 uint32_t graphics_queue_node_index;
Chia-I Wuc19795a2014-09-13 11:12:55 +0800125
126 int width, height;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600127 VkFormat format;
Chia-I Wuc19795a2014-09-13 11:12:55 +0800128
Jon Ashburncedc15f2015-05-21 18:13:33 -0600129 PFN_vkCreateSwapChainWSI fpCreateSwapChainWSI;
130 PFN_vkDestroySwapChainWSI fpDestroySwapChainWSI;
131 PFN_vkGetSwapChainInfoWSI fpGetSwapChainInfoWSI;
132 PFN_vkQueuePresentWSI fpQueuePresentWSI;
133
Chia-I Wu5b66aa52015-04-16 22:02:10 +0800134 VkSwapChainWSI swap_chain;
Chia-I Wuc19795a2014-09-13 11:12:55 +0800135 struct {
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600136 VkImage image;
Tony Barbour8205d902015-04-16 15:59:00 -0600137 VkDeviceMemory mem;
Chia-I Wuc19795a2014-09-13 11:12:55 +0800138
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600139 VkColorAttachmentView view;
Chia-I Wuc19795a2014-09-13 11:12:55 +0800140 } buffers[DEMO_BUFFER_COUNT];
141
Chia-I Wub043fe32014-10-06 15:30:33 +0800142 struct {
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600143 VkFormat format;
Chia-I Wu9ae87c92014-10-07 14:15:01 +0800144
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600145 VkImage image;
Mark Lobodzinski23182612015-05-29 09:32:35 -0500146 VkDeviceMemory mem;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600147 VkDepthStencilView view;
Chia-I Wu9ae87c92014-10-07 14:15:01 +0800148 } depth;
149
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600150 struct texture_object textures[DEMO_TEXTURE_COUNT];
Chia-I Wub043fe32014-10-06 15:30:33 +0800151
Chia-I Wu99621bc2014-10-08 11:52:22 +0800152 struct {
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600153 VkBuffer buf;
Mark Lobodzinski23182612015-05-29 09:32:35 -0500154 VkDeviceMemory mem;
Chia-I Wu8d29d022014-10-08 12:14:39 +0800155
Mark Lobodzinski0e0fb5c2015-06-23 15:11:57 -0600156 VkPipelineVertexInputStateCreateInfo vi;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600157 VkVertexInputBindingDescription vi_bindings[1];
158 VkVertexInputAttributeDescription vi_attrs[2];
Chia-I Wu99621bc2014-10-08 11:52:22 +0800159 } vertices;
160
Courtney Goeltzenleuchter633f9c52015-04-30 10:58:33 -0600161 VkCmdBuffer setup_cmd; // Command Buffer for initialization commands
162 VkCmdBuffer draw_cmd; // Command Buffer for drawing commands
Mark Lobodzinski556f7212015-04-17 14:11:39 -0500163 VkPipelineLayout pipeline_layout;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600164 VkDescriptorSetLayout desc_layout;
165 VkPipeline pipeline;
Chia-I Wuc19795a2014-09-13 11:12:55 +0800166
Courtney Goeltzenleuchterfcf855f2015-04-10 16:24:50 -0600167 VkDynamicVpState viewport;
168 VkDynamicRsState raster;
169 VkDynamicCbState color_blend;
170 VkDynamicDsState depth_stencil;
Chia-I Wuc19795a2014-09-13 11:12:55 +0800171
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600172 VkDescriptorPool desc_pool;
173 VkDescriptorSet desc_set;
Chia-I Wuf8385062015-01-04 16:27:24 +0800174
Chia-I Wuc19795a2014-09-13 11:12:55 +0800175 bool quit;
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600176 uint32_t current_buffer;
Chia-I Wuc19795a2014-09-13 11:12:55 +0800177};
178
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600179static void demo_flush_init_cmd(struct demo *demo)
180{
Tony Barbour22a30862015-04-22 09:02:32 -0600181 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600182
Courtney Goeltzenleuchter633f9c52015-04-30 10:58:33 -0600183 if (demo->setup_cmd == VK_NULL_HANDLE)
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600184 return;
185
Courtney Goeltzenleuchter633f9c52015-04-30 10:58:33 -0600186 err = vkEndCommandBuffer(demo->setup_cmd);
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600187 assert(!err);
188
Courtney Goeltzenleuchter633f9c52015-04-30 10:58:33 -0600189 const VkCmdBuffer cmd_bufs[] = { demo->setup_cmd };
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600190
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600191 err = vkQueueSubmit(demo->queue, 1, cmd_bufs, VK_NULL_HANDLE);
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600192 assert(!err);
193
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600194 err = vkQueueWaitIdle(demo->queue);
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600195 assert(!err);
196
Courtney Goeltzenleuchter633f9c52015-04-30 10:58:33 -0600197 vkDestroyObject(demo->device, VK_OBJECT_TYPE_COMMAND_BUFFER, demo->setup_cmd);
198 demo->setup_cmd = VK_NULL_HANDLE;
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600199}
200
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600201static void demo_set_image_layout(
202 struct demo *demo,
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600203 VkImage image,
malnasse4b8ba4d2015-06-03 17:28:38 -0400204 VkImageAspect aspect,
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600205 VkImageLayout old_image_layout,
206 VkImageLayout new_image_layout)
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600207{
Tony Barbour22a30862015-04-22 09:02:32 -0600208 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600209
Courtney Goeltzenleuchter633f9c52015-04-30 10:58:33 -0600210 if (demo->setup_cmd == VK_NULL_HANDLE) {
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600211 const VkCmdBufferCreateInfo cmd = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600212 .sType = VK_STRUCTURE_TYPE_CMD_BUFFER_CREATE_INFO,
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600213 .pNext = NULL,
214 .queueNodeIndex = demo->graphics_queue_node_index,
215 .flags = 0,
216 };
217
Courtney Goeltzenleuchter633f9c52015-04-30 10:58:33 -0600218 err = vkCreateCommandBuffer(demo->device, &cmd, &demo->setup_cmd);
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600219 assert(!err);
220
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600221 VkCmdBufferBeginInfo cmd_buf_info = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600222 .sType = VK_STRUCTURE_TYPE_CMD_BUFFER_BEGIN_INFO,
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600223 .pNext = NULL,
Tony Barbour8205d902015-04-16 15:59:00 -0600224 .flags = VK_CMD_BUFFER_OPTIMIZE_SMALL_BATCH_BIT |
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600225 VK_CMD_BUFFER_OPTIMIZE_ONE_TIME_SUBMIT_BIT,
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600226 };
Courtney Goeltzenleuchter633f9c52015-04-30 10:58:33 -0600227 err = vkBeginCommandBuffer(demo->setup_cmd, &cmd_buf_info);
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600228 }
229
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600230 VkImageMemoryBarrier image_memory_barrier = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600231 .sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600232 .pNext = NULL,
233 .outputMask = 0,
234 .inputMask = 0,
235 .oldLayout = old_image_layout,
236 .newLayout = new_image_layout,
237 .image = image,
malnasse4b8ba4d2015-06-03 17:28:38 -0400238 .subresourceRange = { aspect, 0, 1, 0, 0 }
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600239 };
240
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600241 if (new_image_layout == VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL) {
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600242 /* Make sure anything that was copying from this image has completed */
Courtney Goeltzenleuchterad870812015-04-15 15:29:59 -0600243 image_memory_barrier.inputMask = VK_MEMORY_INPUT_TRANSFER_BIT;
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600244 }
245
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600246 if (new_image_layout == VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL) {
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600247 /* Make sure any Copy or CPU writes to image are flushed */
Courtney Goeltzenleuchtera569a502015-04-29 17:16:21 -0600248 image_memory_barrier.outputMask = VK_MEMORY_OUTPUT_TRANSFER_BIT | VK_MEMORY_OUTPUT_HOST_WRITE_BIT;
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600249 }
250
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600251 VkImageMemoryBarrier *pmemory_barrier = &image_memory_barrier;
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600252
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600253 VkPipeEvent set_events[] = { VK_PIPE_EVENT_TOP_OF_PIPE };
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600254
Mike Stroyand678e562015-06-08 13:33:44 -0600255 vkCmdPipelineBarrier(demo->setup_cmd, VK_WAIT_EVENT_TOP_OF_PIPE, 1, set_events, 1, (const void **)&pmemory_barrier);
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600256}
257
Chia-I Wuc19795a2014-09-13 11:12:55 +0800258static void demo_draw_build_cmd(struct demo *demo)
259{
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600260 const VkColorAttachmentBindInfo color_attachment = {
Chia-I Wuc19795a2014-09-13 11:12:55 +0800261 .view = demo->buffers[demo->current_buffer].view,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600262 .layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
Chia-I Wuc19795a2014-09-13 11:12:55 +0800263 };
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600264 const VkDepthStencilBindInfo depth_stencil = {
Chia-I Wu9ae87c92014-10-07 14:15:01 +0800265 .view = demo->depth.view,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600266 .layout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
Chia-I Wu9ae87c92014-10-07 14:15:01 +0800267 };
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600268 const VkClearColor clear_color = {
Courtney Goeltzenleuchter9a1ded82015-04-03 16:35:32 -0600269 .color.floatColor = { 0.2f, 0.2f, 0.2f, 0.2f },
270 .useRawValue = false,
271 };
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600272 const float clear_depth = 0.9f;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600273 VkImageSubresourceRange clear_range;
274 VkCmdBufferBeginInfo cmd_buf_info = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600275 .sType = VK_STRUCTURE_TYPE_CMD_BUFFER_BEGIN_INFO,
Courtney Goeltzenleuchtere3b0f3a2015-04-03 15:25:24 -0600276 .pNext = NULL,
Tony Barbour8205d902015-04-16 15:59:00 -0600277 .flags = VK_CMD_BUFFER_OPTIMIZE_SMALL_BATCH_BIT |
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600278 VK_CMD_BUFFER_OPTIMIZE_ONE_TIME_SUBMIT_BIT,
Jon Ashburn53d27af2014-12-31 17:08:35 -0700279 };
Tony Barbour22a30862015-04-22 09:02:32 -0600280 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600281 VkAttachmentLoadOp load_op = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
282 VkAttachmentStoreOp store_op = VK_ATTACHMENT_STORE_OP_DONT_CARE;
283 const VkFramebufferCreateInfo fb_info = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600284 .sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO,
Jon Ashburn3325d6b2015-01-02 18:24:05 -0700285 .pNext = NULL,
286 .colorAttachmentCount = 1,
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600287 .pColorAttachments = (VkColorAttachmentBindInfo*) &color_attachment,
288 .pDepthStencilAttachment = (VkDepthStencilBindInfo*) &depth_stencil,
Jon Ashburn3325d6b2015-01-02 18:24:05 -0700289 .sampleCount = 1,
Mark Lobodzinski71fcc2d2015-01-27 13:24:03 -0600290 .width = demo->width,
291 .height = demo->height,
292 .layers = 1,
Jon Ashburn3325d6b2015-01-02 18:24:05 -0700293 };
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600294 VkRenderPassCreateInfo rp_info;
295 VkRenderPassBegin rp_begin;
Jon Ashburn3325d6b2015-01-02 18:24:05 -0700296
297 memset(&rp_info, 0 , sizeof(rp_info));
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600298 err = vkCreateFramebuffer(demo->device, &fb_info, &rp_begin.framebuffer);
Jon Ashburn3325d6b2015-01-02 18:24:05 -0700299 assert(!err);
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600300 rp_info.sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO;
Jon Ashburn3325d6b2015-01-02 18:24:05 -0700301 rp_info.renderArea.extent.width = demo->width;
302 rp_info.renderArea.extent.height = demo->height;
Piers Daniell886be472015-02-23 16:23:13 -0700303 rp_info.colorAttachmentCount = fb_info.colorAttachmentCount;
Courtney Goeltzenleuchtere3b0f3a2015-04-03 15:25:24 -0600304 rp_info.pColorFormats = &demo->format;
305 rp_info.pColorLayouts = &color_attachment.layout;
Jon Ashburn3325d6b2015-01-02 18:24:05 -0700306 rp_info.pColorLoadOps = &load_op;
307 rp_info.pColorStoreOps = &store_op;
Courtney Goeltzenleuchtere3b0f3a2015-04-03 15:25:24 -0600308 rp_info.pColorLoadClearValues = &clear_color;
Tony Barbour8205d902015-04-16 15:59:00 -0600309 rp_info.depthStencilFormat = VK_FORMAT_D16_UNORM;
Courtney Goeltzenleuchtere3b0f3a2015-04-03 15:25:24 -0600310 rp_info.depthStencilLayout = depth_stencil.layout;
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600311 rp_info.depthLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
Courtney Goeltzenleuchtere3b0f3a2015-04-03 15:25:24 -0600312 rp_info.depthLoadClearValue = clear_depth;
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600313 rp_info.depthStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE;
314 rp_info.stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
Courtney Goeltzenleuchtere3b0f3a2015-04-03 15:25:24 -0600315 rp_info.stencilLoadClearValue = 0;
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600316 rp_info.stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE;
317 err = vkCreateRenderPass(demo->device, &rp_info, &(rp_begin.renderPass));
Jon Ashburn3325d6b2015-01-02 18:24:05 -0700318 assert(!err);
Chia-I Wuc19795a2014-09-13 11:12:55 +0800319
Courtney Goeltzenleuchter633f9c52015-04-30 10:58:33 -0600320 err = vkBeginCommandBuffer(demo->draw_cmd, &cmd_buf_info);
Chia-I Wuc19795a2014-09-13 11:12:55 +0800321 assert(!err);
322
Tobin Ehlise4076782015-06-24 15:53:07 -0600323 vkCmdBeginRenderPass(demo->draw_cmd, &rp_begin);
Courtney Goeltzenleuchter633f9c52015-04-30 10:58:33 -0600324 vkCmdBindPipeline(demo->draw_cmd, VK_PIPELINE_BIND_POINT_GRAPHICS,
Chia-I Wuc19795a2014-09-13 11:12:55 +0800325 demo->pipeline);
Mark Lobodzinskia65c4632015-06-15 13:21:21 -0600326 vkCmdBindDescriptorSets(demo->draw_cmd, VK_PIPELINE_BIND_POINT_GRAPHICS, demo->pipeline_layout,
Cody Northrop1a01b1d2015-04-16 13:41:56 -0600327 0, 1, & demo->desc_set, 0, NULL);
Chia-I Wuc19795a2014-09-13 11:12:55 +0800328
Courtney Goeltzenleuchter633f9c52015-04-30 10:58:33 -0600329 vkCmdBindDynamicStateObject(demo->draw_cmd, VK_STATE_BIND_POINT_VIEWPORT, demo->viewport);
330 vkCmdBindDynamicStateObject(demo->draw_cmd, VK_STATE_BIND_POINT_RASTER, demo->raster);
331 vkCmdBindDynamicStateObject(demo->draw_cmd, VK_STATE_BIND_POINT_COLOR_BLEND,
Chia-I Wuc19795a2014-09-13 11:12:55 +0800332 demo->color_blend);
Courtney Goeltzenleuchter633f9c52015-04-30 10:58:33 -0600333 vkCmdBindDynamicStateObject(demo->draw_cmd, VK_STATE_BIND_POINT_DEPTH_STENCIL,
Chia-I Wuc19795a2014-09-13 11:12:55 +0800334 demo->depth_stencil);
335
Tony Barbour8205d902015-04-16 15:59:00 -0600336 VkDeviceSize offsets[1] = {0};
Courtney Goeltzenleuchter633f9c52015-04-30 10:58:33 -0600337 vkCmdBindVertexBuffers(demo->draw_cmd, VERTEX_BUFFER_BIND_ID, 1, &demo->vertices.buf, offsets);
Chia-I Wu3b04af52014-11-08 10:48:20 +0800338
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600339 clear_range.aspect = VK_IMAGE_ASPECT_COLOR;
Chia-I Wu68a7de42014-10-25 12:40:28 +0800340 clear_range.baseMipLevel = 0;
341 clear_range.mipLevels = 1;
342 clear_range.baseArraySlice = 0;
343 clear_range.arraySize = 1;
Courtney Goeltzenleuchter633f9c52015-04-30 10:58:33 -0600344 vkCmdClearColorImage(demo->draw_cmd,
Chia-I Wu68a7de42014-10-25 12:40:28 +0800345 demo->buffers[demo->current_buffer].image,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600346 VK_IMAGE_LAYOUT_CLEAR_OPTIMAL,
Courtney Goeltzenleuchterda4a99e2015-04-23 17:49:22 -0600347 &clear_color, 1, &clear_range);
Chia-I Wu68a7de42014-10-25 12:40:28 +0800348
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600349 clear_range.aspect = VK_IMAGE_ASPECT_DEPTH;
Courtney Goeltzenleuchter633f9c52015-04-30 10:58:33 -0600350 vkCmdClearDepthStencil(demo->draw_cmd,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600351 demo->depth.image, VK_IMAGE_LAYOUT_CLEAR_OPTIMAL,
Chia-I Wu68a7de42014-10-25 12:40:28 +0800352 clear_depth, 0, 1, &clear_range);
353
Courtney Goeltzenleuchter633f9c52015-04-30 10:58:33 -0600354 vkCmdDraw(demo->draw_cmd, 0, 3, 0, 1);
355 vkCmdEndRenderPass(demo->draw_cmd, rp_begin.renderPass);
Chia-I Wuc19795a2014-09-13 11:12:55 +0800356
Courtney Goeltzenleuchter633f9c52015-04-30 10:58:33 -0600357 err = vkEndCommandBuffer(demo->draw_cmd);
Chia-I Wuc19795a2014-09-13 11:12:55 +0800358 assert(!err);
Courtney Goeltzenleuchtere9afa992015-02-25 16:55:23 -0700359
Mike Stroyan230e6252015-04-17 12:36:38 -0600360 vkDestroyObject(demo->device, VK_OBJECT_TYPE_RENDER_PASS, rp_begin.renderPass);
361 vkDestroyObject(demo->device, VK_OBJECT_TYPE_FRAMEBUFFER, rp_begin.framebuffer);
Chia-I Wuc19795a2014-09-13 11:12:55 +0800362}
363
364static void demo_draw(struct demo *demo)
365{
Chia-I Wu5b66aa52015-04-16 22:02:10 +0800366 const VkPresentInfoWSI present = {
367 .sType = VK_STRUCTURE_TYPE_PRESENT_INFO_WSI,
368 .pNext = NULL,
369 .image = demo->buffers[demo->current_buffer].image,
370 .flipInterval = 0,
Chia-I Wuc19795a2014-09-13 11:12:55 +0800371 };
Tony Barbour22a30862015-04-22 09:02:32 -0600372 VkResult U_ASSERT_ONLY err;
Chia-I Wuc19795a2014-09-13 11:12:55 +0800373
374 demo_draw_build_cmd(demo);
375
Courtney Goeltzenleuchter633f9c52015-04-30 10:58:33 -0600376 err = vkQueueSubmit(demo->queue, 1, &demo->draw_cmd, VK_NULL_HANDLE);
Chia-I Wuc19795a2014-09-13 11:12:55 +0800377 assert(!err);
378
Jon Ashburncedc15f2015-05-21 18:13:33 -0600379 err = demo->fpQueuePresentWSI(demo->queue, &present);
Chia-I Wuc19795a2014-09-13 11:12:55 +0800380 assert(!err);
381
382 demo->current_buffer = (demo->current_buffer + 1) % DEMO_BUFFER_COUNT;
Chia-I Wu5b66aa52015-04-16 22:02:10 +0800383
384 err = vkQueueWaitIdle(demo->queue);
385 assert(err == VK_SUCCESS);
Chia-I Wuc19795a2014-09-13 11:12:55 +0800386}
387
388static void demo_prepare_buffers(struct demo *demo)
389{
Chia-I Wu5b66aa52015-04-16 22:02:10 +0800390 const VkSwapChainCreateInfoWSI swap_chain = {
391 .sType = VK_STRUCTURE_TYPE_SWAP_CHAIN_CREATE_INFO_WSI,
392 .pNext = NULL,
393 .pNativeWindowSystemHandle = demo->connection,
394 .pNativeWindowHandle = (void *) (intptr_t) demo->window,
Ian Elliott32536f92015-04-21 16:41:02 -0600395 .displayCount = 1,
Chia-I Wu5b66aa52015-04-16 22:02:10 +0800396 .imageCount = DEMO_BUFFER_COUNT,
397 .imageFormat = demo->format,
398 .imageExtent = {
Chia-I Wuc19795a2014-09-13 11:12:55 +0800399 .width = demo->width,
400 .height = demo->height,
401 },
Chia-I Wu5b66aa52015-04-16 22:02:10 +0800402 .imageArraySize = 1,
403 .imageUsageFlags = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT,
Chia-I Wuc19795a2014-09-13 11:12:55 +0800404 };
Chia-I Wu5b66aa52015-04-16 22:02:10 +0800405 VkSwapChainImageInfoWSI images[DEMO_BUFFER_COUNT];
406 size_t images_size = sizeof(images);
Tony Barbour22a30862015-04-22 09:02:32 -0600407 VkResult U_ASSERT_ONLY err;
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600408 uint32_t i;
Chia-I Wuc19795a2014-09-13 11:12:55 +0800409
Jon Ashburncedc15f2015-05-21 18:13:33 -0600410 err = demo->fpCreateSwapChainWSI(demo->device, &swap_chain, &demo->swap_chain);
Chia-I Wu5b66aa52015-04-16 22:02:10 +0800411 assert(!err);
412
Jon Ashburncedc15f2015-05-21 18:13:33 -0600413 err = demo->fpGetSwapChainInfoWSI(demo->swap_chain,
Chia-I Wu5b66aa52015-04-16 22:02:10 +0800414 VK_SWAP_CHAIN_INFO_TYPE_PERSISTENT_IMAGES_WSI,
415 &images_size, images);
416 assert(!err && images_size == sizeof(images));
417
Chia-I Wuc19795a2014-09-13 11:12:55 +0800418 for (i = 0; i < DEMO_BUFFER_COUNT; i++) {
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600419 VkColorAttachmentViewCreateInfo color_attachment_view = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600420 .sType = VK_STRUCTURE_TYPE_COLOR_ATTACHMENT_VIEW_CREATE_INFO,
Chia-I Wuc19795a2014-09-13 11:12:55 +0800421 .pNext = NULL,
422 .format = demo->format,
423 .mipLevel = 0,
424 .baseArraySlice = 0,
425 .arraySize = 1,
426 };
427
Chia-I Wu5b66aa52015-04-16 22:02:10 +0800428 demo->buffers[i].image = images[i].image;
429 demo->buffers[i].mem = images[i].memory;
Mark Lobodzinski97dcd042015-04-16 08:52:00 -0500430
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600431 demo_set_image_layout(demo, demo->buffers[i].image,
malnasse4b8ba4d2015-06-03 17:28:38 -0400432 VK_IMAGE_ASPECT_COLOR,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600433 VK_IMAGE_LAYOUT_UNDEFINED,
434 VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL);
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600435
Chia-I Wuc19795a2014-09-13 11:12:55 +0800436 color_attachment_view.image = demo->buffers[i].image;
437
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600438 err = vkCreateColorAttachmentView(demo->device,
Chia-I Wuc19795a2014-09-13 11:12:55 +0800439 &color_attachment_view, &demo->buffers[i].view);
440 assert(!err);
441 }
Piers Daniell886be472015-02-23 16:23:13 -0700442
443 demo->current_buffer = 0;
Chia-I Wuc19795a2014-09-13 11:12:55 +0800444}
445
Chia-I Wu9ae87c92014-10-07 14:15:01 +0800446static void demo_prepare_depth(struct demo *demo)
447{
Tony Barbour8205d902015-04-16 15:59:00 -0600448 const VkFormat depth_format = VK_FORMAT_D16_UNORM;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600449 const VkImageCreateInfo image = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600450 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
Chia-I Wu9ae87c92014-10-07 14:15:01 +0800451 .pNext = NULL,
Tony Barbour8205d902015-04-16 15:59:00 -0600452 .imageType = VK_IMAGE_TYPE_2D,
Chia-I Wu9ae87c92014-10-07 14:15:01 +0800453 .format = depth_format,
454 .extent = { demo->width, demo->height, 1 },
455 .mipLevels = 1,
456 .arraySize = 1,
457 .samples = 1,
Tony Barbour8205d902015-04-16 15:59:00 -0600458 .tiling = VK_IMAGE_TILING_OPTIMAL,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600459 .usage = VK_IMAGE_USAGE_DEPTH_STENCIL_BIT,
Chia-I Wu9ae87c92014-10-07 14:15:01 +0800460 .flags = 0,
461 };
Courtney Goeltzenleuchterddcb6192015-04-14 18:48:46 -0600462 VkMemoryAllocInfo mem_alloc = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600463 .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOC_INFO,
Mark Lobodzinski97dcd042015-04-16 08:52:00 -0500464 .pNext = NULL,
Chia-I Wu9ae87c92014-10-07 14:15:01 +0800465 .allocationSize = 0,
Tony Barbour8205d902015-04-16 15:59:00 -0600466 .memProps = VK_MEMORY_PROPERTY_DEVICE_ONLY,
Chia-I Wu9ae87c92014-10-07 14:15:01 +0800467 };
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600468 VkDepthStencilViewCreateInfo view = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600469 .sType = VK_STRUCTURE_TYPE_DEPTH_STENCIL_VIEW_CREATE_INFO,
Chia-I Wu9ae87c92014-10-07 14:15:01 +0800470 .pNext = NULL,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600471 .image = VK_NULL_HANDLE,
Chia-I Wu9ae87c92014-10-07 14:15:01 +0800472 .mipLevel = 0,
473 .baseArraySlice = 0,
474 .arraySize = 1,
475 .flags = 0,
476 };
Jon Ashburna9ae3832015-01-16 09:37:43 -0700477
Mark Lobodzinski23182612015-05-29 09:32:35 -0500478 VkMemoryRequirements mem_reqs;
Tony Barbour22a30862015-04-22 09:02:32 -0600479 VkResult U_ASSERT_ONLY err;
Chia-I Wu9ae87c92014-10-07 14:15:01 +0800480
481 demo->depth.format = depth_format;
482
483 /* create image */
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600484 err = vkCreateImage(demo->device, &image,
Chia-I Wu9ae87c92014-10-07 14:15:01 +0800485 &demo->depth.image);
486 assert(!err);
487
Tony Barbour426b9052015-06-24 16:06:58 -0600488 err = vkGetObjectMemoryRequirements(demo->device,
489 VK_OBJECT_TYPE_IMAGE, demo->depth.image, &mem_reqs);
Mark Lobodzinski23182612015-05-29 09:32:35 -0500490 mem_alloc.allocationSize = mem_reqs.size;
Chia-I Wu9ae87c92014-10-07 14:15:01 +0800491
Mark Lobodzinski23182612015-05-29 09:32:35 -0500492 /* allocate memory */
493 err = vkAllocMemory(demo->device, &mem_alloc, &demo->depth.mem);
494 assert(!err);
Chia-I Wu9ae87c92014-10-07 14:15:01 +0800495
Mark Lobodzinski23182612015-05-29 09:32:35 -0500496 /* bind memory */
497 err = vkBindObjectMemory(demo->device,
498 VK_OBJECT_TYPE_IMAGE, demo->depth.image,
499 demo->depth.mem, 0);
500 assert(!err);
Chia-I Wu9ae87c92014-10-07 14:15:01 +0800501
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600502 demo_set_image_layout(demo, demo->depth.image,
malnasse4b8ba4d2015-06-03 17:28:38 -0400503 VK_IMAGE_ASPECT_DEPTH,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600504 VK_IMAGE_LAYOUT_UNDEFINED,
505 VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL);
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600506
Chia-I Wu9ae87c92014-10-07 14:15:01 +0800507 /* create image view */
508 view.image = demo->depth.image;
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600509 err = vkCreateDepthStencilView(demo->device, &view,
Chia-I Wu9ae87c92014-10-07 14:15:01 +0800510 &demo->depth.view);
511 assert(!err);
Chia-I Wu9ae87c92014-10-07 14:15:01 +0800512}
513
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700514static void demo_prepare_texture_image(struct demo *demo,
515 const uint32_t *tex_colors,
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600516 struct texture_object *tex_obj,
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600517 VkImageTiling tiling,
Courtney Goeltzenleuchtercb67a322015-04-21 09:31:23 -0600518 VkImageUsageFlags usage,
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600519 VkFlags mem_props)
Chia-I Wub043fe32014-10-06 15:30:33 +0800520{
Tony Barbour8205d902015-04-16 15:59:00 -0600521 const VkFormat tex_format = VK_FORMAT_B8G8R8A8_UNORM;
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600522 const int32_t tex_width = 2;
523 const int32_t tex_height = 2;
Tony Barbour22a30862015-04-22 09:02:32 -0600524 VkResult U_ASSERT_ONLY err;
Chia-I Wub043fe32014-10-06 15:30:33 +0800525
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600526 tex_obj->tex_width = tex_width;
527 tex_obj->tex_height = tex_height;
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700528
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600529 const VkImageCreateInfo image_create_info = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600530 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
Courtney Goeltzenleuchter3226a6f2015-02-11 18:17:22 -0700531 .pNext = NULL,
Tony Barbour8205d902015-04-16 15:59:00 -0600532 .imageType = VK_IMAGE_TYPE_2D,
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700533 .format = tex_format,
534 .extent = { tex_width, tex_height, 1 },
535 .mipLevels = 1,
536 .arraySize = 1,
537 .samples = 1,
538 .tiling = tiling,
Courtney Goeltzenleuchtercb67a322015-04-21 09:31:23 -0600539 .usage = usage,
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700540 .flags = 0,
541 };
Courtney Goeltzenleuchterddcb6192015-04-14 18:48:46 -0600542 VkMemoryAllocInfo mem_alloc = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600543 .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOC_INFO,
Mark Lobodzinski97dcd042015-04-16 08:52:00 -0500544 .pNext = NULL,
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700545 .allocationSize = 0,
546 .memProps = mem_props,
Courtney Goeltzenleuchter3226a6f2015-02-11 18:17:22 -0700547 };
548
Mark Lobodzinski23182612015-05-29 09:32:35 -0500549 VkMemoryRequirements mem_reqs;
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700550
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600551 err = vkCreateImage(demo->device, &image_create_info,
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600552 &tex_obj->image);
Courtney Goeltzenleuchter3226a6f2015-02-11 18:17:22 -0700553 assert(!err);
554
Tony Barbour426b9052015-06-24 16:06:58 -0600555 err = vkGetObjectMemoryRequirements(demo->device,
556 VK_OBJECT_TYPE_IMAGE, tex_obj->image, &mem_reqs);
Mark Lobodzinski23182612015-05-29 09:32:35 -0500557 mem_alloc.allocationSize = mem_reqs.size;
Courtney Goeltzenleuchter3226a6f2015-02-11 18:17:22 -0700558
Mark Lobodzinski23182612015-05-29 09:32:35 -0500559 /* allocate memory */
560 err = vkAllocMemory(demo->device, &mem_alloc, &tex_obj->mem);
561 assert(!err);
Courtney Goeltzenleuchter3226a6f2015-02-11 18:17:22 -0700562
Mark Lobodzinski23182612015-05-29 09:32:35 -0500563 /* bind memory */
564 err = vkBindObjectMemory(demo->device,
565 VK_OBJECT_TYPE_IMAGE, tex_obj->image,
566 tex_obj->mem, 0);
567 assert(!err);
Courtney Goeltzenleuchter3226a6f2015-02-11 18:17:22 -0700568
Tony Barbour8205d902015-04-16 15:59:00 -0600569 if (mem_props & VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT) {
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600570 const VkImageSubresource subres = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600571 .aspect = VK_IMAGE_ASPECT_COLOR,
Courtney Goeltzenleuchter3226a6f2015-02-11 18:17:22 -0700572 .mipLevel = 0,
573 .arraySlice = 0,
574 };
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600575 VkSubresourceLayout layout;
Courtney Goeltzenleuchter3226a6f2015-02-11 18:17:22 -0700576 void *data;
577 int32_t x, y;
578
Tony Barbour426b9052015-06-24 16:06:58 -0600579 err = vkGetImageSubresourceLayout(demo->device, tex_obj->image, &subres, &layout);
580 assert(!err);
Courtney Goeltzenleuchter3226a6f2015-02-11 18:17:22 -0700581
Mark Lobodzinski23182612015-05-29 09:32:35 -0500582 err = vkMapMemory(demo->device, tex_obj->mem, 0, 0, 0, &data);
Courtney Goeltzenleuchter3226a6f2015-02-11 18:17:22 -0700583 assert(!err);
584
585 for (y = 0; y < tex_height; y++) {
586 uint32_t *row = (uint32_t *) ((char *) data + layout.rowPitch * y);
587 for (x = 0; x < tex_width; x++)
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700588 row[x] = tex_colors[(x & 1) ^ (y & 1)];
Courtney Goeltzenleuchter3226a6f2015-02-11 18:17:22 -0700589 }
590
Mark Lobodzinski23182612015-05-29 09:32:35 -0500591 err = vkUnmapMemory(demo->device, tex_obj->mem);
Courtney Goeltzenleuchter3226a6f2015-02-11 18:17:22 -0700592 assert(!err);
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700593 }
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600594
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600595 tex_obj->imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600596 demo_set_image_layout(demo, tex_obj->image,
malnasse4b8ba4d2015-06-03 17:28:38 -0400597 VK_IMAGE_ASPECT_COLOR,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600598 VK_IMAGE_LAYOUT_UNDEFINED,
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600599 tex_obj->imageLayout);
600 /* 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 -0700601}
602
Mark Lobodzinskicf26e072015-04-16 11:44:05 -0500603static void demo_destroy_texture_image(struct demo *demo, struct texture_object *tex_obj)
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700604{
605 /* clean up staging resources */
Mike Stroyan230e6252015-04-17 12:36:38 -0600606 vkDestroyObject(demo->device, VK_OBJECT_TYPE_IMAGE, tex_obj->image);
Courtney Goeltzenleuchtera063d9b2015-06-10 16:16:22 -0600607 vkFreeMemory(demo->device, tex_obj->mem);
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700608}
609
610static void demo_prepare_textures(struct demo *demo)
611{
Tony Barbour8205d902015-04-16 15:59:00 -0600612 const VkFormat tex_format = VK_FORMAT_B8G8R8A8_UNORM;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600613 VkFormatProperties props;
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700614 const uint32_t tex_colors[DEMO_TEXTURE_COUNT][2] = {
615 { 0xffff0000, 0xff00ff00 },
616 };
Tony Barbour22a30862015-04-22 09:02:32 -0600617 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700618 uint32_t i;
619
Chris Forbesd7576302015-06-21 22:55:02 +1200620 err = vkGetPhysicalDeviceFormatInfo(demo->gpu, tex_format, &props);
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700621 assert(!err);
622
623 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Tony Barbour8205d902015-04-16 15:59:00 -0600624 if ((props.linearTilingFeatures & VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT) && !demo->use_staging_buffer) {
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700625 /* Device can texture using linear textures */
626 demo_prepare_texture_image(demo, tex_colors[i], &demo->textures[i],
Courtney Goeltzenleuchtercb67a322015-04-21 09:31:23 -0600627 VK_IMAGE_TILING_LINEAR, VK_IMAGE_USAGE_SAMPLED_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT);
Tony Barbour8205d902015-04-16 15:59:00 -0600628 } else if (props.optimalTilingFeatures & VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT){
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700629 /* Must use staging buffer to copy linear texture to optimized */
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600630 struct texture_object staging_texture;
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700631
632 memset(&staging_texture, 0, sizeof(staging_texture));
633 demo_prepare_texture_image(demo, tex_colors[i], &staging_texture,
Courtney Goeltzenleuchtercb67a322015-04-21 09:31:23 -0600634 VK_IMAGE_TILING_LINEAR, VK_IMAGE_USAGE_TRANSFER_SOURCE_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT);
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700635
636 demo_prepare_texture_image(demo, tex_colors[i], &demo->textures[i],
Courtney Goeltzenleuchtercb67a322015-04-21 09:31:23 -0600637 VK_IMAGE_TILING_OPTIMAL,
638 (VK_IMAGE_USAGE_TRANSFER_DESTINATION_BIT | VK_IMAGE_USAGE_SAMPLED_BIT),
639 VK_MEMORY_PROPERTY_DEVICE_ONLY);
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700640
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600641 demo_set_image_layout(demo, staging_texture.image,
malnasse4b8ba4d2015-06-03 17:28:38 -0400642 VK_IMAGE_ASPECT_COLOR,
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600643 staging_texture.imageLayout,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600644 VK_IMAGE_LAYOUT_TRANSFER_SOURCE_OPTIMAL);
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700645
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600646 demo_set_image_layout(demo, demo->textures[i].image,
malnasse4b8ba4d2015-06-03 17:28:38 -0400647 VK_IMAGE_ASPECT_COLOR,
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600648 demo->textures[i].imageLayout,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600649 VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL);
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700650
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600651 VkImageCopy copy_region = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600652 .srcSubresource = { VK_IMAGE_ASPECT_COLOR, 0, 0 },
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700653 .srcOffset = { 0, 0, 0 },
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600654 .destSubresource = { VK_IMAGE_ASPECT_COLOR, 0, 0 },
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700655 .destOffset = { 0, 0, 0 },
656 .extent = { staging_texture.tex_width, staging_texture.tex_height, 1 },
657 };
Courtney Goeltzenleuchter633f9c52015-04-30 10:58:33 -0600658 vkCmdCopyImage(demo->setup_cmd,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600659 staging_texture.image, VK_IMAGE_LAYOUT_TRANSFER_SOURCE_OPTIMAL,
660 demo->textures[i].image, VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL,
Courtney Goeltzenleuchter51cbf302015-03-25 11:25:10 -0600661 1, &copy_region);
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700662
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600663 demo_set_image_layout(demo, demo->textures[i].image,
malnasse4b8ba4d2015-06-03 17:28:38 -0400664 VK_IMAGE_ASPECT_COLOR,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600665 VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL,
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600666 demo->textures[i].imageLayout);
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700667
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600668 demo_flush_init_cmd(demo);
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700669
Courtney Goeltzenleuchter876629f2015-04-21 09:30:03 -0600670 demo_destroy_texture_image(demo, &staging_texture);
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700671 } else {
Tony Barbour8205d902015-04-16 15:59:00 -0600672 /* Can't support VK_FORMAT_B8G8R8A8_UNORM !? */
Piers Daniell886be472015-02-23 16:23:13 -0700673 assert(!"No support for B8G8R8A8_UNORM as texture image format");
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700674 }
Courtney Goeltzenleuchter3226a6f2015-02-11 18:17:22 -0700675
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600676 const VkSamplerCreateInfo sampler = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600677 .sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO,
Chia-I Wub043fe32014-10-06 15:30:33 +0800678 .pNext = NULL,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600679 .magFilter = VK_TEX_FILTER_NEAREST,
680 .minFilter = VK_TEX_FILTER_NEAREST,
Tony Barbour8205d902015-04-16 15:59:00 -0600681 .mipMode = VK_TEX_MIPMAP_MODE_BASE,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600682 .addressU = VK_TEX_ADDRESS_WRAP,
683 .addressV = VK_TEX_ADDRESS_WRAP,
684 .addressW = VK_TEX_ADDRESS_WRAP,
Chia-I Wub043fe32014-10-06 15:30:33 +0800685 .mipLodBias = 0.0f,
Courtney Goeltzenleuchterbc9c8162015-02-13 18:20:24 -0700686 .maxAnisotropy = 1,
Tony Barbour8205d902015-04-16 15:59:00 -0600687 .compareOp = VK_COMPARE_OP_NEVER,
Chia-I Wub043fe32014-10-06 15:30:33 +0800688 .minLod = 0.0f,
689 .maxLod = 0.0f,
Tony Barbour8205d902015-04-16 15:59:00 -0600690 .borderColor = VK_BORDER_COLOR_OPAQUE_WHITE,
Chia-I Wub043fe32014-10-06 15:30:33 +0800691 };
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600692 VkImageViewCreateInfo view = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600693 .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
Chia-I Wub043fe32014-10-06 15:30:33 +0800694 .pNext = NULL,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600695 .image = VK_NULL_HANDLE,
Tony Barbour8205d902015-04-16 15:59:00 -0600696 .viewType = VK_IMAGE_VIEW_TYPE_2D,
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700697 .format = tex_format,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600698 .channels = { VK_CHANNEL_SWIZZLE_R,
699 VK_CHANNEL_SWIZZLE_G,
700 VK_CHANNEL_SWIZZLE_B,
701 VK_CHANNEL_SWIZZLE_A, },
702 .subresourceRange = { VK_IMAGE_ASPECT_COLOR, 0, 1, 0, 1 },
Chia-I Wub043fe32014-10-06 15:30:33 +0800703 };
Jon Ashburna9ae3832015-01-16 09:37:43 -0700704
Chia-I Wub043fe32014-10-06 15:30:33 +0800705 /* create sampler */
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600706 err = vkCreateSampler(demo->device, &sampler,
Chia-I Wub043fe32014-10-06 15:30:33 +0800707 &demo->textures[i].sampler);
708 assert(!err);
709
Chia-I Wub043fe32014-10-06 15:30:33 +0800710 /* create image view */
711 view.image = demo->textures[i].image;
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600712 err = vkCreateImageView(demo->device, &view,
Courtney Goeltzenleuchter3226a6f2015-02-11 18:17:22 -0700713 &demo->textures[i].view);
Chia-I Wub043fe32014-10-06 15:30:33 +0800714 assert(!err);
Chia-I Wub043fe32014-10-06 15:30:33 +0800715 }
716}
717
Chia-I Wu99621bc2014-10-08 11:52:22 +0800718static void demo_prepare_vertices(struct demo *demo)
719{
720 const float vb[3][5] = {
721 /* position texcoord */
Chia-I Wue2504cb2015-04-22 14:20:52 +0800722 { -1.0f, -1.0f, 0.2f, 0.0f, 0.0f },
723 { 1.0f, -1.0f, 0.25f, 1.0f, 0.0f },
Chia-I Wu99621bc2014-10-08 11:52:22 +0800724 { 0.0f, 1.0f, 1.0f, 0.5f, 1.0f },
725 };
Courtney Goeltzenleuchterddcb6192015-04-14 18:48:46 -0600726 const VkBufferCreateInfo buf_info = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600727 .sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO,
Chia-I Wu714df452015-01-01 07:55:04 +0800728 .pNext = NULL,
729 .size = sizeof(vb),
Courtney Goeltzenleuchterad870812015-04-15 15:29:59 -0600730 .usage = VK_BUFFER_USAGE_VERTEX_BUFFER_BIT,
Chia-I Wu714df452015-01-01 07:55:04 +0800731 .flags = 0,
732 };
Courtney Goeltzenleuchterddcb6192015-04-14 18:48:46 -0600733 VkMemoryAllocInfo mem_alloc = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600734 .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOC_INFO,
Mark Lobodzinski97dcd042015-04-16 08:52:00 -0500735 .pNext = NULL,
Chia-I Wu714df452015-01-01 07:55:04 +0800736 .allocationSize = 0,
Tony Barbour8205d902015-04-16 15:59:00 -0600737 .memProps = VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT,
Chia-I Wu99621bc2014-10-08 11:52:22 +0800738 };
Mark Lobodzinski23182612015-05-29 09:32:35 -0500739 VkMemoryRequirements mem_reqs;
Tony Barbour22a30862015-04-22 09:02:32 -0600740 VkResult U_ASSERT_ONLY err;
Chia-I Wu99621bc2014-10-08 11:52:22 +0800741 void *data;
742
743 memset(&demo->vertices, 0, sizeof(demo->vertices));
744
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600745 err = vkCreateBuffer(demo->device, &buf_info, &demo->vertices.buf);
Chia-I Wu714df452015-01-01 07:55:04 +0800746 assert(!err);
747
Tony Barbour426b9052015-06-24 16:06:58 -0600748 err = vkGetObjectMemoryRequirements(demo->device,
749 VK_OBJECT_TYPE_BUFFER, demo->vertices.buf, &mem_reqs);
Chia-I Wu714df452015-01-01 07:55:04 +0800750
Mark Lobodzinski23182612015-05-29 09:32:35 -0500751 mem_alloc.allocationSize = mem_reqs.size;
Chia-I Wu99621bc2014-10-08 11:52:22 +0800752
Mark Lobodzinski23182612015-05-29 09:32:35 -0500753 err = vkAllocMemory(demo->device, &mem_alloc, &demo->vertices.mem);
754 assert(!err);
Chia-I Wu99621bc2014-10-08 11:52:22 +0800755
Mark Lobodzinski23182612015-05-29 09:32:35 -0500756 err = vkMapMemory(demo->device, demo->vertices.mem, 0, 0, 0, &data);
757 assert(!err);
Chia-I Wu99621bc2014-10-08 11:52:22 +0800758
Mark Lobodzinski23182612015-05-29 09:32:35 -0500759 memcpy(data, vb, sizeof(vb));
Chia-I Wu99621bc2014-10-08 11:52:22 +0800760
Mark Lobodzinski23182612015-05-29 09:32:35 -0500761 err = vkUnmapMemory(demo->device, demo->vertices.mem);
762 assert(!err);
763
764 err = vkBindObjectMemory(demo->device,
765 VK_OBJECT_TYPE_BUFFER, demo->vertices.buf,
766 demo->vertices.mem, 0);
767 assert(!err);
Chia-I Wu714df452015-01-01 07:55:04 +0800768
Mark Lobodzinski0e0fb5c2015-06-23 15:11:57 -0600769 demo->vertices.vi.sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO;
Chia-I Wu8d29d022014-10-08 12:14:39 +0800770 demo->vertices.vi.pNext = NULL;
771 demo->vertices.vi.bindingCount = 1;
772 demo->vertices.vi.pVertexBindingDescriptions = demo->vertices.vi_bindings;
773 demo->vertices.vi.attributeCount = 2;
774 demo->vertices.vi.pVertexAttributeDescriptions = demo->vertices.vi_attrs;
775
Courtney Goeltzenleuchterf5cdad02015-03-31 16:36:30 -0600776 demo->vertices.vi_bindings[0].binding = VERTEX_BUFFER_BIND_ID;
Chia-I Wu8d29d022014-10-08 12:14:39 +0800777 demo->vertices.vi_bindings[0].strideInBytes = sizeof(vb[0]);
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600778 demo->vertices.vi_bindings[0].stepRate = VK_VERTEX_INPUT_STEP_RATE_VERTEX;
Chia-I Wu8d29d022014-10-08 12:14:39 +0800779
Courtney Goeltzenleuchterf5cdad02015-03-31 16:36:30 -0600780 demo->vertices.vi_attrs[0].binding = VERTEX_BUFFER_BIND_ID;
781 demo->vertices.vi_attrs[0].location = 0;
Tony Barbour8205d902015-04-16 15:59:00 -0600782 demo->vertices.vi_attrs[0].format = VK_FORMAT_R32G32B32_SFLOAT;
Chia-I Wu8d29d022014-10-08 12:14:39 +0800783 demo->vertices.vi_attrs[0].offsetInBytes = 0;
784
Courtney Goeltzenleuchterf5cdad02015-03-31 16:36:30 -0600785 demo->vertices.vi_attrs[1].binding = VERTEX_BUFFER_BIND_ID;
786 demo->vertices.vi_attrs[1].location = 1;
Tony Barbour8205d902015-04-16 15:59:00 -0600787 demo->vertices.vi_attrs[1].format = VK_FORMAT_R32G32_SFLOAT;
Chia-I Wu8d29d022014-10-08 12:14:39 +0800788 demo->vertices.vi_attrs[1].offsetInBytes = sizeof(float) * 3;
Chia-I Wu99621bc2014-10-08 11:52:22 +0800789}
790
Chia-I Wuf8385062015-01-04 16:27:24 +0800791static void demo_prepare_descriptor_layout(struct demo *demo)
Chia-I Wub043fe32014-10-06 15:30:33 +0800792{
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600793 const VkDescriptorSetLayoutBinding layout_binding = {
Courtney Goeltzenleuchterad870812015-04-15 15:29:59 -0600794 .descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
Chia-I Wud3114a22015-05-25 16:22:52 +0800795 .arraySize = DEMO_TEXTURE_COUNT,
Tony Barbour8205d902015-04-16 15:59:00 -0600796 .stageFlags = VK_SHADER_STAGE_FRAGMENT_BIT,
Chia-I Wu310eece2015-03-27 12:56:09 +0800797 .pImmutableSamplers = NULL,
Chia-I Wub043fe32014-10-06 15:30:33 +0800798 };
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600799 const VkDescriptorSetLayoutCreateInfo descriptor_layout = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600800 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO,
Chia-I Wufc9d9132015-03-26 15:04:41 +0800801 .pNext = NULL,
802 .count = 1,
803 .pBinding = &layout_binding,
804 };
Tony Barbour22a30862015-04-22 09:02:32 -0600805 VkResult U_ASSERT_ONLY err;
Chia-I Wub043fe32014-10-06 15:30:33 +0800806
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600807 err = vkCreateDescriptorSetLayout(demo->device,
Chia-I Wu7732cb22015-03-26 15:27:55 +0800808 &descriptor_layout, &demo->desc_layout);
809 assert(!err);
810
Mark Lobodzinski556f7212015-04-17 14:11:39 -0500811 const VkPipelineLayoutCreateInfo pPipelineLayoutCreateInfo = {
812 .sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO,
813 .pNext = NULL,
814 .descriptorSetCount = 1,
815 .pSetLayouts = &demo->desc_layout,
816 };
817
818 err = vkCreatePipelineLayout(demo->device,
819 &pPipelineLayoutCreateInfo,
820 &demo->pipeline_layout);
Chia-I Wub043fe32014-10-06 15:30:33 +0800821 assert(!err);
Chia-I Wub043fe32014-10-06 15:30:33 +0800822}
823
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600824static VkShader demo_prepare_shader(struct demo *demo,
Tony Barbour8205d902015-04-16 15:59:00 -0600825 VkShaderStage stage,
Chia-I Wuc19795a2014-09-13 11:12:55 +0800826 const void *code,
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600827 size_t size)
Chia-I Wuc19795a2014-09-13 11:12:55 +0800828{
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600829 VkShaderCreateInfo createInfo;
830 VkShader shader;
831 VkResult err;
Chia-I Wuc19795a2014-09-13 11:12:55 +0800832
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600833 createInfo.sType = VK_STRUCTURE_TYPE_SHADER_CREATE_INFO;
Courtney Goeltzenleuchter3f2606d2014-10-13 17:51:58 -0600834 createInfo.pNext = NULL;
835
Cody Northrop75db0322015-05-28 11:27:16 -0600836 if (!demo->use_glsl) {
837 createInfo.codeSize = size;
838 createInfo.pCode = code;
839 createInfo.flags = 0;
Courtney Goeltzenleuchter3f2606d2014-10-13 17:51:58 -0600840
Cody Northrop75db0322015-05-28 11:27:16 -0600841 err = vkCreateShader(demo->device, &createInfo, &shader);
842 if (err) {
843 free((void *)createInfo.pCode);
844 }
845 } else {
846 // Create fake SPV structure to feed GLSL
847 // to the driver "under the covers"
848 createInfo.codeSize = 3 * sizeof(uint32_t) + size + 1;
849 createInfo.pCode = malloc(createInfo.codeSize);
850 createInfo.flags = 0;
Courtney Goeltzenleuchter3f2606d2014-10-13 17:51:58 -0600851
Cody Northrop75db0322015-05-28 11:27:16 -0600852 /* try version 0 first: VkShaderStage followed by GLSL */
853 ((uint32_t *) createInfo.pCode)[0] = ICD_SPV_MAGIC;
854 ((uint32_t *) createInfo.pCode)[1] = 0;
855 ((uint32_t *) createInfo.pCode)[2] = stage;
856 memcpy(((uint32_t *) createInfo.pCode + 3), code, size + 1);
857
858 err = vkCreateShader(demo->device, &createInfo, &shader);
859 if (err) {
860 free((void *) createInfo.pCode);
861 return VK_NULL_HANDLE;
862 }
Courtney Goeltzenleuchter3f2606d2014-10-13 17:51:58 -0600863 }
Chia-I Wuc19795a2014-09-13 11:12:55 +0800864
865 return shader;
866}
867
Cody Northrop75db0322015-05-28 11:27:16 -0600868char *demo_read_spv(const char *filename, size_t *psize)
869{
870 long int size;
871 void *shader_code;
872
873 FILE *fp = fopen(filename, "rb");
874 if (!fp) return NULL;
875
876 fseek(fp, 0L, SEEK_END);
877 size = ftell(fp);
878
879 fseek(fp, 0L, SEEK_SET);
880
881 shader_code = malloc(size);
882 fread(shader_code, size, 1, fp);
883
884 *psize = size;
885
886 return shader_code;
887}
888
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600889static VkShader demo_prepare_vs(struct demo *demo)
Chia-I Wuc19795a2014-09-13 11:12:55 +0800890{
Cody Northrop75db0322015-05-28 11:27:16 -0600891 if (!demo->use_glsl) {
892 void *vertShaderCode;
893 size_t size;
894
895 vertShaderCode = demo_read_spv("tri-vert.spv", &size);
896
897 return demo_prepare_shader(demo, VK_SHADER_STAGE_VERTEX,
898 vertShaderCode, size);
899 } else {
900 static const char *vertShaderText =
Mark Lobodzinskiba4d2f02015-04-06 15:24:40 -0500901 "#version 140\n"
Courtney Goeltzenleuchterf5cdad02015-03-31 16:36:30 -0600902 "#extension GL_ARB_separate_shader_objects : enable\n"
903 "#extension GL_ARB_shading_language_420pack : enable\n"
904 "layout (location = 0) in vec4 pos;\n"
905 "layout (location = 1) in vec2 attr;\n"
Chia-I Wuf5caeb02014-10-25 12:11:27 +0800906 "out vec2 texcoord;\n"
Courtney Goeltzenleuchter3f2606d2014-10-13 17:51:58 -0600907 "void main() {\n"
Chia-I Wuf5caeb02014-10-25 12:11:27 +0800908 " texcoord = attr;\n"
909 " gl_Position = pos;\n"
Courtney Goeltzenleuchter3f2606d2014-10-13 17:51:58 -0600910 "}\n";
Courtney Goeltzenleuchteref7301b2014-09-17 13:17:12 -0600911
Cody Northrop75db0322015-05-28 11:27:16 -0600912 return demo_prepare_shader(demo, VK_SHADER_STAGE_VERTEX,
913 (const void *) vertShaderText,
914 strlen(vertShaderText));
915 }
Chia-I Wuc19795a2014-09-13 11:12:55 +0800916}
917
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600918static VkShader demo_prepare_fs(struct demo *demo)
Chia-I Wuc19795a2014-09-13 11:12:55 +0800919{
Cody Northrop75db0322015-05-28 11:27:16 -0600920 if (!demo->use_glsl) {
921 void *fragShaderCode;
922 size_t size;
Courtney Goeltzenleuchteref7301b2014-09-17 13:17:12 -0600923
Cody Northrop75db0322015-05-28 11:27:16 -0600924 fragShaderCode = demo_read_spv("tri-frag.spv", &size);
925
926 return demo_prepare_shader(demo, VK_SHADER_STAGE_FRAGMENT,
927 fragShaderCode, size);
928 } else {
929 static const char *fragShaderText =
930 "#version 140\n"
931 "#extension GL_ARB_separate_shader_objects : enable\n"
932 "#extension GL_ARB_shading_language_420pack : enable\n"
933 "layout (binding = 0) uniform sampler2D tex;\n"
934 "layout (location = 0) in vec2 texcoord;\n"
935 "layout (location = 0) out vec4 uFragColor;\n"
936 "void main() {\n"
937 " uFragColor = texture(tex, texcoord);\n"
938 "}\n";
939
940 return demo_prepare_shader(demo, VK_SHADER_STAGE_FRAGMENT,
941 (const void *) fragShaderText,
942 strlen(fragShaderText));
943 }
Chia-I Wuc19795a2014-09-13 11:12:55 +0800944}
945
946static void demo_prepare_pipeline(struct demo *demo)
947{
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600948 VkGraphicsPipelineCreateInfo pipeline;
Mark Lobodzinski0e0fb5c2015-06-23 15:11:57 -0600949
950 VkPipelineVertexInputStateCreateInfo vi;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600951 VkPipelineIaStateCreateInfo ia;
952 VkPipelineRsStateCreateInfo rs;
953 VkPipelineCbStateCreateInfo cb;
954 VkPipelineDsStateCreateInfo ds;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600955 VkPipelineVpStateCreateInfo vp;
956 VkPipelineMsStateCreateInfo ms;
Mark Lobodzinski0e0fb5c2015-06-23 15:11:57 -0600957
Tony Barbour22a30862015-04-22 09:02:32 -0600958 VkResult U_ASSERT_ONLY err;
Chia-I Wuc19795a2014-09-13 11:12:55 +0800959
960 memset(&pipeline, 0, sizeof(pipeline));
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600961 pipeline.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
Mark Lobodzinski556f7212015-04-17 14:11:39 -0500962 pipeline.layout = demo->pipeline_layout;
Chia-I Wuc19795a2014-09-13 11:12:55 +0800963
Chia-I Wu8d29d022014-10-08 12:14:39 +0800964 vi = demo->vertices.vi;
965
Chia-I Wuc19795a2014-09-13 11:12:55 +0800966 memset(&ia, 0, sizeof(ia));
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600967 ia.sType = VK_STRUCTURE_TYPE_PIPELINE_IA_STATE_CREATE_INFO;
Tony Barbour8205d902015-04-16 15:59:00 -0600968 ia.topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
Chia-I Wuc19795a2014-09-13 11:12:55 +0800969
970 memset(&rs, 0, sizeof(rs));
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600971 rs.sType = VK_STRUCTURE_TYPE_PIPELINE_RS_STATE_CREATE_INFO;
Tony Barbour8205d902015-04-16 15:59:00 -0600972 rs.fillMode = VK_FILL_MODE_SOLID;
Chia-I Wuc414ba82015-04-22 15:44:24 +0800973 rs.cullMode = VK_CULL_MODE_BACK;
974 rs.frontFace = VK_FRONT_FACE_CW;
Chia-I Wue2504cb2015-04-22 14:20:52 +0800975 rs.depthClipEnable = VK_TRUE;
Chia-I Wuc19795a2014-09-13 11:12:55 +0800976
977 memset(&cb, 0, sizeof(cb));
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600978 cb.sType = VK_STRUCTURE_TYPE_PIPELINE_CB_STATE_CREATE_INFO;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600979 VkPipelineCbAttachmentState att_state[1];
Tony Barbourfa6cac72015-01-16 14:27:35 -0700980 memset(att_state, 0, sizeof(att_state));
981 att_state[0].format = demo->format;
982 att_state[0].channelWriteMask = 0xf;
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600983 att_state[0].blendEnable = VK_FALSE;
Tony Barbourfa6cac72015-01-16 14:27:35 -0700984 cb.attachmentCount = 1;
985 cb.pAttachments = att_state;
Chia-I Wuc19795a2014-09-13 11:12:55 +0800986
Tony Barbourfa6cac72015-01-16 14:27:35 -0700987 memset(&vp, 0, sizeof(vp));
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600988 vp.sType = VK_STRUCTURE_TYPE_PIPELINE_VP_STATE_CREATE_INFO;
Tony Barbour8205d902015-04-16 15:59:00 -0600989 vp.viewportCount = 1;
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600990 vp.clipOrigin = VK_COORDINATE_ORIGIN_UPPER_LEFT;
Tony Barbourfa6cac72015-01-16 14:27:35 -0700991
992 memset(&ds, 0, sizeof(ds));
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600993 ds.sType = VK_STRUCTURE_TYPE_PIPELINE_DS_STATE_CREATE_INFO;
Tony Barbourfa6cac72015-01-16 14:27:35 -0700994 ds.format = demo->depth.format;
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600995 ds.depthTestEnable = VK_TRUE;
996 ds.depthWriteEnable = VK_TRUE;
Tony Barbour8205d902015-04-16 15:59:00 -0600997 ds.depthCompareOp = VK_COMPARE_OP_LESS_EQUAL;
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600998 ds.depthBoundsEnable = VK_FALSE;
999 ds.back.stencilFailOp = VK_STENCIL_OP_KEEP;
1000 ds.back.stencilPassOp = VK_STENCIL_OP_KEEP;
Tony Barbour8205d902015-04-16 15:59:00 -06001001 ds.back.stencilCompareOp = VK_COMPARE_OP_ALWAYS;
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001002 ds.stencilTestEnable = VK_FALSE;
Tony Barbourfa6cac72015-01-16 14:27:35 -07001003 ds.front = ds.back;
Chia-I Wuc19795a2014-09-13 11:12:55 +08001004
Tony Barbourfa6cac72015-01-16 14:27:35 -07001005 memset(&ms, 0, sizeof(ms));
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001006 ms.sType = VK_STRUCTURE_TYPE_PIPELINE_MS_STATE_CREATE_INFO;
Tony Barbourfa6cac72015-01-16 14:27:35 -07001007 ms.sampleMask = 1;
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001008 ms.multisampleEnable = VK_FALSE;
Tony Barboure094edf2015-06-26 10:18:34 -06001009 ms.rasterSamples = 1;
Chia-I Wub043fe32014-10-06 15:30:33 +08001010
Mark Lobodzinski0e0fb5c2015-06-23 15:11:57 -06001011 // Two stages: vs and fs
1012 pipeline.stageCount = 2;
1013 VkPipelineShaderStageCreateInfo shaderStages[2];
1014 memset(&shaderStages, 0, 2 * sizeof(VkPipelineShaderStageCreateInfo));
1015
1016 shaderStages[0].sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
1017 shaderStages[0].stage = VK_SHADER_STAGE_VERTEX;
1018 shaderStages[0].shader = demo_prepare_vs(demo);
1019 shaderStages[0].linkConstBufferCount = 0;
1020
1021 shaderStages[1].sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
1022 shaderStages[1].stage = VK_SHADER_STAGE_FRAGMENT;
1023 shaderStages[1].shader = demo_prepare_fs(demo);
1024
1025 pipeline.pVertexInputState = &vi;
1026 pipeline.pIaState = &ia;
1027 pipeline.pRsState = &rs;
1028 pipeline.pCbState = &cb;
1029 pipeline.pMsState = &ms;
1030 pipeline.pVpState = &vp;
1031 pipeline.pDsState = &ds;
1032 pipeline.pStages = shaderStages;
Chia-I Wuc19795a2014-09-13 11:12:55 +08001033
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001034 err = vkCreateGraphicsPipeline(demo->device, &pipeline, &demo->pipeline);
Chia-I Wuc19795a2014-09-13 11:12:55 +08001035 assert(!err);
1036
Mark Lobodzinski0e0fb5c2015-06-23 15:11:57 -06001037 for (uint32_t i = 0; i < pipeline.stageCount; i++) {
1038 vkDestroyObject(demo->device, VK_OBJECT_TYPE_SHADER, shaderStages[i].shader);
1039 }
Chia-I Wuc19795a2014-09-13 11:12:55 +08001040}
1041
1042static void demo_prepare_dynamic_states(struct demo *demo)
1043{
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001044 VkDynamicVpStateCreateInfo viewport_create;
1045 VkDynamicRsStateCreateInfo raster;
1046 VkDynamicCbStateCreateInfo color_blend;
1047 VkDynamicDsStateCreateInfo depth_stencil;
Tony Barbour22a30862015-04-22 09:02:32 -06001048 VkResult U_ASSERT_ONLY err;
Chia-I Wuc19795a2014-09-13 11:12:55 +08001049
Tony Barbourfa6cac72015-01-16 14:27:35 -07001050 memset(&viewport_create, 0, sizeof(viewport_create));
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001051 viewport_create.sType = VK_STRUCTURE_TYPE_DYNAMIC_VP_STATE_CREATE_INFO;
Courtney Goeltzenleuchterc6e32f92015-02-11 14:13:34 -07001052 viewport_create.viewportAndScissorCount = 1;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001053 VkViewport viewport;
Chia-I Wuc19795a2014-09-13 11:12:55 +08001054 memset(&viewport, 0, sizeof(viewport));
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -06001055 viewport.height = (float) demo->height;
Courtney Goeltzenleuchterc6e32f92015-02-11 14:13:34 -07001056 viewport.width = (float) demo->width;
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -06001057 viewport.minDepth = (float) 0.0f;
1058 viewport.maxDepth = (float) 1.0f;
Piers Daniell886be472015-02-23 16:23:13 -07001059 viewport_create.pViewports = &viewport;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001060 VkRect scissor;
Piers Daniell886be472015-02-23 16:23:13 -07001061 memset(&scissor, 0, sizeof(scissor));
Courtney Goeltzenleuchterc6e32f92015-02-11 14:13:34 -07001062 scissor.extent.width = demo->width;
1063 scissor.extent.height = demo->height;
1064 scissor.offset.x = 0;
1065 scissor.offset.y = 0;
Courtney Goeltzenleuchterc6e32f92015-02-11 14:13:34 -07001066 viewport_create.pScissors = &scissor;
Chia-I Wuc19795a2014-09-13 11:12:55 +08001067
1068 memset(&raster, 0, sizeof(raster));
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001069 raster.sType = VK_STRUCTURE_TYPE_DYNAMIC_RS_STATE_CREATE_INFO;
Piers Daniell886be472015-02-23 16:23:13 -07001070 raster.lineWidth = 1.0;
Chia-I Wuc19795a2014-09-13 11:12:55 +08001071
1072 memset(&color_blend, 0, sizeof(color_blend));
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001073 color_blend.sType = VK_STRUCTURE_TYPE_DYNAMIC_CB_STATE_CREATE_INFO;
Piers Daniell886be472015-02-23 16:23:13 -07001074 color_blend.blendConst[0] = 1.0f;
1075 color_blend.blendConst[1] = 1.0f;
1076 color_blend.blendConst[2] = 1.0f;
1077 color_blend.blendConst[3] = 1.0f;
Chia-I Wuc19795a2014-09-13 11:12:55 +08001078
1079 memset(&depth_stencil, 0, sizeof(depth_stencil));
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001080 depth_stencil.sType = VK_STRUCTURE_TYPE_DYNAMIC_DS_STATE_CREATE_INFO;
Mark Lobodzinski4405fbf2015-06-12 11:14:17 -06001081 depth_stencil.minDepthBounds = 0.0f;
1082 depth_stencil.maxDepthBounds = 1.0f;
Tony Barbourfa6cac72015-01-16 14:27:35 -07001083 depth_stencil.stencilBackRef = 0;
1084 depth_stencil.stencilFrontRef = 0;
1085 depth_stencil.stencilReadMask = 0xff;
1086 depth_stencil.stencilWriteMask = 0xff;
Chia-I Wuc19795a2014-09-13 11:12:55 +08001087
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001088 err = vkCreateDynamicViewportState(demo->device, &viewport_create, &demo->viewport);
Chia-I Wuc19795a2014-09-13 11:12:55 +08001089 assert(!err);
1090
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001091 err = vkCreateDynamicRasterState(demo->device, &raster, &demo->raster);
Chia-I Wuc19795a2014-09-13 11:12:55 +08001092 assert(!err);
1093
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001094 err = vkCreateDynamicColorBlendState(demo->device,
Chia-I Wuc19795a2014-09-13 11:12:55 +08001095 &color_blend, &demo->color_blend);
1096 assert(!err);
1097
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001098 err = vkCreateDynamicDepthStencilState(demo->device,
Chia-I Wuc19795a2014-09-13 11:12:55 +08001099 &depth_stencil, &demo->depth_stencil);
1100 assert(!err);
1101}
1102
Chia-I Wu8d24b3b2015-03-26 13:14:16 +08001103static void demo_prepare_descriptor_pool(struct demo *demo)
Chia-I Wuf8385062015-01-04 16:27:24 +08001104{
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001105 const VkDescriptorTypeCount type_count = {
Courtney Goeltzenleuchterad870812015-04-15 15:29:59 -06001106 .type = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
Chia-I Wuf8385062015-01-04 16:27:24 +08001107 .count = DEMO_TEXTURE_COUNT,
1108 };
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001109 const VkDescriptorPoolCreateInfo descriptor_pool = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001110 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO,
Chia-I Wuf8385062015-01-04 16:27:24 +08001111 .pNext = NULL,
1112 .count = 1,
1113 .pTypeCount = &type_count,
1114 };
Tony Barbour22a30862015-04-22 09:02:32 -06001115 VkResult U_ASSERT_ONLY err;
Chia-I Wuf8385062015-01-04 16:27:24 +08001116
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001117 err = vkCreateDescriptorPool(demo->device,
1118 VK_DESCRIPTOR_POOL_USAGE_ONE_SHOT, 1,
Chia-I Wu8d24b3b2015-03-26 13:14:16 +08001119 &descriptor_pool, &demo->desc_pool);
Chia-I Wuf8385062015-01-04 16:27:24 +08001120 assert(!err);
1121}
1122
1123static void demo_prepare_descriptor_set(struct demo *demo)
1124{
Chia-I Wu8cd8ecd2015-05-25 16:27:55 +08001125 VkDescriptorInfo tex_descs[DEMO_TEXTURE_COUNT];
1126 VkWriteDescriptorSet write;
Tony Barbour22a30862015-04-22 09:02:32 -06001127 VkResult U_ASSERT_ONLY err;
Chia-I Wuf8385062015-01-04 16:27:24 +08001128 uint32_t count;
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -06001129 uint32_t i;
Chia-I Wuf8385062015-01-04 16:27:24 +08001130
Mike Stroyan230e6252015-04-17 12:36:38 -06001131 err = vkAllocDescriptorSets(demo->device, demo->desc_pool,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001132 VK_DESCRIPTOR_SET_USAGE_STATIC,
Chia-I Wuf8385062015-01-04 16:27:24 +08001133 1, &demo->desc_layout,
1134 &demo->desc_set, &count);
1135 assert(!err && count == 1);
1136
Chia-I Wu8cd8ecd2015-05-25 16:27:55 +08001137 memset(&tex_descs, 0, sizeof(tex_descs));
1138 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
1139 tex_descs[i].sampler = demo->textures[i].sampler;
1140 tex_descs[i].imageView = demo->textures[i].view;
1141 tex_descs[i].imageLayout = VK_IMAGE_LAYOUT_GENERAL;
1142 }
1143
1144 memset(&write, 0, sizeof(write));
1145 write.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
1146 write.destSet = demo->desc_set;
1147 write.count = DEMO_TEXTURE_COUNT;
1148 write.descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
1149 write.pDescriptors = tex_descs;
1150
1151 err = vkUpdateDescriptorSets(demo->device, 1, &write, 0, NULL);
1152 assert(!err);
Chia-I Wuf8385062015-01-04 16:27:24 +08001153}
1154
Chia-I Wuc19795a2014-09-13 11:12:55 +08001155static void demo_prepare(struct demo *demo)
1156{
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001157 const VkCmdBufferCreateInfo cmd = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001158 .sType = VK_STRUCTURE_TYPE_CMD_BUFFER_CREATE_INFO,
Chia-I Wuc19795a2014-09-13 11:12:55 +08001159 .pNext = NULL,
Courtney Goeltzenleuchterf3168062015-03-05 18:09:39 -07001160 .queueNodeIndex = demo->graphics_queue_node_index,
Chia-I Wuc19795a2014-09-13 11:12:55 +08001161 .flags = 0,
1162 };
Tony Barbour22a30862015-04-22 09:02:32 -06001163 VkResult U_ASSERT_ONLY err;
Chia-I Wuc19795a2014-09-13 11:12:55 +08001164
Courtney Goeltzenleuchter633f9c52015-04-30 10:58:33 -06001165 err = vkCreateCommandBuffer(demo->device, &cmd, &demo->draw_cmd);
1166 assert(!err);
1167
Chia-I Wuc19795a2014-09-13 11:12:55 +08001168 demo_prepare_buffers(demo);
Chia-I Wu9ae87c92014-10-07 14:15:01 +08001169 demo_prepare_depth(demo);
Chia-I Wub043fe32014-10-06 15:30:33 +08001170 demo_prepare_textures(demo);
Chia-I Wu99621bc2014-10-08 11:52:22 +08001171 demo_prepare_vertices(demo);
Chia-I Wuf8385062015-01-04 16:27:24 +08001172 demo_prepare_descriptor_layout(demo);
Chia-I Wuc19795a2014-09-13 11:12:55 +08001173 demo_prepare_pipeline(demo);
1174 demo_prepare_dynamic_states(demo);
1175
Chia-I Wu8d24b3b2015-03-26 13:14:16 +08001176 demo_prepare_descriptor_pool(demo);
Chia-I Wuf8385062015-01-04 16:27:24 +08001177 demo_prepare_descriptor_set(demo);
Courtney Goeltzenleuchter633f9c52015-04-30 10:58:33 -06001178 demo->prepared = true;
Chia-I Wuc19795a2014-09-13 11:12:55 +08001179}
1180
Ian Elliotte14e9f92015-04-16 15:23:05 -06001181#ifdef _WIN32
1182static void demo_run(struct demo *demo)
1183{
Courtney Goeltzenleuchter633f9c52015-04-30 10:58:33 -06001184 if (!demo->prepared)
1185 return;
Ian Elliotte14e9f92015-04-16 15:23:05 -06001186 demo_draw(demo);
1187}
1188
1189// On MS-Windows, make this a global, so it's available to WndProc()
1190struct demo demo;
1191
1192// MS-Windows event handling function:
1193LRESULT CALLBACK WndProc(HWND hWnd,
1194 UINT uMsg,
1195 WPARAM wParam,
1196 LPARAM lParam)
1197{
Ian Elliott4e19ed02015-04-28 10:52:52 -06001198 char tmp_str[] = APP_LONG_NAME;
Ian Elliotte14e9f92015-04-16 15:23:05 -06001199
1200 switch(uMsg)
1201 {
1202 case WM_CREATE:
1203 return 0;
1204 case WM_CLOSE:
1205 PostQuitMessage(0);
1206 return 0;
1207 case WM_PAINT:
1208 demo_run(&demo);
1209 return 0;
1210 default:
1211 break;
1212 }
1213 return (DefWindowProc(hWnd, uMsg, wParam, lParam));
1214}
1215
1216static void demo_create_window(struct demo *demo)
1217{
1218 WNDCLASSEX win_class;
1219
1220 // Initialize the window class structure:
1221 win_class.cbSize = sizeof(WNDCLASSEX);
1222 win_class.style = CS_HREDRAW | CS_VREDRAW;
1223 win_class.lpfnWndProc = WndProc;
1224 win_class.cbClsExtra = 0;
1225 win_class.cbWndExtra = 0;
1226 win_class.hInstance = demo->connection; // hInstance
1227 win_class.hIcon = LoadIcon(NULL, IDI_APPLICATION);
1228 win_class.hCursor = LoadCursor(NULL, IDC_ARROW);
1229 win_class.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
1230 win_class.lpszMenuName = NULL;
1231 win_class.lpszClassName = demo->name;
1232 win_class.hIconSm = LoadIcon(NULL, IDI_WINLOGO);
1233 // Register window class:
1234 if (!RegisterClassEx(&win_class)) {
1235 // It didn't work, so try to give a useful error:
1236 printf("Unexpected error trying to start the application!\n");
1237 fflush(stdout);
1238 exit(1);
1239 }
1240 // Create window with the registered class:
Mike Stroyan7eef5742015-06-15 14:19:19 -06001241 RECT wr = { 0, 0, demo->width, demo->height };
1242 AdjustWindowRect(&wr, WS_OVERLAPPEDWINDOW, FALSE);
Ian Elliotte14e9f92015-04-16 15:23:05 -06001243 demo->window = CreateWindowEx(0,
1244 demo->name, // class name
1245 demo->name, // app name
1246 WS_OVERLAPPEDWINDOW | // window style
1247 WS_VISIBLE |
1248 WS_SYSMENU,
1249 100,100, // x/y coords
Mike Stroyan7eef5742015-06-15 14:19:19 -06001250 wr.right-wr.left, // width
1251 wr.bottom-wr.top, // height
Ian Elliotte14e9f92015-04-16 15:23:05 -06001252 NULL, // handle to parent
1253 NULL, // handle to menu
1254 demo->connection, // hInstance
1255 NULL); // no extra parameters
1256 if (!demo->window) {
1257 // It didn't work, so try to give a useful error:
1258 printf("Cannot create a window in which to draw!\n");
1259 fflush(stdout);
1260 exit(1);
1261 }
1262}
1263#else // _WIN32
1264
Chia-I Wuc19795a2014-09-13 11:12:55 +08001265static void demo_handle_event(struct demo *demo,
1266 const xcb_generic_event_t *event)
1267{
1268 switch (event->response_type & 0x7f) {
1269 case XCB_EXPOSE:
1270 demo_draw(demo);
1271 break;
Courtney Goeltzenleuchterca698052014-11-07 15:17:03 -07001272 case XCB_CLIENT_MESSAGE:
1273 if((*(xcb_client_message_event_t*)event).data.data32[0] ==
1274 (*demo->atom_wm_delete_window).atom) {
1275 demo->quit = true;
1276 }
1277 break;
Chia-I Wuc19795a2014-09-13 11:12:55 +08001278 case XCB_KEY_RELEASE:
1279 {
1280 const xcb_key_release_event_t *key =
1281 (const xcb_key_release_event_t *) event;
1282
1283 if (key->detail == 0x9)
1284 demo->quit = true;
1285 }
1286 break;
Courtney Goeltzenleuchterca698052014-11-07 15:17:03 -07001287 case XCB_DESTROY_NOTIFY:
1288 demo->quit = true;
1289 break;
Chia-I Wuc19795a2014-09-13 11:12:55 +08001290 default:
1291 break;
1292 }
1293}
1294
1295static void demo_run(struct demo *demo)
1296{
1297 xcb_flush(demo->connection);
1298
1299 while (!demo->quit) {
1300 xcb_generic_event_t *event;
1301
1302 event = xcb_wait_for_event(demo->connection);
1303 if (event) {
1304 demo_handle_event(demo, event);
1305 free(event);
1306 }
1307 }
1308}
1309
1310static void demo_create_window(struct demo *demo)
1311{
1312 uint32_t value_mask, value_list[32];
1313
1314 demo->window = xcb_generate_id(demo->connection);
1315
1316 value_mask = XCB_CW_BACK_PIXEL | XCB_CW_EVENT_MASK;
1317 value_list[0] = demo->screen->black_pixel;
1318 value_list[1] = XCB_EVENT_MASK_KEY_RELEASE |
Courtney Goeltzenleuchterca698052014-11-07 15:17:03 -07001319 XCB_EVENT_MASK_EXPOSURE |
1320 XCB_EVENT_MASK_STRUCTURE_NOTIFY;
Chia-I Wuc19795a2014-09-13 11:12:55 +08001321
1322 xcb_create_window(demo->connection,
1323 XCB_COPY_FROM_PARENT,
1324 demo->window, demo->screen->root,
1325 0, 0, demo->width, demo->height, 0,
1326 XCB_WINDOW_CLASS_INPUT_OUTPUT,
1327 demo->screen->root_visual,
1328 value_mask, value_list);
1329
Courtney Goeltzenleuchterca698052014-11-07 15:17:03 -07001330 /* Magic code that will send notification when window is destroyed */
1331 xcb_intern_atom_cookie_t cookie = xcb_intern_atom(demo->connection, 1, 12,
1332 "WM_PROTOCOLS");
1333 xcb_intern_atom_reply_t* reply = xcb_intern_atom_reply(demo->connection, cookie, 0);
1334
1335 xcb_intern_atom_cookie_t cookie2 = xcb_intern_atom(demo->connection, 0, 16, "WM_DELETE_WINDOW");
1336 demo->atom_wm_delete_window = xcb_intern_atom_reply(demo->connection, cookie2, 0);
1337
1338 xcb_change_property(demo->connection, XCB_PROP_MODE_REPLACE,
1339 demo->window, (*reply).atom, 4, 32, 1,
1340 &(*demo->atom_wm_delete_window).atom);
1341 free(reply);
1342
Chia-I Wuc19795a2014-09-13 11:12:55 +08001343 xcb_map_window(demo->connection, demo->window);
1344}
Ian Elliotte14e9f92015-04-16 15:23:05 -06001345#endif // _WIN32
Chia-I Wuc19795a2014-09-13 11:12:55 +08001346
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001347static void demo_init_vk(struct demo *demo)
Chia-I Wuc19795a2014-09-13 11:12:55 +08001348{
Tobin Ehlis3536b442015-04-16 18:04:57 -06001349 VkResult err;
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -06001350 VkExtensionProperties *instance_extensions;
1351 uint32_t instance_extension_count = 0;
1352 VkExtensionProperties *device_extensions;
1353 uint32_t device_extension_count = 0;
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -06001354 uint32_t total_extension_count = 0;
Tony Barbour426b9052015-06-24 16:06:58 -06001355 err = vkGetGlobalExtensionCount(&total_extension_count);
Tobin Ehlis3536b442015-04-16 18:04:57 -06001356 assert(!err);
1357
1358 VkExtensionProperties extProp;
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -06001359 bool32_t WSIextFound = 0;
1360 instance_extensions = malloc(sizeof(VkExtensionProperties) * total_extension_count);
1361 device_extensions = malloc(sizeof(VkExtensionProperties) * total_extension_count);
1362 for (uint32_t i = 0; i < total_extension_count; i++) {
Tony Barbour426b9052015-06-24 16:06:58 -06001363 err = vkGetGlobalExtensionProperties(i, &extProp);
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -06001364 if (!strcmp("VK_WSI_LunarG", extProp.name)) {
1365 WSIextFound = 1;
1366 memcpy(&instance_extensions[instance_extension_count++], &extProp, sizeof(VkExtensionProperties));
1367 memcpy(&device_extensions[device_extension_count++], &extProp, sizeof(VkExtensionProperties));
1368 }
Tobin Ehlis3536b442015-04-16 18:04:57 -06001369 }
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -06001370 if (!WSIextFound) {
Tony Barbour426b9052015-06-24 16:06:58 -06001371 ERR_EXIT("vkGetGlobalExtensionProperties failed to find the "
Ian Elliott3b375cf2015-04-28 13:22:33 -06001372 "\"VK_WSI_LunarG\" extension.\n\nDo you have a compatible "
1373 "Vulkan installable client driver (ICD) installed?\nPlease "
1374 "look at the Getting Started guide for additional "
1375 "information.\n",
1376 "vkCreateInstance Failure");
1377 }
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001378 const VkApplicationInfo app = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001379 .sType = VK_STRUCTURE_TYPE_APPLICATION_INFO,
Chia-I Wuc19795a2014-09-13 11:12:55 +08001380 .pNext = NULL,
Ian Elliott4e19ed02015-04-28 10:52:52 -06001381 .pAppName = APP_SHORT_NAME,
Chia-I Wuc19795a2014-09-13 11:12:55 +08001382 .appVersion = 0,
Ian Elliott4e19ed02015-04-28 10:52:52 -06001383 .pEngineName = APP_SHORT_NAME,
Chia-I Wuc19795a2014-09-13 11:12:55 +08001384 .engineVersion = 0,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001385 .apiVersion = VK_API_VERSION,
Chia-I Wuc19795a2014-09-13 11:12:55 +08001386 };
Courtney Goeltzenleuchterddcb6192015-04-14 18:48:46 -06001387 const VkInstanceCreateInfo inst_info = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001388 .sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO,
Jon Ashburn29669a42015-04-04 14:52:07 -06001389 .pNext = NULL,
1390 .pAppInfo = &app,
1391 .pAllocCb = NULL,
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -06001392 .extensionCount = instance_extension_count,
1393 .pEnabledExtensions = instance_extensions,
Jon Ashburn29669a42015-04-04 14:52:07 -06001394 };
Courtney Goeltzenleuchterddcb6192015-04-14 18:48:46 -06001395 const VkDeviceQueueCreateInfo queue = {
Chia-I Wuc19795a2014-09-13 11:12:55 +08001396 .queueNodeIndex = 0,
1397 .queueCount = 1,
1398 };
Courtney Goeltzenleuchterddcb6192015-04-14 18:48:46 -06001399 const VkDeviceCreateInfo device = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001400 .sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO,
Chia-I Wuc19795a2014-09-13 11:12:55 +08001401 .pNext = NULL,
1402 .queueRecordCount = 1,
1403 .pRequestedQueues = &queue,
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -06001404 .extensionCount = device_extension_count,
1405 .pEnabledExtensions = device_extensions,
Jon Ashburn80d3b712015-05-18 09:06:15 -06001406 .flags = 0,
Chia-I Wuc19795a2014-09-13 11:12:55 +08001407 };
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -06001408 uint32_t gpu_count;
1409 uint32_t i;
Courtney Goeltzenleuchterf3168062015-03-05 18:09:39 -07001410 uint32_t queue_count;
Chia-I Wuc19795a2014-09-13 11:12:55 +08001411
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001412 err = vkCreateInstance(&inst_info, &demo->inst);
Ian Elliottcaa9f272015-04-28 11:35:02 -06001413 if (err == VK_ERROR_INCOMPATIBLE_DRIVER) {
1414 ERR_EXIT("Cannot find a compatible Vulkan installable client driver "
Ian Elliott3b375cf2015-04-28 13:22:33 -06001415 "(ICD).\n\nPlease look at the Getting Started guide for "
Ian Elliottcaa9f272015-04-28 11:35:02 -06001416 "additional information.\n",
1417 "vkCreateInstance Failure");
1418 } else if (err) {
Ian Elliott3b375cf2015-04-28 13:22:33 -06001419 ERR_EXIT("vkCreateInstance failed.\n\nDo you have a compatible Vulkan "
1420 "installable client driver (ICD) installed?\nPlease look at "
Ian Elliottcaa9f272015-04-28 11:35:02 -06001421 "the Getting Started guide for additional information.\n",
1422 "vkCreateInstance Failure");
Ian Elliottdfe55f72015-04-03 15:24:55 -06001423 }
Jon Ashburn29669a42015-04-04 14:52:07 -06001424
Jon Ashburn07b309a2015-04-15 11:31:12 -06001425 gpu_count = 1;
1426 err = vkEnumeratePhysicalDevices(demo->inst, &gpu_count, &demo->gpu);
Chia-I Wuc19795a2014-09-13 11:12:55 +08001427 assert(!err && gpu_count == 1);
1428
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001429 err = vkCreateDevice(demo->gpu, &device, &demo->device);
Chia-I Wuc19795a2014-09-13 11:12:55 +08001430 assert(!err);
1431
Ian Elliott1b6de092015-06-22 15:07:49 -06001432 GET_DEVICE_PROC_ADDR(demo->device, CreateSwapChainWSI);
1433 GET_DEVICE_PROC_ADDR(demo->device, CreateSwapChainWSI);
1434 GET_DEVICE_PROC_ADDR(demo->device, DestroySwapChainWSI);
1435 GET_DEVICE_PROC_ADDR(demo->device, GetSwapChainInfoWSI);
1436 GET_DEVICE_PROC_ADDR(demo->device, QueuePresentWSI);
1437
Tony Barbour426b9052015-06-24 16:06:58 -06001438 err = vkGetPhysicalDeviceProperties(demo->gpu, &demo->gpu_props);
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -06001439 assert(!err);
1440
Tony Barbour426b9052015-06-24 16:06:58 -06001441 err = vkGetPhysicalDeviceQueueCount(demo->gpu, &queue_count);
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -06001442 assert(!err);
1443
Tony Barbour426b9052015-06-24 16:06:58 -06001444 demo->queue_props = (VkPhysicalDeviceQueueProperties *) malloc(queue_count * sizeof(VkPhysicalDeviceQueueProperties));
1445 err = vkGetPhysicalDeviceQueueProperties(demo->gpu, queue_count, demo->queue_props);
Courtney Goeltzenleuchterf3168062015-03-05 18:09:39 -07001446 assert(!err);
Courtney Goeltzenleuchterf3168062015-03-05 18:09:39 -07001447 assert(queue_count >= 1);
1448
1449 for (i = 0; i < queue_count; i++) {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001450 if (demo->queue_props[i].queueFlags & VK_QUEUE_GRAPHICS_BIT)
Courtney Goeltzenleuchterf3168062015-03-05 18:09:39 -07001451 break;
1452 }
1453 assert(i < queue_count);
1454 demo->graphics_queue_node_index = i;
1455
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001456 err = vkGetDeviceQueue(demo->device, demo->graphics_queue_node_index,
Chia-I Wuc19795a2014-09-13 11:12:55 +08001457 0, &demo->queue);
1458 assert(!err);
Ian Elliott32536f92015-04-21 16:41:02 -06001459
Jon Ashburnba4a1952015-06-16 12:44:51 -06001460 // for now hardcode format till get WSI support
1461 demo->format = VK_FORMAT_B8G8R8A8_UNORM;
Ian Elliott32536f92015-04-21 16:41:02 -06001462
Chia-I Wuc19795a2014-09-13 11:12:55 +08001463}
1464
1465static void demo_init_connection(struct demo *demo)
1466{
Ian Elliotte14e9f92015-04-16 15:23:05 -06001467#ifndef _WIN32
Chia-I Wuc19795a2014-09-13 11:12:55 +08001468 const xcb_setup_t *setup;
1469 xcb_screen_iterator_t iter;
1470 int scr;
1471
1472 demo->connection = xcb_connect(NULL, &scr);
Ian Elliottdfe55f72015-04-03 15:24:55 -06001473 if (demo->connection == NULL) {
1474 printf("Cannot find a compatible Vulkan installable client driver "
1475 "(ICD).\nExiting ...\n");
1476 fflush(stdout);
1477 exit(1);
1478 }
Chia-I Wuc19795a2014-09-13 11:12:55 +08001479
1480 setup = xcb_get_setup(demo->connection);
1481 iter = xcb_setup_roots_iterator(setup);
1482 while (scr-- > 0)
1483 xcb_screen_next(&iter);
1484
1485 demo->screen = iter.data;
Ian Elliotte14e9f92015-04-16 15:23:05 -06001486#endif // _WIN32
Chia-I Wuc19795a2014-09-13 11:12:55 +08001487}
1488
Ian Elliotte14e9f92015-04-16 15:23:05 -06001489#ifdef _WIN32
1490static void demo_init(struct demo *demo, HINSTANCE hInstance, LPSTR pCmdLine)
1491#else // _WIN32
Courtney Goeltzenleuchterf113a952015-02-25 11:46:58 -07001492static void demo_init(struct demo *demo, const int argc, const char *argv[])
Ian Elliotte14e9f92015-04-16 15:23:05 -06001493#endif // _WIN32
Chia-I Wuc19795a2014-09-13 11:12:55 +08001494{
Ian Elliotte14e9f92015-04-16 15:23:05 -06001495 bool argv_error = false;
1496
Chia-I Wuc19795a2014-09-13 11:12:55 +08001497 memset(demo, 0, sizeof(*demo));
1498
Ian Elliotte14e9f92015-04-16 15:23:05 -06001499#ifdef _WIN32
1500 demo->connection = hInstance;
Ian Elliott4e19ed02015-04-28 10:52:52 -06001501 strncpy(demo->name, APP_SHORT_NAME, APP_NAME_STR_LEN);
Ian Elliotte14e9f92015-04-16 15:23:05 -06001502
1503 if (strncmp(pCmdLine, "--use_staging", strlen("--use_staging")) == 0)
1504 demo->use_staging_buffer = true;
Cody Northrop75db0322015-05-28 11:27:16 -06001505 else if (strncmp(pCmdLine, "--use_glsl", strlen("--use_glsl")) == 0)
1506 demo->use_glsl = true;
Ian Elliotte14e9f92015-04-16 15:23:05 -06001507 else if (strlen(pCmdLine) != 0) {
1508 fprintf(stderr, "Do not recognize argument \"%s\".\n", pCmdLine);
1509 argv_error = true;
1510 }
1511#else // _WIN32
Courtney Goeltzenleuchterf113a952015-02-25 11:46:58 -07001512 for (int i = 0; i < argc; i++) {
1513 if (strncmp(argv[i], "--use_staging", strlen("--use_staging")) == 0)
1514 demo->use_staging_buffer = true;
Cody Northrop75db0322015-05-28 11:27:16 -06001515 else if (strncmp(argv[i], "--use_glsl", strlen("--use_glsl")) == 0)
1516 demo->use_glsl = true;
Courtney Goeltzenleuchterf113a952015-02-25 11:46:58 -07001517 }
Ian Elliotte14e9f92015-04-16 15:23:05 -06001518#endif // _WIN32
1519 if (argv_error) {
Ian Elliott4e19ed02015-04-28 10:52:52 -06001520 fprintf(stderr, "Usage:\n %s [--use_staging]\n", APP_SHORT_NAME);
Ian Elliotte14e9f92015-04-16 15:23:05 -06001521 fflush(stderr);
1522 exit(1);
1523 }
Courtney Goeltzenleuchterf113a952015-02-25 11:46:58 -07001524
Chia-I Wuc19795a2014-09-13 11:12:55 +08001525 demo_init_connection(demo);
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001526 demo_init_vk(demo);
Chia-I Wuc19795a2014-09-13 11:12:55 +08001527
1528 demo->width = 300;
1529 demo->height = 300;
Chia-I Wuc19795a2014-09-13 11:12:55 +08001530}
1531
1532static void demo_cleanup(struct demo *demo)
1533{
Mark Lobodzinski23182612015-05-29 09:32:35 -05001534 uint32_t i;
Chia-I Wuc19795a2014-09-13 11:12:55 +08001535
Mike Stroyan230e6252015-04-17 12:36:38 -06001536 vkDestroyObject(demo->device, VK_OBJECT_TYPE_DESCRIPTOR_SET, demo->desc_set);
1537 vkDestroyObject(demo->device, VK_OBJECT_TYPE_DESCRIPTOR_POOL, demo->desc_pool);
Chia-I Wuf8385062015-01-04 16:27:24 +08001538
Courtney Goeltzenleuchter633f9c52015-04-30 10:58:33 -06001539 if (demo->setup_cmd) {
1540 vkDestroyObject(demo->device, VK_OBJECT_TYPE_COMMAND_BUFFER, demo->setup_cmd);
1541 }
1542 vkDestroyObject(demo->device, VK_OBJECT_TYPE_COMMAND_BUFFER, demo->draw_cmd);
Chia-I Wuc19795a2014-09-13 11:12:55 +08001543
Mike Stroyan230e6252015-04-17 12:36:38 -06001544 vkDestroyObject(demo->device, VK_OBJECT_TYPE_DYNAMIC_VP_STATE, demo->viewport);
1545 vkDestroyObject(demo->device, VK_OBJECT_TYPE_DYNAMIC_RS_STATE, demo->raster);
1546 vkDestroyObject(demo->device, VK_OBJECT_TYPE_DYNAMIC_CB_STATE, demo->color_blend);
1547 vkDestroyObject(demo->device, VK_OBJECT_TYPE_DYNAMIC_DS_STATE, demo->depth_stencil);
Chia-I Wuc19795a2014-09-13 11:12:55 +08001548
Mike Stroyan230e6252015-04-17 12:36:38 -06001549 vkDestroyObject(demo->device, VK_OBJECT_TYPE_PIPELINE, demo->pipeline);
1550 vkDestroyObject(demo->device, VK_OBJECT_TYPE_PIPELINE_LAYOUT, demo->pipeline_layout);
1551 vkDestroyObject(demo->device, VK_OBJECT_TYPE_DESCRIPTOR_SET_LAYOUT, demo->desc_layout);
Chia-I Wub043fe32014-10-06 15:30:33 +08001552
Mike Stroyan230e6252015-04-17 12:36:38 -06001553 vkDestroyObject(demo->device, VK_OBJECT_TYPE_BUFFER, demo->vertices.buf);
Mark Lobodzinski23182612015-05-29 09:32:35 -05001554 vkFreeMemory(demo->device, demo->vertices.mem);
Chia-I Wu99621bc2014-10-08 11:52:22 +08001555
Chia-I Wub043fe32014-10-06 15:30:33 +08001556 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Mike Stroyan230e6252015-04-17 12:36:38 -06001557 vkDestroyObject(demo->device, VK_OBJECT_TYPE_IMAGE_VIEW, demo->textures[i].view);
Mike Stroyan230e6252015-04-17 12:36:38 -06001558 vkDestroyObject(demo->device, VK_OBJECT_TYPE_IMAGE, demo->textures[i].image);
Mark Lobodzinski23182612015-05-29 09:32:35 -05001559 vkFreeMemory(demo->device, demo->textures[i].mem);
Mike Stroyan230e6252015-04-17 12:36:38 -06001560 vkDestroyObject(demo->device, VK_OBJECT_TYPE_SAMPLER, demo->textures[i].sampler);
Chia-I Wub043fe32014-10-06 15:30:33 +08001561 }
1562
Mike Stroyan230e6252015-04-17 12:36:38 -06001563 vkDestroyObject(demo->device, VK_OBJECT_TYPE_DEPTH_STENCIL_VIEW, demo->depth.view);
Mike Stroyan230e6252015-04-17 12:36:38 -06001564 vkDestroyObject(demo->device, VK_OBJECT_TYPE_IMAGE, demo->depth.image);
Mark Lobodzinski23182612015-05-29 09:32:35 -05001565 vkFreeMemory(demo->device, demo->depth.mem);
Chia-I Wu9ae87c92014-10-07 14:15:01 +08001566
Chia-I Wuc19795a2014-09-13 11:12:55 +08001567 for (i = 0; i < DEMO_BUFFER_COUNT; i++) {
Mike Stroyan230e6252015-04-17 12:36:38 -06001568 vkDestroyObject(demo->device, VK_OBJECT_TYPE_COLOR_ATTACHMENT_VIEW, demo->buffers[i].view);
Chia-I Wuc19795a2014-09-13 11:12:55 +08001569 }
Jon Ashburncedc15f2015-05-21 18:13:33 -06001570 demo->fpDestroySwapChainWSI(demo->swap_chain);
Chia-I Wuc19795a2014-09-13 11:12:55 +08001571
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001572 vkDestroyDevice(demo->device);
1573 vkDestroyInstance(demo->inst);
Chia-I Wuc19795a2014-09-13 11:12:55 +08001574
Ian Elliotte14e9f92015-04-16 15:23:05 -06001575#ifndef _WIN32
Chia-I Wuc19795a2014-09-13 11:12:55 +08001576 xcb_destroy_window(demo->connection, demo->window);
1577 xcb_disconnect(demo->connection);
Ian Elliotte14e9f92015-04-16 15:23:05 -06001578#endif // _WIN32
Chia-I Wuc19795a2014-09-13 11:12:55 +08001579}
1580
Ian Elliotte14e9f92015-04-16 15:23:05 -06001581#ifdef _WIN32
1582int APIENTRY WinMain(HINSTANCE hInstance,
1583 HINSTANCE hPrevInstance,
1584 LPSTR pCmdLine,
1585 int nCmdShow)
1586{
1587 MSG msg; // message
1588 bool done; // flag saying when app is complete
1589
1590 demo_init(&demo, hInstance, pCmdLine);
1591 demo_create_window(&demo);
1592
1593 demo_prepare(&demo);
1594
1595 done = false; //initialize loop condition variable
1596 /* main message loop*/
1597 while(!done)
1598 {
Ian Elliott421107f2015-04-28 15:50:36 -06001599 PeekMessage(&msg, NULL, 0, 0, PM_REMOVE);
Ian Elliotte14e9f92015-04-16 15:23:05 -06001600 if (msg.message == WM_QUIT) //check for a quit message
1601 {
1602 done = true; //if found, quit app
1603 }
1604 else
1605 {
1606 /* Translate and dispatch to event queue*/
1607 TranslateMessage(&msg);
1608 DispatchMessage(&msg);
1609 }
1610 }
1611
1612 demo_cleanup(&demo);
1613
Tony Barboura938abb2015-04-22 11:36:22 -06001614 return (int) msg.wParam;
Ian Elliotte14e9f92015-04-16 15:23:05 -06001615}
1616#else // _WIN32
Courtney Goeltzenleuchterf113a952015-02-25 11:46:58 -07001617int main(const int argc, const char *argv[])
Chia-I Wuc19795a2014-09-13 11:12:55 +08001618{
1619 struct demo demo;
1620
Courtney Goeltzenleuchterf113a952015-02-25 11:46:58 -07001621 demo_init(&demo, argc, argv);
Chia-I Wu5b66aa52015-04-16 22:02:10 +08001622 demo_create_window(&demo);
Chia-I Wuc19795a2014-09-13 11:12:55 +08001623
1624 demo_prepare(&demo);
Chia-I Wuc19795a2014-09-13 11:12:55 +08001625 demo_run(&demo);
1626
1627 demo_cleanup(&demo);
1628
Chia-I Wuc19795a2014-09-13 11:12:55 +08001629 return 0;
1630}
Ian Elliotte14e9f92015-04-16 15:23:05 -06001631#endif // _WIN32