blob: d59d9e62b97d3eca9602688e16d998994e36eb16 [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>
45#include <vkDbg.h>
Chia-I Wu5b66aa52015-04-16 22:02:10 +080046#include <vk_wsi_lunarg.h>
Chia-I Wuc19795a2014-09-13 11:12:55 +080047
Cody Northropd4e020a2015-03-17 14:54:35 -060048#include "icd-spv.h"
Courtney Goeltzenleuchter3f2606d2014-10-13 17:51:58 -060049
Chia-I Wuc19795a2014-09-13 11:12:55 +080050#define DEMO_BUFFER_COUNT 2
Chia-I Wub043fe32014-10-06 15:30:33 +080051#define DEMO_TEXTURE_COUNT 1
Courtney Goeltzenleuchterf5cdad02015-03-31 16:36:30 -060052#define VERTEX_BUFFER_BIND_ID 0
Ian Elliott4e19ed02015-04-28 10:52:52 -060053#define APP_SHORT_NAME "tri"
54#define APP_LONG_NAME "The Vulkan Triangle Demo Program"
Chia-I Wuc19795a2014-09-13 11:12:55 +080055
Tony Barbour22a30862015-04-22 09:02:32 -060056#if defined(NDEBUG) && defined(__GNUC__)
57#define U_ASSERT_ONLY __attribute__((unused))
58#else
59#define U_ASSERT_ONLY
60#endif
61
Ian Elliottcaa9f272015-04-28 11:35:02 -060062#ifdef _WIN32
63#define ERR_EXIT(err_msg, err_class) \
64 do { \
65 MessageBox(NULL, err_msg, err_class, MB_OK); \
66 exit(1); \
67 } while (0)
68
69// NOTE: If the following values (copied from "loader_platform.h") change, they
70// need to change here as well:
71#define LAYER_NAMES_ENV "VK_LAYER_NAMES"
72#define LAYER_NAMES_REGISTRY_VALUE "VK_LAYER_NAMES"
73#else // _WIN32
74
75#define ERR_EXIT(err_msg, err_class) \
76 do { \
77 printf(err_msg); \
78 fflush(stdout); \
79 exit(1); \
80 } while (0)
81#endif // _WIN32
Tony Barbour22a30862015-04-22 09:02:32 -060082
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -060083struct texture_object {
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -060084 VkSampler sampler;
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -070085
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -060086 VkImage image;
87 VkImageLayout imageLayout;
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -060088
Mark Lobodzinski23182612015-05-29 09:32:35 -050089 VkDeviceMemory mem;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -060090 VkImageView view;
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -070091 int32_t tex_width, tex_height;
92};
93
Chia-I Wuc19795a2014-09-13 11:12:55 +080094struct demo {
Ian Elliotte14e9f92015-04-16 15:23:05 -060095#ifdef _WIN32
96#define APP_NAME_STR_LEN 80
97 HINSTANCE connection; // hInstance - Windows Instance
98 char name[APP_NAME_STR_LEN]; // Name to put on the window/icon
99 HWND window; // hWnd - window handle
100#else // _WIN32
Chia-I Wuc19795a2014-09-13 11:12:55 +0800101 xcb_connection_t *connection;
102 xcb_screen_t *screen;
Chia-I Wu5b66aa52015-04-16 22:02:10 +0800103 xcb_window_t window;
104 xcb_intern_atom_reply_t *atom_wm_delete_window;
Ian Elliotte14e9f92015-04-16 15:23:05 -0600105#endif // _WIN32
Jon Ashburn8a399e92015-04-24 09:46:24 -0700106 bool prepared;
Ian Elliotte14e9f92015-04-16 15:23:05 -0600107 bool use_staging_buffer;
Chia-I Wuc19795a2014-09-13 11:12:55 +0800108
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600109 VkInstance inst;
Tony Barbour8205d902015-04-16 15:59:00 -0600110 VkPhysicalDevice gpu;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600111 VkDevice device;
112 VkQueue queue;
Tony Barbour8205d902015-04-16 15:59:00 -0600113 VkPhysicalDeviceProperties *gpu_props;
114 VkPhysicalDeviceQueueProperties *queue_props;
Courtney Goeltzenleuchterf3168062015-03-05 18:09:39 -0700115 uint32_t graphics_queue_node_index;
Chia-I Wuc19795a2014-09-13 11:12:55 +0800116
117 int width, height;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600118 VkFormat format;
Chia-I Wuc19795a2014-09-13 11:12:55 +0800119
Ian Elliott32536f92015-04-21 16:41:02 -0600120 VkDisplayPropertiesWSI *display_props;
121 int num_displays;
Chia-I Wu5b66aa52015-04-16 22:02:10 +0800122 VkSwapChainWSI swap_chain;
Chia-I Wuc19795a2014-09-13 11:12:55 +0800123 struct {
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600124 VkImage image;
Tony Barbour8205d902015-04-16 15:59:00 -0600125 VkDeviceMemory mem;
Chia-I Wuc19795a2014-09-13 11:12:55 +0800126
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600127 VkColorAttachmentView view;
Chia-I Wuc19795a2014-09-13 11:12:55 +0800128 } buffers[DEMO_BUFFER_COUNT];
129
Chia-I Wub043fe32014-10-06 15:30:33 +0800130 struct {
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600131 VkFormat format;
Chia-I Wu9ae87c92014-10-07 14:15:01 +0800132
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600133 VkImage image;
Mark Lobodzinski23182612015-05-29 09:32:35 -0500134 VkDeviceMemory mem;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600135 VkDepthStencilView view;
Chia-I Wu9ae87c92014-10-07 14:15:01 +0800136 } depth;
137
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600138 struct texture_object textures[DEMO_TEXTURE_COUNT];
Chia-I Wub043fe32014-10-06 15:30:33 +0800139
Chia-I Wu99621bc2014-10-08 11:52:22 +0800140 struct {
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600141 VkBuffer buf;
Mark Lobodzinski23182612015-05-29 09:32:35 -0500142 VkDeviceMemory mem;
Chia-I Wu8d29d022014-10-08 12:14:39 +0800143
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600144 VkPipelineVertexInputCreateInfo vi;
145 VkVertexInputBindingDescription vi_bindings[1];
146 VkVertexInputAttributeDescription vi_attrs[2];
Chia-I Wu99621bc2014-10-08 11:52:22 +0800147 } vertices;
148
Courtney Goeltzenleuchter633f9c52015-04-30 10:58:33 -0600149 VkCmdBuffer setup_cmd; // Command Buffer for initialization commands
150 VkCmdBuffer draw_cmd; // Command Buffer for drawing commands
Mark Lobodzinski556f7212015-04-17 14:11:39 -0500151 VkPipelineLayout pipeline_layout;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600152 VkDescriptorSetLayout desc_layout;
153 VkPipeline pipeline;
Chia-I Wuc19795a2014-09-13 11:12:55 +0800154
Courtney Goeltzenleuchterfcf855f2015-04-10 16:24:50 -0600155 VkDynamicVpState viewport;
156 VkDynamicRsState raster;
157 VkDynamicCbState color_blend;
158 VkDynamicDsState depth_stencil;
Chia-I Wuc19795a2014-09-13 11:12:55 +0800159
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600160 VkDescriptorPool desc_pool;
161 VkDescriptorSet desc_set;
Chia-I Wuf8385062015-01-04 16:27:24 +0800162
Chia-I Wuc19795a2014-09-13 11:12:55 +0800163 bool quit;
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600164 uint32_t current_buffer;
Chia-I Wuc19795a2014-09-13 11:12:55 +0800165};
166
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600167static void demo_flush_init_cmd(struct demo *demo)
168{
Tony Barbour22a30862015-04-22 09:02:32 -0600169 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600170
Courtney Goeltzenleuchter633f9c52015-04-30 10:58:33 -0600171 if (demo->setup_cmd == VK_NULL_HANDLE)
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600172 return;
173
Courtney Goeltzenleuchter633f9c52015-04-30 10:58:33 -0600174 err = vkEndCommandBuffer(demo->setup_cmd);
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600175 assert(!err);
176
Courtney Goeltzenleuchter633f9c52015-04-30 10:58:33 -0600177 const VkCmdBuffer cmd_bufs[] = { demo->setup_cmd };
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600178
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600179 err = vkQueueSubmit(demo->queue, 1, cmd_bufs, VK_NULL_HANDLE);
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600180 assert(!err);
181
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600182 err = vkQueueWaitIdle(demo->queue);
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600183 assert(!err);
184
Courtney Goeltzenleuchter633f9c52015-04-30 10:58:33 -0600185 vkDestroyObject(demo->device, VK_OBJECT_TYPE_COMMAND_BUFFER, demo->setup_cmd);
186 demo->setup_cmd = VK_NULL_HANDLE;
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600187}
188
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600189static void demo_set_image_layout(
190 struct demo *demo,
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600191 VkImage image,
malnasse4b8ba4d2015-06-03 17:28:38 -0400192 VkImageAspect aspect,
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600193 VkImageLayout old_image_layout,
194 VkImageLayout new_image_layout)
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600195{
Tony Barbour22a30862015-04-22 09:02:32 -0600196 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600197
Courtney Goeltzenleuchter633f9c52015-04-30 10:58:33 -0600198 if (demo->setup_cmd == VK_NULL_HANDLE) {
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600199 const VkCmdBufferCreateInfo cmd = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600200 .sType = VK_STRUCTURE_TYPE_CMD_BUFFER_CREATE_INFO,
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600201 .pNext = NULL,
202 .queueNodeIndex = demo->graphics_queue_node_index,
203 .flags = 0,
204 };
205
Courtney Goeltzenleuchter633f9c52015-04-30 10:58:33 -0600206 err = vkCreateCommandBuffer(demo->device, &cmd, &demo->setup_cmd);
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600207 assert(!err);
208
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600209 VkCmdBufferBeginInfo cmd_buf_info = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600210 .sType = VK_STRUCTURE_TYPE_CMD_BUFFER_BEGIN_INFO,
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600211 .pNext = NULL,
Tony Barbour8205d902015-04-16 15:59:00 -0600212 .flags = VK_CMD_BUFFER_OPTIMIZE_SMALL_BATCH_BIT |
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600213 VK_CMD_BUFFER_OPTIMIZE_ONE_TIME_SUBMIT_BIT,
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600214 };
Courtney Goeltzenleuchter633f9c52015-04-30 10:58:33 -0600215 err = vkBeginCommandBuffer(demo->setup_cmd, &cmd_buf_info);
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600216 }
217
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600218 VkImageMemoryBarrier image_memory_barrier = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600219 .sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600220 .pNext = NULL,
221 .outputMask = 0,
222 .inputMask = 0,
223 .oldLayout = old_image_layout,
224 .newLayout = new_image_layout,
225 .image = image,
malnasse4b8ba4d2015-06-03 17:28:38 -0400226 .subresourceRange = { aspect, 0, 1, 0, 0 }
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600227 };
228
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600229 if (new_image_layout == VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL) {
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600230 /* Make sure anything that was copying from this image has completed */
Courtney Goeltzenleuchterad870812015-04-15 15:29:59 -0600231 image_memory_barrier.inputMask = VK_MEMORY_INPUT_TRANSFER_BIT;
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600232 }
233
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600234 if (new_image_layout == VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL) {
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600235 /* Make sure any Copy or CPU writes to image are flushed */
Courtney Goeltzenleuchtera569a502015-04-29 17:16:21 -0600236 image_memory_barrier.outputMask = VK_MEMORY_OUTPUT_TRANSFER_BIT | VK_MEMORY_OUTPUT_HOST_WRITE_BIT;
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600237 }
238
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600239 VkImageMemoryBarrier *pmemory_barrier = &image_memory_barrier;
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600240
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600241 VkPipeEvent set_events[] = { VK_PIPE_EVENT_TOP_OF_PIPE };
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600242
Courtney Goeltzenleuchter633f9c52015-04-30 10:58:33 -0600243 vkCmdPipelineBarrier(demo->draw_cmd, VK_WAIT_EVENT_TOP_OF_PIPE, 1, set_events, 1, (const void **)&pmemory_barrier);
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600244}
245
Chia-I Wuc19795a2014-09-13 11:12:55 +0800246static void demo_draw_build_cmd(struct demo *demo)
247{
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600248 const VkColorAttachmentBindInfo color_attachment = {
Chia-I Wuc19795a2014-09-13 11:12:55 +0800249 .view = demo->buffers[demo->current_buffer].view,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600250 .layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
Chia-I Wuc19795a2014-09-13 11:12:55 +0800251 };
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600252 const VkDepthStencilBindInfo depth_stencil = {
Chia-I Wu9ae87c92014-10-07 14:15:01 +0800253 .view = demo->depth.view,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600254 .layout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
Chia-I Wu9ae87c92014-10-07 14:15:01 +0800255 };
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600256 const VkClearColor clear_color = {
Courtney Goeltzenleuchter9a1ded82015-04-03 16:35:32 -0600257 .color.floatColor = { 0.2f, 0.2f, 0.2f, 0.2f },
258 .useRawValue = false,
259 };
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600260 const float clear_depth = 0.9f;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600261 VkImageSubresourceRange clear_range;
262 VkCmdBufferBeginInfo cmd_buf_info = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600263 .sType = VK_STRUCTURE_TYPE_CMD_BUFFER_BEGIN_INFO,
Courtney Goeltzenleuchtere3b0f3a2015-04-03 15:25:24 -0600264 .pNext = NULL,
Tony Barbour8205d902015-04-16 15:59:00 -0600265 .flags = VK_CMD_BUFFER_OPTIMIZE_SMALL_BATCH_BIT |
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600266 VK_CMD_BUFFER_OPTIMIZE_ONE_TIME_SUBMIT_BIT,
Jon Ashburn53d27af2014-12-31 17:08:35 -0700267 };
Tony Barbour22a30862015-04-22 09:02:32 -0600268 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600269 VkAttachmentLoadOp load_op = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
270 VkAttachmentStoreOp store_op = VK_ATTACHMENT_STORE_OP_DONT_CARE;
271 const VkFramebufferCreateInfo fb_info = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600272 .sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO,
Jon Ashburn3325d6b2015-01-02 18:24:05 -0700273 .pNext = NULL,
274 .colorAttachmentCount = 1,
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600275 .pColorAttachments = (VkColorAttachmentBindInfo*) &color_attachment,
276 .pDepthStencilAttachment = (VkDepthStencilBindInfo*) &depth_stencil,
Jon Ashburn3325d6b2015-01-02 18:24:05 -0700277 .sampleCount = 1,
Mark Lobodzinski71fcc2d2015-01-27 13:24:03 -0600278 .width = demo->width,
279 .height = demo->height,
280 .layers = 1,
Jon Ashburn3325d6b2015-01-02 18:24:05 -0700281 };
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600282 VkRenderPassCreateInfo rp_info;
283 VkRenderPassBegin rp_begin;
Jon Ashburn3325d6b2015-01-02 18:24:05 -0700284
285 memset(&rp_info, 0 , sizeof(rp_info));
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600286 err = vkCreateFramebuffer(demo->device, &fb_info, &rp_begin.framebuffer);
Jon Ashburn3325d6b2015-01-02 18:24:05 -0700287 assert(!err);
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600288 rp_info.sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO;
Jon Ashburn3325d6b2015-01-02 18:24:05 -0700289 rp_info.renderArea.extent.width = demo->width;
290 rp_info.renderArea.extent.height = demo->height;
Piers Daniell886be472015-02-23 16:23:13 -0700291 rp_info.colorAttachmentCount = fb_info.colorAttachmentCount;
Courtney Goeltzenleuchtere3b0f3a2015-04-03 15:25:24 -0600292 rp_info.pColorFormats = &demo->format;
293 rp_info.pColorLayouts = &color_attachment.layout;
Jon Ashburn3325d6b2015-01-02 18:24:05 -0700294 rp_info.pColorLoadOps = &load_op;
295 rp_info.pColorStoreOps = &store_op;
Courtney Goeltzenleuchtere3b0f3a2015-04-03 15:25:24 -0600296 rp_info.pColorLoadClearValues = &clear_color;
Tony Barbour8205d902015-04-16 15:59:00 -0600297 rp_info.depthStencilFormat = VK_FORMAT_D16_UNORM;
Courtney Goeltzenleuchtere3b0f3a2015-04-03 15:25:24 -0600298 rp_info.depthStencilLayout = depth_stencil.layout;
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600299 rp_info.depthLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
Courtney Goeltzenleuchtere3b0f3a2015-04-03 15:25:24 -0600300 rp_info.depthLoadClearValue = clear_depth;
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600301 rp_info.depthStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE;
302 rp_info.stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
Courtney Goeltzenleuchtere3b0f3a2015-04-03 15:25:24 -0600303 rp_info.stencilLoadClearValue = 0;
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600304 rp_info.stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE;
305 err = vkCreateRenderPass(demo->device, &rp_info, &(rp_begin.renderPass));
Jon Ashburn3325d6b2015-01-02 18:24:05 -0700306 assert(!err);
Chia-I Wuc19795a2014-09-13 11:12:55 +0800307
Courtney Goeltzenleuchter633f9c52015-04-30 10:58:33 -0600308 err = vkBeginCommandBuffer(demo->draw_cmd, &cmd_buf_info);
Chia-I Wuc19795a2014-09-13 11:12:55 +0800309 assert(!err);
310
Courtney Goeltzenleuchter633f9c52015-04-30 10:58:33 -0600311 vkCmdBindPipeline(demo->draw_cmd, VK_PIPELINE_BIND_POINT_GRAPHICS,
Chia-I Wuc19795a2014-09-13 11:12:55 +0800312 demo->pipeline);
Courtney Goeltzenleuchter633f9c52015-04-30 10:58:33 -0600313 vkCmdBindDescriptorSets(demo->draw_cmd, VK_PIPELINE_BIND_POINT_GRAPHICS,
Cody Northrop1a01b1d2015-04-16 13:41:56 -0600314 0, 1, & demo->desc_set, 0, NULL);
Chia-I Wuc19795a2014-09-13 11:12:55 +0800315
Courtney Goeltzenleuchter633f9c52015-04-30 10:58:33 -0600316 vkCmdBindDynamicStateObject(demo->draw_cmd, VK_STATE_BIND_POINT_VIEWPORT, demo->viewport);
317 vkCmdBindDynamicStateObject(demo->draw_cmd, VK_STATE_BIND_POINT_RASTER, demo->raster);
318 vkCmdBindDynamicStateObject(demo->draw_cmd, VK_STATE_BIND_POINT_COLOR_BLEND,
Chia-I Wuc19795a2014-09-13 11:12:55 +0800319 demo->color_blend);
Courtney Goeltzenleuchter633f9c52015-04-30 10:58:33 -0600320 vkCmdBindDynamicStateObject(demo->draw_cmd, VK_STATE_BIND_POINT_DEPTH_STENCIL,
Chia-I Wuc19795a2014-09-13 11:12:55 +0800321 demo->depth_stencil);
322
Tony Barbour8205d902015-04-16 15:59:00 -0600323 VkDeviceSize offsets[1] = {0};
Courtney Goeltzenleuchter633f9c52015-04-30 10:58:33 -0600324 vkCmdBindVertexBuffers(demo->draw_cmd, VERTEX_BUFFER_BIND_ID, 1, &demo->vertices.buf, offsets);
Chia-I Wu3b04af52014-11-08 10:48:20 +0800325
Courtney Goeltzenleuchter633f9c52015-04-30 10:58:33 -0600326 vkCmdBeginRenderPass(demo->draw_cmd, &rp_begin);
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600327 clear_range.aspect = VK_IMAGE_ASPECT_COLOR;
Chia-I Wu68a7de42014-10-25 12:40:28 +0800328 clear_range.baseMipLevel = 0;
329 clear_range.mipLevels = 1;
330 clear_range.baseArraySlice = 0;
331 clear_range.arraySize = 1;
Courtney Goeltzenleuchter633f9c52015-04-30 10:58:33 -0600332 vkCmdClearColorImage(demo->draw_cmd,
Chia-I Wu68a7de42014-10-25 12:40:28 +0800333 demo->buffers[demo->current_buffer].image,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600334 VK_IMAGE_LAYOUT_CLEAR_OPTIMAL,
Courtney Goeltzenleuchterda4a99e2015-04-23 17:49:22 -0600335 &clear_color, 1, &clear_range);
Chia-I Wu68a7de42014-10-25 12:40:28 +0800336
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600337 clear_range.aspect = VK_IMAGE_ASPECT_DEPTH;
Courtney Goeltzenleuchter633f9c52015-04-30 10:58:33 -0600338 vkCmdClearDepthStencil(demo->draw_cmd,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600339 demo->depth.image, VK_IMAGE_LAYOUT_CLEAR_OPTIMAL,
Chia-I Wu68a7de42014-10-25 12:40:28 +0800340 clear_depth, 0, 1, &clear_range);
341
Courtney Goeltzenleuchter633f9c52015-04-30 10:58:33 -0600342 vkCmdDraw(demo->draw_cmd, 0, 3, 0, 1);
343 vkCmdEndRenderPass(demo->draw_cmd, rp_begin.renderPass);
Chia-I Wuc19795a2014-09-13 11:12:55 +0800344
Courtney Goeltzenleuchter633f9c52015-04-30 10:58:33 -0600345 err = vkEndCommandBuffer(demo->draw_cmd);
Chia-I Wuc19795a2014-09-13 11:12:55 +0800346 assert(!err);
Courtney Goeltzenleuchtere9afa992015-02-25 16:55:23 -0700347
Mike Stroyan230e6252015-04-17 12:36:38 -0600348 vkDestroyObject(demo->device, VK_OBJECT_TYPE_RENDER_PASS, rp_begin.renderPass);
349 vkDestroyObject(demo->device, VK_OBJECT_TYPE_FRAMEBUFFER, rp_begin.framebuffer);
Chia-I Wuc19795a2014-09-13 11:12:55 +0800350}
351
352static void demo_draw(struct demo *demo)
353{
Chia-I Wu5b66aa52015-04-16 22:02:10 +0800354 const VkPresentInfoWSI present = {
355 .sType = VK_STRUCTURE_TYPE_PRESENT_INFO_WSI,
356 .pNext = NULL,
357 .image = demo->buffers[demo->current_buffer].image,
358 .flipInterval = 0,
Chia-I Wuc19795a2014-09-13 11:12:55 +0800359 };
Tony Barbour22a30862015-04-22 09:02:32 -0600360 VkResult U_ASSERT_ONLY err;
Chia-I Wuc19795a2014-09-13 11:12:55 +0800361
362 demo_draw_build_cmd(demo);
363
Courtney Goeltzenleuchter633f9c52015-04-30 10:58:33 -0600364 err = vkQueueSubmit(demo->queue, 1, &demo->draw_cmd, VK_NULL_HANDLE);
Chia-I Wuc19795a2014-09-13 11:12:55 +0800365 assert(!err);
366
Chia-I Wu5b66aa52015-04-16 22:02:10 +0800367 err = vkQueuePresentWSI(demo->queue, &present);
Chia-I Wuc19795a2014-09-13 11:12:55 +0800368 assert(!err);
369
370 demo->current_buffer = (demo->current_buffer + 1) % DEMO_BUFFER_COUNT;
Chia-I Wu5b66aa52015-04-16 22:02:10 +0800371
372 err = vkQueueWaitIdle(demo->queue);
373 assert(err == VK_SUCCESS);
Chia-I Wuc19795a2014-09-13 11:12:55 +0800374}
375
376static void demo_prepare_buffers(struct demo *demo)
377{
Chia-I Wu5b66aa52015-04-16 22:02:10 +0800378 const VkSwapChainCreateInfoWSI swap_chain = {
379 .sType = VK_STRUCTURE_TYPE_SWAP_CHAIN_CREATE_INFO_WSI,
380 .pNext = NULL,
381 .pNativeWindowSystemHandle = demo->connection,
382 .pNativeWindowHandle = (void *) (intptr_t) demo->window,
Ian Elliott32536f92015-04-21 16:41:02 -0600383 .displayCount = 1,
Chia-I Wu5b66aa52015-04-16 22:02:10 +0800384 .imageCount = DEMO_BUFFER_COUNT,
385 .imageFormat = demo->format,
386 .imageExtent = {
Chia-I Wuc19795a2014-09-13 11:12:55 +0800387 .width = demo->width,
388 .height = demo->height,
389 },
Chia-I Wu5b66aa52015-04-16 22:02:10 +0800390 .imageArraySize = 1,
391 .imageUsageFlags = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT,
Chia-I Wuc19795a2014-09-13 11:12:55 +0800392 };
Chia-I Wu5b66aa52015-04-16 22:02:10 +0800393 VkSwapChainImageInfoWSI images[DEMO_BUFFER_COUNT];
394 size_t images_size = sizeof(images);
Tony Barbour22a30862015-04-22 09:02:32 -0600395 VkResult U_ASSERT_ONLY err;
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600396 uint32_t i;
Chia-I Wuc19795a2014-09-13 11:12:55 +0800397
Chia-I Wu5b66aa52015-04-16 22:02:10 +0800398 err = vkCreateSwapChainWSI(demo->device, &swap_chain, &demo->swap_chain);
399 assert(!err);
400
401 err = vkGetSwapChainInfoWSI(demo->swap_chain,
402 VK_SWAP_CHAIN_INFO_TYPE_PERSISTENT_IMAGES_WSI,
403 &images_size, images);
404 assert(!err && images_size == sizeof(images));
405
Chia-I Wuc19795a2014-09-13 11:12:55 +0800406 for (i = 0; i < DEMO_BUFFER_COUNT; i++) {
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600407 VkColorAttachmentViewCreateInfo color_attachment_view = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600408 .sType = VK_STRUCTURE_TYPE_COLOR_ATTACHMENT_VIEW_CREATE_INFO,
Chia-I Wuc19795a2014-09-13 11:12:55 +0800409 .pNext = NULL,
410 .format = demo->format,
411 .mipLevel = 0,
412 .baseArraySlice = 0,
413 .arraySize = 1,
414 };
415
Chia-I Wu5b66aa52015-04-16 22:02:10 +0800416 demo->buffers[i].image = images[i].image;
417 demo->buffers[i].mem = images[i].memory;
Mark Lobodzinski97dcd042015-04-16 08:52:00 -0500418
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600419 demo_set_image_layout(demo, demo->buffers[i].image,
malnasse4b8ba4d2015-06-03 17:28:38 -0400420 VK_IMAGE_ASPECT_COLOR,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600421 VK_IMAGE_LAYOUT_UNDEFINED,
422 VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL);
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600423
Chia-I Wuc19795a2014-09-13 11:12:55 +0800424 color_attachment_view.image = demo->buffers[i].image;
425
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600426 err = vkCreateColorAttachmentView(demo->device,
Chia-I Wuc19795a2014-09-13 11:12:55 +0800427 &color_attachment_view, &demo->buffers[i].view);
428 assert(!err);
429 }
Piers Daniell886be472015-02-23 16:23:13 -0700430
431 demo->current_buffer = 0;
Chia-I Wuc19795a2014-09-13 11:12:55 +0800432}
433
Chia-I Wu9ae87c92014-10-07 14:15:01 +0800434static void demo_prepare_depth(struct demo *demo)
435{
Tony Barbour8205d902015-04-16 15:59:00 -0600436 const VkFormat depth_format = VK_FORMAT_D16_UNORM;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600437 const VkImageCreateInfo image = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600438 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
Chia-I Wu9ae87c92014-10-07 14:15:01 +0800439 .pNext = NULL,
Tony Barbour8205d902015-04-16 15:59:00 -0600440 .imageType = VK_IMAGE_TYPE_2D,
Chia-I Wu9ae87c92014-10-07 14:15:01 +0800441 .format = depth_format,
442 .extent = { demo->width, demo->height, 1 },
443 .mipLevels = 1,
444 .arraySize = 1,
445 .samples = 1,
Tony Barbour8205d902015-04-16 15:59:00 -0600446 .tiling = VK_IMAGE_TILING_OPTIMAL,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600447 .usage = VK_IMAGE_USAGE_DEPTH_STENCIL_BIT,
Chia-I Wu9ae87c92014-10-07 14:15:01 +0800448 .flags = 0,
449 };
Courtney Goeltzenleuchterddcb6192015-04-14 18:48:46 -0600450 VkMemoryAllocInfo mem_alloc = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600451 .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOC_INFO,
Mark Lobodzinski97dcd042015-04-16 08:52:00 -0500452 .pNext = NULL,
Chia-I Wu9ae87c92014-10-07 14:15:01 +0800453 .allocationSize = 0,
Tony Barbour8205d902015-04-16 15:59:00 -0600454 .memProps = VK_MEMORY_PROPERTY_DEVICE_ONLY,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600455 .memPriority = VK_MEMORY_PRIORITY_NORMAL,
Chia-I Wu9ae87c92014-10-07 14:15:01 +0800456 };
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600457 VkDepthStencilViewCreateInfo view = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600458 .sType = VK_STRUCTURE_TYPE_DEPTH_STENCIL_VIEW_CREATE_INFO,
Chia-I Wu9ae87c92014-10-07 14:15:01 +0800459 .pNext = NULL,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600460 .image = VK_NULL_HANDLE,
Chia-I Wu9ae87c92014-10-07 14:15:01 +0800461 .mipLevel = 0,
462 .baseArraySlice = 0,
463 .arraySize = 1,
464 .flags = 0,
465 };
Jon Ashburna9ae3832015-01-16 09:37:43 -0700466
Mark Lobodzinski23182612015-05-29 09:32:35 -0500467 VkMemoryRequirements mem_reqs;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600468 size_t mem_reqs_size = sizeof(VkMemoryRequirements);
Tony Barbour22a30862015-04-22 09:02:32 -0600469 VkResult U_ASSERT_ONLY err;
Chia-I Wu9ae87c92014-10-07 14:15:01 +0800470
471 demo->depth.format = depth_format;
472
473 /* create image */
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600474 err = vkCreateImage(demo->device, &image,
Chia-I Wu9ae87c92014-10-07 14:15:01 +0800475 &demo->depth.image);
476 assert(!err);
477
Mike Stroyan230e6252015-04-17 12:36:38 -0600478 err = vkGetObjectInfo(demo->device,
479 VK_OBJECT_TYPE_IMAGE, demo->depth.image,
Tony Barbour8205d902015-04-16 15:59:00 -0600480 VK_OBJECT_INFO_TYPE_MEMORY_REQUIREMENTS,
Mark Lobodzinski23182612015-05-29 09:32:35 -0500481 &mem_reqs_size, &mem_reqs);
482 mem_alloc.allocationSize = mem_reqs.size;
Chia-I Wu9ae87c92014-10-07 14:15:01 +0800483
Mark Lobodzinski23182612015-05-29 09:32:35 -0500484 /* allocate memory */
485 err = vkAllocMemory(demo->device, &mem_alloc, &demo->depth.mem);
486 assert(!err);
Chia-I Wu9ae87c92014-10-07 14:15:01 +0800487
Mark Lobodzinski23182612015-05-29 09:32:35 -0500488 /* bind memory */
489 err = vkBindObjectMemory(demo->device,
490 VK_OBJECT_TYPE_IMAGE, demo->depth.image,
491 demo->depth.mem, 0);
492 assert(!err);
Chia-I Wu9ae87c92014-10-07 14:15:01 +0800493
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600494 demo_set_image_layout(demo, demo->depth.image,
malnasse4b8ba4d2015-06-03 17:28:38 -0400495 VK_IMAGE_ASPECT_DEPTH,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600496 VK_IMAGE_LAYOUT_UNDEFINED,
497 VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL);
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600498
Chia-I Wu9ae87c92014-10-07 14:15:01 +0800499 /* create image view */
500 view.image = demo->depth.image;
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600501 err = vkCreateDepthStencilView(demo->device, &view,
Chia-I Wu9ae87c92014-10-07 14:15:01 +0800502 &demo->depth.view);
503 assert(!err);
Chia-I Wu9ae87c92014-10-07 14:15:01 +0800504}
505
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700506static void demo_prepare_texture_image(struct demo *demo,
507 const uint32_t *tex_colors,
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600508 struct texture_object *tex_obj,
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600509 VkImageTiling tiling,
Courtney Goeltzenleuchtercb67a322015-04-21 09:31:23 -0600510 VkImageUsageFlags usage,
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600511 VkFlags mem_props)
Chia-I Wub043fe32014-10-06 15:30:33 +0800512{
Tony Barbour8205d902015-04-16 15:59:00 -0600513 const VkFormat tex_format = VK_FORMAT_B8G8R8A8_UNORM;
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600514 const int32_t tex_width = 2;
515 const int32_t tex_height = 2;
Tony Barbour22a30862015-04-22 09:02:32 -0600516 VkResult U_ASSERT_ONLY err;
Chia-I Wub043fe32014-10-06 15:30:33 +0800517
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600518 tex_obj->tex_width = tex_width;
519 tex_obj->tex_height = tex_height;
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700520
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600521 const VkImageCreateInfo image_create_info = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600522 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
Courtney Goeltzenleuchter3226a6f2015-02-11 18:17:22 -0700523 .pNext = NULL,
Tony Barbour8205d902015-04-16 15:59:00 -0600524 .imageType = VK_IMAGE_TYPE_2D,
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700525 .format = tex_format,
526 .extent = { tex_width, tex_height, 1 },
527 .mipLevels = 1,
528 .arraySize = 1,
529 .samples = 1,
530 .tiling = tiling,
Courtney Goeltzenleuchtercb67a322015-04-21 09:31:23 -0600531 .usage = usage,
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700532 .flags = 0,
533 };
Courtney Goeltzenleuchterddcb6192015-04-14 18:48:46 -0600534 VkMemoryAllocInfo mem_alloc = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600535 .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOC_INFO,
Mark Lobodzinski97dcd042015-04-16 08:52:00 -0500536 .pNext = NULL,
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700537 .allocationSize = 0,
538 .memProps = mem_props,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600539 .memPriority = VK_MEMORY_PRIORITY_NORMAL,
Courtney Goeltzenleuchter3226a6f2015-02-11 18:17:22 -0700540 };
541
Mark Lobodzinski23182612015-05-29 09:32:35 -0500542 VkMemoryRequirements mem_reqs;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600543 size_t mem_reqs_size = sizeof(VkMemoryRequirements);
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700544
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600545 err = vkCreateImage(demo->device, &image_create_info,
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600546 &tex_obj->image);
Courtney Goeltzenleuchter3226a6f2015-02-11 18:17:22 -0700547 assert(!err);
548
Mike Stroyan230e6252015-04-17 12:36:38 -0600549 err = vkGetObjectInfo(demo->device,
550 VK_OBJECT_TYPE_IMAGE, tex_obj->image,
Tony Barbour8205d902015-04-16 15:59:00 -0600551 VK_OBJECT_INFO_TYPE_MEMORY_REQUIREMENTS,
Mark Lobodzinski23182612015-05-29 09:32:35 -0500552 &mem_reqs_size, &mem_reqs);
553 mem_alloc.allocationSize = mem_reqs.size;
Courtney Goeltzenleuchter3226a6f2015-02-11 18:17:22 -0700554
Mark Lobodzinski23182612015-05-29 09:32:35 -0500555 /* allocate memory */
556 err = vkAllocMemory(demo->device, &mem_alloc, &tex_obj->mem);
557 assert(!err);
Courtney Goeltzenleuchter3226a6f2015-02-11 18:17:22 -0700558
Mark Lobodzinski23182612015-05-29 09:32:35 -0500559 /* bind memory */
560 err = vkBindObjectMemory(demo->device,
561 VK_OBJECT_TYPE_IMAGE, tex_obj->image,
562 tex_obj->mem, 0);
563 assert(!err);
Courtney Goeltzenleuchter3226a6f2015-02-11 18:17:22 -0700564
Tony Barbour8205d902015-04-16 15:59:00 -0600565 if (mem_props & VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT) {
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600566 const VkImageSubresource subres = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600567 .aspect = VK_IMAGE_ASPECT_COLOR,
Courtney Goeltzenleuchter3226a6f2015-02-11 18:17:22 -0700568 .mipLevel = 0,
569 .arraySlice = 0,
570 };
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600571 VkSubresourceLayout layout;
572 size_t layout_size = sizeof(VkSubresourceLayout);
Courtney Goeltzenleuchter3226a6f2015-02-11 18:17:22 -0700573 void *data;
574 int32_t x, y;
575
Mike Stroyan230e6252015-04-17 12:36:38 -0600576 err = vkGetImageSubresourceInfo(demo->device, tex_obj->image, &subres,
Tony Barbour8205d902015-04-16 15:59:00 -0600577 VK_SUBRESOURCE_INFO_TYPE_LAYOUT,
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700578 &layout_size, &layout);
Courtney Goeltzenleuchter3226a6f2015-02-11 18:17:22 -0700579 assert(!err && layout_size == sizeof(layout));
Courtney Goeltzenleuchter3226a6f2015-02-11 18:17:22 -0700580
Mark Lobodzinski23182612015-05-29 09:32:35 -0500581 err = vkMapMemory(demo->device, tex_obj->mem, 0, 0, 0, &data);
Courtney Goeltzenleuchter3226a6f2015-02-11 18:17:22 -0700582 assert(!err);
583
584 for (y = 0; y < tex_height; y++) {
585 uint32_t *row = (uint32_t *) ((char *) data + layout.rowPitch * y);
586 for (x = 0; x < tex_width; x++)
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700587 row[x] = tex_colors[(x & 1) ^ (y & 1)];
Courtney Goeltzenleuchter3226a6f2015-02-11 18:17:22 -0700588 }
589
Mark Lobodzinski23182612015-05-29 09:32:35 -0500590 err = vkUnmapMemory(demo->device, tex_obj->mem);
Courtney Goeltzenleuchter3226a6f2015-02-11 18:17:22 -0700591 assert(!err);
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700592 }
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600593
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600594 tex_obj->imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600595 demo_set_image_layout(demo, tex_obj->image,
malnasse4b8ba4d2015-06-03 17:28:38 -0400596 VK_IMAGE_ASPECT_COLOR,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600597 VK_IMAGE_LAYOUT_UNDEFINED,
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600598 tex_obj->imageLayout);
599 /* 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 -0700600}
601
Mark Lobodzinskicf26e072015-04-16 11:44:05 -0500602static void demo_destroy_texture_image(struct demo *demo, struct texture_object *tex_obj)
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700603{
604 /* clean up staging resources */
Mark Lobodzinski23182612015-05-29 09:32:35 -0500605 vkBindObjectMemory(demo->device,
606 VK_OBJECT_TYPE_IMAGE, tex_obj->image, VK_NULL_HANDLE, 0);
607 vkFreeMemory(demo->device, tex_obj->mem);
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700608
Mike Stroyan230e6252015-04-17 12:36:38 -0600609 vkDestroyObject(demo->device, VK_OBJECT_TYPE_IMAGE, tex_obj->image);
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700610}
611
612static void demo_prepare_textures(struct demo *demo)
613{
Tony Barbour8205d902015-04-16 15:59:00 -0600614 const VkFormat tex_format = VK_FORMAT_B8G8R8A8_UNORM;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600615 VkFormatProperties props;
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700616 size_t size = sizeof(props);
617 const uint32_t tex_colors[DEMO_TEXTURE_COUNT][2] = {
618 { 0xffff0000, 0xff00ff00 },
619 };
Tony Barbour22a30862015-04-22 09:02:32 -0600620 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700621 uint32_t i;
622
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600623 err = vkGetFormatInfo(demo->device, tex_format,
Tony Barbour8205d902015-04-16 15:59:00 -0600624 VK_FORMAT_INFO_TYPE_PROPERTIES,
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700625 &size, &props);
626 assert(!err);
627
628 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Tony Barbour8205d902015-04-16 15:59:00 -0600629 if ((props.linearTilingFeatures & VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT) && !demo->use_staging_buffer) {
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700630 /* Device can texture using linear textures */
631 demo_prepare_texture_image(demo, tex_colors[i], &demo->textures[i],
Courtney Goeltzenleuchtercb67a322015-04-21 09:31:23 -0600632 VK_IMAGE_TILING_LINEAR, VK_IMAGE_USAGE_SAMPLED_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT);
Tony Barbour8205d902015-04-16 15:59:00 -0600633 } else if (props.optimalTilingFeatures & VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT){
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700634 /* Must use staging buffer to copy linear texture to optimized */
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600635 struct texture_object staging_texture;
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700636
637 memset(&staging_texture, 0, sizeof(staging_texture));
638 demo_prepare_texture_image(demo, tex_colors[i], &staging_texture,
Courtney Goeltzenleuchtercb67a322015-04-21 09:31:23 -0600639 VK_IMAGE_TILING_LINEAR, VK_IMAGE_USAGE_TRANSFER_SOURCE_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT);
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700640
641 demo_prepare_texture_image(demo, tex_colors[i], &demo->textures[i],
Courtney Goeltzenleuchtercb67a322015-04-21 09:31:23 -0600642 VK_IMAGE_TILING_OPTIMAL,
643 (VK_IMAGE_USAGE_TRANSFER_DESTINATION_BIT | VK_IMAGE_USAGE_SAMPLED_BIT),
644 VK_MEMORY_PROPERTY_DEVICE_ONLY);
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700645
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600646 demo_set_image_layout(demo, staging_texture.image,
malnasse4b8ba4d2015-06-03 17:28:38 -0400647 VK_IMAGE_ASPECT_COLOR,
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600648 staging_texture.imageLayout,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600649 VK_IMAGE_LAYOUT_TRANSFER_SOURCE_OPTIMAL);
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700650
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600651 demo_set_image_layout(demo, demo->textures[i].image,
malnasse4b8ba4d2015-06-03 17:28:38 -0400652 VK_IMAGE_ASPECT_COLOR,
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600653 demo->textures[i].imageLayout,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600654 VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL);
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700655
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600656 VkImageCopy copy_region = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600657 .srcSubresource = { VK_IMAGE_ASPECT_COLOR, 0, 0 },
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700658 .srcOffset = { 0, 0, 0 },
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600659 .destSubresource = { VK_IMAGE_ASPECT_COLOR, 0, 0 },
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700660 .destOffset = { 0, 0, 0 },
661 .extent = { staging_texture.tex_width, staging_texture.tex_height, 1 },
662 };
Courtney Goeltzenleuchter633f9c52015-04-30 10:58:33 -0600663 vkCmdCopyImage(demo->setup_cmd,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600664 staging_texture.image, VK_IMAGE_LAYOUT_TRANSFER_SOURCE_OPTIMAL,
665 demo->textures[i].image, VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL,
Courtney Goeltzenleuchter51cbf302015-03-25 11:25:10 -0600666 1, &copy_region);
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700667
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600668 demo_set_image_layout(demo, demo->textures[i].image,
malnasse4b8ba4d2015-06-03 17:28:38 -0400669 VK_IMAGE_ASPECT_COLOR,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600670 VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL,
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600671 demo->textures[i].imageLayout);
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700672
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600673 demo_flush_init_cmd(demo);
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700674
Courtney Goeltzenleuchter876629f2015-04-21 09:30:03 -0600675 demo_destroy_texture_image(demo, &staging_texture);
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700676 } else {
Tony Barbour8205d902015-04-16 15:59:00 -0600677 /* Can't support VK_FORMAT_B8G8R8A8_UNORM !? */
Piers Daniell886be472015-02-23 16:23:13 -0700678 assert(!"No support for B8G8R8A8_UNORM as texture image format");
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700679 }
Courtney Goeltzenleuchter3226a6f2015-02-11 18:17:22 -0700680
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600681 const VkSamplerCreateInfo sampler = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600682 .sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO,
Chia-I Wub043fe32014-10-06 15:30:33 +0800683 .pNext = NULL,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600684 .magFilter = VK_TEX_FILTER_NEAREST,
685 .minFilter = VK_TEX_FILTER_NEAREST,
Tony Barbour8205d902015-04-16 15:59:00 -0600686 .mipMode = VK_TEX_MIPMAP_MODE_BASE,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600687 .addressU = VK_TEX_ADDRESS_WRAP,
688 .addressV = VK_TEX_ADDRESS_WRAP,
689 .addressW = VK_TEX_ADDRESS_WRAP,
Chia-I Wub043fe32014-10-06 15:30:33 +0800690 .mipLodBias = 0.0f,
Courtney Goeltzenleuchterbc9c8162015-02-13 18:20:24 -0700691 .maxAnisotropy = 1,
Tony Barbour8205d902015-04-16 15:59:00 -0600692 .compareOp = VK_COMPARE_OP_NEVER,
Chia-I Wub043fe32014-10-06 15:30:33 +0800693 .minLod = 0.0f,
694 .maxLod = 0.0f,
Tony Barbour8205d902015-04-16 15:59:00 -0600695 .borderColor = VK_BORDER_COLOR_OPAQUE_WHITE,
Chia-I Wub043fe32014-10-06 15:30:33 +0800696 };
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600697 VkImageViewCreateInfo view = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600698 .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
Chia-I Wub043fe32014-10-06 15:30:33 +0800699 .pNext = NULL,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600700 .image = VK_NULL_HANDLE,
Tony Barbour8205d902015-04-16 15:59:00 -0600701 .viewType = VK_IMAGE_VIEW_TYPE_2D,
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700702 .format = tex_format,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600703 .channels = { VK_CHANNEL_SWIZZLE_R,
704 VK_CHANNEL_SWIZZLE_G,
705 VK_CHANNEL_SWIZZLE_B,
706 VK_CHANNEL_SWIZZLE_A, },
707 .subresourceRange = { VK_IMAGE_ASPECT_COLOR, 0, 1, 0, 1 },
Chia-I Wub043fe32014-10-06 15:30:33 +0800708 .minLod = 0.0f,
709 };
Jon Ashburna9ae3832015-01-16 09:37:43 -0700710
Chia-I Wub043fe32014-10-06 15:30:33 +0800711 /* create sampler */
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600712 err = vkCreateSampler(demo->device, &sampler,
Chia-I Wub043fe32014-10-06 15:30:33 +0800713 &demo->textures[i].sampler);
714 assert(!err);
715
Chia-I Wub043fe32014-10-06 15:30:33 +0800716 /* create image view */
717 view.image = demo->textures[i].image;
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600718 err = vkCreateImageView(demo->device, &view,
Courtney Goeltzenleuchter3226a6f2015-02-11 18:17:22 -0700719 &demo->textures[i].view);
Chia-I Wub043fe32014-10-06 15:30:33 +0800720 assert(!err);
Chia-I Wub043fe32014-10-06 15:30:33 +0800721 }
722}
723
Chia-I Wu99621bc2014-10-08 11:52:22 +0800724static void demo_prepare_vertices(struct demo *demo)
725{
726 const float vb[3][5] = {
727 /* position texcoord */
Chia-I Wue2504cb2015-04-22 14:20:52 +0800728 { -1.0f, -1.0f, 0.2f, 0.0f, 0.0f },
729 { 1.0f, -1.0f, 0.25f, 1.0f, 0.0f },
Chia-I Wu99621bc2014-10-08 11:52:22 +0800730 { 0.0f, 1.0f, 1.0f, 0.5f, 1.0f },
731 };
Courtney Goeltzenleuchterddcb6192015-04-14 18:48:46 -0600732 const VkBufferCreateInfo buf_info = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600733 .sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO,
Chia-I Wu714df452015-01-01 07:55:04 +0800734 .pNext = NULL,
735 .size = sizeof(vb),
Courtney Goeltzenleuchterad870812015-04-15 15:29:59 -0600736 .usage = VK_BUFFER_USAGE_VERTEX_BUFFER_BIT,
Chia-I Wu714df452015-01-01 07:55:04 +0800737 .flags = 0,
738 };
Courtney Goeltzenleuchterddcb6192015-04-14 18:48:46 -0600739 VkMemoryAllocInfo mem_alloc = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600740 .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOC_INFO,
Mark Lobodzinski97dcd042015-04-16 08:52:00 -0500741 .pNext = NULL,
Chia-I Wu714df452015-01-01 07:55:04 +0800742 .allocationSize = 0,
Tony Barbour8205d902015-04-16 15:59:00 -0600743 .memProps = VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600744 .memPriority = VK_MEMORY_PRIORITY_NORMAL,
Chia-I Wu99621bc2014-10-08 11:52:22 +0800745 };
Mark Lobodzinski23182612015-05-29 09:32:35 -0500746 VkMemoryRequirements mem_reqs;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600747 size_t mem_reqs_size = sizeof(VkMemoryRequirements);
Tony Barbour22a30862015-04-22 09:02:32 -0600748 VkResult U_ASSERT_ONLY err;
Chia-I Wu99621bc2014-10-08 11:52:22 +0800749 void *data;
750
751 memset(&demo->vertices, 0, sizeof(demo->vertices));
752
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600753 err = vkCreateBuffer(demo->device, &buf_info, &demo->vertices.buf);
Chia-I Wu714df452015-01-01 07:55:04 +0800754 assert(!err);
755
Mike Stroyan230e6252015-04-17 12:36:38 -0600756 err = vkGetObjectInfo(demo->device,
Mike Stroyan230e6252015-04-17 12:36:38 -0600757 VK_OBJECT_TYPE_BUFFER, demo->vertices.buf,
Tony Barbour8205d902015-04-16 15:59:00 -0600758 VK_OBJECT_INFO_TYPE_MEMORY_REQUIREMENTS,
Mark Lobodzinski23182612015-05-29 09:32:35 -0500759 &mem_reqs_size, &mem_reqs);
Chia-I Wu714df452015-01-01 07:55:04 +0800760
Mark Lobodzinski23182612015-05-29 09:32:35 -0500761 mem_alloc.allocationSize = mem_reqs.size;
Chia-I Wu99621bc2014-10-08 11:52:22 +0800762
Mark Lobodzinski23182612015-05-29 09:32:35 -0500763 err = vkAllocMemory(demo->device, &mem_alloc, &demo->vertices.mem);
764 assert(!err);
Chia-I Wu99621bc2014-10-08 11:52:22 +0800765
Mark Lobodzinski23182612015-05-29 09:32:35 -0500766 err = vkMapMemory(demo->device, demo->vertices.mem, 0, 0, 0, &data);
767 assert(!err);
Chia-I Wu99621bc2014-10-08 11:52:22 +0800768
Mark Lobodzinski23182612015-05-29 09:32:35 -0500769 memcpy(data, vb, sizeof(vb));
Chia-I Wu99621bc2014-10-08 11:52:22 +0800770
Mark Lobodzinski23182612015-05-29 09:32:35 -0500771 err = vkUnmapMemory(demo->device, demo->vertices.mem);
772 assert(!err);
773
774 err = vkBindObjectMemory(demo->device,
775 VK_OBJECT_TYPE_BUFFER, demo->vertices.buf,
776 demo->vertices.mem, 0);
777 assert(!err);
Chia-I Wu714df452015-01-01 07:55:04 +0800778
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600779 demo->vertices.vi.sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_CREATE_INFO;
Chia-I Wu8d29d022014-10-08 12:14:39 +0800780 demo->vertices.vi.pNext = NULL;
781 demo->vertices.vi.bindingCount = 1;
782 demo->vertices.vi.pVertexBindingDescriptions = demo->vertices.vi_bindings;
783 demo->vertices.vi.attributeCount = 2;
784 demo->vertices.vi.pVertexAttributeDescriptions = demo->vertices.vi_attrs;
785
Courtney Goeltzenleuchterf5cdad02015-03-31 16:36:30 -0600786 demo->vertices.vi_bindings[0].binding = VERTEX_BUFFER_BIND_ID;
Chia-I Wu8d29d022014-10-08 12:14:39 +0800787 demo->vertices.vi_bindings[0].strideInBytes = sizeof(vb[0]);
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600788 demo->vertices.vi_bindings[0].stepRate = VK_VERTEX_INPUT_STEP_RATE_VERTEX;
Chia-I Wu8d29d022014-10-08 12:14:39 +0800789
Courtney Goeltzenleuchterf5cdad02015-03-31 16:36:30 -0600790 demo->vertices.vi_attrs[0].binding = VERTEX_BUFFER_BIND_ID;
791 demo->vertices.vi_attrs[0].location = 0;
Tony Barbour8205d902015-04-16 15:59:00 -0600792 demo->vertices.vi_attrs[0].format = VK_FORMAT_R32G32B32_SFLOAT;
Chia-I Wu8d29d022014-10-08 12:14:39 +0800793 demo->vertices.vi_attrs[0].offsetInBytes = 0;
794
Courtney Goeltzenleuchterf5cdad02015-03-31 16:36:30 -0600795 demo->vertices.vi_attrs[1].binding = VERTEX_BUFFER_BIND_ID;
796 demo->vertices.vi_attrs[1].location = 1;
Tony Barbour8205d902015-04-16 15:59:00 -0600797 demo->vertices.vi_attrs[1].format = VK_FORMAT_R32G32_SFLOAT;
Chia-I Wu8d29d022014-10-08 12:14:39 +0800798 demo->vertices.vi_attrs[1].offsetInBytes = sizeof(float) * 3;
Chia-I Wu99621bc2014-10-08 11:52:22 +0800799}
800
Chia-I Wuf8385062015-01-04 16:27:24 +0800801static void demo_prepare_descriptor_layout(struct demo *demo)
Chia-I Wub043fe32014-10-06 15:30:33 +0800802{
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600803 const VkDescriptorSetLayoutBinding layout_binding = {
Courtney Goeltzenleuchterad870812015-04-15 15:29:59 -0600804 .descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
Chia-I Wud3114a22015-05-25 16:22:52 +0800805 .arraySize = DEMO_TEXTURE_COUNT,
Tony Barbour8205d902015-04-16 15:59:00 -0600806 .stageFlags = VK_SHADER_STAGE_FRAGMENT_BIT,
Chia-I Wu310eece2015-03-27 12:56:09 +0800807 .pImmutableSamplers = NULL,
Chia-I Wub043fe32014-10-06 15:30:33 +0800808 };
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600809 const VkDescriptorSetLayoutCreateInfo descriptor_layout = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600810 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO,
Chia-I Wufc9d9132015-03-26 15:04:41 +0800811 .pNext = NULL,
812 .count = 1,
813 .pBinding = &layout_binding,
814 };
Tony Barbour22a30862015-04-22 09:02:32 -0600815 VkResult U_ASSERT_ONLY err;
Chia-I Wub043fe32014-10-06 15:30:33 +0800816
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600817 err = vkCreateDescriptorSetLayout(demo->device,
Chia-I Wu7732cb22015-03-26 15:27:55 +0800818 &descriptor_layout, &demo->desc_layout);
819 assert(!err);
820
Mark Lobodzinski556f7212015-04-17 14:11:39 -0500821 const VkPipelineLayoutCreateInfo pPipelineLayoutCreateInfo = {
822 .sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO,
823 .pNext = NULL,
824 .descriptorSetCount = 1,
825 .pSetLayouts = &demo->desc_layout,
826 };
827
828 err = vkCreatePipelineLayout(demo->device,
829 &pPipelineLayoutCreateInfo,
830 &demo->pipeline_layout);
Chia-I Wub043fe32014-10-06 15:30:33 +0800831 assert(!err);
Chia-I Wub043fe32014-10-06 15:30:33 +0800832}
833
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600834static VkShader demo_prepare_shader(struct demo *demo,
Tony Barbour8205d902015-04-16 15:59:00 -0600835 VkShaderStage stage,
Chia-I Wuc19795a2014-09-13 11:12:55 +0800836 const void *code,
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600837 size_t size)
Chia-I Wuc19795a2014-09-13 11:12:55 +0800838{
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600839 VkShaderCreateInfo createInfo;
840 VkShader shader;
841 VkResult err;
Chia-I Wuc19795a2014-09-13 11:12:55 +0800842
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600843 createInfo.sType = VK_STRUCTURE_TYPE_SHADER_CREATE_INFO;
Courtney Goeltzenleuchter3f2606d2014-10-13 17:51:58 -0600844 createInfo.pNext = NULL;
845
Cody Northropacfb0492015-03-17 15:55:58 -0600846 // Create fake SPV structure to feed GLSL
Courtney Goeltzenleuchter3f2606d2014-10-13 17:51:58 -0600847 // 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;
851
Tony Barbour8205d902015-04-16 15:59:00 -0600852 /* try version 0 first: VkShaderStage followed by GLSL */
Cody Northropacfb0492015-03-17 15:55:58 -0600853 ((uint32_t *) createInfo.pCode)[0] = ICD_SPV_MAGIC;
Courtney Goeltzenleuchter3f2606d2014-10-13 17:51:58 -0600854 ((uint32_t *) createInfo.pCode)[1] = 0;
855 ((uint32_t *) createInfo.pCode)[2] = stage;
856 memcpy(((uint32_t *) createInfo.pCode + 3), code, size + 1);
857
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600858 err = vkCreateShader(demo->device, &createInfo, &shader);
Courtney Goeltzenleuchter3f2606d2014-10-13 17:51:58 -0600859 if (err) {
860 free((void *) createInfo.pCode);
Mike Stroyan230e6252015-04-17 12:36:38 -0600861 return VK_NULL_HANDLE;
Courtney Goeltzenleuchter3f2606d2014-10-13 17:51:58 -0600862 }
Chia-I Wuc19795a2014-09-13 11:12:55 +0800863
864 return shader;
865}
866
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600867static VkShader demo_prepare_vs(struct demo *demo)
Chia-I Wuc19795a2014-09-13 11:12:55 +0800868{
Courtney Goeltzenleuchter3f2606d2014-10-13 17:51:58 -0600869 static const char *vertShaderText =
Mark Lobodzinskiba4d2f02015-04-06 15:24:40 -0500870 "#version 140\n"
Courtney Goeltzenleuchterf5cdad02015-03-31 16:36:30 -0600871 "#extension GL_ARB_separate_shader_objects : enable\n"
872 "#extension GL_ARB_shading_language_420pack : enable\n"
873 "layout (location = 0) in vec4 pos;\n"
874 "layout (location = 1) in vec2 attr;\n"
Chia-I Wuf5caeb02014-10-25 12:11:27 +0800875 "out vec2 texcoord;\n"
Courtney Goeltzenleuchter3f2606d2014-10-13 17:51:58 -0600876 "void main() {\n"
Chia-I Wuf5caeb02014-10-25 12:11:27 +0800877 " texcoord = attr;\n"
878 " gl_Position = pos;\n"
Courtney Goeltzenleuchter3f2606d2014-10-13 17:51:58 -0600879 "}\n";
Courtney Goeltzenleuchteref7301b2014-09-17 13:17:12 -0600880
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600881 return demo_prepare_shader(demo, VK_SHADER_STAGE_VERTEX,
Courtney Goeltzenleuchter3f2606d2014-10-13 17:51:58 -0600882 (const void *) vertShaderText,
883 strlen(vertShaderText));
Chia-I Wuc19795a2014-09-13 11:12:55 +0800884}
885
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600886static VkShader demo_prepare_fs(struct demo *demo)
Chia-I Wuc19795a2014-09-13 11:12:55 +0800887{
Courtney Goeltzenleuchter3f2606d2014-10-13 17:51:58 -0600888 static const char *fragShaderText =
Mark Lobodzinskiba4d2f02015-04-06 15:24:40 -0500889 "#version 140\n"
Courtney Goeltzenleuchterf5cdad02015-03-31 16:36:30 -0600890 "#extension GL_ARB_separate_shader_objects : enable\n"
891 "#extension GL_ARB_shading_language_420pack : enable\n"
892 "layout (binding = 0) uniform sampler2D tex;\n"
893 "layout (location = 0) in vec2 texcoord;\n"
Courtney Goeltzenleuchter3f2606d2014-10-13 17:51:58 -0600894 "void main() {\n"
Chia-I Wuf5caeb02014-10-25 12:11:27 +0800895 " gl_FragColor = texture(tex, texcoord);\n"
Courtney Goeltzenleuchter3f2606d2014-10-13 17:51:58 -0600896 "}\n";
Courtney Goeltzenleuchteref7301b2014-09-17 13:17:12 -0600897
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600898 return demo_prepare_shader(demo, VK_SHADER_STAGE_FRAGMENT,
Courtney Goeltzenleuchter3f2606d2014-10-13 17:51:58 -0600899 (const void *) fragShaderText,
900 strlen(fragShaderText));
Chia-I Wuc19795a2014-09-13 11:12:55 +0800901}
902
903static void demo_prepare_pipeline(struct demo *demo)
904{
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600905 VkGraphicsPipelineCreateInfo pipeline;
906 VkPipelineVertexInputCreateInfo vi;
907 VkPipelineIaStateCreateInfo ia;
908 VkPipelineRsStateCreateInfo rs;
909 VkPipelineCbStateCreateInfo cb;
910 VkPipelineDsStateCreateInfo ds;
911 VkPipelineShaderStageCreateInfo vs;
912 VkPipelineShaderStageCreateInfo fs;
913 VkPipelineVpStateCreateInfo vp;
914 VkPipelineMsStateCreateInfo ms;
Tony Barbour22a30862015-04-22 09:02:32 -0600915 VkResult U_ASSERT_ONLY err;
Chia-I Wuc19795a2014-09-13 11:12:55 +0800916
917 memset(&pipeline, 0, sizeof(pipeline));
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600918 pipeline.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
Mark Lobodzinski556f7212015-04-17 14:11:39 -0500919 pipeline.layout = demo->pipeline_layout;
Chia-I Wuc19795a2014-09-13 11:12:55 +0800920
Chia-I Wu8d29d022014-10-08 12:14:39 +0800921 vi = demo->vertices.vi;
922
Chia-I Wuc19795a2014-09-13 11:12:55 +0800923 memset(&ia, 0, sizeof(ia));
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600924 ia.sType = VK_STRUCTURE_TYPE_PIPELINE_IA_STATE_CREATE_INFO;
Tony Barbour8205d902015-04-16 15:59:00 -0600925 ia.topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
Chia-I Wuc19795a2014-09-13 11:12:55 +0800926
927 memset(&rs, 0, sizeof(rs));
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600928 rs.sType = VK_STRUCTURE_TYPE_PIPELINE_RS_STATE_CREATE_INFO;
Tony Barbour8205d902015-04-16 15:59:00 -0600929 rs.fillMode = VK_FILL_MODE_SOLID;
Chia-I Wuc414ba82015-04-22 15:44:24 +0800930 rs.cullMode = VK_CULL_MODE_BACK;
931 rs.frontFace = VK_FRONT_FACE_CW;
Chia-I Wue2504cb2015-04-22 14:20:52 +0800932 rs.depthClipEnable = VK_TRUE;
Chia-I Wuc19795a2014-09-13 11:12:55 +0800933
934 memset(&cb, 0, sizeof(cb));
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600935 cb.sType = VK_STRUCTURE_TYPE_PIPELINE_CB_STATE_CREATE_INFO;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600936 VkPipelineCbAttachmentState att_state[1];
Tony Barbourfa6cac72015-01-16 14:27:35 -0700937 memset(att_state, 0, sizeof(att_state));
938 att_state[0].format = demo->format;
939 att_state[0].channelWriteMask = 0xf;
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600940 att_state[0].blendEnable = VK_FALSE;
Tony Barbourfa6cac72015-01-16 14:27:35 -0700941 cb.attachmentCount = 1;
942 cb.pAttachments = att_state;
Chia-I Wuc19795a2014-09-13 11:12:55 +0800943
Tony Barbourfa6cac72015-01-16 14:27:35 -0700944
945 memset(&vp, 0, sizeof(vp));
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600946 vp.sType = VK_STRUCTURE_TYPE_PIPELINE_VP_STATE_CREATE_INFO;
Tony Barbour8205d902015-04-16 15:59:00 -0600947 vp.viewportCount = 1;
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600948 vp.clipOrigin = VK_COORDINATE_ORIGIN_UPPER_LEFT;
Tony Barbourfa6cac72015-01-16 14:27:35 -0700949
950 memset(&ds, 0, sizeof(ds));
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600951 ds.sType = VK_STRUCTURE_TYPE_PIPELINE_DS_STATE_CREATE_INFO;
Tony Barbourfa6cac72015-01-16 14:27:35 -0700952 ds.format = demo->depth.format;
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600953 ds.depthTestEnable = VK_TRUE;
954 ds.depthWriteEnable = VK_TRUE;
Tony Barbour8205d902015-04-16 15:59:00 -0600955 ds.depthCompareOp = VK_COMPARE_OP_LESS_EQUAL;
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600956 ds.depthBoundsEnable = VK_FALSE;
957 ds.back.stencilFailOp = VK_STENCIL_OP_KEEP;
958 ds.back.stencilPassOp = VK_STENCIL_OP_KEEP;
Tony Barbour8205d902015-04-16 15:59:00 -0600959 ds.back.stencilCompareOp = VK_COMPARE_OP_ALWAYS;
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600960 ds.stencilTestEnable = VK_FALSE;
Tony Barbourfa6cac72015-01-16 14:27:35 -0700961 ds.front = ds.back;
Chia-I Wuc19795a2014-09-13 11:12:55 +0800962
Chia-I Wu99621bc2014-10-08 11:52:22 +0800963 memset(&vs, 0, sizeof(vs));
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600964 vs.sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
965 vs.shader.stage = VK_SHADER_STAGE_VERTEX;
Chia-I Wu99621bc2014-10-08 11:52:22 +0800966 vs.shader.shader = demo_prepare_vs(demo);
Tony Barbourfa6cac72015-01-16 14:27:35 -0700967 vs.shader.linkConstBufferCount = 0;
Chia-I Wu99621bc2014-10-08 11:52:22 +0800968
969 memset(&fs, 0, sizeof(fs));
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600970 fs.sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
971 fs.shader.stage = VK_SHADER_STAGE_FRAGMENT;
Chia-I Wu99621bc2014-10-08 11:52:22 +0800972 fs.shader.shader = demo_prepare_fs(demo);
Tony Barbourfa6cac72015-01-16 14:27:35 -0700973
974 memset(&ms, 0, sizeof(ms));
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600975 ms.sType = VK_STRUCTURE_TYPE_PIPELINE_MS_STATE_CREATE_INFO;
Tony Barbourfa6cac72015-01-16 14:27:35 -0700976 ms.sampleMask = 1;
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600977 ms.multisampleEnable = VK_FALSE;
Tony Barbourfa6cac72015-01-16 14:27:35 -0700978 ms.samples = 1;
Chia-I Wub043fe32014-10-06 15:30:33 +0800979
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600980 pipeline.pNext = (const void *) &vi;
981 vi.pNext = (void *) &ia;
982 ia.pNext = (const void *) &rs;
983 rs.pNext = (const void *) &cb;
984 cb.pNext = (const void *) &ms;
985 ms.pNext = (const void *) &vp;
986 vp.pNext = (const void *) &ds;
987 ds.pNext = (const void *) &vs;
988 vs.pNext = (const void *) &fs;
Chia-I Wuc19795a2014-09-13 11:12:55 +0800989
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600990 err = vkCreateGraphicsPipeline(demo->device, &pipeline, &demo->pipeline);
Chia-I Wuc19795a2014-09-13 11:12:55 +0800991 assert(!err);
992
Mike Stroyan230e6252015-04-17 12:36:38 -0600993 vkDestroyObject(demo->device, VK_OBJECT_TYPE_SHADER, vs.shader.shader);
994 vkDestroyObject(demo->device, VK_OBJECT_TYPE_SHADER, fs.shader.shader);
Chia-I Wuc19795a2014-09-13 11:12:55 +0800995}
996
997static void demo_prepare_dynamic_states(struct demo *demo)
998{
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600999 VkDynamicVpStateCreateInfo viewport_create;
1000 VkDynamicRsStateCreateInfo raster;
1001 VkDynamicCbStateCreateInfo color_blend;
1002 VkDynamicDsStateCreateInfo depth_stencil;
Tony Barbour22a30862015-04-22 09:02:32 -06001003 VkResult U_ASSERT_ONLY err;
Chia-I Wuc19795a2014-09-13 11:12:55 +08001004
Tony Barbourfa6cac72015-01-16 14:27:35 -07001005 memset(&viewport_create, 0, sizeof(viewport_create));
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001006 viewport_create.sType = VK_STRUCTURE_TYPE_DYNAMIC_VP_STATE_CREATE_INFO;
Courtney Goeltzenleuchterc6e32f92015-02-11 14:13:34 -07001007 viewport_create.viewportAndScissorCount = 1;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001008 VkViewport viewport;
Chia-I Wuc19795a2014-09-13 11:12:55 +08001009 memset(&viewport, 0, sizeof(viewport));
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -06001010 viewport.height = (float) demo->height;
Courtney Goeltzenleuchterc6e32f92015-02-11 14:13:34 -07001011 viewport.width = (float) demo->width;
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -06001012 viewport.minDepth = (float) 0.0f;
1013 viewport.maxDepth = (float) 1.0f;
Piers Daniell886be472015-02-23 16:23:13 -07001014 viewport_create.pViewports = &viewport;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001015 VkRect scissor;
Piers Daniell886be472015-02-23 16:23:13 -07001016 memset(&scissor, 0, sizeof(scissor));
Courtney Goeltzenleuchterc6e32f92015-02-11 14:13:34 -07001017 scissor.extent.width = demo->width;
1018 scissor.extent.height = demo->height;
1019 scissor.offset.x = 0;
1020 scissor.offset.y = 0;
Courtney Goeltzenleuchterc6e32f92015-02-11 14:13:34 -07001021 viewport_create.pScissors = &scissor;
Chia-I Wuc19795a2014-09-13 11:12:55 +08001022
1023 memset(&raster, 0, sizeof(raster));
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001024 raster.sType = VK_STRUCTURE_TYPE_DYNAMIC_RS_STATE_CREATE_INFO;
Piers Daniell886be472015-02-23 16:23:13 -07001025 raster.pointSize = 1.0;
1026 raster.lineWidth = 1.0;
Chia-I Wuc19795a2014-09-13 11:12:55 +08001027
1028 memset(&color_blend, 0, sizeof(color_blend));
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001029 color_blend.sType = VK_STRUCTURE_TYPE_DYNAMIC_CB_STATE_CREATE_INFO;
Piers Daniell886be472015-02-23 16:23:13 -07001030 color_blend.blendConst[0] = 1.0f;
1031 color_blend.blendConst[1] = 1.0f;
1032 color_blend.blendConst[2] = 1.0f;
1033 color_blend.blendConst[3] = 1.0f;
Chia-I Wuc19795a2014-09-13 11:12:55 +08001034
1035 memset(&depth_stencil, 0, sizeof(depth_stencil));
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001036 depth_stencil.sType = VK_STRUCTURE_TYPE_DYNAMIC_DS_STATE_CREATE_INFO;
Piers Daniell886be472015-02-23 16:23:13 -07001037 depth_stencil.minDepth = 0.0f;
1038 depth_stencil.maxDepth = 1.0f;
Tony Barbourfa6cac72015-01-16 14:27:35 -07001039 depth_stencil.stencilBackRef = 0;
1040 depth_stencil.stencilFrontRef = 0;
1041 depth_stencil.stencilReadMask = 0xff;
1042 depth_stencil.stencilWriteMask = 0xff;
Chia-I Wuc19795a2014-09-13 11:12:55 +08001043
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001044 err = vkCreateDynamicViewportState(demo->device, &viewport_create, &demo->viewport);
Chia-I Wuc19795a2014-09-13 11:12:55 +08001045 assert(!err);
1046
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001047 err = vkCreateDynamicRasterState(demo->device, &raster, &demo->raster);
Chia-I Wuc19795a2014-09-13 11:12:55 +08001048 assert(!err);
1049
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001050 err = vkCreateDynamicColorBlendState(demo->device,
Chia-I Wuc19795a2014-09-13 11:12:55 +08001051 &color_blend, &demo->color_blend);
1052 assert(!err);
1053
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001054 err = vkCreateDynamicDepthStencilState(demo->device,
Chia-I Wuc19795a2014-09-13 11:12:55 +08001055 &depth_stencil, &demo->depth_stencil);
1056 assert(!err);
1057}
1058
Chia-I Wu8d24b3b2015-03-26 13:14:16 +08001059static void demo_prepare_descriptor_pool(struct demo *demo)
Chia-I Wuf8385062015-01-04 16:27:24 +08001060{
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001061 const VkDescriptorTypeCount type_count = {
Courtney Goeltzenleuchterad870812015-04-15 15:29:59 -06001062 .type = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
Chia-I Wuf8385062015-01-04 16:27:24 +08001063 .count = DEMO_TEXTURE_COUNT,
1064 };
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001065 const VkDescriptorPoolCreateInfo descriptor_pool = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001066 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO,
Chia-I Wuf8385062015-01-04 16:27:24 +08001067 .pNext = NULL,
1068 .count = 1,
1069 .pTypeCount = &type_count,
1070 };
Tony Barbour22a30862015-04-22 09:02:32 -06001071 VkResult U_ASSERT_ONLY err;
Chia-I Wuf8385062015-01-04 16:27:24 +08001072
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001073 err = vkCreateDescriptorPool(demo->device,
1074 VK_DESCRIPTOR_POOL_USAGE_ONE_SHOT, 1,
Chia-I Wu8d24b3b2015-03-26 13:14:16 +08001075 &descriptor_pool, &demo->desc_pool);
Chia-I Wuf8385062015-01-04 16:27:24 +08001076 assert(!err);
1077}
1078
1079static void demo_prepare_descriptor_set(struct demo *demo)
1080{
Chia-I Wu8cd8ecd2015-05-25 16:27:55 +08001081 VkDescriptorInfo tex_descs[DEMO_TEXTURE_COUNT];
1082 VkWriteDescriptorSet write;
Tony Barbour22a30862015-04-22 09:02:32 -06001083 VkResult U_ASSERT_ONLY err;
Chia-I Wuf8385062015-01-04 16:27:24 +08001084 uint32_t count;
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -06001085 uint32_t i;
Chia-I Wuf8385062015-01-04 16:27:24 +08001086
Mike Stroyan230e6252015-04-17 12:36:38 -06001087 err = vkAllocDescriptorSets(demo->device, demo->desc_pool,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001088 VK_DESCRIPTOR_SET_USAGE_STATIC,
Chia-I Wuf8385062015-01-04 16:27:24 +08001089 1, &demo->desc_layout,
1090 &demo->desc_set, &count);
1091 assert(!err && count == 1);
1092
Mike Stroyan230e6252015-04-17 12:36:38 -06001093 vkClearDescriptorSets(demo->device, demo->desc_pool, 1, &demo->desc_set);
Chia-I Wu8cd8ecd2015-05-25 16:27:55 +08001094
1095 memset(&tex_descs, 0, sizeof(tex_descs));
1096 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
1097 tex_descs[i].sampler = demo->textures[i].sampler;
1098 tex_descs[i].imageView = demo->textures[i].view;
1099 tex_descs[i].imageLayout = VK_IMAGE_LAYOUT_GENERAL;
1100 }
1101
1102 memset(&write, 0, sizeof(write));
1103 write.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
1104 write.destSet = demo->desc_set;
1105 write.count = DEMO_TEXTURE_COUNT;
1106 write.descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
1107 write.pDescriptors = tex_descs;
1108
1109 err = vkUpdateDescriptorSets(demo->device, 1, &write, 0, NULL);
1110 assert(!err);
Chia-I Wuf8385062015-01-04 16:27:24 +08001111}
1112
Chia-I Wuc19795a2014-09-13 11:12:55 +08001113static void demo_prepare(struct demo *demo)
1114{
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001115 const VkCmdBufferCreateInfo cmd = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001116 .sType = VK_STRUCTURE_TYPE_CMD_BUFFER_CREATE_INFO,
Chia-I Wuc19795a2014-09-13 11:12:55 +08001117 .pNext = NULL,
Courtney Goeltzenleuchterf3168062015-03-05 18:09:39 -07001118 .queueNodeIndex = demo->graphics_queue_node_index,
Chia-I Wuc19795a2014-09-13 11:12:55 +08001119 .flags = 0,
1120 };
Tony Barbour22a30862015-04-22 09:02:32 -06001121 VkResult U_ASSERT_ONLY err;
Chia-I Wuc19795a2014-09-13 11:12:55 +08001122
Courtney Goeltzenleuchter633f9c52015-04-30 10:58:33 -06001123 err = vkCreateCommandBuffer(demo->device, &cmd, &demo->draw_cmd);
1124 assert(!err);
1125
Chia-I Wuc19795a2014-09-13 11:12:55 +08001126 demo_prepare_buffers(demo);
Chia-I Wu9ae87c92014-10-07 14:15:01 +08001127 demo_prepare_depth(demo);
Chia-I Wub043fe32014-10-06 15:30:33 +08001128 demo_prepare_textures(demo);
Chia-I Wu99621bc2014-10-08 11:52:22 +08001129 demo_prepare_vertices(demo);
Chia-I Wuf8385062015-01-04 16:27:24 +08001130 demo_prepare_descriptor_layout(demo);
Chia-I Wuc19795a2014-09-13 11:12:55 +08001131 demo_prepare_pipeline(demo);
1132 demo_prepare_dynamic_states(demo);
1133
Chia-I Wu8d24b3b2015-03-26 13:14:16 +08001134 demo_prepare_descriptor_pool(demo);
Chia-I Wuf8385062015-01-04 16:27:24 +08001135 demo_prepare_descriptor_set(demo);
Courtney Goeltzenleuchter633f9c52015-04-30 10:58:33 -06001136 demo->prepared = true;
Chia-I Wuc19795a2014-09-13 11:12:55 +08001137}
1138
Ian Elliotte14e9f92015-04-16 15:23:05 -06001139#ifdef _WIN32
1140static void demo_run(struct demo *demo)
1141{
Courtney Goeltzenleuchter633f9c52015-04-30 10:58:33 -06001142 if (!demo->prepared)
1143 return;
Ian Elliotte14e9f92015-04-16 15:23:05 -06001144 demo_draw(demo);
1145}
1146
1147// On MS-Windows, make this a global, so it's available to WndProc()
1148struct demo demo;
1149
1150// MS-Windows event handling function:
1151LRESULT CALLBACK WndProc(HWND hWnd,
1152 UINT uMsg,
1153 WPARAM wParam,
1154 LPARAM lParam)
1155{
Ian Elliott4e19ed02015-04-28 10:52:52 -06001156 char tmp_str[] = APP_LONG_NAME;
Ian Elliotte14e9f92015-04-16 15:23:05 -06001157
1158 switch(uMsg)
1159 {
1160 case WM_CREATE:
1161 return 0;
1162 case WM_CLOSE:
1163 PostQuitMessage(0);
1164 return 0;
1165 case WM_PAINT:
1166 demo_run(&demo);
1167 return 0;
1168 default:
1169 break;
1170 }
1171 return (DefWindowProc(hWnd, uMsg, wParam, lParam));
1172}
1173
1174static void demo_create_window(struct demo *demo)
1175{
1176 WNDCLASSEX win_class;
1177
1178 // Initialize the window class structure:
1179 win_class.cbSize = sizeof(WNDCLASSEX);
1180 win_class.style = CS_HREDRAW | CS_VREDRAW;
1181 win_class.lpfnWndProc = WndProc;
1182 win_class.cbClsExtra = 0;
1183 win_class.cbWndExtra = 0;
1184 win_class.hInstance = demo->connection; // hInstance
1185 win_class.hIcon = LoadIcon(NULL, IDI_APPLICATION);
1186 win_class.hCursor = LoadCursor(NULL, IDC_ARROW);
1187 win_class.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
1188 win_class.lpszMenuName = NULL;
1189 win_class.lpszClassName = demo->name;
1190 win_class.hIconSm = LoadIcon(NULL, IDI_WINLOGO);
1191 // Register window class:
1192 if (!RegisterClassEx(&win_class)) {
1193 // It didn't work, so try to give a useful error:
1194 printf("Unexpected error trying to start the application!\n");
1195 fflush(stdout);
1196 exit(1);
1197 }
1198 // Create window with the registered class:
1199 demo->window = CreateWindowEx(0,
1200 demo->name, // class name
1201 demo->name, // app name
1202 WS_OVERLAPPEDWINDOW | // window style
1203 WS_VISIBLE |
1204 WS_SYSMENU,
1205 100,100, // x/y coords
1206 demo->width, // width
1207 demo->height, // height
1208 NULL, // handle to parent
1209 NULL, // handle to menu
1210 demo->connection, // hInstance
1211 NULL); // no extra parameters
1212 if (!demo->window) {
1213 // It didn't work, so try to give a useful error:
1214 printf("Cannot create a window in which to draw!\n");
1215 fflush(stdout);
1216 exit(1);
1217 }
1218}
1219#else // _WIN32
1220
Chia-I Wuc19795a2014-09-13 11:12:55 +08001221static void demo_handle_event(struct demo *demo,
1222 const xcb_generic_event_t *event)
1223{
1224 switch (event->response_type & 0x7f) {
1225 case XCB_EXPOSE:
1226 demo_draw(demo);
1227 break;
Courtney Goeltzenleuchterca698052014-11-07 15:17:03 -07001228 case XCB_CLIENT_MESSAGE:
1229 if((*(xcb_client_message_event_t*)event).data.data32[0] ==
1230 (*demo->atom_wm_delete_window).atom) {
1231 demo->quit = true;
1232 }
1233 break;
Chia-I Wuc19795a2014-09-13 11:12:55 +08001234 case XCB_KEY_RELEASE:
1235 {
1236 const xcb_key_release_event_t *key =
1237 (const xcb_key_release_event_t *) event;
1238
1239 if (key->detail == 0x9)
1240 demo->quit = true;
1241 }
1242 break;
Courtney Goeltzenleuchterca698052014-11-07 15:17:03 -07001243 case XCB_DESTROY_NOTIFY:
1244 demo->quit = true;
1245 break;
Chia-I Wuc19795a2014-09-13 11:12:55 +08001246 default:
1247 break;
1248 }
1249}
1250
1251static void demo_run(struct demo *demo)
1252{
1253 xcb_flush(demo->connection);
1254
1255 while (!demo->quit) {
1256 xcb_generic_event_t *event;
1257
1258 event = xcb_wait_for_event(demo->connection);
1259 if (event) {
1260 demo_handle_event(demo, event);
1261 free(event);
1262 }
1263 }
1264}
1265
1266static void demo_create_window(struct demo *demo)
1267{
1268 uint32_t value_mask, value_list[32];
1269
1270 demo->window = xcb_generate_id(demo->connection);
1271
1272 value_mask = XCB_CW_BACK_PIXEL | XCB_CW_EVENT_MASK;
1273 value_list[0] = demo->screen->black_pixel;
1274 value_list[1] = XCB_EVENT_MASK_KEY_RELEASE |
Courtney Goeltzenleuchterca698052014-11-07 15:17:03 -07001275 XCB_EVENT_MASK_EXPOSURE |
1276 XCB_EVENT_MASK_STRUCTURE_NOTIFY;
Chia-I Wuc19795a2014-09-13 11:12:55 +08001277
1278 xcb_create_window(demo->connection,
1279 XCB_COPY_FROM_PARENT,
1280 demo->window, demo->screen->root,
1281 0, 0, demo->width, demo->height, 0,
1282 XCB_WINDOW_CLASS_INPUT_OUTPUT,
1283 demo->screen->root_visual,
1284 value_mask, value_list);
1285
Courtney Goeltzenleuchterca698052014-11-07 15:17:03 -07001286 /* Magic code that will send notification when window is destroyed */
1287 xcb_intern_atom_cookie_t cookie = xcb_intern_atom(demo->connection, 1, 12,
1288 "WM_PROTOCOLS");
1289 xcb_intern_atom_reply_t* reply = xcb_intern_atom_reply(demo->connection, cookie, 0);
1290
1291 xcb_intern_atom_cookie_t cookie2 = xcb_intern_atom(demo->connection, 0, 16, "WM_DELETE_WINDOW");
1292 demo->atom_wm_delete_window = xcb_intern_atom_reply(demo->connection, cookie2, 0);
1293
1294 xcb_change_property(demo->connection, XCB_PROP_MODE_REPLACE,
1295 demo->window, (*reply).atom, 4, 32, 1,
1296 &(*demo->atom_wm_delete_window).atom);
1297 free(reply);
1298
Chia-I Wuc19795a2014-09-13 11:12:55 +08001299 xcb_map_window(demo->connection, demo->window);
1300}
Ian Elliotte14e9f92015-04-16 15:23:05 -06001301#endif // _WIN32
Chia-I Wuc19795a2014-09-13 11:12:55 +08001302
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001303static void demo_init_vk(struct demo *demo)
Chia-I Wuc19795a2014-09-13 11:12:55 +08001304{
Tobin Ehlis3536b442015-04-16 18:04:57 -06001305 VkResult err;
1306 // Extensions to enable
1307 const char *ext_names[] = {
Chia-I Wu5b66aa52015-04-16 22:02:10 +08001308 "VK_WSI_LunarG",
Tobin Ehlis3536b442015-04-16 18:04:57 -06001309 };
1310 size_t extSize = sizeof(uint32_t);
1311 uint32_t extCount = 0;
1312 err = vkGetGlobalExtensionInfo(VK_EXTENSION_INFO_TYPE_COUNT, 0, &extSize, &extCount);
1313 assert(!err);
1314
1315 VkExtensionProperties extProp;
1316 extSize = sizeof(VkExtensionProperties);
Tony Barbour22a30862015-04-22 09:02:32 -06001317 bool32_t U_ASSERT_ONLY extFound = 0;
Tobin Ehlis3536b442015-04-16 18:04:57 -06001318 for (uint32_t i = 0; i < extCount; i++) {
1319 err = vkGetGlobalExtensionInfo(VK_EXTENSION_INFO_TYPE_PROPERTIES, i, &extSize, &extProp);
1320 if (!strcmp(ext_names[0], extProp.extName))
1321 extFound = 1;
1322 }
Ian Elliott3b375cf2015-04-28 13:22:33 -06001323 if (!extFound) {
1324 ERR_EXIT("vkGetGlobalExtensionInfo failed to find the "
1325 "\"VK_WSI_LunarG\" extension.\n\nDo you have a compatible "
1326 "Vulkan installable client driver (ICD) installed?\nPlease "
1327 "look at the Getting Started guide for additional "
1328 "information.\n",
1329 "vkCreateInstance Failure");
1330 }
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001331 const VkApplicationInfo app = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001332 .sType = VK_STRUCTURE_TYPE_APPLICATION_INFO,
Chia-I Wuc19795a2014-09-13 11:12:55 +08001333 .pNext = NULL,
Ian Elliott4e19ed02015-04-28 10:52:52 -06001334 .pAppName = APP_SHORT_NAME,
Chia-I Wuc19795a2014-09-13 11:12:55 +08001335 .appVersion = 0,
Ian Elliott4e19ed02015-04-28 10:52:52 -06001336 .pEngineName = APP_SHORT_NAME,
Chia-I Wuc19795a2014-09-13 11:12:55 +08001337 .engineVersion = 0,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001338 .apiVersion = VK_API_VERSION,
Chia-I Wuc19795a2014-09-13 11:12:55 +08001339 };
Courtney Goeltzenleuchterddcb6192015-04-14 18:48:46 -06001340 const VkInstanceCreateInfo inst_info = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001341 .sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO,
Jon Ashburn29669a42015-04-04 14:52:07 -06001342 .pNext = NULL,
1343 .pAppInfo = &app,
1344 .pAllocCb = NULL,
Tobin Ehlis3536b442015-04-16 18:04:57 -06001345 .extensionCount = 1,
1346 .ppEnabledExtensionNames = ext_names,
Jon Ashburn29669a42015-04-04 14:52:07 -06001347 };
Courtney Goeltzenleuchterddcb6192015-04-14 18:48:46 -06001348 const VkDeviceQueueCreateInfo queue = {
Chia-I Wuc19795a2014-09-13 11:12:55 +08001349 .queueNodeIndex = 0,
1350 .queueCount = 1,
1351 };
Courtney Goeltzenleuchterddcb6192015-04-14 18:48:46 -06001352 const VkDeviceCreateInfo device = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001353 .sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO,
Chia-I Wuc19795a2014-09-13 11:12:55 +08001354 .pNext = NULL,
1355 .queueRecordCount = 1,
1356 .pRequestedQueues = &queue,
Tobin Ehlis3536b442015-04-16 18:04:57 -06001357 .extensionCount = 1,
Chia-I Wuc19795a2014-09-13 11:12:55 +08001358 .ppEnabledExtensionNames = ext_names,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001359 .flags = VK_DEVICE_CREATE_VALIDATION_BIT,
Chia-I Wuc19795a2014-09-13 11:12:55 +08001360 };
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -06001361 uint32_t gpu_count;
1362 uint32_t i;
Courtney Goeltzenleuchterf3168062015-03-05 18:09:39 -07001363 size_t data_size;
1364 uint32_t queue_count;
Chia-I Wuc19795a2014-09-13 11:12:55 +08001365
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001366 err = vkCreateInstance(&inst_info, &demo->inst);
Ian Elliottcaa9f272015-04-28 11:35:02 -06001367 if (err == VK_ERROR_INCOMPATIBLE_DRIVER) {
1368 ERR_EXIT("Cannot find a compatible Vulkan installable client driver "
Ian Elliott3b375cf2015-04-28 13:22:33 -06001369 "(ICD).\n\nPlease look at the Getting Started guide for "
Ian Elliottcaa9f272015-04-28 11:35:02 -06001370 "additional information.\n",
1371 "vkCreateInstance Failure");
1372 } else if (err) {
Ian Elliott3b375cf2015-04-28 13:22:33 -06001373 ERR_EXIT("vkCreateInstance failed.\n\nDo you have a compatible Vulkan "
1374 "installable client driver (ICD) installed?\nPlease look at "
Ian Elliottcaa9f272015-04-28 11:35:02 -06001375 "the Getting Started guide for additional information.\n",
1376 "vkCreateInstance Failure");
Ian Elliottdfe55f72015-04-03 15:24:55 -06001377 }
Jon Ashburn29669a42015-04-04 14:52:07 -06001378
Jon Ashburn07b309a2015-04-15 11:31:12 -06001379 gpu_count = 1;
1380 err = vkEnumeratePhysicalDevices(demo->inst, &gpu_count, &demo->gpu);
Chia-I Wuc19795a2014-09-13 11:12:55 +08001381 assert(!err && gpu_count == 1);
1382
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001383 err = vkCreateDevice(demo->gpu, &device, &demo->device);
Chia-I Wuc19795a2014-09-13 11:12:55 +08001384 assert(!err);
1385
Tony Barbour8205d902015-04-16 15:59:00 -06001386 err = vkGetPhysicalDeviceInfo(demo->gpu, VK_PHYSICAL_DEVICE_INFO_TYPE_PROPERTIES,
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -06001387 &data_size, NULL);
1388 assert(!err);
1389
Tony Barbour8205d902015-04-16 15:59:00 -06001390 demo->gpu_props = (VkPhysicalDeviceProperties *) malloc(data_size);
1391 err = vkGetPhysicalDeviceInfo(demo->gpu, VK_PHYSICAL_DEVICE_INFO_TYPE_PROPERTIES,
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -06001392 &data_size, demo->gpu_props);
1393 assert(!err);
1394
Tony Barbour8205d902015-04-16 15:59:00 -06001395 err = vkGetPhysicalDeviceInfo(demo->gpu, VK_PHYSICAL_DEVICE_INFO_TYPE_QUEUE_PROPERTIES,
Courtney Goeltzenleuchterf3168062015-03-05 18:09:39 -07001396 &data_size, NULL);
1397 assert(!err);
1398
Tony Barbour8205d902015-04-16 15:59:00 -06001399 demo->queue_props = (VkPhysicalDeviceQueueProperties *) malloc(data_size);
1400 err = vkGetPhysicalDeviceInfo(demo->gpu, VK_PHYSICAL_DEVICE_INFO_TYPE_QUEUE_PROPERTIES,
Courtney Goeltzenleuchterf3168062015-03-05 18:09:39 -07001401 &data_size, demo->queue_props);
1402 assert(!err);
Mike Stroyan230e6252015-04-17 12:36:38 -06001403 queue_count = (uint32_t) (data_size / sizeof(VkPhysicalDeviceQueueProperties));
Courtney Goeltzenleuchterf3168062015-03-05 18:09:39 -07001404 assert(queue_count >= 1);
1405
1406 for (i = 0; i < queue_count; i++) {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001407 if (demo->queue_props[i].queueFlags & VK_QUEUE_GRAPHICS_BIT)
Courtney Goeltzenleuchterf3168062015-03-05 18:09:39 -07001408 break;
1409 }
1410 assert(i < queue_count);
1411 demo->graphics_queue_node_index = i;
1412
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001413 err = vkGetDeviceQueue(demo->device, demo->graphics_queue_node_index,
Chia-I Wuc19795a2014-09-13 11:12:55 +08001414 0, &demo->queue);
1415 assert(!err);
Ian Elliott32536f92015-04-21 16:41:02 -06001416
1417
1418 // Get the VkDisplayWSI's associated with this physical device:
1419 VkDisplayWSI display;
1420 err = vkGetPhysicalDeviceInfo(demo->gpu, VK_PHYSICAL_DEVICE_INFO_TYPE_DISPLAY_PROPERTIES_WSI,
1421 &data_size, NULL);
Ian Elliotte16c6cf2015-05-20 14:54:42 -06001422 if (err != VK_SUCCESS) {
1423 printf("The Vulkan installable client driver (ICD) does not support "
1424 "querying\nfor the swap-chain image format. Therefore, am "
1425 "hardcoding this\nformat to VK_FORMAT_B8G8R8A8_UNORM.\n");
1426 fflush(stdout);
1427 demo->format = VK_FORMAT_B8G8R8A8_UNORM;
1428 return;
1429 }
Ian Elliott32536f92015-04-21 16:41:02 -06001430 demo->display_props = (VkDisplayPropertiesWSI *) malloc(data_size);
1431 err = vkGetPhysicalDeviceInfo(demo->gpu, VK_PHYSICAL_DEVICE_INFO_TYPE_DISPLAY_PROPERTIES_WSI,
1432 &data_size, demo->display_props);
1433 assert(!err);
1434 demo->num_displays = data_size / sizeof(VkDisplayPropertiesWSI);
1435 // For now, simply use the first display (TODO: Enhance this for the
1436 // future):
1437 display = demo->display_props[0].display;
1438
1439 // Get a VkFormat to use with the VkDisplayWSI we are using:
1440 err = vkGetDisplayInfoWSI(display, VK_DISPLAY_INFO_TYPE_FORMAT_PROPERTIES_WSI,
1441 &data_size, NULL);
1442 VkDisplayFormatPropertiesWSI* display_format_props =
1443 (VkDisplayFormatPropertiesWSI*) malloc(data_size);
1444 err = vkGetDisplayInfoWSI(display, VK_DISPLAY_INFO_TYPE_FORMAT_PROPERTIES_WSI,
1445 &data_size, display_format_props);
1446 // For now, simply use the first VkFormat (TODO: Enhance this for the
1447 // future):
1448 demo->format = display_format_props[0].swapChainFormat;
Chia-I Wuc19795a2014-09-13 11:12:55 +08001449}
1450
1451static void demo_init_connection(struct demo *demo)
1452{
Ian Elliotte14e9f92015-04-16 15:23:05 -06001453#ifndef _WIN32
Chia-I Wuc19795a2014-09-13 11:12:55 +08001454 const xcb_setup_t *setup;
1455 xcb_screen_iterator_t iter;
1456 int scr;
1457
1458 demo->connection = xcb_connect(NULL, &scr);
Ian Elliottdfe55f72015-04-03 15:24:55 -06001459 if (demo->connection == NULL) {
1460 printf("Cannot find a compatible Vulkan installable client driver "
1461 "(ICD).\nExiting ...\n");
1462 fflush(stdout);
1463 exit(1);
1464 }
Chia-I Wuc19795a2014-09-13 11:12:55 +08001465
1466 setup = xcb_get_setup(demo->connection);
1467 iter = xcb_setup_roots_iterator(setup);
1468 while (scr-- > 0)
1469 xcb_screen_next(&iter);
1470
1471 demo->screen = iter.data;
Ian Elliotte14e9f92015-04-16 15:23:05 -06001472#endif // _WIN32
Chia-I Wuc19795a2014-09-13 11:12:55 +08001473}
1474
Ian Elliotte14e9f92015-04-16 15:23:05 -06001475#ifdef _WIN32
1476static void demo_init(struct demo *demo, HINSTANCE hInstance, LPSTR pCmdLine)
1477#else // _WIN32
Courtney Goeltzenleuchterf113a952015-02-25 11:46:58 -07001478static void demo_init(struct demo *demo, const int argc, const char *argv[])
Ian Elliotte14e9f92015-04-16 15:23:05 -06001479#endif // _WIN32
Chia-I Wuc19795a2014-09-13 11:12:55 +08001480{
Ian Elliotte14e9f92015-04-16 15:23:05 -06001481 bool argv_error = false;
1482
Chia-I Wuc19795a2014-09-13 11:12:55 +08001483 memset(demo, 0, sizeof(*demo));
1484
Ian Elliotte14e9f92015-04-16 15:23:05 -06001485#ifdef _WIN32
1486 demo->connection = hInstance;
Ian Elliott4e19ed02015-04-28 10:52:52 -06001487 strncpy(demo->name, APP_SHORT_NAME, APP_NAME_STR_LEN);
Ian Elliotte14e9f92015-04-16 15:23:05 -06001488
1489 if (strncmp(pCmdLine, "--use_staging", strlen("--use_staging")) == 0)
1490 demo->use_staging_buffer = true;
1491 else if (strlen(pCmdLine) != 0) {
1492 fprintf(stderr, "Do not recognize argument \"%s\".\n", pCmdLine);
1493 argv_error = true;
1494 }
1495#else // _WIN32
Courtney Goeltzenleuchterf113a952015-02-25 11:46:58 -07001496 for (int i = 0; i < argc; i++) {
1497 if (strncmp(argv[i], "--use_staging", strlen("--use_staging")) == 0)
1498 demo->use_staging_buffer = true;
1499 }
Ian Elliotte14e9f92015-04-16 15:23:05 -06001500#endif // _WIN32
1501 if (argv_error) {
Ian Elliott4e19ed02015-04-28 10:52:52 -06001502 fprintf(stderr, "Usage:\n %s [--use_staging]\n", APP_SHORT_NAME);
Ian Elliotte14e9f92015-04-16 15:23:05 -06001503 fflush(stderr);
1504 exit(1);
1505 }
Courtney Goeltzenleuchterf113a952015-02-25 11:46:58 -07001506
Chia-I Wuc19795a2014-09-13 11:12:55 +08001507 demo_init_connection(demo);
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001508 demo_init_vk(demo);
Chia-I Wuc19795a2014-09-13 11:12:55 +08001509
1510 demo->width = 300;
1511 demo->height = 300;
Chia-I Wuc19795a2014-09-13 11:12:55 +08001512}
1513
1514static void demo_cleanup(struct demo *demo)
1515{
Mark Lobodzinski23182612015-05-29 09:32:35 -05001516 uint32_t i;
Chia-I Wuc19795a2014-09-13 11:12:55 +08001517
Mike Stroyan230e6252015-04-17 12:36:38 -06001518 vkDestroyObject(demo->device, VK_OBJECT_TYPE_DESCRIPTOR_SET, demo->desc_set);
1519 vkDestroyObject(demo->device, VK_OBJECT_TYPE_DESCRIPTOR_POOL, demo->desc_pool);
Chia-I Wuf8385062015-01-04 16:27:24 +08001520
Courtney Goeltzenleuchter633f9c52015-04-30 10:58:33 -06001521 if (demo->setup_cmd) {
1522 vkDestroyObject(demo->device, VK_OBJECT_TYPE_COMMAND_BUFFER, demo->setup_cmd);
1523 }
1524 vkDestroyObject(demo->device, VK_OBJECT_TYPE_COMMAND_BUFFER, demo->draw_cmd);
Chia-I Wuc19795a2014-09-13 11:12:55 +08001525
Mike Stroyan230e6252015-04-17 12:36:38 -06001526 vkDestroyObject(demo->device, VK_OBJECT_TYPE_DYNAMIC_VP_STATE, demo->viewport);
1527 vkDestroyObject(demo->device, VK_OBJECT_TYPE_DYNAMIC_RS_STATE, demo->raster);
1528 vkDestroyObject(demo->device, VK_OBJECT_TYPE_DYNAMIC_CB_STATE, demo->color_blend);
1529 vkDestroyObject(demo->device, VK_OBJECT_TYPE_DYNAMIC_DS_STATE, demo->depth_stencil);
Chia-I Wuc19795a2014-09-13 11:12:55 +08001530
Mike Stroyan230e6252015-04-17 12:36:38 -06001531 vkDestroyObject(demo->device, VK_OBJECT_TYPE_PIPELINE, demo->pipeline);
1532 vkDestroyObject(demo->device, VK_OBJECT_TYPE_PIPELINE_LAYOUT, demo->pipeline_layout);
1533 vkDestroyObject(demo->device, VK_OBJECT_TYPE_DESCRIPTOR_SET_LAYOUT, demo->desc_layout);
Chia-I Wub043fe32014-10-06 15:30:33 +08001534
Mark Lobodzinski23182612015-05-29 09:32:35 -05001535 vkBindObjectMemory(demo->device, VK_OBJECT_TYPE_BUFFER, demo->vertices.buf, VK_NULL_HANDLE, 0);
Mike Stroyan230e6252015-04-17 12:36:38 -06001536 vkDestroyObject(demo->device, VK_OBJECT_TYPE_BUFFER, demo->vertices.buf);
Mark Lobodzinski23182612015-05-29 09:32:35 -05001537 vkFreeMemory(demo->device, demo->vertices.mem);
Chia-I Wu99621bc2014-10-08 11:52:22 +08001538
Chia-I Wub043fe32014-10-06 15:30:33 +08001539 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Mike Stroyan230e6252015-04-17 12:36:38 -06001540 vkDestroyObject(demo->device, VK_OBJECT_TYPE_IMAGE_VIEW, demo->textures[i].view);
Mark Lobodzinski23182612015-05-29 09:32:35 -05001541 vkBindObjectMemory(demo->device, VK_OBJECT_TYPE_IMAGE, demo->textures[i].image, VK_NULL_HANDLE, 0);
Mike Stroyan230e6252015-04-17 12:36:38 -06001542 vkDestroyObject(demo->device, VK_OBJECT_TYPE_IMAGE, demo->textures[i].image);
Mark Lobodzinski23182612015-05-29 09:32:35 -05001543 vkFreeMemory(demo->device, demo->textures[i].mem);
Mike Stroyan230e6252015-04-17 12:36:38 -06001544 vkDestroyObject(demo->device, VK_OBJECT_TYPE_SAMPLER, demo->textures[i].sampler);
Chia-I Wub043fe32014-10-06 15:30:33 +08001545 }
1546
Mike Stroyan230e6252015-04-17 12:36:38 -06001547 vkDestroyObject(demo->device, VK_OBJECT_TYPE_DEPTH_STENCIL_VIEW, demo->depth.view);
Mark Lobodzinski23182612015-05-29 09:32:35 -05001548 vkBindObjectMemory(demo->device, VK_OBJECT_TYPE_IMAGE, demo->depth.image, VK_NULL_HANDLE, 0);
Mike Stroyan230e6252015-04-17 12:36:38 -06001549 vkDestroyObject(demo->device, VK_OBJECT_TYPE_IMAGE, demo->depth.image);
Mark Lobodzinski23182612015-05-29 09:32:35 -05001550 vkFreeMemory(demo->device, demo->depth.mem);
Chia-I Wu9ae87c92014-10-07 14:15:01 +08001551
Chia-I Wuc19795a2014-09-13 11:12:55 +08001552 for (i = 0; i < DEMO_BUFFER_COUNT; i++) {
Mike Stroyan230e6252015-04-17 12:36:38 -06001553 vkDestroyObject(demo->device, VK_OBJECT_TYPE_COLOR_ATTACHMENT_VIEW, demo->buffers[i].view);
Chia-I Wuc19795a2014-09-13 11:12:55 +08001554 }
Chia-I Wu5b66aa52015-04-16 22:02:10 +08001555 vkDestroySwapChainWSI(demo->swap_chain);
Chia-I Wuc19795a2014-09-13 11:12:55 +08001556
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001557 vkDestroyDevice(demo->device);
1558 vkDestroyInstance(demo->inst);
Chia-I Wuc19795a2014-09-13 11:12:55 +08001559
Ian Elliotte14e9f92015-04-16 15:23:05 -06001560#ifndef _WIN32
Chia-I Wuc19795a2014-09-13 11:12:55 +08001561 xcb_destroy_window(demo->connection, demo->window);
1562 xcb_disconnect(demo->connection);
Ian Elliotte14e9f92015-04-16 15:23:05 -06001563#endif // _WIN32
Chia-I Wuc19795a2014-09-13 11:12:55 +08001564}
1565
Ian Elliotte14e9f92015-04-16 15:23:05 -06001566#ifdef _WIN32
1567int APIENTRY WinMain(HINSTANCE hInstance,
1568 HINSTANCE hPrevInstance,
1569 LPSTR pCmdLine,
1570 int nCmdShow)
1571{
1572 MSG msg; // message
1573 bool done; // flag saying when app is complete
1574
1575 demo_init(&demo, hInstance, pCmdLine);
1576 demo_create_window(&demo);
1577
1578 demo_prepare(&demo);
1579
1580 done = false; //initialize loop condition variable
1581 /* main message loop*/
1582 while(!done)
1583 {
Ian Elliott421107f2015-04-28 15:50:36 -06001584 PeekMessage(&msg, NULL, 0, 0, PM_REMOVE);
Ian Elliotte14e9f92015-04-16 15:23:05 -06001585 if (msg.message == WM_QUIT) //check for a quit message
1586 {
1587 done = true; //if found, quit app
1588 }
1589 else
1590 {
1591 /* Translate and dispatch to event queue*/
1592 TranslateMessage(&msg);
1593 DispatchMessage(&msg);
1594 }
1595 }
1596
1597 demo_cleanup(&demo);
1598
Tony Barboura938abb2015-04-22 11:36:22 -06001599 return (int) msg.wParam;
Ian Elliotte14e9f92015-04-16 15:23:05 -06001600}
1601#else // _WIN32
Courtney Goeltzenleuchterf113a952015-02-25 11:46:58 -07001602int main(const int argc, const char *argv[])
Chia-I Wuc19795a2014-09-13 11:12:55 +08001603{
1604 struct demo demo;
1605
Courtney Goeltzenleuchterf113a952015-02-25 11:46:58 -07001606 demo_init(&demo, argc, argv);
Chia-I Wu5b66aa52015-04-16 22:02:10 +08001607 demo_create_window(&demo);
Chia-I Wuc19795a2014-09-13 11:12:55 +08001608
1609 demo_prepare(&demo);
Chia-I Wuc19795a2014-09-13 11:12:55 +08001610 demo_run(&demo);
1611
1612 demo_cleanup(&demo);
1613
Chia-I Wuc19795a2014-09-13 11:12:55 +08001614 return 0;
1615}
Ian Elliotte14e9f92015-04-16 15:23:05 -06001616#endif // _WIN32