blob: 8276c587c246292b5b334149cabe7d30ca9dfb4f [file] [log] [blame]
Chia-I Wuf5caeb02014-10-25 12:11:27 +08001/*
Ian Elliott7595eee2015-04-28 10:33:11 -06002 * Vulkan
3 *
4 * Copyright (C) 2014-2015 LunarG, Inc.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included
14 * in all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
23 */
24/*
Chia-I Wuf5caeb02014-10-25 12:11:27 +080025 * Draw a textured triangle with depth testing. This is written against Intel
26 * ICD. It does not do state transition nor object memory binding like it
27 * should. It also does no error checking.
28 */
29
Chia-I Wuc19795a2014-09-13 11:12:55 +080030#include <stdio.h>
31#include <stdlib.h>
32#include <string.h>
33#include <stdbool.h>
34#include <assert.h>
35
Ian Elliotte14e9f92015-04-16 15:23:05 -060036#ifdef _WIN32
37#pragma comment(linker, "/subsystem:windows")
38#include <windows.h>
39#define APP_NAME_STR_LEN 80
40#else // _WIN32
Chia-I Wuc19795a2014-09-13 11:12:55 +080041#include <xcb/xcb.h>
Ian Elliotte14e9f92015-04-16 15:23:05 -060042#endif // _WIN32
43
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -060044#include <vulkan.h>
Chia-I Wu5b66aa52015-04-16 22:02:10 +080045#include <vk_wsi_lunarg.h>
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -060046#include "vk_debug_report_lunarg.h"
Chia-I Wuc19795a2014-09-13 11:12:55 +080047
Cody Northropd4e020a2015-03-17 14:54:35 -060048#include "icd-spv.h"
Courtney Goeltzenleuchter3f2606d2014-10-13 17:51:58 -060049
Chia-I Wuc19795a2014-09-13 11:12:55 +080050#define DEMO_BUFFER_COUNT 2
Chia-I Wub043fe32014-10-06 15:30:33 +080051#define DEMO_TEXTURE_COUNT 1
Courtney Goeltzenleuchterf5cdad02015-03-31 16:36:30 -060052#define VERTEX_BUFFER_BIND_ID 0
Ian Elliott4e19ed02015-04-28 10:52:52 -060053#define APP_SHORT_NAME "tri"
54#define APP_LONG_NAME "The Vulkan Triangle Demo Program"
Chia-I Wuc19795a2014-09-13 11:12:55 +080055
Tony Barbour22a30862015-04-22 09:02:32 -060056#if defined(NDEBUG) && defined(__GNUC__)
57#define U_ASSERT_ONLY __attribute__((unused))
58#else
59#define U_ASSERT_ONLY
60#endif
61
Ian Elliottcaa9f272015-04-28 11:35:02 -060062#ifdef _WIN32
63#define ERR_EXIT(err_msg, err_class) \
64 do { \
65 MessageBox(NULL, err_msg, err_class, MB_OK); \
66 exit(1); \
67 } while (0)
68
69// NOTE: If the following values (copied from "loader_platform.h") change, they
70// need to change here as well:
71#define LAYER_NAMES_ENV "VK_LAYER_NAMES"
72#define LAYER_NAMES_REGISTRY_VALUE "VK_LAYER_NAMES"
73#else // _WIN32
74
75#define ERR_EXIT(err_msg, err_class) \
76 do { \
77 printf(err_msg); \
78 fflush(stdout); \
79 exit(1); \
80 } while (0)
81#endif // _WIN32
Tony Barbour22a30862015-04-22 09:02:32 -060082
Ian Elliott1b6de092015-06-22 15:07:49 -060083#define GET_DEVICE_PROC_ADDR(dev, entrypoint) \
84{ \
85 demo->fp##entrypoint = vkGetDeviceProcAddr(dev, "vk"#entrypoint); \
86 if (demo->fp##entrypoint == NULL) { \
87 ERR_EXIT("vkGetDeviceProcAddr failed to find vk"#entrypoint, \
88 "vkGetDeviceProcAddr Failure"); \
89 } \
90}
91
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -060092struct texture_object {
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -060093 VkSampler sampler;
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -070094
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -060095 VkImage image;
96 VkImageLayout imageLayout;
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -060097
Mark Lobodzinski23182612015-05-29 09:32:35 -050098 VkDeviceMemory mem;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -060099 VkImageView view;
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700100 int32_t tex_width, tex_height;
101};
102
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -0600103void dbgFunc(
104 VkFlags msgFlags,
105 VkObjectType objType,
106 VkObject srcObject,
107 size_t location,
108 int32_t msgCode,
109 const char* pLayerPrefix,
110 const char* pMsg,
111 void* pUserData)
112{
113 char *message = (char *) malloc(strlen(pMsg)+100);
114
115 assert (message);
116
117 if (msgFlags & VK_DBG_REPORT_ERROR_BIT) {
118 sprintf(message,"ERROR: [%s] Code %d : %s", pLayerPrefix, msgCode, pMsg);
119 } else if (msgFlags & VK_DBG_REPORT_WARN_BIT) {
120 sprintf(message,"WARNING: [%s] Code %d : %s", pLayerPrefix, msgCode, pMsg);
121 } else {
122 return;
123 }
124
125#ifdef _WIN32
126 MessageBox(NULL, message, "Alert", MB_OK);
127#else
128 printf("%s\n",message);
129 fflush(stdout);
130#endif
131 free(message);
132}
133
Chia-I Wuc19795a2014-09-13 11:12:55 +0800134struct demo {
Ian Elliotte14e9f92015-04-16 15:23:05 -0600135#ifdef _WIN32
136#define APP_NAME_STR_LEN 80
137 HINSTANCE connection; // hInstance - Windows Instance
138 char name[APP_NAME_STR_LEN]; // Name to put on the window/icon
139 HWND window; // hWnd - window handle
140#else // _WIN32
Chia-I Wuc19795a2014-09-13 11:12:55 +0800141 xcb_connection_t *connection;
142 xcb_screen_t *screen;
Chia-I Wu5b66aa52015-04-16 22:02:10 +0800143 xcb_window_t window;
144 xcb_intern_atom_reply_t *atom_wm_delete_window;
Ian Elliotte14e9f92015-04-16 15:23:05 -0600145#endif // _WIN32
Jon Ashburn8a399e92015-04-24 09:46:24 -0700146 bool prepared;
Ian Elliotte14e9f92015-04-16 15:23:05 -0600147 bool use_staging_buffer;
Cody Northrop75db0322015-05-28 11:27:16 -0600148 bool use_glsl;
Chia-I Wuc19795a2014-09-13 11:12:55 +0800149
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600150 VkInstance inst;
Tony Barbour8205d902015-04-16 15:59:00 -0600151 VkPhysicalDevice gpu;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600152 VkDevice device;
153 VkQueue queue;
Tony Barbour426b9052015-06-24 16:06:58 -0600154 VkPhysicalDeviceProperties gpu_props;
Tony Barbour8205d902015-04-16 15:59:00 -0600155 VkPhysicalDeviceQueueProperties *queue_props;
Courtney Goeltzenleuchterf3168062015-03-05 18:09:39 -0700156 uint32_t graphics_queue_node_index;
Chia-I Wuc19795a2014-09-13 11:12:55 +0800157
158 int width, height;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600159 VkFormat format;
Chia-I Wuc19795a2014-09-13 11:12:55 +0800160
Jon Ashburncedc15f2015-05-21 18:13:33 -0600161 PFN_vkCreateSwapChainWSI fpCreateSwapChainWSI;
162 PFN_vkDestroySwapChainWSI fpDestroySwapChainWSI;
163 PFN_vkGetSwapChainInfoWSI fpGetSwapChainInfoWSI;
164 PFN_vkQueuePresentWSI fpQueuePresentWSI;
165
Chia-I Wu5b66aa52015-04-16 22:02:10 +0800166 VkSwapChainWSI swap_chain;
Chia-I Wuc19795a2014-09-13 11:12:55 +0800167 struct {
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600168 VkImage image;
Tony Barbour8205d902015-04-16 15:59:00 -0600169 VkDeviceMemory mem;
Chia-I Wuc19795a2014-09-13 11:12:55 +0800170
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600171 VkColorAttachmentView view;
Chia-I Wuc19795a2014-09-13 11:12:55 +0800172 } buffers[DEMO_BUFFER_COUNT];
173
Chia-I Wub043fe32014-10-06 15:30:33 +0800174 struct {
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600175 VkFormat format;
Chia-I Wu9ae87c92014-10-07 14:15:01 +0800176
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600177 VkImage image;
Mark Lobodzinski23182612015-05-29 09:32:35 -0500178 VkDeviceMemory mem;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600179 VkDepthStencilView view;
Chia-I Wu9ae87c92014-10-07 14:15:01 +0800180 } depth;
181
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600182 struct texture_object textures[DEMO_TEXTURE_COUNT];
Chia-I Wub043fe32014-10-06 15:30:33 +0800183
Chia-I Wu99621bc2014-10-08 11:52:22 +0800184 struct {
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600185 VkBuffer buf;
Mark Lobodzinski23182612015-05-29 09:32:35 -0500186 VkDeviceMemory mem;
Chia-I Wu8d29d022014-10-08 12:14:39 +0800187
Mark Lobodzinski0e0fb5c2015-06-23 15:11:57 -0600188 VkPipelineVertexInputStateCreateInfo vi;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600189 VkVertexInputBindingDescription vi_bindings[1];
190 VkVertexInputAttributeDescription vi_attrs[2];
Chia-I Wu99621bc2014-10-08 11:52:22 +0800191 } vertices;
192
Courtney Goeltzenleuchter633f9c52015-04-30 10:58:33 -0600193 VkCmdBuffer setup_cmd; // Command Buffer for initialization commands
194 VkCmdBuffer draw_cmd; // Command Buffer for drawing commands
Mark Lobodzinski556f7212015-04-17 14:11:39 -0500195 VkPipelineLayout pipeline_layout;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600196 VkDescriptorSetLayout desc_layout;
Jon Ashburn0d60d272015-07-09 15:02:25 -0600197 VkPipelineCache pipelineCache;
Chia-I Wu76cd4222015-07-08 13:34:24 +0800198 VkRenderPass render_pass;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600199 VkPipeline pipeline;
Chia-I Wuc19795a2014-09-13 11:12:55 +0800200
Courtney Goeltzenleuchterfcf855f2015-04-10 16:24:50 -0600201 VkDynamicVpState viewport;
202 VkDynamicRsState raster;
203 VkDynamicCbState color_blend;
204 VkDynamicDsState depth_stencil;
Chia-I Wuc19795a2014-09-13 11:12:55 +0800205
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600206 VkDescriptorPool desc_pool;
207 VkDescriptorSet desc_set;
Chia-I Wuf8385062015-01-04 16:27:24 +0800208
Chia-I Wu76cd4222015-07-08 13:34:24 +0800209 VkFramebuffer framebuffers[DEMO_BUFFER_COUNT];
210
Mark Lobodzinski72346292015-07-02 16:49:40 -0600211 VkPhysicalDeviceMemoryProperties memory_properties;
212
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -0600213 bool validate;
214 PFN_vkDbgCreateMsgCallback dbgCreateMsgCallback;
215 VkDbgMsgCallback msg_callback;
216
Chia-I Wuc19795a2014-09-13 11:12:55 +0800217 bool quit;
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600218 uint32_t current_buffer;
Chia-I Wuc19795a2014-09-13 11:12:55 +0800219};
220
Mark Lobodzinski72346292015-07-02 16:49:40 -0600221static VkResult memory_type_from_properties(struct demo *demo, uint32_t typeBits, VkFlags properties, uint32_t *typeIndex)
222{
223 // Search memtypes to find first index with those properties
224 for (uint32_t i = 0; i < 32; i++) {
225 if ((typeBits & 1) == 1) {
226 // Type is available, does it match user properties?
227 if ((demo->memory_properties.memoryTypes[i].propertyFlags & properties) == properties) {
228 *typeIndex = i;
229 return VK_SUCCESS;
230 }
231 }
232 typeBits >>= 1;
233 }
234 // No memory types matched, return failure
235 return VK_UNSUPPORTED;
236}
237
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600238static void demo_flush_init_cmd(struct demo *demo)
239{
Tony Barbour22a30862015-04-22 09:02:32 -0600240 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600241
Courtney Goeltzenleuchter633f9c52015-04-30 10:58:33 -0600242 if (demo->setup_cmd == VK_NULL_HANDLE)
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600243 return;
244
Courtney Goeltzenleuchter633f9c52015-04-30 10:58:33 -0600245 err = vkEndCommandBuffer(demo->setup_cmd);
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600246 assert(!err);
247
Courtney Goeltzenleuchter633f9c52015-04-30 10:58:33 -0600248 const VkCmdBuffer cmd_bufs[] = { demo->setup_cmd };
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600249
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600250 err = vkQueueSubmit(demo->queue, 1, cmd_bufs, VK_NULL_HANDLE);
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600251 assert(!err);
252
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600253 err = vkQueueWaitIdle(demo->queue);
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600254 assert(!err);
255
Courtney Goeltzenleuchter633f9c52015-04-30 10:58:33 -0600256 vkDestroyObject(demo->device, VK_OBJECT_TYPE_COMMAND_BUFFER, demo->setup_cmd);
257 demo->setup_cmd = VK_NULL_HANDLE;
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600258}
259
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600260static void demo_set_image_layout(
261 struct demo *demo,
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600262 VkImage image,
malnasse4b8ba4d2015-06-03 17:28:38 -0400263 VkImageAspect aspect,
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600264 VkImageLayout old_image_layout,
265 VkImageLayout new_image_layout)
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600266{
Tony Barbour22a30862015-04-22 09:02:32 -0600267 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600268
Courtney Goeltzenleuchter633f9c52015-04-30 10:58:33 -0600269 if (demo->setup_cmd == VK_NULL_HANDLE) {
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600270 const VkCmdBufferCreateInfo cmd = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600271 .sType = VK_STRUCTURE_TYPE_CMD_BUFFER_CREATE_INFO,
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600272 .pNext = NULL,
273 .queueNodeIndex = demo->graphics_queue_node_index,
Chia-I Wu88eaa3b2015-06-26 15:34:39 +0800274 .level = VK_CMD_BUFFER_LEVEL_PRIMARY,
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600275 .flags = 0,
276 };
277
Courtney Goeltzenleuchter633f9c52015-04-30 10:58:33 -0600278 err = vkCreateCommandBuffer(demo->device, &cmd, &demo->setup_cmd);
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600279 assert(!err);
280
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600281 VkCmdBufferBeginInfo cmd_buf_info = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600282 .sType = VK_STRUCTURE_TYPE_CMD_BUFFER_BEGIN_INFO,
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600283 .pNext = NULL,
Tony Barbour8205d902015-04-16 15:59:00 -0600284 .flags = VK_CMD_BUFFER_OPTIMIZE_SMALL_BATCH_BIT |
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600285 VK_CMD_BUFFER_OPTIMIZE_ONE_TIME_SUBMIT_BIT,
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600286 };
Courtney Goeltzenleuchter633f9c52015-04-30 10:58:33 -0600287 err = vkBeginCommandBuffer(demo->setup_cmd, &cmd_buf_info);
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600288 }
289
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600290 VkImageMemoryBarrier image_memory_barrier = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600291 .sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600292 .pNext = NULL,
293 .outputMask = 0,
294 .inputMask = 0,
295 .oldLayout = old_image_layout,
296 .newLayout = new_image_layout,
297 .image = image,
malnasse4b8ba4d2015-06-03 17:28:38 -0400298 .subresourceRange = { aspect, 0, 1, 0, 0 }
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600299 };
300
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600301 if (new_image_layout == VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL) {
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600302 /* Make sure anything that was copying from this image has completed */
Courtney Goeltzenleuchterad870812015-04-15 15:29:59 -0600303 image_memory_barrier.inputMask = VK_MEMORY_INPUT_TRANSFER_BIT;
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600304 }
305
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600306 if (new_image_layout == VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL) {
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600307 /* Make sure any Copy or CPU writes to image are flushed */
Courtney Goeltzenleuchtera569a502015-04-29 17:16:21 -0600308 image_memory_barrier.outputMask = VK_MEMORY_OUTPUT_TRANSFER_BIT | VK_MEMORY_OUTPUT_HOST_WRITE_BIT;
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600309 }
310
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600311 VkImageMemoryBarrier *pmemory_barrier = &image_memory_barrier;
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600312
Tony Barbourc2e987e2015-06-29 16:20:35 -0600313 VkPipelineStageFlags src_stages = VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT;
314 VkPipelineStageFlags dest_stages = VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT;
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600315
Tony Barbourc2e987e2015-06-29 16:20:35 -0600316 vkCmdPipelineBarrier(demo->setup_cmd, src_stages, dest_stages, false, 1, (const void **)&pmemory_barrier);
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600317}
318
Chia-I Wuc19795a2014-09-13 11:12:55 +0800319static void demo_draw_build_cmd(struct demo *demo)
320{
Chia-I Wu76cd4222015-07-08 13:34:24 +0800321 const VkCmdBufferBeginInfo cmd_buf_info = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600322 .sType = VK_STRUCTURE_TYPE_CMD_BUFFER_BEGIN_INFO,
Courtney Goeltzenleuchtere3b0f3a2015-04-03 15:25:24 -0600323 .pNext = NULL,
Tony Barbour8205d902015-04-16 15:59:00 -0600324 .flags = VK_CMD_BUFFER_OPTIMIZE_SMALL_BATCH_BIT |
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600325 VK_CMD_BUFFER_OPTIMIZE_ONE_TIME_SUBMIT_BIT,
Jon Ashburn53d27af2014-12-31 17:08:35 -0700326 };
Chia-I Wu76cd4222015-07-08 13:34:24 +0800327 const VkRenderPassBegin rp_begin = {
328 .renderPass = demo->render_pass,
329 .framebuffer = demo->framebuffers[demo->current_buffer],
330 .contents = VK_RENDER_PASS_CONTENTS_INLINE,
Jon Ashburn3325d6b2015-01-02 18:24:05 -0700331 };
Chia-I Wu76cd4222015-07-08 13:34:24 +0800332 VkResult U_ASSERT_ONLY err;
Chia-I Wuc19795a2014-09-13 11:12:55 +0800333
Courtney Goeltzenleuchter633f9c52015-04-30 10:58:33 -0600334 err = vkBeginCommandBuffer(demo->draw_cmd, &cmd_buf_info);
Chia-I Wuc19795a2014-09-13 11:12:55 +0800335 assert(!err);
336
Tobin Ehlise4076782015-06-24 15:53:07 -0600337 vkCmdBeginRenderPass(demo->draw_cmd, &rp_begin);
Courtney Goeltzenleuchter633f9c52015-04-30 10:58:33 -0600338 vkCmdBindPipeline(demo->draw_cmd, VK_PIPELINE_BIND_POINT_GRAPHICS,
Chia-I Wuc19795a2014-09-13 11:12:55 +0800339 demo->pipeline);
Mark Lobodzinskia65c4632015-06-15 13:21:21 -0600340 vkCmdBindDescriptorSets(demo->draw_cmd, VK_PIPELINE_BIND_POINT_GRAPHICS, demo->pipeline_layout,
Cody Northrop1a01b1d2015-04-16 13:41:56 -0600341 0, 1, & demo->desc_set, 0, NULL);
Chia-I Wuc19795a2014-09-13 11:12:55 +0800342
Courtney Goeltzenleuchter633f9c52015-04-30 10:58:33 -0600343 vkCmdBindDynamicStateObject(demo->draw_cmd, VK_STATE_BIND_POINT_VIEWPORT, demo->viewport);
344 vkCmdBindDynamicStateObject(demo->draw_cmd, VK_STATE_BIND_POINT_RASTER, demo->raster);
345 vkCmdBindDynamicStateObject(demo->draw_cmd, VK_STATE_BIND_POINT_COLOR_BLEND,
Chia-I Wuc19795a2014-09-13 11:12:55 +0800346 demo->color_blend);
Courtney Goeltzenleuchter633f9c52015-04-30 10:58:33 -0600347 vkCmdBindDynamicStateObject(demo->draw_cmd, VK_STATE_BIND_POINT_DEPTH_STENCIL,
Chia-I Wuc19795a2014-09-13 11:12:55 +0800348 demo->depth_stencil);
349
Tony Barbour8205d902015-04-16 15:59:00 -0600350 VkDeviceSize offsets[1] = {0};
Courtney Goeltzenleuchter633f9c52015-04-30 10:58:33 -0600351 vkCmdBindVertexBuffers(demo->draw_cmd, VERTEX_BUFFER_BIND_ID, 1, &demo->vertices.buf, offsets);
Chia-I Wu3b04af52014-11-08 10:48:20 +0800352
Courtney Goeltzenleuchter633f9c52015-04-30 10:58:33 -0600353 vkCmdDraw(demo->draw_cmd, 0, 3, 0, 1);
Chia-I Wu88eaa3b2015-06-26 15:34:39 +0800354 vkCmdEndRenderPass(demo->draw_cmd);
Chia-I Wuc19795a2014-09-13 11:12:55 +0800355
Courtney Goeltzenleuchter633f9c52015-04-30 10:58:33 -0600356 err = vkEndCommandBuffer(demo->draw_cmd);
Chia-I Wuc19795a2014-09-13 11:12:55 +0800357 assert(!err);
358}
359
360static void demo_draw(struct demo *demo)
361{
Chia-I Wu5b66aa52015-04-16 22:02:10 +0800362 const VkPresentInfoWSI present = {
363 .sType = VK_STRUCTURE_TYPE_PRESENT_INFO_WSI,
364 .pNext = NULL,
365 .image = demo->buffers[demo->current_buffer].image,
366 .flipInterval = 0,
Chia-I Wuc19795a2014-09-13 11:12:55 +0800367 };
Tony Barbour22a30862015-04-22 09:02:32 -0600368 VkResult U_ASSERT_ONLY err;
Chia-I Wuc19795a2014-09-13 11:12:55 +0800369
370 demo_draw_build_cmd(demo);
371
Courtney Goeltzenleuchter633f9c52015-04-30 10:58:33 -0600372 err = vkQueueSubmit(demo->queue, 1, &demo->draw_cmd, VK_NULL_HANDLE);
Chia-I Wuc19795a2014-09-13 11:12:55 +0800373 assert(!err);
374
Jon Ashburncedc15f2015-05-21 18:13:33 -0600375 err = demo->fpQueuePresentWSI(demo->queue, &present);
Chia-I Wuc19795a2014-09-13 11:12:55 +0800376 assert(!err);
377
378 demo->current_buffer = (demo->current_buffer + 1) % DEMO_BUFFER_COUNT;
Chia-I Wu5b66aa52015-04-16 22:02:10 +0800379
380 err = vkQueueWaitIdle(demo->queue);
381 assert(err == VK_SUCCESS);
Chia-I Wuc19795a2014-09-13 11:12:55 +0800382}
383
384static void demo_prepare_buffers(struct demo *demo)
385{
Chia-I Wu5b66aa52015-04-16 22:02:10 +0800386 const VkSwapChainCreateInfoWSI swap_chain = {
387 .sType = VK_STRUCTURE_TYPE_SWAP_CHAIN_CREATE_INFO_WSI,
388 .pNext = NULL,
389 .pNativeWindowSystemHandle = demo->connection,
390 .pNativeWindowHandle = (void *) (intptr_t) demo->window,
Ian Elliott32536f92015-04-21 16:41:02 -0600391 .displayCount = 1,
Chia-I Wu5b66aa52015-04-16 22:02:10 +0800392 .imageCount = DEMO_BUFFER_COUNT,
393 .imageFormat = demo->format,
394 .imageExtent = {
Chia-I Wuc19795a2014-09-13 11:12:55 +0800395 .width = demo->width,
396 .height = demo->height,
397 },
Chia-I Wu5b66aa52015-04-16 22:02:10 +0800398 .imageArraySize = 1,
399 .imageUsageFlags = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT,
Chia-I Wuc19795a2014-09-13 11:12:55 +0800400 };
Chia-I Wu5b66aa52015-04-16 22:02:10 +0800401 VkSwapChainImageInfoWSI images[DEMO_BUFFER_COUNT];
402 size_t images_size = sizeof(images);
Tony Barbour22a30862015-04-22 09:02:32 -0600403 VkResult U_ASSERT_ONLY err;
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600404 uint32_t i;
Chia-I Wuc19795a2014-09-13 11:12:55 +0800405
Jon Ashburncedc15f2015-05-21 18:13:33 -0600406 err = demo->fpCreateSwapChainWSI(demo->device, &swap_chain, &demo->swap_chain);
Chia-I Wu5b66aa52015-04-16 22:02:10 +0800407 assert(!err);
408
Jon Ashburncedc15f2015-05-21 18:13:33 -0600409 err = demo->fpGetSwapChainInfoWSI(demo->swap_chain,
Chia-I Wu5b66aa52015-04-16 22:02:10 +0800410 VK_SWAP_CHAIN_INFO_TYPE_PERSISTENT_IMAGES_WSI,
411 &images_size, images);
412 assert(!err && images_size == sizeof(images));
413
Chia-I Wuc19795a2014-09-13 11:12:55 +0800414 for (i = 0; i < DEMO_BUFFER_COUNT; i++) {
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600415 VkColorAttachmentViewCreateInfo color_attachment_view = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600416 .sType = VK_STRUCTURE_TYPE_COLOR_ATTACHMENT_VIEW_CREATE_INFO,
Chia-I Wuc19795a2014-09-13 11:12:55 +0800417 .pNext = NULL,
418 .format = demo->format,
419 .mipLevel = 0,
420 .baseArraySlice = 0,
421 .arraySize = 1,
422 };
423
Chia-I Wu5b66aa52015-04-16 22:02:10 +0800424 demo->buffers[i].image = images[i].image;
425 demo->buffers[i].mem = images[i].memory;
Mark Lobodzinski97dcd042015-04-16 08:52:00 -0500426
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600427 demo_set_image_layout(demo, demo->buffers[i].image,
malnasse4b8ba4d2015-06-03 17:28:38 -0400428 VK_IMAGE_ASPECT_COLOR,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600429 VK_IMAGE_LAYOUT_UNDEFINED,
430 VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL);
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600431
Chia-I Wuc19795a2014-09-13 11:12:55 +0800432 color_attachment_view.image = demo->buffers[i].image;
433
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600434 err = vkCreateColorAttachmentView(demo->device,
Chia-I Wuc19795a2014-09-13 11:12:55 +0800435 &color_attachment_view, &demo->buffers[i].view);
436 assert(!err);
437 }
Piers Daniell886be472015-02-23 16:23:13 -0700438
439 demo->current_buffer = 0;
Chia-I Wuc19795a2014-09-13 11:12:55 +0800440}
441
Chia-I Wu9ae87c92014-10-07 14:15:01 +0800442static void demo_prepare_depth(struct demo *demo)
443{
Tony Barbour8205d902015-04-16 15:59:00 -0600444 const VkFormat depth_format = VK_FORMAT_D16_UNORM;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600445 const VkImageCreateInfo image = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600446 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
Chia-I Wu9ae87c92014-10-07 14:15:01 +0800447 .pNext = NULL,
Tony Barbour8205d902015-04-16 15:59:00 -0600448 .imageType = VK_IMAGE_TYPE_2D,
Chia-I Wu9ae87c92014-10-07 14:15:01 +0800449 .format = depth_format,
450 .extent = { demo->width, demo->height, 1 },
451 .mipLevels = 1,
452 .arraySize = 1,
453 .samples = 1,
Tony Barbour8205d902015-04-16 15:59:00 -0600454 .tiling = VK_IMAGE_TILING_OPTIMAL,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600455 .usage = VK_IMAGE_USAGE_DEPTH_STENCIL_BIT,
Chia-I Wu9ae87c92014-10-07 14:15:01 +0800456 .flags = 0,
457 };
Courtney Goeltzenleuchterddcb6192015-04-14 18:48:46 -0600458 VkMemoryAllocInfo mem_alloc = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600459 .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOC_INFO,
Mark Lobodzinski97dcd042015-04-16 08:52:00 -0500460 .pNext = NULL,
Chia-I Wu9ae87c92014-10-07 14:15:01 +0800461 .allocationSize = 0,
Mark Lobodzinski72346292015-07-02 16:49:40 -0600462 .memoryTypeIndex = 0,
Chia-I Wu9ae87c92014-10-07 14:15:01 +0800463 };
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600464 VkDepthStencilViewCreateInfo view = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600465 .sType = VK_STRUCTURE_TYPE_DEPTH_STENCIL_VIEW_CREATE_INFO,
Chia-I Wu9ae87c92014-10-07 14:15:01 +0800466 .pNext = NULL,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600467 .image = VK_NULL_HANDLE,
Chia-I Wu9ae87c92014-10-07 14:15:01 +0800468 .mipLevel = 0,
469 .baseArraySlice = 0,
470 .arraySize = 1,
471 .flags = 0,
472 };
Jon Ashburna9ae3832015-01-16 09:37:43 -0700473
Mark Lobodzinski23182612015-05-29 09:32:35 -0500474 VkMemoryRequirements mem_reqs;
Tony Barbour22a30862015-04-22 09:02:32 -0600475 VkResult U_ASSERT_ONLY err;
Chia-I Wu9ae87c92014-10-07 14:15:01 +0800476
477 demo->depth.format = depth_format;
478
479 /* create image */
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600480 err = vkCreateImage(demo->device, &image,
Chia-I Wu9ae87c92014-10-07 14:15:01 +0800481 &demo->depth.image);
482 assert(!err);
483
Mark Lobodzinski72346292015-07-02 16:49:40 -0600484 /* get memory requirements for this object */
Tony Barbour426b9052015-06-24 16:06:58 -0600485 err = vkGetObjectMemoryRequirements(demo->device,
486 VK_OBJECT_TYPE_IMAGE, demo->depth.image, &mem_reqs);
Mark Lobodzinski72346292015-07-02 16:49:40 -0600487
488 /* select memory size and type */
Mark Lobodzinski23182612015-05-29 09:32:35 -0500489 mem_alloc.allocationSize = mem_reqs.size;
Mark Lobodzinski72346292015-07-02 16:49:40 -0600490 err = memory_type_from_properties(demo,
491 mem_reqs.memoryTypeBits,
492 VK_MEMORY_PROPERTY_DEVICE_ONLY,
493 &mem_alloc.memoryTypeIndex);
494 assert(!err);
Chia-I Wu9ae87c92014-10-07 14:15:01 +0800495
Mark Lobodzinski23182612015-05-29 09:32:35 -0500496 /* allocate memory */
497 err = vkAllocMemory(demo->device, &mem_alloc, &demo->depth.mem);
498 assert(!err);
Chia-I Wu9ae87c92014-10-07 14:15:01 +0800499
Mark Lobodzinski23182612015-05-29 09:32:35 -0500500 /* bind memory */
501 err = vkBindObjectMemory(demo->device,
502 VK_OBJECT_TYPE_IMAGE, demo->depth.image,
503 demo->depth.mem, 0);
504 assert(!err);
Chia-I Wu9ae87c92014-10-07 14:15:01 +0800505
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600506 demo_set_image_layout(demo, demo->depth.image,
malnasse4b8ba4d2015-06-03 17:28:38 -0400507 VK_IMAGE_ASPECT_DEPTH,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600508 VK_IMAGE_LAYOUT_UNDEFINED,
509 VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL);
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600510
Chia-I Wu9ae87c92014-10-07 14:15:01 +0800511 /* create image view */
512 view.image = demo->depth.image;
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600513 err = vkCreateDepthStencilView(demo->device, &view,
Chia-I Wu9ae87c92014-10-07 14:15:01 +0800514 &demo->depth.view);
515 assert(!err);
Chia-I Wu9ae87c92014-10-07 14:15:01 +0800516}
517
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700518static void demo_prepare_texture_image(struct demo *demo,
519 const uint32_t *tex_colors,
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600520 struct texture_object *tex_obj,
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600521 VkImageTiling tiling,
Courtney Goeltzenleuchtercb67a322015-04-21 09:31:23 -0600522 VkImageUsageFlags usage,
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600523 VkFlags mem_props)
Chia-I Wub043fe32014-10-06 15:30:33 +0800524{
Tony Barbour8205d902015-04-16 15:59:00 -0600525 const VkFormat tex_format = VK_FORMAT_B8G8R8A8_UNORM;
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600526 const int32_t tex_width = 2;
527 const int32_t tex_height = 2;
Tony Barbour22a30862015-04-22 09:02:32 -0600528 VkResult U_ASSERT_ONLY err;
Chia-I Wub043fe32014-10-06 15:30:33 +0800529
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600530 tex_obj->tex_width = tex_width;
531 tex_obj->tex_height = tex_height;
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700532
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600533 const VkImageCreateInfo image_create_info = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600534 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
Courtney Goeltzenleuchter3226a6f2015-02-11 18:17:22 -0700535 .pNext = NULL,
Tony Barbour8205d902015-04-16 15:59:00 -0600536 .imageType = VK_IMAGE_TYPE_2D,
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700537 .format = tex_format,
538 .extent = { tex_width, tex_height, 1 },
539 .mipLevels = 1,
540 .arraySize = 1,
541 .samples = 1,
542 .tiling = tiling,
Courtney Goeltzenleuchtercb67a322015-04-21 09:31:23 -0600543 .usage = usage,
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700544 .flags = 0,
545 };
Courtney Goeltzenleuchterddcb6192015-04-14 18:48:46 -0600546 VkMemoryAllocInfo mem_alloc = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600547 .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOC_INFO,
Mark Lobodzinski97dcd042015-04-16 08:52:00 -0500548 .pNext = NULL,
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700549 .allocationSize = 0,
Mark Lobodzinski72346292015-07-02 16:49:40 -0600550 .memoryTypeIndex = 0,
Courtney Goeltzenleuchter3226a6f2015-02-11 18:17:22 -0700551 };
552
Mark Lobodzinski23182612015-05-29 09:32:35 -0500553 VkMemoryRequirements mem_reqs;
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700554
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600555 err = vkCreateImage(demo->device, &image_create_info,
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600556 &tex_obj->image);
Courtney Goeltzenleuchter3226a6f2015-02-11 18:17:22 -0700557 assert(!err);
558
Tony Barbour426b9052015-06-24 16:06:58 -0600559 err = vkGetObjectMemoryRequirements(demo->device,
560 VK_OBJECT_TYPE_IMAGE, tex_obj->image, &mem_reqs);
Mark Lobodzinski72346292015-07-02 16:49:40 -0600561
562 mem_alloc.allocationSize = mem_reqs.size;
563 err = memory_type_from_properties(demo, mem_reqs.memoryTypeBits, mem_props, &mem_alloc.memoryTypeIndex);
564 assert(!err);
Courtney Goeltzenleuchter3226a6f2015-02-11 18:17:22 -0700565
Mark Lobodzinski23182612015-05-29 09:32:35 -0500566 /* allocate memory */
567 err = vkAllocMemory(demo->device, &mem_alloc, &tex_obj->mem);
568 assert(!err);
Courtney Goeltzenleuchter3226a6f2015-02-11 18:17:22 -0700569
Mark Lobodzinski23182612015-05-29 09:32:35 -0500570 /* bind memory */
571 err = vkBindObjectMemory(demo->device,
572 VK_OBJECT_TYPE_IMAGE, tex_obj->image,
573 tex_obj->mem, 0);
574 assert(!err);
Courtney Goeltzenleuchter3226a6f2015-02-11 18:17:22 -0700575
Tony Barbour8205d902015-04-16 15:59:00 -0600576 if (mem_props & VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT) {
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600577 const VkImageSubresource subres = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600578 .aspect = VK_IMAGE_ASPECT_COLOR,
Courtney Goeltzenleuchter3226a6f2015-02-11 18:17:22 -0700579 .mipLevel = 0,
580 .arraySlice = 0,
581 };
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600582 VkSubresourceLayout layout;
Courtney Goeltzenleuchter3226a6f2015-02-11 18:17:22 -0700583 void *data;
584 int32_t x, y;
585
Tony Barbour426b9052015-06-24 16:06:58 -0600586 err = vkGetImageSubresourceLayout(demo->device, tex_obj->image, &subres, &layout);
587 assert(!err);
Courtney Goeltzenleuchter3226a6f2015-02-11 18:17:22 -0700588
Mark Lobodzinski23182612015-05-29 09:32:35 -0500589 err = vkMapMemory(demo->device, tex_obj->mem, 0, 0, 0, &data);
Courtney Goeltzenleuchter3226a6f2015-02-11 18:17:22 -0700590 assert(!err);
591
592 for (y = 0; y < tex_height; y++) {
593 uint32_t *row = (uint32_t *) ((char *) data + layout.rowPitch * y);
594 for (x = 0; x < tex_width; x++)
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700595 row[x] = tex_colors[(x & 1) ^ (y & 1)];
Courtney Goeltzenleuchter3226a6f2015-02-11 18:17:22 -0700596 }
597
Mark Lobodzinski23182612015-05-29 09:32:35 -0500598 err = vkUnmapMemory(demo->device, tex_obj->mem);
Courtney Goeltzenleuchter3226a6f2015-02-11 18:17:22 -0700599 assert(!err);
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700600 }
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600601
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600602 tex_obj->imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600603 demo_set_image_layout(demo, tex_obj->image,
malnasse4b8ba4d2015-06-03 17:28:38 -0400604 VK_IMAGE_ASPECT_COLOR,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600605 VK_IMAGE_LAYOUT_UNDEFINED,
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600606 tex_obj->imageLayout);
607 /* 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 -0700608}
609
Mark Lobodzinskicf26e072015-04-16 11:44:05 -0500610static void demo_destroy_texture_image(struct demo *demo, struct texture_object *tex_obj)
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700611{
612 /* clean up staging resources */
Mike Stroyan230e6252015-04-17 12:36:38 -0600613 vkDestroyObject(demo->device, VK_OBJECT_TYPE_IMAGE, tex_obj->image);
Courtney Goeltzenleuchtera063d9b2015-06-10 16:16:22 -0600614 vkFreeMemory(demo->device, tex_obj->mem);
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700615}
616
617static void demo_prepare_textures(struct demo *demo)
618{
Tony Barbour8205d902015-04-16 15:59:00 -0600619 const VkFormat tex_format = VK_FORMAT_B8G8R8A8_UNORM;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600620 VkFormatProperties props;
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700621 const uint32_t tex_colors[DEMO_TEXTURE_COUNT][2] = {
622 { 0xffff0000, 0xff00ff00 },
623 };
Tony Barbour22a30862015-04-22 09:02:32 -0600624 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700625 uint32_t i;
626
Chris Forbesd7576302015-06-21 22:55:02 +1200627 err = vkGetPhysicalDeviceFormatInfo(demo->gpu, tex_format, &props);
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700628 assert(!err);
629
630 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Tony Barbour8205d902015-04-16 15:59:00 -0600631 if ((props.linearTilingFeatures & VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT) && !demo->use_staging_buffer) {
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700632 /* Device can texture using linear textures */
633 demo_prepare_texture_image(demo, tex_colors[i], &demo->textures[i],
Courtney Goeltzenleuchtercb67a322015-04-21 09:31:23 -0600634 VK_IMAGE_TILING_LINEAR, VK_IMAGE_USAGE_SAMPLED_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT);
Tony Barbour8205d902015-04-16 15:59:00 -0600635 } else if (props.optimalTilingFeatures & VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT){
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700636 /* Must use staging buffer to copy linear texture to optimized */
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600637 struct texture_object staging_texture;
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700638
639 memset(&staging_texture, 0, sizeof(staging_texture));
640 demo_prepare_texture_image(demo, tex_colors[i], &staging_texture,
Courtney Goeltzenleuchtercb67a322015-04-21 09:31:23 -0600641 VK_IMAGE_TILING_LINEAR, VK_IMAGE_USAGE_TRANSFER_SOURCE_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT);
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700642
643 demo_prepare_texture_image(demo, tex_colors[i], &demo->textures[i],
Courtney Goeltzenleuchtercb67a322015-04-21 09:31:23 -0600644 VK_IMAGE_TILING_OPTIMAL,
645 (VK_IMAGE_USAGE_TRANSFER_DESTINATION_BIT | VK_IMAGE_USAGE_SAMPLED_BIT),
646 VK_MEMORY_PROPERTY_DEVICE_ONLY);
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700647
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600648 demo_set_image_layout(demo, staging_texture.image,
malnasse4b8ba4d2015-06-03 17:28:38 -0400649 VK_IMAGE_ASPECT_COLOR,
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600650 staging_texture.imageLayout,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600651 VK_IMAGE_LAYOUT_TRANSFER_SOURCE_OPTIMAL);
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700652
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600653 demo_set_image_layout(demo, demo->textures[i].image,
malnasse4b8ba4d2015-06-03 17:28:38 -0400654 VK_IMAGE_ASPECT_COLOR,
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600655 demo->textures[i].imageLayout,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600656 VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL);
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700657
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600658 VkImageCopy copy_region = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600659 .srcSubresource = { VK_IMAGE_ASPECT_COLOR, 0, 0 },
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700660 .srcOffset = { 0, 0, 0 },
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600661 .destSubresource = { VK_IMAGE_ASPECT_COLOR, 0, 0 },
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700662 .destOffset = { 0, 0, 0 },
663 .extent = { staging_texture.tex_width, staging_texture.tex_height, 1 },
664 };
Courtney Goeltzenleuchter633f9c52015-04-30 10:58:33 -0600665 vkCmdCopyImage(demo->setup_cmd,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600666 staging_texture.image, VK_IMAGE_LAYOUT_TRANSFER_SOURCE_OPTIMAL,
667 demo->textures[i].image, VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL,
Courtney Goeltzenleuchter51cbf302015-03-25 11:25:10 -0600668 1, &copy_region);
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700669
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600670 demo_set_image_layout(demo, demo->textures[i].image,
malnasse4b8ba4d2015-06-03 17:28:38 -0400671 VK_IMAGE_ASPECT_COLOR,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600672 VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL,
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600673 demo->textures[i].imageLayout);
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700674
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600675 demo_flush_init_cmd(demo);
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700676
Courtney Goeltzenleuchter876629f2015-04-21 09:30:03 -0600677 demo_destroy_texture_image(demo, &staging_texture);
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700678 } else {
Tony Barbour8205d902015-04-16 15:59:00 -0600679 /* Can't support VK_FORMAT_B8G8R8A8_UNORM !? */
Piers Daniell886be472015-02-23 16:23:13 -0700680 assert(!"No support for B8G8R8A8_UNORM as texture image format");
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700681 }
Courtney Goeltzenleuchter3226a6f2015-02-11 18:17:22 -0700682
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600683 const VkSamplerCreateInfo sampler = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600684 .sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO,
Chia-I Wub043fe32014-10-06 15:30:33 +0800685 .pNext = NULL,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600686 .magFilter = VK_TEX_FILTER_NEAREST,
687 .minFilter = VK_TEX_FILTER_NEAREST,
Tony Barbour8205d902015-04-16 15:59:00 -0600688 .mipMode = VK_TEX_MIPMAP_MODE_BASE,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600689 .addressU = VK_TEX_ADDRESS_WRAP,
690 .addressV = VK_TEX_ADDRESS_WRAP,
691 .addressW = VK_TEX_ADDRESS_WRAP,
Chia-I Wub043fe32014-10-06 15:30:33 +0800692 .mipLodBias = 0.0f,
Courtney Goeltzenleuchterbc9c8162015-02-13 18:20:24 -0700693 .maxAnisotropy = 1,
Tony Barbour8205d902015-04-16 15:59:00 -0600694 .compareOp = VK_COMPARE_OP_NEVER,
Chia-I Wub043fe32014-10-06 15:30:33 +0800695 .minLod = 0.0f,
696 .maxLod = 0.0f,
Tony Barbour2c4e7c72015-06-25 16:56:44 -0600697 .borderColor = VK_BORDER_COLOR_FLOAT_OPAQUE_WHITE,
Chia-I Wub043fe32014-10-06 15:30:33 +0800698 };
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600699 VkImageViewCreateInfo view = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600700 .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
Chia-I Wub043fe32014-10-06 15:30:33 +0800701 .pNext = NULL,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600702 .image = VK_NULL_HANDLE,
Tony Barbour8205d902015-04-16 15:59:00 -0600703 .viewType = VK_IMAGE_VIEW_TYPE_2D,
Courtney Goeltzenleuchter372e13c2015-02-13 17:52:46 -0700704 .format = tex_format,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600705 .channels = { VK_CHANNEL_SWIZZLE_R,
706 VK_CHANNEL_SWIZZLE_G,
707 VK_CHANNEL_SWIZZLE_B,
708 VK_CHANNEL_SWIZZLE_A, },
709 .subresourceRange = { VK_IMAGE_ASPECT_COLOR, 0, 1, 0, 1 },
Chia-I Wub043fe32014-10-06 15:30:33 +0800710 };
Jon Ashburna9ae3832015-01-16 09:37:43 -0700711
Chia-I Wub043fe32014-10-06 15:30:33 +0800712 /* create sampler */
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600713 err = vkCreateSampler(demo->device, &sampler,
Chia-I Wub043fe32014-10-06 15:30:33 +0800714 &demo->textures[i].sampler);
715 assert(!err);
716
Chia-I Wub043fe32014-10-06 15:30:33 +0800717 /* create image view */
718 view.image = demo->textures[i].image;
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600719 err = vkCreateImageView(demo->device, &view,
Courtney Goeltzenleuchter3226a6f2015-02-11 18:17:22 -0700720 &demo->textures[i].view);
Chia-I Wub043fe32014-10-06 15:30:33 +0800721 assert(!err);
Chia-I Wub043fe32014-10-06 15:30:33 +0800722 }
723}
724
Chia-I Wu99621bc2014-10-08 11:52:22 +0800725static void demo_prepare_vertices(struct demo *demo)
726{
727 const float vb[3][5] = {
728 /* position texcoord */
Chia-I Wue2504cb2015-04-22 14:20:52 +0800729 { -1.0f, -1.0f, 0.2f, 0.0f, 0.0f },
730 { 1.0f, -1.0f, 0.25f, 1.0f, 0.0f },
Chia-I Wu99621bc2014-10-08 11:52:22 +0800731 { 0.0f, 1.0f, 1.0f, 0.5f, 1.0f },
732 };
Courtney Goeltzenleuchterddcb6192015-04-14 18:48:46 -0600733 const VkBufferCreateInfo buf_info = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600734 .sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO,
Chia-I Wu714df452015-01-01 07:55:04 +0800735 .pNext = NULL,
736 .size = sizeof(vb),
Courtney Goeltzenleuchterad870812015-04-15 15:29:59 -0600737 .usage = VK_BUFFER_USAGE_VERTEX_BUFFER_BIT,
Chia-I Wu714df452015-01-01 07:55:04 +0800738 .flags = 0,
739 };
Courtney Goeltzenleuchterddcb6192015-04-14 18:48:46 -0600740 VkMemoryAllocInfo mem_alloc = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600741 .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOC_INFO,
Mark Lobodzinski97dcd042015-04-16 08:52:00 -0500742 .pNext = NULL,
Chia-I Wu714df452015-01-01 07:55:04 +0800743 .allocationSize = 0,
Mark Lobodzinski72346292015-07-02 16:49:40 -0600744 .memoryTypeIndex = 0,
Chia-I Wu99621bc2014-10-08 11:52:22 +0800745 };
Mark Lobodzinski23182612015-05-29 09:32:35 -0500746 VkMemoryRequirements mem_reqs;
Tony Barbour22a30862015-04-22 09:02:32 -0600747 VkResult U_ASSERT_ONLY err;
Chia-I Wu99621bc2014-10-08 11:52:22 +0800748 void *data;
749
750 memset(&demo->vertices, 0, sizeof(demo->vertices));
751
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600752 err = vkCreateBuffer(demo->device, &buf_info, &demo->vertices.buf);
Chia-I Wu714df452015-01-01 07:55:04 +0800753 assert(!err);
754
Tony Barbour426b9052015-06-24 16:06:58 -0600755 err = vkGetObjectMemoryRequirements(demo->device,
756 VK_OBJECT_TYPE_BUFFER, demo->vertices.buf, &mem_reqs);
Chia-I Wu714df452015-01-01 07:55:04 +0800757
Mark Lobodzinski72346292015-07-02 16:49:40 -0600758 mem_alloc.allocationSize = mem_reqs.size;
759 err = memory_type_from_properties(demo,
760 mem_reqs.memoryTypeBits,
761 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT,
762 &mem_alloc.memoryTypeIndex);
763 assert(!err);
Chia-I Wu99621bc2014-10-08 11:52:22 +0800764
Mark Lobodzinski23182612015-05-29 09:32:35 -0500765 err = vkAllocMemory(demo->device, &mem_alloc, &demo->vertices.mem);
766 assert(!err);
Chia-I Wu99621bc2014-10-08 11:52:22 +0800767
Mark Lobodzinski23182612015-05-29 09:32:35 -0500768 err = vkMapMemory(demo->device, demo->vertices.mem, 0, 0, 0, &data);
769 assert(!err);
Chia-I Wu99621bc2014-10-08 11:52:22 +0800770
Mark Lobodzinski23182612015-05-29 09:32:35 -0500771 memcpy(data, vb, sizeof(vb));
Chia-I Wu99621bc2014-10-08 11:52:22 +0800772
Mark Lobodzinski23182612015-05-29 09:32:35 -0500773 err = vkUnmapMemory(demo->device, demo->vertices.mem);
774 assert(!err);
775
776 err = vkBindObjectMemory(demo->device,
777 VK_OBJECT_TYPE_BUFFER, demo->vertices.buf,
778 demo->vertices.mem, 0);
779 assert(!err);
Chia-I Wu714df452015-01-01 07:55:04 +0800780
Mark Lobodzinski0e0fb5c2015-06-23 15:11:57 -0600781 demo->vertices.vi.sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO;
Chia-I Wu8d29d022014-10-08 12:14:39 +0800782 demo->vertices.vi.pNext = NULL;
783 demo->vertices.vi.bindingCount = 1;
784 demo->vertices.vi.pVertexBindingDescriptions = demo->vertices.vi_bindings;
785 demo->vertices.vi.attributeCount = 2;
786 demo->vertices.vi.pVertexAttributeDescriptions = demo->vertices.vi_attrs;
787
Courtney Goeltzenleuchterf5cdad02015-03-31 16:36:30 -0600788 demo->vertices.vi_bindings[0].binding = VERTEX_BUFFER_BIND_ID;
Chia-I Wu8d29d022014-10-08 12:14:39 +0800789 demo->vertices.vi_bindings[0].strideInBytes = sizeof(vb[0]);
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600790 demo->vertices.vi_bindings[0].stepRate = VK_VERTEX_INPUT_STEP_RATE_VERTEX;
Chia-I Wu8d29d022014-10-08 12:14:39 +0800791
Courtney Goeltzenleuchterf5cdad02015-03-31 16:36:30 -0600792 demo->vertices.vi_attrs[0].binding = VERTEX_BUFFER_BIND_ID;
793 demo->vertices.vi_attrs[0].location = 0;
Tony Barbour8205d902015-04-16 15:59:00 -0600794 demo->vertices.vi_attrs[0].format = VK_FORMAT_R32G32B32_SFLOAT;
Chia-I Wu8d29d022014-10-08 12:14:39 +0800795 demo->vertices.vi_attrs[0].offsetInBytes = 0;
796
Courtney Goeltzenleuchterf5cdad02015-03-31 16:36:30 -0600797 demo->vertices.vi_attrs[1].binding = VERTEX_BUFFER_BIND_ID;
798 demo->vertices.vi_attrs[1].location = 1;
Tony Barbour8205d902015-04-16 15:59:00 -0600799 demo->vertices.vi_attrs[1].format = VK_FORMAT_R32G32_SFLOAT;
Chia-I Wu8d29d022014-10-08 12:14:39 +0800800 demo->vertices.vi_attrs[1].offsetInBytes = sizeof(float) * 3;
Chia-I Wu99621bc2014-10-08 11:52:22 +0800801}
802
Chia-I Wuf8385062015-01-04 16:27:24 +0800803static void demo_prepare_descriptor_layout(struct demo *demo)
Chia-I Wub043fe32014-10-06 15:30:33 +0800804{
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600805 const VkDescriptorSetLayoutBinding layout_binding = {
Courtney Goeltzenleuchterad870812015-04-15 15:29:59 -0600806 .descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
Chia-I Wud3114a22015-05-25 16:22:52 +0800807 .arraySize = DEMO_TEXTURE_COUNT,
Tony Barbour8205d902015-04-16 15:59:00 -0600808 .stageFlags = VK_SHADER_STAGE_FRAGMENT_BIT,
Chia-I Wu310eece2015-03-27 12:56:09 +0800809 .pImmutableSamplers = NULL,
Chia-I Wub043fe32014-10-06 15:30:33 +0800810 };
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600811 const VkDescriptorSetLayoutCreateInfo descriptor_layout = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600812 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO,
Chia-I Wufc9d9132015-03-26 15:04:41 +0800813 .pNext = NULL,
814 .count = 1,
815 .pBinding = &layout_binding,
816 };
Tony Barbour22a30862015-04-22 09:02:32 -0600817 VkResult U_ASSERT_ONLY err;
Chia-I Wub043fe32014-10-06 15:30:33 +0800818
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600819 err = vkCreateDescriptorSetLayout(demo->device,
Chia-I Wu7732cb22015-03-26 15:27:55 +0800820 &descriptor_layout, &demo->desc_layout);
821 assert(!err);
822
Mark Lobodzinski556f7212015-04-17 14:11:39 -0500823 const VkPipelineLayoutCreateInfo pPipelineLayoutCreateInfo = {
824 .sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO,
825 .pNext = NULL,
826 .descriptorSetCount = 1,
827 .pSetLayouts = &demo->desc_layout,
828 };
829
830 err = vkCreatePipelineLayout(demo->device,
831 &pPipelineLayoutCreateInfo,
832 &demo->pipeline_layout);
Chia-I Wub043fe32014-10-06 15:30:33 +0800833 assert(!err);
Chia-I Wub043fe32014-10-06 15:30:33 +0800834}
835
Chia-I Wu76cd4222015-07-08 13:34:24 +0800836static void demo_prepare_render_pass(struct demo *demo)
837{
838 const VkClearColorValue clear_color = {
839 .f32 = { 0.2f, 0.2f, 0.2f, 0.2f },
840 };
841 const float clear_depth = 0.9f;
842 VkResult U_ASSERT_ONLY err;
843 const VkImageLayout color_layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
844 const VkAttachmentLoadOp color_load_op = VK_ATTACHMENT_LOAD_OP_CLEAR;
Chia-I Wu337d70a2015-07-08 13:58:11 +0800845 const VkAttachmentStoreOp color_store_op = VK_ATTACHMENT_STORE_OP_STORE;
Chia-I Wu76cd4222015-07-08 13:34:24 +0800846 const VkRenderPassCreateInfo rp_info = {
847 .sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO,
848 .pNext = NULL,
849 .renderArea.offset.x = 0,
850 .renderArea.offset.y = 0,
851 .renderArea.extent.width = demo->width,
852 .renderArea.extent.height = demo->height,
853 .colorAttachmentCount = 1,
854 .extent.width = demo->width,
855 .extent.height = demo->height,
856 .sampleCount = 1,
857 .layers = 1,
858 .pColorFormats = &demo->format,
859 .pColorLayouts = &color_layout,
860 .pColorLoadOps = &color_load_op,
861 .pColorStoreOps = &color_store_op,
862 .pColorLoadClearValues = &clear_color,
863 .depthStencilFormat = demo->depth.format,
864 .depthStencilLayout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
865 .depthLoadOp = VK_ATTACHMENT_LOAD_OP_CLEAR,
866 .depthLoadClearValue = clear_depth,
867 .depthStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE,
868 .stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE,
869 .stencilLoadClearValue = 0,
870 .stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE,
871 };
872
873 err = vkCreateRenderPass(demo->device, &rp_info, &demo->render_pass);
874 assert(!err);
875}
876
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600877static VkShader demo_prepare_shader(struct demo *demo,
Tony Barbour8205d902015-04-16 15:59:00 -0600878 VkShaderStage stage,
Chia-I Wuc19795a2014-09-13 11:12:55 +0800879 const void *code,
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600880 size_t size)
Chia-I Wuc19795a2014-09-13 11:12:55 +0800881{
Courtney Goeltzenleuchter2d034fd2015-06-28 13:01:17 -0600882 VkShaderModuleCreateInfo moduleCreateInfo;
883 VkShaderCreateInfo shaderCreateInfo;
884 VkShaderModule shaderModule;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600885 VkShader shader;
886 VkResult err;
Chia-I Wuc19795a2014-09-13 11:12:55 +0800887
Courtney Goeltzenleuchter2d034fd2015-06-28 13:01:17 -0600888
889 moduleCreateInfo.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
890 moduleCreateInfo.pNext = NULL;
891
892 shaderCreateInfo.sType = VK_STRUCTURE_TYPE_SHADER_CREATE_INFO;
893 shaderCreateInfo.pNext = NULL;
Courtney Goeltzenleuchter3f2606d2014-10-13 17:51:58 -0600894
Cody Northrop75db0322015-05-28 11:27:16 -0600895 if (!demo->use_glsl) {
Courtney Goeltzenleuchter2d034fd2015-06-28 13:01:17 -0600896 moduleCreateInfo.codeSize = size;
897 moduleCreateInfo.pCode = code;
898 moduleCreateInfo.flags = 0;
899 err = vkCreateShaderModule(demo->device, &moduleCreateInfo, &shaderModule);
900 if (err) {
901 free((void *) moduleCreateInfo.pCode);
902 }
Courtney Goeltzenleuchter3f2606d2014-10-13 17:51:58 -0600903
Courtney Goeltzenleuchter2d034fd2015-06-28 13:01:17 -0600904 shaderCreateInfo.flags = 0;
905 shaderCreateInfo.module = shaderModule;
906 err = vkCreateShader(demo->device, &shaderCreateInfo, &shader);
Cody Northrop75db0322015-05-28 11:27:16 -0600907 } else {
908 // Create fake SPV structure to feed GLSL
909 // to the driver "under the covers"
Courtney Goeltzenleuchter2d034fd2015-06-28 13:01:17 -0600910 moduleCreateInfo.codeSize = 3 * sizeof(uint32_t) + size + 1;
911 moduleCreateInfo.pCode = malloc(moduleCreateInfo.codeSize);
912 moduleCreateInfo.flags = 0;
Courtney Goeltzenleuchter3f2606d2014-10-13 17:51:58 -0600913
Cody Northrop75db0322015-05-28 11:27:16 -0600914 /* try version 0 first: VkShaderStage followed by GLSL */
Courtney Goeltzenleuchter2d034fd2015-06-28 13:01:17 -0600915 ((uint32_t *) moduleCreateInfo.pCode)[0] = ICD_SPV_MAGIC;
916 ((uint32_t *) moduleCreateInfo.pCode)[1] = 0;
917 ((uint32_t *) moduleCreateInfo.pCode)[2] = stage;
918 memcpy(((uint32_t *) moduleCreateInfo.pCode + 3), code, size + 1);
Cody Northrop75db0322015-05-28 11:27:16 -0600919
Courtney Goeltzenleuchter2d034fd2015-06-28 13:01:17 -0600920 err = vkCreateShaderModule(demo->device, &moduleCreateInfo, &shaderModule);
Cody Northrop75db0322015-05-28 11:27:16 -0600921 if (err) {
Courtney Goeltzenleuchter2d034fd2015-06-28 13:01:17 -0600922 free((void *) moduleCreateInfo.pCode);
Cody Northrop75db0322015-05-28 11:27:16 -0600923 }
Chia-I Wuc19795a2014-09-13 11:12:55 +0800924
Courtney Goeltzenleuchter2d034fd2015-06-28 13:01:17 -0600925 shaderCreateInfo.flags = 0;
926 shaderCreateInfo.module = shaderModule;
927 err = vkCreateShader(demo->device, &shaderCreateInfo, &shader);
928 }
Chia-I Wuc19795a2014-09-13 11:12:55 +0800929 return shader;
930}
931
Cody Northrop75db0322015-05-28 11:27:16 -0600932char *demo_read_spv(const char *filename, size_t *psize)
933{
934 long int size;
935 void *shader_code;
936
937 FILE *fp = fopen(filename, "rb");
938 if (!fp) return NULL;
939
940 fseek(fp, 0L, SEEK_END);
941 size = ftell(fp);
942
943 fseek(fp, 0L, SEEK_SET);
944
945 shader_code = malloc(size);
946 fread(shader_code, size, 1, fp);
947
948 *psize = size;
949
950 return shader_code;
951}
952
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600953static VkShader demo_prepare_vs(struct demo *demo)
Chia-I Wuc19795a2014-09-13 11:12:55 +0800954{
Cody Northrop75db0322015-05-28 11:27:16 -0600955 if (!demo->use_glsl) {
956 void *vertShaderCode;
957 size_t size;
958
959 vertShaderCode = demo_read_spv("tri-vert.spv", &size);
960
961 return demo_prepare_shader(demo, VK_SHADER_STAGE_VERTEX,
962 vertShaderCode, size);
963 } else {
964 static const char *vertShaderText =
Mark Lobodzinskiba4d2f02015-04-06 15:24:40 -0500965 "#version 140\n"
Courtney Goeltzenleuchterf5cdad02015-03-31 16:36:30 -0600966 "#extension GL_ARB_separate_shader_objects : enable\n"
967 "#extension GL_ARB_shading_language_420pack : enable\n"
968 "layout (location = 0) in vec4 pos;\n"
969 "layout (location = 1) in vec2 attr;\n"
Chia-I Wuf5caeb02014-10-25 12:11:27 +0800970 "out vec2 texcoord;\n"
Courtney Goeltzenleuchter3f2606d2014-10-13 17:51:58 -0600971 "void main() {\n"
Chia-I Wuf5caeb02014-10-25 12:11:27 +0800972 " texcoord = attr;\n"
973 " gl_Position = pos;\n"
Courtney Goeltzenleuchter3f2606d2014-10-13 17:51:58 -0600974 "}\n";
Courtney Goeltzenleuchteref7301b2014-09-17 13:17:12 -0600975
Cody Northrop75db0322015-05-28 11:27:16 -0600976 return demo_prepare_shader(demo, VK_SHADER_STAGE_VERTEX,
977 (const void *) vertShaderText,
978 strlen(vertShaderText));
979 }
Chia-I Wuc19795a2014-09-13 11:12:55 +0800980}
981
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600982static VkShader demo_prepare_fs(struct demo *demo)
Chia-I Wuc19795a2014-09-13 11:12:55 +0800983{
Cody Northrop75db0322015-05-28 11:27:16 -0600984 if (!demo->use_glsl) {
985 void *fragShaderCode;
986 size_t size;
Courtney Goeltzenleuchteref7301b2014-09-17 13:17:12 -0600987
Cody Northrop75db0322015-05-28 11:27:16 -0600988 fragShaderCode = demo_read_spv("tri-frag.spv", &size);
989
990 return demo_prepare_shader(demo, VK_SHADER_STAGE_FRAGMENT,
991 fragShaderCode, size);
992 } else {
993 static const char *fragShaderText =
994 "#version 140\n"
995 "#extension GL_ARB_separate_shader_objects : enable\n"
996 "#extension GL_ARB_shading_language_420pack : enable\n"
997 "layout (binding = 0) uniform sampler2D tex;\n"
998 "layout (location = 0) in vec2 texcoord;\n"
999 "layout (location = 0) out vec4 uFragColor;\n"
1000 "void main() {\n"
1001 " uFragColor = texture(tex, texcoord);\n"
1002 "}\n";
1003
1004 return demo_prepare_shader(demo, VK_SHADER_STAGE_FRAGMENT,
1005 (const void *) fragShaderText,
1006 strlen(fragShaderText));
1007 }
Chia-I Wuc19795a2014-09-13 11:12:55 +08001008}
1009
1010static void demo_prepare_pipeline(struct demo *demo)
1011{
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001012 VkGraphicsPipelineCreateInfo pipeline;
Jon Ashburn0d60d272015-07-09 15:02:25 -06001013 VkPipelineCacheCreateInfo pipelineCache;
Mark Lobodzinski0e0fb5c2015-06-23 15:11:57 -06001014
1015 VkPipelineVertexInputStateCreateInfo vi;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001016 VkPipelineIaStateCreateInfo ia;
1017 VkPipelineRsStateCreateInfo rs;
1018 VkPipelineCbStateCreateInfo cb;
1019 VkPipelineDsStateCreateInfo ds;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001020 VkPipelineVpStateCreateInfo vp;
1021 VkPipelineMsStateCreateInfo ms;
Mark Lobodzinski0e0fb5c2015-06-23 15:11:57 -06001022
Tony Barbour22a30862015-04-22 09:02:32 -06001023 VkResult U_ASSERT_ONLY err;
Chia-I Wuc19795a2014-09-13 11:12:55 +08001024
1025 memset(&pipeline, 0, sizeof(pipeline));
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001026 pipeline.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
Mark Lobodzinski556f7212015-04-17 14:11:39 -05001027 pipeline.layout = demo->pipeline_layout;
Chia-I Wuc19795a2014-09-13 11:12:55 +08001028
Chia-I Wu8d29d022014-10-08 12:14:39 +08001029 vi = demo->vertices.vi;
1030
Chia-I Wuc19795a2014-09-13 11:12:55 +08001031 memset(&ia, 0, sizeof(ia));
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001032 ia.sType = VK_STRUCTURE_TYPE_PIPELINE_IA_STATE_CREATE_INFO;
Tony Barbour8205d902015-04-16 15:59:00 -06001033 ia.topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
Chia-I Wuc19795a2014-09-13 11:12:55 +08001034
1035 memset(&rs, 0, sizeof(rs));
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001036 rs.sType = VK_STRUCTURE_TYPE_PIPELINE_RS_STATE_CREATE_INFO;
Tony Barbour8205d902015-04-16 15:59:00 -06001037 rs.fillMode = VK_FILL_MODE_SOLID;
Chia-I Wuc414ba82015-04-22 15:44:24 +08001038 rs.cullMode = VK_CULL_MODE_BACK;
1039 rs.frontFace = VK_FRONT_FACE_CW;
Chia-I Wue2504cb2015-04-22 14:20:52 +08001040 rs.depthClipEnable = VK_TRUE;
Chia-I Wuc19795a2014-09-13 11:12:55 +08001041
1042 memset(&cb, 0, sizeof(cb));
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001043 cb.sType = VK_STRUCTURE_TYPE_PIPELINE_CB_STATE_CREATE_INFO;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001044 VkPipelineCbAttachmentState att_state[1];
Tony Barbourfa6cac72015-01-16 14:27:35 -07001045 memset(att_state, 0, sizeof(att_state));
1046 att_state[0].format = demo->format;
1047 att_state[0].channelWriteMask = 0xf;
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001048 att_state[0].blendEnable = VK_FALSE;
Tony Barbourfa6cac72015-01-16 14:27:35 -07001049 cb.attachmentCount = 1;
1050 cb.pAttachments = att_state;
Chia-I Wuc19795a2014-09-13 11:12:55 +08001051
Tony Barbourfa6cac72015-01-16 14:27:35 -07001052 memset(&vp, 0, sizeof(vp));
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001053 vp.sType = VK_STRUCTURE_TYPE_PIPELINE_VP_STATE_CREATE_INFO;
Tony Barbour8205d902015-04-16 15:59:00 -06001054 vp.viewportCount = 1;
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001055 vp.clipOrigin = VK_COORDINATE_ORIGIN_UPPER_LEFT;
Tony Barbourfa6cac72015-01-16 14:27:35 -07001056
1057 memset(&ds, 0, sizeof(ds));
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001058 ds.sType = VK_STRUCTURE_TYPE_PIPELINE_DS_STATE_CREATE_INFO;
Tony Barbourfa6cac72015-01-16 14:27:35 -07001059 ds.format = demo->depth.format;
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001060 ds.depthTestEnable = VK_TRUE;
1061 ds.depthWriteEnable = VK_TRUE;
Tony Barbour8205d902015-04-16 15:59:00 -06001062 ds.depthCompareOp = VK_COMPARE_OP_LESS_EQUAL;
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001063 ds.depthBoundsEnable = VK_FALSE;
1064 ds.back.stencilFailOp = VK_STENCIL_OP_KEEP;
1065 ds.back.stencilPassOp = VK_STENCIL_OP_KEEP;
Tony Barbour8205d902015-04-16 15:59:00 -06001066 ds.back.stencilCompareOp = VK_COMPARE_OP_ALWAYS;
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001067 ds.stencilTestEnable = VK_FALSE;
Tony Barbourfa6cac72015-01-16 14:27:35 -07001068 ds.front = ds.back;
Chia-I Wuc19795a2014-09-13 11:12:55 +08001069
Tony Barbourfa6cac72015-01-16 14:27:35 -07001070 memset(&ms, 0, sizeof(ms));
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001071 ms.sType = VK_STRUCTURE_TYPE_PIPELINE_MS_STATE_CREATE_INFO;
Tony Barbourfa6cac72015-01-16 14:27:35 -07001072 ms.sampleMask = 1;
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001073 ms.multisampleEnable = VK_FALSE;
Tony Barboure094edf2015-06-26 10:18:34 -06001074 ms.rasterSamples = 1;
Chia-I Wub043fe32014-10-06 15:30:33 +08001075
Mark Lobodzinski0e0fb5c2015-06-23 15:11:57 -06001076 // Two stages: vs and fs
1077 pipeline.stageCount = 2;
1078 VkPipelineShaderStageCreateInfo shaderStages[2];
1079 memset(&shaderStages, 0, 2 * sizeof(VkPipelineShaderStageCreateInfo));
1080
1081 shaderStages[0].sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
1082 shaderStages[0].stage = VK_SHADER_STAGE_VERTEX;
1083 shaderStages[0].shader = demo_prepare_vs(demo);
1084 shaderStages[0].linkConstBufferCount = 0;
1085
1086 shaderStages[1].sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
1087 shaderStages[1].stage = VK_SHADER_STAGE_FRAGMENT;
1088 shaderStages[1].shader = demo_prepare_fs(demo);
1089
1090 pipeline.pVertexInputState = &vi;
1091 pipeline.pIaState = &ia;
1092 pipeline.pRsState = &rs;
1093 pipeline.pCbState = &cb;
1094 pipeline.pMsState = &ms;
1095 pipeline.pVpState = &vp;
1096 pipeline.pDsState = &ds;
1097 pipeline.pStages = shaderStages;
Chia-I Wuc19795a2014-09-13 11:12:55 +08001098
Jon Ashburn0d60d272015-07-09 15:02:25 -06001099 memset(&pipelineCache, 0, sizeof(pipelineCache));
1100 pipelineCache.sType = VK_STRUCTURE_TYPE_PIPELINE_CACHE_CREATE_INFO;
1101
1102 err = vkCreatePipelineCache(demo->device, &pipelineCache, &demo->pipelineCache);
1103 assert(!err);
1104 err = vkCreateGraphicsPipelines(demo->device, demo->pipelineCache, 1, &pipeline, &demo->pipeline);
Chia-I Wuc19795a2014-09-13 11:12:55 +08001105 assert(!err);
1106
Mark Lobodzinski0e0fb5c2015-06-23 15:11:57 -06001107 for (uint32_t i = 0; i < pipeline.stageCount; i++) {
1108 vkDestroyObject(demo->device, VK_OBJECT_TYPE_SHADER, shaderStages[i].shader);
1109 }
Chia-I Wuc19795a2014-09-13 11:12:55 +08001110}
1111
1112static void demo_prepare_dynamic_states(struct demo *demo)
1113{
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001114 VkDynamicVpStateCreateInfo viewport_create;
1115 VkDynamicRsStateCreateInfo raster;
1116 VkDynamicCbStateCreateInfo color_blend;
1117 VkDynamicDsStateCreateInfo depth_stencil;
Tony Barbour22a30862015-04-22 09:02:32 -06001118 VkResult U_ASSERT_ONLY err;
Chia-I Wuc19795a2014-09-13 11:12:55 +08001119
Tony Barbourfa6cac72015-01-16 14:27:35 -07001120 memset(&viewport_create, 0, sizeof(viewport_create));
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001121 viewport_create.sType = VK_STRUCTURE_TYPE_DYNAMIC_VP_STATE_CREATE_INFO;
Courtney Goeltzenleuchterc6e32f92015-02-11 14:13:34 -07001122 viewport_create.viewportAndScissorCount = 1;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001123 VkViewport viewport;
Chia-I Wuc19795a2014-09-13 11:12:55 +08001124 memset(&viewport, 0, sizeof(viewport));
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -06001125 viewport.height = (float) demo->height;
Courtney Goeltzenleuchterc6e32f92015-02-11 14:13:34 -07001126 viewport.width = (float) demo->width;
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -06001127 viewport.minDepth = (float) 0.0f;
1128 viewport.maxDepth = (float) 1.0f;
Piers Daniell886be472015-02-23 16:23:13 -07001129 viewport_create.pViewports = &viewport;
Chris Forbes2951d7d2015-06-22 17:21:59 +12001130 VkRect2D scissor;
Piers Daniell886be472015-02-23 16:23:13 -07001131 memset(&scissor, 0, sizeof(scissor));
Courtney Goeltzenleuchterc6e32f92015-02-11 14:13:34 -07001132 scissor.extent.width = demo->width;
1133 scissor.extent.height = demo->height;
1134 scissor.offset.x = 0;
1135 scissor.offset.y = 0;
Courtney Goeltzenleuchterc6e32f92015-02-11 14:13:34 -07001136 viewport_create.pScissors = &scissor;
Chia-I Wuc19795a2014-09-13 11:12:55 +08001137
1138 memset(&raster, 0, sizeof(raster));
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001139 raster.sType = VK_STRUCTURE_TYPE_DYNAMIC_RS_STATE_CREATE_INFO;
Piers Daniell886be472015-02-23 16:23:13 -07001140 raster.lineWidth = 1.0;
Chia-I Wuc19795a2014-09-13 11:12:55 +08001141
1142 memset(&color_blend, 0, sizeof(color_blend));
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001143 color_blend.sType = VK_STRUCTURE_TYPE_DYNAMIC_CB_STATE_CREATE_INFO;
Piers Daniell886be472015-02-23 16:23:13 -07001144 color_blend.blendConst[0] = 1.0f;
1145 color_blend.blendConst[1] = 1.0f;
1146 color_blend.blendConst[2] = 1.0f;
1147 color_blend.blendConst[3] = 1.0f;
Chia-I Wuc19795a2014-09-13 11:12:55 +08001148
1149 memset(&depth_stencil, 0, sizeof(depth_stencil));
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001150 depth_stencil.sType = VK_STRUCTURE_TYPE_DYNAMIC_DS_STATE_CREATE_INFO;
Mark Lobodzinski4405fbf2015-06-12 11:14:17 -06001151 depth_stencil.minDepthBounds = 0.0f;
1152 depth_stencil.maxDepthBounds = 1.0f;
Tony Barbourfa6cac72015-01-16 14:27:35 -07001153 depth_stencil.stencilBackRef = 0;
1154 depth_stencil.stencilFrontRef = 0;
1155 depth_stencil.stencilReadMask = 0xff;
1156 depth_stencil.stencilWriteMask = 0xff;
Chia-I Wuc19795a2014-09-13 11:12:55 +08001157
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001158 err = vkCreateDynamicViewportState(demo->device, &viewport_create, &demo->viewport);
Chia-I Wuc19795a2014-09-13 11:12:55 +08001159 assert(!err);
1160
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001161 err = vkCreateDynamicRasterState(demo->device, &raster, &demo->raster);
Chia-I Wuc19795a2014-09-13 11:12:55 +08001162 assert(!err);
1163
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001164 err = vkCreateDynamicColorBlendState(demo->device,
Chia-I Wuc19795a2014-09-13 11:12:55 +08001165 &color_blend, &demo->color_blend);
1166 assert(!err);
1167
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001168 err = vkCreateDynamicDepthStencilState(demo->device,
Chia-I Wuc19795a2014-09-13 11:12:55 +08001169 &depth_stencil, &demo->depth_stencil);
1170 assert(!err);
1171}
1172
Chia-I Wu8d24b3b2015-03-26 13:14:16 +08001173static void demo_prepare_descriptor_pool(struct demo *demo)
Chia-I Wuf8385062015-01-04 16:27:24 +08001174{
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001175 const VkDescriptorTypeCount type_count = {
Courtney Goeltzenleuchterad870812015-04-15 15:29:59 -06001176 .type = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
Chia-I Wuf8385062015-01-04 16:27:24 +08001177 .count = DEMO_TEXTURE_COUNT,
1178 };
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001179 const VkDescriptorPoolCreateInfo descriptor_pool = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001180 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO,
Chia-I Wuf8385062015-01-04 16:27:24 +08001181 .pNext = NULL,
1182 .count = 1,
1183 .pTypeCount = &type_count,
1184 };
Tony Barbour22a30862015-04-22 09:02:32 -06001185 VkResult U_ASSERT_ONLY err;
Chia-I Wuf8385062015-01-04 16:27:24 +08001186
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001187 err = vkCreateDescriptorPool(demo->device,
1188 VK_DESCRIPTOR_POOL_USAGE_ONE_SHOT, 1,
Chia-I Wu8d24b3b2015-03-26 13:14:16 +08001189 &descriptor_pool, &demo->desc_pool);
Chia-I Wuf8385062015-01-04 16:27:24 +08001190 assert(!err);
1191}
1192
1193static void demo_prepare_descriptor_set(struct demo *demo)
1194{
Chia-I Wu8cd8ecd2015-05-25 16:27:55 +08001195 VkDescriptorInfo tex_descs[DEMO_TEXTURE_COUNT];
1196 VkWriteDescriptorSet write;
Tony Barbour22a30862015-04-22 09:02:32 -06001197 VkResult U_ASSERT_ONLY err;
Chia-I Wuf8385062015-01-04 16:27:24 +08001198 uint32_t count;
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -06001199 uint32_t i;
Chia-I Wuf8385062015-01-04 16:27:24 +08001200
Mike Stroyan230e6252015-04-17 12:36:38 -06001201 err = vkAllocDescriptorSets(demo->device, demo->desc_pool,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001202 VK_DESCRIPTOR_SET_USAGE_STATIC,
Chia-I Wuf8385062015-01-04 16:27:24 +08001203 1, &demo->desc_layout,
1204 &demo->desc_set, &count);
1205 assert(!err && count == 1);
1206
Chia-I Wu8cd8ecd2015-05-25 16:27:55 +08001207 memset(&tex_descs, 0, sizeof(tex_descs));
1208 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
1209 tex_descs[i].sampler = demo->textures[i].sampler;
1210 tex_descs[i].imageView = demo->textures[i].view;
1211 tex_descs[i].imageLayout = VK_IMAGE_LAYOUT_GENERAL;
1212 }
1213
1214 memset(&write, 0, sizeof(write));
1215 write.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
1216 write.destSet = demo->desc_set;
1217 write.count = DEMO_TEXTURE_COUNT;
1218 write.descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
1219 write.pDescriptors = tex_descs;
1220
1221 err = vkUpdateDescriptorSets(demo->device, 1, &write, 0, NULL);
1222 assert(!err);
Chia-I Wuf8385062015-01-04 16:27:24 +08001223}
1224
Chia-I Wu76cd4222015-07-08 13:34:24 +08001225static void demo_prepare_framebuffers(struct demo *demo)
1226{
1227 VkColorAttachmentBindInfo color_attachment = {
1228 .view = VK_NULL_HANDLE,
1229 .layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
1230 };
1231 const VkDepthStencilBindInfo depth_stencil = {
1232 .view = demo->depth.view,
1233 .layout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
1234 };
1235 const VkFramebufferCreateInfo fb_info = {
1236 .sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO,
1237 .pNext = NULL,
1238 .colorAttachmentCount = 1,
1239 .pColorAttachments = &color_attachment,
1240 .pDepthStencilAttachment = &depth_stencil,
1241 .sampleCount = 1,
1242 .width = demo->width,
1243 .height = demo->height,
1244 .layers = 1,
1245 };
1246 VkResult U_ASSERT_ONLY err;
1247 uint32_t i;
1248
1249 for (i = 0; i < DEMO_BUFFER_COUNT; i++) {
1250 color_attachment.view = demo->buffers[i].view;
1251 err = vkCreateFramebuffer(demo->device, &fb_info, &demo->framebuffers[i]);
1252 assert(!err);
1253 }
1254}
1255
Chia-I Wuc19795a2014-09-13 11:12:55 +08001256static void demo_prepare(struct demo *demo)
1257{
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001258 const VkCmdBufferCreateInfo cmd = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001259 .sType = VK_STRUCTURE_TYPE_CMD_BUFFER_CREATE_INFO,
Chia-I Wuc19795a2014-09-13 11:12:55 +08001260 .pNext = NULL,
Courtney Goeltzenleuchterf3168062015-03-05 18:09:39 -07001261 .queueNodeIndex = demo->graphics_queue_node_index,
Chia-I Wu88eaa3b2015-06-26 15:34:39 +08001262 .level = VK_CMD_BUFFER_LEVEL_PRIMARY,
Chia-I Wuc19795a2014-09-13 11:12:55 +08001263 .flags = 0,
1264 };
Tony Barbour22a30862015-04-22 09:02:32 -06001265 VkResult U_ASSERT_ONLY err;
Chia-I Wuc19795a2014-09-13 11:12:55 +08001266
Courtney Goeltzenleuchter633f9c52015-04-30 10:58:33 -06001267 err = vkCreateCommandBuffer(demo->device, &cmd, &demo->draw_cmd);
1268 assert(!err);
1269
Chia-I Wuc19795a2014-09-13 11:12:55 +08001270 demo_prepare_buffers(demo);
Chia-I Wu9ae87c92014-10-07 14:15:01 +08001271 demo_prepare_depth(demo);
Chia-I Wub043fe32014-10-06 15:30:33 +08001272 demo_prepare_textures(demo);
Chia-I Wu99621bc2014-10-08 11:52:22 +08001273 demo_prepare_vertices(demo);
Chia-I Wuf8385062015-01-04 16:27:24 +08001274 demo_prepare_descriptor_layout(demo);
Chia-I Wu76cd4222015-07-08 13:34:24 +08001275 demo_prepare_render_pass(demo);
Chia-I Wuc19795a2014-09-13 11:12:55 +08001276 demo_prepare_pipeline(demo);
1277 demo_prepare_dynamic_states(demo);
1278
Chia-I Wu8d24b3b2015-03-26 13:14:16 +08001279 demo_prepare_descriptor_pool(demo);
Chia-I Wuf8385062015-01-04 16:27:24 +08001280 demo_prepare_descriptor_set(demo);
Chia-I Wu76cd4222015-07-08 13:34:24 +08001281
1282 demo_prepare_framebuffers(demo);
1283
Courtney Goeltzenleuchter633f9c52015-04-30 10:58:33 -06001284 demo->prepared = true;
Chia-I Wuc19795a2014-09-13 11:12:55 +08001285}
1286
Ian Elliotte14e9f92015-04-16 15:23:05 -06001287#ifdef _WIN32
1288static void demo_run(struct demo *demo)
1289{
Courtney Goeltzenleuchter633f9c52015-04-30 10:58:33 -06001290 if (!demo->prepared)
1291 return;
Ian Elliotte14e9f92015-04-16 15:23:05 -06001292 demo_draw(demo);
1293}
1294
1295// On MS-Windows, make this a global, so it's available to WndProc()
1296struct demo demo;
1297
1298// MS-Windows event handling function:
1299LRESULT CALLBACK WndProc(HWND hWnd,
1300 UINT uMsg,
1301 WPARAM wParam,
1302 LPARAM lParam)
1303{
Ian Elliott4e19ed02015-04-28 10:52:52 -06001304 char tmp_str[] = APP_LONG_NAME;
Ian Elliotte14e9f92015-04-16 15:23:05 -06001305
1306 switch(uMsg)
1307 {
1308 case WM_CREATE:
1309 return 0;
1310 case WM_CLOSE:
1311 PostQuitMessage(0);
1312 return 0;
1313 case WM_PAINT:
1314 demo_run(&demo);
1315 return 0;
1316 default:
1317 break;
1318 }
1319 return (DefWindowProc(hWnd, uMsg, wParam, lParam));
1320}
1321
1322static void demo_create_window(struct demo *demo)
1323{
1324 WNDCLASSEX win_class;
1325
1326 // Initialize the window class structure:
1327 win_class.cbSize = sizeof(WNDCLASSEX);
1328 win_class.style = CS_HREDRAW | CS_VREDRAW;
1329 win_class.lpfnWndProc = WndProc;
1330 win_class.cbClsExtra = 0;
1331 win_class.cbWndExtra = 0;
1332 win_class.hInstance = demo->connection; // hInstance
1333 win_class.hIcon = LoadIcon(NULL, IDI_APPLICATION);
1334 win_class.hCursor = LoadCursor(NULL, IDC_ARROW);
1335 win_class.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
1336 win_class.lpszMenuName = NULL;
1337 win_class.lpszClassName = demo->name;
1338 win_class.hIconSm = LoadIcon(NULL, IDI_WINLOGO);
1339 // Register window class:
1340 if (!RegisterClassEx(&win_class)) {
1341 // It didn't work, so try to give a useful error:
1342 printf("Unexpected error trying to start the application!\n");
1343 fflush(stdout);
1344 exit(1);
1345 }
1346 // Create window with the registered class:
Mike Stroyan7eef5742015-06-15 14:19:19 -06001347 RECT wr = { 0, 0, demo->width, demo->height };
1348 AdjustWindowRect(&wr, WS_OVERLAPPEDWINDOW, FALSE);
Ian Elliotte14e9f92015-04-16 15:23:05 -06001349 demo->window = CreateWindowEx(0,
1350 demo->name, // class name
1351 demo->name, // app name
1352 WS_OVERLAPPEDWINDOW | // window style
1353 WS_VISIBLE |
1354 WS_SYSMENU,
1355 100,100, // x/y coords
Mike Stroyan7eef5742015-06-15 14:19:19 -06001356 wr.right-wr.left, // width
1357 wr.bottom-wr.top, // height
Ian Elliotte14e9f92015-04-16 15:23:05 -06001358 NULL, // handle to parent
1359 NULL, // handle to menu
1360 demo->connection, // hInstance
1361 NULL); // no extra parameters
1362 if (!demo->window) {
1363 // It didn't work, so try to give a useful error:
1364 printf("Cannot create a window in which to draw!\n");
1365 fflush(stdout);
1366 exit(1);
1367 }
1368}
1369#else // _WIN32
1370
Chia-I Wuc19795a2014-09-13 11:12:55 +08001371static void demo_handle_event(struct demo *demo,
1372 const xcb_generic_event_t *event)
1373{
1374 switch (event->response_type & 0x7f) {
1375 case XCB_EXPOSE:
1376 demo_draw(demo);
1377 break;
Courtney Goeltzenleuchterca698052014-11-07 15:17:03 -07001378 case XCB_CLIENT_MESSAGE:
1379 if((*(xcb_client_message_event_t*)event).data.data32[0] ==
1380 (*demo->atom_wm_delete_window).atom) {
1381 demo->quit = true;
1382 }
1383 break;
Chia-I Wuc19795a2014-09-13 11:12:55 +08001384 case XCB_KEY_RELEASE:
1385 {
1386 const xcb_key_release_event_t *key =
1387 (const xcb_key_release_event_t *) event;
1388
1389 if (key->detail == 0x9)
1390 demo->quit = true;
1391 }
1392 break;
Courtney Goeltzenleuchterca698052014-11-07 15:17:03 -07001393 case XCB_DESTROY_NOTIFY:
1394 demo->quit = true;
1395 break;
Chia-I Wuc19795a2014-09-13 11:12:55 +08001396 default:
1397 break;
1398 }
1399}
1400
1401static void demo_run(struct demo *demo)
1402{
1403 xcb_flush(demo->connection);
1404
1405 while (!demo->quit) {
1406 xcb_generic_event_t *event;
1407
1408 event = xcb_wait_for_event(demo->connection);
1409 if (event) {
1410 demo_handle_event(demo, event);
1411 free(event);
1412 }
1413 }
1414}
1415
1416static void demo_create_window(struct demo *demo)
1417{
1418 uint32_t value_mask, value_list[32];
1419
1420 demo->window = xcb_generate_id(demo->connection);
1421
1422 value_mask = XCB_CW_BACK_PIXEL | XCB_CW_EVENT_MASK;
1423 value_list[0] = demo->screen->black_pixel;
1424 value_list[1] = XCB_EVENT_MASK_KEY_RELEASE |
Courtney Goeltzenleuchterca698052014-11-07 15:17:03 -07001425 XCB_EVENT_MASK_EXPOSURE |
1426 XCB_EVENT_MASK_STRUCTURE_NOTIFY;
Chia-I Wuc19795a2014-09-13 11:12:55 +08001427
1428 xcb_create_window(demo->connection,
1429 XCB_COPY_FROM_PARENT,
1430 demo->window, demo->screen->root,
1431 0, 0, demo->width, demo->height, 0,
1432 XCB_WINDOW_CLASS_INPUT_OUTPUT,
1433 demo->screen->root_visual,
1434 value_mask, value_list);
1435
Courtney Goeltzenleuchterca698052014-11-07 15:17:03 -07001436 /* Magic code that will send notification when window is destroyed */
1437 xcb_intern_atom_cookie_t cookie = xcb_intern_atom(demo->connection, 1, 12,
1438 "WM_PROTOCOLS");
1439 xcb_intern_atom_reply_t* reply = xcb_intern_atom_reply(demo->connection, cookie, 0);
1440
1441 xcb_intern_atom_cookie_t cookie2 = xcb_intern_atom(demo->connection, 0, 16, "WM_DELETE_WINDOW");
1442 demo->atom_wm_delete_window = xcb_intern_atom_reply(demo->connection, cookie2, 0);
1443
1444 xcb_change_property(demo->connection, XCB_PROP_MODE_REPLACE,
1445 demo->window, (*reply).atom, 4, 32, 1,
1446 &(*demo->atom_wm_delete_window).atom);
1447 free(reply);
1448
Chia-I Wuc19795a2014-09-13 11:12:55 +08001449 xcb_map_window(demo->connection, demo->window);
1450}
Ian Elliotte14e9f92015-04-16 15:23:05 -06001451#endif // _WIN32
Chia-I Wuc19795a2014-09-13 11:12:55 +08001452
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001453static void demo_init_vk(struct demo *demo)
Chia-I Wuc19795a2014-09-13 11:12:55 +08001454{
Tobin Ehlis3536b442015-04-16 18:04:57 -06001455 VkResult err;
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06001456 char *extension_names[64];
1457 char *layer_names[64];
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -06001458 VkExtensionProperties *instance_extensions;
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06001459 VkLayerProperties *instance_layers;
1460 VkLayerProperties *device_layers;
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -06001461 uint32_t instance_extension_count = 0;
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06001462 uint32_t instance_layer_count = 0;
1463 uint32_t enabled_extension_count = 0;
1464 uint32_t enabled_layer_count = 0;
1465
1466 /* Look for validation layers */
Courtney Goeltzenleuchter1f41f542015-07-09 11:44:38 -06001467 VkBool32 validation_found = 0;
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06001468 err = vkGetGlobalLayerProperties(&instance_layer_count, NULL);
Tobin Ehlis3536b442015-04-16 18:04:57 -06001469 assert(!err);
1470
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06001471 memset(layer_names, 0, sizeof(layer_names));
1472 instance_layers = malloc(sizeof(VkLayerProperties) * instance_layer_count);
1473 err = vkGetGlobalLayerProperties(&instance_layer_count, instance_layers);
1474 assert(!err);
1475 for (uint32_t i = 0; i < instance_layer_count; i++) {
1476 if (!validation_found && demo->validate && !strcmp("Validation", instance_layers[i].layerName)) {
1477 layer_names[enabled_layer_count++] = "Validation";
1478 validation_found = 1;
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -06001479 }
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06001480 assert(enabled_layer_count < 64);
1481 }
1482 if (demo->validate && !validation_found) {
1483 ERR_EXIT("vkGetGlobalLayerProperties failed to find any "
1484 "\"Validation\" layers.\n\n"
1485 "Please look at the Getting Started guide for additional "
1486 "information.\n",
1487 "vkCreateInstance Failure");
1488 }
1489
1490 err = vkGetGlobalExtensionProperties(NULL, &instance_extension_count, NULL);
1491 assert(!err);
1492
Courtney Goeltzenleuchter1f41f542015-07-09 11:44:38 -06001493 VkBool32 WSIextFound = 0;
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06001494 memset(extension_names, 0, sizeof(extension_names));
1495 instance_extensions = malloc(sizeof(VkExtensionProperties) * instance_extension_count);
1496 err = vkGetGlobalExtensionProperties(NULL, &instance_extension_count, instance_extensions);
1497 assert(!err);
1498 for (uint32_t i = 0; i < instance_extension_count; i++) {
1499 if (!strcmp(VK_WSI_LUNARG_EXTENSION_NAME, instance_extensions[i].extName)) {
1500 WSIextFound = 1;
1501 extension_names[enabled_extension_count++] = VK_WSI_LUNARG_EXTENSION_NAME;
1502 }
1503 if (!strcmp(DEBUG_REPORT_EXTENSION_NAME, instance_extensions[i].extName)) {
1504 if (demo->validate) {
1505 extension_names[enabled_extension_count++] = DEBUG_REPORT_EXTENSION_NAME;
1506 }
1507 }
1508 assert(enabled_extension_count < 64);
Tobin Ehlis3536b442015-04-16 18:04:57 -06001509 }
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -06001510 if (!WSIextFound) {
Tony Barbour426b9052015-06-24 16:06:58 -06001511 ERR_EXIT("vkGetGlobalExtensionProperties failed to find the "
Ian Elliott3b375cf2015-04-28 13:22:33 -06001512 "\"VK_WSI_LunarG\" extension.\n\nDo you have a compatible "
1513 "Vulkan installable client driver (ICD) installed?\nPlease "
1514 "look at the Getting Started guide for additional "
1515 "information.\n",
1516 "vkCreateInstance Failure");
1517 }
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001518 const VkApplicationInfo app = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001519 .sType = VK_STRUCTURE_TYPE_APPLICATION_INFO,
Chia-I Wuc19795a2014-09-13 11:12:55 +08001520 .pNext = NULL,
Ian Elliott4e19ed02015-04-28 10:52:52 -06001521 .pAppName = APP_SHORT_NAME,
Chia-I Wuc19795a2014-09-13 11:12:55 +08001522 .appVersion = 0,
Ian Elliott4e19ed02015-04-28 10:52:52 -06001523 .pEngineName = APP_SHORT_NAME,
Chia-I Wuc19795a2014-09-13 11:12:55 +08001524 .engineVersion = 0,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001525 .apiVersion = VK_API_VERSION,
Chia-I Wuc19795a2014-09-13 11:12:55 +08001526 };
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06001527 VkInstanceCreateInfo inst_info = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001528 .sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO,
Jon Ashburn29669a42015-04-04 14:52:07 -06001529 .pNext = NULL,
1530 .pAppInfo = &app,
1531 .pAllocCb = NULL,
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06001532 .layerCount = enabled_layer_count,
1533 .ppEnabledLayerNames = (const char *const*) layer_names,
1534 .extensionCount = enabled_extension_count,
1535 .ppEnabledExtensionNames = (const char *const*) extension_names,
Jon Ashburn29669a42015-04-04 14:52:07 -06001536 };
Courtney Goeltzenleuchterddcb6192015-04-14 18:48:46 -06001537 const VkDeviceQueueCreateInfo queue = {
Chia-I Wuc19795a2014-09-13 11:12:55 +08001538 .queueNodeIndex = 0,
1539 .queueCount = 1,
1540 };
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06001541
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -06001542 uint32_t gpu_count;
1543 uint32_t i;
Courtney Goeltzenleuchterf3168062015-03-05 18:09:39 -07001544 uint32_t queue_count;
Chia-I Wuc19795a2014-09-13 11:12:55 +08001545
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001546 err = vkCreateInstance(&inst_info, &demo->inst);
Ian Elliottcaa9f272015-04-28 11:35:02 -06001547 if (err == VK_ERROR_INCOMPATIBLE_DRIVER) {
1548 ERR_EXIT("Cannot find a compatible Vulkan installable client driver "
Ian Elliott3b375cf2015-04-28 13:22:33 -06001549 "(ICD).\n\nPlease look at the Getting Started guide for "
Ian Elliottcaa9f272015-04-28 11:35:02 -06001550 "additional information.\n",
1551 "vkCreateInstance Failure");
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06001552 } else if (err == VK_ERROR_INVALID_EXTENSION) {
1553 ERR_EXIT("Cannot find a specified extension library"
1554 ".\nMake sure your layers path is set appropriately\n",
1555 "vkCreateInstance Failure");
Ian Elliottcaa9f272015-04-28 11:35:02 -06001556 } else if (err) {
Ian Elliott3b375cf2015-04-28 13:22:33 -06001557 ERR_EXIT("vkCreateInstance failed.\n\nDo you have a compatible Vulkan "
1558 "installable client driver (ICD) installed?\nPlease look at "
Ian Elliottcaa9f272015-04-28 11:35:02 -06001559 "the Getting Started guide for additional information.\n",
1560 "vkCreateInstance Failure");
Ian Elliottdfe55f72015-04-03 15:24:55 -06001561 }
Jon Ashburn29669a42015-04-04 14:52:07 -06001562
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06001563 free(instance_layers);
1564 free(instance_extensions);
1565
Jon Ashburn07b309a2015-04-15 11:31:12 -06001566 gpu_count = 1;
1567 err = vkEnumeratePhysicalDevices(demo->inst, &gpu_count, &demo->gpu);
Chia-I Wuc19795a2014-09-13 11:12:55 +08001568 assert(!err && gpu_count == 1);
1569
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06001570 /* Look for validation layers */
1571 validation_found = 0;
1572 enabled_layer_count = 0;
1573 uint32_t device_layer_count = 0;
1574 err = vkGetPhysicalDeviceLayerProperties(demo->gpu, &device_layer_count, NULL);
1575 assert(!err);
1576
1577 memset(layer_names, 0, sizeof(layer_names));
1578 device_layers = malloc(sizeof(VkLayerProperties) * device_layer_count);
1579 err = vkGetPhysicalDeviceLayerProperties(demo->gpu, &device_layer_count, device_layers);
1580 assert(!err);
1581 for (uint32_t i = 0; i < device_layer_count; i++) {
1582 if (!validation_found && demo->validate &&
1583 !strcmp("Validation", device_layers[i].layerName)) {
1584 layer_names[enabled_layer_count++] = "Validation";
1585 validation_found = 1;
1586 }
1587 assert(enabled_layer_count < 64);
1588 }
1589 if (demo->validate && !validation_found) {
1590 ERR_EXIT("vkGetGlobalLayerProperties failed to find any "
1591 "\"Validation\" layers.\n\n"
1592 "Please look at the Getting Started guide for additional "
1593 "information.\n",
1594 "vkCreateInstance Failure");
1595 }
1596
1597 /* Don't need any device extensions */
1598 /* TODO: WSI device extension will go here eventually */
1599
1600 VkDeviceCreateInfo device = {
1601 .sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO,
1602 .pNext = NULL,
1603 .queueRecordCount = 1,
1604 .pRequestedQueues = &queue,
1605 .layerCount = enabled_layer_count,
1606 .ppEnabledLayerNames = (const char*const*) layer_names,
1607 .extensionCount = 0,
1608 .ppEnabledExtensionNames = NULL,
1609 .flags = 0,
1610 };
1611
1612 if (demo->validate) {
1613 demo->dbgCreateMsgCallback = vkGetInstanceProcAddr((VkPhysicalDevice) NULL, "vkDbgCreateMsgCallback");
1614 if (!demo->dbgCreateMsgCallback) {
1615 ERR_EXIT("GetProcAddr: Unable to find vkDbgCreateMsgCallback\n",
1616 "vkGetProcAddr Failure");
1617 }
1618 err = demo->dbgCreateMsgCallback(
1619 demo->inst,
1620 VK_DBG_REPORT_ERROR_BIT | VK_DBG_REPORT_WARN_BIT,
1621 dbgFunc, NULL,
1622 &demo->msg_callback);
1623 switch (err) {
1624 case VK_SUCCESS:
1625 break;
1626 case VK_ERROR_INVALID_POINTER:
1627 ERR_EXIT("dbgCreateMsgCallback: Invalid pointer\n",
1628 "dbgCreateMsgCallback Failure");
1629 break;
1630 case VK_ERROR_OUT_OF_HOST_MEMORY:
1631 ERR_EXIT("dbgCreateMsgCallback: out of host memory\n",
1632 "dbgCreateMsgCallback Failure");
1633 break;
1634 default:
1635 ERR_EXIT("dbgCreateMsgCallback: unknown failure\n",
1636 "dbgCreateMsgCallback Failure");
1637 break;
1638 }
1639 }
1640
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001641 err = vkCreateDevice(demo->gpu, &device, &demo->device);
Chia-I Wuc19795a2014-09-13 11:12:55 +08001642 assert(!err);
1643
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06001644 free(device_layers);
1645
Ian Elliott1b6de092015-06-22 15:07:49 -06001646 GET_DEVICE_PROC_ADDR(demo->device, CreateSwapChainWSI);
1647 GET_DEVICE_PROC_ADDR(demo->device, CreateSwapChainWSI);
1648 GET_DEVICE_PROC_ADDR(demo->device, DestroySwapChainWSI);
1649 GET_DEVICE_PROC_ADDR(demo->device, GetSwapChainInfoWSI);
1650 GET_DEVICE_PROC_ADDR(demo->device, QueuePresentWSI);
1651
Tony Barbour426b9052015-06-24 16:06:58 -06001652 err = vkGetPhysicalDeviceProperties(demo->gpu, &demo->gpu_props);
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -06001653 assert(!err);
1654
Tony Barbour426b9052015-06-24 16:06:58 -06001655 err = vkGetPhysicalDeviceQueueCount(demo->gpu, &queue_count);
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -06001656 assert(!err);
1657
Tony Barbour426b9052015-06-24 16:06:58 -06001658 demo->queue_props = (VkPhysicalDeviceQueueProperties *) malloc(queue_count * sizeof(VkPhysicalDeviceQueueProperties));
1659 err = vkGetPhysicalDeviceQueueProperties(demo->gpu, queue_count, demo->queue_props);
Courtney Goeltzenleuchterf3168062015-03-05 18:09:39 -07001660 assert(!err);
Courtney Goeltzenleuchterf3168062015-03-05 18:09:39 -07001661 assert(queue_count >= 1);
1662
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06001663 // Graphics queue and MemMgr queue can be separate.
1664 // TODO: Add support for separate queues, including synchronization,
1665 // and appropriate tracking for QueueSubmit
Courtney Goeltzenleuchterf3168062015-03-05 18:09:39 -07001666 for (i = 0; i < queue_count; i++) {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001667 if (demo->queue_props[i].queueFlags & VK_QUEUE_GRAPHICS_BIT)
Courtney Goeltzenleuchterf3168062015-03-05 18:09:39 -07001668 break;
1669 }
1670 assert(i < queue_count);
1671 demo->graphics_queue_node_index = i;
1672
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001673 err = vkGetDeviceQueue(demo->device, demo->graphics_queue_node_index,
Chia-I Wuc19795a2014-09-13 11:12:55 +08001674 0, &demo->queue);
1675 assert(!err);
Ian Elliott32536f92015-04-21 16:41:02 -06001676
Jon Ashburnba4a1952015-06-16 12:44:51 -06001677 // for now hardcode format till get WSI support
1678 demo->format = VK_FORMAT_B8G8R8A8_UNORM;
Ian Elliott32536f92015-04-21 16:41:02 -06001679
Mark Lobodzinski72346292015-07-02 16:49:40 -06001680 // Get Memory information and properties
1681 err = vkGetPhysicalDeviceMemoryProperties(demo->gpu, &demo->memory_properties);
1682 assert(!err);
Chia-I Wuc19795a2014-09-13 11:12:55 +08001683}
1684
1685static void demo_init_connection(struct demo *demo)
1686{
Ian Elliotte14e9f92015-04-16 15:23:05 -06001687#ifndef _WIN32
Chia-I Wuc19795a2014-09-13 11:12:55 +08001688 const xcb_setup_t *setup;
1689 xcb_screen_iterator_t iter;
1690 int scr;
1691
1692 demo->connection = xcb_connect(NULL, &scr);
Ian Elliottdfe55f72015-04-03 15:24:55 -06001693 if (demo->connection == NULL) {
1694 printf("Cannot find a compatible Vulkan installable client driver "
1695 "(ICD).\nExiting ...\n");
1696 fflush(stdout);
1697 exit(1);
1698 }
Chia-I Wuc19795a2014-09-13 11:12:55 +08001699
1700 setup = xcb_get_setup(demo->connection);
1701 iter = xcb_setup_roots_iterator(setup);
1702 while (scr-- > 0)
1703 xcb_screen_next(&iter);
1704
1705 demo->screen = iter.data;
Ian Elliotte14e9f92015-04-16 15:23:05 -06001706#endif // _WIN32
Chia-I Wuc19795a2014-09-13 11:12:55 +08001707}
1708
Ian Elliotte14e9f92015-04-16 15:23:05 -06001709#ifdef _WIN32
1710static void demo_init(struct demo *demo, HINSTANCE hInstance, LPSTR pCmdLine)
1711#else // _WIN32
Courtney Goeltzenleuchterf113a952015-02-25 11:46:58 -07001712static void demo_init(struct demo *demo, const int argc, const char *argv[])
Ian Elliotte14e9f92015-04-16 15:23:05 -06001713#endif // _WIN32
Chia-I Wuc19795a2014-09-13 11:12:55 +08001714{
Ian Elliotte14e9f92015-04-16 15:23:05 -06001715 bool argv_error = false;
1716
Chia-I Wuc19795a2014-09-13 11:12:55 +08001717 memset(demo, 0, sizeof(*demo));
1718
Ian Elliotte14e9f92015-04-16 15:23:05 -06001719#ifdef _WIN32
1720 demo->connection = hInstance;
Ian Elliott4e19ed02015-04-28 10:52:52 -06001721 strncpy(demo->name, APP_SHORT_NAME, APP_NAME_STR_LEN);
Ian Elliotte14e9f92015-04-16 15:23:05 -06001722
1723 if (strncmp(pCmdLine, "--use_staging", strlen("--use_staging")) == 0)
1724 demo->use_staging_buffer = true;
Cody Northrop75db0322015-05-28 11:27:16 -06001725 else if (strncmp(pCmdLine, "--use_glsl", strlen("--use_glsl")) == 0)
1726 demo->use_glsl = true;
Ian Elliotte14e9f92015-04-16 15:23:05 -06001727 else if (strlen(pCmdLine) != 0) {
1728 fprintf(stderr, "Do not recognize argument \"%s\".\n", pCmdLine);
1729 argv_error = true;
1730 }
1731#else // _WIN32
Courtney Goeltzenleuchterf113a952015-02-25 11:46:58 -07001732 for (int i = 0; i < argc; i++) {
1733 if (strncmp(argv[i], "--use_staging", strlen("--use_staging")) == 0)
1734 demo->use_staging_buffer = true;
Cody Northrop75db0322015-05-28 11:27:16 -06001735 else if (strncmp(argv[i], "--use_glsl", strlen("--use_glsl")) == 0)
1736 demo->use_glsl = true;
Courtney Goeltzenleuchterf113a952015-02-25 11:46:58 -07001737 }
Ian Elliotte14e9f92015-04-16 15:23:05 -06001738#endif // _WIN32
1739 if (argv_error) {
Ian Elliott4e19ed02015-04-28 10:52:52 -06001740 fprintf(stderr, "Usage:\n %s [--use_staging]\n", APP_SHORT_NAME);
Ian Elliotte14e9f92015-04-16 15:23:05 -06001741 fflush(stderr);
1742 exit(1);
1743 }
Courtney Goeltzenleuchterf113a952015-02-25 11:46:58 -07001744
Chia-I Wuc19795a2014-09-13 11:12:55 +08001745 demo_init_connection(demo);
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001746 demo_init_vk(demo);
Chia-I Wuc19795a2014-09-13 11:12:55 +08001747
1748 demo->width = 300;
1749 demo->height = 300;
Chia-I Wuc19795a2014-09-13 11:12:55 +08001750}
1751
1752static void demo_cleanup(struct demo *demo)
1753{
Mark Lobodzinski23182612015-05-29 09:32:35 -05001754 uint32_t i;
Chia-I Wuc19795a2014-09-13 11:12:55 +08001755
Chia-I Wu76cd4222015-07-08 13:34:24 +08001756 for (i = 0; i < DEMO_BUFFER_COUNT; i++)
1757 vkDestroyObject(demo->device, VK_OBJECT_TYPE_FRAMEBUFFER, demo->framebuffers[i]);
1758
Mike Stroyan230e6252015-04-17 12:36:38 -06001759 vkDestroyObject(demo->device, VK_OBJECT_TYPE_DESCRIPTOR_SET, demo->desc_set);
1760 vkDestroyObject(demo->device, VK_OBJECT_TYPE_DESCRIPTOR_POOL, demo->desc_pool);
Chia-I Wuf8385062015-01-04 16:27:24 +08001761
Courtney Goeltzenleuchter633f9c52015-04-30 10:58:33 -06001762 if (demo->setup_cmd) {
1763 vkDestroyObject(demo->device, VK_OBJECT_TYPE_COMMAND_BUFFER, demo->setup_cmd);
1764 }
1765 vkDestroyObject(demo->device, VK_OBJECT_TYPE_COMMAND_BUFFER, demo->draw_cmd);
Chia-I Wuc19795a2014-09-13 11:12:55 +08001766
Mike Stroyan230e6252015-04-17 12:36:38 -06001767 vkDestroyObject(demo->device, VK_OBJECT_TYPE_DYNAMIC_VP_STATE, demo->viewport);
1768 vkDestroyObject(demo->device, VK_OBJECT_TYPE_DYNAMIC_RS_STATE, demo->raster);
1769 vkDestroyObject(demo->device, VK_OBJECT_TYPE_DYNAMIC_CB_STATE, demo->color_blend);
1770 vkDestroyObject(demo->device, VK_OBJECT_TYPE_DYNAMIC_DS_STATE, demo->depth_stencil);
Chia-I Wuc19795a2014-09-13 11:12:55 +08001771
Mike Stroyan230e6252015-04-17 12:36:38 -06001772 vkDestroyObject(demo->device, VK_OBJECT_TYPE_PIPELINE, demo->pipeline);
Chia-I Wu76cd4222015-07-08 13:34:24 +08001773 vkDestroyObject(demo->device, VK_OBJECT_TYPE_RENDER_PASS, demo->render_pass);
Mike Stroyan230e6252015-04-17 12:36:38 -06001774 vkDestroyObject(demo->device, VK_OBJECT_TYPE_PIPELINE_LAYOUT, demo->pipeline_layout);
1775 vkDestroyObject(demo->device, VK_OBJECT_TYPE_DESCRIPTOR_SET_LAYOUT, demo->desc_layout);
Chia-I Wub043fe32014-10-06 15:30:33 +08001776
Mike Stroyan230e6252015-04-17 12:36:38 -06001777 vkDestroyObject(demo->device, VK_OBJECT_TYPE_BUFFER, demo->vertices.buf);
Mark Lobodzinski23182612015-05-29 09:32:35 -05001778 vkFreeMemory(demo->device, demo->vertices.mem);
Chia-I Wu99621bc2014-10-08 11:52:22 +08001779
Chia-I Wub043fe32014-10-06 15:30:33 +08001780 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Mike Stroyan230e6252015-04-17 12:36:38 -06001781 vkDestroyObject(demo->device, VK_OBJECT_TYPE_IMAGE_VIEW, demo->textures[i].view);
Mike Stroyan230e6252015-04-17 12:36:38 -06001782 vkDestroyObject(demo->device, VK_OBJECT_TYPE_IMAGE, demo->textures[i].image);
Mark Lobodzinski23182612015-05-29 09:32:35 -05001783 vkFreeMemory(demo->device, demo->textures[i].mem);
Mike Stroyan230e6252015-04-17 12:36:38 -06001784 vkDestroyObject(demo->device, VK_OBJECT_TYPE_SAMPLER, demo->textures[i].sampler);
Chia-I Wub043fe32014-10-06 15:30:33 +08001785 }
1786
Mike Stroyan230e6252015-04-17 12:36:38 -06001787 vkDestroyObject(demo->device, VK_OBJECT_TYPE_DEPTH_STENCIL_VIEW, demo->depth.view);
Mike Stroyan230e6252015-04-17 12:36:38 -06001788 vkDestroyObject(demo->device, VK_OBJECT_TYPE_IMAGE, demo->depth.image);
Mark Lobodzinski23182612015-05-29 09:32:35 -05001789 vkFreeMemory(demo->device, demo->depth.mem);
Chia-I Wu9ae87c92014-10-07 14:15:01 +08001790
Chia-I Wuc19795a2014-09-13 11:12:55 +08001791 for (i = 0; i < DEMO_BUFFER_COUNT; i++) {
Mike Stroyan230e6252015-04-17 12:36:38 -06001792 vkDestroyObject(demo->device, VK_OBJECT_TYPE_COLOR_ATTACHMENT_VIEW, demo->buffers[i].view);
Chia-I Wuc19795a2014-09-13 11:12:55 +08001793 }
Jon Ashburncedc15f2015-05-21 18:13:33 -06001794 demo->fpDestroySwapChainWSI(demo->swap_chain);
Chia-I Wuc19795a2014-09-13 11:12:55 +08001795
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001796 vkDestroyDevice(demo->device);
1797 vkDestroyInstance(demo->inst);
Chia-I Wuc19795a2014-09-13 11:12:55 +08001798
Ian Elliotte14e9f92015-04-16 15:23:05 -06001799#ifndef _WIN32
Chia-I Wuc19795a2014-09-13 11:12:55 +08001800 xcb_destroy_window(demo->connection, demo->window);
1801 xcb_disconnect(demo->connection);
Ian Elliotte14e9f92015-04-16 15:23:05 -06001802#endif // _WIN32
Chia-I Wuc19795a2014-09-13 11:12:55 +08001803}
1804
Ian Elliotte14e9f92015-04-16 15:23:05 -06001805#ifdef _WIN32
1806int APIENTRY WinMain(HINSTANCE hInstance,
1807 HINSTANCE hPrevInstance,
1808 LPSTR pCmdLine,
1809 int nCmdShow)
1810{
1811 MSG msg; // message
1812 bool done; // flag saying when app is complete
1813
1814 demo_init(&demo, hInstance, pCmdLine);
1815 demo_create_window(&demo);
1816
1817 demo_prepare(&demo);
1818
1819 done = false; //initialize loop condition variable
1820 /* main message loop*/
1821 while(!done)
1822 {
Ian Elliott421107f2015-04-28 15:50:36 -06001823 PeekMessage(&msg, NULL, 0, 0, PM_REMOVE);
Ian Elliotte14e9f92015-04-16 15:23:05 -06001824 if (msg.message == WM_QUIT) //check for a quit message
1825 {
1826 done = true; //if found, quit app
1827 }
1828 else
1829 {
1830 /* Translate and dispatch to event queue*/
1831 TranslateMessage(&msg);
1832 DispatchMessage(&msg);
1833 }
1834 }
1835
1836 demo_cleanup(&demo);
1837
Tony Barboura938abb2015-04-22 11:36:22 -06001838 return (int) msg.wParam;
Ian Elliotte14e9f92015-04-16 15:23:05 -06001839}
1840#else // _WIN32
Courtney Goeltzenleuchterf113a952015-02-25 11:46:58 -07001841int main(const int argc, const char *argv[])
Chia-I Wuc19795a2014-09-13 11:12:55 +08001842{
1843 struct demo demo;
1844
Courtney Goeltzenleuchterf113a952015-02-25 11:46:58 -07001845 demo_init(&demo, argc, argv);
Chia-I Wu5b66aa52015-04-16 22:02:10 +08001846 demo_create_window(&demo);
Chia-I Wuc19795a2014-09-13 11:12:55 +08001847
1848 demo_prepare(&demo);
Chia-I Wuc19795a2014-09-13 11:12:55 +08001849 demo_run(&demo);
1850
1851 demo_cleanup(&demo);
1852
Chia-I Wuc19795a2014-09-13 11:12:55 +08001853 return 0;
1854}
Ian Elliotte14e9f92015-04-16 15:23:05 -06001855#endif // _WIN32