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