Chia-I Wu | f5caeb0 | 2014-10-25 12:11:27 +0800 | [diff] [blame] | 1 | /* |
Ian Elliott | 7595eee | 2015-04-28 10:33:11 -0600 | [diff] [blame] | 2 | * 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 Wu | f5caeb0 | 2014-10-25 12:11:27 +0800 | [diff] [blame] | 25 | * 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 Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 30 | #include <stdio.h> |
| 31 | #include <stdlib.h> |
| 32 | #include <string.h> |
| 33 | #include <stdbool.h> |
| 34 | #include <assert.h> |
| 35 | |
Ian Elliott | e14e9f9 | 2015-04-16 15:23:05 -0600 | [diff] [blame] | 36 | #ifdef _WIN32 |
| 37 | #pragma comment(linker, "/subsystem:windows") |
| 38 | #include <windows.h> |
| 39 | #define APP_NAME_STR_LEN 80 |
| 40 | #else // _WIN32 |
Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 41 | #include <xcb/xcb.h> |
Ian Elliott | e14e9f9 | 2015-04-16 15:23:05 -0600 | [diff] [blame] | 42 | #endif // _WIN32 |
| 43 | |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 44 | #include <vulkan.h> |
Chia-I Wu | 5b66aa5 | 2015-04-16 22:02:10 +0800 | [diff] [blame] | 45 | #include <vk_wsi_lunarg.h> |
Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 46 | |
Cody Northrop | d4e020a | 2015-03-17 14:54:35 -0600 | [diff] [blame] | 47 | #include "icd-spv.h" |
Courtney Goeltzenleuchter | 3f2606d | 2014-10-13 17:51:58 -0600 | [diff] [blame] | 48 | |
Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 49 | #define DEMO_BUFFER_COUNT 2 |
Chia-I Wu | b043fe3 | 2014-10-06 15:30:33 +0800 | [diff] [blame] | 50 | #define DEMO_TEXTURE_COUNT 1 |
Courtney Goeltzenleuchter | f5cdad0 | 2015-03-31 16:36:30 -0600 | [diff] [blame] | 51 | #define VERTEX_BUFFER_BIND_ID 0 |
Ian Elliott | 4e19ed0 | 2015-04-28 10:52:52 -0600 | [diff] [blame] | 52 | #define APP_SHORT_NAME "tri" |
| 53 | #define APP_LONG_NAME "The Vulkan Triangle Demo Program" |
Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 54 | |
Tony Barbour | 22a3086 | 2015-04-22 09:02:32 -0600 | [diff] [blame] | 55 | #if defined(NDEBUG) && defined(__GNUC__) |
| 56 | #define U_ASSERT_ONLY __attribute__((unused)) |
| 57 | #else |
| 58 | #define U_ASSERT_ONLY |
| 59 | #endif |
| 60 | |
Ian Elliott | caa9f27 | 2015-04-28 11:35:02 -0600 | [diff] [blame] | 61 | #ifdef _WIN32 |
| 62 | #define ERR_EXIT(err_msg, err_class) \ |
| 63 | do { \ |
| 64 | MessageBox(NULL, err_msg, err_class, MB_OK); \ |
| 65 | exit(1); \ |
| 66 | } while (0) |
| 67 | |
| 68 | // NOTE: If the following values (copied from "loader_platform.h") change, they |
| 69 | // need to change here as well: |
| 70 | #define LAYER_NAMES_ENV "VK_LAYER_NAMES" |
| 71 | #define LAYER_NAMES_REGISTRY_VALUE "VK_LAYER_NAMES" |
| 72 | #else // _WIN32 |
| 73 | |
| 74 | #define ERR_EXIT(err_msg, err_class) \ |
| 75 | do { \ |
| 76 | printf(err_msg); \ |
| 77 | fflush(stdout); \ |
| 78 | exit(1); \ |
| 79 | } while (0) |
| 80 | #endif // _WIN32 |
Tony Barbour | 22a3086 | 2015-04-22 09:02:32 -0600 | [diff] [blame] | 81 | |
Ian Elliott | 1b6de09 | 2015-06-22 15:07:49 -0600 | [diff] [blame] | 82 | #define GET_DEVICE_PROC_ADDR(dev, entrypoint) \ |
| 83 | { \ |
| 84 | demo->fp##entrypoint = vkGetDeviceProcAddr(dev, "vk"#entrypoint); \ |
| 85 | if (demo->fp##entrypoint == NULL) { \ |
| 86 | ERR_EXIT("vkGetDeviceProcAddr failed to find vk"#entrypoint, \ |
| 87 | "vkGetDeviceProcAddr Failure"); \ |
| 88 | } \ |
| 89 | } |
| 90 | |
Courtney Goeltzenleuchter | bfccf41 | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 91 | struct texture_object { |
Courtney Goeltzenleuchter | 382489d | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 92 | VkSampler sampler; |
Courtney Goeltzenleuchter | 372e13c | 2015-02-13 17:52:46 -0700 | [diff] [blame] | 93 | |
Courtney Goeltzenleuchter | 382489d | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 94 | VkImage image; |
| 95 | VkImageLayout imageLayout; |
Courtney Goeltzenleuchter | bfccf41 | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 96 | |
Mark Lobodzinski | 2318261 | 2015-05-29 09:32:35 -0500 | [diff] [blame] | 97 | VkDeviceMemory mem; |
Courtney Goeltzenleuchter | 382489d | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 98 | VkImageView view; |
Courtney Goeltzenleuchter | 372e13c | 2015-02-13 17:52:46 -0700 | [diff] [blame] | 99 | int32_t tex_width, tex_height; |
| 100 | }; |
| 101 | |
Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 102 | struct demo { |
Ian Elliott | e14e9f9 | 2015-04-16 15:23:05 -0600 | [diff] [blame] | 103 | #ifdef _WIN32 |
| 104 | #define APP_NAME_STR_LEN 80 |
| 105 | HINSTANCE connection; // hInstance - Windows Instance |
| 106 | char name[APP_NAME_STR_LEN]; // Name to put on the window/icon |
| 107 | HWND window; // hWnd - window handle |
| 108 | #else // _WIN32 |
Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 109 | xcb_connection_t *connection; |
| 110 | xcb_screen_t *screen; |
Chia-I Wu | 5b66aa5 | 2015-04-16 22:02:10 +0800 | [diff] [blame] | 111 | xcb_window_t window; |
| 112 | xcb_intern_atom_reply_t *atom_wm_delete_window; |
Ian Elliott | e14e9f9 | 2015-04-16 15:23:05 -0600 | [diff] [blame] | 113 | #endif // _WIN32 |
Jon Ashburn | 8a399e9 | 2015-04-24 09:46:24 -0700 | [diff] [blame] | 114 | bool prepared; |
Ian Elliott | e14e9f9 | 2015-04-16 15:23:05 -0600 | [diff] [blame] | 115 | bool use_staging_buffer; |
Cody Northrop | 75db032 | 2015-05-28 11:27:16 -0600 | [diff] [blame] | 116 | bool use_glsl; |
Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 117 | |
Courtney Goeltzenleuchter | 382489d | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 118 | VkInstance inst; |
Tony Barbour | 8205d90 | 2015-04-16 15:59:00 -0600 | [diff] [blame] | 119 | VkPhysicalDevice gpu; |
Courtney Goeltzenleuchter | 382489d | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 120 | VkDevice device; |
| 121 | VkQueue queue; |
Tony Barbour | 426b905 | 2015-06-24 16:06:58 -0600 | [diff] [blame] | 122 | VkPhysicalDeviceProperties gpu_props; |
Tony Barbour | 8205d90 | 2015-04-16 15:59:00 -0600 | [diff] [blame] | 123 | VkPhysicalDeviceQueueProperties *queue_props; |
Courtney Goeltzenleuchter | f316806 | 2015-03-05 18:09:39 -0700 | [diff] [blame] | 124 | uint32_t graphics_queue_node_index; |
Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 125 | |
| 126 | int width, height; |
Courtney Goeltzenleuchter | 382489d | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 127 | VkFormat format; |
Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 128 | |
Jon Ashburn | cedc15f | 2015-05-21 18:13:33 -0600 | [diff] [blame] | 129 | PFN_vkCreateSwapChainWSI fpCreateSwapChainWSI; |
| 130 | PFN_vkDestroySwapChainWSI fpDestroySwapChainWSI; |
| 131 | PFN_vkGetSwapChainInfoWSI fpGetSwapChainInfoWSI; |
| 132 | PFN_vkQueuePresentWSI fpQueuePresentWSI; |
| 133 | |
Chia-I Wu | 5b66aa5 | 2015-04-16 22:02:10 +0800 | [diff] [blame] | 134 | VkSwapChainWSI swap_chain; |
Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 135 | struct { |
Courtney Goeltzenleuchter | 382489d | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 136 | VkImage image; |
Tony Barbour | 8205d90 | 2015-04-16 15:59:00 -0600 | [diff] [blame] | 137 | VkDeviceMemory mem; |
Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 138 | |
Courtney Goeltzenleuchter | 382489d | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 139 | VkColorAttachmentView view; |
Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 140 | } buffers[DEMO_BUFFER_COUNT]; |
| 141 | |
Chia-I Wu | b043fe3 | 2014-10-06 15:30:33 +0800 | [diff] [blame] | 142 | struct { |
Courtney Goeltzenleuchter | 382489d | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 143 | VkFormat format; |
Chia-I Wu | 9ae87c9 | 2014-10-07 14:15:01 +0800 | [diff] [blame] | 144 | |
Courtney Goeltzenleuchter | 382489d | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 145 | VkImage image; |
Mark Lobodzinski | 2318261 | 2015-05-29 09:32:35 -0500 | [diff] [blame] | 146 | VkDeviceMemory mem; |
Courtney Goeltzenleuchter | 382489d | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 147 | VkDepthStencilView view; |
Chia-I Wu | 9ae87c9 | 2014-10-07 14:15:01 +0800 | [diff] [blame] | 148 | } depth; |
| 149 | |
Courtney Goeltzenleuchter | bfccf41 | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 150 | struct texture_object textures[DEMO_TEXTURE_COUNT]; |
Chia-I Wu | b043fe3 | 2014-10-06 15:30:33 +0800 | [diff] [blame] | 151 | |
Chia-I Wu | 99621bc | 2014-10-08 11:52:22 +0800 | [diff] [blame] | 152 | struct { |
Courtney Goeltzenleuchter | 382489d | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 153 | VkBuffer buf; |
Mark Lobodzinski | 2318261 | 2015-05-29 09:32:35 -0500 | [diff] [blame] | 154 | VkDeviceMemory mem; |
Chia-I Wu | 8d29d02 | 2014-10-08 12:14:39 +0800 | [diff] [blame] | 155 | |
Mark Lobodzinski | 0e0fb5c | 2015-06-23 15:11:57 -0600 | [diff] [blame] | 156 | VkPipelineVertexInputStateCreateInfo vi; |
Courtney Goeltzenleuchter | 382489d | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 157 | VkVertexInputBindingDescription vi_bindings[1]; |
| 158 | VkVertexInputAttributeDescription vi_attrs[2]; |
Chia-I Wu | 99621bc | 2014-10-08 11:52:22 +0800 | [diff] [blame] | 159 | } vertices; |
| 160 | |
Courtney Goeltzenleuchter | 633f9c5 | 2015-04-30 10:58:33 -0600 | [diff] [blame] | 161 | VkCmdBuffer setup_cmd; // Command Buffer for initialization commands |
| 162 | VkCmdBuffer draw_cmd; // Command Buffer for drawing commands |
Mark Lobodzinski | 556f721 | 2015-04-17 14:11:39 -0500 | [diff] [blame] | 163 | VkPipelineLayout pipeline_layout; |
Courtney Goeltzenleuchter | 382489d | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 164 | VkDescriptorSetLayout desc_layout; |
| 165 | VkPipeline pipeline; |
Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 166 | |
Courtney Goeltzenleuchter | fcf855f | 2015-04-10 16:24:50 -0600 | [diff] [blame] | 167 | VkDynamicVpState viewport; |
| 168 | VkDynamicRsState raster; |
| 169 | VkDynamicCbState color_blend; |
| 170 | VkDynamicDsState depth_stencil; |
Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 171 | |
Courtney Goeltzenleuchter | 382489d | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 172 | VkDescriptorPool desc_pool; |
| 173 | VkDescriptorSet desc_set; |
Chia-I Wu | f838506 | 2015-01-04 16:27:24 +0800 | [diff] [blame] | 174 | |
Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 175 | bool quit; |
Mark Lobodzinski | e2d07a5 | 2015-01-29 08:55:56 -0600 | [diff] [blame] | 176 | uint32_t current_buffer; |
Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 177 | }; |
| 178 | |
Courtney Goeltzenleuchter | bfccf41 | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 179 | static void demo_flush_init_cmd(struct demo *demo) |
| 180 | { |
Tony Barbour | 22a3086 | 2015-04-22 09:02:32 -0600 | [diff] [blame] | 181 | VkResult U_ASSERT_ONLY err; |
Courtney Goeltzenleuchter | bfccf41 | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 182 | |
Courtney Goeltzenleuchter | 633f9c5 | 2015-04-30 10:58:33 -0600 | [diff] [blame] | 183 | if (demo->setup_cmd == VK_NULL_HANDLE) |
Courtney Goeltzenleuchter | bfccf41 | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 184 | return; |
| 185 | |
Courtney Goeltzenleuchter | 633f9c5 | 2015-04-30 10:58:33 -0600 | [diff] [blame] | 186 | err = vkEndCommandBuffer(demo->setup_cmd); |
Courtney Goeltzenleuchter | bfccf41 | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 187 | assert(!err); |
| 188 | |
Courtney Goeltzenleuchter | 633f9c5 | 2015-04-30 10:58:33 -0600 | [diff] [blame] | 189 | const VkCmdBuffer cmd_bufs[] = { demo->setup_cmd }; |
Courtney Goeltzenleuchter | bfccf41 | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 190 | |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 191 | err = vkQueueSubmit(demo->queue, 1, cmd_bufs, VK_NULL_HANDLE); |
Courtney Goeltzenleuchter | bfccf41 | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 192 | assert(!err); |
| 193 | |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 194 | err = vkQueueWaitIdle(demo->queue); |
Courtney Goeltzenleuchter | bfccf41 | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 195 | assert(!err); |
| 196 | |
Courtney Goeltzenleuchter | 633f9c5 | 2015-04-30 10:58:33 -0600 | [diff] [blame] | 197 | vkDestroyObject(demo->device, VK_OBJECT_TYPE_COMMAND_BUFFER, demo->setup_cmd); |
| 198 | demo->setup_cmd = VK_NULL_HANDLE; |
Courtney Goeltzenleuchter | bfccf41 | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 199 | } |
| 200 | |
Courtney Goeltzenleuchter | bfccf41 | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 201 | static void demo_set_image_layout( |
| 202 | struct demo *demo, |
Courtney Goeltzenleuchter | 382489d | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 203 | VkImage image, |
malnasse | 4b8ba4d | 2015-06-03 17:28:38 -0400 | [diff] [blame] | 204 | VkImageAspect aspect, |
Courtney Goeltzenleuchter | 382489d | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 205 | VkImageLayout old_image_layout, |
| 206 | VkImageLayout new_image_layout) |
Courtney Goeltzenleuchter | bfccf41 | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 207 | { |
Tony Barbour | 22a3086 | 2015-04-22 09:02:32 -0600 | [diff] [blame] | 208 | VkResult U_ASSERT_ONLY err; |
Courtney Goeltzenleuchter | bfccf41 | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 209 | |
Courtney Goeltzenleuchter | 633f9c5 | 2015-04-30 10:58:33 -0600 | [diff] [blame] | 210 | if (demo->setup_cmd == VK_NULL_HANDLE) { |
Courtney Goeltzenleuchter | 382489d | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 211 | const VkCmdBufferCreateInfo cmd = { |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 212 | .sType = VK_STRUCTURE_TYPE_CMD_BUFFER_CREATE_INFO, |
Courtney Goeltzenleuchter | bfccf41 | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 213 | .pNext = NULL, |
| 214 | .queueNodeIndex = demo->graphics_queue_node_index, |
| 215 | .flags = 0, |
| 216 | }; |
| 217 | |
Courtney Goeltzenleuchter | 633f9c5 | 2015-04-30 10:58:33 -0600 | [diff] [blame] | 218 | err = vkCreateCommandBuffer(demo->device, &cmd, &demo->setup_cmd); |
Courtney Goeltzenleuchter | bfccf41 | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 219 | assert(!err); |
| 220 | |
Courtney Goeltzenleuchter | 382489d | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 221 | VkCmdBufferBeginInfo cmd_buf_info = { |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 222 | .sType = VK_STRUCTURE_TYPE_CMD_BUFFER_BEGIN_INFO, |
Courtney Goeltzenleuchter | bfccf41 | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 223 | .pNext = NULL, |
Tony Barbour | 8205d90 | 2015-04-16 15:59:00 -0600 | [diff] [blame] | 224 | .flags = VK_CMD_BUFFER_OPTIMIZE_SMALL_BATCH_BIT | |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 225 | VK_CMD_BUFFER_OPTIMIZE_ONE_TIME_SUBMIT_BIT, |
Courtney Goeltzenleuchter | bfccf41 | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 226 | }; |
Courtney Goeltzenleuchter | 633f9c5 | 2015-04-30 10:58:33 -0600 | [diff] [blame] | 227 | err = vkBeginCommandBuffer(demo->setup_cmd, &cmd_buf_info); |
Courtney Goeltzenleuchter | bfccf41 | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 228 | } |
| 229 | |
Courtney Goeltzenleuchter | 382489d | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 230 | VkImageMemoryBarrier image_memory_barrier = { |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 231 | .sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER, |
Courtney Goeltzenleuchter | bfccf41 | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 232 | .pNext = NULL, |
| 233 | .outputMask = 0, |
| 234 | .inputMask = 0, |
| 235 | .oldLayout = old_image_layout, |
| 236 | .newLayout = new_image_layout, |
| 237 | .image = image, |
malnasse | 4b8ba4d | 2015-06-03 17:28:38 -0400 | [diff] [blame] | 238 | .subresourceRange = { aspect, 0, 1, 0, 0 } |
Courtney Goeltzenleuchter | bfccf41 | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 239 | }; |
| 240 | |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 241 | if (new_image_layout == VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL) { |
Courtney Goeltzenleuchter | bfccf41 | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 242 | /* Make sure anything that was copying from this image has completed */ |
Courtney Goeltzenleuchter | ad87081 | 2015-04-15 15:29:59 -0600 | [diff] [blame] | 243 | image_memory_barrier.inputMask = VK_MEMORY_INPUT_TRANSFER_BIT; |
Courtney Goeltzenleuchter | bfccf41 | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 244 | } |
| 245 | |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 246 | if (new_image_layout == VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL) { |
Courtney Goeltzenleuchter | bfccf41 | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 247 | /* Make sure any Copy or CPU writes to image are flushed */ |
Courtney Goeltzenleuchter | a569a50 | 2015-04-29 17:16:21 -0600 | [diff] [blame] | 248 | image_memory_barrier.outputMask = VK_MEMORY_OUTPUT_TRANSFER_BIT | VK_MEMORY_OUTPUT_HOST_WRITE_BIT; |
Courtney Goeltzenleuchter | bfccf41 | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 249 | } |
| 250 | |
Courtney Goeltzenleuchter | 382489d | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 251 | VkImageMemoryBarrier *pmemory_barrier = &image_memory_barrier; |
Courtney Goeltzenleuchter | bfccf41 | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 252 | |
Courtney Goeltzenleuchter | 382489d | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 253 | VkPipeEvent set_events[] = { VK_PIPE_EVENT_TOP_OF_PIPE }; |
Courtney Goeltzenleuchter | bfccf41 | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 254 | |
Mike Stroyan | d678e56 | 2015-06-08 13:33:44 -0600 | [diff] [blame] | 255 | vkCmdPipelineBarrier(demo->setup_cmd, VK_WAIT_EVENT_TOP_OF_PIPE, 1, set_events, 1, (const void **)&pmemory_barrier); |
Courtney Goeltzenleuchter | bfccf41 | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 256 | } |
| 257 | |
Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 258 | static void demo_draw_build_cmd(struct demo *demo) |
| 259 | { |
Courtney Goeltzenleuchter | 382489d | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 260 | const VkColorAttachmentBindInfo color_attachment = { |
Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 261 | .view = demo->buffers[demo->current_buffer].view, |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 262 | .layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL, |
Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 263 | }; |
Courtney Goeltzenleuchter | 382489d | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 264 | const VkDepthStencilBindInfo depth_stencil = { |
Chia-I Wu | 9ae87c9 | 2014-10-07 14:15:01 +0800 | [diff] [blame] | 265 | .view = demo->depth.view, |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 266 | .layout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL, |
Chia-I Wu | 9ae87c9 | 2014-10-07 14:15:01 +0800 | [diff] [blame] | 267 | }; |
Courtney Goeltzenleuchter | 382489d | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 268 | const VkClearColor clear_color = { |
Courtney Goeltzenleuchter | 9a1ded8 | 2015-04-03 16:35:32 -0600 | [diff] [blame] | 269 | .color.floatColor = { 0.2f, 0.2f, 0.2f, 0.2f }, |
| 270 | .useRawValue = false, |
| 271 | }; |
Mark Lobodzinski | e2d07a5 | 2015-01-29 08:55:56 -0600 | [diff] [blame] | 272 | const float clear_depth = 0.9f; |
Courtney Goeltzenleuchter | 382489d | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 273 | VkImageSubresourceRange clear_range; |
| 274 | VkCmdBufferBeginInfo cmd_buf_info = { |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 275 | .sType = VK_STRUCTURE_TYPE_CMD_BUFFER_BEGIN_INFO, |
Courtney Goeltzenleuchter | e3b0f3a | 2015-04-03 15:25:24 -0600 | [diff] [blame] | 276 | .pNext = NULL, |
Tony Barbour | 8205d90 | 2015-04-16 15:59:00 -0600 | [diff] [blame] | 277 | .flags = VK_CMD_BUFFER_OPTIMIZE_SMALL_BATCH_BIT | |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 278 | VK_CMD_BUFFER_OPTIMIZE_ONE_TIME_SUBMIT_BIT, |
Jon Ashburn | 53d27af | 2014-12-31 17:08:35 -0700 | [diff] [blame] | 279 | }; |
Tony Barbour | 22a3086 | 2015-04-22 09:02:32 -0600 | [diff] [blame] | 280 | VkResult U_ASSERT_ONLY err; |
Courtney Goeltzenleuchter | 382489d | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 281 | VkAttachmentLoadOp load_op = VK_ATTACHMENT_LOAD_OP_DONT_CARE; |
| 282 | VkAttachmentStoreOp store_op = VK_ATTACHMENT_STORE_OP_DONT_CARE; |
| 283 | const VkFramebufferCreateInfo fb_info = { |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 284 | .sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO, |
Jon Ashburn | 3325d6b | 2015-01-02 18:24:05 -0700 | [diff] [blame] | 285 | .pNext = NULL, |
| 286 | .colorAttachmentCount = 1, |
Courtney Goeltzenleuchter | 382489d | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 287 | .pColorAttachments = (VkColorAttachmentBindInfo*) &color_attachment, |
| 288 | .pDepthStencilAttachment = (VkDepthStencilBindInfo*) &depth_stencil, |
Jon Ashburn | 3325d6b | 2015-01-02 18:24:05 -0700 | [diff] [blame] | 289 | .sampleCount = 1, |
Mark Lobodzinski | 71fcc2d | 2015-01-27 13:24:03 -0600 | [diff] [blame] | 290 | .width = demo->width, |
| 291 | .height = demo->height, |
| 292 | .layers = 1, |
Jon Ashburn | 3325d6b | 2015-01-02 18:24:05 -0700 | [diff] [blame] | 293 | }; |
Courtney Goeltzenleuchter | 382489d | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 294 | VkRenderPassCreateInfo rp_info; |
| 295 | VkRenderPassBegin rp_begin; |
Jon Ashburn | 3325d6b | 2015-01-02 18:24:05 -0700 | [diff] [blame] | 296 | |
| 297 | memset(&rp_info, 0 , sizeof(rp_info)); |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 298 | err = vkCreateFramebuffer(demo->device, &fb_info, &rp_begin.framebuffer); |
Jon Ashburn | 3325d6b | 2015-01-02 18:24:05 -0700 | [diff] [blame] | 299 | assert(!err); |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 300 | rp_info.sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO; |
Jon Ashburn | 3325d6b | 2015-01-02 18:24:05 -0700 | [diff] [blame] | 301 | rp_info.renderArea.extent.width = demo->width; |
| 302 | rp_info.renderArea.extent.height = demo->height; |
Piers Daniell | 886be47 | 2015-02-23 16:23:13 -0700 | [diff] [blame] | 303 | rp_info.colorAttachmentCount = fb_info.colorAttachmentCount; |
Courtney Goeltzenleuchter | e3b0f3a | 2015-04-03 15:25:24 -0600 | [diff] [blame] | 304 | rp_info.pColorFormats = &demo->format; |
| 305 | rp_info.pColorLayouts = &color_attachment.layout; |
Jon Ashburn | 3325d6b | 2015-01-02 18:24:05 -0700 | [diff] [blame] | 306 | rp_info.pColorLoadOps = &load_op; |
| 307 | rp_info.pColorStoreOps = &store_op; |
Courtney Goeltzenleuchter | e3b0f3a | 2015-04-03 15:25:24 -0600 | [diff] [blame] | 308 | rp_info.pColorLoadClearValues = &clear_color; |
Tony Barbour | 8205d90 | 2015-04-16 15:59:00 -0600 | [diff] [blame] | 309 | rp_info.depthStencilFormat = VK_FORMAT_D16_UNORM; |
Courtney Goeltzenleuchter | e3b0f3a | 2015-04-03 15:25:24 -0600 | [diff] [blame] | 310 | rp_info.depthStencilLayout = depth_stencil.layout; |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 311 | rp_info.depthLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE; |
Courtney Goeltzenleuchter | e3b0f3a | 2015-04-03 15:25:24 -0600 | [diff] [blame] | 312 | rp_info.depthLoadClearValue = clear_depth; |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 313 | rp_info.depthStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE; |
| 314 | rp_info.stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE; |
Courtney Goeltzenleuchter | e3b0f3a | 2015-04-03 15:25:24 -0600 | [diff] [blame] | 315 | rp_info.stencilLoadClearValue = 0; |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 316 | rp_info.stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE; |
| 317 | err = vkCreateRenderPass(demo->device, &rp_info, &(rp_begin.renderPass)); |
Jon Ashburn | 3325d6b | 2015-01-02 18:24:05 -0700 | [diff] [blame] | 318 | assert(!err); |
Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 319 | |
Courtney Goeltzenleuchter | 633f9c5 | 2015-04-30 10:58:33 -0600 | [diff] [blame] | 320 | err = vkBeginCommandBuffer(demo->draw_cmd, &cmd_buf_info); |
Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 321 | assert(!err); |
| 322 | |
Tobin Ehlis | e407678 | 2015-06-24 15:53:07 -0600 | [diff] [blame] | 323 | vkCmdBeginRenderPass(demo->draw_cmd, &rp_begin); |
Courtney Goeltzenleuchter | 633f9c5 | 2015-04-30 10:58:33 -0600 | [diff] [blame] | 324 | vkCmdBindPipeline(demo->draw_cmd, VK_PIPELINE_BIND_POINT_GRAPHICS, |
Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 325 | demo->pipeline); |
Mark Lobodzinski | a65c463 | 2015-06-15 13:21:21 -0600 | [diff] [blame] | 326 | vkCmdBindDescriptorSets(demo->draw_cmd, VK_PIPELINE_BIND_POINT_GRAPHICS, demo->pipeline_layout, |
Cody Northrop | 1a01b1d | 2015-04-16 13:41:56 -0600 | [diff] [blame] | 327 | 0, 1, & demo->desc_set, 0, NULL); |
Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 328 | |
Courtney Goeltzenleuchter | 633f9c5 | 2015-04-30 10:58:33 -0600 | [diff] [blame] | 329 | vkCmdBindDynamicStateObject(demo->draw_cmd, VK_STATE_BIND_POINT_VIEWPORT, demo->viewport); |
| 330 | vkCmdBindDynamicStateObject(demo->draw_cmd, VK_STATE_BIND_POINT_RASTER, demo->raster); |
| 331 | vkCmdBindDynamicStateObject(demo->draw_cmd, VK_STATE_BIND_POINT_COLOR_BLEND, |
Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 332 | demo->color_blend); |
Courtney Goeltzenleuchter | 633f9c5 | 2015-04-30 10:58:33 -0600 | [diff] [blame] | 333 | vkCmdBindDynamicStateObject(demo->draw_cmd, VK_STATE_BIND_POINT_DEPTH_STENCIL, |
Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 334 | demo->depth_stencil); |
| 335 | |
Tony Barbour | 8205d90 | 2015-04-16 15:59:00 -0600 | [diff] [blame] | 336 | VkDeviceSize offsets[1] = {0}; |
Courtney Goeltzenleuchter | 633f9c5 | 2015-04-30 10:58:33 -0600 | [diff] [blame] | 337 | vkCmdBindVertexBuffers(demo->draw_cmd, VERTEX_BUFFER_BIND_ID, 1, &demo->vertices.buf, offsets); |
Chia-I Wu | 3b04af5 | 2014-11-08 10:48:20 +0800 | [diff] [blame] | 338 | |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 339 | clear_range.aspect = VK_IMAGE_ASPECT_COLOR; |
Chia-I Wu | 68a7de4 | 2014-10-25 12:40:28 +0800 | [diff] [blame] | 340 | clear_range.baseMipLevel = 0; |
| 341 | clear_range.mipLevels = 1; |
| 342 | clear_range.baseArraySlice = 0; |
| 343 | clear_range.arraySize = 1; |
Courtney Goeltzenleuchter | 633f9c5 | 2015-04-30 10:58:33 -0600 | [diff] [blame] | 344 | vkCmdClearColorImage(demo->draw_cmd, |
Chia-I Wu | 68a7de4 | 2014-10-25 12:40:28 +0800 | [diff] [blame] | 345 | demo->buffers[demo->current_buffer].image, |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 346 | VK_IMAGE_LAYOUT_CLEAR_OPTIMAL, |
Courtney Goeltzenleuchter | da4a99e | 2015-04-23 17:49:22 -0600 | [diff] [blame] | 347 | &clear_color, 1, &clear_range); |
Chia-I Wu | 68a7de4 | 2014-10-25 12:40:28 +0800 | [diff] [blame] | 348 | |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 349 | clear_range.aspect = VK_IMAGE_ASPECT_DEPTH; |
Courtney Goeltzenleuchter | 633f9c5 | 2015-04-30 10:58:33 -0600 | [diff] [blame] | 350 | vkCmdClearDepthStencil(demo->draw_cmd, |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 351 | demo->depth.image, VK_IMAGE_LAYOUT_CLEAR_OPTIMAL, |
Chia-I Wu | 68a7de4 | 2014-10-25 12:40:28 +0800 | [diff] [blame] | 352 | clear_depth, 0, 1, &clear_range); |
| 353 | |
Courtney Goeltzenleuchter | 633f9c5 | 2015-04-30 10:58:33 -0600 | [diff] [blame] | 354 | vkCmdDraw(demo->draw_cmd, 0, 3, 0, 1); |
| 355 | vkCmdEndRenderPass(demo->draw_cmd, rp_begin.renderPass); |
Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 356 | |
Courtney Goeltzenleuchter | 633f9c5 | 2015-04-30 10:58:33 -0600 | [diff] [blame] | 357 | err = vkEndCommandBuffer(demo->draw_cmd); |
Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 358 | assert(!err); |
Courtney Goeltzenleuchter | e9afa99 | 2015-02-25 16:55:23 -0700 | [diff] [blame] | 359 | |
Mike Stroyan | 230e625 | 2015-04-17 12:36:38 -0600 | [diff] [blame] | 360 | vkDestroyObject(demo->device, VK_OBJECT_TYPE_RENDER_PASS, rp_begin.renderPass); |
| 361 | vkDestroyObject(demo->device, VK_OBJECT_TYPE_FRAMEBUFFER, rp_begin.framebuffer); |
Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 362 | } |
| 363 | |
| 364 | static void demo_draw(struct demo *demo) |
| 365 | { |
Chia-I Wu | 5b66aa5 | 2015-04-16 22:02:10 +0800 | [diff] [blame] | 366 | const VkPresentInfoWSI present = { |
| 367 | .sType = VK_STRUCTURE_TYPE_PRESENT_INFO_WSI, |
| 368 | .pNext = NULL, |
| 369 | .image = demo->buffers[demo->current_buffer].image, |
| 370 | .flipInterval = 0, |
Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 371 | }; |
Tony Barbour | 22a3086 | 2015-04-22 09:02:32 -0600 | [diff] [blame] | 372 | VkResult U_ASSERT_ONLY err; |
Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 373 | |
| 374 | demo_draw_build_cmd(demo); |
| 375 | |
Courtney Goeltzenleuchter | 633f9c5 | 2015-04-30 10:58:33 -0600 | [diff] [blame] | 376 | err = vkQueueSubmit(demo->queue, 1, &demo->draw_cmd, VK_NULL_HANDLE); |
Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 377 | assert(!err); |
| 378 | |
Jon Ashburn | cedc15f | 2015-05-21 18:13:33 -0600 | [diff] [blame] | 379 | err = demo->fpQueuePresentWSI(demo->queue, &present); |
Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 380 | assert(!err); |
| 381 | |
| 382 | demo->current_buffer = (demo->current_buffer + 1) % DEMO_BUFFER_COUNT; |
Chia-I Wu | 5b66aa5 | 2015-04-16 22:02:10 +0800 | [diff] [blame] | 383 | |
| 384 | err = vkQueueWaitIdle(demo->queue); |
| 385 | assert(err == VK_SUCCESS); |
Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 386 | } |
| 387 | |
| 388 | static void demo_prepare_buffers(struct demo *demo) |
| 389 | { |
Chia-I Wu | 5b66aa5 | 2015-04-16 22:02:10 +0800 | [diff] [blame] | 390 | const VkSwapChainCreateInfoWSI swap_chain = { |
| 391 | .sType = VK_STRUCTURE_TYPE_SWAP_CHAIN_CREATE_INFO_WSI, |
| 392 | .pNext = NULL, |
| 393 | .pNativeWindowSystemHandle = demo->connection, |
| 394 | .pNativeWindowHandle = (void *) (intptr_t) demo->window, |
Ian Elliott | 32536f9 | 2015-04-21 16:41:02 -0600 | [diff] [blame] | 395 | .displayCount = 1, |
Chia-I Wu | 5b66aa5 | 2015-04-16 22:02:10 +0800 | [diff] [blame] | 396 | .imageCount = DEMO_BUFFER_COUNT, |
| 397 | .imageFormat = demo->format, |
| 398 | .imageExtent = { |
Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 399 | .width = demo->width, |
| 400 | .height = demo->height, |
| 401 | }, |
Chia-I Wu | 5b66aa5 | 2015-04-16 22:02:10 +0800 | [diff] [blame] | 402 | .imageArraySize = 1, |
| 403 | .imageUsageFlags = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT, |
Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 404 | }; |
Chia-I Wu | 5b66aa5 | 2015-04-16 22:02:10 +0800 | [diff] [blame] | 405 | VkSwapChainImageInfoWSI images[DEMO_BUFFER_COUNT]; |
| 406 | size_t images_size = sizeof(images); |
Tony Barbour | 22a3086 | 2015-04-22 09:02:32 -0600 | [diff] [blame] | 407 | VkResult U_ASSERT_ONLY err; |
Mark Lobodzinski | e2d07a5 | 2015-01-29 08:55:56 -0600 | [diff] [blame] | 408 | uint32_t i; |
Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 409 | |
Jon Ashburn | cedc15f | 2015-05-21 18:13:33 -0600 | [diff] [blame] | 410 | err = demo->fpCreateSwapChainWSI(demo->device, &swap_chain, &demo->swap_chain); |
Chia-I Wu | 5b66aa5 | 2015-04-16 22:02:10 +0800 | [diff] [blame] | 411 | assert(!err); |
| 412 | |
Jon Ashburn | cedc15f | 2015-05-21 18:13:33 -0600 | [diff] [blame] | 413 | err = demo->fpGetSwapChainInfoWSI(demo->swap_chain, |
Chia-I Wu | 5b66aa5 | 2015-04-16 22:02:10 +0800 | [diff] [blame] | 414 | VK_SWAP_CHAIN_INFO_TYPE_PERSISTENT_IMAGES_WSI, |
| 415 | &images_size, images); |
| 416 | assert(!err && images_size == sizeof(images)); |
| 417 | |
Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 418 | for (i = 0; i < DEMO_BUFFER_COUNT; i++) { |
Courtney Goeltzenleuchter | 382489d | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 419 | VkColorAttachmentViewCreateInfo color_attachment_view = { |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 420 | .sType = VK_STRUCTURE_TYPE_COLOR_ATTACHMENT_VIEW_CREATE_INFO, |
Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 421 | .pNext = NULL, |
| 422 | .format = demo->format, |
| 423 | .mipLevel = 0, |
| 424 | .baseArraySlice = 0, |
| 425 | .arraySize = 1, |
| 426 | }; |
| 427 | |
Chia-I Wu | 5b66aa5 | 2015-04-16 22:02:10 +0800 | [diff] [blame] | 428 | demo->buffers[i].image = images[i].image; |
| 429 | demo->buffers[i].mem = images[i].memory; |
Mark Lobodzinski | 97dcd04 | 2015-04-16 08:52:00 -0500 | [diff] [blame] | 430 | |
Courtney Goeltzenleuchter | bfccf41 | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 431 | demo_set_image_layout(demo, demo->buffers[i].image, |
malnasse | 4b8ba4d | 2015-06-03 17:28:38 -0400 | [diff] [blame] | 432 | VK_IMAGE_ASPECT_COLOR, |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 433 | VK_IMAGE_LAYOUT_UNDEFINED, |
| 434 | VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL); |
Courtney Goeltzenleuchter | bfccf41 | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 435 | |
Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 436 | color_attachment_view.image = demo->buffers[i].image; |
| 437 | |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 438 | err = vkCreateColorAttachmentView(demo->device, |
Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 439 | &color_attachment_view, &demo->buffers[i].view); |
| 440 | assert(!err); |
| 441 | } |
Piers Daniell | 886be47 | 2015-02-23 16:23:13 -0700 | [diff] [blame] | 442 | |
| 443 | demo->current_buffer = 0; |
Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 444 | } |
| 445 | |
Chia-I Wu | 9ae87c9 | 2014-10-07 14:15:01 +0800 | [diff] [blame] | 446 | static void demo_prepare_depth(struct demo *demo) |
| 447 | { |
Tony Barbour | 8205d90 | 2015-04-16 15:59:00 -0600 | [diff] [blame] | 448 | const VkFormat depth_format = VK_FORMAT_D16_UNORM; |
Courtney Goeltzenleuchter | 382489d | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 449 | const VkImageCreateInfo image = { |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 450 | .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO, |
Chia-I Wu | 9ae87c9 | 2014-10-07 14:15:01 +0800 | [diff] [blame] | 451 | .pNext = NULL, |
Tony Barbour | 8205d90 | 2015-04-16 15:59:00 -0600 | [diff] [blame] | 452 | .imageType = VK_IMAGE_TYPE_2D, |
Chia-I Wu | 9ae87c9 | 2014-10-07 14:15:01 +0800 | [diff] [blame] | 453 | .format = depth_format, |
| 454 | .extent = { demo->width, demo->height, 1 }, |
| 455 | .mipLevels = 1, |
| 456 | .arraySize = 1, |
| 457 | .samples = 1, |
Tony Barbour | 8205d90 | 2015-04-16 15:59:00 -0600 | [diff] [blame] | 458 | .tiling = VK_IMAGE_TILING_OPTIMAL, |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 459 | .usage = VK_IMAGE_USAGE_DEPTH_STENCIL_BIT, |
Chia-I Wu | 9ae87c9 | 2014-10-07 14:15:01 +0800 | [diff] [blame] | 460 | .flags = 0, |
| 461 | }; |
Courtney Goeltzenleuchter | ddcb619 | 2015-04-14 18:48:46 -0600 | [diff] [blame] | 462 | VkMemoryAllocInfo mem_alloc = { |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 463 | .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOC_INFO, |
Mark Lobodzinski | 97dcd04 | 2015-04-16 08:52:00 -0500 | [diff] [blame] | 464 | .pNext = NULL, |
Chia-I Wu | 9ae87c9 | 2014-10-07 14:15:01 +0800 | [diff] [blame] | 465 | .allocationSize = 0, |
Tony Barbour | 8205d90 | 2015-04-16 15:59:00 -0600 | [diff] [blame] | 466 | .memProps = VK_MEMORY_PROPERTY_DEVICE_ONLY, |
Chia-I Wu | 9ae87c9 | 2014-10-07 14:15:01 +0800 | [diff] [blame] | 467 | }; |
Courtney Goeltzenleuchter | 382489d | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 468 | VkDepthStencilViewCreateInfo view = { |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 469 | .sType = VK_STRUCTURE_TYPE_DEPTH_STENCIL_VIEW_CREATE_INFO, |
Chia-I Wu | 9ae87c9 | 2014-10-07 14:15:01 +0800 | [diff] [blame] | 470 | .pNext = NULL, |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 471 | .image = VK_NULL_HANDLE, |
Chia-I Wu | 9ae87c9 | 2014-10-07 14:15:01 +0800 | [diff] [blame] | 472 | .mipLevel = 0, |
| 473 | .baseArraySlice = 0, |
| 474 | .arraySize = 1, |
| 475 | .flags = 0, |
| 476 | }; |
Jon Ashburn | a9ae383 | 2015-01-16 09:37:43 -0700 | [diff] [blame] | 477 | |
Mark Lobodzinski | 2318261 | 2015-05-29 09:32:35 -0500 | [diff] [blame] | 478 | VkMemoryRequirements mem_reqs; |
Tony Barbour | 22a3086 | 2015-04-22 09:02:32 -0600 | [diff] [blame] | 479 | VkResult U_ASSERT_ONLY err; |
Chia-I Wu | 9ae87c9 | 2014-10-07 14:15:01 +0800 | [diff] [blame] | 480 | |
| 481 | demo->depth.format = depth_format; |
| 482 | |
| 483 | /* create image */ |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 484 | err = vkCreateImage(demo->device, &image, |
Chia-I Wu | 9ae87c9 | 2014-10-07 14:15:01 +0800 | [diff] [blame] | 485 | &demo->depth.image); |
| 486 | assert(!err); |
| 487 | |
Tony Barbour | 426b905 | 2015-06-24 16:06:58 -0600 | [diff] [blame] | 488 | err = vkGetObjectMemoryRequirements(demo->device, |
| 489 | VK_OBJECT_TYPE_IMAGE, demo->depth.image, &mem_reqs); |
Mark Lobodzinski | 2318261 | 2015-05-29 09:32:35 -0500 | [diff] [blame] | 490 | mem_alloc.allocationSize = mem_reqs.size; |
Chia-I Wu | 9ae87c9 | 2014-10-07 14:15:01 +0800 | [diff] [blame] | 491 | |
Mark Lobodzinski | 2318261 | 2015-05-29 09:32:35 -0500 | [diff] [blame] | 492 | /* allocate memory */ |
| 493 | err = vkAllocMemory(demo->device, &mem_alloc, &demo->depth.mem); |
| 494 | assert(!err); |
Chia-I Wu | 9ae87c9 | 2014-10-07 14:15:01 +0800 | [diff] [blame] | 495 | |
Mark Lobodzinski | 2318261 | 2015-05-29 09:32:35 -0500 | [diff] [blame] | 496 | /* bind memory */ |
| 497 | err = vkBindObjectMemory(demo->device, |
| 498 | VK_OBJECT_TYPE_IMAGE, demo->depth.image, |
| 499 | demo->depth.mem, 0); |
| 500 | assert(!err); |
Chia-I Wu | 9ae87c9 | 2014-10-07 14:15:01 +0800 | [diff] [blame] | 501 | |
Courtney Goeltzenleuchter | bfccf41 | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 502 | demo_set_image_layout(demo, demo->depth.image, |
malnasse | 4b8ba4d | 2015-06-03 17:28:38 -0400 | [diff] [blame] | 503 | VK_IMAGE_ASPECT_DEPTH, |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 504 | VK_IMAGE_LAYOUT_UNDEFINED, |
| 505 | VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL); |
Courtney Goeltzenleuchter | bfccf41 | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 506 | |
Chia-I Wu | 9ae87c9 | 2014-10-07 14:15:01 +0800 | [diff] [blame] | 507 | /* create image view */ |
| 508 | view.image = demo->depth.image; |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 509 | err = vkCreateDepthStencilView(demo->device, &view, |
Chia-I Wu | 9ae87c9 | 2014-10-07 14:15:01 +0800 | [diff] [blame] | 510 | &demo->depth.view); |
| 511 | assert(!err); |
Chia-I Wu | 9ae87c9 | 2014-10-07 14:15:01 +0800 | [diff] [blame] | 512 | } |
| 513 | |
Courtney Goeltzenleuchter | 372e13c | 2015-02-13 17:52:46 -0700 | [diff] [blame] | 514 | static void demo_prepare_texture_image(struct demo *demo, |
| 515 | const uint32_t *tex_colors, |
Courtney Goeltzenleuchter | bfccf41 | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 516 | struct texture_object *tex_obj, |
Courtney Goeltzenleuchter | 382489d | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 517 | VkImageTiling tiling, |
Courtney Goeltzenleuchter | cb67a32 | 2015-04-21 09:31:23 -0600 | [diff] [blame] | 518 | VkImageUsageFlags usage, |
Courtney Goeltzenleuchter | 382489d | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 519 | VkFlags mem_props) |
Chia-I Wu | b043fe3 | 2014-10-06 15:30:33 +0800 | [diff] [blame] | 520 | { |
Tony Barbour | 8205d90 | 2015-04-16 15:59:00 -0600 | [diff] [blame] | 521 | const VkFormat tex_format = VK_FORMAT_B8G8R8A8_UNORM; |
Mark Lobodzinski | e2d07a5 | 2015-01-29 08:55:56 -0600 | [diff] [blame] | 522 | const int32_t tex_width = 2; |
| 523 | const int32_t tex_height = 2; |
Tony Barbour | 22a3086 | 2015-04-22 09:02:32 -0600 | [diff] [blame] | 524 | VkResult U_ASSERT_ONLY err; |
Chia-I Wu | b043fe3 | 2014-10-06 15:30:33 +0800 | [diff] [blame] | 525 | |
Courtney Goeltzenleuchter | bfccf41 | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 526 | tex_obj->tex_width = tex_width; |
| 527 | tex_obj->tex_height = tex_height; |
Courtney Goeltzenleuchter | 372e13c | 2015-02-13 17:52:46 -0700 | [diff] [blame] | 528 | |
Courtney Goeltzenleuchter | 382489d | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 529 | const VkImageCreateInfo image_create_info = { |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 530 | .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO, |
Courtney Goeltzenleuchter | 3226a6f | 2015-02-11 18:17:22 -0700 | [diff] [blame] | 531 | .pNext = NULL, |
Tony Barbour | 8205d90 | 2015-04-16 15:59:00 -0600 | [diff] [blame] | 532 | .imageType = VK_IMAGE_TYPE_2D, |
Courtney Goeltzenleuchter | 372e13c | 2015-02-13 17:52:46 -0700 | [diff] [blame] | 533 | .format = tex_format, |
| 534 | .extent = { tex_width, tex_height, 1 }, |
| 535 | .mipLevels = 1, |
| 536 | .arraySize = 1, |
| 537 | .samples = 1, |
| 538 | .tiling = tiling, |
Courtney Goeltzenleuchter | cb67a32 | 2015-04-21 09:31:23 -0600 | [diff] [blame] | 539 | .usage = usage, |
Courtney Goeltzenleuchter | 372e13c | 2015-02-13 17:52:46 -0700 | [diff] [blame] | 540 | .flags = 0, |
| 541 | }; |
Courtney Goeltzenleuchter | ddcb619 | 2015-04-14 18:48:46 -0600 | [diff] [blame] | 542 | VkMemoryAllocInfo mem_alloc = { |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 543 | .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOC_INFO, |
Mark Lobodzinski | 97dcd04 | 2015-04-16 08:52:00 -0500 | [diff] [blame] | 544 | .pNext = NULL, |
Courtney Goeltzenleuchter | 372e13c | 2015-02-13 17:52:46 -0700 | [diff] [blame] | 545 | .allocationSize = 0, |
| 546 | .memProps = mem_props, |
Courtney Goeltzenleuchter | 3226a6f | 2015-02-11 18:17:22 -0700 | [diff] [blame] | 547 | }; |
| 548 | |
Mark Lobodzinski | 2318261 | 2015-05-29 09:32:35 -0500 | [diff] [blame] | 549 | VkMemoryRequirements mem_reqs; |
Courtney Goeltzenleuchter | 372e13c | 2015-02-13 17:52:46 -0700 | [diff] [blame] | 550 | |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 551 | err = vkCreateImage(demo->device, &image_create_info, |
Courtney Goeltzenleuchter | bfccf41 | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 552 | &tex_obj->image); |
Courtney Goeltzenleuchter | 3226a6f | 2015-02-11 18:17:22 -0700 | [diff] [blame] | 553 | assert(!err); |
| 554 | |
Tony Barbour | 426b905 | 2015-06-24 16:06:58 -0600 | [diff] [blame] | 555 | err = vkGetObjectMemoryRequirements(demo->device, |
| 556 | VK_OBJECT_TYPE_IMAGE, tex_obj->image, &mem_reqs); |
Mark Lobodzinski | 2318261 | 2015-05-29 09:32:35 -0500 | [diff] [blame] | 557 | mem_alloc.allocationSize = mem_reqs.size; |
Courtney Goeltzenleuchter | 3226a6f | 2015-02-11 18:17:22 -0700 | [diff] [blame] | 558 | |
Mark Lobodzinski | 2318261 | 2015-05-29 09:32:35 -0500 | [diff] [blame] | 559 | /* allocate memory */ |
| 560 | err = vkAllocMemory(demo->device, &mem_alloc, &tex_obj->mem); |
| 561 | assert(!err); |
Courtney Goeltzenleuchter | 3226a6f | 2015-02-11 18:17:22 -0700 | [diff] [blame] | 562 | |
Mark Lobodzinski | 2318261 | 2015-05-29 09:32:35 -0500 | [diff] [blame] | 563 | /* bind memory */ |
| 564 | err = vkBindObjectMemory(demo->device, |
| 565 | VK_OBJECT_TYPE_IMAGE, tex_obj->image, |
| 566 | tex_obj->mem, 0); |
| 567 | assert(!err); |
Courtney Goeltzenleuchter | 3226a6f | 2015-02-11 18:17:22 -0700 | [diff] [blame] | 568 | |
Tony Barbour | 8205d90 | 2015-04-16 15:59:00 -0600 | [diff] [blame] | 569 | if (mem_props & VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT) { |
Courtney Goeltzenleuchter | 382489d | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 570 | const VkImageSubresource subres = { |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 571 | .aspect = VK_IMAGE_ASPECT_COLOR, |
Courtney Goeltzenleuchter | 3226a6f | 2015-02-11 18:17:22 -0700 | [diff] [blame] | 572 | .mipLevel = 0, |
| 573 | .arraySlice = 0, |
| 574 | }; |
Courtney Goeltzenleuchter | 382489d | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 575 | VkSubresourceLayout layout; |
Courtney Goeltzenleuchter | 3226a6f | 2015-02-11 18:17:22 -0700 | [diff] [blame] | 576 | void *data; |
| 577 | int32_t x, y; |
| 578 | |
Tony Barbour | 426b905 | 2015-06-24 16:06:58 -0600 | [diff] [blame] | 579 | err = vkGetImageSubresourceLayout(demo->device, tex_obj->image, &subres, &layout); |
| 580 | assert(!err); |
Courtney Goeltzenleuchter | 3226a6f | 2015-02-11 18:17:22 -0700 | [diff] [blame] | 581 | |
Mark Lobodzinski | 2318261 | 2015-05-29 09:32:35 -0500 | [diff] [blame] | 582 | err = vkMapMemory(demo->device, tex_obj->mem, 0, 0, 0, &data); |
Courtney Goeltzenleuchter | 3226a6f | 2015-02-11 18:17:22 -0700 | [diff] [blame] | 583 | assert(!err); |
| 584 | |
| 585 | for (y = 0; y < tex_height; y++) { |
| 586 | uint32_t *row = (uint32_t *) ((char *) data + layout.rowPitch * y); |
| 587 | for (x = 0; x < tex_width; x++) |
Courtney Goeltzenleuchter | 372e13c | 2015-02-13 17:52:46 -0700 | [diff] [blame] | 588 | row[x] = tex_colors[(x & 1) ^ (y & 1)]; |
Courtney Goeltzenleuchter | 3226a6f | 2015-02-11 18:17:22 -0700 | [diff] [blame] | 589 | } |
| 590 | |
Mark Lobodzinski | 2318261 | 2015-05-29 09:32:35 -0500 | [diff] [blame] | 591 | err = vkUnmapMemory(demo->device, tex_obj->mem); |
Courtney Goeltzenleuchter | 3226a6f | 2015-02-11 18:17:22 -0700 | [diff] [blame] | 592 | assert(!err); |
Courtney Goeltzenleuchter | 372e13c | 2015-02-13 17:52:46 -0700 | [diff] [blame] | 593 | } |
Courtney Goeltzenleuchter | bfccf41 | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 594 | |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 595 | tex_obj->imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL; |
Courtney Goeltzenleuchter | bfccf41 | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 596 | demo_set_image_layout(demo, tex_obj->image, |
malnasse | 4b8ba4d | 2015-06-03 17:28:38 -0400 | [diff] [blame] | 597 | VK_IMAGE_ASPECT_COLOR, |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 598 | VK_IMAGE_LAYOUT_UNDEFINED, |
Courtney Goeltzenleuchter | bfccf41 | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 599 | tex_obj->imageLayout); |
| 600 | /* setting the image layout does not reference the actual memory so no need to add a mem ref */ |
Courtney Goeltzenleuchter | 372e13c | 2015-02-13 17:52:46 -0700 | [diff] [blame] | 601 | } |
| 602 | |
Mark Lobodzinski | cf26e07 | 2015-04-16 11:44:05 -0500 | [diff] [blame] | 603 | static void demo_destroy_texture_image(struct demo *demo, struct texture_object *tex_obj) |
Courtney Goeltzenleuchter | 372e13c | 2015-02-13 17:52:46 -0700 | [diff] [blame] | 604 | { |
| 605 | /* clean up staging resources */ |
Mike Stroyan | 230e625 | 2015-04-17 12:36:38 -0600 | [diff] [blame] | 606 | vkDestroyObject(demo->device, VK_OBJECT_TYPE_IMAGE, tex_obj->image); |
Courtney Goeltzenleuchter | a063d9b | 2015-06-10 16:16:22 -0600 | [diff] [blame] | 607 | vkFreeMemory(demo->device, tex_obj->mem); |
Courtney Goeltzenleuchter | 372e13c | 2015-02-13 17:52:46 -0700 | [diff] [blame] | 608 | } |
| 609 | |
| 610 | static void demo_prepare_textures(struct demo *demo) |
| 611 | { |
Tony Barbour | 8205d90 | 2015-04-16 15:59:00 -0600 | [diff] [blame] | 612 | const VkFormat tex_format = VK_FORMAT_B8G8R8A8_UNORM; |
Courtney Goeltzenleuchter | 382489d | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 613 | VkFormatProperties props; |
Courtney Goeltzenleuchter | 372e13c | 2015-02-13 17:52:46 -0700 | [diff] [blame] | 614 | const uint32_t tex_colors[DEMO_TEXTURE_COUNT][2] = { |
| 615 | { 0xffff0000, 0xff00ff00 }, |
| 616 | }; |
Tony Barbour | 22a3086 | 2015-04-22 09:02:32 -0600 | [diff] [blame] | 617 | VkResult U_ASSERT_ONLY err; |
Courtney Goeltzenleuchter | 372e13c | 2015-02-13 17:52:46 -0700 | [diff] [blame] | 618 | uint32_t i; |
| 619 | |
Chris Forbes | d757630 | 2015-06-21 22:55:02 +1200 | [diff] [blame] | 620 | err = vkGetPhysicalDeviceFormatInfo(demo->gpu, tex_format, &props); |
Courtney Goeltzenleuchter | 372e13c | 2015-02-13 17:52:46 -0700 | [diff] [blame] | 621 | assert(!err); |
| 622 | |
| 623 | for (i = 0; i < DEMO_TEXTURE_COUNT; i++) { |
Tony Barbour | 8205d90 | 2015-04-16 15:59:00 -0600 | [diff] [blame] | 624 | if ((props.linearTilingFeatures & VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT) && !demo->use_staging_buffer) { |
Courtney Goeltzenleuchter | 372e13c | 2015-02-13 17:52:46 -0700 | [diff] [blame] | 625 | /* Device can texture using linear textures */ |
| 626 | demo_prepare_texture_image(demo, tex_colors[i], &demo->textures[i], |
Courtney Goeltzenleuchter | cb67a32 | 2015-04-21 09:31:23 -0600 | [diff] [blame] | 627 | VK_IMAGE_TILING_LINEAR, VK_IMAGE_USAGE_SAMPLED_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT); |
Tony Barbour | 8205d90 | 2015-04-16 15:59:00 -0600 | [diff] [blame] | 628 | } else if (props.optimalTilingFeatures & VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT){ |
Courtney Goeltzenleuchter | 372e13c | 2015-02-13 17:52:46 -0700 | [diff] [blame] | 629 | /* Must use staging buffer to copy linear texture to optimized */ |
Courtney Goeltzenleuchter | bfccf41 | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 630 | struct texture_object staging_texture; |
Courtney Goeltzenleuchter | 372e13c | 2015-02-13 17:52:46 -0700 | [diff] [blame] | 631 | |
| 632 | memset(&staging_texture, 0, sizeof(staging_texture)); |
| 633 | demo_prepare_texture_image(demo, tex_colors[i], &staging_texture, |
Courtney Goeltzenleuchter | cb67a32 | 2015-04-21 09:31:23 -0600 | [diff] [blame] | 634 | VK_IMAGE_TILING_LINEAR, VK_IMAGE_USAGE_TRANSFER_SOURCE_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT); |
Courtney Goeltzenleuchter | 372e13c | 2015-02-13 17:52:46 -0700 | [diff] [blame] | 635 | |
| 636 | demo_prepare_texture_image(demo, tex_colors[i], &demo->textures[i], |
Courtney Goeltzenleuchter | cb67a32 | 2015-04-21 09:31:23 -0600 | [diff] [blame] | 637 | VK_IMAGE_TILING_OPTIMAL, |
| 638 | (VK_IMAGE_USAGE_TRANSFER_DESTINATION_BIT | VK_IMAGE_USAGE_SAMPLED_BIT), |
| 639 | VK_MEMORY_PROPERTY_DEVICE_ONLY); |
Courtney Goeltzenleuchter | 372e13c | 2015-02-13 17:52:46 -0700 | [diff] [blame] | 640 | |
Courtney Goeltzenleuchter | bfccf41 | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 641 | demo_set_image_layout(demo, staging_texture.image, |
malnasse | 4b8ba4d | 2015-06-03 17:28:38 -0400 | [diff] [blame] | 642 | VK_IMAGE_ASPECT_COLOR, |
Courtney Goeltzenleuchter | bfccf41 | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 643 | staging_texture.imageLayout, |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 644 | VK_IMAGE_LAYOUT_TRANSFER_SOURCE_OPTIMAL); |
Courtney Goeltzenleuchter | 372e13c | 2015-02-13 17:52:46 -0700 | [diff] [blame] | 645 | |
Courtney Goeltzenleuchter | bfccf41 | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 646 | demo_set_image_layout(demo, demo->textures[i].image, |
malnasse | 4b8ba4d | 2015-06-03 17:28:38 -0400 | [diff] [blame] | 647 | VK_IMAGE_ASPECT_COLOR, |
Courtney Goeltzenleuchter | bfccf41 | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 648 | demo->textures[i].imageLayout, |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 649 | VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL); |
Courtney Goeltzenleuchter | 372e13c | 2015-02-13 17:52:46 -0700 | [diff] [blame] | 650 | |
Courtney Goeltzenleuchter | 382489d | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 651 | VkImageCopy copy_region = { |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 652 | .srcSubresource = { VK_IMAGE_ASPECT_COLOR, 0, 0 }, |
Courtney Goeltzenleuchter | 372e13c | 2015-02-13 17:52:46 -0700 | [diff] [blame] | 653 | .srcOffset = { 0, 0, 0 }, |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 654 | .destSubresource = { VK_IMAGE_ASPECT_COLOR, 0, 0 }, |
Courtney Goeltzenleuchter | 372e13c | 2015-02-13 17:52:46 -0700 | [diff] [blame] | 655 | .destOffset = { 0, 0, 0 }, |
| 656 | .extent = { staging_texture.tex_width, staging_texture.tex_height, 1 }, |
| 657 | }; |
Courtney Goeltzenleuchter | 633f9c5 | 2015-04-30 10:58:33 -0600 | [diff] [blame] | 658 | vkCmdCopyImage(demo->setup_cmd, |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 659 | staging_texture.image, VK_IMAGE_LAYOUT_TRANSFER_SOURCE_OPTIMAL, |
| 660 | demo->textures[i].image, VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL, |
Courtney Goeltzenleuchter | 51cbf30 | 2015-03-25 11:25:10 -0600 | [diff] [blame] | 661 | 1, ©_region); |
Courtney Goeltzenleuchter | 372e13c | 2015-02-13 17:52:46 -0700 | [diff] [blame] | 662 | |
Courtney Goeltzenleuchter | bfccf41 | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 663 | demo_set_image_layout(demo, demo->textures[i].image, |
malnasse | 4b8ba4d | 2015-06-03 17:28:38 -0400 | [diff] [blame] | 664 | VK_IMAGE_ASPECT_COLOR, |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 665 | VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL, |
Courtney Goeltzenleuchter | bfccf41 | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 666 | demo->textures[i].imageLayout); |
Courtney Goeltzenleuchter | 372e13c | 2015-02-13 17:52:46 -0700 | [diff] [blame] | 667 | |
Courtney Goeltzenleuchter | bfccf41 | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 668 | demo_flush_init_cmd(demo); |
Courtney Goeltzenleuchter | 372e13c | 2015-02-13 17:52:46 -0700 | [diff] [blame] | 669 | |
Courtney Goeltzenleuchter | 876629f | 2015-04-21 09:30:03 -0600 | [diff] [blame] | 670 | demo_destroy_texture_image(demo, &staging_texture); |
Courtney Goeltzenleuchter | 372e13c | 2015-02-13 17:52:46 -0700 | [diff] [blame] | 671 | } else { |
Tony Barbour | 8205d90 | 2015-04-16 15:59:00 -0600 | [diff] [blame] | 672 | /* Can't support VK_FORMAT_B8G8R8A8_UNORM !? */ |
Piers Daniell | 886be47 | 2015-02-23 16:23:13 -0700 | [diff] [blame] | 673 | assert(!"No support for B8G8R8A8_UNORM as texture image format"); |
Courtney Goeltzenleuchter | 372e13c | 2015-02-13 17:52:46 -0700 | [diff] [blame] | 674 | } |
Courtney Goeltzenleuchter | 3226a6f | 2015-02-11 18:17:22 -0700 | [diff] [blame] | 675 | |
Courtney Goeltzenleuchter | 382489d | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 676 | const VkSamplerCreateInfo sampler = { |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 677 | .sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO, |
Chia-I Wu | b043fe3 | 2014-10-06 15:30:33 +0800 | [diff] [blame] | 678 | .pNext = NULL, |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 679 | .magFilter = VK_TEX_FILTER_NEAREST, |
| 680 | .minFilter = VK_TEX_FILTER_NEAREST, |
Tony Barbour | 8205d90 | 2015-04-16 15:59:00 -0600 | [diff] [blame] | 681 | .mipMode = VK_TEX_MIPMAP_MODE_BASE, |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 682 | .addressU = VK_TEX_ADDRESS_WRAP, |
| 683 | .addressV = VK_TEX_ADDRESS_WRAP, |
| 684 | .addressW = VK_TEX_ADDRESS_WRAP, |
Chia-I Wu | b043fe3 | 2014-10-06 15:30:33 +0800 | [diff] [blame] | 685 | .mipLodBias = 0.0f, |
Courtney Goeltzenleuchter | bc9c816 | 2015-02-13 18:20:24 -0700 | [diff] [blame] | 686 | .maxAnisotropy = 1, |
Tony Barbour | 8205d90 | 2015-04-16 15:59:00 -0600 | [diff] [blame] | 687 | .compareOp = VK_COMPARE_OP_NEVER, |
Chia-I Wu | b043fe3 | 2014-10-06 15:30:33 +0800 | [diff] [blame] | 688 | .minLod = 0.0f, |
| 689 | .maxLod = 0.0f, |
Tony Barbour | 8205d90 | 2015-04-16 15:59:00 -0600 | [diff] [blame] | 690 | .borderColor = VK_BORDER_COLOR_OPAQUE_WHITE, |
Chia-I Wu | b043fe3 | 2014-10-06 15:30:33 +0800 | [diff] [blame] | 691 | }; |
Courtney Goeltzenleuchter | 382489d | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 692 | VkImageViewCreateInfo view = { |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 693 | .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO, |
Chia-I Wu | b043fe3 | 2014-10-06 15:30:33 +0800 | [diff] [blame] | 694 | .pNext = NULL, |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 695 | .image = VK_NULL_HANDLE, |
Tony Barbour | 8205d90 | 2015-04-16 15:59:00 -0600 | [diff] [blame] | 696 | .viewType = VK_IMAGE_VIEW_TYPE_2D, |
Courtney Goeltzenleuchter | 372e13c | 2015-02-13 17:52:46 -0700 | [diff] [blame] | 697 | .format = tex_format, |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 698 | .channels = { VK_CHANNEL_SWIZZLE_R, |
| 699 | VK_CHANNEL_SWIZZLE_G, |
| 700 | VK_CHANNEL_SWIZZLE_B, |
| 701 | VK_CHANNEL_SWIZZLE_A, }, |
| 702 | .subresourceRange = { VK_IMAGE_ASPECT_COLOR, 0, 1, 0, 1 }, |
Chia-I Wu | b043fe3 | 2014-10-06 15:30:33 +0800 | [diff] [blame] | 703 | }; |
Jon Ashburn | a9ae383 | 2015-01-16 09:37:43 -0700 | [diff] [blame] | 704 | |
Chia-I Wu | b043fe3 | 2014-10-06 15:30:33 +0800 | [diff] [blame] | 705 | /* create sampler */ |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 706 | err = vkCreateSampler(demo->device, &sampler, |
Chia-I Wu | b043fe3 | 2014-10-06 15:30:33 +0800 | [diff] [blame] | 707 | &demo->textures[i].sampler); |
| 708 | assert(!err); |
| 709 | |
Chia-I Wu | b043fe3 | 2014-10-06 15:30:33 +0800 | [diff] [blame] | 710 | /* create image view */ |
| 711 | view.image = demo->textures[i].image; |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 712 | err = vkCreateImageView(demo->device, &view, |
Courtney Goeltzenleuchter | 3226a6f | 2015-02-11 18:17:22 -0700 | [diff] [blame] | 713 | &demo->textures[i].view); |
Chia-I Wu | b043fe3 | 2014-10-06 15:30:33 +0800 | [diff] [blame] | 714 | assert(!err); |
Chia-I Wu | b043fe3 | 2014-10-06 15:30:33 +0800 | [diff] [blame] | 715 | } |
| 716 | } |
| 717 | |
Chia-I Wu | 99621bc | 2014-10-08 11:52:22 +0800 | [diff] [blame] | 718 | static void demo_prepare_vertices(struct demo *demo) |
| 719 | { |
| 720 | const float vb[3][5] = { |
| 721 | /* position texcoord */ |
Chia-I Wu | e2504cb | 2015-04-22 14:20:52 +0800 | [diff] [blame] | 722 | { -1.0f, -1.0f, 0.2f, 0.0f, 0.0f }, |
| 723 | { 1.0f, -1.0f, 0.25f, 1.0f, 0.0f }, |
Chia-I Wu | 99621bc | 2014-10-08 11:52:22 +0800 | [diff] [blame] | 724 | { 0.0f, 1.0f, 1.0f, 0.5f, 1.0f }, |
| 725 | }; |
Courtney Goeltzenleuchter | ddcb619 | 2015-04-14 18:48:46 -0600 | [diff] [blame] | 726 | const VkBufferCreateInfo buf_info = { |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 727 | .sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO, |
Chia-I Wu | 714df45 | 2015-01-01 07:55:04 +0800 | [diff] [blame] | 728 | .pNext = NULL, |
| 729 | .size = sizeof(vb), |
Courtney Goeltzenleuchter | ad87081 | 2015-04-15 15:29:59 -0600 | [diff] [blame] | 730 | .usage = VK_BUFFER_USAGE_VERTEX_BUFFER_BIT, |
Chia-I Wu | 714df45 | 2015-01-01 07:55:04 +0800 | [diff] [blame] | 731 | .flags = 0, |
| 732 | }; |
Courtney Goeltzenleuchter | ddcb619 | 2015-04-14 18:48:46 -0600 | [diff] [blame] | 733 | VkMemoryAllocInfo mem_alloc = { |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 734 | .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOC_INFO, |
Mark Lobodzinski | 97dcd04 | 2015-04-16 08:52:00 -0500 | [diff] [blame] | 735 | .pNext = NULL, |
Chia-I Wu | 714df45 | 2015-01-01 07:55:04 +0800 | [diff] [blame] | 736 | .allocationSize = 0, |
Tony Barbour | 8205d90 | 2015-04-16 15:59:00 -0600 | [diff] [blame] | 737 | .memProps = VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT, |
Chia-I Wu | 99621bc | 2014-10-08 11:52:22 +0800 | [diff] [blame] | 738 | }; |
Mark Lobodzinski | 2318261 | 2015-05-29 09:32:35 -0500 | [diff] [blame] | 739 | VkMemoryRequirements mem_reqs; |
Tony Barbour | 22a3086 | 2015-04-22 09:02:32 -0600 | [diff] [blame] | 740 | VkResult U_ASSERT_ONLY err; |
Chia-I Wu | 99621bc | 2014-10-08 11:52:22 +0800 | [diff] [blame] | 741 | void *data; |
| 742 | |
| 743 | memset(&demo->vertices, 0, sizeof(demo->vertices)); |
| 744 | |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 745 | err = vkCreateBuffer(demo->device, &buf_info, &demo->vertices.buf); |
Chia-I Wu | 714df45 | 2015-01-01 07:55:04 +0800 | [diff] [blame] | 746 | assert(!err); |
| 747 | |
Tony Barbour | 426b905 | 2015-06-24 16:06:58 -0600 | [diff] [blame] | 748 | err = vkGetObjectMemoryRequirements(demo->device, |
| 749 | VK_OBJECT_TYPE_BUFFER, demo->vertices.buf, &mem_reqs); |
Chia-I Wu | 714df45 | 2015-01-01 07:55:04 +0800 | [diff] [blame] | 750 | |
Mark Lobodzinski | 2318261 | 2015-05-29 09:32:35 -0500 | [diff] [blame] | 751 | mem_alloc.allocationSize = mem_reqs.size; |
Chia-I Wu | 99621bc | 2014-10-08 11:52:22 +0800 | [diff] [blame] | 752 | |
Mark Lobodzinski | 2318261 | 2015-05-29 09:32:35 -0500 | [diff] [blame] | 753 | err = vkAllocMemory(demo->device, &mem_alloc, &demo->vertices.mem); |
| 754 | assert(!err); |
Chia-I Wu | 99621bc | 2014-10-08 11:52:22 +0800 | [diff] [blame] | 755 | |
Mark Lobodzinski | 2318261 | 2015-05-29 09:32:35 -0500 | [diff] [blame] | 756 | err = vkMapMemory(demo->device, demo->vertices.mem, 0, 0, 0, &data); |
| 757 | assert(!err); |
Chia-I Wu | 99621bc | 2014-10-08 11:52:22 +0800 | [diff] [blame] | 758 | |
Mark Lobodzinski | 2318261 | 2015-05-29 09:32:35 -0500 | [diff] [blame] | 759 | memcpy(data, vb, sizeof(vb)); |
Chia-I Wu | 99621bc | 2014-10-08 11:52:22 +0800 | [diff] [blame] | 760 | |
Mark Lobodzinski | 2318261 | 2015-05-29 09:32:35 -0500 | [diff] [blame] | 761 | err = vkUnmapMemory(demo->device, demo->vertices.mem); |
| 762 | assert(!err); |
| 763 | |
| 764 | err = vkBindObjectMemory(demo->device, |
| 765 | VK_OBJECT_TYPE_BUFFER, demo->vertices.buf, |
| 766 | demo->vertices.mem, 0); |
| 767 | assert(!err); |
Chia-I Wu | 714df45 | 2015-01-01 07:55:04 +0800 | [diff] [blame] | 768 | |
Mark Lobodzinski | 0e0fb5c | 2015-06-23 15:11:57 -0600 | [diff] [blame] | 769 | demo->vertices.vi.sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO; |
Chia-I Wu | 8d29d02 | 2014-10-08 12:14:39 +0800 | [diff] [blame] | 770 | demo->vertices.vi.pNext = NULL; |
| 771 | demo->vertices.vi.bindingCount = 1; |
| 772 | demo->vertices.vi.pVertexBindingDescriptions = demo->vertices.vi_bindings; |
| 773 | demo->vertices.vi.attributeCount = 2; |
| 774 | demo->vertices.vi.pVertexAttributeDescriptions = demo->vertices.vi_attrs; |
| 775 | |
Courtney Goeltzenleuchter | f5cdad0 | 2015-03-31 16:36:30 -0600 | [diff] [blame] | 776 | demo->vertices.vi_bindings[0].binding = VERTEX_BUFFER_BIND_ID; |
Chia-I Wu | 8d29d02 | 2014-10-08 12:14:39 +0800 | [diff] [blame] | 777 | demo->vertices.vi_bindings[0].strideInBytes = sizeof(vb[0]); |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 778 | demo->vertices.vi_bindings[0].stepRate = VK_VERTEX_INPUT_STEP_RATE_VERTEX; |
Chia-I Wu | 8d29d02 | 2014-10-08 12:14:39 +0800 | [diff] [blame] | 779 | |
Courtney Goeltzenleuchter | f5cdad0 | 2015-03-31 16:36:30 -0600 | [diff] [blame] | 780 | demo->vertices.vi_attrs[0].binding = VERTEX_BUFFER_BIND_ID; |
| 781 | demo->vertices.vi_attrs[0].location = 0; |
Tony Barbour | 8205d90 | 2015-04-16 15:59:00 -0600 | [diff] [blame] | 782 | demo->vertices.vi_attrs[0].format = VK_FORMAT_R32G32B32_SFLOAT; |
Chia-I Wu | 8d29d02 | 2014-10-08 12:14:39 +0800 | [diff] [blame] | 783 | demo->vertices.vi_attrs[0].offsetInBytes = 0; |
| 784 | |
Courtney Goeltzenleuchter | f5cdad0 | 2015-03-31 16:36:30 -0600 | [diff] [blame] | 785 | demo->vertices.vi_attrs[1].binding = VERTEX_BUFFER_BIND_ID; |
| 786 | demo->vertices.vi_attrs[1].location = 1; |
Tony Barbour | 8205d90 | 2015-04-16 15:59:00 -0600 | [diff] [blame] | 787 | demo->vertices.vi_attrs[1].format = VK_FORMAT_R32G32_SFLOAT; |
Chia-I Wu | 8d29d02 | 2014-10-08 12:14:39 +0800 | [diff] [blame] | 788 | demo->vertices.vi_attrs[1].offsetInBytes = sizeof(float) * 3; |
Chia-I Wu | 99621bc | 2014-10-08 11:52:22 +0800 | [diff] [blame] | 789 | } |
| 790 | |
Chia-I Wu | f838506 | 2015-01-04 16:27:24 +0800 | [diff] [blame] | 791 | static void demo_prepare_descriptor_layout(struct demo *demo) |
Chia-I Wu | b043fe3 | 2014-10-06 15:30:33 +0800 | [diff] [blame] | 792 | { |
Courtney Goeltzenleuchter | 382489d | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 793 | const VkDescriptorSetLayoutBinding layout_binding = { |
Courtney Goeltzenleuchter | ad87081 | 2015-04-15 15:29:59 -0600 | [diff] [blame] | 794 | .descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, |
Chia-I Wu | d3114a2 | 2015-05-25 16:22:52 +0800 | [diff] [blame] | 795 | .arraySize = DEMO_TEXTURE_COUNT, |
Tony Barbour | 8205d90 | 2015-04-16 15:59:00 -0600 | [diff] [blame] | 796 | .stageFlags = VK_SHADER_STAGE_FRAGMENT_BIT, |
Chia-I Wu | 310eece | 2015-03-27 12:56:09 +0800 | [diff] [blame] | 797 | .pImmutableSamplers = NULL, |
Chia-I Wu | b043fe3 | 2014-10-06 15:30:33 +0800 | [diff] [blame] | 798 | }; |
Courtney Goeltzenleuchter | 382489d | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 799 | const VkDescriptorSetLayoutCreateInfo descriptor_layout = { |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 800 | .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO, |
Chia-I Wu | fc9d913 | 2015-03-26 15:04:41 +0800 | [diff] [blame] | 801 | .pNext = NULL, |
| 802 | .count = 1, |
| 803 | .pBinding = &layout_binding, |
| 804 | }; |
Tony Barbour | 22a3086 | 2015-04-22 09:02:32 -0600 | [diff] [blame] | 805 | VkResult U_ASSERT_ONLY err; |
Chia-I Wu | b043fe3 | 2014-10-06 15:30:33 +0800 | [diff] [blame] | 806 | |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 807 | err = vkCreateDescriptorSetLayout(demo->device, |
Chia-I Wu | 7732cb2 | 2015-03-26 15:27:55 +0800 | [diff] [blame] | 808 | &descriptor_layout, &demo->desc_layout); |
| 809 | assert(!err); |
| 810 | |
Mark Lobodzinski | 556f721 | 2015-04-17 14:11:39 -0500 | [diff] [blame] | 811 | const VkPipelineLayoutCreateInfo pPipelineLayoutCreateInfo = { |
| 812 | .sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO, |
| 813 | .pNext = NULL, |
| 814 | .descriptorSetCount = 1, |
| 815 | .pSetLayouts = &demo->desc_layout, |
| 816 | }; |
| 817 | |
| 818 | err = vkCreatePipelineLayout(demo->device, |
| 819 | &pPipelineLayoutCreateInfo, |
| 820 | &demo->pipeline_layout); |
Chia-I Wu | b043fe3 | 2014-10-06 15:30:33 +0800 | [diff] [blame] | 821 | assert(!err); |
Chia-I Wu | b043fe3 | 2014-10-06 15:30:33 +0800 | [diff] [blame] | 822 | } |
| 823 | |
Courtney Goeltzenleuchter | 382489d | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 824 | static VkShader demo_prepare_shader(struct demo *demo, |
Tony Barbour | 8205d90 | 2015-04-16 15:59:00 -0600 | [diff] [blame] | 825 | VkShaderStage stage, |
Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 826 | const void *code, |
Mark Lobodzinski | e2d07a5 | 2015-01-29 08:55:56 -0600 | [diff] [blame] | 827 | size_t size) |
Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 828 | { |
Courtney Goeltzenleuchter | 382489d | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 829 | VkShaderCreateInfo createInfo; |
| 830 | VkShader shader; |
| 831 | VkResult err; |
Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 832 | |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 833 | createInfo.sType = VK_STRUCTURE_TYPE_SHADER_CREATE_INFO; |
Courtney Goeltzenleuchter | 3f2606d | 2014-10-13 17:51:58 -0600 | [diff] [blame] | 834 | createInfo.pNext = NULL; |
| 835 | |
Cody Northrop | 75db032 | 2015-05-28 11:27:16 -0600 | [diff] [blame] | 836 | if (!demo->use_glsl) { |
| 837 | createInfo.codeSize = size; |
| 838 | createInfo.pCode = code; |
| 839 | createInfo.flags = 0; |
Courtney Goeltzenleuchter | 3f2606d | 2014-10-13 17:51:58 -0600 | [diff] [blame] | 840 | |
Cody Northrop | 75db032 | 2015-05-28 11:27:16 -0600 | [diff] [blame] | 841 | err = vkCreateShader(demo->device, &createInfo, &shader); |
| 842 | if (err) { |
| 843 | free((void *)createInfo.pCode); |
| 844 | } |
| 845 | } else { |
| 846 | // Create fake SPV structure to feed GLSL |
| 847 | // to the driver "under the covers" |
| 848 | createInfo.codeSize = 3 * sizeof(uint32_t) + size + 1; |
| 849 | createInfo.pCode = malloc(createInfo.codeSize); |
| 850 | createInfo.flags = 0; |
Courtney Goeltzenleuchter | 3f2606d | 2014-10-13 17:51:58 -0600 | [diff] [blame] | 851 | |
Cody Northrop | 75db032 | 2015-05-28 11:27:16 -0600 | [diff] [blame] | 852 | /* try version 0 first: VkShaderStage followed by GLSL */ |
| 853 | ((uint32_t *) createInfo.pCode)[0] = ICD_SPV_MAGIC; |
| 854 | ((uint32_t *) createInfo.pCode)[1] = 0; |
| 855 | ((uint32_t *) createInfo.pCode)[2] = stage; |
| 856 | memcpy(((uint32_t *) createInfo.pCode + 3), code, size + 1); |
| 857 | |
| 858 | err = vkCreateShader(demo->device, &createInfo, &shader); |
| 859 | if (err) { |
| 860 | free((void *) createInfo.pCode); |
| 861 | return VK_NULL_HANDLE; |
| 862 | } |
Courtney Goeltzenleuchter | 3f2606d | 2014-10-13 17:51:58 -0600 | [diff] [blame] | 863 | } |
Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 864 | |
| 865 | return shader; |
| 866 | } |
| 867 | |
Cody Northrop | 75db032 | 2015-05-28 11:27:16 -0600 | [diff] [blame] | 868 | char *demo_read_spv(const char *filename, size_t *psize) |
| 869 | { |
| 870 | long int size; |
| 871 | void *shader_code; |
| 872 | |
| 873 | FILE *fp = fopen(filename, "rb"); |
| 874 | if (!fp) return NULL; |
| 875 | |
| 876 | fseek(fp, 0L, SEEK_END); |
| 877 | size = ftell(fp); |
| 878 | |
| 879 | fseek(fp, 0L, SEEK_SET); |
| 880 | |
| 881 | shader_code = malloc(size); |
| 882 | fread(shader_code, size, 1, fp); |
| 883 | |
| 884 | *psize = size; |
| 885 | |
| 886 | return shader_code; |
| 887 | } |
| 888 | |
Courtney Goeltzenleuchter | 382489d | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 889 | static VkShader demo_prepare_vs(struct demo *demo) |
Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 890 | { |
Cody Northrop | 75db032 | 2015-05-28 11:27:16 -0600 | [diff] [blame] | 891 | if (!demo->use_glsl) { |
| 892 | void *vertShaderCode; |
| 893 | size_t size; |
| 894 | |
| 895 | vertShaderCode = demo_read_spv("tri-vert.spv", &size); |
| 896 | |
| 897 | return demo_prepare_shader(demo, VK_SHADER_STAGE_VERTEX, |
| 898 | vertShaderCode, size); |
| 899 | } else { |
| 900 | static const char *vertShaderText = |
Mark Lobodzinski | ba4d2f0 | 2015-04-06 15:24:40 -0500 | [diff] [blame] | 901 | "#version 140\n" |
Courtney Goeltzenleuchter | f5cdad0 | 2015-03-31 16:36:30 -0600 | [diff] [blame] | 902 | "#extension GL_ARB_separate_shader_objects : enable\n" |
| 903 | "#extension GL_ARB_shading_language_420pack : enable\n" |
| 904 | "layout (location = 0) in vec4 pos;\n" |
| 905 | "layout (location = 1) in vec2 attr;\n" |
Chia-I Wu | f5caeb0 | 2014-10-25 12:11:27 +0800 | [diff] [blame] | 906 | "out vec2 texcoord;\n" |
Courtney Goeltzenleuchter | 3f2606d | 2014-10-13 17:51:58 -0600 | [diff] [blame] | 907 | "void main() {\n" |
Chia-I Wu | f5caeb0 | 2014-10-25 12:11:27 +0800 | [diff] [blame] | 908 | " texcoord = attr;\n" |
| 909 | " gl_Position = pos;\n" |
Courtney Goeltzenleuchter | 3f2606d | 2014-10-13 17:51:58 -0600 | [diff] [blame] | 910 | "}\n"; |
Courtney Goeltzenleuchter | ef7301b | 2014-09-17 13:17:12 -0600 | [diff] [blame] | 911 | |
Cody Northrop | 75db032 | 2015-05-28 11:27:16 -0600 | [diff] [blame] | 912 | return demo_prepare_shader(demo, VK_SHADER_STAGE_VERTEX, |
| 913 | (const void *) vertShaderText, |
| 914 | strlen(vertShaderText)); |
| 915 | } |
Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 916 | } |
| 917 | |
Courtney Goeltzenleuchter | 382489d | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 918 | static VkShader demo_prepare_fs(struct demo *demo) |
Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 919 | { |
Cody Northrop | 75db032 | 2015-05-28 11:27:16 -0600 | [diff] [blame] | 920 | if (!demo->use_glsl) { |
| 921 | void *fragShaderCode; |
| 922 | size_t size; |
Courtney Goeltzenleuchter | ef7301b | 2014-09-17 13:17:12 -0600 | [diff] [blame] | 923 | |
Cody Northrop | 75db032 | 2015-05-28 11:27:16 -0600 | [diff] [blame] | 924 | fragShaderCode = demo_read_spv("tri-frag.spv", &size); |
| 925 | |
| 926 | return demo_prepare_shader(demo, VK_SHADER_STAGE_FRAGMENT, |
| 927 | fragShaderCode, size); |
| 928 | } else { |
| 929 | static const char *fragShaderText = |
| 930 | "#version 140\n" |
| 931 | "#extension GL_ARB_separate_shader_objects : enable\n" |
| 932 | "#extension GL_ARB_shading_language_420pack : enable\n" |
| 933 | "layout (binding = 0) uniform sampler2D tex;\n" |
| 934 | "layout (location = 0) in vec2 texcoord;\n" |
| 935 | "layout (location = 0) out vec4 uFragColor;\n" |
| 936 | "void main() {\n" |
| 937 | " uFragColor = texture(tex, texcoord);\n" |
| 938 | "}\n"; |
| 939 | |
| 940 | return demo_prepare_shader(demo, VK_SHADER_STAGE_FRAGMENT, |
| 941 | (const void *) fragShaderText, |
| 942 | strlen(fragShaderText)); |
| 943 | } |
Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 944 | } |
| 945 | |
| 946 | static void demo_prepare_pipeline(struct demo *demo) |
| 947 | { |
Courtney Goeltzenleuchter | 382489d | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 948 | VkGraphicsPipelineCreateInfo pipeline; |
Mark Lobodzinski | 0e0fb5c | 2015-06-23 15:11:57 -0600 | [diff] [blame] | 949 | |
| 950 | VkPipelineVertexInputStateCreateInfo vi; |
Courtney Goeltzenleuchter | 382489d | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 951 | VkPipelineIaStateCreateInfo ia; |
| 952 | VkPipelineRsStateCreateInfo rs; |
| 953 | VkPipelineCbStateCreateInfo cb; |
| 954 | VkPipelineDsStateCreateInfo ds; |
Courtney Goeltzenleuchter | 382489d | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 955 | VkPipelineVpStateCreateInfo vp; |
| 956 | VkPipelineMsStateCreateInfo ms; |
Mark Lobodzinski | 0e0fb5c | 2015-06-23 15:11:57 -0600 | [diff] [blame] | 957 | |
Tony Barbour | 22a3086 | 2015-04-22 09:02:32 -0600 | [diff] [blame] | 958 | VkResult U_ASSERT_ONLY err; |
Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 959 | |
| 960 | memset(&pipeline, 0, sizeof(pipeline)); |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 961 | pipeline.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO; |
Mark Lobodzinski | 556f721 | 2015-04-17 14:11:39 -0500 | [diff] [blame] | 962 | pipeline.layout = demo->pipeline_layout; |
Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 963 | |
Chia-I Wu | 8d29d02 | 2014-10-08 12:14:39 +0800 | [diff] [blame] | 964 | vi = demo->vertices.vi; |
| 965 | |
Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 966 | memset(&ia, 0, sizeof(ia)); |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 967 | ia.sType = VK_STRUCTURE_TYPE_PIPELINE_IA_STATE_CREATE_INFO; |
Tony Barbour | 8205d90 | 2015-04-16 15:59:00 -0600 | [diff] [blame] | 968 | ia.topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST; |
Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 969 | |
| 970 | memset(&rs, 0, sizeof(rs)); |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 971 | rs.sType = VK_STRUCTURE_TYPE_PIPELINE_RS_STATE_CREATE_INFO; |
Tony Barbour | 8205d90 | 2015-04-16 15:59:00 -0600 | [diff] [blame] | 972 | rs.fillMode = VK_FILL_MODE_SOLID; |
Chia-I Wu | c414ba8 | 2015-04-22 15:44:24 +0800 | [diff] [blame] | 973 | rs.cullMode = VK_CULL_MODE_BACK; |
| 974 | rs.frontFace = VK_FRONT_FACE_CW; |
Chia-I Wu | e2504cb | 2015-04-22 14:20:52 +0800 | [diff] [blame] | 975 | rs.depthClipEnable = VK_TRUE; |
Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 976 | |
| 977 | memset(&cb, 0, sizeof(cb)); |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 978 | cb.sType = VK_STRUCTURE_TYPE_PIPELINE_CB_STATE_CREATE_INFO; |
Courtney Goeltzenleuchter | 382489d | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 979 | VkPipelineCbAttachmentState att_state[1]; |
Tony Barbour | fa6cac7 | 2015-01-16 14:27:35 -0700 | [diff] [blame] | 980 | memset(att_state, 0, sizeof(att_state)); |
| 981 | att_state[0].format = demo->format; |
| 982 | att_state[0].channelWriteMask = 0xf; |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 983 | att_state[0].blendEnable = VK_FALSE; |
Tony Barbour | fa6cac7 | 2015-01-16 14:27:35 -0700 | [diff] [blame] | 984 | cb.attachmentCount = 1; |
| 985 | cb.pAttachments = att_state; |
Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 986 | |
Tony Barbour | fa6cac7 | 2015-01-16 14:27:35 -0700 | [diff] [blame] | 987 | memset(&vp, 0, sizeof(vp)); |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 988 | vp.sType = VK_STRUCTURE_TYPE_PIPELINE_VP_STATE_CREATE_INFO; |
Tony Barbour | 8205d90 | 2015-04-16 15:59:00 -0600 | [diff] [blame] | 989 | vp.viewportCount = 1; |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 990 | vp.clipOrigin = VK_COORDINATE_ORIGIN_UPPER_LEFT; |
Tony Barbour | fa6cac7 | 2015-01-16 14:27:35 -0700 | [diff] [blame] | 991 | |
| 992 | memset(&ds, 0, sizeof(ds)); |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 993 | ds.sType = VK_STRUCTURE_TYPE_PIPELINE_DS_STATE_CREATE_INFO; |
Tony Barbour | fa6cac7 | 2015-01-16 14:27:35 -0700 | [diff] [blame] | 994 | ds.format = demo->depth.format; |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 995 | ds.depthTestEnable = VK_TRUE; |
| 996 | ds.depthWriteEnable = VK_TRUE; |
Tony Barbour | 8205d90 | 2015-04-16 15:59:00 -0600 | [diff] [blame] | 997 | ds.depthCompareOp = VK_COMPARE_OP_LESS_EQUAL; |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 998 | ds.depthBoundsEnable = VK_FALSE; |
| 999 | ds.back.stencilFailOp = VK_STENCIL_OP_KEEP; |
| 1000 | ds.back.stencilPassOp = VK_STENCIL_OP_KEEP; |
Tony Barbour | 8205d90 | 2015-04-16 15:59:00 -0600 | [diff] [blame] | 1001 | ds.back.stencilCompareOp = VK_COMPARE_OP_ALWAYS; |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1002 | ds.stencilTestEnable = VK_FALSE; |
Tony Barbour | fa6cac7 | 2015-01-16 14:27:35 -0700 | [diff] [blame] | 1003 | ds.front = ds.back; |
Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 1004 | |
Tony Barbour | fa6cac7 | 2015-01-16 14:27:35 -0700 | [diff] [blame] | 1005 | memset(&ms, 0, sizeof(ms)); |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1006 | ms.sType = VK_STRUCTURE_TYPE_PIPELINE_MS_STATE_CREATE_INFO; |
Tony Barbour | fa6cac7 | 2015-01-16 14:27:35 -0700 | [diff] [blame] | 1007 | ms.sampleMask = 1; |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1008 | ms.multisampleEnable = VK_FALSE; |
Tony Barbour | e094edf | 2015-06-26 10:18:34 -0600 | [diff] [blame^] | 1009 | ms.rasterSamples = 1; |
Chia-I Wu | b043fe3 | 2014-10-06 15:30:33 +0800 | [diff] [blame] | 1010 | |
Mark Lobodzinski | 0e0fb5c | 2015-06-23 15:11:57 -0600 | [diff] [blame] | 1011 | // Two stages: vs and fs |
| 1012 | pipeline.stageCount = 2; |
| 1013 | VkPipelineShaderStageCreateInfo shaderStages[2]; |
| 1014 | memset(&shaderStages, 0, 2 * sizeof(VkPipelineShaderStageCreateInfo)); |
| 1015 | |
| 1016 | shaderStages[0].sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO; |
| 1017 | shaderStages[0].stage = VK_SHADER_STAGE_VERTEX; |
| 1018 | shaderStages[0].shader = demo_prepare_vs(demo); |
| 1019 | shaderStages[0].linkConstBufferCount = 0; |
| 1020 | |
| 1021 | shaderStages[1].sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO; |
| 1022 | shaderStages[1].stage = VK_SHADER_STAGE_FRAGMENT; |
| 1023 | shaderStages[1].shader = demo_prepare_fs(demo); |
| 1024 | |
| 1025 | pipeline.pVertexInputState = &vi; |
| 1026 | pipeline.pIaState = &ia; |
| 1027 | pipeline.pRsState = &rs; |
| 1028 | pipeline.pCbState = &cb; |
| 1029 | pipeline.pMsState = &ms; |
| 1030 | pipeline.pVpState = &vp; |
| 1031 | pipeline.pDsState = &ds; |
| 1032 | pipeline.pStages = shaderStages; |
Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 1033 | |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1034 | err = vkCreateGraphicsPipeline(demo->device, &pipeline, &demo->pipeline); |
Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 1035 | assert(!err); |
| 1036 | |
Mark Lobodzinski | 0e0fb5c | 2015-06-23 15:11:57 -0600 | [diff] [blame] | 1037 | for (uint32_t i = 0; i < pipeline.stageCount; i++) { |
| 1038 | vkDestroyObject(demo->device, VK_OBJECT_TYPE_SHADER, shaderStages[i].shader); |
| 1039 | } |
Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 1040 | } |
| 1041 | |
| 1042 | static void demo_prepare_dynamic_states(struct demo *demo) |
| 1043 | { |
Courtney Goeltzenleuchter | 382489d | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 1044 | VkDynamicVpStateCreateInfo viewport_create; |
| 1045 | VkDynamicRsStateCreateInfo raster; |
| 1046 | VkDynamicCbStateCreateInfo color_blend; |
| 1047 | VkDynamicDsStateCreateInfo depth_stencil; |
Tony Barbour | 22a3086 | 2015-04-22 09:02:32 -0600 | [diff] [blame] | 1048 | VkResult U_ASSERT_ONLY err; |
Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 1049 | |
Tony Barbour | fa6cac7 | 2015-01-16 14:27:35 -0700 | [diff] [blame] | 1050 | memset(&viewport_create, 0, sizeof(viewport_create)); |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1051 | viewport_create.sType = VK_STRUCTURE_TYPE_DYNAMIC_VP_STATE_CREATE_INFO; |
Courtney Goeltzenleuchter | c6e32f9 | 2015-02-11 14:13:34 -0700 | [diff] [blame] | 1052 | viewport_create.viewportAndScissorCount = 1; |
Courtney Goeltzenleuchter | 382489d | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 1053 | VkViewport viewport; |
Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 1054 | memset(&viewport, 0, sizeof(viewport)); |
Mark Lobodzinski | e2d07a5 | 2015-01-29 08:55:56 -0600 | [diff] [blame] | 1055 | viewport.height = (float) demo->height; |
Courtney Goeltzenleuchter | c6e32f9 | 2015-02-11 14:13:34 -0700 | [diff] [blame] | 1056 | viewport.width = (float) demo->width; |
Mark Lobodzinski | e2d07a5 | 2015-01-29 08:55:56 -0600 | [diff] [blame] | 1057 | viewport.minDepth = (float) 0.0f; |
| 1058 | viewport.maxDepth = (float) 1.0f; |
Piers Daniell | 886be47 | 2015-02-23 16:23:13 -0700 | [diff] [blame] | 1059 | viewport_create.pViewports = &viewport; |
Courtney Goeltzenleuchter | 382489d | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 1060 | VkRect scissor; |
Piers Daniell | 886be47 | 2015-02-23 16:23:13 -0700 | [diff] [blame] | 1061 | memset(&scissor, 0, sizeof(scissor)); |
Courtney Goeltzenleuchter | c6e32f9 | 2015-02-11 14:13:34 -0700 | [diff] [blame] | 1062 | scissor.extent.width = demo->width; |
| 1063 | scissor.extent.height = demo->height; |
| 1064 | scissor.offset.x = 0; |
| 1065 | scissor.offset.y = 0; |
Courtney Goeltzenleuchter | c6e32f9 | 2015-02-11 14:13:34 -0700 | [diff] [blame] | 1066 | viewport_create.pScissors = &scissor; |
Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 1067 | |
| 1068 | memset(&raster, 0, sizeof(raster)); |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1069 | raster.sType = VK_STRUCTURE_TYPE_DYNAMIC_RS_STATE_CREATE_INFO; |
Piers Daniell | 886be47 | 2015-02-23 16:23:13 -0700 | [diff] [blame] | 1070 | raster.lineWidth = 1.0; |
Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 1071 | |
| 1072 | memset(&color_blend, 0, sizeof(color_blend)); |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1073 | color_blend.sType = VK_STRUCTURE_TYPE_DYNAMIC_CB_STATE_CREATE_INFO; |
Piers Daniell | 886be47 | 2015-02-23 16:23:13 -0700 | [diff] [blame] | 1074 | color_blend.blendConst[0] = 1.0f; |
| 1075 | color_blend.blendConst[1] = 1.0f; |
| 1076 | color_blend.blendConst[2] = 1.0f; |
| 1077 | color_blend.blendConst[3] = 1.0f; |
Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 1078 | |
| 1079 | memset(&depth_stencil, 0, sizeof(depth_stencil)); |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1080 | depth_stencil.sType = VK_STRUCTURE_TYPE_DYNAMIC_DS_STATE_CREATE_INFO; |
Mark Lobodzinski | 4405fbf | 2015-06-12 11:14:17 -0600 | [diff] [blame] | 1081 | depth_stencil.minDepthBounds = 0.0f; |
| 1082 | depth_stencil.maxDepthBounds = 1.0f; |
Tony Barbour | fa6cac7 | 2015-01-16 14:27:35 -0700 | [diff] [blame] | 1083 | depth_stencil.stencilBackRef = 0; |
| 1084 | depth_stencil.stencilFrontRef = 0; |
| 1085 | depth_stencil.stencilReadMask = 0xff; |
| 1086 | depth_stencil.stencilWriteMask = 0xff; |
Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 1087 | |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1088 | err = vkCreateDynamicViewportState(demo->device, &viewport_create, &demo->viewport); |
Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 1089 | assert(!err); |
| 1090 | |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1091 | err = vkCreateDynamicRasterState(demo->device, &raster, &demo->raster); |
Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 1092 | assert(!err); |
| 1093 | |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1094 | err = vkCreateDynamicColorBlendState(demo->device, |
Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 1095 | &color_blend, &demo->color_blend); |
| 1096 | assert(!err); |
| 1097 | |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1098 | err = vkCreateDynamicDepthStencilState(demo->device, |
Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 1099 | &depth_stencil, &demo->depth_stencil); |
| 1100 | assert(!err); |
| 1101 | } |
| 1102 | |
Chia-I Wu | 8d24b3b | 2015-03-26 13:14:16 +0800 | [diff] [blame] | 1103 | static void demo_prepare_descriptor_pool(struct demo *demo) |
Chia-I Wu | f838506 | 2015-01-04 16:27:24 +0800 | [diff] [blame] | 1104 | { |
Courtney Goeltzenleuchter | 382489d | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 1105 | const VkDescriptorTypeCount type_count = { |
Courtney Goeltzenleuchter | ad87081 | 2015-04-15 15:29:59 -0600 | [diff] [blame] | 1106 | .type = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, |
Chia-I Wu | f838506 | 2015-01-04 16:27:24 +0800 | [diff] [blame] | 1107 | .count = DEMO_TEXTURE_COUNT, |
| 1108 | }; |
Courtney Goeltzenleuchter | 382489d | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 1109 | const VkDescriptorPoolCreateInfo descriptor_pool = { |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1110 | .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO, |
Chia-I Wu | f838506 | 2015-01-04 16:27:24 +0800 | [diff] [blame] | 1111 | .pNext = NULL, |
| 1112 | .count = 1, |
| 1113 | .pTypeCount = &type_count, |
| 1114 | }; |
Tony Barbour | 22a3086 | 2015-04-22 09:02:32 -0600 | [diff] [blame] | 1115 | VkResult U_ASSERT_ONLY err; |
Chia-I Wu | f838506 | 2015-01-04 16:27:24 +0800 | [diff] [blame] | 1116 | |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1117 | err = vkCreateDescriptorPool(demo->device, |
| 1118 | VK_DESCRIPTOR_POOL_USAGE_ONE_SHOT, 1, |
Chia-I Wu | 8d24b3b | 2015-03-26 13:14:16 +0800 | [diff] [blame] | 1119 | &descriptor_pool, &demo->desc_pool); |
Chia-I Wu | f838506 | 2015-01-04 16:27:24 +0800 | [diff] [blame] | 1120 | assert(!err); |
| 1121 | } |
| 1122 | |
| 1123 | static void demo_prepare_descriptor_set(struct demo *demo) |
| 1124 | { |
Chia-I Wu | 8cd8ecd | 2015-05-25 16:27:55 +0800 | [diff] [blame] | 1125 | VkDescriptorInfo tex_descs[DEMO_TEXTURE_COUNT]; |
| 1126 | VkWriteDescriptorSet write; |
Tony Barbour | 22a3086 | 2015-04-22 09:02:32 -0600 | [diff] [blame] | 1127 | VkResult U_ASSERT_ONLY err; |
Chia-I Wu | f838506 | 2015-01-04 16:27:24 +0800 | [diff] [blame] | 1128 | uint32_t count; |
Mark Lobodzinski | e2d07a5 | 2015-01-29 08:55:56 -0600 | [diff] [blame] | 1129 | uint32_t i; |
Chia-I Wu | f838506 | 2015-01-04 16:27:24 +0800 | [diff] [blame] | 1130 | |
Mike Stroyan | 230e625 | 2015-04-17 12:36:38 -0600 | [diff] [blame] | 1131 | err = vkAllocDescriptorSets(demo->device, demo->desc_pool, |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1132 | VK_DESCRIPTOR_SET_USAGE_STATIC, |
Chia-I Wu | f838506 | 2015-01-04 16:27:24 +0800 | [diff] [blame] | 1133 | 1, &demo->desc_layout, |
| 1134 | &demo->desc_set, &count); |
| 1135 | assert(!err && count == 1); |
| 1136 | |
Chia-I Wu | 8cd8ecd | 2015-05-25 16:27:55 +0800 | [diff] [blame] | 1137 | memset(&tex_descs, 0, sizeof(tex_descs)); |
| 1138 | for (i = 0; i < DEMO_TEXTURE_COUNT; i++) { |
| 1139 | tex_descs[i].sampler = demo->textures[i].sampler; |
| 1140 | tex_descs[i].imageView = demo->textures[i].view; |
| 1141 | tex_descs[i].imageLayout = VK_IMAGE_LAYOUT_GENERAL; |
| 1142 | } |
| 1143 | |
| 1144 | memset(&write, 0, sizeof(write)); |
| 1145 | write.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET; |
| 1146 | write.destSet = demo->desc_set; |
| 1147 | write.count = DEMO_TEXTURE_COUNT; |
| 1148 | write.descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER; |
| 1149 | write.pDescriptors = tex_descs; |
| 1150 | |
| 1151 | err = vkUpdateDescriptorSets(demo->device, 1, &write, 0, NULL); |
| 1152 | assert(!err); |
Chia-I Wu | f838506 | 2015-01-04 16:27:24 +0800 | [diff] [blame] | 1153 | } |
| 1154 | |
Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 1155 | static void demo_prepare(struct demo *demo) |
| 1156 | { |
Courtney Goeltzenleuchter | 382489d | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 1157 | const VkCmdBufferCreateInfo cmd = { |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1158 | .sType = VK_STRUCTURE_TYPE_CMD_BUFFER_CREATE_INFO, |
Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 1159 | .pNext = NULL, |
Courtney Goeltzenleuchter | f316806 | 2015-03-05 18:09:39 -0700 | [diff] [blame] | 1160 | .queueNodeIndex = demo->graphics_queue_node_index, |
Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 1161 | .flags = 0, |
| 1162 | }; |
Tony Barbour | 22a3086 | 2015-04-22 09:02:32 -0600 | [diff] [blame] | 1163 | VkResult U_ASSERT_ONLY err; |
Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 1164 | |
Courtney Goeltzenleuchter | 633f9c5 | 2015-04-30 10:58:33 -0600 | [diff] [blame] | 1165 | err = vkCreateCommandBuffer(demo->device, &cmd, &demo->draw_cmd); |
| 1166 | assert(!err); |
| 1167 | |
Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 1168 | demo_prepare_buffers(demo); |
Chia-I Wu | 9ae87c9 | 2014-10-07 14:15:01 +0800 | [diff] [blame] | 1169 | demo_prepare_depth(demo); |
Chia-I Wu | b043fe3 | 2014-10-06 15:30:33 +0800 | [diff] [blame] | 1170 | demo_prepare_textures(demo); |
Chia-I Wu | 99621bc | 2014-10-08 11:52:22 +0800 | [diff] [blame] | 1171 | demo_prepare_vertices(demo); |
Chia-I Wu | f838506 | 2015-01-04 16:27:24 +0800 | [diff] [blame] | 1172 | demo_prepare_descriptor_layout(demo); |
Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 1173 | demo_prepare_pipeline(demo); |
| 1174 | demo_prepare_dynamic_states(demo); |
| 1175 | |
Chia-I Wu | 8d24b3b | 2015-03-26 13:14:16 +0800 | [diff] [blame] | 1176 | demo_prepare_descriptor_pool(demo); |
Chia-I Wu | f838506 | 2015-01-04 16:27:24 +0800 | [diff] [blame] | 1177 | demo_prepare_descriptor_set(demo); |
Courtney Goeltzenleuchter | 633f9c5 | 2015-04-30 10:58:33 -0600 | [diff] [blame] | 1178 | demo->prepared = true; |
Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 1179 | } |
| 1180 | |
Ian Elliott | e14e9f9 | 2015-04-16 15:23:05 -0600 | [diff] [blame] | 1181 | #ifdef _WIN32 |
| 1182 | static void demo_run(struct demo *demo) |
| 1183 | { |
Courtney Goeltzenleuchter | 633f9c5 | 2015-04-30 10:58:33 -0600 | [diff] [blame] | 1184 | if (!demo->prepared) |
| 1185 | return; |
Ian Elliott | e14e9f9 | 2015-04-16 15:23:05 -0600 | [diff] [blame] | 1186 | demo_draw(demo); |
| 1187 | } |
| 1188 | |
| 1189 | // On MS-Windows, make this a global, so it's available to WndProc() |
| 1190 | struct demo demo; |
| 1191 | |
| 1192 | // MS-Windows event handling function: |
| 1193 | LRESULT CALLBACK WndProc(HWND hWnd, |
| 1194 | UINT uMsg, |
| 1195 | WPARAM wParam, |
| 1196 | LPARAM lParam) |
| 1197 | { |
Ian Elliott | 4e19ed0 | 2015-04-28 10:52:52 -0600 | [diff] [blame] | 1198 | char tmp_str[] = APP_LONG_NAME; |
Ian Elliott | e14e9f9 | 2015-04-16 15:23:05 -0600 | [diff] [blame] | 1199 | |
| 1200 | switch(uMsg) |
| 1201 | { |
| 1202 | case WM_CREATE: |
| 1203 | return 0; |
| 1204 | case WM_CLOSE: |
| 1205 | PostQuitMessage(0); |
| 1206 | return 0; |
| 1207 | case WM_PAINT: |
| 1208 | demo_run(&demo); |
| 1209 | return 0; |
| 1210 | default: |
| 1211 | break; |
| 1212 | } |
| 1213 | return (DefWindowProc(hWnd, uMsg, wParam, lParam)); |
| 1214 | } |
| 1215 | |
| 1216 | static void demo_create_window(struct demo *demo) |
| 1217 | { |
| 1218 | WNDCLASSEX win_class; |
| 1219 | |
| 1220 | // Initialize the window class structure: |
| 1221 | win_class.cbSize = sizeof(WNDCLASSEX); |
| 1222 | win_class.style = CS_HREDRAW | CS_VREDRAW; |
| 1223 | win_class.lpfnWndProc = WndProc; |
| 1224 | win_class.cbClsExtra = 0; |
| 1225 | win_class.cbWndExtra = 0; |
| 1226 | win_class.hInstance = demo->connection; // hInstance |
| 1227 | win_class.hIcon = LoadIcon(NULL, IDI_APPLICATION); |
| 1228 | win_class.hCursor = LoadCursor(NULL, IDC_ARROW); |
| 1229 | win_class.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH); |
| 1230 | win_class.lpszMenuName = NULL; |
| 1231 | win_class.lpszClassName = demo->name; |
| 1232 | win_class.hIconSm = LoadIcon(NULL, IDI_WINLOGO); |
| 1233 | // Register window class: |
| 1234 | if (!RegisterClassEx(&win_class)) { |
| 1235 | // It didn't work, so try to give a useful error: |
| 1236 | printf("Unexpected error trying to start the application!\n"); |
| 1237 | fflush(stdout); |
| 1238 | exit(1); |
| 1239 | } |
| 1240 | // Create window with the registered class: |
Mike Stroyan | 7eef574 | 2015-06-15 14:19:19 -0600 | [diff] [blame] | 1241 | RECT wr = { 0, 0, demo->width, demo->height }; |
| 1242 | AdjustWindowRect(&wr, WS_OVERLAPPEDWINDOW, FALSE); |
Ian Elliott | e14e9f9 | 2015-04-16 15:23:05 -0600 | [diff] [blame] | 1243 | demo->window = CreateWindowEx(0, |
| 1244 | demo->name, // class name |
| 1245 | demo->name, // app name |
| 1246 | WS_OVERLAPPEDWINDOW | // window style |
| 1247 | WS_VISIBLE | |
| 1248 | WS_SYSMENU, |
| 1249 | 100,100, // x/y coords |
Mike Stroyan | 7eef574 | 2015-06-15 14:19:19 -0600 | [diff] [blame] | 1250 | wr.right-wr.left, // width |
| 1251 | wr.bottom-wr.top, // height |
Ian Elliott | e14e9f9 | 2015-04-16 15:23:05 -0600 | [diff] [blame] | 1252 | NULL, // handle to parent |
| 1253 | NULL, // handle to menu |
| 1254 | demo->connection, // hInstance |
| 1255 | NULL); // no extra parameters |
| 1256 | if (!demo->window) { |
| 1257 | // It didn't work, so try to give a useful error: |
| 1258 | printf("Cannot create a window in which to draw!\n"); |
| 1259 | fflush(stdout); |
| 1260 | exit(1); |
| 1261 | } |
| 1262 | } |
| 1263 | #else // _WIN32 |
| 1264 | |
Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 1265 | static void demo_handle_event(struct demo *demo, |
| 1266 | const xcb_generic_event_t *event) |
| 1267 | { |
| 1268 | switch (event->response_type & 0x7f) { |
| 1269 | case XCB_EXPOSE: |
| 1270 | demo_draw(demo); |
| 1271 | break; |
Courtney Goeltzenleuchter | ca69805 | 2014-11-07 15:17:03 -0700 | [diff] [blame] | 1272 | case XCB_CLIENT_MESSAGE: |
| 1273 | if((*(xcb_client_message_event_t*)event).data.data32[0] == |
| 1274 | (*demo->atom_wm_delete_window).atom) { |
| 1275 | demo->quit = true; |
| 1276 | } |
| 1277 | break; |
Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 1278 | case XCB_KEY_RELEASE: |
| 1279 | { |
| 1280 | const xcb_key_release_event_t *key = |
| 1281 | (const xcb_key_release_event_t *) event; |
| 1282 | |
| 1283 | if (key->detail == 0x9) |
| 1284 | demo->quit = true; |
| 1285 | } |
| 1286 | break; |
Courtney Goeltzenleuchter | ca69805 | 2014-11-07 15:17:03 -0700 | [diff] [blame] | 1287 | case XCB_DESTROY_NOTIFY: |
| 1288 | demo->quit = true; |
| 1289 | break; |
Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 1290 | default: |
| 1291 | break; |
| 1292 | } |
| 1293 | } |
| 1294 | |
| 1295 | static void demo_run(struct demo *demo) |
| 1296 | { |
| 1297 | xcb_flush(demo->connection); |
| 1298 | |
| 1299 | while (!demo->quit) { |
| 1300 | xcb_generic_event_t *event; |
| 1301 | |
| 1302 | event = xcb_wait_for_event(demo->connection); |
| 1303 | if (event) { |
| 1304 | demo_handle_event(demo, event); |
| 1305 | free(event); |
| 1306 | } |
| 1307 | } |
| 1308 | } |
| 1309 | |
| 1310 | static void demo_create_window(struct demo *demo) |
| 1311 | { |
| 1312 | uint32_t value_mask, value_list[32]; |
| 1313 | |
| 1314 | demo->window = xcb_generate_id(demo->connection); |
| 1315 | |
| 1316 | value_mask = XCB_CW_BACK_PIXEL | XCB_CW_EVENT_MASK; |
| 1317 | value_list[0] = demo->screen->black_pixel; |
| 1318 | value_list[1] = XCB_EVENT_MASK_KEY_RELEASE | |
Courtney Goeltzenleuchter | ca69805 | 2014-11-07 15:17:03 -0700 | [diff] [blame] | 1319 | XCB_EVENT_MASK_EXPOSURE | |
| 1320 | XCB_EVENT_MASK_STRUCTURE_NOTIFY; |
Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 1321 | |
| 1322 | xcb_create_window(demo->connection, |
| 1323 | XCB_COPY_FROM_PARENT, |
| 1324 | demo->window, demo->screen->root, |
| 1325 | 0, 0, demo->width, demo->height, 0, |
| 1326 | XCB_WINDOW_CLASS_INPUT_OUTPUT, |
| 1327 | demo->screen->root_visual, |
| 1328 | value_mask, value_list); |
| 1329 | |
Courtney Goeltzenleuchter | ca69805 | 2014-11-07 15:17:03 -0700 | [diff] [blame] | 1330 | /* Magic code that will send notification when window is destroyed */ |
| 1331 | xcb_intern_atom_cookie_t cookie = xcb_intern_atom(demo->connection, 1, 12, |
| 1332 | "WM_PROTOCOLS"); |
| 1333 | xcb_intern_atom_reply_t* reply = xcb_intern_atom_reply(demo->connection, cookie, 0); |
| 1334 | |
| 1335 | xcb_intern_atom_cookie_t cookie2 = xcb_intern_atom(demo->connection, 0, 16, "WM_DELETE_WINDOW"); |
| 1336 | demo->atom_wm_delete_window = xcb_intern_atom_reply(demo->connection, cookie2, 0); |
| 1337 | |
| 1338 | xcb_change_property(demo->connection, XCB_PROP_MODE_REPLACE, |
| 1339 | demo->window, (*reply).atom, 4, 32, 1, |
| 1340 | &(*demo->atom_wm_delete_window).atom); |
| 1341 | free(reply); |
| 1342 | |
Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 1343 | xcb_map_window(demo->connection, demo->window); |
| 1344 | } |
Ian Elliott | e14e9f9 | 2015-04-16 15:23:05 -0600 | [diff] [blame] | 1345 | #endif // _WIN32 |
Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 1346 | |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1347 | static void demo_init_vk(struct demo *demo) |
Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 1348 | { |
Tobin Ehlis | 3536b44 | 2015-04-16 18:04:57 -0600 | [diff] [blame] | 1349 | VkResult err; |
Courtney Goeltzenleuchter | 1c7c65d | 2015-06-10 17:39:03 -0600 | [diff] [blame] | 1350 | VkExtensionProperties *instance_extensions; |
| 1351 | uint32_t instance_extension_count = 0; |
| 1352 | VkExtensionProperties *device_extensions; |
| 1353 | uint32_t device_extension_count = 0; |
Courtney Goeltzenleuchter | 1c7c65d | 2015-06-10 17:39:03 -0600 | [diff] [blame] | 1354 | uint32_t total_extension_count = 0; |
Tony Barbour | 426b905 | 2015-06-24 16:06:58 -0600 | [diff] [blame] | 1355 | err = vkGetGlobalExtensionCount(&total_extension_count); |
Tobin Ehlis | 3536b44 | 2015-04-16 18:04:57 -0600 | [diff] [blame] | 1356 | assert(!err); |
| 1357 | |
| 1358 | VkExtensionProperties extProp; |
Courtney Goeltzenleuchter | 1c7c65d | 2015-06-10 17:39:03 -0600 | [diff] [blame] | 1359 | bool32_t WSIextFound = 0; |
| 1360 | instance_extensions = malloc(sizeof(VkExtensionProperties) * total_extension_count); |
| 1361 | device_extensions = malloc(sizeof(VkExtensionProperties) * total_extension_count); |
| 1362 | for (uint32_t i = 0; i < total_extension_count; i++) { |
Tony Barbour | 426b905 | 2015-06-24 16:06:58 -0600 | [diff] [blame] | 1363 | err = vkGetGlobalExtensionProperties(i, &extProp); |
Courtney Goeltzenleuchter | 1c7c65d | 2015-06-10 17:39:03 -0600 | [diff] [blame] | 1364 | if (!strcmp("VK_WSI_LunarG", extProp.name)) { |
| 1365 | WSIextFound = 1; |
| 1366 | memcpy(&instance_extensions[instance_extension_count++], &extProp, sizeof(VkExtensionProperties)); |
| 1367 | memcpy(&device_extensions[device_extension_count++], &extProp, sizeof(VkExtensionProperties)); |
| 1368 | } |
Tobin Ehlis | 3536b44 | 2015-04-16 18:04:57 -0600 | [diff] [blame] | 1369 | } |
Courtney Goeltzenleuchter | 1c7c65d | 2015-06-10 17:39:03 -0600 | [diff] [blame] | 1370 | if (!WSIextFound) { |
Tony Barbour | 426b905 | 2015-06-24 16:06:58 -0600 | [diff] [blame] | 1371 | ERR_EXIT("vkGetGlobalExtensionProperties failed to find the " |
Ian Elliott | 3b375cf | 2015-04-28 13:22:33 -0600 | [diff] [blame] | 1372 | "\"VK_WSI_LunarG\" extension.\n\nDo you have a compatible " |
| 1373 | "Vulkan installable client driver (ICD) installed?\nPlease " |
| 1374 | "look at the Getting Started guide for additional " |
| 1375 | "information.\n", |
| 1376 | "vkCreateInstance Failure"); |
| 1377 | } |
Courtney Goeltzenleuchter | 382489d | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 1378 | const VkApplicationInfo app = { |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1379 | .sType = VK_STRUCTURE_TYPE_APPLICATION_INFO, |
Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 1380 | .pNext = NULL, |
Ian Elliott | 4e19ed0 | 2015-04-28 10:52:52 -0600 | [diff] [blame] | 1381 | .pAppName = APP_SHORT_NAME, |
Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 1382 | .appVersion = 0, |
Ian Elliott | 4e19ed0 | 2015-04-28 10:52:52 -0600 | [diff] [blame] | 1383 | .pEngineName = APP_SHORT_NAME, |
Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 1384 | .engineVersion = 0, |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1385 | .apiVersion = VK_API_VERSION, |
Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 1386 | }; |
Courtney Goeltzenleuchter | ddcb619 | 2015-04-14 18:48:46 -0600 | [diff] [blame] | 1387 | const VkInstanceCreateInfo inst_info = { |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1388 | .sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO, |
Jon Ashburn | 29669a4 | 2015-04-04 14:52:07 -0600 | [diff] [blame] | 1389 | .pNext = NULL, |
| 1390 | .pAppInfo = &app, |
| 1391 | .pAllocCb = NULL, |
Courtney Goeltzenleuchter | 1c7c65d | 2015-06-10 17:39:03 -0600 | [diff] [blame] | 1392 | .extensionCount = instance_extension_count, |
| 1393 | .pEnabledExtensions = instance_extensions, |
Jon Ashburn | 29669a4 | 2015-04-04 14:52:07 -0600 | [diff] [blame] | 1394 | }; |
Courtney Goeltzenleuchter | ddcb619 | 2015-04-14 18:48:46 -0600 | [diff] [blame] | 1395 | const VkDeviceQueueCreateInfo queue = { |
Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 1396 | .queueNodeIndex = 0, |
| 1397 | .queueCount = 1, |
| 1398 | }; |
Courtney Goeltzenleuchter | ddcb619 | 2015-04-14 18:48:46 -0600 | [diff] [blame] | 1399 | const VkDeviceCreateInfo device = { |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1400 | .sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO, |
Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 1401 | .pNext = NULL, |
| 1402 | .queueRecordCount = 1, |
| 1403 | .pRequestedQueues = &queue, |
Courtney Goeltzenleuchter | 1c7c65d | 2015-06-10 17:39:03 -0600 | [diff] [blame] | 1404 | .extensionCount = device_extension_count, |
| 1405 | .pEnabledExtensions = device_extensions, |
Jon Ashburn | 80d3b71 | 2015-05-18 09:06:15 -0600 | [diff] [blame] | 1406 | .flags = 0, |
Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 1407 | }; |
Mark Lobodzinski | e2d07a5 | 2015-01-29 08:55:56 -0600 | [diff] [blame] | 1408 | uint32_t gpu_count; |
| 1409 | uint32_t i; |
Courtney Goeltzenleuchter | f316806 | 2015-03-05 18:09:39 -0700 | [diff] [blame] | 1410 | uint32_t queue_count; |
Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 1411 | |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1412 | err = vkCreateInstance(&inst_info, &demo->inst); |
Ian Elliott | caa9f27 | 2015-04-28 11:35:02 -0600 | [diff] [blame] | 1413 | if (err == VK_ERROR_INCOMPATIBLE_DRIVER) { |
| 1414 | ERR_EXIT("Cannot find a compatible Vulkan installable client driver " |
Ian Elliott | 3b375cf | 2015-04-28 13:22:33 -0600 | [diff] [blame] | 1415 | "(ICD).\n\nPlease look at the Getting Started guide for " |
Ian Elliott | caa9f27 | 2015-04-28 11:35:02 -0600 | [diff] [blame] | 1416 | "additional information.\n", |
| 1417 | "vkCreateInstance Failure"); |
| 1418 | } else if (err) { |
Ian Elliott | 3b375cf | 2015-04-28 13:22:33 -0600 | [diff] [blame] | 1419 | ERR_EXIT("vkCreateInstance failed.\n\nDo you have a compatible Vulkan " |
| 1420 | "installable client driver (ICD) installed?\nPlease look at " |
Ian Elliott | caa9f27 | 2015-04-28 11:35:02 -0600 | [diff] [blame] | 1421 | "the Getting Started guide for additional information.\n", |
| 1422 | "vkCreateInstance Failure"); |
Ian Elliott | dfe55f7 | 2015-04-03 15:24:55 -0600 | [diff] [blame] | 1423 | } |
Jon Ashburn | 29669a4 | 2015-04-04 14:52:07 -0600 | [diff] [blame] | 1424 | |
Jon Ashburn | 07b309a | 2015-04-15 11:31:12 -0600 | [diff] [blame] | 1425 | gpu_count = 1; |
| 1426 | err = vkEnumeratePhysicalDevices(demo->inst, &gpu_count, &demo->gpu); |
Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 1427 | assert(!err && gpu_count == 1); |
| 1428 | |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1429 | err = vkCreateDevice(demo->gpu, &device, &demo->device); |
Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 1430 | assert(!err); |
| 1431 | |
Ian Elliott | 1b6de09 | 2015-06-22 15:07:49 -0600 | [diff] [blame] | 1432 | GET_DEVICE_PROC_ADDR(demo->device, CreateSwapChainWSI); |
| 1433 | GET_DEVICE_PROC_ADDR(demo->device, CreateSwapChainWSI); |
| 1434 | GET_DEVICE_PROC_ADDR(demo->device, DestroySwapChainWSI); |
| 1435 | GET_DEVICE_PROC_ADDR(demo->device, GetSwapChainInfoWSI); |
| 1436 | GET_DEVICE_PROC_ADDR(demo->device, QueuePresentWSI); |
| 1437 | |
Tony Barbour | 426b905 | 2015-06-24 16:06:58 -0600 | [diff] [blame] | 1438 | err = vkGetPhysicalDeviceProperties(demo->gpu, &demo->gpu_props); |
Courtney Goeltzenleuchter | bfccf41 | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 1439 | assert(!err); |
| 1440 | |
Tony Barbour | 426b905 | 2015-06-24 16:06:58 -0600 | [diff] [blame] | 1441 | err = vkGetPhysicalDeviceQueueCount(demo->gpu, &queue_count); |
Courtney Goeltzenleuchter | bfccf41 | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 1442 | assert(!err); |
| 1443 | |
Tony Barbour | 426b905 | 2015-06-24 16:06:58 -0600 | [diff] [blame] | 1444 | demo->queue_props = (VkPhysicalDeviceQueueProperties *) malloc(queue_count * sizeof(VkPhysicalDeviceQueueProperties)); |
| 1445 | err = vkGetPhysicalDeviceQueueProperties(demo->gpu, queue_count, demo->queue_props); |
Courtney Goeltzenleuchter | f316806 | 2015-03-05 18:09:39 -0700 | [diff] [blame] | 1446 | assert(!err); |
Courtney Goeltzenleuchter | f316806 | 2015-03-05 18:09:39 -0700 | [diff] [blame] | 1447 | assert(queue_count >= 1); |
| 1448 | |
| 1449 | for (i = 0; i < queue_count; i++) { |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1450 | if (demo->queue_props[i].queueFlags & VK_QUEUE_GRAPHICS_BIT) |
Courtney Goeltzenleuchter | f316806 | 2015-03-05 18:09:39 -0700 | [diff] [blame] | 1451 | break; |
| 1452 | } |
| 1453 | assert(i < queue_count); |
| 1454 | demo->graphics_queue_node_index = i; |
| 1455 | |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1456 | err = vkGetDeviceQueue(demo->device, demo->graphics_queue_node_index, |
Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 1457 | 0, &demo->queue); |
| 1458 | assert(!err); |
Ian Elliott | 32536f9 | 2015-04-21 16:41:02 -0600 | [diff] [blame] | 1459 | |
Jon Ashburn | ba4a195 | 2015-06-16 12:44:51 -0600 | [diff] [blame] | 1460 | // for now hardcode format till get WSI support |
| 1461 | demo->format = VK_FORMAT_B8G8R8A8_UNORM; |
Ian Elliott | 32536f9 | 2015-04-21 16:41:02 -0600 | [diff] [blame] | 1462 | |
Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 1463 | } |
| 1464 | |
| 1465 | static void demo_init_connection(struct demo *demo) |
| 1466 | { |
Ian Elliott | e14e9f9 | 2015-04-16 15:23:05 -0600 | [diff] [blame] | 1467 | #ifndef _WIN32 |
Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 1468 | const xcb_setup_t *setup; |
| 1469 | xcb_screen_iterator_t iter; |
| 1470 | int scr; |
| 1471 | |
| 1472 | demo->connection = xcb_connect(NULL, &scr); |
Ian Elliott | dfe55f7 | 2015-04-03 15:24:55 -0600 | [diff] [blame] | 1473 | if (demo->connection == NULL) { |
| 1474 | printf("Cannot find a compatible Vulkan installable client driver " |
| 1475 | "(ICD).\nExiting ...\n"); |
| 1476 | fflush(stdout); |
| 1477 | exit(1); |
| 1478 | } |
Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 1479 | |
| 1480 | setup = xcb_get_setup(demo->connection); |
| 1481 | iter = xcb_setup_roots_iterator(setup); |
| 1482 | while (scr-- > 0) |
| 1483 | xcb_screen_next(&iter); |
| 1484 | |
| 1485 | demo->screen = iter.data; |
Ian Elliott | e14e9f9 | 2015-04-16 15:23:05 -0600 | [diff] [blame] | 1486 | #endif // _WIN32 |
Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 1487 | } |
| 1488 | |
Ian Elliott | e14e9f9 | 2015-04-16 15:23:05 -0600 | [diff] [blame] | 1489 | #ifdef _WIN32 |
| 1490 | static void demo_init(struct demo *demo, HINSTANCE hInstance, LPSTR pCmdLine) |
| 1491 | #else // _WIN32 |
Courtney Goeltzenleuchter | f113a95 | 2015-02-25 11:46:58 -0700 | [diff] [blame] | 1492 | static void demo_init(struct demo *demo, const int argc, const char *argv[]) |
Ian Elliott | e14e9f9 | 2015-04-16 15:23:05 -0600 | [diff] [blame] | 1493 | #endif // _WIN32 |
Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 1494 | { |
Ian Elliott | e14e9f9 | 2015-04-16 15:23:05 -0600 | [diff] [blame] | 1495 | bool argv_error = false; |
| 1496 | |
Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 1497 | memset(demo, 0, sizeof(*demo)); |
| 1498 | |
Ian Elliott | e14e9f9 | 2015-04-16 15:23:05 -0600 | [diff] [blame] | 1499 | #ifdef _WIN32 |
| 1500 | demo->connection = hInstance; |
Ian Elliott | 4e19ed0 | 2015-04-28 10:52:52 -0600 | [diff] [blame] | 1501 | strncpy(demo->name, APP_SHORT_NAME, APP_NAME_STR_LEN); |
Ian Elliott | e14e9f9 | 2015-04-16 15:23:05 -0600 | [diff] [blame] | 1502 | |
| 1503 | if (strncmp(pCmdLine, "--use_staging", strlen("--use_staging")) == 0) |
| 1504 | demo->use_staging_buffer = true; |
Cody Northrop | 75db032 | 2015-05-28 11:27:16 -0600 | [diff] [blame] | 1505 | else if (strncmp(pCmdLine, "--use_glsl", strlen("--use_glsl")) == 0) |
| 1506 | demo->use_glsl = true; |
Ian Elliott | e14e9f9 | 2015-04-16 15:23:05 -0600 | [diff] [blame] | 1507 | else if (strlen(pCmdLine) != 0) { |
| 1508 | fprintf(stderr, "Do not recognize argument \"%s\".\n", pCmdLine); |
| 1509 | argv_error = true; |
| 1510 | } |
| 1511 | #else // _WIN32 |
Courtney Goeltzenleuchter | f113a95 | 2015-02-25 11:46:58 -0700 | [diff] [blame] | 1512 | for (int i = 0; i < argc; i++) { |
| 1513 | if (strncmp(argv[i], "--use_staging", strlen("--use_staging")) == 0) |
| 1514 | demo->use_staging_buffer = true; |
Cody Northrop | 75db032 | 2015-05-28 11:27:16 -0600 | [diff] [blame] | 1515 | else if (strncmp(argv[i], "--use_glsl", strlen("--use_glsl")) == 0) |
| 1516 | demo->use_glsl = true; |
Courtney Goeltzenleuchter | f113a95 | 2015-02-25 11:46:58 -0700 | [diff] [blame] | 1517 | } |
Ian Elliott | e14e9f9 | 2015-04-16 15:23:05 -0600 | [diff] [blame] | 1518 | #endif // _WIN32 |
| 1519 | if (argv_error) { |
Ian Elliott | 4e19ed0 | 2015-04-28 10:52:52 -0600 | [diff] [blame] | 1520 | fprintf(stderr, "Usage:\n %s [--use_staging]\n", APP_SHORT_NAME); |
Ian Elliott | e14e9f9 | 2015-04-16 15:23:05 -0600 | [diff] [blame] | 1521 | fflush(stderr); |
| 1522 | exit(1); |
| 1523 | } |
Courtney Goeltzenleuchter | f113a95 | 2015-02-25 11:46:58 -0700 | [diff] [blame] | 1524 | |
Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 1525 | demo_init_connection(demo); |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1526 | demo_init_vk(demo); |
Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 1527 | |
| 1528 | demo->width = 300; |
| 1529 | demo->height = 300; |
Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 1530 | } |
| 1531 | |
| 1532 | static void demo_cleanup(struct demo *demo) |
| 1533 | { |
Mark Lobodzinski | 2318261 | 2015-05-29 09:32:35 -0500 | [diff] [blame] | 1534 | uint32_t i; |
Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 1535 | |
Mike Stroyan | 230e625 | 2015-04-17 12:36:38 -0600 | [diff] [blame] | 1536 | vkDestroyObject(demo->device, VK_OBJECT_TYPE_DESCRIPTOR_SET, demo->desc_set); |
| 1537 | vkDestroyObject(demo->device, VK_OBJECT_TYPE_DESCRIPTOR_POOL, demo->desc_pool); |
Chia-I Wu | f838506 | 2015-01-04 16:27:24 +0800 | [diff] [blame] | 1538 | |
Courtney Goeltzenleuchter | 633f9c5 | 2015-04-30 10:58:33 -0600 | [diff] [blame] | 1539 | if (demo->setup_cmd) { |
| 1540 | vkDestroyObject(demo->device, VK_OBJECT_TYPE_COMMAND_BUFFER, demo->setup_cmd); |
| 1541 | } |
| 1542 | vkDestroyObject(demo->device, VK_OBJECT_TYPE_COMMAND_BUFFER, demo->draw_cmd); |
Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 1543 | |
Mike Stroyan | 230e625 | 2015-04-17 12:36:38 -0600 | [diff] [blame] | 1544 | vkDestroyObject(demo->device, VK_OBJECT_TYPE_DYNAMIC_VP_STATE, demo->viewport); |
| 1545 | vkDestroyObject(demo->device, VK_OBJECT_TYPE_DYNAMIC_RS_STATE, demo->raster); |
| 1546 | vkDestroyObject(demo->device, VK_OBJECT_TYPE_DYNAMIC_CB_STATE, demo->color_blend); |
| 1547 | vkDestroyObject(demo->device, VK_OBJECT_TYPE_DYNAMIC_DS_STATE, demo->depth_stencil); |
Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 1548 | |
Mike Stroyan | 230e625 | 2015-04-17 12:36:38 -0600 | [diff] [blame] | 1549 | vkDestroyObject(demo->device, VK_OBJECT_TYPE_PIPELINE, demo->pipeline); |
| 1550 | vkDestroyObject(demo->device, VK_OBJECT_TYPE_PIPELINE_LAYOUT, demo->pipeline_layout); |
| 1551 | vkDestroyObject(demo->device, VK_OBJECT_TYPE_DESCRIPTOR_SET_LAYOUT, demo->desc_layout); |
Chia-I Wu | b043fe3 | 2014-10-06 15:30:33 +0800 | [diff] [blame] | 1552 | |
Mike Stroyan | 230e625 | 2015-04-17 12:36:38 -0600 | [diff] [blame] | 1553 | vkDestroyObject(demo->device, VK_OBJECT_TYPE_BUFFER, demo->vertices.buf); |
Mark Lobodzinski | 2318261 | 2015-05-29 09:32:35 -0500 | [diff] [blame] | 1554 | vkFreeMemory(demo->device, demo->vertices.mem); |
Chia-I Wu | 99621bc | 2014-10-08 11:52:22 +0800 | [diff] [blame] | 1555 | |
Chia-I Wu | b043fe3 | 2014-10-06 15:30:33 +0800 | [diff] [blame] | 1556 | for (i = 0; i < DEMO_TEXTURE_COUNT; i++) { |
Mike Stroyan | 230e625 | 2015-04-17 12:36:38 -0600 | [diff] [blame] | 1557 | vkDestroyObject(demo->device, VK_OBJECT_TYPE_IMAGE_VIEW, demo->textures[i].view); |
Mike Stroyan | 230e625 | 2015-04-17 12:36:38 -0600 | [diff] [blame] | 1558 | vkDestroyObject(demo->device, VK_OBJECT_TYPE_IMAGE, demo->textures[i].image); |
Mark Lobodzinski | 2318261 | 2015-05-29 09:32:35 -0500 | [diff] [blame] | 1559 | vkFreeMemory(demo->device, demo->textures[i].mem); |
Mike Stroyan | 230e625 | 2015-04-17 12:36:38 -0600 | [diff] [blame] | 1560 | vkDestroyObject(demo->device, VK_OBJECT_TYPE_SAMPLER, demo->textures[i].sampler); |
Chia-I Wu | b043fe3 | 2014-10-06 15:30:33 +0800 | [diff] [blame] | 1561 | } |
| 1562 | |
Mike Stroyan | 230e625 | 2015-04-17 12:36:38 -0600 | [diff] [blame] | 1563 | vkDestroyObject(demo->device, VK_OBJECT_TYPE_DEPTH_STENCIL_VIEW, demo->depth.view); |
Mike Stroyan | 230e625 | 2015-04-17 12:36:38 -0600 | [diff] [blame] | 1564 | vkDestroyObject(demo->device, VK_OBJECT_TYPE_IMAGE, demo->depth.image); |
Mark Lobodzinski | 2318261 | 2015-05-29 09:32:35 -0500 | [diff] [blame] | 1565 | vkFreeMemory(demo->device, demo->depth.mem); |
Chia-I Wu | 9ae87c9 | 2014-10-07 14:15:01 +0800 | [diff] [blame] | 1566 | |
Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 1567 | for (i = 0; i < DEMO_BUFFER_COUNT; i++) { |
Mike Stroyan | 230e625 | 2015-04-17 12:36:38 -0600 | [diff] [blame] | 1568 | vkDestroyObject(demo->device, VK_OBJECT_TYPE_COLOR_ATTACHMENT_VIEW, demo->buffers[i].view); |
Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 1569 | } |
Jon Ashburn | cedc15f | 2015-05-21 18:13:33 -0600 | [diff] [blame] | 1570 | demo->fpDestroySwapChainWSI(demo->swap_chain); |
Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 1571 | |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1572 | vkDestroyDevice(demo->device); |
| 1573 | vkDestroyInstance(demo->inst); |
Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 1574 | |
Ian Elliott | e14e9f9 | 2015-04-16 15:23:05 -0600 | [diff] [blame] | 1575 | #ifndef _WIN32 |
Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 1576 | xcb_destroy_window(demo->connection, demo->window); |
| 1577 | xcb_disconnect(demo->connection); |
Ian Elliott | e14e9f9 | 2015-04-16 15:23:05 -0600 | [diff] [blame] | 1578 | #endif // _WIN32 |
Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 1579 | } |
| 1580 | |
Ian Elliott | e14e9f9 | 2015-04-16 15:23:05 -0600 | [diff] [blame] | 1581 | #ifdef _WIN32 |
| 1582 | int APIENTRY WinMain(HINSTANCE hInstance, |
| 1583 | HINSTANCE hPrevInstance, |
| 1584 | LPSTR pCmdLine, |
| 1585 | int nCmdShow) |
| 1586 | { |
| 1587 | MSG msg; // message |
| 1588 | bool done; // flag saying when app is complete |
| 1589 | |
| 1590 | demo_init(&demo, hInstance, pCmdLine); |
| 1591 | demo_create_window(&demo); |
| 1592 | |
| 1593 | demo_prepare(&demo); |
| 1594 | |
| 1595 | done = false; //initialize loop condition variable |
| 1596 | /* main message loop*/ |
| 1597 | while(!done) |
| 1598 | { |
Ian Elliott | 421107f | 2015-04-28 15:50:36 -0600 | [diff] [blame] | 1599 | PeekMessage(&msg, NULL, 0, 0, PM_REMOVE); |
Ian Elliott | e14e9f9 | 2015-04-16 15:23:05 -0600 | [diff] [blame] | 1600 | if (msg.message == WM_QUIT) //check for a quit message |
| 1601 | { |
| 1602 | done = true; //if found, quit app |
| 1603 | } |
| 1604 | else |
| 1605 | { |
| 1606 | /* Translate and dispatch to event queue*/ |
| 1607 | TranslateMessage(&msg); |
| 1608 | DispatchMessage(&msg); |
| 1609 | } |
| 1610 | } |
| 1611 | |
| 1612 | demo_cleanup(&demo); |
| 1613 | |
Tony Barbour | a938abb | 2015-04-22 11:36:22 -0600 | [diff] [blame] | 1614 | return (int) msg.wParam; |
Ian Elliott | e14e9f9 | 2015-04-16 15:23:05 -0600 | [diff] [blame] | 1615 | } |
| 1616 | #else // _WIN32 |
Courtney Goeltzenleuchter | f113a95 | 2015-02-25 11:46:58 -0700 | [diff] [blame] | 1617 | int main(const int argc, const char *argv[]) |
Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 1618 | { |
| 1619 | struct demo demo; |
| 1620 | |
Courtney Goeltzenleuchter | f113a95 | 2015-02-25 11:46:58 -0700 | [diff] [blame] | 1621 | demo_init(&demo, argc, argv); |
Chia-I Wu | 5b66aa5 | 2015-04-16 22:02:10 +0800 | [diff] [blame] | 1622 | demo_create_window(&demo); |
Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 1623 | |
| 1624 | demo_prepare(&demo); |
Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 1625 | demo_run(&demo); |
| 1626 | |
| 1627 | demo_cleanup(&demo); |
| 1628 | |
Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 1629 | return 0; |
| 1630 | } |
Ian Elliott | e14e9f9 | 2015-04-16 15:23:05 -0600 | [diff] [blame] | 1631 | #endif // _WIN32 |