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