blob: 4101cf965fbf1e9ed5f25092c99686d5604de3df [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;
Cody Northrop75db0322015-05-28 11:27:16 -0600108 bool use_glsl;
Chia-I Wuc19795a2014-09-13 11:12:55 +0800109
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600110 VkInstance inst;
Tony Barbour8205d902015-04-16 15:59:00 -0600111 VkPhysicalDevice gpu;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600112 VkDevice device;
113 VkQueue queue;
Tony Barbour8205d902015-04-16 15:59:00 -0600114 VkPhysicalDeviceProperties *gpu_props;
115 VkPhysicalDeviceQueueProperties *queue_props;
Courtney Goeltzenleuchterf3168062015-03-05 18:09:39 -0700116 uint32_t graphics_queue_node_index;
Chia-I Wuc19795a2014-09-13 11:12:55 +0800117
118 int width, height;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600119 VkFormat format;
Chia-I Wuc19795a2014-09-13 11:12:55 +0800120
Ian Elliott32536f92015-04-21 16:41:02 -0600121 VkDisplayPropertiesWSI *display_props;
122 int num_displays;
Chia-I Wu5b66aa52015-04-16 22:02:10 +0800123 VkSwapChainWSI swap_chain;
Chia-I Wuc19795a2014-09-13 11:12:55 +0800124 struct {
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600125 VkImage image;
Tony Barbour8205d902015-04-16 15:59:00 -0600126 VkDeviceMemory mem;
Chia-I Wuc19795a2014-09-13 11:12:55 +0800127
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600128 VkColorAttachmentView view;
Chia-I Wuc19795a2014-09-13 11:12:55 +0800129 } buffers[DEMO_BUFFER_COUNT];
130
Chia-I Wub043fe32014-10-06 15:30:33 +0800131 struct {
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600132 VkFormat format;
Chia-I Wu9ae87c92014-10-07 14:15:01 +0800133
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600134 VkImage image;
Mark Lobodzinski23182612015-05-29 09:32:35 -0500135 VkDeviceMemory mem;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600136 VkDepthStencilView view;
Chia-I Wu9ae87c92014-10-07 14:15:01 +0800137 } depth;
138
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600139 struct texture_object textures[DEMO_TEXTURE_COUNT];
Chia-I Wub043fe32014-10-06 15:30:33 +0800140
Chia-I Wu99621bc2014-10-08 11:52:22 +0800141 struct {
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600142 VkBuffer buf;
Mark Lobodzinski23182612015-05-29 09:32:35 -0500143 VkDeviceMemory mem;
Chia-I Wu8d29d022014-10-08 12:14:39 +0800144
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600145 VkPipelineVertexInputCreateInfo vi;
146 VkVertexInputBindingDescription vi_bindings[1];
147 VkVertexInputAttributeDescription vi_attrs[2];
Chia-I Wu99621bc2014-10-08 11:52:22 +0800148 } vertices;
149
Courtney Goeltzenleuchter633f9c52015-04-30 10:58:33 -0600150 VkCmdBuffer setup_cmd; // Command Buffer for initialization commands
151 VkCmdBuffer draw_cmd; // Command Buffer for drawing commands
Mark Lobodzinski556f7212015-04-17 14:11:39 -0500152 VkPipelineLayout pipeline_layout;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600153 VkDescriptorSetLayout desc_layout;
154 VkPipeline pipeline;
Chia-I Wuc19795a2014-09-13 11:12:55 +0800155
Courtney Goeltzenleuchterfcf855f2015-04-10 16:24:50 -0600156 VkDynamicVpState viewport;
157 VkDynamicRsState raster;
158 VkDynamicCbState color_blend;
159 VkDynamicDsState depth_stencil;
Chia-I Wuc19795a2014-09-13 11:12:55 +0800160
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600161 VkDescriptorPool desc_pool;
162 VkDescriptorSet desc_set;
Chia-I Wuf8385062015-01-04 16:27:24 +0800163
Chia-I Wuc19795a2014-09-13 11:12:55 +0800164 bool quit;
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600165 uint32_t current_buffer;
Chia-I Wuc19795a2014-09-13 11:12:55 +0800166};
167
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600168static void demo_flush_init_cmd(struct demo *demo)
169{
Tony Barbour22a30862015-04-22 09:02:32 -0600170 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600171
Courtney Goeltzenleuchter633f9c52015-04-30 10:58:33 -0600172 if (demo->setup_cmd == VK_NULL_HANDLE)
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600173 return;
174
Courtney Goeltzenleuchter633f9c52015-04-30 10:58:33 -0600175 err = vkEndCommandBuffer(demo->setup_cmd);
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600176 assert(!err);
177
Courtney Goeltzenleuchter633f9c52015-04-30 10:58:33 -0600178 const VkCmdBuffer cmd_bufs[] = { demo->setup_cmd };
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600179
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600180 err = vkQueueSubmit(demo->queue, 1, cmd_bufs, VK_NULL_HANDLE);
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600181 assert(!err);
182
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600183 err = vkQueueWaitIdle(demo->queue);
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600184 assert(!err);
185
Courtney Goeltzenleuchter633f9c52015-04-30 10:58:33 -0600186 vkDestroyObject(demo->device, VK_OBJECT_TYPE_COMMAND_BUFFER, demo->setup_cmd);
187 demo->setup_cmd = VK_NULL_HANDLE;
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600188}
189
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600190static void demo_set_image_layout(
191 struct demo *demo,
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600192 VkImage image,
malnasse4b8ba4d2015-06-03 17:28:38 -0400193 VkImageAspect aspect,
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600194 VkImageLayout old_image_layout,
195 VkImageLayout new_image_layout)
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600196{
Tony Barbour22a30862015-04-22 09:02:32 -0600197 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600198
Courtney Goeltzenleuchter633f9c52015-04-30 10:58:33 -0600199 if (demo->setup_cmd == VK_NULL_HANDLE) {
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600200 const VkCmdBufferCreateInfo cmd = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600201 .sType = VK_STRUCTURE_TYPE_CMD_BUFFER_CREATE_INFO,
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600202 .pNext = NULL,
203 .queueNodeIndex = demo->graphics_queue_node_index,
204 .flags = 0,
205 };
206
Courtney Goeltzenleuchter633f9c52015-04-30 10:58:33 -0600207 err = vkCreateCommandBuffer(demo->device, &cmd, &demo->setup_cmd);
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600208 assert(!err);
209
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600210 VkCmdBufferBeginInfo cmd_buf_info = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600211 .sType = VK_STRUCTURE_TYPE_CMD_BUFFER_BEGIN_INFO,
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600212 .pNext = NULL,
Tony Barbour8205d902015-04-16 15:59:00 -0600213 .flags = VK_CMD_BUFFER_OPTIMIZE_SMALL_BATCH_BIT |
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600214 VK_CMD_BUFFER_OPTIMIZE_ONE_TIME_SUBMIT_BIT,
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600215 };
Courtney Goeltzenleuchter633f9c52015-04-30 10:58:33 -0600216 err = vkBeginCommandBuffer(demo->setup_cmd, &cmd_buf_info);
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600217 }
218
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600219 VkImageMemoryBarrier image_memory_barrier = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600220 .sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600221 .pNext = NULL,
222 .outputMask = 0,
223 .inputMask = 0,
224 .oldLayout = old_image_layout,
225 .newLayout = new_image_layout,
226 .image = image,
malnasse4b8ba4d2015-06-03 17:28:38 -0400227 .subresourceRange = { aspect, 0, 1, 0, 0 }
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600228 };
229
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600230 if (new_image_layout == VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL) {
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600231 /* Make sure anything that was copying from this image has completed */
Courtney Goeltzenleuchterad870812015-04-15 15:29:59 -0600232 image_memory_barrier.inputMask = VK_MEMORY_INPUT_TRANSFER_BIT;
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600233 }
234
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600235 if (new_image_layout == VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL) {
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600236 /* Make sure any Copy or CPU writes to image are flushed */
Courtney Goeltzenleuchtera569a502015-04-29 17:16:21 -0600237 image_memory_barrier.outputMask = VK_MEMORY_OUTPUT_TRANSFER_BIT | VK_MEMORY_OUTPUT_HOST_WRITE_BIT;
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600238 }
239
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600240 VkImageMemoryBarrier *pmemory_barrier = &image_memory_barrier;
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600241
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600242 VkPipeEvent set_events[] = { VK_PIPE_EVENT_TOP_OF_PIPE };
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600243
Mike Stroyand678e562015-06-08 13:33:44 -0600244 vkCmdPipelineBarrier(demo->setup_cmd, VK_WAIT_EVENT_TOP_OF_PIPE, 1, set_events, 1, (const void **)&pmemory_barrier);
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600245}
246
Chia-I Wuc19795a2014-09-13 11:12:55 +0800247static void demo_draw_build_cmd(struct demo *demo)
248{
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600249 const VkColorAttachmentBindInfo color_attachment = {
Chia-I Wuc19795a2014-09-13 11:12:55 +0800250 .view = demo->buffers[demo->current_buffer].view,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600251 .layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
Chia-I Wuc19795a2014-09-13 11:12:55 +0800252 };
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600253 const VkDepthStencilBindInfo depth_stencil = {
Chia-I Wu9ae87c92014-10-07 14:15:01 +0800254 .view = demo->depth.view,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600255 .layout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
Chia-I Wu9ae87c92014-10-07 14:15:01 +0800256 };
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600257 const VkClearColor clear_color = {
Courtney Goeltzenleuchter9a1ded82015-04-03 16:35:32 -0600258 .color.floatColor = { 0.2f, 0.2f, 0.2f, 0.2f },
259 .useRawValue = false,
260 };
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600261 const float clear_depth = 0.9f;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600262 VkImageSubresourceRange clear_range;
263 VkCmdBufferBeginInfo cmd_buf_info = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600264 .sType = VK_STRUCTURE_TYPE_CMD_BUFFER_BEGIN_INFO,
Courtney Goeltzenleuchtere3b0f3a2015-04-03 15:25:24 -0600265 .pNext = NULL,
Tony Barbour8205d902015-04-16 15:59:00 -0600266 .flags = VK_CMD_BUFFER_OPTIMIZE_SMALL_BATCH_BIT |
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600267 VK_CMD_BUFFER_OPTIMIZE_ONE_TIME_SUBMIT_BIT,
Jon Ashburn53d27af2014-12-31 17:08:35 -0700268 };
Tony Barbour22a30862015-04-22 09:02:32 -0600269 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600270 VkAttachmentLoadOp load_op = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
271 VkAttachmentStoreOp store_op = VK_ATTACHMENT_STORE_OP_DONT_CARE;
272 const VkFramebufferCreateInfo fb_info = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600273 .sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO,
Jon Ashburn3325d6b2015-01-02 18:24:05 -0700274 .pNext = NULL,
275 .colorAttachmentCount = 1,
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600276 .pColorAttachments = (VkColorAttachmentBindInfo*) &color_attachment,
277 .pDepthStencilAttachment = (VkDepthStencilBindInfo*) &depth_stencil,
Jon Ashburn3325d6b2015-01-02 18:24:05 -0700278 .sampleCount = 1,
Mark Lobodzinski71fcc2d2015-01-27 13:24:03 -0600279 .width = demo->width,
280 .height = demo->height,
281 .layers = 1,
Jon Ashburn3325d6b2015-01-02 18:24:05 -0700282 };
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600283 VkRenderPassCreateInfo rp_info;
284 VkRenderPassBegin rp_begin;
Jon Ashburn3325d6b2015-01-02 18:24:05 -0700285
286 memset(&rp_info, 0 , sizeof(rp_info));
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600287 err = vkCreateFramebuffer(demo->device, &fb_info, &rp_begin.framebuffer);
Jon Ashburn3325d6b2015-01-02 18:24:05 -0700288 assert(!err);
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600289 rp_info.sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO;
Jon Ashburn3325d6b2015-01-02 18:24:05 -0700290 rp_info.renderArea.extent.width = demo->width;
291 rp_info.renderArea.extent.height = demo->height;
Piers Daniell886be472015-02-23 16:23:13 -0700292 rp_info.colorAttachmentCount = fb_info.colorAttachmentCount;
Courtney Goeltzenleuchtere3b0f3a2015-04-03 15:25:24 -0600293 rp_info.pColorFormats = &demo->format;
294 rp_info.pColorLayouts = &color_attachment.layout;
Jon Ashburn3325d6b2015-01-02 18:24:05 -0700295 rp_info.pColorLoadOps = &load_op;
296 rp_info.pColorStoreOps = &store_op;
Courtney Goeltzenleuchtere3b0f3a2015-04-03 15:25:24 -0600297 rp_info.pColorLoadClearValues = &clear_color;
Tony Barbour8205d902015-04-16 15:59:00 -0600298 rp_info.depthStencilFormat = VK_FORMAT_D16_UNORM;
Courtney Goeltzenleuchtere3b0f3a2015-04-03 15:25:24 -0600299 rp_info.depthStencilLayout = depth_stencil.layout;
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600300 rp_info.depthLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
Courtney Goeltzenleuchtere3b0f3a2015-04-03 15:25:24 -0600301 rp_info.depthLoadClearValue = clear_depth;
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600302 rp_info.depthStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE;
303 rp_info.stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
Courtney Goeltzenleuchtere3b0f3a2015-04-03 15:25:24 -0600304 rp_info.stencilLoadClearValue = 0;
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600305 rp_info.stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE;
306 err = vkCreateRenderPass(demo->device, &rp_info, &(rp_begin.renderPass));
Jon Ashburn3325d6b2015-01-02 18:24:05 -0700307 assert(!err);
Chia-I Wuc19795a2014-09-13 11:12:55 +0800308
Courtney Goeltzenleuchter633f9c52015-04-30 10:58:33 -0600309 err = vkBeginCommandBuffer(demo->draw_cmd, &cmd_buf_info);
Chia-I Wuc19795a2014-09-13 11:12:55 +0800310 assert(!err);
311
Courtney Goeltzenleuchter633f9c52015-04-30 10:58:33 -0600312 vkCmdBindPipeline(demo->draw_cmd, VK_PIPELINE_BIND_POINT_GRAPHICS,
Chia-I Wuc19795a2014-09-13 11:12:55 +0800313 demo->pipeline);
Courtney Goeltzenleuchter633f9c52015-04-30 10:58:33 -0600314 vkCmdBindDescriptorSets(demo->draw_cmd, VK_PIPELINE_BIND_POINT_GRAPHICS,
Cody Northrop1a01b1d2015-04-16 13:41:56 -0600315 0, 1, & demo->desc_set, 0, NULL);
Chia-I Wuc19795a2014-09-13 11:12:55 +0800316
Courtney Goeltzenleuchter633f9c52015-04-30 10:58:33 -0600317 vkCmdBindDynamicStateObject(demo->draw_cmd, VK_STATE_BIND_POINT_VIEWPORT, demo->viewport);
318 vkCmdBindDynamicStateObject(demo->draw_cmd, VK_STATE_BIND_POINT_RASTER, demo->raster);
319 vkCmdBindDynamicStateObject(demo->draw_cmd, VK_STATE_BIND_POINT_COLOR_BLEND,
Chia-I Wuc19795a2014-09-13 11:12:55 +0800320 demo->color_blend);
Courtney Goeltzenleuchter633f9c52015-04-30 10:58:33 -0600321 vkCmdBindDynamicStateObject(demo->draw_cmd, VK_STATE_BIND_POINT_DEPTH_STENCIL,
Chia-I Wuc19795a2014-09-13 11:12:55 +0800322 demo->depth_stencil);
323
Tony Barbour8205d902015-04-16 15:59:00 -0600324 VkDeviceSize offsets[1] = {0};
Courtney Goeltzenleuchter633f9c52015-04-30 10:58:33 -0600325 vkCmdBindVertexBuffers(demo->draw_cmd, VERTEX_BUFFER_BIND_ID, 1, &demo->vertices.buf, offsets);
Chia-I Wu3b04af52014-11-08 10:48:20 +0800326
Courtney Goeltzenleuchter633f9c52015-04-30 10:58:33 -0600327 vkCmdBeginRenderPass(demo->draw_cmd, &rp_begin);
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600328 clear_range.aspect = VK_IMAGE_ASPECT_COLOR;
Chia-I Wu68a7de42014-10-25 12:40:28 +0800329 clear_range.baseMipLevel = 0;
330 clear_range.mipLevels = 1;
331 clear_range.baseArraySlice = 0;
332 clear_range.arraySize = 1;
Courtney Goeltzenleuchter633f9c52015-04-30 10:58:33 -0600333 vkCmdClearColorImage(demo->draw_cmd,
Chia-I Wu68a7de42014-10-25 12:40:28 +0800334 demo->buffers[demo->current_buffer].image,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600335 VK_IMAGE_LAYOUT_CLEAR_OPTIMAL,
Courtney Goeltzenleuchterda4a99e2015-04-23 17:49:22 -0600336 &clear_color, 1, &clear_range);
Chia-I Wu68a7de42014-10-25 12:40:28 +0800337
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600338 clear_range.aspect = VK_IMAGE_ASPECT_DEPTH;
Courtney Goeltzenleuchter633f9c52015-04-30 10:58:33 -0600339 vkCmdClearDepthStencil(demo->draw_cmd,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600340 demo->depth.image, VK_IMAGE_LAYOUT_CLEAR_OPTIMAL,
Chia-I Wu68a7de42014-10-25 12:40:28 +0800341 clear_depth, 0, 1, &clear_range);
342
Courtney Goeltzenleuchter633f9c52015-04-30 10:58:33 -0600343 vkCmdDraw(demo->draw_cmd, 0, 3, 0, 1);
344 vkCmdEndRenderPass(demo->draw_cmd, rp_begin.renderPass);
Chia-I Wuc19795a2014-09-13 11:12:55 +0800345
Courtney Goeltzenleuchter633f9c52015-04-30 10:58:33 -0600346 err = vkEndCommandBuffer(demo->draw_cmd);
Chia-I Wuc19795a2014-09-13 11:12:55 +0800347 assert(!err);
Courtney Goeltzenleuchtere9afa992015-02-25 16:55:23 -0700348
Mike Stroyan230e6252015-04-17 12:36:38 -0600349 vkDestroyObject(demo->device, VK_OBJECT_TYPE_RENDER_PASS, rp_begin.renderPass);
350 vkDestroyObject(demo->device, VK_OBJECT_TYPE_FRAMEBUFFER, rp_begin.framebuffer);
Chia-I Wuc19795a2014-09-13 11:12:55 +0800351}
352
353static void demo_draw(struct demo *demo)
354{
Chia-I Wu5b66aa52015-04-16 22:02:10 +0800355 const VkPresentInfoWSI present = {
356 .sType = VK_STRUCTURE_TYPE_PRESENT_INFO_WSI,
357 .pNext = NULL,
358 .image = demo->buffers[demo->current_buffer].image,
359 .flipInterval = 0,
Chia-I Wuc19795a2014-09-13 11:12:55 +0800360 };
Tony Barbour22a30862015-04-22 09:02:32 -0600361 VkResult U_ASSERT_ONLY err;
Chia-I Wuc19795a2014-09-13 11:12:55 +0800362
363 demo_draw_build_cmd(demo);
364
Courtney Goeltzenleuchter633f9c52015-04-30 10:58:33 -0600365 err = vkQueueSubmit(demo->queue, 1, &demo->draw_cmd, VK_NULL_HANDLE);
Chia-I Wuc19795a2014-09-13 11:12:55 +0800366 assert(!err);
367
Chia-I Wu5b66aa52015-04-16 22:02:10 +0800368 err = vkQueuePresentWSI(demo->queue, &present);
Chia-I Wuc19795a2014-09-13 11:12:55 +0800369 assert(!err);
370
371 demo->current_buffer = (demo->current_buffer + 1) % DEMO_BUFFER_COUNT;
Chia-I Wu5b66aa52015-04-16 22:02:10 +0800372
373 err = vkQueueWaitIdle(demo->queue);
374 assert(err == VK_SUCCESS);
Chia-I Wuc19795a2014-09-13 11:12:55 +0800375}
376
377static void demo_prepare_buffers(struct demo *demo)
378{
Chia-I Wu5b66aa52015-04-16 22:02:10 +0800379 const VkSwapChainCreateInfoWSI swap_chain = {
380 .sType = VK_STRUCTURE_TYPE_SWAP_CHAIN_CREATE_INFO_WSI,
381 .pNext = NULL,
382 .pNativeWindowSystemHandle = demo->connection,
383 .pNativeWindowHandle = (void *) (intptr_t) demo->window,
Ian Elliott32536f92015-04-21 16:41:02 -0600384 .displayCount = 1,
Chia-I Wu5b66aa52015-04-16 22:02:10 +0800385 .imageCount = DEMO_BUFFER_COUNT,
386 .imageFormat = demo->format,
387 .imageExtent = {
Chia-I Wuc19795a2014-09-13 11:12:55 +0800388 .width = demo->width,
389 .height = demo->height,
390 },
Chia-I Wu5b66aa52015-04-16 22:02:10 +0800391 .imageArraySize = 1,
392 .imageUsageFlags = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT,
Chia-I Wuc19795a2014-09-13 11:12:55 +0800393 };
Chia-I Wu5b66aa52015-04-16 22:02:10 +0800394 VkSwapChainImageInfoWSI images[DEMO_BUFFER_COUNT];
395 size_t images_size = sizeof(images);
Tony Barbour22a30862015-04-22 09:02:32 -0600396 VkResult U_ASSERT_ONLY err;
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600397 uint32_t i;
Chia-I Wuc19795a2014-09-13 11:12:55 +0800398
Chia-I Wu5b66aa52015-04-16 22:02:10 +0800399 err = vkCreateSwapChainWSI(demo->device, &swap_chain, &demo->swap_chain);
400 assert(!err);
401
402 err = vkGetSwapChainInfoWSI(demo->swap_chain,
403 VK_SWAP_CHAIN_INFO_TYPE_PERSISTENT_IMAGES_WSI,
404 &images_size, images);
405 assert(!err && images_size == sizeof(images));
406
Chia-I Wuc19795a2014-09-13 11:12:55 +0800407 for (i = 0; i < DEMO_BUFFER_COUNT; i++) {
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600408 VkColorAttachmentViewCreateInfo color_attachment_view = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600409 .sType = VK_STRUCTURE_TYPE_COLOR_ATTACHMENT_VIEW_CREATE_INFO,
Chia-I Wuc19795a2014-09-13 11:12:55 +0800410 .pNext = NULL,
411 .format = demo->format,
412 .mipLevel = 0,
413 .baseArraySlice = 0,
414 .arraySize = 1,
415 };
416
Chia-I Wu5b66aa52015-04-16 22:02:10 +0800417 demo->buffers[i].image = images[i].image;
418 demo->buffers[i].mem = images[i].memory;
Mark Lobodzinski97dcd042015-04-16 08:52:00 -0500419
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600420 demo_set_image_layout(demo, demo->buffers[i].image,
malnasse4b8ba4d2015-06-03 17:28:38 -0400421 VK_IMAGE_ASPECT_COLOR,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600422 VK_IMAGE_LAYOUT_UNDEFINED,
423 VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL);
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600424
Chia-I Wuc19795a2014-09-13 11:12:55 +0800425 color_attachment_view.image = demo->buffers[i].image;
426
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600427 err = vkCreateColorAttachmentView(demo->device,
Chia-I Wuc19795a2014-09-13 11:12:55 +0800428 &color_attachment_view, &demo->buffers[i].view);
429 assert(!err);
430 }
Piers Daniell886be472015-02-23 16:23:13 -0700431
432 demo->current_buffer = 0;
Chia-I Wuc19795a2014-09-13 11:12:55 +0800433}
434
Chia-I Wu9ae87c92014-10-07 14:15:01 +0800435static void demo_prepare_depth(struct demo *demo)
436{
Tony Barbour8205d902015-04-16 15:59:00 -0600437 const VkFormat depth_format = VK_FORMAT_D16_UNORM;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600438 const VkImageCreateInfo image = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600439 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
Chia-I Wu9ae87c92014-10-07 14:15:01 +0800440 .pNext = NULL,
Tony Barbour8205d902015-04-16 15:59:00 -0600441 .imageType = VK_IMAGE_TYPE_2D,
Chia-I Wu9ae87c92014-10-07 14:15:01 +0800442 .format = depth_format,
443 .extent = { demo->width, demo->height, 1 },
444 .mipLevels = 1,
445 .arraySize = 1,
446 .samples = 1,
Tony Barbour8205d902015-04-16 15:59:00 -0600447 .tiling = VK_IMAGE_TILING_OPTIMAL,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600448 .usage = VK_IMAGE_USAGE_DEPTH_STENCIL_BIT,
Chia-I Wu9ae87c92014-10-07 14:15:01 +0800449 .flags = 0,
450 };
Courtney Goeltzenleuchterddcb6192015-04-14 18:48:46 -0600451 VkMemoryAllocInfo mem_alloc = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600452 .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOC_INFO,
Mark Lobodzinski97dcd042015-04-16 08:52:00 -0500453 .pNext = NULL,
Chia-I Wu9ae87c92014-10-07 14:15:01 +0800454 .allocationSize = 0,
Tony Barbour8205d902015-04-16 15:59:00 -0600455 .memProps = VK_MEMORY_PROPERTY_DEVICE_ONLY,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600456 .memPriority = VK_MEMORY_PRIORITY_NORMAL,
Chia-I Wu9ae87c92014-10-07 14:15:01 +0800457 };
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600458 VkDepthStencilViewCreateInfo view = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600459 .sType = VK_STRUCTURE_TYPE_DEPTH_STENCIL_VIEW_CREATE_INFO,
Chia-I Wu9ae87c92014-10-07 14:15:01 +0800460 .pNext = NULL,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600461 .image = VK_NULL_HANDLE,
Chia-I Wu9ae87c92014-10-07 14:15:01 +0800462 .mipLevel = 0,
463 .baseArraySlice = 0,
464 .arraySize = 1,
465 .flags = 0,
466 };
Jon Ashburna9ae3832015-01-16 09:37:43 -0700467
Mark Lobodzinski23182612015-05-29 09:32:35 -0500468 VkMemoryRequirements mem_reqs;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600469 size_t mem_reqs_size = sizeof(VkMemoryRequirements);
Tony Barbour22a30862015-04-22 09:02:32 -0600470 VkResult U_ASSERT_ONLY err;
Chia-I Wu9ae87c92014-10-07 14:15:01 +0800471
472 demo->depth.format = depth_format;
473
474 /* create image */
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600475 err = vkCreateImage(demo->device, &image,
Chia-I Wu9ae87c92014-10-07 14:15:01 +0800476 &demo->depth.image);
477 assert(!err);
478
Mike Stroyan230e6252015-04-17 12:36:38 -0600479 err = vkGetObjectInfo(demo->device,
480 VK_OBJECT_TYPE_IMAGE, demo->depth.image,
Tony Barbour8205d902015-04-16 15:59:00 -0600481 VK_OBJECT_INFO_TYPE_MEMORY_REQUIREMENTS,
Mark Lobodzinski23182612015-05-29 09:32:35 -0500482 &mem_reqs_size, &mem_reqs);
483 mem_alloc.allocationSize = mem_reqs.size;
Chia-I Wu9ae87c92014-10-07 14:15:01 +0800484
Mark Lobodzinski23182612015-05-29 09:32:35 -0500485 /* allocate memory */
486 err = vkAllocMemory(demo->device, &mem_alloc, &demo->depth.mem);
487 assert(!err);
Chia-I Wu9ae87c92014-10-07 14:15:01 +0800488
Mark Lobodzinski23182612015-05-29 09:32:35 -0500489 /* bind memory */
490 err = vkBindObjectMemory(demo->device,
491 VK_OBJECT_TYPE_IMAGE, demo->depth.image,
492 demo->depth.mem, 0);
493 assert(!err);
Chia-I Wu9ae87c92014-10-07 14:15:01 +0800494
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600495 demo_set_image_layout(demo, demo->depth.image,
malnasse4b8ba4d2015-06-03 17:28:38 -0400496 VK_IMAGE_ASPECT_DEPTH,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600497 VK_IMAGE_LAYOUT_UNDEFINED,
498 VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL);
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600499
Chia-I Wu9ae87c92014-10-07 14:15:01 +0800500 /* create image view */
501 view.image = demo->depth.image;
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600502 err = vkCreateDepthStencilView(demo->device, &view,
Chia-I Wu9ae87c92014-10-07 14:15:01 +0800503 &demo->depth.view);
504 assert(!err);
Chia-I Wu9ae87c92014-10-07 14:15:01 +0800505}
506
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700507static void demo_prepare_texture_image(struct demo *demo,
508 const uint32_t *tex_colors,
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600509 struct texture_object *tex_obj,
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600510 VkImageTiling tiling,
Courtney Goeltzenleuchtercb67a322015-04-21 09:31:23 -0600511 VkImageUsageFlags usage,
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600512 VkFlags mem_props)
Chia-I Wub043fe32014-10-06 15:30:33 +0800513{
Tony Barbour8205d902015-04-16 15:59:00 -0600514 const VkFormat tex_format = VK_FORMAT_B8G8R8A8_UNORM;
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600515 const int32_t tex_width = 2;
516 const int32_t tex_height = 2;
Tony Barbour22a30862015-04-22 09:02:32 -0600517 VkResult U_ASSERT_ONLY err;
Chia-I Wub043fe32014-10-06 15:30:33 +0800518
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600519 tex_obj->tex_width = tex_width;
520 tex_obj->tex_height = tex_height;
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700521
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600522 const VkImageCreateInfo image_create_info = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600523 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
Courtney Goeltzenleuchter3226a6f2015-02-11 18:17:22 -0700524 .pNext = NULL,
Tony Barbour8205d902015-04-16 15:59:00 -0600525 .imageType = VK_IMAGE_TYPE_2D,
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700526 .format = tex_format,
527 .extent = { tex_width, tex_height, 1 },
528 .mipLevels = 1,
529 .arraySize = 1,
530 .samples = 1,
531 .tiling = tiling,
Courtney Goeltzenleuchtercb67a322015-04-21 09:31:23 -0600532 .usage = usage,
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700533 .flags = 0,
534 };
Courtney Goeltzenleuchterddcb6192015-04-14 18:48:46 -0600535 VkMemoryAllocInfo mem_alloc = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600536 .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOC_INFO,
Mark Lobodzinski97dcd042015-04-16 08:52:00 -0500537 .pNext = NULL,
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700538 .allocationSize = 0,
539 .memProps = mem_props,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600540 .memPriority = VK_MEMORY_PRIORITY_NORMAL,
Courtney Goeltzenleuchter3226a6f2015-02-11 18:17:22 -0700541 };
542
Mark Lobodzinski23182612015-05-29 09:32:35 -0500543 VkMemoryRequirements mem_reqs;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600544 size_t mem_reqs_size = sizeof(VkMemoryRequirements);
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700545
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600546 err = vkCreateImage(demo->device, &image_create_info,
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600547 &tex_obj->image);
Courtney Goeltzenleuchter3226a6f2015-02-11 18:17:22 -0700548 assert(!err);
549
Mike Stroyan230e6252015-04-17 12:36:38 -0600550 err = vkGetObjectInfo(demo->device,
551 VK_OBJECT_TYPE_IMAGE, tex_obj->image,
Tony Barbour8205d902015-04-16 15:59:00 -0600552 VK_OBJECT_INFO_TYPE_MEMORY_REQUIREMENTS,
Mark Lobodzinski23182612015-05-29 09:32:35 -0500553 &mem_reqs_size, &mem_reqs);
554 mem_alloc.allocationSize = mem_reqs.size;
Courtney Goeltzenleuchter3226a6f2015-02-11 18:17:22 -0700555
Mark Lobodzinski23182612015-05-29 09:32:35 -0500556 /* allocate memory */
557 err = vkAllocMemory(demo->device, &mem_alloc, &tex_obj->mem);
558 assert(!err);
Courtney Goeltzenleuchter3226a6f2015-02-11 18:17:22 -0700559
Mark Lobodzinski23182612015-05-29 09:32:35 -0500560 /* bind memory */
561 err = vkBindObjectMemory(demo->device,
562 VK_OBJECT_TYPE_IMAGE, tex_obj->image,
563 tex_obj->mem, 0);
564 assert(!err);
Courtney Goeltzenleuchter3226a6f2015-02-11 18:17:22 -0700565
Tony Barbour8205d902015-04-16 15:59:00 -0600566 if (mem_props & VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT) {
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600567 const VkImageSubresource subres = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600568 .aspect = VK_IMAGE_ASPECT_COLOR,
Courtney Goeltzenleuchter3226a6f2015-02-11 18:17:22 -0700569 .mipLevel = 0,
570 .arraySlice = 0,
571 };
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600572 VkSubresourceLayout layout;
573 size_t layout_size = sizeof(VkSubresourceLayout);
Courtney Goeltzenleuchter3226a6f2015-02-11 18:17:22 -0700574 void *data;
575 int32_t x, y;
576
Mike Stroyan230e6252015-04-17 12:36:38 -0600577 err = vkGetImageSubresourceInfo(demo->device, tex_obj->image, &subres,
Tony Barbour8205d902015-04-16 15:59:00 -0600578 VK_SUBRESOURCE_INFO_TYPE_LAYOUT,
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700579 &layout_size, &layout);
Courtney Goeltzenleuchter3226a6f2015-02-11 18:17:22 -0700580 assert(!err && layout_size == sizeof(layout));
Courtney Goeltzenleuchter3226a6f2015-02-11 18:17:22 -0700581
Mark Lobodzinski23182612015-05-29 09:32:35 -0500582 err = vkMapMemory(demo->device, tex_obj->mem, 0, 0, 0, &data);
Courtney Goeltzenleuchter3226a6f2015-02-11 18:17:22 -0700583 assert(!err);
584
585 for (y = 0; y < tex_height; y++) {
586 uint32_t *row = (uint32_t *) ((char *) data + layout.rowPitch * y);
587 for (x = 0; x < tex_width; x++)
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700588 row[x] = tex_colors[(x & 1) ^ (y & 1)];
Courtney Goeltzenleuchter3226a6f2015-02-11 18:17:22 -0700589 }
590
Mark Lobodzinski23182612015-05-29 09:32:35 -0500591 err = vkUnmapMemory(demo->device, tex_obj->mem);
Courtney Goeltzenleuchter3226a6f2015-02-11 18:17:22 -0700592 assert(!err);
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700593 }
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600594
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600595 tex_obj->imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600596 demo_set_image_layout(demo, tex_obj->image,
malnasse4b8ba4d2015-06-03 17:28:38 -0400597 VK_IMAGE_ASPECT_COLOR,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600598 VK_IMAGE_LAYOUT_UNDEFINED,
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600599 tex_obj->imageLayout);
600 /* setting the image layout does not reference the actual memory so no need to add a mem ref */
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700601}
602
Mark Lobodzinskicf26e072015-04-16 11:44:05 -0500603static void demo_destroy_texture_image(struct demo *demo, struct texture_object *tex_obj)
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700604{
605 /* clean up staging resources */
Mike Stroyan230e6252015-04-17 12:36:38 -0600606 vkDestroyObject(demo->device, VK_OBJECT_TYPE_IMAGE, tex_obj->image);
Courtney Goeltzenleuchtera063d9b2015-06-10 16:16:22 -0600607 vkFreeMemory(demo->device, tex_obj->mem);
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700608}
609
610static void demo_prepare_textures(struct demo *demo)
611{
Tony Barbour8205d902015-04-16 15:59:00 -0600612 const VkFormat tex_format = VK_FORMAT_B8G8R8A8_UNORM;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600613 VkFormatProperties props;
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700614 size_t size = sizeof(props);
615 const uint32_t tex_colors[DEMO_TEXTURE_COUNT][2] = {
616 { 0xffff0000, 0xff00ff00 },
617 };
Tony Barbour22a30862015-04-22 09:02:32 -0600618 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700619 uint32_t i;
620
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600621 err = vkGetFormatInfo(demo->device, tex_format,
Tony Barbour8205d902015-04-16 15:59:00 -0600622 VK_FORMAT_INFO_TYPE_PROPERTIES,
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700623 &size, &props);
624 assert(!err);
625
626 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Tony Barbour8205d902015-04-16 15:59:00 -0600627 if ((props.linearTilingFeatures & VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT) && !demo->use_staging_buffer) {
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700628 /* Device can texture using linear textures */
629 demo_prepare_texture_image(demo, tex_colors[i], &demo->textures[i],
Courtney Goeltzenleuchtercb67a322015-04-21 09:31:23 -0600630 VK_IMAGE_TILING_LINEAR, VK_IMAGE_USAGE_SAMPLED_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT);
Tony Barbour8205d902015-04-16 15:59:00 -0600631 } else if (props.optimalTilingFeatures & VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT){
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700632 /* Must use staging buffer to copy linear texture to optimized */
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600633 struct texture_object staging_texture;
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700634
635 memset(&staging_texture, 0, sizeof(staging_texture));
636 demo_prepare_texture_image(demo, tex_colors[i], &staging_texture,
Courtney Goeltzenleuchtercb67a322015-04-21 09:31:23 -0600637 VK_IMAGE_TILING_LINEAR, VK_IMAGE_USAGE_TRANSFER_SOURCE_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT);
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700638
639 demo_prepare_texture_image(demo, tex_colors[i], &demo->textures[i],
Courtney Goeltzenleuchtercb67a322015-04-21 09:31:23 -0600640 VK_IMAGE_TILING_OPTIMAL,
641 (VK_IMAGE_USAGE_TRANSFER_DESTINATION_BIT | VK_IMAGE_USAGE_SAMPLED_BIT),
642 VK_MEMORY_PROPERTY_DEVICE_ONLY);
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700643
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600644 demo_set_image_layout(demo, staging_texture.image,
malnasse4b8ba4d2015-06-03 17:28:38 -0400645 VK_IMAGE_ASPECT_COLOR,
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600646 staging_texture.imageLayout,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600647 VK_IMAGE_LAYOUT_TRANSFER_SOURCE_OPTIMAL);
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700648
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600649 demo_set_image_layout(demo, demo->textures[i].image,
malnasse4b8ba4d2015-06-03 17:28:38 -0400650 VK_IMAGE_ASPECT_COLOR,
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600651 demo->textures[i].imageLayout,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600652 VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL);
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700653
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600654 VkImageCopy copy_region = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600655 .srcSubresource = { VK_IMAGE_ASPECT_COLOR, 0, 0 },
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700656 .srcOffset = { 0, 0, 0 },
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600657 .destSubresource = { VK_IMAGE_ASPECT_COLOR, 0, 0 },
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700658 .destOffset = { 0, 0, 0 },
659 .extent = { staging_texture.tex_width, staging_texture.tex_height, 1 },
660 };
Courtney Goeltzenleuchter633f9c52015-04-30 10:58:33 -0600661 vkCmdCopyImage(demo->setup_cmd,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600662 staging_texture.image, VK_IMAGE_LAYOUT_TRANSFER_SOURCE_OPTIMAL,
663 demo->textures[i].image, VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL,
Courtney Goeltzenleuchter51cbf302015-03-25 11:25:10 -0600664 1, &copy_region);
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700665
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600666 demo_set_image_layout(demo, demo->textures[i].image,
malnasse4b8ba4d2015-06-03 17:28:38 -0400667 VK_IMAGE_ASPECT_COLOR,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600668 VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL,
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600669 demo->textures[i].imageLayout);
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700670
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600671 demo_flush_init_cmd(demo);
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700672
Courtney Goeltzenleuchter876629f2015-04-21 09:30:03 -0600673 demo_destroy_texture_image(demo, &staging_texture);
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700674 } else {
Tony Barbour8205d902015-04-16 15:59:00 -0600675 /* Can't support VK_FORMAT_B8G8R8A8_UNORM !? */
Piers Daniell886be472015-02-23 16:23:13 -0700676 assert(!"No support for B8G8R8A8_UNORM as texture image format");
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700677 }
Courtney Goeltzenleuchter3226a6f2015-02-11 18:17:22 -0700678
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600679 const VkSamplerCreateInfo sampler = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600680 .sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO,
Chia-I Wub043fe32014-10-06 15:30:33 +0800681 .pNext = NULL,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600682 .magFilter = VK_TEX_FILTER_NEAREST,
683 .minFilter = VK_TEX_FILTER_NEAREST,
Tony Barbour8205d902015-04-16 15:59:00 -0600684 .mipMode = VK_TEX_MIPMAP_MODE_BASE,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600685 .addressU = VK_TEX_ADDRESS_WRAP,
686 .addressV = VK_TEX_ADDRESS_WRAP,
687 .addressW = VK_TEX_ADDRESS_WRAP,
Chia-I Wub043fe32014-10-06 15:30:33 +0800688 .mipLodBias = 0.0f,
Courtney Goeltzenleuchterbc9c8162015-02-13 18:20:24 -0700689 .maxAnisotropy = 1,
Tony Barbour8205d902015-04-16 15:59:00 -0600690 .compareOp = VK_COMPARE_OP_NEVER,
Chia-I Wub043fe32014-10-06 15:30:33 +0800691 .minLod = 0.0f,
692 .maxLod = 0.0f,
Tony Barbour8205d902015-04-16 15:59:00 -0600693 .borderColor = VK_BORDER_COLOR_OPAQUE_WHITE,
Chia-I Wub043fe32014-10-06 15:30:33 +0800694 };
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600695 VkImageViewCreateInfo view = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600696 .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
Chia-I Wub043fe32014-10-06 15:30:33 +0800697 .pNext = NULL,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600698 .image = VK_NULL_HANDLE,
Tony Barbour8205d902015-04-16 15:59:00 -0600699 .viewType = VK_IMAGE_VIEW_TYPE_2D,
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700700 .format = tex_format,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600701 .channels = { VK_CHANNEL_SWIZZLE_R,
702 VK_CHANNEL_SWIZZLE_G,
703 VK_CHANNEL_SWIZZLE_B,
704 VK_CHANNEL_SWIZZLE_A, },
705 .subresourceRange = { VK_IMAGE_ASPECT_COLOR, 0, 1, 0, 1 },
Chia-I Wub043fe32014-10-06 15:30:33 +0800706 .minLod = 0.0f,
707 };
Jon Ashburna9ae3832015-01-16 09:37:43 -0700708
Chia-I Wub043fe32014-10-06 15:30:33 +0800709 /* create sampler */
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600710 err = vkCreateSampler(demo->device, &sampler,
Chia-I Wub043fe32014-10-06 15:30:33 +0800711 &demo->textures[i].sampler);
712 assert(!err);
713
Chia-I Wub043fe32014-10-06 15:30:33 +0800714 /* create image view */
715 view.image = demo->textures[i].image;
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600716 err = vkCreateImageView(demo->device, &view,
Courtney Goeltzenleuchter3226a6f2015-02-11 18:17:22 -0700717 &demo->textures[i].view);
Chia-I Wub043fe32014-10-06 15:30:33 +0800718 assert(!err);
Chia-I Wub043fe32014-10-06 15:30:33 +0800719 }
720}
721
Chia-I Wu99621bc2014-10-08 11:52:22 +0800722static void demo_prepare_vertices(struct demo *demo)
723{
724 const float vb[3][5] = {
725 /* position texcoord */
Chia-I Wue2504cb2015-04-22 14:20:52 +0800726 { -1.0f, -1.0f, 0.2f, 0.0f, 0.0f },
727 { 1.0f, -1.0f, 0.25f, 1.0f, 0.0f },
Chia-I Wu99621bc2014-10-08 11:52:22 +0800728 { 0.0f, 1.0f, 1.0f, 0.5f, 1.0f },
729 };
Courtney Goeltzenleuchterddcb6192015-04-14 18:48:46 -0600730 const VkBufferCreateInfo buf_info = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600731 .sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO,
Chia-I Wu714df452015-01-01 07:55:04 +0800732 .pNext = NULL,
733 .size = sizeof(vb),
Courtney Goeltzenleuchterad870812015-04-15 15:29:59 -0600734 .usage = VK_BUFFER_USAGE_VERTEX_BUFFER_BIT,
Chia-I Wu714df452015-01-01 07:55:04 +0800735 .flags = 0,
736 };
Courtney Goeltzenleuchterddcb6192015-04-14 18:48:46 -0600737 VkMemoryAllocInfo mem_alloc = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600738 .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOC_INFO,
Mark Lobodzinski97dcd042015-04-16 08:52:00 -0500739 .pNext = NULL,
Chia-I Wu714df452015-01-01 07:55:04 +0800740 .allocationSize = 0,
Tony Barbour8205d902015-04-16 15:59:00 -0600741 .memProps = VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600742 .memPriority = VK_MEMORY_PRIORITY_NORMAL,
Chia-I Wu99621bc2014-10-08 11:52:22 +0800743 };
Mark Lobodzinski23182612015-05-29 09:32:35 -0500744 VkMemoryRequirements mem_reqs;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600745 size_t mem_reqs_size = sizeof(VkMemoryRequirements);
Tony Barbour22a30862015-04-22 09:02:32 -0600746 VkResult U_ASSERT_ONLY err;
Chia-I Wu99621bc2014-10-08 11:52:22 +0800747 void *data;
748
749 memset(&demo->vertices, 0, sizeof(demo->vertices));
750
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600751 err = vkCreateBuffer(demo->device, &buf_info, &demo->vertices.buf);
Chia-I Wu714df452015-01-01 07:55:04 +0800752 assert(!err);
753
Mike Stroyan230e6252015-04-17 12:36:38 -0600754 err = vkGetObjectInfo(demo->device,
Mike Stroyan230e6252015-04-17 12:36:38 -0600755 VK_OBJECT_TYPE_BUFFER, demo->vertices.buf,
Tony Barbour8205d902015-04-16 15:59:00 -0600756 VK_OBJECT_INFO_TYPE_MEMORY_REQUIREMENTS,
Mark Lobodzinski23182612015-05-29 09:32:35 -0500757 &mem_reqs_size, &mem_reqs);
Chia-I Wu714df452015-01-01 07:55:04 +0800758
Mark Lobodzinski23182612015-05-29 09:32:35 -0500759 mem_alloc.allocationSize = mem_reqs.size;
Chia-I Wu99621bc2014-10-08 11:52:22 +0800760
Mark Lobodzinski23182612015-05-29 09:32:35 -0500761 err = vkAllocMemory(demo->device, &mem_alloc, &demo->vertices.mem);
762 assert(!err);
Chia-I Wu99621bc2014-10-08 11:52:22 +0800763
Mark Lobodzinski23182612015-05-29 09:32:35 -0500764 err = vkMapMemory(demo->device, demo->vertices.mem, 0, 0, 0, &data);
765 assert(!err);
Chia-I Wu99621bc2014-10-08 11:52:22 +0800766
Mark Lobodzinski23182612015-05-29 09:32:35 -0500767 memcpy(data, vb, sizeof(vb));
Chia-I Wu99621bc2014-10-08 11:52:22 +0800768
Mark Lobodzinski23182612015-05-29 09:32:35 -0500769 err = vkUnmapMemory(demo->device, demo->vertices.mem);
770 assert(!err);
771
772 err = vkBindObjectMemory(demo->device,
773 VK_OBJECT_TYPE_BUFFER, demo->vertices.buf,
774 demo->vertices.mem, 0);
775 assert(!err);
Chia-I Wu714df452015-01-01 07:55:04 +0800776
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600777 demo->vertices.vi.sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_CREATE_INFO;
Chia-I Wu8d29d022014-10-08 12:14:39 +0800778 demo->vertices.vi.pNext = NULL;
779 demo->vertices.vi.bindingCount = 1;
780 demo->vertices.vi.pVertexBindingDescriptions = demo->vertices.vi_bindings;
781 demo->vertices.vi.attributeCount = 2;
782 demo->vertices.vi.pVertexAttributeDescriptions = demo->vertices.vi_attrs;
783
Courtney Goeltzenleuchterf5cdad02015-03-31 16:36:30 -0600784 demo->vertices.vi_bindings[0].binding = VERTEX_BUFFER_BIND_ID;
Chia-I Wu8d29d022014-10-08 12:14:39 +0800785 demo->vertices.vi_bindings[0].strideInBytes = sizeof(vb[0]);
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600786 demo->vertices.vi_bindings[0].stepRate = VK_VERTEX_INPUT_STEP_RATE_VERTEX;
Chia-I Wu8d29d022014-10-08 12:14:39 +0800787
Courtney Goeltzenleuchterf5cdad02015-03-31 16:36:30 -0600788 demo->vertices.vi_attrs[0].binding = VERTEX_BUFFER_BIND_ID;
789 demo->vertices.vi_attrs[0].location = 0;
Tony Barbour8205d902015-04-16 15:59:00 -0600790 demo->vertices.vi_attrs[0].format = VK_FORMAT_R32G32B32_SFLOAT;
Chia-I Wu8d29d022014-10-08 12:14:39 +0800791 demo->vertices.vi_attrs[0].offsetInBytes = 0;
792
Courtney Goeltzenleuchterf5cdad02015-03-31 16:36:30 -0600793 demo->vertices.vi_attrs[1].binding = VERTEX_BUFFER_BIND_ID;
794 demo->vertices.vi_attrs[1].location = 1;
Tony Barbour8205d902015-04-16 15:59:00 -0600795 demo->vertices.vi_attrs[1].format = VK_FORMAT_R32G32_SFLOAT;
Chia-I Wu8d29d022014-10-08 12:14:39 +0800796 demo->vertices.vi_attrs[1].offsetInBytes = sizeof(float) * 3;
Chia-I Wu99621bc2014-10-08 11:52:22 +0800797}
798
Chia-I Wuf8385062015-01-04 16:27:24 +0800799static void demo_prepare_descriptor_layout(struct demo *demo)
Chia-I Wub043fe32014-10-06 15:30:33 +0800800{
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600801 const VkDescriptorSetLayoutBinding layout_binding = {
Courtney Goeltzenleuchterad870812015-04-15 15:29:59 -0600802 .descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
Chia-I Wud3114a22015-05-25 16:22:52 +0800803 .arraySize = DEMO_TEXTURE_COUNT,
Tony Barbour8205d902015-04-16 15:59:00 -0600804 .stageFlags = VK_SHADER_STAGE_FRAGMENT_BIT,
Chia-I Wu310eece2015-03-27 12:56:09 +0800805 .pImmutableSamplers = NULL,
Chia-I Wub043fe32014-10-06 15:30:33 +0800806 };
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600807 const VkDescriptorSetLayoutCreateInfo descriptor_layout = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600808 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO,
Chia-I Wufc9d9132015-03-26 15:04:41 +0800809 .pNext = NULL,
810 .count = 1,
811 .pBinding = &layout_binding,
812 };
Tony Barbour22a30862015-04-22 09:02:32 -0600813 VkResult U_ASSERT_ONLY err;
Chia-I Wub043fe32014-10-06 15:30:33 +0800814
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600815 err = vkCreateDescriptorSetLayout(demo->device,
Chia-I Wu7732cb22015-03-26 15:27:55 +0800816 &descriptor_layout, &demo->desc_layout);
817 assert(!err);
818
Mark Lobodzinski556f7212015-04-17 14:11:39 -0500819 const VkPipelineLayoutCreateInfo pPipelineLayoutCreateInfo = {
820 .sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO,
821 .pNext = NULL,
822 .descriptorSetCount = 1,
823 .pSetLayouts = &demo->desc_layout,
824 };
825
826 err = vkCreatePipelineLayout(demo->device,
827 &pPipelineLayoutCreateInfo,
828 &demo->pipeline_layout);
Chia-I Wub043fe32014-10-06 15:30:33 +0800829 assert(!err);
Chia-I Wub043fe32014-10-06 15:30:33 +0800830}
831
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600832static VkShader demo_prepare_shader(struct demo *demo,
Tony Barbour8205d902015-04-16 15:59:00 -0600833 VkShaderStage stage,
Chia-I Wuc19795a2014-09-13 11:12:55 +0800834 const void *code,
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600835 size_t size)
Chia-I Wuc19795a2014-09-13 11:12:55 +0800836{
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600837 VkShaderCreateInfo createInfo;
838 VkShader shader;
839 VkResult err;
Chia-I Wuc19795a2014-09-13 11:12:55 +0800840
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600841 createInfo.sType = VK_STRUCTURE_TYPE_SHADER_CREATE_INFO;
Courtney Goeltzenleuchter3f2606d2014-10-13 17:51:58 -0600842 createInfo.pNext = NULL;
843
Cody Northrop75db0322015-05-28 11:27:16 -0600844 if (!demo->use_glsl) {
845 createInfo.codeSize = size;
846 createInfo.pCode = code;
847 createInfo.flags = 0;
Courtney Goeltzenleuchter3f2606d2014-10-13 17:51:58 -0600848
Cody Northrop75db0322015-05-28 11:27:16 -0600849 err = vkCreateShader(demo->device, &createInfo, &shader);
850 if (err) {
851 free((void *)createInfo.pCode);
852 }
853 } else {
854 // Create fake SPV structure to feed GLSL
855 // to the driver "under the covers"
856 createInfo.codeSize = 3 * sizeof(uint32_t) + size + 1;
857 createInfo.pCode = malloc(createInfo.codeSize);
858 createInfo.flags = 0;
Courtney Goeltzenleuchter3f2606d2014-10-13 17:51:58 -0600859
Cody Northrop75db0322015-05-28 11:27:16 -0600860 /* try version 0 first: VkShaderStage followed by GLSL */
861 ((uint32_t *) createInfo.pCode)[0] = ICD_SPV_MAGIC;
862 ((uint32_t *) createInfo.pCode)[1] = 0;
863 ((uint32_t *) createInfo.pCode)[2] = stage;
864 memcpy(((uint32_t *) createInfo.pCode + 3), code, size + 1);
865
866 err = vkCreateShader(demo->device, &createInfo, &shader);
867 if (err) {
868 free((void *) createInfo.pCode);
869 return VK_NULL_HANDLE;
870 }
Courtney Goeltzenleuchter3f2606d2014-10-13 17:51:58 -0600871 }
Chia-I Wuc19795a2014-09-13 11:12:55 +0800872
873 return shader;
874}
875
Cody Northrop75db0322015-05-28 11:27:16 -0600876char *demo_read_spv(const char *filename, size_t *psize)
877{
878 long int size;
879 void *shader_code;
880
881 FILE *fp = fopen(filename, "rb");
882 if (!fp) return NULL;
883
884 fseek(fp, 0L, SEEK_END);
885 size = ftell(fp);
886
887 fseek(fp, 0L, SEEK_SET);
888
889 shader_code = malloc(size);
890 fread(shader_code, size, 1, fp);
891
892 *psize = size;
893
894 return shader_code;
895}
896
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600897static VkShader demo_prepare_vs(struct demo *demo)
Chia-I Wuc19795a2014-09-13 11:12:55 +0800898{
Cody Northrop75db0322015-05-28 11:27:16 -0600899 if (!demo->use_glsl) {
900 void *vertShaderCode;
901 size_t size;
902
903 vertShaderCode = demo_read_spv("tri-vert.spv", &size);
904
905 return demo_prepare_shader(demo, VK_SHADER_STAGE_VERTEX,
906 vertShaderCode, size);
907 } else {
908 static const char *vertShaderText =
Mark Lobodzinskiba4d2f02015-04-06 15:24:40 -0500909 "#version 140\n"
Courtney Goeltzenleuchterf5cdad02015-03-31 16:36:30 -0600910 "#extension GL_ARB_separate_shader_objects : enable\n"
911 "#extension GL_ARB_shading_language_420pack : enable\n"
912 "layout (location = 0) in vec4 pos;\n"
913 "layout (location = 1) in vec2 attr;\n"
Chia-I Wuf5caeb02014-10-25 12:11:27 +0800914 "out vec2 texcoord;\n"
Courtney Goeltzenleuchter3f2606d2014-10-13 17:51:58 -0600915 "void main() {\n"
Chia-I Wuf5caeb02014-10-25 12:11:27 +0800916 " texcoord = attr;\n"
917 " gl_Position = pos;\n"
Courtney Goeltzenleuchter3f2606d2014-10-13 17:51:58 -0600918 "}\n";
Courtney Goeltzenleuchteref7301b2014-09-17 13:17:12 -0600919
Cody Northrop75db0322015-05-28 11:27:16 -0600920 return demo_prepare_shader(demo, VK_SHADER_STAGE_VERTEX,
921 (const void *) vertShaderText,
922 strlen(vertShaderText));
923 }
Chia-I Wuc19795a2014-09-13 11:12:55 +0800924}
925
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600926static VkShader demo_prepare_fs(struct demo *demo)
Chia-I Wuc19795a2014-09-13 11:12:55 +0800927{
Cody Northrop75db0322015-05-28 11:27:16 -0600928 if (!demo->use_glsl) {
929 void *fragShaderCode;
930 size_t size;
Courtney Goeltzenleuchteref7301b2014-09-17 13:17:12 -0600931
Cody Northrop75db0322015-05-28 11:27:16 -0600932 fragShaderCode = demo_read_spv("tri-frag.spv", &size);
933
934 return demo_prepare_shader(demo, VK_SHADER_STAGE_FRAGMENT,
935 fragShaderCode, size);
936 } else {
937 static const char *fragShaderText =
938 "#version 140\n"
939 "#extension GL_ARB_separate_shader_objects : enable\n"
940 "#extension GL_ARB_shading_language_420pack : enable\n"
941 "layout (binding = 0) uniform sampler2D tex;\n"
942 "layout (location = 0) in vec2 texcoord;\n"
943 "layout (location = 0) out vec4 uFragColor;\n"
944 "void main() {\n"
945 " uFragColor = texture(tex, texcoord);\n"
946 "}\n";
947
948 return demo_prepare_shader(demo, VK_SHADER_STAGE_FRAGMENT,
949 (const void *) fragShaderText,
950 strlen(fragShaderText));
951 }
Chia-I Wuc19795a2014-09-13 11:12:55 +0800952}
953
954static void demo_prepare_pipeline(struct demo *demo)
955{
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600956 VkGraphicsPipelineCreateInfo pipeline;
957 VkPipelineVertexInputCreateInfo vi;
958 VkPipelineIaStateCreateInfo ia;
959 VkPipelineRsStateCreateInfo rs;
960 VkPipelineCbStateCreateInfo cb;
961 VkPipelineDsStateCreateInfo ds;
962 VkPipelineShaderStageCreateInfo vs;
963 VkPipelineShaderStageCreateInfo fs;
964 VkPipelineVpStateCreateInfo vp;
965 VkPipelineMsStateCreateInfo ms;
Tony Barbour22a30862015-04-22 09:02:32 -0600966 VkResult U_ASSERT_ONLY err;
Chia-I Wuc19795a2014-09-13 11:12:55 +0800967
968 memset(&pipeline, 0, sizeof(pipeline));
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600969 pipeline.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
Mark Lobodzinski556f7212015-04-17 14:11:39 -0500970 pipeline.layout = demo->pipeline_layout;
Chia-I Wuc19795a2014-09-13 11:12:55 +0800971
Chia-I Wu8d29d022014-10-08 12:14:39 +0800972 vi = demo->vertices.vi;
973
Chia-I Wuc19795a2014-09-13 11:12:55 +0800974 memset(&ia, 0, sizeof(ia));
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600975 ia.sType = VK_STRUCTURE_TYPE_PIPELINE_IA_STATE_CREATE_INFO;
Tony Barbour8205d902015-04-16 15:59:00 -0600976 ia.topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
Chia-I Wuc19795a2014-09-13 11:12:55 +0800977
978 memset(&rs, 0, sizeof(rs));
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600979 rs.sType = VK_STRUCTURE_TYPE_PIPELINE_RS_STATE_CREATE_INFO;
Tony Barbour8205d902015-04-16 15:59:00 -0600980 rs.fillMode = VK_FILL_MODE_SOLID;
Chia-I Wuc414ba82015-04-22 15:44:24 +0800981 rs.cullMode = VK_CULL_MODE_BACK;
982 rs.frontFace = VK_FRONT_FACE_CW;
Chia-I Wue2504cb2015-04-22 14:20:52 +0800983 rs.depthClipEnable = VK_TRUE;
Chia-I Wuc19795a2014-09-13 11:12:55 +0800984
985 memset(&cb, 0, sizeof(cb));
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600986 cb.sType = VK_STRUCTURE_TYPE_PIPELINE_CB_STATE_CREATE_INFO;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600987 VkPipelineCbAttachmentState att_state[1];
Tony Barbourfa6cac72015-01-16 14:27:35 -0700988 memset(att_state, 0, sizeof(att_state));
989 att_state[0].format = demo->format;
990 att_state[0].channelWriteMask = 0xf;
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600991 att_state[0].blendEnable = VK_FALSE;
Tony Barbourfa6cac72015-01-16 14:27:35 -0700992 cb.attachmentCount = 1;
993 cb.pAttachments = att_state;
Chia-I Wuc19795a2014-09-13 11:12:55 +0800994
Tony Barbourfa6cac72015-01-16 14:27:35 -0700995
996 memset(&vp, 0, sizeof(vp));
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600997 vp.sType = VK_STRUCTURE_TYPE_PIPELINE_VP_STATE_CREATE_INFO;
Tony Barbour8205d902015-04-16 15:59:00 -0600998 vp.viewportCount = 1;
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600999 vp.clipOrigin = VK_COORDINATE_ORIGIN_UPPER_LEFT;
Tony Barbourfa6cac72015-01-16 14:27:35 -07001000
1001 memset(&ds, 0, sizeof(ds));
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001002 ds.sType = VK_STRUCTURE_TYPE_PIPELINE_DS_STATE_CREATE_INFO;
Tony Barbourfa6cac72015-01-16 14:27:35 -07001003 ds.format = demo->depth.format;
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001004 ds.depthTestEnable = VK_TRUE;
1005 ds.depthWriteEnable = VK_TRUE;
Tony Barbour8205d902015-04-16 15:59:00 -06001006 ds.depthCompareOp = VK_COMPARE_OP_LESS_EQUAL;
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001007 ds.depthBoundsEnable = VK_FALSE;
1008 ds.back.stencilFailOp = VK_STENCIL_OP_KEEP;
1009 ds.back.stencilPassOp = VK_STENCIL_OP_KEEP;
Tony Barbour8205d902015-04-16 15:59:00 -06001010 ds.back.stencilCompareOp = VK_COMPARE_OP_ALWAYS;
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001011 ds.stencilTestEnable = VK_FALSE;
Tony Barbourfa6cac72015-01-16 14:27:35 -07001012 ds.front = ds.back;
Chia-I Wuc19795a2014-09-13 11:12:55 +08001013
Chia-I Wu99621bc2014-10-08 11:52:22 +08001014 memset(&vs, 0, sizeof(vs));
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001015 vs.sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
1016 vs.shader.stage = VK_SHADER_STAGE_VERTEX;
Chia-I Wu99621bc2014-10-08 11:52:22 +08001017 vs.shader.shader = demo_prepare_vs(demo);
Tony Barbourfa6cac72015-01-16 14:27:35 -07001018 vs.shader.linkConstBufferCount = 0;
Chia-I Wu99621bc2014-10-08 11:52:22 +08001019
1020 memset(&fs, 0, sizeof(fs));
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001021 fs.sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
1022 fs.shader.stage = VK_SHADER_STAGE_FRAGMENT;
Chia-I Wu99621bc2014-10-08 11:52:22 +08001023 fs.shader.shader = demo_prepare_fs(demo);
Tony Barbourfa6cac72015-01-16 14:27:35 -07001024
1025 memset(&ms, 0, sizeof(ms));
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001026 ms.sType = VK_STRUCTURE_TYPE_PIPELINE_MS_STATE_CREATE_INFO;
Tony Barbourfa6cac72015-01-16 14:27:35 -07001027 ms.sampleMask = 1;
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001028 ms.multisampleEnable = VK_FALSE;
Tony Barbourfa6cac72015-01-16 14:27:35 -07001029 ms.samples = 1;
Chia-I Wub043fe32014-10-06 15:30:33 +08001030
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -06001031 pipeline.pNext = (const void *) &vi;
1032 vi.pNext = (void *) &ia;
1033 ia.pNext = (const void *) &rs;
1034 rs.pNext = (const void *) &cb;
1035 cb.pNext = (const void *) &ms;
1036 ms.pNext = (const void *) &vp;
1037 vp.pNext = (const void *) &ds;
1038 ds.pNext = (const void *) &vs;
1039 vs.pNext = (const void *) &fs;
Chia-I Wuc19795a2014-09-13 11:12:55 +08001040
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001041 err = vkCreateGraphicsPipeline(demo->device, &pipeline, &demo->pipeline);
Chia-I Wuc19795a2014-09-13 11:12:55 +08001042 assert(!err);
1043
Mike Stroyan230e6252015-04-17 12:36:38 -06001044 vkDestroyObject(demo->device, VK_OBJECT_TYPE_SHADER, vs.shader.shader);
1045 vkDestroyObject(demo->device, VK_OBJECT_TYPE_SHADER, fs.shader.shader);
Chia-I Wuc19795a2014-09-13 11:12:55 +08001046}
1047
1048static void demo_prepare_dynamic_states(struct demo *demo)
1049{
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001050 VkDynamicVpStateCreateInfo viewport_create;
1051 VkDynamicRsStateCreateInfo raster;
1052 VkDynamicCbStateCreateInfo color_blend;
1053 VkDynamicDsStateCreateInfo depth_stencil;
Tony Barbour22a30862015-04-22 09:02:32 -06001054 VkResult U_ASSERT_ONLY err;
Chia-I Wuc19795a2014-09-13 11:12:55 +08001055
Tony Barbourfa6cac72015-01-16 14:27:35 -07001056 memset(&viewport_create, 0, sizeof(viewport_create));
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001057 viewport_create.sType = VK_STRUCTURE_TYPE_DYNAMIC_VP_STATE_CREATE_INFO;
Courtney Goeltzenleuchterc6e32f92015-02-11 14:13:34 -07001058 viewport_create.viewportAndScissorCount = 1;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001059 VkViewport viewport;
Chia-I Wuc19795a2014-09-13 11:12:55 +08001060 memset(&viewport, 0, sizeof(viewport));
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -06001061 viewport.height = (float) demo->height;
Courtney Goeltzenleuchterc6e32f92015-02-11 14:13:34 -07001062 viewport.width = (float) demo->width;
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -06001063 viewport.minDepth = (float) 0.0f;
1064 viewport.maxDepth = (float) 1.0f;
Piers Daniell886be472015-02-23 16:23:13 -07001065 viewport_create.pViewports = &viewport;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001066 VkRect scissor;
Piers Daniell886be472015-02-23 16:23:13 -07001067 memset(&scissor, 0, sizeof(scissor));
Courtney Goeltzenleuchterc6e32f92015-02-11 14:13:34 -07001068 scissor.extent.width = demo->width;
1069 scissor.extent.height = demo->height;
1070 scissor.offset.x = 0;
1071 scissor.offset.y = 0;
Courtney Goeltzenleuchterc6e32f92015-02-11 14:13:34 -07001072 viewport_create.pScissors = &scissor;
Chia-I Wuc19795a2014-09-13 11:12:55 +08001073
1074 memset(&raster, 0, sizeof(raster));
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001075 raster.sType = VK_STRUCTURE_TYPE_DYNAMIC_RS_STATE_CREATE_INFO;
Piers Daniell886be472015-02-23 16:23:13 -07001076 raster.pointSize = 1.0;
1077 raster.lineWidth = 1.0;
Chia-I Wuc19795a2014-09-13 11:12:55 +08001078
1079 memset(&color_blend, 0, sizeof(color_blend));
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001080 color_blend.sType = VK_STRUCTURE_TYPE_DYNAMIC_CB_STATE_CREATE_INFO;
Piers Daniell886be472015-02-23 16:23:13 -07001081 color_blend.blendConst[0] = 1.0f;
1082 color_blend.blendConst[1] = 1.0f;
1083 color_blend.blendConst[2] = 1.0f;
1084 color_blend.blendConst[3] = 1.0f;
Chia-I Wuc19795a2014-09-13 11:12:55 +08001085
1086 memset(&depth_stencil, 0, sizeof(depth_stencil));
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001087 depth_stencil.sType = VK_STRUCTURE_TYPE_DYNAMIC_DS_STATE_CREATE_INFO;
Piers Daniell886be472015-02-23 16:23:13 -07001088 depth_stencil.minDepth = 0.0f;
1089 depth_stencil.maxDepth = 1.0f;
Tony Barbourfa6cac72015-01-16 14:27:35 -07001090 depth_stencil.stencilBackRef = 0;
1091 depth_stencil.stencilFrontRef = 0;
1092 depth_stencil.stencilReadMask = 0xff;
1093 depth_stencil.stencilWriteMask = 0xff;
Chia-I Wuc19795a2014-09-13 11:12:55 +08001094
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001095 err = vkCreateDynamicViewportState(demo->device, &viewport_create, &demo->viewport);
Chia-I Wuc19795a2014-09-13 11:12:55 +08001096 assert(!err);
1097
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001098 err = vkCreateDynamicRasterState(demo->device, &raster, &demo->raster);
Chia-I Wuc19795a2014-09-13 11:12:55 +08001099 assert(!err);
1100
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001101 err = vkCreateDynamicColorBlendState(demo->device,
Chia-I Wuc19795a2014-09-13 11:12:55 +08001102 &color_blend, &demo->color_blend);
1103 assert(!err);
1104
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001105 err = vkCreateDynamicDepthStencilState(demo->device,
Chia-I Wuc19795a2014-09-13 11:12:55 +08001106 &depth_stencil, &demo->depth_stencil);
1107 assert(!err);
1108}
1109
Chia-I Wu8d24b3b2015-03-26 13:14:16 +08001110static void demo_prepare_descriptor_pool(struct demo *demo)
Chia-I Wuf8385062015-01-04 16:27:24 +08001111{
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001112 const VkDescriptorTypeCount type_count = {
Courtney Goeltzenleuchterad870812015-04-15 15:29:59 -06001113 .type = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
Chia-I Wuf8385062015-01-04 16:27:24 +08001114 .count = DEMO_TEXTURE_COUNT,
1115 };
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001116 const VkDescriptorPoolCreateInfo descriptor_pool = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001117 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO,
Chia-I Wuf8385062015-01-04 16:27:24 +08001118 .pNext = NULL,
1119 .count = 1,
1120 .pTypeCount = &type_count,
1121 };
Tony Barbour22a30862015-04-22 09:02:32 -06001122 VkResult U_ASSERT_ONLY err;
Chia-I Wuf8385062015-01-04 16:27:24 +08001123
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001124 err = vkCreateDescriptorPool(demo->device,
1125 VK_DESCRIPTOR_POOL_USAGE_ONE_SHOT, 1,
Chia-I Wu8d24b3b2015-03-26 13:14:16 +08001126 &descriptor_pool, &demo->desc_pool);
Chia-I Wuf8385062015-01-04 16:27:24 +08001127 assert(!err);
1128}
1129
1130static void demo_prepare_descriptor_set(struct demo *demo)
1131{
Chia-I Wu8cd8ecd2015-05-25 16:27:55 +08001132 VkDescriptorInfo tex_descs[DEMO_TEXTURE_COUNT];
1133 VkWriteDescriptorSet write;
Tony Barbour22a30862015-04-22 09:02:32 -06001134 VkResult U_ASSERT_ONLY err;
Chia-I Wuf8385062015-01-04 16:27:24 +08001135 uint32_t count;
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -06001136 uint32_t i;
Chia-I Wuf8385062015-01-04 16:27:24 +08001137
Mike Stroyan230e6252015-04-17 12:36:38 -06001138 err = vkAllocDescriptorSets(demo->device, demo->desc_pool,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001139 VK_DESCRIPTOR_SET_USAGE_STATIC,
Chia-I Wuf8385062015-01-04 16:27:24 +08001140 1, &demo->desc_layout,
1141 &demo->desc_set, &count);
1142 assert(!err && count == 1);
1143
Mike Stroyan230e6252015-04-17 12:36:38 -06001144 vkClearDescriptorSets(demo->device, demo->desc_pool, 1, &demo->desc_set);
Chia-I Wu8cd8ecd2015-05-25 16:27:55 +08001145
1146 memset(&tex_descs, 0, sizeof(tex_descs));
1147 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
1148 tex_descs[i].sampler = demo->textures[i].sampler;
1149 tex_descs[i].imageView = demo->textures[i].view;
1150 tex_descs[i].imageLayout = VK_IMAGE_LAYOUT_GENERAL;
1151 }
1152
1153 memset(&write, 0, sizeof(write));
1154 write.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
1155 write.destSet = demo->desc_set;
1156 write.count = DEMO_TEXTURE_COUNT;
1157 write.descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
1158 write.pDescriptors = tex_descs;
1159
1160 err = vkUpdateDescriptorSets(demo->device, 1, &write, 0, NULL);
1161 assert(!err);
Chia-I Wuf8385062015-01-04 16:27:24 +08001162}
1163
Chia-I Wuc19795a2014-09-13 11:12:55 +08001164static void demo_prepare(struct demo *demo)
1165{
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001166 const VkCmdBufferCreateInfo cmd = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001167 .sType = VK_STRUCTURE_TYPE_CMD_BUFFER_CREATE_INFO,
Chia-I Wuc19795a2014-09-13 11:12:55 +08001168 .pNext = NULL,
Courtney Goeltzenleuchterf3168062015-03-05 18:09:39 -07001169 .queueNodeIndex = demo->graphics_queue_node_index,
Chia-I Wuc19795a2014-09-13 11:12:55 +08001170 .flags = 0,
1171 };
Tony Barbour22a30862015-04-22 09:02:32 -06001172 VkResult U_ASSERT_ONLY err;
Chia-I Wuc19795a2014-09-13 11:12:55 +08001173
Courtney Goeltzenleuchter633f9c52015-04-30 10:58:33 -06001174 err = vkCreateCommandBuffer(demo->device, &cmd, &demo->draw_cmd);
1175 assert(!err);
1176
Chia-I Wuc19795a2014-09-13 11:12:55 +08001177 demo_prepare_buffers(demo);
Chia-I Wu9ae87c92014-10-07 14:15:01 +08001178 demo_prepare_depth(demo);
Chia-I Wub043fe32014-10-06 15:30:33 +08001179 demo_prepare_textures(demo);
Chia-I Wu99621bc2014-10-08 11:52:22 +08001180 demo_prepare_vertices(demo);
Chia-I Wuf8385062015-01-04 16:27:24 +08001181 demo_prepare_descriptor_layout(demo);
Chia-I Wuc19795a2014-09-13 11:12:55 +08001182 demo_prepare_pipeline(demo);
1183 demo_prepare_dynamic_states(demo);
1184
Chia-I Wu8d24b3b2015-03-26 13:14:16 +08001185 demo_prepare_descriptor_pool(demo);
Chia-I Wuf8385062015-01-04 16:27:24 +08001186 demo_prepare_descriptor_set(demo);
Courtney Goeltzenleuchter633f9c52015-04-30 10:58:33 -06001187 demo->prepared = true;
Chia-I Wuc19795a2014-09-13 11:12:55 +08001188}
1189
Ian Elliotte14e9f92015-04-16 15:23:05 -06001190#ifdef _WIN32
1191static void demo_run(struct demo *demo)
1192{
Courtney Goeltzenleuchter633f9c52015-04-30 10:58:33 -06001193 if (!demo->prepared)
1194 return;
Ian Elliotte14e9f92015-04-16 15:23:05 -06001195 demo_draw(demo);
1196}
1197
1198// On MS-Windows, make this a global, so it's available to WndProc()
1199struct demo demo;
1200
1201// MS-Windows event handling function:
1202LRESULT CALLBACK WndProc(HWND hWnd,
1203 UINT uMsg,
1204 WPARAM wParam,
1205 LPARAM lParam)
1206{
Ian Elliott4e19ed02015-04-28 10:52:52 -06001207 char tmp_str[] = APP_LONG_NAME;
Ian Elliotte14e9f92015-04-16 15:23:05 -06001208
1209 switch(uMsg)
1210 {
1211 case WM_CREATE:
1212 return 0;
1213 case WM_CLOSE:
1214 PostQuitMessage(0);
1215 return 0;
1216 case WM_PAINT:
1217 demo_run(&demo);
1218 return 0;
1219 default:
1220 break;
1221 }
1222 return (DefWindowProc(hWnd, uMsg, wParam, lParam));
1223}
1224
1225static void demo_create_window(struct demo *demo)
1226{
1227 WNDCLASSEX win_class;
1228
1229 // Initialize the window class structure:
1230 win_class.cbSize = sizeof(WNDCLASSEX);
1231 win_class.style = CS_HREDRAW | CS_VREDRAW;
1232 win_class.lpfnWndProc = WndProc;
1233 win_class.cbClsExtra = 0;
1234 win_class.cbWndExtra = 0;
1235 win_class.hInstance = demo->connection; // hInstance
1236 win_class.hIcon = LoadIcon(NULL, IDI_APPLICATION);
1237 win_class.hCursor = LoadCursor(NULL, IDC_ARROW);
1238 win_class.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
1239 win_class.lpszMenuName = NULL;
1240 win_class.lpszClassName = demo->name;
1241 win_class.hIconSm = LoadIcon(NULL, IDI_WINLOGO);
1242 // Register window class:
1243 if (!RegisterClassEx(&win_class)) {
1244 // It didn't work, so try to give a useful error:
1245 printf("Unexpected error trying to start the application!\n");
1246 fflush(stdout);
1247 exit(1);
1248 }
1249 // Create window with the registered class:
Mike Stroyan7eef5742015-06-15 14:19:19 -06001250 RECT wr = { 0, 0, demo->width, demo->height };
1251 AdjustWindowRect(&wr, WS_OVERLAPPEDWINDOW, FALSE);
Ian Elliotte14e9f92015-04-16 15:23:05 -06001252 demo->window = CreateWindowEx(0,
1253 demo->name, // class name
1254 demo->name, // app name
1255 WS_OVERLAPPEDWINDOW | // window style
1256 WS_VISIBLE |
1257 WS_SYSMENU,
1258 100,100, // x/y coords
Mike Stroyan7eef5742015-06-15 14:19:19 -06001259 wr.right-wr.left, // width
1260 wr.bottom-wr.top, // height
Ian Elliotte14e9f92015-04-16 15:23:05 -06001261 NULL, // handle to parent
1262 NULL, // handle to menu
1263 demo->connection, // hInstance
1264 NULL); // no extra parameters
1265 if (!demo->window) {
1266 // It didn't work, so try to give a useful error:
1267 printf("Cannot create a window in which to draw!\n");
1268 fflush(stdout);
1269 exit(1);
1270 }
1271}
1272#else // _WIN32
1273
Chia-I Wuc19795a2014-09-13 11:12:55 +08001274static void demo_handle_event(struct demo *demo,
1275 const xcb_generic_event_t *event)
1276{
1277 switch (event->response_type & 0x7f) {
1278 case XCB_EXPOSE:
1279 demo_draw(demo);
1280 break;
Courtney Goeltzenleuchterca698052014-11-07 15:17:03 -07001281 case XCB_CLIENT_MESSAGE:
1282 if((*(xcb_client_message_event_t*)event).data.data32[0] ==
1283 (*demo->atom_wm_delete_window).atom) {
1284 demo->quit = true;
1285 }
1286 break;
Chia-I Wuc19795a2014-09-13 11:12:55 +08001287 case XCB_KEY_RELEASE:
1288 {
1289 const xcb_key_release_event_t *key =
1290 (const xcb_key_release_event_t *) event;
1291
1292 if (key->detail == 0x9)
1293 demo->quit = true;
1294 }
1295 break;
Courtney Goeltzenleuchterca698052014-11-07 15:17:03 -07001296 case XCB_DESTROY_NOTIFY:
1297 demo->quit = true;
1298 break;
Chia-I Wuc19795a2014-09-13 11:12:55 +08001299 default:
1300 break;
1301 }
1302}
1303
1304static void demo_run(struct demo *demo)
1305{
1306 xcb_flush(demo->connection);
1307
1308 while (!demo->quit) {
1309 xcb_generic_event_t *event;
1310
1311 event = xcb_wait_for_event(demo->connection);
1312 if (event) {
1313 demo_handle_event(demo, event);
1314 free(event);
1315 }
1316 }
1317}
1318
1319static void demo_create_window(struct demo *demo)
1320{
1321 uint32_t value_mask, value_list[32];
1322
1323 demo->window = xcb_generate_id(demo->connection);
1324
1325 value_mask = XCB_CW_BACK_PIXEL | XCB_CW_EVENT_MASK;
1326 value_list[0] = demo->screen->black_pixel;
1327 value_list[1] = XCB_EVENT_MASK_KEY_RELEASE |
Courtney Goeltzenleuchterca698052014-11-07 15:17:03 -07001328 XCB_EVENT_MASK_EXPOSURE |
1329 XCB_EVENT_MASK_STRUCTURE_NOTIFY;
Chia-I Wuc19795a2014-09-13 11:12:55 +08001330
1331 xcb_create_window(demo->connection,
1332 XCB_COPY_FROM_PARENT,
1333 demo->window, demo->screen->root,
1334 0, 0, demo->width, demo->height, 0,
1335 XCB_WINDOW_CLASS_INPUT_OUTPUT,
1336 demo->screen->root_visual,
1337 value_mask, value_list);
1338
Courtney Goeltzenleuchterca698052014-11-07 15:17:03 -07001339 /* Magic code that will send notification when window is destroyed */
1340 xcb_intern_atom_cookie_t cookie = xcb_intern_atom(demo->connection, 1, 12,
1341 "WM_PROTOCOLS");
1342 xcb_intern_atom_reply_t* reply = xcb_intern_atom_reply(demo->connection, cookie, 0);
1343
1344 xcb_intern_atom_cookie_t cookie2 = xcb_intern_atom(demo->connection, 0, 16, "WM_DELETE_WINDOW");
1345 demo->atom_wm_delete_window = xcb_intern_atom_reply(demo->connection, cookie2, 0);
1346
1347 xcb_change_property(demo->connection, XCB_PROP_MODE_REPLACE,
1348 demo->window, (*reply).atom, 4, 32, 1,
1349 &(*demo->atom_wm_delete_window).atom);
1350 free(reply);
1351
Chia-I Wuc19795a2014-09-13 11:12:55 +08001352 xcb_map_window(demo->connection, demo->window);
1353}
Ian Elliotte14e9f92015-04-16 15:23:05 -06001354#endif // _WIN32
Chia-I Wuc19795a2014-09-13 11:12:55 +08001355
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001356static void demo_init_vk(struct demo *demo)
Chia-I Wuc19795a2014-09-13 11:12:55 +08001357{
Tobin Ehlis3536b442015-04-16 18:04:57 -06001358 VkResult err;
1359 // Extensions to enable
1360 const char *ext_names[] = {
Chia-I Wu5b66aa52015-04-16 22:02:10 +08001361 "VK_WSI_LunarG",
Tobin Ehlis3536b442015-04-16 18:04:57 -06001362 };
1363 size_t extSize = sizeof(uint32_t);
1364 uint32_t extCount = 0;
1365 err = vkGetGlobalExtensionInfo(VK_EXTENSION_INFO_TYPE_COUNT, 0, &extSize, &extCount);
1366 assert(!err);
1367
1368 VkExtensionProperties extProp;
1369 extSize = sizeof(VkExtensionProperties);
Tony Barbour22a30862015-04-22 09:02:32 -06001370 bool32_t U_ASSERT_ONLY extFound = 0;
Tobin Ehlis3536b442015-04-16 18:04:57 -06001371 for (uint32_t i = 0; i < extCount; i++) {
1372 err = vkGetGlobalExtensionInfo(VK_EXTENSION_INFO_TYPE_PROPERTIES, i, &extSize, &extProp);
1373 if (!strcmp(ext_names[0], extProp.extName))
1374 extFound = 1;
1375 }
Ian Elliott3b375cf2015-04-28 13:22:33 -06001376 if (!extFound) {
1377 ERR_EXIT("vkGetGlobalExtensionInfo failed to find the "
1378 "\"VK_WSI_LunarG\" extension.\n\nDo you have a compatible "
1379 "Vulkan installable client driver (ICD) installed?\nPlease "
1380 "look at the Getting Started guide for additional "
1381 "information.\n",
1382 "vkCreateInstance Failure");
1383 }
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001384 const VkApplicationInfo app = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001385 .sType = VK_STRUCTURE_TYPE_APPLICATION_INFO,
Chia-I Wuc19795a2014-09-13 11:12:55 +08001386 .pNext = NULL,
Ian Elliott4e19ed02015-04-28 10:52:52 -06001387 .pAppName = APP_SHORT_NAME,
Chia-I Wuc19795a2014-09-13 11:12:55 +08001388 .appVersion = 0,
Ian Elliott4e19ed02015-04-28 10:52:52 -06001389 .pEngineName = APP_SHORT_NAME,
Chia-I Wuc19795a2014-09-13 11:12:55 +08001390 .engineVersion = 0,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001391 .apiVersion = VK_API_VERSION,
Chia-I Wuc19795a2014-09-13 11:12:55 +08001392 };
Courtney Goeltzenleuchterddcb6192015-04-14 18:48:46 -06001393 const VkInstanceCreateInfo inst_info = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001394 .sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO,
Jon Ashburn29669a42015-04-04 14:52:07 -06001395 .pNext = NULL,
1396 .pAppInfo = &app,
1397 .pAllocCb = NULL,
Tobin Ehlis3536b442015-04-16 18:04:57 -06001398 .extensionCount = 1,
1399 .ppEnabledExtensionNames = ext_names,
Jon Ashburn29669a42015-04-04 14:52:07 -06001400 };
Courtney Goeltzenleuchterddcb6192015-04-14 18:48:46 -06001401 const VkDeviceQueueCreateInfo queue = {
Chia-I Wuc19795a2014-09-13 11:12:55 +08001402 .queueNodeIndex = 0,
1403 .queueCount = 1,
1404 };
Courtney Goeltzenleuchterddcb6192015-04-14 18:48:46 -06001405 const VkDeviceCreateInfo device = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001406 .sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO,
Chia-I Wuc19795a2014-09-13 11:12:55 +08001407 .pNext = NULL,
1408 .queueRecordCount = 1,
1409 .pRequestedQueues = &queue,
Tobin Ehlis3536b442015-04-16 18:04:57 -06001410 .extensionCount = 1,
Chia-I Wuc19795a2014-09-13 11:12:55 +08001411 .ppEnabledExtensionNames = ext_names,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001412 .flags = VK_DEVICE_CREATE_VALIDATION_BIT,
Chia-I Wuc19795a2014-09-13 11:12:55 +08001413 };
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -06001414 uint32_t gpu_count;
1415 uint32_t i;
Courtney Goeltzenleuchterf3168062015-03-05 18:09:39 -07001416 size_t data_size;
1417 uint32_t queue_count;
Chia-I Wuc19795a2014-09-13 11:12:55 +08001418
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001419 err = vkCreateInstance(&inst_info, &demo->inst);
Ian Elliottcaa9f272015-04-28 11:35:02 -06001420 if (err == VK_ERROR_INCOMPATIBLE_DRIVER) {
1421 ERR_EXIT("Cannot find a compatible Vulkan installable client driver "
Ian Elliott3b375cf2015-04-28 13:22:33 -06001422 "(ICD).\n\nPlease look at the Getting Started guide for "
Ian Elliottcaa9f272015-04-28 11:35:02 -06001423 "additional information.\n",
1424 "vkCreateInstance Failure");
1425 } else if (err) {
Ian Elliott3b375cf2015-04-28 13:22:33 -06001426 ERR_EXIT("vkCreateInstance failed.\n\nDo you have a compatible Vulkan "
1427 "installable client driver (ICD) installed?\nPlease look at "
Ian Elliottcaa9f272015-04-28 11:35:02 -06001428 "the Getting Started guide for additional information.\n",
1429 "vkCreateInstance Failure");
Ian Elliottdfe55f72015-04-03 15:24:55 -06001430 }
Jon Ashburn29669a42015-04-04 14:52:07 -06001431
Jon Ashburn07b309a2015-04-15 11:31:12 -06001432 gpu_count = 1;
1433 err = vkEnumeratePhysicalDevices(demo->inst, &gpu_count, &demo->gpu);
Chia-I Wuc19795a2014-09-13 11:12:55 +08001434 assert(!err && gpu_count == 1);
1435
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001436 err = vkCreateDevice(demo->gpu, &device, &demo->device);
Chia-I Wuc19795a2014-09-13 11:12:55 +08001437 assert(!err);
1438
Tony Barbour8205d902015-04-16 15:59:00 -06001439 err = vkGetPhysicalDeviceInfo(demo->gpu, VK_PHYSICAL_DEVICE_INFO_TYPE_PROPERTIES,
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -06001440 &data_size, NULL);
1441 assert(!err);
1442
Tony Barbour8205d902015-04-16 15:59:00 -06001443 demo->gpu_props = (VkPhysicalDeviceProperties *) malloc(data_size);
1444 err = vkGetPhysicalDeviceInfo(demo->gpu, VK_PHYSICAL_DEVICE_INFO_TYPE_PROPERTIES,
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -06001445 &data_size, demo->gpu_props);
1446 assert(!err);
1447
Tony Barbour8205d902015-04-16 15:59:00 -06001448 err = vkGetPhysicalDeviceInfo(demo->gpu, VK_PHYSICAL_DEVICE_INFO_TYPE_QUEUE_PROPERTIES,
Courtney Goeltzenleuchterf3168062015-03-05 18:09:39 -07001449 &data_size, NULL);
1450 assert(!err);
1451
Tony Barbour8205d902015-04-16 15:59:00 -06001452 demo->queue_props = (VkPhysicalDeviceQueueProperties *) malloc(data_size);
1453 err = vkGetPhysicalDeviceInfo(demo->gpu, VK_PHYSICAL_DEVICE_INFO_TYPE_QUEUE_PROPERTIES,
Courtney Goeltzenleuchterf3168062015-03-05 18:09:39 -07001454 &data_size, demo->queue_props);
1455 assert(!err);
Mike Stroyan230e6252015-04-17 12:36:38 -06001456 queue_count = (uint32_t) (data_size / sizeof(VkPhysicalDeviceQueueProperties));
Courtney Goeltzenleuchterf3168062015-03-05 18:09:39 -07001457 assert(queue_count >= 1);
1458
1459 for (i = 0; i < queue_count; i++) {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001460 if (demo->queue_props[i].queueFlags & VK_QUEUE_GRAPHICS_BIT)
Courtney Goeltzenleuchterf3168062015-03-05 18:09:39 -07001461 break;
1462 }
1463 assert(i < queue_count);
1464 demo->graphics_queue_node_index = i;
1465
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001466 err = vkGetDeviceQueue(demo->device, demo->graphics_queue_node_index,
Chia-I Wuc19795a2014-09-13 11:12:55 +08001467 0, &demo->queue);
1468 assert(!err);
Ian Elliott32536f92015-04-21 16:41:02 -06001469
1470
1471 // Get the VkDisplayWSI's associated with this physical device:
1472 VkDisplayWSI display;
1473 err = vkGetPhysicalDeviceInfo(demo->gpu, VK_PHYSICAL_DEVICE_INFO_TYPE_DISPLAY_PROPERTIES_WSI,
1474 &data_size, NULL);
Ian Elliotte16c6cf2015-05-20 14:54:42 -06001475 if (err != VK_SUCCESS) {
1476 printf("The Vulkan installable client driver (ICD) does not support "
1477 "querying\nfor the swap-chain image format. Therefore, am "
1478 "hardcoding this\nformat to VK_FORMAT_B8G8R8A8_UNORM.\n");
1479 fflush(stdout);
1480 demo->format = VK_FORMAT_B8G8R8A8_UNORM;
1481 return;
1482 }
Ian Elliott32536f92015-04-21 16:41:02 -06001483 demo->display_props = (VkDisplayPropertiesWSI *) malloc(data_size);
1484 err = vkGetPhysicalDeviceInfo(demo->gpu, VK_PHYSICAL_DEVICE_INFO_TYPE_DISPLAY_PROPERTIES_WSI,
1485 &data_size, demo->display_props);
1486 assert(!err);
1487 demo->num_displays = data_size / sizeof(VkDisplayPropertiesWSI);
1488 // For now, simply use the first display (TODO: Enhance this for the
1489 // future):
1490 display = demo->display_props[0].display;
1491
1492 // Get a VkFormat to use with the VkDisplayWSI we are using:
1493 err = vkGetDisplayInfoWSI(display, VK_DISPLAY_INFO_TYPE_FORMAT_PROPERTIES_WSI,
1494 &data_size, NULL);
1495 VkDisplayFormatPropertiesWSI* display_format_props =
1496 (VkDisplayFormatPropertiesWSI*) malloc(data_size);
1497 err = vkGetDisplayInfoWSI(display, VK_DISPLAY_INFO_TYPE_FORMAT_PROPERTIES_WSI,
1498 &data_size, display_format_props);
1499 // For now, simply use the first VkFormat (TODO: Enhance this for the
1500 // future):
1501 demo->format = display_format_props[0].swapChainFormat;
Chia-I Wuc19795a2014-09-13 11:12:55 +08001502}
1503
1504static void demo_init_connection(struct demo *demo)
1505{
Ian Elliotte14e9f92015-04-16 15:23:05 -06001506#ifndef _WIN32
Chia-I Wuc19795a2014-09-13 11:12:55 +08001507 const xcb_setup_t *setup;
1508 xcb_screen_iterator_t iter;
1509 int scr;
1510
1511 demo->connection = xcb_connect(NULL, &scr);
Ian Elliottdfe55f72015-04-03 15:24:55 -06001512 if (demo->connection == NULL) {
1513 printf("Cannot find a compatible Vulkan installable client driver "
1514 "(ICD).\nExiting ...\n");
1515 fflush(stdout);
1516 exit(1);
1517 }
Chia-I Wuc19795a2014-09-13 11:12:55 +08001518
1519 setup = xcb_get_setup(demo->connection);
1520 iter = xcb_setup_roots_iterator(setup);
1521 while (scr-- > 0)
1522 xcb_screen_next(&iter);
1523
1524 demo->screen = iter.data;
Ian Elliotte14e9f92015-04-16 15:23:05 -06001525#endif // _WIN32
Chia-I Wuc19795a2014-09-13 11:12:55 +08001526}
1527
Ian Elliotte14e9f92015-04-16 15:23:05 -06001528#ifdef _WIN32
1529static void demo_init(struct demo *demo, HINSTANCE hInstance, LPSTR pCmdLine)
1530#else // _WIN32
Courtney Goeltzenleuchterf113a952015-02-25 11:46:58 -07001531static void demo_init(struct demo *demo, const int argc, const char *argv[])
Ian Elliotte14e9f92015-04-16 15:23:05 -06001532#endif // _WIN32
Chia-I Wuc19795a2014-09-13 11:12:55 +08001533{
Ian Elliotte14e9f92015-04-16 15:23:05 -06001534 bool argv_error = false;
1535
Chia-I Wuc19795a2014-09-13 11:12:55 +08001536 memset(demo, 0, sizeof(*demo));
1537
Ian Elliotte14e9f92015-04-16 15:23:05 -06001538#ifdef _WIN32
1539 demo->connection = hInstance;
Ian Elliott4e19ed02015-04-28 10:52:52 -06001540 strncpy(demo->name, APP_SHORT_NAME, APP_NAME_STR_LEN);
Ian Elliotte14e9f92015-04-16 15:23:05 -06001541
1542 if (strncmp(pCmdLine, "--use_staging", strlen("--use_staging")) == 0)
1543 demo->use_staging_buffer = true;
Cody Northrop75db0322015-05-28 11:27:16 -06001544 else if (strncmp(pCmdLine, "--use_glsl", strlen("--use_glsl")) == 0)
1545 demo->use_glsl = true;
Ian Elliotte14e9f92015-04-16 15:23:05 -06001546 else if (strlen(pCmdLine) != 0) {
1547 fprintf(stderr, "Do not recognize argument \"%s\".\n", pCmdLine);
1548 argv_error = true;
1549 }
1550#else // _WIN32
Courtney Goeltzenleuchterf113a952015-02-25 11:46:58 -07001551 for (int i = 0; i < argc; i++) {
1552 if (strncmp(argv[i], "--use_staging", strlen("--use_staging")) == 0)
1553 demo->use_staging_buffer = true;
Cody Northrop75db0322015-05-28 11:27:16 -06001554 else if (strncmp(argv[i], "--use_glsl", strlen("--use_glsl")) == 0)
1555 demo->use_glsl = true;
Courtney Goeltzenleuchterf113a952015-02-25 11:46:58 -07001556 }
Ian Elliotte14e9f92015-04-16 15:23:05 -06001557#endif // _WIN32
1558 if (argv_error) {
Ian Elliott4e19ed02015-04-28 10:52:52 -06001559 fprintf(stderr, "Usage:\n %s [--use_staging]\n", APP_SHORT_NAME);
Ian Elliotte14e9f92015-04-16 15:23:05 -06001560 fflush(stderr);
1561 exit(1);
1562 }
Courtney Goeltzenleuchterf113a952015-02-25 11:46:58 -07001563
Chia-I Wuc19795a2014-09-13 11:12:55 +08001564 demo_init_connection(demo);
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001565 demo_init_vk(demo);
Chia-I Wuc19795a2014-09-13 11:12:55 +08001566
1567 demo->width = 300;
1568 demo->height = 300;
Chia-I Wuc19795a2014-09-13 11:12:55 +08001569}
1570
1571static void demo_cleanup(struct demo *demo)
1572{
Mark Lobodzinski23182612015-05-29 09:32:35 -05001573 uint32_t i;
Chia-I Wuc19795a2014-09-13 11:12:55 +08001574
Mike Stroyan230e6252015-04-17 12:36:38 -06001575 vkDestroyObject(demo->device, VK_OBJECT_TYPE_DESCRIPTOR_SET, demo->desc_set);
1576 vkDestroyObject(demo->device, VK_OBJECT_TYPE_DESCRIPTOR_POOL, demo->desc_pool);
Chia-I Wuf8385062015-01-04 16:27:24 +08001577
Courtney Goeltzenleuchter633f9c52015-04-30 10:58:33 -06001578 if (demo->setup_cmd) {
1579 vkDestroyObject(demo->device, VK_OBJECT_TYPE_COMMAND_BUFFER, demo->setup_cmd);
1580 }
1581 vkDestroyObject(demo->device, VK_OBJECT_TYPE_COMMAND_BUFFER, demo->draw_cmd);
Chia-I Wuc19795a2014-09-13 11:12:55 +08001582
Mike Stroyan230e6252015-04-17 12:36:38 -06001583 vkDestroyObject(demo->device, VK_OBJECT_TYPE_DYNAMIC_VP_STATE, demo->viewport);
1584 vkDestroyObject(demo->device, VK_OBJECT_TYPE_DYNAMIC_RS_STATE, demo->raster);
1585 vkDestroyObject(demo->device, VK_OBJECT_TYPE_DYNAMIC_CB_STATE, demo->color_blend);
1586 vkDestroyObject(demo->device, VK_OBJECT_TYPE_DYNAMIC_DS_STATE, demo->depth_stencil);
Chia-I Wuc19795a2014-09-13 11:12:55 +08001587
Mike Stroyan230e6252015-04-17 12:36:38 -06001588 vkDestroyObject(demo->device, VK_OBJECT_TYPE_PIPELINE, demo->pipeline);
1589 vkDestroyObject(demo->device, VK_OBJECT_TYPE_PIPELINE_LAYOUT, demo->pipeline_layout);
1590 vkDestroyObject(demo->device, VK_OBJECT_TYPE_DESCRIPTOR_SET_LAYOUT, demo->desc_layout);
Chia-I Wub043fe32014-10-06 15:30:33 +08001591
Mike Stroyan230e6252015-04-17 12:36:38 -06001592 vkDestroyObject(demo->device, VK_OBJECT_TYPE_BUFFER, demo->vertices.buf);
Mark Lobodzinski23182612015-05-29 09:32:35 -05001593 vkFreeMemory(demo->device, demo->vertices.mem);
Chia-I Wu99621bc2014-10-08 11:52:22 +08001594
Chia-I Wub043fe32014-10-06 15:30:33 +08001595 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Mike Stroyan230e6252015-04-17 12:36:38 -06001596 vkDestroyObject(demo->device, VK_OBJECT_TYPE_IMAGE_VIEW, demo->textures[i].view);
Mike Stroyan230e6252015-04-17 12:36:38 -06001597 vkDestroyObject(demo->device, VK_OBJECT_TYPE_IMAGE, demo->textures[i].image);
Mark Lobodzinski23182612015-05-29 09:32:35 -05001598 vkFreeMemory(demo->device, demo->textures[i].mem);
Mike Stroyan230e6252015-04-17 12:36:38 -06001599 vkDestroyObject(demo->device, VK_OBJECT_TYPE_SAMPLER, demo->textures[i].sampler);
Chia-I Wub043fe32014-10-06 15:30:33 +08001600 }
1601
Mike Stroyan230e6252015-04-17 12:36:38 -06001602 vkDestroyObject(demo->device, VK_OBJECT_TYPE_DEPTH_STENCIL_VIEW, demo->depth.view);
Mike Stroyan230e6252015-04-17 12:36:38 -06001603 vkDestroyObject(demo->device, VK_OBJECT_TYPE_IMAGE, demo->depth.image);
Mark Lobodzinski23182612015-05-29 09:32:35 -05001604 vkFreeMemory(demo->device, demo->depth.mem);
Chia-I Wu9ae87c92014-10-07 14:15:01 +08001605
Chia-I Wuc19795a2014-09-13 11:12:55 +08001606 for (i = 0; i < DEMO_BUFFER_COUNT; i++) {
Mike Stroyan230e6252015-04-17 12:36:38 -06001607 vkDestroyObject(demo->device, VK_OBJECT_TYPE_COLOR_ATTACHMENT_VIEW, demo->buffers[i].view);
Chia-I Wuc19795a2014-09-13 11:12:55 +08001608 }
Chia-I Wu5b66aa52015-04-16 22:02:10 +08001609 vkDestroySwapChainWSI(demo->swap_chain);
Chia-I Wuc19795a2014-09-13 11:12:55 +08001610
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001611 vkDestroyDevice(demo->device);
1612 vkDestroyInstance(demo->inst);
Chia-I Wuc19795a2014-09-13 11:12:55 +08001613
Ian Elliotte14e9f92015-04-16 15:23:05 -06001614#ifndef _WIN32
Chia-I Wuc19795a2014-09-13 11:12:55 +08001615 xcb_destroy_window(demo->connection, demo->window);
1616 xcb_disconnect(demo->connection);
Ian Elliotte14e9f92015-04-16 15:23:05 -06001617#endif // _WIN32
Chia-I Wuc19795a2014-09-13 11:12:55 +08001618}
1619
Ian Elliotte14e9f92015-04-16 15:23:05 -06001620#ifdef _WIN32
1621int APIENTRY WinMain(HINSTANCE hInstance,
1622 HINSTANCE hPrevInstance,
1623 LPSTR pCmdLine,
1624 int nCmdShow)
1625{
1626 MSG msg; // message
1627 bool done; // flag saying when app is complete
1628
1629 demo_init(&demo, hInstance, pCmdLine);
1630 demo_create_window(&demo);
1631
1632 demo_prepare(&demo);
1633
1634 done = false; //initialize loop condition variable
1635 /* main message loop*/
1636 while(!done)
1637 {
Ian Elliott421107f2015-04-28 15:50:36 -06001638 PeekMessage(&msg, NULL, 0, 0, PM_REMOVE);
Ian Elliotte14e9f92015-04-16 15:23:05 -06001639 if (msg.message == WM_QUIT) //check for a quit message
1640 {
1641 done = true; //if found, quit app
1642 }
1643 else
1644 {
1645 /* Translate and dispatch to event queue*/
1646 TranslateMessage(&msg);
1647 DispatchMessage(&msg);
1648 }
1649 }
1650
1651 demo_cleanup(&demo);
1652
Tony Barboura938abb2015-04-22 11:36:22 -06001653 return (int) msg.wParam;
Ian Elliotte14e9f92015-04-16 15:23:05 -06001654}
1655#else // _WIN32
Courtney Goeltzenleuchterf113a952015-02-25 11:46:58 -07001656int main(const int argc, const char *argv[])
Chia-I Wuc19795a2014-09-13 11:12:55 +08001657{
1658 struct demo demo;
1659
Courtney Goeltzenleuchterf113a952015-02-25 11:46:58 -07001660 demo_init(&demo, argc, argv);
Chia-I Wu5b66aa52015-04-16 22:02:10 +08001661 demo_create_window(&demo);
Chia-I Wuc19795a2014-09-13 11:12:55 +08001662
1663 demo_prepare(&demo);
Chia-I Wuc19795a2014-09-13 11:12:55 +08001664 demo_run(&demo);
1665
1666 demo_cleanup(&demo);
1667
Chia-I Wuc19795a2014-09-13 11:12:55 +08001668 return 0;
1669}
Ian Elliotte14e9f92015-04-16 15:23:05 -06001670#endif // _WIN32