blob: d8264a1f9a2a29065a3c2a068720f02a159da4eb [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,
192 VkImageLayout old_image_layout,
193 VkImageLayout new_image_layout)
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600194{
Tony Barbour22a30862015-04-22 09:02:32 -0600195 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600196
Courtney Goeltzenleuchter633f9c52015-04-30 10:58:33 -0600197 if (demo->setup_cmd == VK_NULL_HANDLE) {
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600198 const VkCmdBufferCreateInfo cmd = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600199 .sType = VK_STRUCTURE_TYPE_CMD_BUFFER_CREATE_INFO,
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600200 .pNext = NULL,
201 .queueNodeIndex = demo->graphics_queue_node_index,
202 .flags = 0,
203 };
204
Courtney Goeltzenleuchter633f9c52015-04-30 10:58:33 -0600205 err = vkCreateCommandBuffer(demo->device, &cmd, &demo->setup_cmd);
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600206 assert(!err);
207
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600208 VkCmdBufferBeginInfo cmd_buf_info = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600209 .sType = VK_STRUCTURE_TYPE_CMD_BUFFER_BEGIN_INFO,
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600210 .pNext = NULL,
Tony Barbour8205d902015-04-16 15:59:00 -0600211 .flags = VK_CMD_BUFFER_OPTIMIZE_SMALL_BATCH_BIT |
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600212 VK_CMD_BUFFER_OPTIMIZE_ONE_TIME_SUBMIT_BIT,
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600213 };
Courtney Goeltzenleuchter633f9c52015-04-30 10:58:33 -0600214 err = vkBeginCommandBuffer(demo->setup_cmd, &cmd_buf_info);
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600215 }
216
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600217 VkImageMemoryBarrier image_memory_barrier = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600218 .sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600219 .pNext = NULL,
220 .outputMask = 0,
221 .inputMask = 0,
222 .oldLayout = old_image_layout,
223 .newLayout = new_image_layout,
224 .image = image,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600225 .subresourceRange = { VK_IMAGE_ASPECT_COLOR, 0, 1, 0, 0 }
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600226 };
227
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600228 if (new_image_layout == VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL) {
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600229 /* Make sure anything that was copying from this image has completed */
Courtney Goeltzenleuchterad870812015-04-15 15:29:59 -0600230 image_memory_barrier.inputMask = VK_MEMORY_INPUT_TRANSFER_BIT;
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600231 }
232
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600233 if (new_image_layout == VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL) {
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600234 /* Make sure any Copy or CPU writes to image are flushed */
Courtney Goeltzenleuchtera569a502015-04-29 17:16:21 -0600235 image_memory_barrier.outputMask = VK_MEMORY_OUTPUT_TRANSFER_BIT | VK_MEMORY_OUTPUT_HOST_WRITE_BIT;
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600236 }
237
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600238 VkImageMemoryBarrier *pmemory_barrier = &image_memory_barrier;
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600239
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600240 VkPipeEvent set_events[] = { VK_PIPE_EVENT_TOP_OF_PIPE };
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600241
Courtney Goeltzenleuchter633f9c52015-04-30 10:58:33 -0600242 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 -0600243}
244
Chia-I Wuc19795a2014-09-13 11:12:55 +0800245static void demo_draw_build_cmd(struct demo *demo)
246{
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600247 const VkColorAttachmentBindInfo color_attachment = {
Chia-I Wuc19795a2014-09-13 11:12:55 +0800248 .view = demo->buffers[demo->current_buffer].view,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600249 .layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
Chia-I Wuc19795a2014-09-13 11:12:55 +0800250 };
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600251 const VkDepthStencilBindInfo depth_stencil = {
Chia-I Wu9ae87c92014-10-07 14:15:01 +0800252 .view = demo->depth.view,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600253 .layout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
Chia-I Wu9ae87c92014-10-07 14:15:01 +0800254 };
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600255 const VkClearColor clear_color = {
Courtney Goeltzenleuchter9a1ded82015-04-03 16:35:32 -0600256 .color.floatColor = { 0.2f, 0.2f, 0.2f, 0.2f },
257 .useRawValue = false,
258 };
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600259 const float clear_depth = 0.9f;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600260 VkImageSubresourceRange clear_range;
261 VkCmdBufferBeginInfo cmd_buf_info = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600262 .sType = VK_STRUCTURE_TYPE_CMD_BUFFER_BEGIN_INFO,
Courtney Goeltzenleuchtere3b0f3a2015-04-03 15:25:24 -0600263 .pNext = NULL,
Tony Barbour8205d902015-04-16 15:59:00 -0600264 .flags = VK_CMD_BUFFER_OPTIMIZE_SMALL_BATCH_BIT |
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600265 VK_CMD_BUFFER_OPTIMIZE_ONE_TIME_SUBMIT_BIT,
Jon Ashburn53d27af2014-12-31 17:08:35 -0700266 };
Tony Barbour22a30862015-04-22 09:02:32 -0600267 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600268 VkAttachmentLoadOp load_op = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
269 VkAttachmentStoreOp store_op = VK_ATTACHMENT_STORE_OP_DONT_CARE;
270 const VkFramebufferCreateInfo fb_info = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600271 .sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO,
Jon Ashburn3325d6b2015-01-02 18:24:05 -0700272 .pNext = NULL,
273 .colorAttachmentCount = 1,
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600274 .pColorAttachments = (VkColorAttachmentBindInfo*) &color_attachment,
275 .pDepthStencilAttachment = (VkDepthStencilBindInfo*) &depth_stencil,
Jon Ashburn3325d6b2015-01-02 18:24:05 -0700276 .sampleCount = 1,
Mark Lobodzinski71fcc2d2015-01-27 13:24:03 -0600277 .width = demo->width,
278 .height = demo->height,
279 .layers = 1,
Jon Ashburn3325d6b2015-01-02 18:24:05 -0700280 };
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600281 VkRenderPassCreateInfo rp_info;
282 VkRenderPassBegin rp_begin;
Jon Ashburn3325d6b2015-01-02 18:24:05 -0700283
284 memset(&rp_info, 0 , sizeof(rp_info));
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600285 err = vkCreateFramebuffer(demo->device, &fb_info, &rp_begin.framebuffer);
Jon Ashburn3325d6b2015-01-02 18:24:05 -0700286 assert(!err);
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600287 rp_info.sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO;
Jon Ashburn3325d6b2015-01-02 18:24:05 -0700288 rp_info.renderArea.extent.width = demo->width;
289 rp_info.renderArea.extent.height = demo->height;
Piers Daniell886be472015-02-23 16:23:13 -0700290 rp_info.colorAttachmentCount = fb_info.colorAttachmentCount;
Courtney Goeltzenleuchtere3b0f3a2015-04-03 15:25:24 -0600291 rp_info.pColorFormats = &demo->format;
292 rp_info.pColorLayouts = &color_attachment.layout;
Jon Ashburn3325d6b2015-01-02 18:24:05 -0700293 rp_info.pColorLoadOps = &load_op;
294 rp_info.pColorStoreOps = &store_op;
Courtney Goeltzenleuchtere3b0f3a2015-04-03 15:25:24 -0600295 rp_info.pColorLoadClearValues = &clear_color;
Tony Barbour8205d902015-04-16 15:59:00 -0600296 rp_info.depthStencilFormat = VK_FORMAT_D16_UNORM;
Courtney Goeltzenleuchtere3b0f3a2015-04-03 15:25:24 -0600297 rp_info.depthStencilLayout = depth_stencil.layout;
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600298 rp_info.depthLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
Courtney Goeltzenleuchtere3b0f3a2015-04-03 15:25:24 -0600299 rp_info.depthLoadClearValue = clear_depth;
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600300 rp_info.depthStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE;
301 rp_info.stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
Courtney Goeltzenleuchtere3b0f3a2015-04-03 15:25:24 -0600302 rp_info.stencilLoadClearValue = 0;
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600303 rp_info.stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE;
304 err = vkCreateRenderPass(demo->device, &rp_info, &(rp_begin.renderPass));
Jon Ashburn3325d6b2015-01-02 18:24:05 -0700305 assert(!err);
Chia-I Wuc19795a2014-09-13 11:12:55 +0800306
Courtney Goeltzenleuchter633f9c52015-04-30 10:58:33 -0600307 err = vkBeginCommandBuffer(demo->draw_cmd, &cmd_buf_info);
Chia-I Wuc19795a2014-09-13 11:12:55 +0800308 assert(!err);
309
Courtney Goeltzenleuchter633f9c52015-04-30 10:58:33 -0600310 vkCmdBindPipeline(demo->draw_cmd, VK_PIPELINE_BIND_POINT_GRAPHICS,
Chia-I Wuc19795a2014-09-13 11:12:55 +0800311 demo->pipeline);
Courtney Goeltzenleuchter633f9c52015-04-30 10:58:33 -0600312 vkCmdBindDescriptorSets(demo->draw_cmd, VK_PIPELINE_BIND_POINT_GRAPHICS,
Cody Northrop1a01b1d2015-04-16 13:41:56 -0600313 0, 1, & demo->desc_set, 0, NULL);
Chia-I Wuc19795a2014-09-13 11:12:55 +0800314
Courtney Goeltzenleuchter633f9c52015-04-30 10:58:33 -0600315 vkCmdBindDynamicStateObject(demo->draw_cmd, VK_STATE_BIND_POINT_VIEWPORT, demo->viewport);
316 vkCmdBindDynamicStateObject(demo->draw_cmd, VK_STATE_BIND_POINT_RASTER, demo->raster);
317 vkCmdBindDynamicStateObject(demo->draw_cmd, VK_STATE_BIND_POINT_COLOR_BLEND,
Chia-I Wuc19795a2014-09-13 11:12:55 +0800318 demo->color_blend);
Courtney Goeltzenleuchter633f9c52015-04-30 10:58:33 -0600319 vkCmdBindDynamicStateObject(demo->draw_cmd, VK_STATE_BIND_POINT_DEPTH_STENCIL,
Chia-I Wuc19795a2014-09-13 11:12:55 +0800320 demo->depth_stencil);
321
Tony Barbour8205d902015-04-16 15:59:00 -0600322 VkDeviceSize offsets[1] = {0};
Courtney Goeltzenleuchter633f9c52015-04-30 10:58:33 -0600323 vkCmdBindVertexBuffers(demo->draw_cmd, VERTEX_BUFFER_BIND_ID, 1, &demo->vertices.buf, offsets);
Chia-I Wu3b04af52014-11-08 10:48:20 +0800324
Courtney Goeltzenleuchter633f9c52015-04-30 10:58:33 -0600325 vkCmdBeginRenderPass(demo->draw_cmd, &rp_begin);
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600326 clear_range.aspect = VK_IMAGE_ASPECT_COLOR;
Chia-I Wu68a7de42014-10-25 12:40:28 +0800327 clear_range.baseMipLevel = 0;
328 clear_range.mipLevels = 1;
329 clear_range.baseArraySlice = 0;
330 clear_range.arraySize = 1;
Courtney Goeltzenleuchter633f9c52015-04-30 10:58:33 -0600331 vkCmdClearColorImage(demo->draw_cmd,
Chia-I Wu68a7de42014-10-25 12:40:28 +0800332 demo->buffers[demo->current_buffer].image,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600333 VK_IMAGE_LAYOUT_CLEAR_OPTIMAL,
Courtney Goeltzenleuchterda4a99e2015-04-23 17:49:22 -0600334 &clear_color, 1, &clear_range);
Chia-I Wu68a7de42014-10-25 12:40:28 +0800335
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600336 clear_range.aspect = VK_IMAGE_ASPECT_DEPTH;
Courtney Goeltzenleuchter633f9c52015-04-30 10:58:33 -0600337 vkCmdClearDepthStencil(demo->draw_cmd,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600338 demo->depth.image, VK_IMAGE_LAYOUT_CLEAR_OPTIMAL,
Chia-I Wu68a7de42014-10-25 12:40:28 +0800339 clear_depth, 0, 1, &clear_range);
340
Courtney Goeltzenleuchter633f9c52015-04-30 10:58:33 -0600341 vkCmdDraw(demo->draw_cmd, 0, 3, 0, 1);
342 vkCmdEndRenderPass(demo->draw_cmd, rp_begin.renderPass);
Chia-I Wuc19795a2014-09-13 11:12:55 +0800343
Courtney Goeltzenleuchter633f9c52015-04-30 10:58:33 -0600344 err = vkEndCommandBuffer(demo->draw_cmd);
Chia-I Wuc19795a2014-09-13 11:12:55 +0800345 assert(!err);
Courtney Goeltzenleuchtere9afa992015-02-25 16:55:23 -0700346
Mike Stroyan230e6252015-04-17 12:36:38 -0600347 vkDestroyObject(demo->device, VK_OBJECT_TYPE_RENDER_PASS, rp_begin.renderPass);
348 vkDestroyObject(demo->device, VK_OBJECT_TYPE_FRAMEBUFFER, rp_begin.framebuffer);
Chia-I Wuc19795a2014-09-13 11:12:55 +0800349}
350
351static void demo_draw(struct demo *demo)
352{
Chia-I Wu5b66aa52015-04-16 22:02:10 +0800353 const VkPresentInfoWSI present = {
354 .sType = VK_STRUCTURE_TYPE_PRESENT_INFO_WSI,
355 .pNext = NULL,
356 .image = demo->buffers[demo->current_buffer].image,
357 .flipInterval = 0,
Chia-I Wuc19795a2014-09-13 11:12:55 +0800358 };
Tony Barbour22a30862015-04-22 09:02:32 -0600359 VkResult U_ASSERT_ONLY err;
Chia-I Wuc19795a2014-09-13 11:12:55 +0800360
361 demo_draw_build_cmd(demo);
362
Courtney Goeltzenleuchter633f9c52015-04-30 10:58:33 -0600363 err = vkQueueSubmit(demo->queue, 1, &demo->draw_cmd, VK_NULL_HANDLE);
Chia-I Wuc19795a2014-09-13 11:12:55 +0800364 assert(!err);
365
Chia-I Wu5b66aa52015-04-16 22:02:10 +0800366 err = vkQueuePresentWSI(demo->queue, &present);
Chia-I Wuc19795a2014-09-13 11:12:55 +0800367 assert(!err);
368
369 demo->current_buffer = (demo->current_buffer + 1) % DEMO_BUFFER_COUNT;
Chia-I Wu5b66aa52015-04-16 22:02:10 +0800370
371 err = vkQueueWaitIdle(demo->queue);
372 assert(err == VK_SUCCESS);
Chia-I Wuc19795a2014-09-13 11:12:55 +0800373}
374
375static void demo_prepare_buffers(struct demo *demo)
376{
Chia-I Wu5b66aa52015-04-16 22:02:10 +0800377 const VkSwapChainCreateInfoWSI swap_chain = {
378 .sType = VK_STRUCTURE_TYPE_SWAP_CHAIN_CREATE_INFO_WSI,
379 .pNext = NULL,
380 .pNativeWindowSystemHandle = demo->connection,
381 .pNativeWindowHandle = (void *) (intptr_t) demo->window,
Ian Elliott32536f92015-04-21 16:41:02 -0600382 .displayCount = 1,
Chia-I Wu5b66aa52015-04-16 22:02:10 +0800383 .imageCount = DEMO_BUFFER_COUNT,
384 .imageFormat = demo->format,
385 .imageExtent = {
Chia-I Wuc19795a2014-09-13 11:12:55 +0800386 .width = demo->width,
387 .height = demo->height,
388 },
Chia-I Wu5b66aa52015-04-16 22:02:10 +0800389 .imageArraySize = 1,
390 .imageUsageFlags = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT,
Chia-I Wuc19795a2014-09-13 11:12:55 +0800391 };
Chia-I Wu5b66aa52015-04-16 22:02:10 +0800392 VkSwapChainImageInfoWSI images[DEMO_BUFFER_COUNT];
393 size_t images_size = sizeof(images);
Tony Barbour22a30862015-04-22 09:02:32 -0600394 VkResult U_ASSERT_ONLY err;
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600395 uint32_t i;
Chia-I Wuc19795a2014-09-13 11:12:55 +0800396
Chia-I Wu5b66aa52015-04-16 22:02:10 +0800397 err = vkCreateSwapChainWSI(demo->device, &swap_chain, &demo->swap_chain);
398 assert(!err);
399
400 err = vkGetSwapChainInfoWSI(demo->swap_chain,
401 VK_SWAP_CHAIN_INFO_TYPE_PERSISTENT_IMAGES_WSI,
402 &images_size, images);
403 assert(!err && images_size == sizeof(images));
404
Chia-I Wuc19795a2014-09-13 11:12:55 +0800405 for (i = 0; i < DEMO_BUFFER_COUNT; i++) {
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600406 VkColorAttachmentViewCreateInfo color_attachment_view = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600407 .sType = VK_STRUCTURE_TYPE_COLOR_ATTACHMENT_VIEW_CREATE_INFO,
Chia-I Wuc19795a2014-09-13 11:12:55 +0800408 .pNext = NULL,
409 .format = demo->format,
410 .mipLevel = 0,
411 .baseArraySlice = 0,
412 .arraySize = 1,
413 };
414
Chia-I Wu5b66aa52015-04-16 22:02:10 +0800415 demo->buffers[i].image = images[i].image;
416 demo->buffers[i].mem = images[i].memory;
Mark Lobodzinski97dcd042015-04-16 08:52:00 -0500417
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600418 demo_set_image_layout(demo, demo->buffers[i].image,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600419 VK_IMAGE_LAYOUT_UNDEFINED,
420 VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL);
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600421
Chia-I Wuc19795a2014-09-13 11:12:55 +0800422 color_attachment_view.image = demo->buffers[i].image;
423
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600424 err = vkCreateColorAttachmentView(demo->device,
Chia-I Wuc19795a2014-09-13 11:12:55 +0800425 &color_attachment_view, &demo->buffers[i].view);
426 assert(!err);
427 }
Piers Daniell886be472015-02-23 16:23:13 -0700428
429 demo->current_buffer = 0;
Chia-I Wuc19795a2014-09-13 11:12:55 +0800430}
431
Chia-I Wu9ae87c92014-10-07 14:15:01 +0800432static void demo_prepare_depth(struct demo *demo)
433{
Tony Barbour8205d902015-04-16 15:59:00 -0600434 const VkFormat depth_format = VK_FORMAT_D16_UNORM;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600435 const VkImageCreateInfo image = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600436 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
Chia-I Wu9ae87c92014-10-07 14:15:01 +0800437 .pNext = NULL,
Tony Barbour8205d902015-04-16 15:59:00 -0600438 .imageType = VK_IMAGE_TYPE_2D,
Chia-I Wu9ae87c92014-10-07 14:15:01 +0800439 .format = depth_format,
440 .extent = { demo->width, demo->height, 1 },
441 .mipLevels = 1,
442 .arraySize = 1,
443 .samples = 1,
Tony Barbour8205d902015-04-16 15:59:00 -0600444 .tiling = VK_IMAGE_TILING_OPTIMAL,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600445 .usage = VK_IMAGE_USAGE_DEPTH_STENCIL_BIT,
Chia-I Wu9ae87c92014-10-07 14:15:01 +0800446 .flags = 0,
447 };
Courtney Goeltzenleuchterddcb6192015-04-14 18:48:46 -0600448 VkMemoryAllocInfo mem_alloc = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600449 .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOC_INFO,
Mark Lobodzinski97dcd042015-04-16 08:52:00 -0500450 .pNext = NULL,
Chia-I Wu9ae87c92014-10-07 14:15:01 +0800451 .allocationSize = 0,
Tony Barbour8205d902015-04-16 15:59:00 -0600452 .memProps = VK_MEMORY_PROPERTY_DEVICE_ONLY,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600453 .memPriority = VK_MEMORY_PRIORITY_NORMAL,
Chia-I Wu9ae87c92014-10-07 14:15:01 +0800454 };
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600455 VkDepthStencilViewCreateInfo view = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600456 .sType = VK_STRUCTURE_TYPE_DEPTH_STENCIL_VIEW_CREATE_INFO,
Chia-I Wu9ae87c92014-10-07 14:15:01 +0800457 .pNext = NULL,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600458 .image = VK_NULL_HANDLE,
Chia-I Wu9ae87c92014-10-07 14:15:01 +0800459 .mipLevel = 0,
460 .baseArraySlice = 0,
461 .arraySize = 1,
462 .flags = 0,
463 };
Jon Ashburna9ae3832015-01-16 09:37:43 -0700464
Mark Lobodzinski23182612015-05-29 09:32:35 -0500465 VkMemoryRequirements mem_reqs;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600466 size_t mem_reqs_size = sizeof(VkMemoryRequirements);
Tony Barbour22a30862015-04-22 09:02:32 -0600467 VkResult U_ASSERT_ONLY err;
Chia-I Wu9ae87c92014-10-07 14:15:01 +0800468
469 demo->depth.format = depth_format;
470
471 /* create image */
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600472 err = vkCreateImage(demo->device, &image,
Chia-I Wu9ae87c92014-10-07 14:15:01 +0800473 &demo->depth.image);
474 assert(!err);
475
Mike Stroyan230e6252015-04-17 12:36:38 -0600476 err = vkGetObjectInfo(demo->device,
477 VK_OBJECT_TYPE_IMAGE, demo->depth.image,
Tony Barbour8205d902015-04-16 15:59:00 -0600478 VK_OBJECT_INFO_TYPE_MEMORY_REQUIREMENTS,
Mark Lobodzinski23182612015-05-29 09:32:35 -0500479 &mem_reqs_size, &mem_reqs);
480 mem_alloc.allocationSize = mem_reqs.size;
Chia-I Wu9ae87c92014-10-07 14:15:01 +0800481
Mark Lobodzinski23182612015-05-29 09:32:35 -0500482 /* allocate memory */
483 err = vkAllocMemory(demo->device, &mem_alloc, &demo->depth.mem);
484 assert(!err);
Chia-I Wu9ae87c92014-10-07 14:15:01 +0800485
Mark Lobodzinski23182612015-05-29 09:32:35 -0500486 /* bind memory */
487 err = vkBindObjectMemory(demo->device,
488 VK_OBJECT_TYPE_IMAGE, demo->depth.image,
489 demo->depth.mem, 0);
490 assert(!err);
Chia-I Wu9ae87c92014-10-07 14:15:01 +0800491
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600492 demo_set_image_layout(demo, demo->depth.image,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600493 VK_IMAGE_LAYOUT_UNDEFINED,
494 VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL);
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600495
Chia-I Wu9ae87c92014-10-07 14:15:01 +0800496 /* create image view */
497 view.image = demo->depth.image;
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600498 err = vkCreateDepthStencilView(demo->device, &view,
Chia-I Wu9ae87c92014-10-07 14:15:01 +0800499 &demo->depth.view);
500 assert(!err);
Chia-I Wu9ae87c92014-10-07 14:15:01 +0800501}
502
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700503static void demo_prepare_texture_image(struct demo *demo,
504 const uint32_t *tex_colors,
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600505 struct texture_object *tex_obj,
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600506 VkImageTiling tiling,
Courtney Goeltzenleuchtercb67a322015-04-21 09:31:23 -0600507 VkImageUsageFlags usage,
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600508 VkFlags mem_props)
Chia-I Wub043fe32014-10-06 15:30:33 +0800509{
Tony Barbour8205d902015-04-16 15:59:00 -0600510 const VkFormat tex_format = VK_FORMAT_B8G8R8A8_UNORM;
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600511 const int32_t tex_width = 2;
512 const int32_t tex_height = 2;
Tony Barbour22a30862015-04-22 09:02:32 -0600513 VkResult U_ASSERT_ONLY err;
Chia-I Wub043fe32014-10-06 15:30:33 +0800514
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600515 tex_obj->tex_width = tex_width;
516 tex_obj->tex_height = tex_height;
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700517
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600518 const VkImageCreateInfo image_create_info = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600519 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
Courtney Goeltzenleuchter3226a6f2015-02-11 18:17:22 -0700520 .pNext = NULL,
Tony Barbour8205d902015-04-16 15:59:00 -0600521 .imageType = VK_IMAGE_TYPE_2D,
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700522 .format = tex_format,
523 .extent = { tex_width, tex_height, 1 },
524 .mipLevels = 1,
525 .arraySize = 1,
526 .samples = 1,
527 .tiling = tiling,
Courtney Goeltzenleuchtercb67a322015-04-21 09:31:23 -0600528 .usage = usage,
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700529 .flags = 0,
530 };
Courtney Goeltzenleuchterddcb6192015-04-14 18:48:46 -0600531 VkMemoryAllocInfo mem_alloc = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600532 .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOC_INFO,
Mark Lobodzinski97dcd042015-04-16 08:52:00 -0500533 .pNext = NULL,
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700534 .allocationSize = 0,
535 .memProps = mem_props,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600536 .memPriority = VK_MEMORY_PRIORITY_NORMAL,
Courtney Goeltzenleuchter3226a6f2015-02-11 18:17:22 -0700537 };
538
Mark Lobodzinski23182612015-05-29 09:32:35 -0500539 VkMemoryRequirements mem_reqs;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600540 size_t mem_reqs_size = sizeof(VkMemoryRequirements);
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700541
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600542 err = vkCreateImage(demo->device, &image_create_info,
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600543 &tex_obj->image);
Courtney Goeltzenleuchter3226a6f2015-02-11 18:17:22 -0700544 assert(!err);
545
Mike Stroyan230e6252015-04-17 12:36:38 -0600546 err = vkGetObjectInfo(demo->device,
547 VK_OBJECT_TYPE_IMAGE, tex_obj->image,
Tony Barbour8205d902015-04-16 15:59:00 -0600548 VK_OBJECT_INFO_TYPE_MEMORY_REQUIREMENTS,
Mark Lobodzinski23182612015-05-29 09:32:35 -0500549 &mem_reqs_size, &mem_reqs);
550 mem_alloc.allocationSize = mem_reqs.size;
Courtney Goeltzenleuchter3226a6f2015-02-11 18:17:22 -0700551
Mark Lobodzinski23182612015-05-29 09:32:35 -0500552 /* allocate memory */
553 err = vkAllocMemory(demo->device, &mem_alloc, &tex_obj->mem);
554 assert(!err);
Courtney Goeltzenleuchter3226a6f2015-02-11 18:17:22 -0700555
Mark Lobodzinski23182612015-05-29 09:32:35 -0500556 /* bind memory */
557 err = vkBindObjectMemory(demo->device,
558 VK_OBJECT_TYPE_IMAGE, tex_obj->image,
559 tex_obj->mem, 0);
560 assert(!err);
Courtney Goeltzenleuchter3226a6f2015-02-11 18:17:22 -0700561
Tony Barbour8205d902015-04-16 15:59:00 -0600562 if (mem_props & VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT) {
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600563 const VkImageSubresource subres = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600564 .aspect = VK_IMAGE_ASPECT_COLOR,
Courtney Goeltzenleuchter3226a6f2015-02-11 18:17:22 -0700565 .mipLevel = 0,
566 .arraySlice = 0,
567 };
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600568 VkSubresourceLayout layout;
569 size_t layout_size = sizeof(VkSubresourceLayout);
Courtney Goeltzenleuchter3226a6f2015-02-11 18:17:22 -0700570 void *data;
571 int32_t x, y;
572
Mike Stroyan230e6252015-04-17 12:36:38 -0600573 err = vkGetImageSubresourceInfo(demo->device, tex_obj->image, &subres,
Tony Barbour8205d902015-04-16 15:59:00 -0600574 VK_SUBRESOURCE_INFO_TYPE_LAYOUT,
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700575 &layout_size, &layout);
Courtney Goeltzenleuchter3226a6f2015-02-11 18:17:22 -0700576 assert(!err && layout_size == sizeof(layout));
Courtney Goeltzenleuchter3226a6f2015-02-11 18:17:22 -0700577
Mark Lobodzinski23182612015-05-29 09:32:35 -0500578 err = vkMapMemory(demo->device, tex_obj->mem, 0, 0, 0, &data);
Courtney Goeltzenleuchter3226a6f2015-02-11 18:17:22 -0700579 assert(!err);
580
581 for (y = 0; y < tex_height; y++) {
582 uint32_t *row = (uint32_t *) ((char *) data + layout.rowPitch * y);
583 for (x = 0; x < tex_width; x++)
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700584 row[x] = tex_colors[(x & 1) ^ (y & 1)];
Courtney Goeltzenleuchter3226a6f2015-02-11 18:17:22 -0700585 }
586
Mark Lobodzinski23182612015-05-29 09:32:35 -0500587 err = vkUnmapMemory(demo->device, tex_obj->mem);
Courtney Goeltzenleuchter3226a6f2015-02-11 18:17:22 -0700588 assert(!err);
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700589 }
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600590
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600591 tex_obj->imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600592 demo_set_image_layout(demo, tex_obj->image,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600593 VK_IMAGE_LAYOUT_UNDEFINED,
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600594 tex_obj->imageLayout);
595 /* 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 -0700596}
597
Mark Lobodzinskicf26e072015-04-16 11:44:05 -0500598static void demo_destroy_texture_image(struct demo *demo, struct texture_object *tex_obj)
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700599{
600 /* clean up staging resources */
Mark Lobodzinski23182612015-05-29 09:32:35 -0500601 vkBindObjectMemory(demo->device,
602 VK_OBJECT_TYPE_IMAGE, tex_obj->image, VK_NULL_HANDLE, 0);
603 vkFreeMemory(demo->device, tex_obj->mem);
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700604
Mike Stroyan230e6252015-04-17 12:36:38 -0600605 vkDestroyObject(demo->device, VK_OBJECT_TYPE_IMAGE, tex_obj->image);
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700606}
607
608static void demo_prepare_textures(struct demo *demo)
609{
Tony Barbour8205d902015-04-16 15:59:00 -0600610 const VkFormat tex_format = VK_FORMAT_B8G8R8A8_UNORM;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600611 VkFormatProperties props;
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700612 size_t size = sizeof(props);
613 const uint32_t tex_colors[DEMO_TEXTURE_COUNT][2] = {
614 { 0xffff0000, 0xff00ff00 },
615 };
Tony Barbour22a30862015-04-22 09:02:32 -0600616 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700617 uint32_t i;
618
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600619 err = vkGetFormatInfo(demo->device, tex_format,
Tony Barbour8205d902015-04-16 15:59:00 -0600620 VK_FORMAT_INFO_TYPE_PROPERTIES,
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700621 &size, &props);
622 assert(!err);
623
624 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Tony Barbour8205d902015-04-16 15:59:00 -0600625 if ((props.linearTilingFeatures & VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT) && !demo->use_staging_buffer) {
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700626 /* Device can texture using linear textures */
627 demo_prepare_texture_image(demo, tex_colors[i], &demo->textures[i],
Courtney Goeltzenleuchtercb67a322015-04-21 09:31:23 -0600628 VK_IMAGE_TILING_LINEAR, VK_IMAGE_USAGE_SAMPLED_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT);
Tony Barbour8205d902015-04-16 15:59:00 -0600629 } else if (props.optimalTilingFeatures & VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT){
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700630 /* Must use staging buffer to copy linear texture to optimized */
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600631 struct texture_object staging_texture;
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700632
633 memset(&staging_texture, 0, sizeof(staging_texture));
634 demo_prepare_texture_image(demo, tex_colors[i], &staging_texture,
Courtney Goeltzenleuchtercb67a322015-04-21 09:31:23 -0600635 VK_IMAGE_TILING_LINEAR, VK_IMAGE_USAGE_TRANSFER_SOURCE_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT);
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700636
637 demo_prepare_texture_image(demo, tex_colors[i], &demo->textures[i],
Courtney Goeltzenleuchtercb67a322015-04-21 09:31:23 -0600638 VK_IMAGE_TILING_OPTIMAL,
639 (VK_IMAGE_USAGE_TRANSFER_DESTINATION_BIT | VK_IMAGE_USAGE_SAMPLED_BIT),
640 VK_MEMORY_PROPERTY_DEVICE_ONLY);
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700641
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600642 demo_set_image_layout(demo, staging_texture.image,
643 staging_texture.imageLayout,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600644 VK_IMAGE_LAYOUT_TRANSFER_SOURCE_OPTIMAL);
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700645
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600646 demo_set_image_layout(demo, demo->textures[i].image,
647 demo->textures[i].imageLayout,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600648 VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL);
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700649
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600650 VkImageCopy copy_region = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600651 .srcSubresource = { VK_IMAGE_ASPECT_COLOR, 0, 0 },
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700652 .srcOffset = { 0, 0, 0 },
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600653 .destSubresource = { VK_IMAGE_ASPECT_COLOR, 0, 0 },
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700654 .destOffset = { 0, 0, 0 },
655 .extent = { staging_texture.tex_width, staging_texture.tex_height, 1 },
656 };
Courtney Goeltzenleuchter633f9c52015-04-30 10:58:33 -0600657 vkCmdCopyImage(demo->setup_cmd,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600658 staging_texture.image, VK_IMAGE_LAYOUT_TRANSFER_SOURCE_OPTIMAL,
659 demo->textures[i].image, VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL,
Courtney Goeltzenleuchter51cbf302015-03-25 11:25:10 -0600660 1, &copy_region);
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700661
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600662 demo_set_image_layout(demo, demo->textures[i].image,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600663 VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL,
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600664 demo->textures[i].imageLayout);
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700665
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600666 demo_flush_init_cmd(demo);
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700667
Courtney Goeltzenleuchter876629f2015-04-21 09:30:03 -0600668 demo_destroy_texture_image(demo, &staging_texture);
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700669 } else {
Tony Barbour8205d902015-04-16 15:59:00 -0600670 /* Can't support VK_FORMAT_B8G8R8A8_UNORM !? */
Piers Daniell886be472015-02-23 16:23:13 -0700671 assert(!"No support for B8G8R8A8_UNORM as texture image format");
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700672 }
Courtney Goeltzenleuchter3226a6f2015-02-11 18:17:22 -0700673
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600674 const VkSamplerCreateInfo sampler = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600675 .sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO,
Chia-I Wub043fe32014-10-06 15:30:33 +0800676 .pNext = NULL,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600677 .magFilter = VK_TEX_FILTER_NEAREST,
678 .minFilter = VK_TEX_FILTER_NEAREST,
Tony Barbour8205d902015-04-16 15:59:00 -0600679 .mipMode = VK_TEX_MIPMAP_MODE_BASE,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600680 .addressU = VK_TEX_ADDRESS_WRAP,
681 .addressV = VK_TEX_ADDRESS_WRAP,
682 .addressW = VK_TEX_ADDRESS_WRAP,
Chia-I Wub043fe32014-10-06 15:30:33 +0800683 .mipLodBias = 0.0f,
Courtney Goeltzenleuchterbc9c8162015-02-13 18:20:24 -0700684 .maxAnisotropy = 1,
Tony Barbour8205d902015-04-16 15:59:00 -0600685 .compareOp = VK_COMPARE_OP_NEVER,
Chia-I Wub043fe32014-10-06 15:30:33 +0800686 .minLod = 0.0f,
687 .maxLod = 0.0f,
Tony Barbour8205d902015-04-16 15:59:00 -0600688 .borderColor = VK_BORDER_COLOR_OPAQUE_WHITE,
Chia-I Wub043fe32014-10-06 15:30:33 +0800689 };
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600690 VkImageViewCreateInfo view = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600691 .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
Chia-I Wub043fe32014-10-06 15:30:33 +0800692 .pNext = NULL,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600693 .image = VK_NULL_HANDLE,
Tony Barbour8205d902015-04-16 15:59:00 -0600694 .viewType = VK_IMAGE_VIEW_TYPE_2D,
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700695 .format = tex_format,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600696 .channels = { VK_CHANNEL_SWIZZLE_R,
697 VK_CHANNEL_SWIZZLE_G,
698 VK_CHANNEL_SWIZZLE_B,
699 VK_CHANNEL_SWIZZLE_A, },
700 .subresourceRange = { VK_IMAGE_ASPECT_COLOR, 0, 1, 0, 1 },
Chia-I Wub043fe32014-10-06 15:30:33 +0800701 .minLod = 0.0f,
702 };
Jon Ashburna9ae3832015-01-16 09:37:43 -0700703
Chia-I Wub043fe32014-10-06 15:30:33 +0800704 /* create sampler */
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600705 err = vkCreateSampler(demo->device, &sampler,
Chia-I Wub043fe32014-10-06 15:30:33 +0800706 &demo->textures[i].sampler);
707 assert(!err);
708
Chia-I Wub043fe32014-10-06 15:30:33 +0800709 /* create image view */
710 view.image = demo->textures[i].image;
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600711 err = vkCreateImageView(demo->device, &view,
Courtney Goeltzenleuchter3226a6f2015-02-11 18:17:22 -0700712 &demo->textures[i].view);
Chia-I Wub043fe32014-10-06 15:30:33 +0800713 assert(!err);
Chia-I Wub043fe32014-10-06 15:30:33 +0800714 }
715}
716
Chia-I Wu99621bc2014-10-08 11:52:22 +0800717static void demo_prepare_vertices(struct demo *demo)
718{
719 const float vb[3][5] = {
720 /* position texcoord */
Chia-I Wue2504cb2015-04-22 14:20:52 +0800721 { -1.0f, -1.0f, 0.2f, 0.0f, 0.0f },
722 { 1.0f, -1.0f, 0.25f, 1.0f, 0.0f },
Chia-I Wu99621bc2014-10-08 11:52:22 +0800723 { 0.0f, 1.0f, 1.0f, 0.5f, 1.0f },
724 };
Courtney Goeltzenleuchterddcb6192015-04-14 18:48:46 -0600725 const VkBufferCreateInfo buf_info = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600726 .sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO,
Chia-I Wu714df452015-01-01 07:55:04 +0800727 .pNext = NULL,
728 .size = sizeof(vb),
Courtney Goeltzenleuchterad870812015-04-15 15:29:59 -0600729 .usage = VK_BUFFER_USAGE_VERTEX_BUFFER_BIT,
Chia-I Wu714df452015-01-01 07:55:04 +0800730 .flags = 0,
731 };
Courtney Goeltzenleuchterddcb6192015-04-14 18:48:46 -0600732 VkMemoryAllocInfo mem_alloc = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600733 .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOC_INFO,
Mark Lobodzinski97dcd042015-04-16 08:52:00 -0500734 .pNext = NULL,
Chia-I Wu714df452015-01-01 07:55:04 +0800735 .allocationSize = 0,
Tony Barbour8205d902015-04-16 15:59:00 -0600736 .memProps = VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600737 .memPriority = VK_MEMORY_PRIORITY_NORMAL,
Chia-I Wu99621bc2014-10-08 11:52:22 +0800738 };
Mark Lobodzinski23182612015-05-29 09:32:35 -0500739 VkMemoryRequirements mem_reqs;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600740 size_t mem_reqs_size = sizeof(VkMemoryRequirements);
Tony Barbour22a30862015-04-22 09:02:32 -0600741 VkResult U_ASSERT_ONLY err;
Chia-I Wu99621bc2014-10-08 11:52:22 +0800742 void *data;
743
744 memset(&demo->vertices, 0, sizeof(demo->vertices));
745
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600746 err = vkCreateBuffer(demo->device, &buf_info, &demo->vertices.buf);
Chia-I Wu714df452015-01-01 07:55:04 +0800747 assert(!err);
748
Mike Stroyan230e6252015-04-17 12:36:38 -0600749 err = vkGetObjectInfo(demo->device,
Mike Stroyan230e6252015-04-17 12:36:38 -0600750 VK_OBJECT_TYPE_BUFFER, demo->vertices.buf,
Tony Barbour8205d902015-04-16 15:59:00 -0600751 VK_OBJECT_INFO_TYPE_MEMORY_REQUIREMENTS,
Mark Lobodzinski23182612015-05-29 09:32:35 -0500752 &mem_reqs_size, &mem_reqs);
Chia-I Wu714df452015-01-01 07:55:04 +0800753
Mark Lobodzinski23182612015-05-29 09:32:35 -0500754 mem_alloc.allocationSize = mem_reqs.size;
Chia-I Wu99621bc2014-10-08 11:52:22 +0800755
Mark Lobodzinski23182612015-05-29 09:32:35 -0500756 err = vkAllocMemory(demo->device, &mem_alloc, &demo->vertices.mem);
757 assert(!err);
Chia-I Wu99621bc2014-10-08 11:52:22 +0800758
Mark Lobodzinski23182612015-05-29 09:32:35 -0500759 err = vkMapMemory(demo->device, demo->vertices.mem, 0, 0, 0, &data);
760 assert(!err);
Chia-I Wu99621bc2014-10-08 11:52:22 +0800761
Mark Lobodzinski23182612015-05-29 09:32:35 -0500762 memcpy(data, vb, sizeof(vb));
Chia-I Wu99621bc2014-10-08 11:52:22 +0800763
Mark Lobodzinski23182612015-05-29 09:32:35 -0500764 err = vkUnmapMemory(demo->device, demo->vertices.mem);
765 assert(!err);
766
767 err = vkBindObjectMemory(demo->device,
768 VK_OBJECT_TYPE_BUFFER, demo->vertices.buf,
769 demo->vertices.mem, 0);
770 assert(!err);
Chia-I Wu714df452015-01-01 07:55:04 +0800771
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600772 demo->vertices.vi.sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_CREATE_INFO;
Chia-I Wu8d29d022014-10-08 12:14:39 +0800773 demo->vertices.vi.pNext = NULL;
774 demo->vertices.vi.bindingCount = 1;
775 demo->vertices.vi.pVertexBindingDescriptions = demo->vertices.vi_bindings;
776 demo->vertices.vi.attributeCount = 2;
777 demo->vertices.vi.pVertexAttributeDescriptions = demo->vertices.vi_attrs;
778
Courtney Goeltzenleuchterf5cdad02015-03-31 16:36:30 -0600779 demo->vertices.vi_bindings[0].binding = VERTEX_BUFFER_BIND_ID;
Chia-I Wu8d29d022014-10-08 12:14:39 +0800780 demo->vertices.vi_bindings[0].strideInBytes = sizeof(vb[0]);
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600781 demo->vertices.vi_bindings[0].stepRate = VK_VERTEX_INPUT_STEP_RATE_VERTEX;
Chia-I Wu8d29d022014-10-08 12:14:39 +0800782
Courtney Goeltzenleuchterf5cdad02015-03-31 16:36:30 -0600783 demo->vertices.vi_attrs[0].binding = VERTEX_BUFFER_BIND_ID;
784 demo->vertices.vi_attrs[0].location = 0;
Tony Barbour8205d902015-04-16 15:59:00 -0600785 demo->vertices.vi_attrs[0].format = VK_FORMAT_R32G32B32_SFLOAT;
Chia-I Wu8d29d022014-10-08 12:14:39 +0800786 demo->vertices.vi_attrs[0].offsetInBytes = 0;
787
Courtney Goeltzenleuchterf5cdad02015-03-31 16:36:30 -0600788 demo->vertices.vi_attrs[1].binding = VERTEX_BUFFER_BIND_ID;
789 demo->vertices.vi_attrs[1].location = 1;
Tony Barbour8205d902015-04-16 15:59:00 -0600790 demo->vertices.vi_attrs[1].format = VK_FORMAT_R32G32_SFLOAT;
Chia-I Wu8d29d022014-10-08 12:14:39 +0800791 demo->vertices.vi_attrs[1].offsetInBytes = sizeof(float) * 3;
Chia-I Wu99621bc2014-10-08 11:52:22 +0800792}
793
Chia-I Wuf8385062015-01-04 16:27:24 +0800794static void demo_prepare_descriptor_layout(struct demo *demo)
Chia-I Wub043fe32014-10-06 15:30:33 +0800795{
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600796 const VkDescriptorSetLayoutBinding layout_binding = {
Courtney Goeltzenleuchterad870812015-04-15 15:29:59 -0600797 .descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
Chia-I Wud3114a22015-05-25 16:22:52 +0800798 .arraySize = DEMO_TEXTURE_COUNT,
Tony Barbour8205d902015-04-16 15:59:00 -0600799 .stageFlags = VK_SHADER_STAGE_FRAGMENT_BIT,
Chia-I Wu310eece2015-03-27 12:56:09 +0800800 .pImmutableSamplers = NULL,
Chia-I Wub043fe32014-10-06 15:30:33 +0800801 };
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600802 const VkDescriptorSetLayoutCreateInfo descriptor_layout = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600803 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO,
Chia-I Wufc9d9132015-03-26 15:04:41 +0800804 .pNext = NULL,
805 .count = 1,
806 .pBinding = &layout_binding,
807 };
Tony Barbour22a30862015-04-22 09:02:32 -0600808 VkResult U_ASSERT_ONLY err;
Chia-I Wub043fe32014-10-06 15:30:33 +0800809
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600810 err = vkCreateDescriptorSetLayout(demo->device,
Chia-I Wu7732cb22015-03-26 15:27:55 +0800811 &descriptor_layout, &demo->desc_layout);
812 assert(!err);
813
Mark Lobodzinski556f7212015-04-17 14:11:39 -0500814 const VkPipelineLayoutCreateInfo pPipelineLayoutCreateInfo = {
815 .sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO,
816 .pNext = NULL,
817 .descriptorSetCount = 1,
818 .pSetLayouts = &demo->desc_layout,
819 };
820
821 err = vkCreatePipelineLayout(demo->device,
822 &pPipelineLayoutCreateInfo,
823 &demo->pipeline_layout);
Chia-I Wub043fe32014-10-06 15:30:33 +0800824 assert(!err);
Chia-I Wub043fe32014-10-06 15:30:33 +0800825}
826
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600827static VkShader demo_prepare_shader(struct demo *demo,
Tony Barbour8205d902015-04-16 15:59:00 -0600828 VkShaderStage stage,
Chia-I Wuc19795a2014-09-13 11:12:55 +0800829 const void *code,
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600830 size_t size)
Chia-I Wuc19795a2014-09-13 11:12:55 +0800831{
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600832 VkShaderCreateInfo createInfo;
833 VkShader shader;
834 VkResult err;
Chia-I Wuc19795a2014-09-13 11:12:55 +0800835
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600836 createInfo.sType = VK_STRUCTURE_TYPE_SHADER_CREATE_INFO;
Courtney Goeltzenleuchter3f2606d2014-10-13 17:51:58 -0600837 createInfo.pNext = NULL;
838
Cody Northropacfb0492015-03-17 15:55:58 -0600839 // Create fake SPV structure to feed GLSL
Courtney Goeltzenleuchter3f2606d2014-10-13 17:51:58 -0600840 // to the driver "under the covers"
841 createInfo.codeSize = 3 * sizeof(uint32_t) + size + 1;
842 createInfo.pCode = malloc(createInfo.codeSize);
843 createInfo.flags = 0;
844
Tony Barbour8205d902015-04-16 15:59:00 -0600845 /* try version 0 first: VkShaderStage followed by GLSL */
Cody Northropacfb0492015-03-17 15:55:58 -0600846 ((uint32_t *) createInfo.pCode)[0] = ICD_SPV_MAGIC;
Courtney Goeltzenleuchter3f2606d2014-10-13 17:51:58 -0600847 ((uint32_t *) createInfo.pCode)[1] = 0;
848 ((uint32_t *) createInfo.pCode)[2] = stage;
849 memcpy(((uint32_t *) createInfo.pCode + 3), code, size + 1);
850
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600851 err = vkCreateShader(demo->device, &createInfo, &shader);
Courtney Goeltzenleuchter3f2606d2014-10-13 17:51:58 -0600852 if (err) {
853 free((void *) createInfo.pCode);
Mike Stroyan230e6252015-04-17 12:36:38 -0600854 return VK_NULL_HANDLE;
Courtney Goeltzenleuchter3f2606d2014-10-13 17:51:58 -0600855 }
Chia-I Wuc19795a2014-09-13 11:12:55 +0800856
857 return shader;
858}
859
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600860static VkShader demo_prepare_vs(struct demo *demo)
Chia-I Wuc19795a2014-09-13 11:12:55 +0800861{
Courtney Goeltzenleuchter3f2606d2014-10-13 17:51:58 -0600862 static const char *vertShaderText =
Mark Lobodzinskiba4d2f02015-04-06 15:24:40 -0500863 "#version 140\n"
Courtney Goeltzenleuchterf5cdad02015-03-31 16:36:30 -0600864 "#extension GL_ARB_separate_shader_objects : enable\n"
865 "#extension GL_ARB_shading_language_420pack : enable\n"
866 "layout (location = 0) in vec4 pos;\n"
867 "layout (location = 1) in vec2 attr;\n"
Chia-I Wuf5caeb02014-10-25 12:11:27 +0800868 "out vec2 texcoord;\n"
Courtney Goeltzenleuchter3f2606d2014-10-13 17:51:58 -0600869 "void main() {\n"
Chia-I Wuf5caeb02014-10-25 12:11:27 +0800870 " texcoord = attr;\n"
871 " gl_Position = pos;\n"
Courtney Goeltzenleuchter3f2606d2014-10-13 17:51:58 -0600872 "}\n";
Courtney Goeltzenleuchteref7301b2014-09-17 13:17:12 -0600873
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600874 return demo_prepare_shader(demo, VK_SHADER_STAGE_VERTEX,
Courtney Goeltzenleuchter3f2606d2014-10-13 17:51:58 -0600875 (const void *) vertShaderText,
876 strlen(vertShaderText));
Chia-I Wuc19795a2014-09-13 11:12:55 +0800877}
878
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600879static VkShader demo_prepare_fs(struct demo *demo)
Chia-I Wuc19795a2014-09-13 11:12:55 +0800880{
Courtney Goeltzenleuchter3f2606d2014-10-13 17:51:58 -0600881 static const char *fragShaderText =
Mark Lobodzinskiba4d2f02015-04-06 15:24:40 -0500882 "#version 140\n"
Courtney Goeltzenleuchterf5cdad02015-03-31 16:36:30 -0600883 "#extension GL_ARB_separate_shader_objects : enable\n"
884 "#extension GL_ARB_shading_language_420pack : enable\n"
885 "layout (binding = 0) uniform sampler2D tex;\n"
886 "layout (location = 0) in vec2 texcoord;\n"
Courtney Goeltzenleuchter3f2606d2014-10-13 17:51:58 -0600887 "void main() {\n"
Chia-I Wuf5caeb02014-10-25 12:11:27 +0800888 " gl_FragColor = texture(tex, texcoord);\n"
Courtney Goeltzenleuchter3f2606d2014-10-13 17:51:58 -0600889 "}\n";
Courtney Goeltzenleuchteref7301b2014-09-17 13:17:12 -0600890
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600891 return demo_prepare_shader(demo, VK_SHADER_STAGE_FRAGMENT,
Courtney Goeltzenleuchter3f2606d2014-10-13 17:51:58 -0600892 (const void *) fragShaderText,
893 strlen(fragShaderText));
Chia-I Wuc19795a2014-09-13 11:12:55 +0800894}
895
896static void demo_prepare_pipeline(struct demo *demo)
897{
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600898 VkGraphicsPipelineCreateInfo pipeline;
899 VkPipelineVertexInputCreateInfo vi;
900 VkPipelineIaStateCreateInfo ia;
901 VkPipelineRsStateCreateInfo rs;
902 VkPipelineCbStateCreateInfo cb;
903 VkPipelineDsStateCreateInfo ds;
904 VkPipelineShaderStageCreateInfo vs;
905 VkPipelineShaderStageCreateInfo fs;
906 VkPipelineVpStateCreateInfo vp;
907 VkPipelineMsStateCreateInfo ms;
Tony Barbour22a30862015-04-22 09:02:32 -0600908 VkResult U_ASSERT_ONLY err;
Chia-I Wuc19795a2014-09-13 11:12:55 +0800909
910 memset(&pipeline, 0, sizeof(pipeline));
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600911 pipeline.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
Mark Lobodzinski556f7212015-04-17 14:11:39 -0500912 pipeline.layout = demo->pipeline_layout;
Chia-I Wuc19795a2014-09-13 11:12:55 +0800913
Chia-I Wu8d29d022014-10-08 12:14:39 +0800914 vi = demo->vertices.vi;
915
Chia-I Wuc19795a2014-09-13 11:12:55 +0800916 memset(&ia, 0, sizeof(ia));
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600917 ia.sType = VK_STRUCTURE_TYPE_PIPELINE_IA_STATE_CREATE_INFO;
Tony Barbour8205d902015-04-16 15:59:00 -0600918 ia.topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
Chia-I Wuc19795a2014-09-13 11:12:55 +0800919
920 memset(&rs, 0, sizeof(rs));
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600921 rs.sType = VK_STRUCTURE_TYPE_PIPELINE_RS_STATE_CREATE_INFO;
Tony Barbour8205d902015-04-16 15:59:00 -0600922 rs.fillMode = VK_FILL_MODE_SOLID;
Chia-I Wuc414ba82015-04-22 15:44:24 +0800923 rs.cullMode = VK_CULL_MODE_BACK;
924 rs.frontFace = VK_FRONT_FACE_CW;
Chia-I Wue2504cb2015-04-22 14:20:52 +0800925 rs.depthClipEnable = VK_TRUE;
Chia-I Wuc19795a2014-09-13 11:12:55 +0800926
927 memset(&cb, 0, sizeof(cb));
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600928 cb.sType = VK_STRUCTURE_TYPE_PIPELINE_CB_STATE_CREATE_INFO;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600929 VkPipelineCbAttachmentState att_state[1];
Tony Barbourfa6cac72015-01-16 14:27:35 -0700930 memset(att_state, 0, sizeof(att_state));
931 att_state[0].format = demo->format;
932 att_state[0].channelWriteMask = 0xf;
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600933 att_state[0].blendEnable = VK_FALSE;
Tony Barbourfa6cac72015-01-16 14:27:35 -0700934 cb.attachmentCount = 1;
935 cb.pAttachments = att_state;
Chia-I Wuc19795a2014-09-13 11:12:55 +0800936
Tony Barbourfa6cac72015-01-16 14:27:35 -0700937
938 memset(&vp, 0, sizeof(vp));
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600939 vp.sType = VK_STRUCTURE_TYPE_PIPELINE_VP_STATE_CREATE_INFO;
Tony Barbour8205d902015-04-16 15:59:00 -0600940 vp.viewportCount = 1;
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600941 vp.clipOrigin = VK_COORDINATE_ORIGIN_UPPER_LEFT;
Tony Barbourfa6cac72015-01-16 14:27:35 -0700942
943 memset(&ds, 0, sizeof(ds));
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600944 ds.sType = VK_STRUCTURE_TYPE_PIPELINE_DS_STATE_CREATE_INFO;
Tony Barbourfa6cac72015-01-16 14:27:35 -0700945 ds.format = demo->depth.format;
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600946 ds.depthTestEnable = VK_TRUE;
947 ds.depthWriteEnable = VK_TRUE;
Tony Barbour8205d902015-04-16 15:59:00 -0600948 ds.depthCompareOp = VK_COMPARE_OP_LESS_EQUAL;
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600949 ds.depthBoundsEnable = VK_FALSE;
950 ds.back.stencilFailOp = VK_STENCIL_OP_KEEP;
951 ds.back.stencilPassOp = VK_STENCIL_OP_KEEP;
Tony Barbour8205d902015-04-16 15:59:00 -0600952 ds.back.stencilCompareOp = VK_COMPARE_OP_ALWAYS;
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600953 ds.stencilTestEnable = VK_FALSE;
Tony Barbourfa6cac72015-01-16 14:27:35 -0700954 ds.front = ds.back;
Chia-I Wuc19795a2014-09-13 11:12:55 +0800955
Chia-I Wu99621bc2014-10-08 11:52:22 +0800956 memset(&vs, 0, sizeof(vs));
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600957 vs.sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
958 vs.shader.stage = VK_SHADER_STAGE_VERTEX;
Chia-I Wu99621bc2014-10-08 11:52:22 +0800959 vs.shader.shader = demo_prepare_vs(demo);
Tony Barbourfa6cac72015-01-16 14:27:35 -0700960 vs.shader.linkConstBufferCount = 0;
Chia-I Wu99621bc2014-10-08 11:52:22 +0800961
962 memset(&fs, 0, sizeof(fs));
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600963 fs.sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
964 fs.shader.stage = VK_SHADER_STAGE_FRAGMENT;
Chia-I Wu99621bc2014-10-08 11:52:22 +0800965 fs.shader.shader = demo_prepare_fs(demo);
Tony Barbourfa6cac72015-01-16 14:27:35 -0700966
967 memset(&ms, 0, sizeof(ms));
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600968 ms.sType = VK_STRUCTURE_TYPE_PIPELINE_MS_STATE_CREATE_INFO;
Tony Barbourfa6cac72015-01-16 14:27:35 -0700969 ms.sampleMask = 1;
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600970 ms.multisampleEnable = VK_FALSE;
Tony Barbourfa6cac72015-01-16 14:27:35 -0700971 ms.samples = 1;
Chia-I Wub043fe32014-10-06 15:30:33 +0800972
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600973 pipeline.pNext = (const void *) &vi;
974 vi.pNext = (void *) &ia;
975 ia.pNext = (const void *) &rs;
976 rs.pNext = (const void *) &cb;
977 cb.pNext = (const void *) &ms;
978 ms.pNext = (const void *) &vp;
979 vp.pNext = (const void *) &ds;
980 ds.pNext = (const void *) &vs;
981 vs.pNext = (const void *) &fs;
Chia-I Wuc19795a2014-09-13 11:12:55 +0800982
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600983 err = vkCreateGraphicsPipeline(demo->device, &pipeline, &demo->pipeline);
Chia-I Wuc19795a2014-09-13 11:12:55 +0800984 assert(!err);
985
Mike Stroyan230e6252015-04-17 12:36:38 -0600986 vkDestroyObject(demo->device, VK_OBJECT_TYPE_SHADER, vs.shader.shader);
987 vkDestroyObject(demo->device, VK_OBJECT_TYPE_SHADER, fs.shader.shader);
Chia-I Wuc19795a2014-09-13 11:12:55 +0800988}
989
990static void demo_prepare_dynamic_states(struct demo *demo)
991{
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600992 VkDynamicVpStateCreateInfo viewport_create;
993 VkDynamicRsStateCreateInfo raster;
994 VkDynamicCbStateCreateInfo color_blend;
995 VkDynamicDsStateCreateInfo depth_stencil;
Tony Barbour22a30862015-04-22 09:02:32 -0600996 VkResult U_ASSERT_ONLY err;
Chia-I Wuc19795a2014-09-13 11:12:55 +0800997
Tony Barbourfa6cac72015-01-16 14:27:35 -0700998 memset(&viewport_create, 0, sizeof(viewport_create));
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600999 viewport_create.sType = VK_STRUCTURE_TYPE_DYNAMIC_VP_STATE_CREATE_INFO;
Courtney Goeltzenleuchterc6e32f92015-02-11 14:13:34 -07001000 viewport_create.viewportAndScissorCount = 1;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001001 VkViewport viewport;
Chia-I Wuc19795a2014-09-13 11:12:55 +08001002 memset(&viewport, 0, sizeof(viewport));
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -06001003 viewport.height = (float) demo->height;
Courtney Goeltzenleuchterc6e32f92015-02-11 14:13:34 -07001004 viewport.width = (float) demo->width;
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -06001005 viewport.minDepth = (float) 0.0f;
1006 viewport.maxDepth = (float) 1.0f;
Piers Daniell886be472015-02-23 16:23:13 -07001007 viewport_create.pViewports = &viewport;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001008 VkRect scissor;
Piers Daniell886be472015-02-23 16:23:13 -07001009 memset(&scissor, 0, sizeof(scissor));
Courtney Goeltzenleuchterc6e32f92015-02-11 14:13:34 -07001010 scissor.extent.width = demo->width;
1011 scissor.extent.height = demo->height;
1012 scissor.offset.x = 0;
1013 scissor.offset.y = 0;
Courtney Goeltzenleuchterc6e32f92015-02-11 14:13:34 -07001014 viewport_create.pScissors = &scissor;
Chia-I Wuc19795a2014-09-13 11:12:55 +08001015
1016 memset(&raster, 0, sizeof(raster));
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001017 raster.sType = VK_STRUCTURE_TYPE_DYNAMIC_RS_STATE_CREATE_INFO;
Piers Daniell886be472015-02-23 16:23:13 -07001018 raster.pointSize = 1.0;
1019 raster.lineWidth = 1.0;
Chia-I Wuc19795a2014-09-13 11:12:55 +08001020
1021 memset(&color_blend, 0, sizeof(color_blend));
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001022 color_blend.sType = VK_STRUCTURE_TYPE_DYNAMIC_CB_STATE_CREATE_INFO;
Piers Daniell886be472015-02-23 16:23:13 -07001023 color_blend.blendConst[0] = 1.0f;
1024 color_blend.blendConst[1] = 1.0f;
1025 color_blend.blendConst[2] = 1.0f;
1026 color_blend.blendConst[3] = 1.0f;
Chia-I Wuc19795a2014-09-13 11:12:55 +08001027
1028 memset(&depth_stencil, 0, sizeof(depth_stencil));
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001029 depth_stencil.sType = VK_STRUCTURE_TYPE_DYNAMIC_DS_STATE_CREATE_INFO;
Piers Daniell886be472015-02-23 16:23:13 -07001030 depth_stencil.minDepth = 0.0f;
1031 depth_stencil.maxDepth = 1.0f;
Tony Barbourfa6cac72015-01-16 14:27:35 -07001032 depth_stencil.stencilBackRef = 0;
1033 depth_stencil.stencilFrontRef = 0;
1034 depth_stencil.stencilReadMask = 0xff;
1035 depth_stencil.stencilWriteMask = 0xff;
Chia-I Wuc19795a2014-09-13 11:12:55 +08001036
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001037 err = vkCreateDynamicViewportState(demo->device, &viewport_create, &demo->viewport);
Chia-I Wuc19795a2014-09-13 11:12:55 +08001038 assert(!err);
1039
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001040 err = vkCreateDynamicRasterState(demo->device, &raster, &demo->raster);
Chia-I Wuc19795a2014-09-13 11:12:55 +08001041 assert(!err);
1042
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001043 err = vkCreateDynamicColorBlendState(demo->device,
Chia-I Wuc19795a2014-09-13 11:12:55 +08001044 &color_blend, &demo->color_blend);
1045 assert(!err);
1046
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001047 err = vkCreateDynamicDepthStencilState(demo->device,
Chia-I Wuc19795a2014-09-13 11:12:55 +08001048 &depth_stencil, &demo->depth_stencil);
1049 assert(!err);
1050}
1051
Chia-I Wu8d24b3b2015-03-26 13:14:16 +08001052static void demo_prepare_descriptor_pool(struct demo *demo)
Chia-I Wuf8385062015-01-04 16:27:24 +08001053{
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001054 const VkDescriptorTypeCount type_count = {
Courtney Goeltzenleuchterad870812015-04-15 15:29:59 -06001055 .type = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
Chia-I Wuf8385062015-01-04 16:27:24 +08001056 .count = DEMO_TEXTURE_COUNT,
1057 };
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001058 const VkDescriptorPoolCreateInfo descriptor_pool = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001059 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO,
Chia-I Wuf8385062015-01-04 16:27:24 +08001060 .pNext = NULL,
1061 .count = 1,
1062 .pTypeCount = &type_count,
1063 };
Tony Barbour22a30862015-04-22 09:02:32 -06001064 VkResult U_ASSERT_ONLY err;
Chia-I Wuf8385062015-01-04 16:27:24 +08001065
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001066 err = vkCreateDescriptorPool(demo->device,
1067 VK_DESCRIPTOR_POOL_USAGE_ONE_SHOT, 1,
Chia-I Wu8d24b3b2015-03-26 13:14:16 +08001068 &descriptor_pool, &demo->desc_pool);
Chia-I Wuf8385062015-01-04 16:27:24 +08001069 assert(!err);
1070}
1071
1072static void demo_prepare_descriptor_set(struct demo *demo)
1073{
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001074 VkImageViewAttachInfo view_info[DEMO_TEXTURE_COUNT];
1075 VkSamplerImageViewInfo combined_info[DEMO_TEXTURE_COUNT];
1076 VkUpdateSamplerTextures update;
Chia-I Wu7732cb22015-03-26 15:27:55 +08001077 const void *update_array[1] = { &update };
Tony Barbour22a30862015-04-22 09:02:32 -06001078 VkResult U_ASSERT_ONLY err;
Chia-I Wuf8385062015-01-04 16:27:24 +08001079 uint32_t count;
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -06001080 uint32_t i;
Chia-I Wuf8385062015-01-04 16:27:24 +08001081
1082 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001083 view_info[i].sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_ATTACH_INFO;
Chia-I Wuf8385062015-01-04 16:27:24 +08001084 view_info[i].pNext = NULL;
1085 view_info[i].view = demo->textures[i].view,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001086 view_info[i].layout = VK_IMAGE_LAYOUT_GENERAL;
Chia-I Wuf8385062015-01-04 16:27:24 +08001087
Courtney Goeltzenleuchterd38dcc92015-04-09 11:43:10 -06001088 combined_info[i].sampler = demo->textures[i].sampler;
Chia-I Wuf8385062015-01-04 16:27:24 +08001089 combined_info[i].pImageView = &view_info[i];
1090 }
1091
1092 memset(&update, 0, sizeof(update));
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001093 update.sType = VK_STRUCTURE_TYPE_UPDATE_SAMPLER_TEXTURES;
Chia-I Wuf8385062015-01-04 16:27:24 +08001094 update.count = DEMO_TEXTURE_COUNT;
1095 update.pSamplerImageViews = combined_info;
1096
Mike Stroyan230e6252015-04-17 12:36:38 -06001097 err = vkAllocDescriptorSets(demo->device, demo->desc_pool,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001098 VK_DESCRIPTOR_SET_USAGE_STATIC,
Chia-I Wuf8385062015-01-04 16:27:24 +08001099 1, &demo->desc_layout,
1100 &demo->desc_set, &count);
1101 assert(!err && count == 1);
1102
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001103 vkBeginDescriptorPoolUpdate(demo->device,
1104 VK_DESCRIPTOR_UPDATE_MODE_FASTEST);
Chia-I Wuf8385062015-01-04 16:27:24 +08001105
Mike Stroyan230e6252015-04-17 12:36:38 -06001106 vkClearDescriptorSets(demo->device, demo->desc_pool, 1, &demo->desc_set);
1107 vkUpdateDescriptors(demo->device, demo->desc_set, 1, update_array);
Chia-I Wuf8385062015-01-04 16:27:24 +08001108
Courtney Goeltzenleuchter633f9c52015-04-30 10:58:33 -06001109 vkEndDescriptorPoolUpdate(demo->device, demo->draw_cmd);
Chia-I Wuf8385062015-01-04 16:27:24 +08001110}
1111
Chia-I Wuc19795a2014-09-13 11:12:55 +08001112static void demo_prepare(struct demo *demo)
1113{
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001114 const VkCmdBufferCreateInfo cmd = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001115 .sType = VK_STRUCTURE_TYPE_CMD_BUFFER_CREATE_INFO,
Chia-I Wuc19795a2014-09-13 11:12:55 +08001116 .pNext = NULL,
Courtney Goeltzenleuchterf3168062015-03-05 18:09:39 -07001117 .queueNodeIndex = demo->graphics_queue_node_index,
Chia-I Wuc19795a2014-09-13 11:12:55 +08001118 .flags = 0,
1119 };
Tony Barbour22a30862015-04-22 09:02:32 -06001120 VkResult U_ASSERT_ONLY err;
Chia-I Wuc19795a2014-09-13 11:12:55 +08001121
Courtney Goeltzenleuchter633f9c52015-04-30 10:58:33 -06001122 err = vkCreateCommandBuffer(demo->device, &cmd, &demo->draw_cmd);
1123 assert(!err);
1124
Chia-I Wuc19795a2014-09-13 11:12:55 +08001125 demo_prepare_buffers(demo);
Chia-I Wu9ae87c92014-10-07 14:15:01 +08001126 demo_prepare_depth(demo);
Chia-I Wub043fe32014-10-06 15:30:33 +08001127 demo_prepare_textures(demo);
Chia-I Wu99621bc2014-10-08 11:52:22 +08001128 demo_prepare_vertices(demo);
Chia-I Wuf8385062015-01-04 16:27:24 +08001129 demo_prepare_descriptor_layout(demo);
Chia-I Wuc19795a2014-09-13 11:12:55 +08001130 demo_prepare_pipeline(demo);
1131 demo_prepare_dynamic_states(demo);
1132
Chia-I Wu8d24b3b2015-03-26 13:14:16 +08001133 demo_prepare_descriptor_pool(demo);
Chia-I Wuf8385062015-01-04 16:27:24 +08001134 demo_prepare_descriptor_set(demo);
Courtney Goeltzenleuchter633f9c52015-04-30 10:58:33 -06001135 demo->prepared = true;
Chia-I Wuc19795a2014-09-13 11:12:55 +08001136}
1137
Ian Elliotte14e9f92015-04-16 15:23:05 -06001138#ifdef _WIN32
1139static void demo_run(struct demo *demo)
1140{
Courtney Goeltzenleuchter633f9c52015-04-30 10:58:33 -06001141 if (!demo->prepared)
1142 return;
Ian Elliotte14e9f92015-04-16 15:23:05 -06001143 demo_draw(demo);
1144}
1145
1146// On MS-Windows, make this a global, so it's available to WndProc()
1147struct demo demo;
1148
1149// MS-Windows event handling function:
1150LRESULT CALLBACK WndProc(HWND hWnd,
1151 UINT uMsg,
1152 WPARAM wParam,
1153 LPARAM lParam)
1154{
Ian Elliott4e19ed02015-04-28 10:52:52 -06001155 char tmp_str[] = APP_LONG_NAME;
Ian Elliotte14e9f92015-04-16 15:23:05 -06001156
1157 switch(uMsg)
1158 {
1159 case WM_CREATE:
1160 return 0;
1161 case WM_CLOSE:
1162 PostQuitMessage(0);
1163 return 0;
1164 case WM_PAINT:
1165 demo_run(&demo);
1166 return 0;
1167 default:
1168 break;
1169 }
1170 return (DefWindowProc(hWnd, uMsg, wParam, lParam));
1171}
1172
1173static void demo_create_window(struct demo *demo)
1174{
1175 WNDCLASSEX win_class;
1176
1177 // Initialize the window class structure:
1178 win_class.cbSize = sizeof(WNDCLASSEX);
1179 win_class.style = CS_HREDRAW | CS_VREDRAW;
1180 win_class.lpfnWndProc = WndProc;
1181 win_class.cbClsExtra = 0;
1182 win_class.cbWndExtra = 0;
1183 win_class.hInstance = demo->connection; // hInstance
1184 win_class.hIcon = LoadIcon(NULL, IDI_APPLICATION);
1185 win_class.hCursor = LoadCursor(NULL, IDC_ARROW);
1186 win_class.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
1187 win_class.lpszMenuName = NULL;
1188 win_class.lpszClassName = demo->name;
1189 win_class.hIconSm = LoadIcon(NULL, IDI_WINLOGO);
1190 // Register window class:
1191 if (!RegisterClassEx(&win_class)) {
1192 // It didn't work, so try to give a useful error:
1193 printf("Unexpected error trying to start the application!\n");
1194 fflush(stdout);
1195 exit(1);
1196 }
1197 // Create window with the registered class:
1198 demo->window = CreateWindowEx(0,
1199 demo->name, // class name
1200 demo->name, // app name
1201 WS_OVERLAPPEDWINDOW | // window style
1202 WS_VISIBLE |
1203 WS_SYSMENU,
1204 100,100, // x/y coords
1205 demo->width, // width
1206 demo->height, // height
1207 NULL, // handle to parent
1208 NULL, // handle to menu
1209 demo->connection, // hInstance
1210 NULL); // no extra parameters
1211 if (!demo->window) {
1212 // It didn't work, so try to give a useful error:
1213 printf("Cannot create a window in which to draw!\n");
1214 fflush(stdout);
1215 exit(1);
1216 }
1217}
1218#else // _WIN32
1219
Chia-I Wuc19795a2014-09-13 11:12:55 +08001220static void demo_handle_event(struct demo *demo,
1221 const xcb_generic_event_t *event)
1222{
1223 switch (event->response_type & 0x7f) {
1224 case XCB_EXPOSE:
1225 demo_draw(demo);
1226 break;
Courtney Goeltzenleuchterca698052014-11-07 15:17:03 -07001227 case XCB_CLIENT_MESSAGE:
1228 if((*(xcb_client_message_event_t*)event).data.data32[0] ==
1229 (*demo->atom_wm_delete_window).atom) {
1230 demo->quit = true;
1231 }
1232 break;
Chia-I Wuc19795a2014-09-13 11:12:55 +08001233 case XCB_KEY_RELEASE:
1234 {
1235 const xcb_key_release_event_t *key =
1236 (const xcb_key_release_event_t *) event;
1237
1238 if (key->detail == 0x9)
1239 demo->quit = true;
1240 }
1241 break;
Courtney Goeltzenleuchterca698052014-11-07 15:17:03 -07001242 case XCB_DESTROY_NOTIFY:
1243 demo->quit = true;
1244 break;
Chia-I Wuc19795a2014-09-13 11:12:55 +08001245 default:
1246 break;
1247 }
1248}
1249
1250static void demo_run(struct demo *demo)
1251{
1252 xcb_flush(demo->connection);
1253
1254 while (!demo->quit) {
1255 xcb_generic_event_t *event;
1256
1257 event = xcb_wait_for_event(demo->connection);
1258 if (event) {
1259 demo_handle_event(demo, event);
1260 free(event);
1261 }
1262 }
1263}
1264
1265static void demo_create_window(struct demo *demo)
1266{
1267 uint32_t value_mask, value_list[32];
1268
1269 demo->window = xcb_generate_id(demo->connection);
1270
1271 value_mask = XCB_CW_BACK_PIXEL | XCB_CW_EVENT_MASK;
1272 value_list[0] = demo->screen->black_pixel;
1273 value_list[1] = XCB_EVENT_MASK_KEY_RELEASE |
Courtney Goeltzenleuchterca698052014-11-07 15:17:03 -07001274 XCB_EVENT_MASK_EXPOSURE |
1275 XCB_EVENT_MASK_STRUCTURE_NOTIFY;
Chia-I Wuc19795a2014-09-13 11:12:55 +08001276
1277 xcb_create_window(demo->connection,
1278 XCB_COPY_FROM_PARENT,
1279 demo->window, demo->screen->root,
1280 0, 0, demo->width, demo->height, 0,
1281 XCB_WINDOW_CLASS_INPUT_OUTPUT,
1282 demo->screen->root_visual,
1283 value_mask, value_list);
1284
Courtney Goeltzenleuchterca698052014-11-07 15:17:03 -07001285 /* Magic code that will send notification when window is destroyed */
1286 xcb_intern_atom_cookie_t cookie = xcb_intern_atom(demo->connection, 1, 12,
1287 "WM_PROTOCOLS");
1288 xcb_intern_atom_reply_t* reply = xcb_intern_atom_reply(demo->connection, cookie, 0);
1289
1290 xcb_intern_atom_cookie_t cookie2 = xcb_intern_atom(demo->connection, 0, 16, "WM_DELETE_WINDOW");
1291 demo->atom_wm_delete_window = xcb_intern_atom_reply(demo->connection, cookie2, 0);
1292
1293 xcb_change_property(demo->connection, XCB_PROP_MODE_REPLACE,
1294 demo->window, (*reply).atom, 4, 32, 1,
1295 &(*demo->atom_wm_delete_window).atom);
1296 free(reply);
1297
Chia-I Wuc19795a2014-09-13 11:12:55 +08001298 xcb_map_window(demo->connection, demo->window);
1299}
Ian Elliotte14e9f92015-04-16 15:23:05 -06001300#endif // _WIN32
Chia-I Wuc19795a2014-09-13 11:12:55 +08001301
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001302static void demo_init_vk(struct demo *demo)
Chia-I Wuc19795a2014-09-13 11:12:55 +08001303{
Tobin Ehlis3536b442015-04-16 18:04:57 -06001304 VkResult err;
1305 // Extensions to enable
1306 const char *ext_names[] = {
Chia-I Wu5b66aa52015-04-16 22:02:10 +08001307 "VK_WSI_LunarG",
Tobin Ehlis3536b442015-04-16 18:04:57 -06001308 };
1309 size_t extSize = sizeof(uint32_t);
1310 uint32_t extCount = 0;
1311 err = vkGetGlobalExtensionInfo(VK_EXTENSION_INFO_TYPE_COUNT, 0, &extSize, &extCount);
1312 assert(!err);
1313
1314 VkExtensionProperties extProp;
1315 extSize = sizeof(VkExtensionProperties);
Tony Barbour22a30862015-04-22 09:02:32 -06001316 bool32_t U_ASSERT_ONLY extFound = 0;
Tobin Ehlis3536b442015-04-16 18:04:57 -06001317 for (uint32_t i = 0; i < extCount; i++) {
1318 err = vkGetGlobalExtensionInfo(VK_EXTENSION_INFO_TYPE_PROPERTIES, i, &extSize, &extProp);
1319 if (!strcmp(ext_names[0], extProp.extName))
1320 extFound = 1;
1321 }
Ian Elliott3b375cf2015-04-28 13:22:33 -06001322 if (!extFound) {
1323 ERR_EXIT("vkGetGlobalExtensionInfo failed to find the "
1324 "\"VK_WSI_LunarG\" extension.\n\nDo you have a compatible "
1325 "Vulkan installable client driver (ICD) installed?\nPlease "
1326 "look at the Getting Started guide for additional "
1327 "information.\n",
1328 "vkCreateInstance Failure");
1329 }
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001330 const VkApplicationInfo app = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001331 .sType = VK_STRUCTURE_TYPE_APPLICATION_INFO,
Chia-I Wuc19795a2014-09-13 11:12:55 +08001332 .pNext = NULL,
Ian Elliott4e19ed02015-04-28 10:52:52 -06001333 .pAppName = APP_SHORT_NAME,
Chia-I Wuc19795a2014-09-13 11:12:55 +08001334 .appVersion = 0,
Ian Elliott4e19ed02015-04-28 10:52:52 -06001335 .pEngineName = APP_SHORT_NAME,
Chia-I Wuc19795a2014-09-13 11:12:55 +08001336 .engineVersion = 0,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001337 .apiVersion = VK_API_VERSION,
Chia-I Wuc19795a2014-09-13 11:12:55 +08001338 };
Courtney Goeltzenleuchterddcb6192015-04-14 18:48:46 -06001339 const VkInstanceCreateInfo inst_info = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001340 .sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO,
Jon Ashburn29669a42015-04-04 14:52:07 -06001341 .pNext = NULL,
1342 .pAppInfo = &app,
1343 .pAllocCb = NULL,
Tobin Ehlis3536b442015-04-16 18:04:57 -06001344 .extensionCount = 1,
1345 .ppEnabledExtensionNames = ext_names,
Jon Ashburn29669a42015-04-04 14:52:07 -06001346 };
Courtney Goeltzenleuchterddcb6192015-04-14 18:48:46 -06001347 const VkDeviceQueueCreateInfo queue = {
Chia-I Wuc19795a2014-09-13 11:12:55 +08001348 .queueNodeIndex = 0,
1349 .queueCount = 1,
1350 };
Courtney Goeltzenleuchterddcb6192015-04-14 18:48:46 -06001351 const VkDeviceCreateInfo device = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001352 .sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO,
Chia-I Wuc19795a2014-09-13 11:12:55 +08001353 .pNext = NULL,
1354 .queueRecordCount = 1,
1355 .pRequestedQueues = &queue,
Tobin Ehlis3536b442015-04-16 18:04:57 -06001356 .extensionCount = 1,
Chia-I Wuc19795a2014-09-13 11:12:55 +08001357 .ppEnabledExtensionNames = ext_names,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001358 .flags = VK_DEVICE_CREATE_VALIDATION_BIT,
Chia-I Wuc19795a2014-09-13 11:12:55 +08001359 };
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -06001360 uint32_t gpu_count;
1361 uint32_t i;
Courtney Goeltzenleuchterf3168062015-03-05 18:09:39 -07001362 size_t data_size;
1363 uint32_t queue_count;
Chia-I Wuc19795a2014-09-13 11:12:55 +08001364
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001365 err = vkCreateInstance(&inst_info, &demo->inst);
Ian Elliottcaa9f272015-04-28 11:35:02 -06001366 if (err == VK_ERROR_INCOMPATIBLE_DRIVER) {
1367 ERR_EXIT("Cannot find a compatible Vulkan installable client driver "
Ian Elliott3b375cf2015-04-28 13:22:33 -06001368 "(ICD).\n\nPlease look at the Getting Started guide for "
Ian Elliottcaa9f272015-04-28 11:35:02 -06001369 "additional information.\n",
1370 "vkCreateInstance Failure");
1371 } else if (err) {
Ian Elliott3b375cf2015-04-28 13:22:33 -06001372 ERR_EXIT("vkCreateInstance failed.\n\nDo you have a compatible Vulkan "
1373 "installable client driver (ICD) installed?\nPlease look at "
Ian Elliottcaa9f272015-04-28 11:35:02 -06001374 "the Getting Started guide for additional information.\n",
1375 "vkCreateInstance Failure");
Ian Elliottdfe55f72015-04-03 15:24:55 -06001376 }
Jon Ashburn29669a42015-04-04 14:52:07 -06001377
Jon Ashburn07b309a2015-04-15 11:31:12 -06001378 gpu_count = 1;
1379 err = vkEnumeratePhysicalDevices(demo->inst, &gpu_count, &demo->gpu);
Chia-I Wuc19795a2014-09-13 11:12:55 +08001380 assert(!err && gpu_count == 1);
1381
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001382 err = vkCreateDevice(demo->gpu, &device, &demo->device);
Chia-I Wuc19795a2014-09-13 11:12:55 +08001383 assert(!err);
1384
Tony Barbour8205d902015-04-16 15:59:00 -06001385 err = vkGetPhysicalDeviceInfo(demo->gpu, VK_PHYSICAL_DEVICE_INFO_TYPE_PROPERTIES,
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -06001386 &data_size, NULL);
1387 assert(!err);
1388
Tony Barbour8205d902015-04-16 15:59:00 -06001389 demo->gpu_props = (VkPhysicalDeviceProperties *) malloc(data_size);
1390 err = vkGetPhysicalDeviceInfo(demo->gpu, VK_PHYSICAL_DEVICE_INFO_TYPE_PROPERTIES,
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -06001391 &data_size, demo->gpu_props);
1392 assert(!err);
1393
Tony Barbour8205d902015-04-16 15:59:00 -06001394 err = vkGetPhysicalDeviceInfo(demo->gpu, VK_PHYSICAL_DEVICE_INFO_TYPE_QUEUE_PROPERTIES,
Courtney Goeltzenleuchterf3168062015-03-05 18:09:39 -07001395 &data_size, NULL);
1396 assert(!err);
1397
Tony Barbour8205d902015-04-16 15:59:00 -06001398 demo->queue_props = (VkPhysicalDeviceQueueProperties *) malloc(data_size);
1399 err = vkGetPhysicalDeviceInfo(demo->gpu, VK_PHYSICAL_DEVICE_INFO_TYPE_QUEUE_PROPERTIES,
Courtney Goeltzenleuchterf3168062015-03-05 18:09:39 -07001400 &data_size, demo->queue_props);
1401 assert(!err);
Mike Stroyan230e6252015-04-17 12:36:38 -06001402 queue_count = (uint32_t) (data_size / sizeof(VkPhysicalDeviceQueueProperties));
Courtney Goeltzenleuchterf3168062015-03-05 18:09:39 -07001403 assert(queue_count >= 1);
1404
1405 for (i = 0; i < queue_count; i++) {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001406 if (demo->queue_props[i].queueFlags & VK_QUEUE_GRAPHICS_BIT)
Courtney Goeltzenleuchterf3168062015-03-05 18:09:39 -07001407 break;
1408 }
1409 assert(i < queue_count);
1410 demo->graphics_queue_node_index = i;
1411
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001412 err = vkGetDeviceQueue(demo->device, demo->graphics_queue_node_index,
Chia-I Wuc19795a2014-09-13 11:12:55 +08001413 0, &demo->queue);
1414 assert(!err);
Ian Elliott32536f92015-04-21 16:41:02 -06001415
1416
1417 // Get the VkDisplayWSI's associated with this physical device:
1418 VkDisplayWSI display;
1419 err = vkGetPhysicalDeviceInfo(demo->gpu, VK_PHYSICAL_DEVICE_INFO_TYPE_DISPLAY_PROPERTIES_WSI,
1420 &data_size, NULL);
Ian Elliotte16c6cf2015-05-20 14:54:42 -06001421 if (err != VK_SUCCESS) {
1422 printf("The Vulkan installable client driver (ICD) does not support "
1423 "querying\nfor the swap-chain image format. Therefore, am "
1424 "hardcoding this\nformat to VK_FORMAT_B8G8R8A8_UNORM.\n");
1425 fflush(stdout);
1426 demo->format = VK_FORMAT_B8G8R8A8_UNORM;
1427 return;
1428 }
Ian Elliott32536f92015-04-21 16:41:02 -06001429 demo->display_props = (VkDisplayPropertiesWSI *) malloc(data_size);
1430 err = vkGetPhysicalDeviceInfo(demo->gpu, VK_PHYSICAL_DEVICE_INFO_TYPE_DISPLAY_PROPERTIES_WSI,
1431 &data_size, demo->display_props);
1432 assert(!err);
1433 demo->num_displays = data_size / sizeof(VkDisplayPropertiesWSI);
1434 // For now, simply use the first display (TODO: Enhance this for the
1435 // future):
1436 display = demo->display_props[0].display;
1437
1438 // Get a VkFormat to use with the VkDisplayWSI we are using:
1439 err = vkGetDisplayInfoWSI(display, VK_DISPLAY_INFO_TYPE_FORMAT_PROPERTIES_WSI,
1440 &data_size, NULL);
1441 VkDisplayFormatPropertiesWSI* display_format_props =
1442 (VkDisplayFormatPropertiesWSI*) malloc(data_size);
1443 err = vkGetDisplayInfoWSI(display, VK_DISPLAY_INFO_TYPE_FORMAT_PROPERTIES_WSI,
1444 &data_size, display_format_props);
1445 // For now, simply use the first VkFormat (TODO: Enhance this for the
1446 // future):
1447 demo->format = display_format_props[0].swapChainFormat;
Chia-I Wuc19795a2014-09-13 11:12:55 +08001448}
1449
1450static void demo_init_connection(struct demo *demo)
1451{
Ian Elliotte14e9f92015-04-16 15:23:05 -06001452#ifndef _WIN32
Chia-I Wuc19795a2014-09-13 11:12:55 +08001453 const xcb_setup_t *setup;
1454 xcb_screen_iterator_t iter;
1455 int scr;
1456
1457 demo->connection = xcb_connect(NULL, &scr);
Ian Elliottdfe55f72015-04-03 15:24:55 -06001458 if (demo->connection == NULL) {
1459 printf("Cannot find a compatible Vulkan installable client driver "
1460 "(ICD).\nExiting ...\n");
1461 fflush(stdout);
1462 exit(1);
1463 }
Chia-I Wuc19795a2014-09-13 11:12:55 +08001464
1465 setup = xcb_get_setup(demo->connection);
1466 iter = xcb_setup_roots_iterator(setup);
1467 while (scr-- > 0)
1468 xcb_screen_next(&iter);
1469
1470 demo->screen = iter.data;
Ian Elliotte14e9f92015-04-16 15:23:05 -06001471#endif // _WIN32
Chia-I Wuc19795a2014-09-13 11:12:55 +08001472}
1473
Ian Elliotte14e9f92015-04-16 15:23:05 -06001474#ifdef _WIN32
1475static void demo_init(struct demo *demo, HINSTANCE hInstance, LPSTR pCmdLine)
1476#else // _WIN32
Courtney Goeltzenleuchterf113a952015-02-25 11:46:58 -07001477static void demo_init(struct demo *demo, const int argc, const char *argv[])
Ian Elliotte14e9f92015-04-16 15:23:05 -06001478#endif // _WIN32
Chia-I Wuc19795a2014-09-13 11:12:55 +08001479{
Ian Elliotte14e9f92015-04-16 15:23:05 -06001480 bool argv_error = false;
1481
Chia-I Wuc19795a2014-09-13 11:12:55 +08001482 memset(demo, 0, sizeof(*demo));
1483
Ian Elliotte14e9f92015-04-16 15:23:05 -06001484#ifdef _WIN32
1485 demo->connection = hInstance;
Ian Elliott4e19ed02015-04-28 10:52:52 -06001486 strncpy(demo->name, APP_SHORT_NAME, APP_NAME_STR_LEN);
Ian Elliotte14e9f92015-04-16 15:23:05 -06001487
1488 if (strncmp(pCmdLine, "--use_staging", strlen("--use_staging")) == 0)
1489 demo->use_staging_buffer = true;
1490 else if (strlen(pCmdLine) != 0) {
1491 fprintf(stderr, "Do not recognize argument \"%s\".\n", pCmdLine);
1492 argv_error = true;
1493 }
1494#else // _WIN32
Courtney Goeltzenleuchterf113a952015-02-25 11:46:58 -07001495 for (int i = 0; i < argc; i++) {
1496 if (strncmp(argv[i], "--use_staging", strlen("--use_staging")) == 0)
1497 demo->use_staging_buffer = true;
1498 }
Ian Elliotte14e9f92015-04-16 15:23:05 -06001499#endif // _WIN32
1500 if (argv_error) {
Ian Elliott4e19ed02015-04-28 10:52:52 -06001501 fprintf(stderr, "Usage:\n %s [--use_staging]\n", APP_SHORT_NAME);
Ian Elliotte14e9f92015-04-16 15:23:05 -06001502 fflush(stderr);
1503 exit(1);
1504 }
Courtney Goeltzenleuchterf113a952015-02-25 11:46:58 -07001505
Chia-I Wuc19795a2014-09-13 11:12:55 +08001506 demo_init_connection(demo);
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001507 demo_init_vk(demo);
Chia-I Wuc19795a2014-09-13 11:12:55 +08001508
1509 demo->width = 300;
1510 demo->height = 300;
Chia-I Wuc19795a2014-09-13 11:12:55 +08001511}
1512
1513static void demo_cleanup(struct demo *demo)
1514{
Mark Lobodzinski23182612015-05-29 09:32:35 -05001515 uint32_t i;
Chia-I Wuc19795a2014-09-13 11:12:55 +08001516
Mike Stroyan230e6252015-04-17 12:36:38 -06001517 vkDestroyObject(demo->device, VK_OBJECT_TYPE_DESCRIPTOR_SET, demo->desc_set);
1518 vkDestroyObject(demo->device, VK_OBJECT_TYPE_DESCRIPTOR_POOL, demo->desc_pool);
Chia-I Wuf8385062015-01-04 16:27:24 +08001519
Courtney Goeltzenleuchter633f9c52015-04-30 10:58:33 -06001520 if (demo->setup_cmd) {
1521 vkDestroyObject(demo->device, VK_OBJECT_TYPE_COMMAND_BUFFER, demo->setup_cmd);
1522 }
1523 vkDestroyObject(demo->device, VK_OBJECT_TYPE_COMMAND_BUFFER, demo->draw_cmd);
Chia-I Wuc19795a2014-09-13 11:12:55 +08001524
Mike Stroyan230e6252015-04-17 12:36:38 -06001525 vkDestroyObject(demo->device, VK_OBJECT_TYPE_DYNAMIC_VP_STATE, demo->viewport);
1526 vkDestroyObject(demo->device, VK_OBJECT_TYPE_DYNAMIC_RS_STATE, demo->raster);
1527 vkDestroyObject(demo->device, VK_OBJECT_TYPE_DYNAMIC_CB_STATE, demo->color_blend);
1528 vkDestroyObject(demo->device, VK_OBJECT_TYPE_DYNAMIC_DS_STATE, demo->depth_stencil);
Chia-I Wuc19795a2014-09-13 11:12:55 +08001529
Mike Stroyan230e6252015-04-17 12:36:38 -06001530 vkDestroyObject(demo->device, VK_OBJECT_TYPE_PIPELINE, demo->pipeline);
1531 vkDestroyObject(demo->device, VK_OBJECT_TYPE_PIPELINE_LAYOUT, demo->pipeline_layout);
1532 vkDestroyObject(demo->device, VK_OBJECT_TYPE_DESCRIPTOR_SET_LAYOUT, demo->desc_layout);
Chia-I Wub043fe32014-10-06 15:30:33 +08001533
Mark Lobodzinski23182612015-05-29 09:32:35 -05001534 vkBindObjectMemory(demo->device, VK_OBJECT_TYPE_BUFFER, demo->vertices.buf, VK_NULL_HANDLE, 0);
Mike Stroyan230e6252015-04-17 12:36:38 -06001535 vkDestroyObject(demo->device, VK_OBJECT_TYPE_BUFFER, demo->vertices.buf);
Mark Lobodzinski23182612015-05-29 09:32:35 -05001536 vkFreeMemory(demo->device, demo->vertices.mem);
Chia-I Wu99621bc2014-10-08 11:52:22 +08001537
Chia-I Wub043fe32014-10-06 15:30:33 +08001538 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Mike Stroyan230e6252015-04-17 12:36:38 -06001539 vkDestroyObject(demo->device, VK_OBJECT_TYPE_IMAGE_VIEW, demo->textures[i].view);
Mark Lobodzinski23182612015-05-29 09:32:35 -05001540 vkBindObjectMemory(demo->device, VK_OBJECT_TYPE_IMAGE, demo->textures[i].image, VK_NULL_HANDLE, 0);
Mike Stroyan230e6252015-04-17 12:36:38 -06001541 vkDestroyObject(demo->device, VK_OBJECT_TYPE_IMAGE, demo->textures[i].image);
Mark Lobodzinski23182612015-05-29 09:32:35 -05001542 vkFreeMemory(demo->device, demo->textures[i].mem);
Mike Stroyan230e6252015-04-17 12:36:38 -06001543 vkDestroyObject(demo->device, VK_OBJECT_TYPE_SAMPLER, demo->textures[i].sampler);
Chia-I Wub043fe32014-10-06 15:30:33 +08001544 }
1545
Mike Stroyan230e6252015-04-17 12:36:38 -06001546 vkDestroyObject(demo->device, VK_OBJECT_TYPE_DEPTH_STENCIL_VIEW, demo->depth.view);
Mark Lobodzinski23182612015-05-29 09:32:35 -05001547 vkBindObjectMemory(demo->device, VK_OBJECT_TYPE_IMAGE, demo->depth.image, VK_NULL_HANDLE, 0);
Mike Stroyan230e6252015-04-17 12:36:38 -06001548 vkDestroyObject(demo->device, VK_OBJECT_TYPE_IMAGE, demo->depth.image);
Mark Lobodzinski23182612015-05-29 09:32:35 -05001549 vkFreeMemory(demo->device, demo->depth.mem);
Chia-I Wu9ae87c92014-10-07 14:15:01 +08001550
Chia-I Wuc19795a2014-09-13 11:12:55 +08001551 for (i = 0; i < DEMO_BUFFER_COUNT; i++) {
Mike Stroyan230e6252015-04-17 12:36:38 -06001552 vkDestroyObject(demo->device, VK_OBJECT_TYPE_COLOR_ATTACHMENT_VIEW, demo->buffers[i].view);
Chia-I Wuc19795a2014-09-13 11:12:55 +08001553 }
Chia-I Wu5b66aa52015-04-16 22:02:10 +08001554 vkDestroySwapChainWSI(demo->swap_chain);
Chia-I Wuc19795a2014-09-13 11:12:55 +08001555
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001556 vkDestroyDevice(demo->device);
1557 vkDestroyInstance(demo->inst);
Chia-I Wuc19795a2014-09-13 11:12:55 +08001558
Ian Elliotte14e9f92015-04-16 15:23:05 -06001559#ifndef _WIN32
Chia-I Wuc19795a2014-09-13 11:12:55 +08001560 xcb_destroy_window(demo->connection, demo->window);
1561 xcb_disconnect(demo->connection);
Ian Elliotte14e9f92015-04-16 15:23:05 -06001562#endif // _WIN32
Chia-I Wuc19795a2014-09-13 11:12:55 +08001563}
1564
Ian Elliotte14e9f92015-04-16 15:23:05 -06001565#ifdef _WIN32
1566int APIENTRY WinMain(HINSTANCE hInstance,
1567 HINSTANCE hPrevInstance,
1568 LPSTR pCmdLine,
1569 int nCmdShow)
1570{
1571 MSG msg; // message
1572 bool done; // flag saying when app is complete
1573
1574 demo_init(&demo, hInstance, pCmdLine);
1575 demo_create_window(&demo);
1576
1577 demo_prepare(&demo);
1578
1579 done = false; //initialize loop condition variable
1580 /* main message loop*/
1581 while(!done)
1582 {
Ian Elliott421107f2015-04-28 15:50:36 -06001583 PeekMessage(&msg, NULL, 0, 0, PM_REMOVE);
Ian Elliotte14e9f92015-04-16 15:23:05 -06001584 if (msg.message == WM_QUIT) //check for a quit message
1585 {
1586 done = true; //if found, quit app
1587 }
1588 else
1589 {
1590 /* Translate and dispatch to event queue*/
1591 TranslateMessage(&msg);
1592 DispatchMessage(&msg);
1593 }
1594 }
1595
1596 demo_cleanup(&demo);
1597
Tony Barboura938abb2015-04-22 11:36:22 -06001598 return (int) msg.wParam;
Ian Elliotte14e9f92015-04-16 15:23:05 -06001599}
1600#else // _WIN32
Courtney Goeltzenleuchterf113a952015-02-25 11:46:58 -07001601int main(const int argc, const char *argv[])
Chia-I Wuc19795a2014-09-13 11:12:55 +08001602{
1603 struct demo demo;
1604
Courtney Goeltzenleuchterf113a952015-02-25 11:46:58 -07001605 demo_init(&demo, argc, argv);
Chia-I Wu5b66aa52015-04-16 22:02:10 +08001606 demo_create_window(&demo);
Chia-I Wuc19795a2014-09-13 11:12:55 +08001607
1608 demo_prepare(&demo);
Chia-I Wuc19795a2014-09-13 11:12:55 +08001609 demo_run(&demo);
1610
1611 demo_cleanup(&demo);
1612
Chia-I Wuc19795a2014-09-13 11:12:55 +08001613 return 0;
1614}
Ian Elliotte14e9f92015-04-16 15:23:05 -06001615#endif // _WIN32