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> |
Courtney Goeltzenleuchter | 18061cd | 2015-06-29 15:39:26 -0600 | [diff] [blame] | 45 | #include "vk_debug_report_lunarg.h" |
Ian Elliott | e36b208 | 2015-07-06 14:27:58 -0600 | [diff] [blame] | 46 | #include <vk_wsi_swapchain.h> |
| 47 | #include <vk_wsi_device_swapchain.h> |
Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 48 | |
Cody Northrop | d4e020a | 2015-03-17 14:54:35 -0600 | [diff] [blame] | 49 | #include "icd-spv.h" |
Courtney Goeltzenleuchter | 3f2606d | 2014-10-13 17:51:58 -0600 | [diff] [blame] | 50 | |
Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 51 | #define DEMO_BUFFER_COUNT 2 |
Chia-I Wu | b043fe3 | 2014-10-06 15:30:33 +0800 | [diff] [blame] | 52 | #define DEMO_TEXTURE_COUNT 1 |
Courtney Goeltzenleuchter | f5cdad0 | 2015-03-31 16:36:30 -0600 | [diff] [blame] | 53 | #define VERTEX_BUFFER_BIND_ID 0 |
Ian Elliott | 4e19ed0 | 2015-04-28 10:52:52 -0600 | [diff] [blame] | 54 | #define APP_SHORT_NAME "tri" |
| 55 | #define APP_LONG_NAME "The Vulkan Triangle Demo Program" |
Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 56 | |
Ian Elliott | e36b208 | 2015-07-06 14:27:58 -0600 | [diff] [blame] | 57 | #define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0])) |
| 58 | |
Tony Barbour | 22a3086 | 2015-04-22 09:02:32 -0600 | [diff] [blame] | 59 | #if defined(NDEBUG) && defined(__GNUC__) |
| 60 | #define U_ASSERT_ONLY __attribute__((unused)) |
| 61 | #else |
| 62 | #define U_ASSERT_ONLY |
| 63 | #endif |
| 64 | |
Ian Elliott | caa9f27 | 2015-04-28 11:35:02 -0600 | [diff] [blame] | 65 | #ifdef _WIN32 |
| 66 | #define ERR_EXIT(err_msg, err_class) \ |
| 67 | do { \ |
| 68 | MessageBox(NULL, err_msg, err_class, MB_OK); \ |
| 69 | exit(1); \ |
| 70 | } while (0) |
| 71 | |
| 72 | // NOTE: If the following values (copied from "loader_platform.h") change, they |
| 73 | // need to change here as well: |
| 74 | #define LAYER_NAMES_ENV "VK_LAYER_NAMES" |
| 75 | #define LAYER_NAMES_REGISTRY_VALUE "VK_LAYER_NAMES" |
| 76 | #else // _WIN32 |
| 77 | |
| 78 | #define ERR_EXIT(err_msg, err_class) \ |
| 79 | do { \ |
| 80 | printf(err_msg); \ |
| 81 | fflush(stdout); \ |
| 82 | exit(1); \ |
| 83 | } while (0) |
| 84 | #endif // _WIN32 |
Tony Barbour | 22a3086 | 2015-04-22 09:02:32 -0600 | [diff] [blame] | 85 | |
Ian Elliott | e36b208 | 2015-07-06 14:27:58 -0600 | [diff] [blame] | 86 | #define GET_INSTANCE_PROC_ADDR(inst, entrypoint) \ |
| 87 | { \ |
| 88 | demo->fp##entrypoint = (PFN_vk##entrypoint) vkGetInstanceProcAddr(inst, "vk"#entrypoint); \ |
| 89 | if (demo->fp##entrypoint == NULL) { \ |
| 90 | ERR_EXIT("vkGetInstanceProcAddr failed to find vk"#entrypoint, \ |
| 91 | "vkGetInstanceProcAddr Failure"); \ |
| 92 | } \ |
| 93 | } |
| 94 | |
Ian Elliott | 1b6de09 | 2015-06-22 15:07:49 -0600 | [diff] [blame] | 95 | #define GET_DEVICE_PROC_ADDR(dev, entrypoint) \ |
| 96 | { \ |
Courtney Goeltzenleuchter | a4c8c71 | 2015-07-12 14:35:22 -0600 | [diff] [blame] | 97 | demo->fp##entrypoint = (PFN_vk##entrypoint) vkGetDeviceProcAddr(dev, "vk"#entrypoint); \ |
Ian Elliott | 1b6de09 | 2015-06-22 15:07:49 -0600 | [diff] [blame] | 98 | if (demo->fp##entrypoint == NULL) { \ |
| 99 | ERR_EXIT("vkGetDeviceProcAddr failed to find vk"#entrypoint, \ |
| 100 | "vkGetDeviceProcAddr Failure"); \ |
| 101 | } \ |
| 102 | } |
| 103 | |
Courtney Goeltzenleuchter | bfccf41 | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 104 | struct texture_object { |
Courtney Goeltzenleuchter | 382489d | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 105 | VkSampler sampler; |
Courtney Goeltzenleuchter | 372e13c | 2015-02-13 17:52:46 -0700 | [diff] [blame] | 106 | |
Courtney Goeltzenleuchter | 382489d | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 107 | VkImage image; |
| 108 | VkImageLayout imageLayout; |
Courtney Goeltzenleuchter | bfccf41 | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 109 | |
Mark Lobodzinski | 2318261 | 2015-05-29 09:32:35 -0500 | [diff] [blame] | 110 | VkDeviceMemory mem; |
Courtney Goeltzenleuchter | 382489d | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 111 | VkImageView view; |
Courtney Goeltzenleuchter | 372e13c | 2015-02-13 17:52:46 -0700 | [diff] [blame] | 112 | int32_t tex_width, tex_height; |
| 113 | }; |
| 114 | |
Courtney Goeltzenleuchter | 18061cd | 2015-06-29 15:39:26 -0600 | [diff] [blame] | 115 | void dbgFunc( |
Tony Barbour | de4124d | 2015-07-03 10:33:54 -0600 | [diff] [blame] | 116 | VkFlags msgFlags, |
| 117 | VkDbgObjectType objType, |
| 118 | uint64_t srcObject, |
Courtney Goeltzenleuchter | 18061cd | 2015-06-29 15:39:26 -0600 | [diff] [blame] | 119 | size_t location, |
| 120 | int32_t msgCode, |
| 121 | const char* pLayerPrefix, |
| 122 | const char* pMsg, |
| 123 | void* pUserData) |
| 124 | { |
| 125 | char *message = (char *) malloc(strlen(pMsg)+100); |
| 126 | |
| 127 | assert (message); |
| 128 | |
| 129 | if (msgFlags & VK_DBG_REPORT_ERROR_BIT) { |
| 130 | sprintf(message,"ERROR: [%s] Code %d : %s", pLayerPrefix, msgCode, pMsg); |
| 131 | } else if (msgFlags & VK_DBG_REPORT_WARN_BIT) { |
| 132 | sprintf(message,"WARNING: [%s] Code %d : %s", pLayerPrefix, msgCode, pMsg); |
| 133 | } else { |
| 134 | return; |
| 135 | } |
| 136 | |
| 137 | #ifdef _WIN32 |
| 138 | MessageBox(NULL, message, "Alert", MB_OK); |
| 139 | #else |
| 140 | printf("%s\n",message); |
| 141 | fflush(stdout); |
| 142 | #endif |
| 143 | free(message); |
| 144 | } |
| 145 | |
Ian Elliott | e36b208 | 2015-07-06 14:27:58 -0600 | [diff] [blame] | 146 | typedef struct _SwapChainBuffers { |
| 147 | VkImage image; |
| 148 | VkCmdBuffer cmd; |
| 149 | VkAttachmentView view; |
| 150 | } SwapChainBuffers; |
| 151 | |
Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 152 | struct demo { |
Ian Elliott | e14e9f9 | 2015-04-16 15:23:05 -0600 | [diff] [blame] | 153 | #ifdef _WIN32 |
| 154 | #define APP_NAME_STR_LEN 80 |
| 155 | HINSTANCE connection; // hInstance - Windows Instance |
| 156 | char name[APP_NAME_STR_LEN]; // Name to put on the window/icon |
| 157 | HWND window; // hWnd - window handle |
| 158 | #else // _WIN32 |
Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 159 | xcb_connection_t *connection; |
| 160 | xcb_screen_t *screen; |
Chia-I Wu | 5b66aa5 | 2015-04-16 22:02:10 +0800 | [diff] [blame] | 161 | xcb_window_t window; |
| 162 | xcb_intern_atom_reply_t *atom_wm_delete_window; |
Ian Elliott | e36b208 | 2015-07-06 14:27:58 -0600 | [diff] [blame] | 163 | VkPlatformHandleXcbWSI platform_handle_xcb; |
Ian Elliott | e14e9f9 | 2015-04-16 15:23:05 -0600 | [diff] [blame] | 164 | #endif // _WIN32 |
Jon Ashburn | 8a399e9 | 2015-04-24 09:46:24 -0700 | [diff] [blame] | 165 | bool prepared; |
Ian Elliott | e14e9f9 | 2015-04-16 15:23:05 -0600 | [diff] [blame] | 166 | bool use_staging_buffer; |
Cody Northrop | 75db032 | 2015-05-28 11:27:16 -0600 | [diff] [blame] | 167 | bool use_glsl; |
Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 168 | |
Courtney Goeltzenleuchter | 382489d | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 169 | VkInstance inst; |
Tony Barbour | 8205d90 | 2015-04-16 15:59:00 -0600 | [diff] [blame] | 170 | VkPhysicalDevice gpu; |
Courtney Goeltzenleuchter | 382489d | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 171 | VkDevice device; |
| 172 | VkQueue queue; |
Tony Barbour | 426b905 | 2015-06-24 16:06:58 -0600 | [diff] [blame] | 173 | VkPhysicalDeviceProperties gpu_props; |
Tony Barbour | 8205d90 | 2015-04-16 15:59:00 -0600 | [diff] [blame] | 174 | VkPhysicalDeviceQueueProperties *queue_props; |
Courtney Goeltzenleuchter | f316806 | 2015-03-05 18:09:39 -0700 | [diff] [blame] | 175 | uint32_t graphics_queue_node_index; |
Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 176 | |
| 177 | int width, height; |
Courtney Goeltzenleuchter | 382489d | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 178 | VkFormat format; |
Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 179 | |
Ian Elliott | e36b208 | 2015-07-06 14:27:58 -0600 | [diff] [blame] | 180 | PFN_vkGetPhysicalDeviceSurfaceSupportWSI fpGetPhysicalDeviceSurfaceSupportWSI; |
| 181 | PFN_vkGetSurfaceInfoWSI fpGetSurfaceInfoWSI; |
Jon Ashburn | cedc15f | 2015-05-21 18:13:33 -0600 | [diff] [blame] | 182 | PFN_vkCreateSwapChainWSI fpCreateSwapChainWSI; |
| 183 | PFN_vkDestroySwapChainWSI fpDestroySwapChainWSI; |
| 184 | PFN_vkGetSwapChainInfoWSI fpGetSwapChainInfoWSI; |
Ian Elliott | e36b208 | 2015-07-06 14:27:58 -0600 | [diff] [blame] | 185 | PFN_vkAcquireNextImageWSI fpAcquireNextImageWSI; |
Jon Ashburn | cedc15f | 2015-05-21 18:13:33 -0600 | [diff] [blame] | 186 | PFN_vkQueuePresentWSI fpQueuePresentWSI; |
Ian Elliott | e36b208 | 2015-07-06 14:27:58 -0600 | [diff] [blame] | 187 | VkSurfaceDescriptionWindowWSI surface_description; |
| 188 | size_t swapChainImageCount; |
Chia-I Wu | 5b66aa5 | 2015-04-16 22:02:10 +0800 | [diff] [blame] | 189 | VkSwapChainWSI swap_chain; |
Ian Elliott | e36b208 | 2015-07-06 14:27:58 -0600 | [diff] [blame] | 190 | SwapChainBuffers *buffers; |
Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 191 | |
Ian Elliott | e36b208 | 2015-07-06 14:27:58 -0600 | [diff] [blame] | 192 | VkCmdPool cmd_pool; |
Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 193 | |
Chia-I Wu | b043fe3 | 2014-10-06 15:30:33 +0800 | [diff] [blame] | 194 | struct { |
Courtney Goeltzenleuchter | 382489d | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 195 | VkFormat format; |
Chia-I Wu | 9ae87c9 | 2014-10-07 14:15:01 +0800 | [diff] [blame] | 196 | |
Courtney Goeltzenleuchter | 382489d | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 197 | VkImage image; |
Mark Lobodzinski | 2318261 | 2015-05-29 09:32:35 -0500 | [diff] [blame] | 198 | VkDeviceMemory mem; |
Chia-I Wu | c278df8 | 2015-07-07 11:50:03 +0800 | [diff] [blame] | 199 | VkAttachmentView view; |
Chia-I Wu | 9ae87c9 | 2014-10-07 14:15:01 +0800 | [diff] [blame] | 200 | } depth; |
| 201 | |
Courtney Goeltzenleuchter | bfccf41 | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 202 | struct texture_object textures[DEMO_TEXTURE_COUNT]; |
Chia-I Wu | b043fe3 | 2014-10-06 15:30:33 +0800 | [diff] [blame] | 203 | |
Chia-I Wu | 99621bc | 2014-10-08 11:52:22 +0800 | [diff] [blame] | 204 | struct { |
Courtney Goeltzenleuchter | 382489d | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 205 | VkBuffer buf; |
Mark Lobodzinski | 2318261 | 2015-05-29 09:32:35 -0500 | [diff] [blame] | 206 | VkDeviceMemory mem; |
Chia-I Wu | 8d29d02 | 2014-10-08 12:14:39 +0800 | [diff] [blame] | 207 | |
Mark Lobodzinski | 0e0fb5c | 2015-06-23 15:11:57 -0600 | [diff] [blame] | 208 | VkPipelineVertexInputStateCreateInfo vi; |
Courtney Goeltzenleuchter | 382489d | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 209 | VkVertexInputBindingDescription vi_bindings[1]; |
| 210 | VkVertexInputAttributeDescription vi_attrs[2]; |
Chia-I Wu | 99621bc | 2014-10-08 11:52:22 +0800 | [diff] [blame] | 211 | } vertices; |
| 212 | |
Courtney Goeltzenleuchter | 633f9c5 | 2015-04-30 10:58:33 -0600 | [diff] [blame] | 213 | VkCmdBuffer setup_cmd; // Command Buffer for initialization commands |
| 214 | VkCmdBuffer draw_cmd; // Command Buffer for drawing commands |
Mark Lobodzinski | 556f721 | 2015-04-17 14:11:39 -0500 | [diff] [blame] | 215 | VkPipelineLayout pipeline_layout; |
Courtney Goeltzenleuchter | 382489d | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 216 | VkDescriptorSetLayout desc_layout; |
Jon Ashburn | 0d60d27 | 2015-07-09 15:02:25 -0600 | [diff] [blame] | 217 | VkPipelineCache pipelineCache; |
Chia-I Wu | 76cd422 | 2015-07-08 13:34:24 +0800 | [diff] [blame] | 218 | VkRenderPass render_pass; |
Courtney Goeltzenleuchter | 382489d | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 219 | VkPipeline pipeline; |
Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 220 | |
Tony Barbour | de4124d | 2015-07-03 10:33:54 -0600 | [diff] [blame] | 221 | VkDynamicViewportState viewport; |
| 222 | VkDynamicRasterState raster; |
| 223 | VkDynamicColorBlendState color_blend; |
| 224 | VkDynamicDepthStencilState depth_stencil; |
Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 225 | |
Courtney Goeltzenleuchter | 382489d | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 226 | VkDescriptorPool desc_pool; |
| 227 | VkDescriptorSet desc_set; |
Chia-I Wu | f838506 | 2015-01-04 16:27:24 +0800 | [diff] [blame] | 228 | |
Chia-I Wu | 76cd422 | 2015-07-08 13:34:24 +0800 | [diff] [blame] | 229 | VkFramebuffer framebuffers[DEMO_BUFFER_COUNT]; |
| 230 | |
Mark Lobodzinski | 7234629 | 2015-07-02 16:49:40 -0600 | [diff] [blame] | 231 | VkPhysicalDeviceMemoryProperties memory_properties; |
| 232 | |
Courtney Goeltzenleuchter | 18061cd | 2015-06-29 15:39:26 -0600 | [diff] [blame] | 233 | bool validate; |
| 234 | PFN_vkDbgCreateMsgCallback dbgCreateMsgCallback; |
Ian Elliott | e36b208 | 2015-07-06 14:27:58 -0600 | [diff] [blame] | 235 | PFN_vkDbgDestroyMsgCallback dbgDestroyMsgCallback; |
Courtney Goeltzenleuchter | 18061cd | 2015-06-29 15:39:26 -0600 | [diff] [blame] | 236 | VkDbgMsgCallback msg_callback; |
| 237 | |
Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 238 | bool quit; |
Mark Lobodzinski | e2d07a5 | 2015-01-29 08:55:56 -0600 | [diff] [blame] | 239 | uint32_t current_buffer; |
Piers Daniell | 1cf7fe1 | 2015-07-16 09:35:35 -0600 | [diff] [blame^] | 240 | uint32_t queue_count; |
Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 241 | }; |
| 242 | |
Mark Lobodzinski | 7234629 | 2015-07-02 16:49:40 -0600 | [diff] [blame] | 243 | static VkResult memory_type_from_properties(struct demo *demo, uint32_t typeBits, VkFlags properties, uint32_t *typeIndex) |
| 244 | { |
| 245 | // Search memtypes to find first index with those properties |
| 246 | for (uint32_t i = 0; i < 32; i++) { |
| 247 | if ((typeBits & 1) == 1) { |
| 248 | // Type is available, does it match user properties? |
| 249 | if ((demo->memory_properties.memoryTypes[i].propertyFlags & properties) == properties) { |
| 250 | *typeIndex = i; |
| 251 | return VK_SUCCESS; |
| 252 | } |
| 253 | } |
| 254 | typeBits >>= 1; |
| 255 | } |
| 256 | // No memory types matched, return failure |
| 257 | return VK_UNSUPPORTED; |
| 258 | } |
| 259 | |
Courtney Goeltzenleuchter | bfccf41 | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 260 | static void demo_flush_init_cmd(struct demo *demo) |
| 261 | { |
Tony Barbour | 22a3086 | 2015-04-22 09:02:32 -0600 | [diff] [blame] | 262 | VkResult U_ASSERT_ONLY err; |
Courtney Goeltzenleuchter | bfccf41 | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 263 | |
Courtney Goeltzenleuchter | 633f9c5 | 2015-04-30 10:58:33 -0600 | [diff] [blame] | 264 | if (demo->setup_cmd == VK_NULL_HANDLE) |
Courtney Goeltzenleuchter | bfccf41 | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 265 | return; |
| 266 | |
Courtney Goeltzenleuchter | 633f9c5 | 2015-04-30 10:58:33 -0600 | [diff] [blame] | 267 | err = vkEndCommandBuffer(demo->setup_cmd); |
Courtney Goeltzenleuchter | bfccf41 | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 268 | assert(!err); |
| 269 | |
Courtney Goeltzenleuchter | 633f9c5 | 2015-04-30 10:58:33 -0600 | [diff] [blame] | 270 | const VkCmdBuffer cmd_bufs[] = { demo->setup_cmd }; |
Tony Barbour | de4124d | 2015-07-03 10:33:54 -0600 | [diff] [blame] | 271 | VkFence nullFence = {VK_NULL_HANDLE}; |
Courtney Goeltzenleuchter | bfccf41 | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 272 | |
Tony Barbour | de4124d | 2015-07-03 10:33:54 -0600 | [diff] [blame] | 273 | err = vkQueueSubmit(demo->queue, 1, cmd_bufs, nullFence); |
Courtney Goeltzenleuchter | bfccf41 | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 274 | assert(!err); |
| 275 | |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 276 | err = vkQueueWaitIdle(demo->queue); |
Courtney Goeltzenleuchter | bfccf41 | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 277 | assert(!err); |
| 278 | |
Tony Barbour | de4124d | 2015-07-03 10:33:54 -0600 | [diff] [blame] | 279 | vkDestroyCommandBuffer(demo->device, demo->setup_cmd); |
Courtney Goeltzenleuchter | 633f9c5 | 2015-04-30 10:58:33 -0600 | [diff] [blame] | 280 | demo->setup_cmd = VK_NULL_HANDLE; |
Courtney Goeltzenleuchter | bfccf41 | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 281 | } |
| 282 | |
Courtney Goeltzenleuchter | bfccf41 | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 283 | static void demo_set_image_layout( |
| 284 | struct demo *demo, |
Courtney Goeltzenleuchter | 382489d | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 285 | VkImage image, |
malnasse | 4b8ba4d | 2015-06-03 17:28:38 -0400 | [diff] [blame] | 286 | VkImageAspect aspect, |
Courtney Goeltzenleuchter | 382489d | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 287 | VkImageLayout old_image_layout, |
| 288 | VkImageLayout new_image_layout) |
Courtney Goeltzenleuchter | bfccf41 | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 289 | { |
Tony Barbour | 22a3086 | 2015-04-22 09:02:32 -0600 | [diff] [blame] | 290 | VkResult U_ASSERT_ONLY err; |
Courtney Goeltzenleuchter | bfccf41 | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 291 | |
Courtney Goeltzenleuchter | 633f9c5 | 2015-04-30 10:58:33 -0600 | [diff] [blame] | 292 | if (demo->setup_cmd == VK_NULL_HANDLE) { |
Courtney Goeltzenleuchter | 382489d | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 293 | const VkCmdBufferCreateInfo cmd = { |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 294 | .sType = VK_STRUCTURE_TYPE_CMD_BUFFER_CREATE_INFO, |
Courtney Goeltzenleuchter | bfccf41 | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 295 | .pNext = NULL, |
Cody Northrop | 18ea11b | 2015-07-09 18:08:32 -0600 | [diff] [blame] | 296 | .cmdPool = demo->cmd_pool, |
Chia-I Wu | 88eaa3b | 2015-06-26 15:34:39 +0800 | [diff] [blame] | 297 | .level = VK_CMD_BUFFER_LEVEL_PRIMARY, |
Courtney Goeltzenleuchter | bfccf41 | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 298 | .flags = 0, |
| 299 | }; |
| 300 | |
Courtney Goeltzenleuchter | 633f9c5 | 2015-04-30 10:58:33 -0600 | [diff] [blame] | 301 | err = vkCreateCommandBuffer(demo->device, &cmd, &demo->setup_cmd); |
Courtney Goeltzenleuchter | bfccf41 | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 302 | assert(!err); |
| 303 | |
Courtney Goeltzenleuchter | 382489d | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 304 | VkCmdBufferBeginInfo cmd_buf_info = { |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 305 | .sType = VK_STRUCTURE_TYPE_CMD_BUFFER_BEGIN_INFO, |
Courtney Goeltzenleuchter | bfccf41 | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 306 | .pNext = NULL, |
Tony Barbour | 8205d90 | 2015-04-16 15:59:00 -0600 | [diff] [blame] | 307 | .flags = VK_CMD_BUFFER_OPTIMIZE_SMALL_BATCH_BIT | |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 308 | VK_CMD_BUFFER_OPTIMIZE_ONE_TIME_SUBMIT_BIT, |
Courtney Goeltzenleuchter | bfccf41 | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 309 | }; |
Courtney Goeltzenleuchter | 633f9c5 | 2015-04-30 10:58:33 -0600 | [diff] [blame] | 310 | err = vkBeginCommandBuffer(demo->setup_cmd, &cmd_buf_info); |
Courtney Goeltzenleuchter | bfccf41 | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 311 | } |
| 312 | |
Courtney Goeltzenleuchter | 382489d | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 313 | VkImageMemoryBarrier image_memory_barrier = { |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 314 | .sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER, |
Courtney Goeltzenleuchter | bfccf41 | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 315 | .pNext = NULL, |
| 316 | .outputMask = 0, |
| 317 | .inputMask = 0, |
| 318 | .oldLayout = old_image_layout, |
| 319 | .newLayout = new_image_layout, |
| 320 | .image = image, |
malnasse | 4b8ba4d | 2015-06-03 17:28:38 -0400 | [diff] [blame] | 321 | .subresourceRange = { aspect, 0, 1, 0, 0 } |
Courtney Goeltzenleuchter | bfccf41 | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 322 | }; |
| 323 | |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 324 | if (new_image_layout == VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL) { |
Courtney Goeltzenleuchter | bfccf41 | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 325 | /* Make sure anything that was copying from this image has completed */ |
Courtney Goeltzenleuchter | ad87081 | 2015-04-15 15:29:59 -0600 | [diff] [blame] | 326 | image_memory_barrier.inputMask = VK_MEMORY_INPUT_TRANSFER_BIT; |
Courtney Goeltzenleuchter | bfccf41 | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 327 | } |
| 328 | |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 329 | if (new_image_layout == VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL) { |
Courtney Goeltzenleuchter | bfccf41 | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 330 | /* Make sure any Copy or CPU writes to image are flushed */ |
Courtney Goeltzenleuchter | a569a50 | 2015-04-29 17:16:21 -0600 | [diff] [blame] | 331 | 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] | 332 | } |
| 333 | |
Courtney Goeltzenleuchter | 382489d | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 334 | VkImageMemoryBarrier *pmemory_barrier = &image_memory_barrier; |
Courtney Goeltzenleuchter | bfccf41 | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 335 | |
Tony Barbour | c2e987e | 2015-06-29 16:20:35 -0600 | [diff] [blame] | 336 | VkPipelineStageFlags src_stages = VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT; |
| 337 | VkPipelineStageFlags dest_stages = VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT; |
Courtney Goeltzenleuchter | bfccf41 | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 338 | |
Courtney Goeltzenleuchter | 82b348f | 2015-07-12 13:07:46 -0600 | [diff] [blame] | 339 | vkCmdPipelineBarrier(demo->setup_cmd, src_stages, dest_stages, false, 1, (const void * const*)&pmemory_barrier); |
Courtney Goeltzenleuchter | bfccf41 | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 340 | } |
| 341 | |
Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 342 | static void demo_draw_build_cmd(struct demo *demo) |
| 343 | { |
Chia-I Wu | 76cd422 | 2015-07-08 13:34:24 +0800 | [diff] [blame] | 344 | const VkCmdBufferBeginInfo cmd_buf_info = { |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 345 | .sType = VK_STRUCTURE_TYPE_CMD_BUFFER_BEGIN_INFO, |
Courtney Goeltzenleuchter | e3b0f3a | 2015-04-03 15:25:24 -0600 | [diff] [blame] | 346 | .pNext = NULL, |
Tony Barbour | 8205d90 | 2015-04-16 15:59:00 -0600 | [diff] [blame] | 347 | .flags = VK_CMD_BUFFER_OPTIMIZE_SMALL_BATCH_BIT | |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 348 | VK_CMD_BUFFER_OPTIMIZE_ONE_TIME_SUBMIT_BIT, |
Jon Ashburn | 53d27af | 2014-12-31 17:08:35 -0700 | [diff] [blame] | 349 | }; |
Chia-I Wu | c278df8 | 2015-07-07 11:50:03 +0800 | [diff] [blame] | 350 | const VkClearValue clear_values[2] = { |
| 351 | [0] = { .color.f32 = { 0.2f, 0.2f, 0.2f, 0.2f } }, |
| 352 | [1] = { .ds = { 0.9f, 0 } }, |
| 353 | }; |
| 354 | const VkRenderPassBeginInfo rp_begin = { |
| 355 | .sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO, |
| 356 | .pNext = NULL, |
Chia-I Wu | 76cd422 | 2015-07-08 13:34:24 +0800 | [diff] [blame] | 357 | .renderPass = demo->render_pass, |
| 358 | .framebuffer = demo->framebuffers[demo->current_buffer], |
Chia-I Wu | c278df8 | 2015-07-07 11:50:03 +0800 | [diff] [blame] | 359 | .renderArea.offset.x = 0, |
| 360 | .renderArea.offset.y = 0, |
| 361 | .renderArea.extent.width = demo->width, |
| 362 | .renderArea.extent.height = demo->height, |
| 363 | .attachmentCount = 2, |
| 364 | .pAttachmentClearValues = clear_values, |
Jon Ashburn | 3325d6b | 2015-01-02 18:24:05 -0700 | [diff] [blame] | 365 | }; |
Chia-I Wu | 76cd422 | 2015-07-08 13:34:24 +0800 | [diff] [blame] | 366 | VkResult U_ASSERT_ONLY err; |
Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 367 | |
Courtney Goeltzenleuchter | 633f9c5 | 2015-04-30 10:58:33 -0600 | [diff] [blame] | 368 | err = vkBeginCommandBuffer(demo->draw_cmd, &cmd_buf_info); |
Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 369 | assert(!err); |
| 370 | |
Chia-I Wu | c278df8 | 2015-07-07 11:50:03 +0800 | [diff] [blame] | 371 | vkCmdBeginRenderPass(demo->draw_cmd, &rp_begin, VK_RENDER_PASS_CONTENTS_INLINE); |
Courtney Goeltzenleuchter | 633f9c5 | 2015-04-30 10:58:33 -0600 | [diff] [blame] | 372 | vkCmdBindPipeline(demo->draw_cmd, VK_PIPELINE_BIND_POINT_GRAPHICS, |
Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 373 | demo->pipeline); |
Mark Lobodzinski | a65c463 | 2015-06-15 13:21:21 -0600 | [diff] [blame] | 374 | vkCmdBindDescriptorSets(demo->draw_cmd, VK_PIPELINE_BIND_POINT_GRAPHICS, demo->pipeline_layout, |
Cody Northrop | 1a01b1d | 2015-04-16 13:41:56 -0600 | [diff] [blame] | 375 | 0, 1, & demo->desc_set, 0, NULL); |
Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 376 | |
Tony Barbour | de4124d | 2015-07-03 10:33:54 -0600 | [diff] [blame] | 377 | vkCmdBindDynamicViewportState(demo->draw_cmd, demo->viewport); |
| 378 | vkCmdBindDynamicRasterState(demo->draw_cmd, demo->raster); |
| 379 | vkCmdBindDynamicColorBlendState(demo->draw_cmd, demo->color_blend); |
| 380 | vkCmdBindDynamicDepthStencilState(demo->draw_cmd, demo->depth_stencil); |
Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 381 | |
Tony Barbour | 8205d90 | 2015-04-16 15:59:00 -0600 | [diff] [blame] | 382 | VkDeviceSize offsets[1] = {0}; |
Courtney Goeltzenleuchter | 633f9c5 | 2015-04-30 10:58:33 -0600 | [diff] [blame] | 383 | 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] | 384 | |
Courtney Goeltzenleuchter | 633f9c5 | 2015-04-30 10:58:33 -0600 | [diff] [blame] | 385 | vkCmdDraw(demo->draw_cmd, 0, 3, 0, 1); |
Chia-I Wu | 88eaa3b | 2015-06-26 15:34:39 +0800 | [diff] [blame] | 386 | vkCmdEndRenderPass(demo->draw_cmd); |
Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 387 | |
Courtney Goeltzenleuchter | 633f9c5 | 2015-04-30 10:58:33 -0600 | [diff] [blame] | 388 | err = vkEndCommandBuffer(demo->draw_cmd); |
Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 389 | assert(!err); |
| 390 | } |
| 391 | |
| 392 | static void demo_draw(struct demo *demo) |
| 393 | { |
Tony Barbour | 22a3086 | 2015-04-22 09:02:32 -0600 | [diff] [blame] | 394 | VkResult U_ASSERT_ONLY err; |
Ian Elliott | e36b208 | 2015-07-06 14:27:58 -0600 | [diff] [blame] | 395 | VkSemaphore presentCompleteSemaphore; |
| 396 | VkSemaphoreCreateInfo presentCompleteSemaphoreCreateInfo = { |
| 397 | .sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO, |
| 398 | .pNext = NULL, |
| 399 | .flags = VK_FENCE_CREATE_SIGNALED_BIT, |
| 400 | }; |
Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 401 | |
Ian Elliott | e36b208 | 2015-07-06 14:27:58 -0600 | [diff] [blame] | 402 | err = vkCreateSemaphore(demo->device, |
| 403 | &presentCompleteSemaphoreCreateInfo, |
| 404 | &presentCompleteSemaphore); |
| 405 | assert(!err); |
| 406 | |
| 407 | // Get the index of the next available swapchain image: |
| 408 | err = demo->fpAcquireNextImageWSI(demo->device, demo->swap_chain, |
| 409 | UINT64_MAX, |
| 410 | presentCompleteSemaphore, |
| 411 | &demo->current_buffer); |
| 412 | // TODO: Deal with the VK_SUBOPTIMAL_WSI and VK_ERROR_OUT_OF_DATE_WSI |
| 413 | // return codes |
| 414 | assert(!err); |
| 415 | |
| 416 | // Wait for the present complete semaphore to be signaled to ensure |
| 417 | // that the image won't be rendered to until the presentation |
| 418 | // engine has fully released ownership to the application, and it is |
| 419 | // okay to render to the image. |
| 420 | vkQueueWaitSemaphore(demo->queue, presentCompleteSemaphore); |
| 421 | |
| 422 | // FIXME/TODO: DEAL WITH VK_IMAGE_LAYOUT_PRESENT_SOURCE_WSI |
Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 423 | demo_draw_build_cmd(demo); |
Tony Barbour | de4124d | 2015-07-03 10:33:54 -0600 | [diff] [blame] | 424 | VkFence nullFence = { VK_NULL_HANDLE }; |
Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 425 | |
Tony Barbour | de4124d | 2015-07-03 10:33:54 -0600 | [diff] [blame] | 426 | err = vkQueueSubmit(demo->queue, 1, &demo->draw_cmd, nullFence); |
Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 427 | assert(!err); |
| 428 | |
Ian Elliott | e36b208 | 2015-07-06 14:27:58 -0600 | [diff] [blame] | 429 | VkPresentInfoWSI present = { |
| 430 | .sType = VK_STRUCTURE_TYPE_QUEUE_PRESENT_INFO_WSI, |
| 431 | .pNext = NULL, |
| 432 | .swapChainCount = 1, |
| 433 | .swapChains = &demo->swap_chain, |
| 434 | .imageIndices = &demo->current_buffer, |
| 435 | }; |
| 436 | |
| 437 | // TBD/TODO: SHOULD THE "present" PARAMETER BE "const" IN THE HEADER? |
Jon Ashburn | cedc15f | 2015-05-21 18:13:33 -0600 | [diff] [blame] | 438 | err = demo->fpQueuePresentWSI(demo->queue, &present); |
Ian Elliott | e36b208 | 2015-07-06 14:27:58 -0600 | [diff] [blame] | 439 | // TODO: Deal with the VK_SUBOPTIMAL_WSI and VK_ERROR_OUT_OF_DATE_WSI |
| 440 | // return codes |
Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 441 | assert(!err); |
| 442 | |
Ian Elliott | e36b208 | 2015-07-06 14:27:58 -0600 | [diff] [blame] | 443 | // FIXME: UNCOMMENT THE FOLLOWING LINE ONCE WE HAVE A NEW-ENOUGH "vulkan.h" HEADER: |
| 444 | // err = vkDestroySemaphore(demo->device, presentCompleteSemaphore); |
| 445 | assert(!err); |
Chia-I Wu | 5b66aa5 | 2015-04-16 22:02:10 +0800 | [diff] [blame] | 446 | |
| 447 | err = vkQueueWaitIdle(demo->queue); |
| 448 | assert(err == VK_SUCCESS); |
Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 449 | } |
| 450 | |
| 451 | static void demo_prepare_buffers(struct demo *demo) |
| 452 | { |
Ian Elliott | e36b208 | 2015-07-06 14:27:58 -0600 | [diff] [blame] | 453 | VkResult U_ASSERT_ONLY err; |
| 454 | |
| 455 | // Check the surface proprties and formats |
| 456 | size_t capsSize; |
| 457 | size_t presentModesSize; |
| 458 | err = demo->fpGetSurfaceInfoWSI(demo->device, |
| 459 | (const VkSurfaceDescriptionWSI *)&demo->surface_description, |
| 460 | VK_SURFACE_INFO_TYPE_PROPERTIES_WSI, &capsSize, NULL); |
| 461 | assert(!err); |
| 462 | err = demo->fpGetSurfaceInfoWSI(demo->device, |
| 463 | (const VkSurfaceDescriptionWSI *)&demo->surface_description, |
| 464 | VK_SURFACE_INFO_TYPE_PRESENT_MODES_WSI, &presentModesSize, NULL); |
| 465 | assert(!err); |
| 466 | |
| 467 | VkSurfacePropertiesWSI *surfProperties = |
| 468 | (VkSurfacePropertiesWSI *)malloc(capsSize); |
| 469 | VkSurfacePresentModePropertiesWSI *presentModes = |
| 470 | (VkSurfacePresentModePropertiesWSI *)malloc(presentModesSize); |
| 471 | |
| 472 | err = demo->fpGetSurfaceInfoWSI(demo->device, |
| 473 | (const VkSurfaceDescriptionWSI *)&demo->surface_description, |
| 474 | VK_SURFACE_INFO_TYPE_PROPERTIES_WSI, &capsSize, surfProperties); |
| 475 | assert(!err); |
| 476 | err = demo->fpGetSurfaceInfoWSI(demo->device, |
| 477 | (const VkSurfaceDescriptionWSI *)&demo->surface_description, |
| 478 | VK_SURFACE_INFO_TYPE_PRESENT_MODES_WSI, &presentModesSize, presentModes); |
| 479 | assert(!err); |
| 480 | |
| 481 | VkExtent2D swapChainExtent; |
| 482 | // width and height are either both -1, or both not -1. |
| 483 | if (surfProperties->currentExtent.width == -1) |
| 484 | { |
| 485 | // If the surface size is undefined, the size is set to |
| 486 | // the size of the images requested. |
| 487 | swapChainExtent.width = demo->width; |
| 488 | swapChainExtent.height = demo->height; |
| 489 | } |
| 490 | else |
| 491 | { |
| 492 | // If the surface size is defined, the swap chain size must match |
| 493 | swapChainExtent = surfProperties->currentExtent; |
| 494 | } |
| 495 | |
| 496 | // If mailbox mode is available, use it, as is the lowest-latency non- |
| 497 | // tearing mode. If not, fall back to IMMEDIATE which should always be |
| 498 | // available. |
| 499 | VkPresentModeWSI swapChainPresentMode = VK_PRESENT_MODE_IMMEDIATE_WSI; |
| 500 | size_t presentModeCount = presentModesSize / sizeof(VkSurfacePresentModePropertiesWSI); |
| 501 | for (size_t i = 0; i < presentModeCount; i++) { |
| 502 | if (presentModes[i].presentMode == VK_PRESENT_MODE_MAILBOX_WSI) { |
| 503 | swapChainPresentMode = VK_PRESENT_MODE_MAILBOX_WSI; |
| 504 | break; |
| 505 | } |
| 506 | } |
| 507 | |
| 508 | // Determine the number of VkImage's to use in the swap chain (we desire to |
| 509 | // own only 1 image at a time, besides the images being displayed and |
| 510 | // queued for display): |
| 511 | uint32_t desiredNumberOfSwapChainImages = surfProperties->minImageCount + 1; |
| 512 | if ((surfProperties->maxImageCount > 0) && |
| 513 | (desiredNumberOfSwapChainImages > surfProperties->maxImageCount)) |
| 514 | { |
| 515 | // Application must settle for fewer images than desired: |
| 516 | desiredNumberOfSwapChainImages = surfProperties->maxImageCount; |
| 517 | } |
| 518 | |
| 519 | VkSurfaceTransformFlagBitsWSI preTransform; |
| 520 | if (surfProperties->supportedTransforms & VK_SURFACE_TRANSFORM_NONE_BIT_WSI) { |
| 521 | preTransform = VK_SURFACE_TRANSFORM_NONE_WSI; |
| 522 | } else { |
| 523 | preTransform = surfProperties->currentTransform; |
| 524 | } |
| 525 | |
Chia-I Wu | 5b66aa5 | 2015-04-16 22:02:10 +0800 | [diff] [blame] | 526 | const VkSwapChainCreateInfoWSI swap_chain = { |
| 527 | .sType = VK_STRUCTURE_TYPE_SWAP_CHAIN_CREATE_INFO_WSI, |
| 528 | .pNext = NULL, |
Ian Elliott | e36b208 | 2015-07-06 14:27:58 -0600 | [diff] [blame] | 529 | .pSurfaceDescription = (const VkSurfaceDescriptionWSI *)&demo->surface_description, |
| 530 | .minImageCount = desiredNumberOfSwapChainImages, |
Chia-I Wu | 5b66aa5 | 2015-04-16 22:02:10 +0800 | [diff] [blame] | 531 | .imageFormat = demo->format, |
| 532 | .imageExtent = { |
Ian Elliott | e36b208 | 2015-07-06 14:27:58 -0600 | [diff] [blame] | 533 | .width = swapChainExtent.width, |
| 534 | .height = swapChainExtent.height, |
Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 535 | }, |
Ian Elliott | e36b208 | 2015-07-06 14:27:58 -0600 | [diff] [blame] | 536 | .preTransform = preTransform, |
Chia-I Wu | 5b66aa5 | 2015-04-16 22:02:10 +0800 | [diff] [blame] | 537 | .imageArraySize = 1, |
Ian Elliott | e36b208 | 2015-07-06 14:27:58 -0600 | [diff] [blame] | 538 | .presentMode = swapChainPresentMode, |
| 539 | .oldSwapChain.handle = 0, |
| 540 | .clipped = true, |
Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 541 | }; |
Mark Lobodzinski | e2d07a5 | 2015-01-29 08:55:56 -0600 | [diff] [blame] | 542 | uint32_t i; |
Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 543 | |
Jon Ashburn | cedc15f | 2015-05-21 18:13:33 -0600 | [diff] [blame] | 544 | err = demo->fpCreateSwapChainWSI(demo->device, &swap_chain, &demo->swap_chain); |
Chia-I Wu | 5b66aa5 | 2015-04-16 22:02:10 +0800 | [diff] [blame] | 545 | assert(!err); |
| 546 | |
Ian Elliott | e36b208 | 2015-07-06 14:27:58 -0600 | [diff] [blame] | 547 | size_t swapChainImagesSize; |
| 548 | err = demo->fpGetSwapChainInfoWSI(demo->device, demo->swap_chain, |
| 549 | VK_SWAP_CHAIN_INFO_TYPE_IMAGES_WSI, |
| 550 | &swapChainImagesSize, NULL); |
| 551 | assert(!err); |
Chia-I Wu | 5b66aa5 | 2015-04-16 22:02:10 +0800 | [diff] [blame] | 552 | |
Ian Elliott | e36b208 | 2015-07-06 14:27:58 -0600 | [diff] [blame] | 553 | VkSwapChainImagePropertiesWSI* swapChainImages = (VkSwapChainImagePropertiesWSI*)malloc(swapChainImagesSize); |
| 554 | assert(swapChainImages); |
| 555 | err = demo->fpGetSwapChainInfoWSI(demo->device, demo->swap_chain, |
| 556 | VK_SWAP_CHAIN_INFO_TYPE_IMAGES_WSI, |
| 557 | &swapChainImagesSize, swapChainImages); |
| 558 | assert(!err); |
| 559 | |
| 560 | // The number of images within the swap chain is determined based on the |
| 561 | // size of the info returned |
| 562 | demo->swapChainImageCount = swapChainImagesSize / sizeof(VkSwapChainImagePropertiesWSI); |
| 563 | |
| 564 | demo->buffers = (SwapChainBuffers*)malloc(sizeof(SwapChainBuffers)*demo->swapChainImageCount); |
| 565 | assert(demo->buffers); |
| 566 | |
| 567 | for (i = 0; i < demo->swapChainImageCount; i++) { |
Chia-I Wu | c278df8 | 2015-07-07 11:50:03 +0800 | [diff] [blame] | 568 | VkAttachmentViewCreateInfo color_attachment_view = { |
| 569 | .sType = VK_STRUCTURE_TYPE_ATTACHMENT_VIEW_CREATE_INFO, |
Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 570 | .pNext = NULL, |
| 571 | .format = demo->format, |
| 572 | .mipLevel = 0, |
| 573 | .baseArraySlice = 0, |
| 574 | .arraySize = 1, |
| 575 | }; |
| 576 | |
Ian Elliott | e36b208 | 2015-07-06 14:27:58 -0600 | [diff] [blame] | 577 | demo->buffers[i].image = swapChainImages[i].image; |
Mark Lobodzinski | 97dcd04 | 2015-04-16 08:52:00 -0500 | [diff] [blame] | 578 | |
Courtney Goeltzenleuchter | bfccf41 | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 579 | demo_set_image_layout(demo, demo->buffers[i].image, |
malnasse | 4b8ba4d | 2015-06-03 17:28:38 -0400 | [diff] [blame] | 580 | VK_IMAGE_ASPECT_COLOR, |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 581 | VK_IMAGE_LAYOUT_UNDEFINED, |
| 582 | VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL); |
Courtney Goeltzenleuchter | bfccf41 | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 583 | |
Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 584 | color_attachment_view.image = demo->buffers[i].image; |
| 585 | |
Chia-I Wu | c278df8 | 2015-07-07 11:50:03 +0800 | [diff] [blame] | 586 | err = vkCreateAttachmentView(demo->device, |
Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 587 | &color_attachment_view, &demo->buffers[i].view); |
| 588 | assert(!err); |
| 589 | } |
Piers Daniell | 886be47 | 2015-02-23 16:23:13 -0700 | [diff] [blame] | 590 | |
| 591 | demo->current_buffer = 0; |
Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 592 | } |
| 593 | |
Chia-I Wu | 9ae87c9 | 2014-10-07 14:15:01 +0800 | [diff] [blame] | 594 | static void demo_prepare_depth(struct demo *demo) |
| 595 | { |
Tony Barbour | 8205d90 | 2015-04-16 15:59:00 -0600 | [diff] [blame] | 596 | const VkFormat depth_format = VK_FORMAT_D16_UNORM; |
Courtney Goeltzenleuchter | 382489d | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 597 | const VkImageCreateInfo image = { |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 598 | .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO, |
Chia-I Wu | 9ae87c9 | 2014-10-07 14:15:01 +0800 | [diff] [blame] | 599 | .pNext = NULL, |
Tony Barbour | 8205d90 | 2015-04-16 15:59:00 -0600 | [diff] [blame] | 600 | .imageType = VK_IMAGE_TYPE_2D, |
Chia-I Wu | 9ae87c9 | 2014-10-07 14:15:01 +0800 | [diff] [blame] | 601 | .format = depth_format, |
| 602 | .extent = { demo->width, demo->height, 1 }, |
| 603 | .mipLevels = 1, |
| 604 | .arraySize = 1, |
| 605 | .samples = 1, |
Tony Barbour | 8205d90 | 2015-04-16 15:59:00 -0600 | [diff] [blame] | 606 | .tiling = VK_IMAGE_TILING_OPTIMAL, |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 607 | .usage = VK_IMAGE_USAGE_DEPTH_STENCIL_BIT, |
Chia-I Wu | 9ae87c9 | 2014-10-07 14:15:01 +0800 | [diff] [blame] | 608 | .flags = 0, |
| 609 | }; |
Courtney Goeltzenleuchter | ddcb619 | 2015-04-14 18:48:46 -0600 | [diff] [blame] | 610 | VkMemoryAllocInfo mem_alloc = { |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 611 | .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOC_INFO, |
Mark Lobodzinski | 97dcd04 | 2015-04-16 08:52:00 -0500 | [diff] [blame] | 612 | .pNext = NULL, |
Chia-I Wu | 9ae87c9 | 2014-10-07 14:15:01 +0800 | [diff] [blame] | 613 | .allocationSize = 0, |
Mark Lobodzinski | 7234629 | 2015-07-02 16:49:40 -0600 | [diff] [blame] | 614 | .memoryTypeIndex = 0, |
Chia-I Wu | 9ae87c9 | 2014-10-07 14:15:01 +0800 | [diff] [blame] | 615 | }; |
Chia-I Wu | c278df8 | 2015-07-07 11:50:03 +0800 | [diff] [blame] | 616 | VkAttachmentViewCreateInfo view = { |
| 617 | .sType = VK_STRUCTURE_TYPE_ATTACHMENT_VIEW_CREATE_INFO, |
Chia-I Wu | 9ae87c9 | 2014-10-07 14:15:01 +0800 | [diff] [blame] | 618 | .pNext = NULL, |
Tony Barbour | de4124d | 2015-07-03 10:33:54 -0600 | [diff] [blame] | 619 | .image.handle = VK_NULL_HANDLE, |
Piers Daniell | 1cf7fe1 | 2015-07-16 09:35:35 -0600 | [diff] [blame^] | 620 | .format = depth_format, |
Chia-I Wu | 9ae87c9 | 2014-10-07 14:15:01 +0800 | [diff] [blame] | 621 | .mipLevel = 0, |
| 622 | .baseArraySlice = 0, |
| 623 | .arraySize = 1, |
| 624 | .flags = 0, |
| 625 | }; |
Jon Ashburn | a9ae383 | 2015-01-16 09:37:43 -0700 | [diff] [blame] | 626 | |
Mark Lobodzinski | 2318261 | 2015-05-29 09:32:35 -0500 | [diff] [blame] | 627 | VkMemoryRequirements mem_reqs; |
Tony Barbour | 22a3086 | 2015-04-22 09:02:32 -0600 | [diff] [blame] | 628 | VkResult U_ASSERT_ONLY err; |
Chia-I Wu | 9ae87c9 | 2014-10-07 14:15:01 +0800 | [diff] [blame] | 629 | |
| 630 | demo->depth.format = depth_format; |
| 631 | |
| 632 | /* create image */ |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 633 | err = vkCreateImage(demo->device, &image, |
Chia-I Wu | 9ae87c9 | 2014-10-07 14:15:01 +0800 | [diff] [blame] | 634 | &demo->depth.image); |
| 635 | assert(!err); |
| 636 | |
Mark Lobodzinski | 7234629 | 2015-07-02 16:49:40 -0600 | [diff] [blame] | 637 | /* get memory requirements for this object */ |
Tony Barbour | de4124d | 2015-07-03 10:33:54 -0600 | [diff] [blame] | 638 | err = vkGetImageMemoryRequirements(demo->device, demo->depth.image, |
| 639 | &mem_reqs); |
Mark Lobodzinski | 7234629 | 2015-07-02 16:49:40 -0600 | [diff] [blame] | 640 | |
| 641 | /* select memory size and type */ |
Mark Lobodzinski | 2318261 | 2015-05-29 09:32:35 -0500 | [diff] [blame] | 642 | mem_alloc.allocationSize = mem_reqs.size; |
Mark Lobodzinski | 7234629 | 2015-07-02 16:49:40 -0600 | [diff] [blame] | 643 | err = memory_type_from_properties(demo, |
| 644 | mem_reqs.memoryTypeBits, |
| 645 | VK_MEMORY_PROPERTY_DEVICE_ONLY, |
| 646 | &mem_alloc.memoryTypeIndex); |
| 647 | assert(!err); |
Chia-I Wu | 9ae87c9 | 2014-10-07 14:15:01 +0800 | [diff] [blame] | 648 | |
Mark Lobodzinski | 2318261 | 2015-05-29 09:32:35 -0500 | [diff] [blame] | 649 | /* allocate memory */ |
| 650 | err = vkAllocMemory(demo->device, &mem_alloc, &demo->depth.mem); |
| 651 | assert(!err); |
Chia-I Wu | 9ae87c9 | 2014-10-07 14:15:01 +0800 | [diff] [blame] | 652 | |
Mark Lobodzinski | 2318261 | 2015-05-29 09:32:35 -0500 | [diff] [blame] | 653 | /* bind memory */ |
Tony Barbour | de4124d | 2015-07-03 10:33:54 -0600 | [diff] [blame] | 654 | err = vkBindImageMemory(demo->device, demo->depth.image, |
| 655 | demo->depth.mem, 0); |
Mark Lobodzinski | 2318261 | 2015-05-29 09:32:35 -0500 | [diff] [blame] | 656 | assert(!err); |
Chia-I Wu | 9ae87c9 | 2014-10-07 14:15:01 +0800 | [diff] [blame] | 657 | |
Courtney Goeltzenleuchter | bfccf41 | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 658 | demo_set_image_layout(demo, demo->depth.image, |
malnasse | 4b8ba4d | 2015-06-03 17:28:38 -0400 | [diff] [blame] | 659 | VK_IMAGE_ASPECT_DEPTH, |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 660 | VK_IMAGE_LAYOUT_UNDEFINED, |
| 661 | VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL); |
Courtney Goeltzenleuchter | bfccf41 | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 662 | |
Chia-I Wu | 9ae87c9 | 2014-10-07 14:15:01 +0800 | [diff] [blame] | 663 | /* create image view */ |
| 664 | view.image = demo->depth.image; |
Chia-I Wu | c278df8 | 2015-07-07 11:50:03 +0800 | [diff] [blame] | 665 | err = vkCreateAttachmentView(demo->device, &view, &demo->depth.view); |
Chia-I Wu | 9ae87c9 | 2014-10-07 14:15:01 +0800 | [diff] [blame] | 666 | assert(!err); |
Chia-I Wu | 9ae87c9 | 2014-10-07 14:15:01 +0800 | [diff] [blame] | 667 | } |
| 668 | |
Courtney Goeltzenleuchter | 372e13c | 2015-02-13 17:52:46 -0700 | [diff] [blame] | 669 | static void demo_prepare_texture_image(struct demo *demo, |
| 670 | const uint32_t *tex_colors, |
Courtney Goeltzenleuchter | bfccf41 | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 671 | struct texture_object *tex_obj, |
Courtney Goeltzenleuchter | 382489d | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 672 | VkImageTiling tiling, |
Courtney Goeltzenleuchter | cb67a32 | 2015-04-21 09:31:23 -0600 | [diff] [blame] | 673 | VkImageUsageFlags usage, |
Courtney Goeltzenleuchter | 382489d | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 674 | VkFlags mem_props) |
Chia-I Wu | b043fe3 | 2014-10-06 15:30:33 +0800 | [diff] [blame] | 675 | { |
Tony Barbour | 8205d90 | 2015-04-16 15:59:00 -0600 | [diff] [blame] | 676 | const VkFormat tex_format = VK_FORMAT_B8G8R8A8_UNORM; |
Mark Lobodzinski | e2d07a5 | 2015-01-29 08:55:56 -0600 | [diff] [blame] | 677 | const int32_t tex_width = 2; |
| 678 | const int32_t tex_height = 2; |
Tony Barbour | 22a3086 | 2015-04-22 09:02:32 -0600 | [diff] [blame] | 679 | VkResult U_ASSERT_ONLY err; |
Chia-I Wu | b043fe3 | 2014-10-06 15:30:33 +0800 | [diff] [blame] | 680 | |
Courtney Goeltzenleuchter | bfccf41 | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 681 | tex_obj->tex_width = tex_width; |
| 682 | tex_obj->tex_height = tex_height; |
Courtney Goeltzenleuchter | 372e13c | 2015-02-13 17:52:46 -0700 | [diff] [blame] | 683 | |
Courtney Goeltzenleuchter | 382489d | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 684 | const VkImageCreateInfo image_create_info = { |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 685 | .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO, |
Courtney Goeltzenleuchter | 3226a6f | 2015-02-11 18:17:22 -0700 | [diff] [blame] | 686 | .pNext = NULL, |
Tony Barbour | 8205d90 | 2015-04-16 15:59:00 -0600 | [diff] [blame] | 687 | .imageType = VK_IMAGE_TYPE_2D, |
Courtney Goeltzenleuchter | 372e13c | 2015-02-13 17:52:46 -0700 | [diff] [blame] | 688 | .format = tex_format, |
| 689 | .extent = { tex_width, tex_height, 1 }, |
| 690 | .mipLevels = 1, |
| 691 | .arraySize = 1, |
| 692 | .samples = 1, |
| 693 | .tiling = tiling, |
Courtney Goeltzenleuchter | cb67a32 | 2015-04-21 09:31:23 -0600 | [diff] [blame] | 694 | .usage = usage, |
Courtney Goeltzenleuchter | 372e13c | 2015-02-13 17:52:46 -0700 | [diff] [blame] | 695 | .flags = 0, |
| 696 | }; |
Courtney Goeltzenleuchter | ddcb619 | 2015-04-14 18:48:46 -0600 | [diff] [blame] | 697 | VkMemoryAllocInfo mem_alloc = { |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 698 | .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOC_INFO, |
Mark Lobodzinski | 97dcd04 | 2015-04-16 08:52:00 -0500 | [diff] [blame] | 699 | .pNext = NULL, |
Courtney Goeltzenleuchter | 372e13c | 2015-02-13 17:52:46 -0700 | [diff] [blame] | 700 | .allocationSize = 0, |
Mark Lobodzinski | 7234629 | 2015-07-02 16:49:40 -0600 | [diff] [blame] | 701 | .memoryTypeIndex = 0, |
Courtney Goeltzenleuchter | 3226a6f | 2015-02-11 18:17:22 -0700 | [diff] [blame] | 702 | }; |
| 703 | |
Mark Lobodzinski | 2318261 | 2015-05-29 09:32:35 -0500 | [diff] [blame] | 704 | VkMemoryRequirements mem_reqs; |
Courtney Goeltzenleuchter | 372e13c | 2015-02-13 17:52:46 -0700 | [diff] [blame] | 705 | |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 706 | err = vkCreateImage(demo->device, &image_create_info, |
Courtney Goeltzenleuchter | bfccf41 | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 707 | &tex_obj->image); |
Courtney Goeltzenleuchter | 3226a6f | 2015-02-11 18:17:22 -0700 | [diff] [blame] | 708 | assert(!err); |
| 709 | |
Tony Barbour | de4124d | 2015-07-03 10:33:54 -0600 | [diff] [blame] | 710 | err = vkGetImageMemoryRequirements(demo->device, tex_obj->image, &mem_reqs); |
Mark Lobodzinski | 7234629 | 2015-07-02 16:49:40 -0600 | [diff] [blame] | 711 | |
| 712 | mem_alloc.allocationSize = mem_reqs.size; |
| 713 | err = memory_type_from_properties(demo, mem_reqs.memoryTypeBits, mem_props, &mem_alloc.memoryTypeIndex); |
| 714 | assert(!err); |
Courtney Goeltzenleuchter | 3226a6f | 2015-02-11 18:17:22 -0700 | [diff] [blame] | 715 | |
Mark Lobodzinski | 2318261 | 2015-05-29 09:32:35 -0500 | [diff] [blame] | 716 | /* allocate memory */ |
| 717 | err = vkAllocMemory(demo->device, &mem_alloc, &tex_obj->mem); |
| 718 | assert(!err); |
Courtney Goeltzenleuchter | 3226a6f | 2015-02-11 18:17:22 -0700 | [diff] [blame] | 719 | |
Mark Lobodzinski | 2318261 | 2015-05-29 09:32:35 -0500 | [diff] [blame] | 720 | /* bind memory */ |
Tony Barbour | de4124d | 2015-07-03 10:33:54 -0600 | [diff] [blame] | 721 | err = vkBindImageMemory(demo->device, tex_obj->image, |
Mark Lobodzinski | 2318261 | 2015-05-29 09:32:35 -0500 | [diff] [blame] | 722 | tex_obj->mem, 0); |
| 723 | assert(!err); |
Courtney Goeltzenleuchter | 3226a6f | 2015-02-11 18:17:22 -0700 | [diff] [blame] | 724 | |
Tony Barbour | 8205d90 | 2015-04-16 15:59:00 -0600 | [diff] [blame] | 725 | if (mem_props & VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT) { |
Courtney Goeltzenleuchter | 382489d | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 726 | const VkImageSubresource subres = { |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 727 | .aspect = VK_IMAGE_ASPECT_COLOR, |
Courtney Goeltzenleuchter | 3226a6f | 2015-02-11 18:17:22 -0700 | [diff] [blame] | 728 | .mipLevel = 0, |
| 729 | .arraySlice = 0, |
| 730 | }; |
Courtney Goeltzenleuchter | 382489d | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 731 | VkSubresourceLayout layout; |
Courtney Goeltzenleuchter | 3226a6f | 2015-02-11 18:17:22 -0700 | [diff] [blame] | 732 | void *data; |
| 733 | int32_t x, y; |
| 734 | |
Tony Barbour | 426b905 | 2015-06-24 16:06:58 -0600 | [diff] [blame] | 735 | err = vkGetImageSubresourceLayout(demo->device, tex_obj->image, &subres, &layout); |
| 736 | assert(!err); |
Courtney Goeltzenleuchter | 3226a6f | 2015-02-11 18:17:22 -0700 | [diff] [blame] | 737 | |
Mark Lobodzinski | 2318261 | 2015-05-29 09:32:35 -0500 | [diff] [blame] | 738 | err = vkMapMemory(demo->device, tex_obj->mem, 0, 0, 0, &data); |
Courtney Goeltzenleuchter | 3226a6f | 2015-02-11 18:17:22 -0700 | [diff] [blame] | 739 | assert(!err); |
| 740 | |
| 741 | for (y = 0; y < tex_height; y++) { |
| 742 | uint32_t *row = (uint32_t *) ((char *) data + layout.rowPitch * y); |
| 743 | for (x = 0; x < tex_width; x++) |
Courtney Goeltzenleuchter | 372e13c | 2015-02-13 17:52:46 -0700 | [diff] [blame] | 744 | row[x] = tex_colors[(x & 1) ^ (y & 1)]; |
Courtney Goeltzenleuchter | 3226a6f | 2015-02-11 18:17:22 -0700 | [diff] [blame] | 745 | } |
| 746 | |
Mark Lobodzinski | 2318261 | 2015-05-29 09:32:35 -0500 | [diff] [blame] | 747 | err = vkUnmapMemory(demo->device, tex_obj->mem); |
Courtney Goeltzenleuchter | 3226a6f | 2015-02-11 18:17:22 -0700 | [diff] [blame] | 748 | assert(!err); |
Courtney Goeltzenleuchter | 372e13c | 2015-02-13 17:52:46 -0700 | [diff] [blame] | 749 | } |
Courtney Goeltzenleuchter | bfccf41 | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 750 | |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 751 | tex_obj->imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL; |
Courtney Goeltzenleuchter | bfccf41 | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 752 | demo_set_image_layout(demo, tex_obj->image, |
malnasse | 4b8ba4d | 2015-06-03 17:28:38 -0400 | [diff] [blame] | 753 | VK_IMAGE_ASPECT_COLOR, |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 754 | VK_IMAGE_LAYOUT_UNDEFINED, |
Courtney Goeltzenleuchter | bfccf41 | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 755 | tex_obj->imageLayout); |
| 756 | /* 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] | 757 | } |
| 758 | |
Mark Lobodzinski | cf26e07 | 2015-04-16 11:44:05 -0500 | [diff] [blame] | 759 | 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] | 760 | { |
| 761 | /* clean up staging resources */ |
Tony Barbour | de4124d | 2015-07-03 10:33:54 -0600 | [diff] [blame] | 762 | vkDestroyImage(demo->device, tex_obj->image); |
Courtney Goeltzenleuchter | a063d9b | 2015-06-10 16:16:22 -0600 | [diff] [blame] | 763 | vkFreeMemory(demo->device, tex_obj->mem); |
Courtney Goeltzenleuchter | 372e13c | 2015-02-13 17:52:46 -0700 | [diff] [blame] | 764 | } |
| 765 | |
| 766 | static void demo_prepare_textures(struct demo *demo) |
| 767 | { |
Tony Barbour | 8205d90 | 2015-04-16 15:59:00 -0600 | [diff] [blame] | 768 | const VkFormat tex_format = VK_FORMAT_B8G8R8A8_UNORM; |
Courtney Goeltzenleuchter | 382489d | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 769 | VkFormatProperties props; |
Courtney Goeltzenleuchter | 372e13c | 2015-02-13 17:52:46 -0700 | [diff] [blame] | 770 | const uint32_t tex_colors[DEMO_TEXTURE_COUNT][2] = { |
| 771 | { 0xffff0000, 0xff00ff00 }, |
| 772 | }; |
Tony Barbour | 22a3086 | 2015-04-22 09:02:32 -0600 | [diff] [blame] | 773 | VkResult U_ASSERT_ONLY err; |
Courtney Goeltzenleuchter | 372e13c | 2015-02-13 17:52:46 -0700 | [diff] [blame] | 774 | uint32_t i; |
| 775 | |
Courtney Goeltzenleuchter | ab36aa6 | 2015-07-12 12:40:29 -0600 | [diff] [blame] | 776 | err = vkGetPhysicalDeviceFormatProperties(demo->gpu, tex_format, &props); |
Courtney Goeltzenleuchter | 372e13c | 2015-02-13 17:52:46 -0700 | [diff] [blame] | 777 | assert(!err); |
| 778 | |
| 779 | for (i = 0; i < DEMO_TEXTURE_COUNT; i++) { |
Tony Barbour | 8205d90 | 2015-04-16 15:59:00 -0600 | [diff] [blame] | 780 | 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] | 781 | /* Device can texture using linear textures */ |
| 782 | demo_prepare_texture_image(demo, tex_colors[i], &demo->textures[i], |
Courtney Goeltzenleuchter | cb67a32 | 2015-04-21 09:31:23 -0600 | [diff] [blame] | 783 | 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] | 784 | } else if (props.optimalTilingFeatures & VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT){ |
Courtney Goeltzenleuchter | 372e13c | 2015-02-13 17:52:46 -0700 | [diff] [blame] | 785 | /* Must use staging buffer to copy linear texture to optimized */ |
Courtney Goeltzenleuchter | bfccf41 | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 786 | struct texture_object staging_texture; |
Courtney Goeltzenleuchter | 372e13c | 2015-02-13 17:52:46 -0700 | [diff] [blame] | 787 | |
| 788 | memset(&staging_texture, 0, sizeof(staging_texture)); |
| 789 | demo_prepare_texture_image(demo, tex_colors[i], &staging_texture, |
Courtney Goeltzenleuchter | cb67a32 | 2015-04-21 09:31:23 -0600 | [diff] [blame] | 790 | 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] | 791 | |
| 792 | demo_prepare_texture_image(demo, tex_colors[i], &demo->textures[i], |
Courtney Goeltzenleuchter | cb67a32 | 2015-04-21 09:31:23 -0600 | [diff] [blame] | 793 | VK_IMAGE_TILING_OPTIMAL, |
| 794 | (VK_IMAGE_USAGE_TRANSFER_DESTINATION_BIT | VK_IMAGE_USAGE_SAMPLED_BIT), |
| 795 | VK_MEMORY_PROPERTY_DEVICE_ONLY); |
Courtney Goeltzenleuchter | 372e13c | 2015-02-13 17:52:46 -0700 | [diff] [blame] | 796 | |
Courtney Goeltzenleuchter | bfccf41 | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 797 | demo_set_image_layout(demo, staging_texture.image, |
malnasse | 4b8ba4d | 2015-06-03 17:28:38 -0400 | [diff] [blame] | 798 | VK_IMAGE_ASPECT_COLOR, |
Courtney Goeltzenleuchter | bfccf41 | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 799 | staging_texture.imageLayout, |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 800 | VK_IMAGE_LAYOUT_TRANSFER_SOURCE_OPTIMAL); |
Courtney Goeltzenleuchter | 372e13c | 2015-02-13 17:52:46 -0700 | [diff] [blame] | 801 | |
Courtney Goeltzenleuchter | bfccf41 | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 802 | demo_set_image_layout(demo, demo->textures[i].image, |
malnasse | 4b8ba4d | 2015-06-03 17:28:38 -0400 | [diff] [blame] | 803 | VK_IMAGE_ASPECT_COLOR, |
Courtney Goeltzenleuchter | bfccf41 | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 804 | demo->textures[i].imageLayout, |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 805 | VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL); |
Courtney Goeltzenleuchter | 372e13c | 2015-02-13 17:52:46 -0700 | [diff] [blame] | 806 | |
Courtney Goeltzenleuchter | 382489d | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 807 | VkImageCopy copy_region = { |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 808 | .srcSubresource = { VK_IMAGE_ASPECT_COLOR, 0, 0 }, |
Courtney Goeltzenleuchter | 372e13c | 2015-02-13 17:52:46 -0700 | [diff] [blame] | 809 | .srcOffset = { 0, 0, 0 }, |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 810 | .destSubresource = { VK_IMAGE_ASPECT_COLOR, 0, 0 }, |
Courtney Goeltzenleuchter | 372e13c | 2015-02-13 17:52:46 -0700 | [diff] [blame] | 811 | .destOffset = { 0, 0, 0 }, |
| 812 | .extent = { staging_texture.tex_width, staging_texture.tex_height, 1 }, |
| 813 | }; |
Courtney Goeltzenleuchter | 633f9c5 | 2015-04-30 10:58:33 -0600 | [diff] [blame] | 814 | vkCmdCopyImage(demo->setup_cmd, |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 815 | staging_texture.image, VK_IMAGE_LAYOUT_TRANSFER_SOURCE_OPTIMAL, |
| 816 | demo->textures[i].image, VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL, |
Courtney Goeltzenleuchter | 51cbf30 | 2015-03-25 11:25:10 -0600 | [diff] [blame] | 817 | 1, ©_region); |
Courtney Goeltzenleuchter | 372e13c | 2015-02-13 17:52:46 -0700 | [diff] [blame] | 818 | |
Courtney Goeltzenleuchter | bfccf41 | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 819 | demo_set_image_layout(demo, demo->textures[i].image, |
malnasse | 4b8ba4d | 2015-06-03 17:28:38 -0400 | [diff] [blame] | 820 | VK_IMAGE_ASPECT_COLOR, |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 821 | VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL, |
Courtney Goeltzenleuchter | bfccf41 | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 822 | demo->textures[i].imageLayout); |
Courtney Goeltzenleuchter | 372e13c | 2015-02-13 17:52:46 -0700 | [diff] [blame] | 823 | |
Courtney Goeltzenleuchter | bfccf41 | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 824 | demo_flush_init_cmd(demo); |
Courtney Goeltzenleuchter | 372e13c | 2015-02-13 17:52:46 -0700 | [diff] [blame] | 825 | |
Courtney Goeltzenleuchter | 876629f | 2015-04-21 09:30:03 -0600 | [diff] [blame] | 826 | demo_destroy_texture_image(demo, &staging_texture); |
Courtney Goeltzenleuchter | 372e13c | 2015-02-13 17:52:46 -0700 | [diff] [blame] | 827 | } else { |
Tony Barbour | 8205d90 | 2015-04-16 15:59:00 -0600 | [diff] [blame] | 828 | /* Can't support VK_FORMAT_B8G8R8A8_UNORM !? */ |
Piers Daniell | 886be47 | 2015-02-23 16:23:13 -0700 | [diff] [blame] | 829 | assert(!"No support for B8G8R8A8_UNORM as texture image format"); |
Courtney Goeltzenleuchter | 372e13c | 2015-02-13 17:52:46 -0700 | [diff] [blame] | 830 | } |
Courtney Goeltzenleuchter | 3226a6f | 2015-02-11 18:17:22 -0700 | [diff] [blame] | 831 | |
Courtney Goeltzenleuchter | 382489d | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 832 | const VkSamplerCreateInfo sampler = { |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 833 | .sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO, |
Chia-I Wu | b043fe3 | 2014-10-06 15:30:33 +0800 | [diff] [blame] | 834 | .pNext = NULL, |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 835 | .magFilter = VK_TEX_FILTER_NEAREST, |
| 836 | .minFilter = VK_TEX_FILTER_NEAREST, |
Tony Barbour | 8205d90 | 2015-04-16 15:59:00 -0600 | [diff] [blame] | 837 | .mipMode = VK_TEX_MIPMAP_MODE_BASE, |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 838 | .addressU = VK_TEX_ADDRESS_WRAP, |
| 839 | .addressV = VK_TEX_ADDRESS_WRAP, |
| 840 | .addressW = VK_TEX_ADDRESS_WRAP, |
Chia-I Wu | b043fe3 | 2014-10-06 15:30:33 +0800 | [diff] [blame] | 841 | .mipLodBias = 0.0f, |
Courtney Goeltzenleuchter | bc9c816 | 2015-02-13 18:20:24 -0700 | [diff] [blame] | 842 | .maxAnisotropy = 1, |
Tony Barbour | 8205d90 | 2015-04-16 15:59:00 -0600 | [diff] [blame] | 843 | .compareOp = VK_COMPARE_OP_NEVER, |
Chia-I Wu | b043fe3 | 2014-10-06 15:30:33 +0800 | [diff] [blame] | 844 | .minLod = 0.0f, |
| 845 | .maxLod = 0.0f, |
Tony Barbour | 2c4e7c7 | 2015-06-25 16:56:44 -0600 | [diff] [blame] | 846 | .borderColor = VK_BORDER_COLOR_FLOAT_OPAQUE_WHITE, |
Chia-I Wu | b043fe3 | 2014-10-06 15:30:33 +0800 | [diff] [blame] | 847 | }; |
Courtney Goeltzenleuchter | 382489d | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 848 | VkImageViewCreateInfo view = { |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 849 | .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO, |
Chia-I Wu | b043fe3 | 2014-10-06 15:30:33 +0800 | [diff] [blame] | 850 | .pNext = NULL, |
Tony Barbour | de4124d | 2015-07-03 10:33:54 -0600 | [diff] [blame] | 851 | .image.handle = VK_NULL_HANDLE, |
Tony Barbour | 8205d90 | 2015-04-16 15:59:00 -0600 | [diff] [blame] | 852 | .viewType = VK_IMAGE_VIEW_TYPE_2D, |
Courtney Goeltzenleuchter | 372e13c | 2015-02-13 17:52:46 -0700 | [diff] [blame] | 853 | .format = tex_format, |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 854 | .channels = { VK_CHANNEL_SWIZZLE_R, |
| 855 | VK_CHANNEL_SWIZZLE_G, |
| 856 | VK_CHANNEL_SWIZZLE_B, |
| 857 | VK_CHANNEL_SWIZZLE_A, }, |
| 858 | .subresourceRange = { VK_IMAGE_ASPECT_COLOR, 0, 1, 0, 1 }, |
Chia-I Wu | b043fe3 | 2014-10-06 15:30:33 +0800 | [diff] [blame] | 859 | }; |
Jon Ashburn | a9ae383 | 2015-01-16 09:37:43 -0700 | [diff] [blame] | 860 | |
Chia-I Wu | b043fe3 | 2014-10-06 15:30:33 +0800 | [diff] [blame] | 861 | /* create sampler */ |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 862 | err = vkCreateSampler(demo->device, &sampler, |
Chia-I Wu | b043fe3 | 2014-10-06 15:30:33 +0800 | [diff] [blame] | 863 | &demo->textures[i].sampler); |
| 864 | assert(!err); |
| 865 | |
Chia-I Wu | b043fe3 | 2014-10-06 15:30:33 +0800 | [diff] [blame] | 866 | /* create image view */ |
| 867 | view.image = demo->textures[i].image; |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 868 | err = vkCreateImageView(demo->device, &view, |
Courtney Goeltzenleuchter | 3226a6f | 2015-02-11 18:17:22 -0700 | [diff] [blame] | 869 | &demo->textures[i].view); |
Chia-I Wu | b043fe3 | 2014-10-06 15:30:33 +0800 | [diff] [blame] | 870 | assert(!err); |
Chia-I Wu | b043fe3 | 2014-10-06 15:30:33 +0800 | [diff] [blame] | 871 | } |
| 872 | } |
| 873 | |
Chia-I Wu | 99621bc | 2014-10-08 11:52:22 +0800 | [diff] [blame] | 874 | static void demo_prepare_vertices(struct demo *demo) |
| 875 | { |
| 876 | const float vb[3][5] = { |
| 877 | /* position texcoord */ |
Chia-I Wu | e2504cb | 2015-04-22 14:20:52 +0800 | [diff] [blame] | 878 | { -1.0f, -1.0f, 0.2f, 0.0f, 0.0f }, |
| 879 | { 1.0f, -1.0f, 0.25f, 1.0f, 0.0f }, |
Chia-I Wu | 99621bc | 2014-10-08 11:52:22 +0800 | [diff] [blame] | 880 | { 0.0f, 1.0f, 1.0f, 0.5f, 1.0f }, |
| 881 | }; |
Courtney Goeltzenleuchter | ddcb619 | 2015-04-14 18:48:46 -0600 | [diff] [blame] | 882 | const VkBufferCreateInfo buf_info = { |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 883 | .sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO, |
Chia-I Wu | 714df45 | 2015-01-01 07:55:04 +0800 | [diff] [blame] | 884 | .pNext = NULL, |
| 885 | .size = sizeof(vb), |
Courtney Goeltzenleuchter | ad87081 | 2015-04-15 15:29:59 -0600 | [diff] [blame] | 886 | .usage = VK_BUFFER_USAGE_VERTEX_BUFFER_BIT, |
Chia-I Wu | 714df45 | 2015-01-01 07:55:04 +0800 | [diff] [blame] | 887 | .flags = 0, |
| 888 | }; |
Courtney Goeltzenleuchter | ddcb619 | 2015-04-14 18:48:46 -0600 | [diff] [blame] | 889 | VkMemoryAllocInfo mem_alloc = { |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 890 | .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOC_INFO, |
Mark Lobodzinski | 97dcd04 | 2015-04-16 08:52:00 -0500 | [diff] [blame] | 891 | .pNext = NULL, |
Chia-I Wu | 714df45 | 2015-01-01 07:55:04 +0800 | [diff] [blame] | 892 | .allocationSize = 0, |
Mark Lobodzinski | 7234629 | 2015-07-02 16:49:40 -0600 | [diff] [blame] | 893 | .memoryTypeIndex = 0, |
Chia-I Wu | 99621bc | 2014-10-08 11:52:22 +0800 | [diff] [blame] | 894 | }; |
Mark Lobodzinski | 2318261 | 2015-05-29 09:32:35 -0500 | [diff] [blame] | 895 | VkMemoryRequirements mem_reqs; |
Tony Barbour | 22a3086 | 2015-04-22 09:02:32 -0600 | [diff] [blame] | 896 | VkResult U_ASSERT_ONLY err; |
Chia-I Wu | 99621bc | 2014-10-08 11:52:22 +0800 | [diff] [blame] | 897 | void *data; |
| 898 | |
| 899 | memset(&demo->vertices, 0, sizeof(demo->vertices)); |
| 900 | |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 901 | err = vkCreateBuffer(demo->device, &buf_info, &demo->vertices.buf); |
Chia-I Wu | 714df45 | 2015-01-01 07:55:04 +0800 | [diff] [blame] | 902 | assert(!err); |
| 903 | |
Tony Barbour | de4124d | 2015-07-03 10:33:54 -0600 | [diff] [blame] | 904 | err = vkGetBufferMemoryRequirements(demo->device, |
| 905 | demo->vertices.buf, &mem_reqs); |
Chia-I Wu | 714df45 | 2015-01-01 07:55:04 +0800 | [diff] [blame] | 906 | |
Mark Lobodzinski | 7234629 | 2015-07-02 16:49:40 -0600 | [diff] [blame] | 907 | mem_alloc.allocationSize = mem_reqs.size; |
| 908 | err = memory_type_from_properties(demo, |
| 909 | mem_reqs.memoryTypeBits, |
| 910 | VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT, |
| 911 | &mem_alloc.memoryTypeIndex); |
| 912 | assert(!err); |
Chia-I Wu | 99621bc | 2014-10-08 11:52:22 +0800 | [diff] [blame] | 913 | |
Mark Lobodzinski | 2318261 | 2015-05-29 09:32:35 -0500 | [diff] [blame] | 914 | err = vkAllocMemory(demo->device, &mem_alloc, &demo->vertices.mem); |
| 915 | assert(!err); |
Chia-I Wu | 99621bc | 2014-10-08 11:52:22 +0800 | [diff] [blame] | 916 | |
Mark Lobodzinski | 2318261 | 2015-05-29 09:32:35 -0500 | [diff] [blame] | 917 | err = vkMapMemory(demo->device, demo->vertices.mem, 0, 0, 0, &data); |
| 918 | assert(!err); |
Chia-I Wu | 99621bc | 2014-10-08 11:52:22 +0800 | [diff] [blame] | 919 | |
Mark Lobodzinski | 2318261 | 2015-05-29 09:32:35 -0500 | [diff] [blame] | 920 | memcpy(data, vb, sizeof(vb)); |
Chia-I Wu | 99621bc | 2014-10-08 11:52:22 +0800 | [diff] [blame] | 921 | |
Mark Lobodzinski | 2318261 | 2015-05-29 09:32:35 -0500 | [diff] [blame] | 922 | err = vkUnmapMemory(demo->device, demo->vertices.mem); |
| 923 | assert(!err); |
| 924 | |
Tony Barbour | de4124d | 2015-07-03 10:33:54 -0600 | [diff] [blame] | 925 | err = vkBindBufferMemory(demo->device, demo->vertices.buf, |
Mark Lobodzinski | 2318261 | 2015-05-29 09:32:35 -0500 | [diff] [blame] | 926 | demo->vertices.mem, 0); |
| 927 | assert(!err); |
Chia-I Wu | 714df45 | 2015-01-01 07:55:04 +0800 | [diff] [blame] | 928 | |
Mark Lobodzinski | 0e0fb5c | 2015-06-23 15:11:57 -0600 | [diff] [blame] | 929 | 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] | 930 | demo->vertices.vi.pNext = NULL; |
| 931 | demo->vertices.vi.bindingCount = 1; |
| 932 | demo->vertices.vi.pVertexBindingDescriptions = demo->vertices.vi_bindings; |
| 933 | demo->vertices.vi.attributeCount = 2; |
| 934 | demo->vertices.vi.pVertexAttributeDescriptions = demo->vertices.vi_attrs; |
| 935 | |
Courtney Goeltzenleuchter | f5cdad0 | 2015-03-31 16:36:30 -0600 | [diff] [blame] | 936 | demo->vertices.vi_bindings[0].binding = VERTEX_BUFFER_BIND_ID; |
Chia-I Wu | 8d29d02 | 2014-10-08 12:14:39 +0800 | [diff] [blame] | 937 | demo->vertices.vi_bindings[0].strideInBytes = sizeof(vb[0]); |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 938 | 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] | 939 | |
Courtney Goeltzenleuchter | f5cdad0 | 2015-03-31 16:36:30 -0600 | [diff] [blame] | 940 | demo->vertices.vi_attrs[0].binding = VERTEX_BUFFER_BIND_ID; |
| 941 | demo->vertices.vi_attrs[0].location = 0; |
Tony Barbour | 8205d90 | 2015-04-16 15:59:00 -0600 | [diff] [blame] | 942 | demo->vertices.vi_attrs[0].format = VK_FORMAT_R32G32B32_SFLOAT; |
Chia-I Wu | 8d29d02 | 2014-10-08 12:14:39 +0800 | [diff] [blame] | 943 | demo->vertices.vi_attrs[0].offsetInBytes = 0; |
| 944 | |
Courtney Goeltzenleuchter | f5cdad0 | 2015-03-31 16:36:30 -0600 | [diff] [blame] | 945 | demo->vertices.vi_attrs[1].binding = VERTEX_BUFFER_BIND_ID; |
| 946 | demo->vertices.vi_attrs[1].location = 1; |
Tony Barbour | 8205d90 | 2015-04-16 15:59:00 -0600 | [diff] [blame] | 947 | demo->vertices.vi_attrs[1].format = VK_FORMAT_R32G32_SFLOAT; |
Chia-I Wu | 8d29d02 | 2014-10-08 12:14:39 +0800 | [diff] [blame] | 948 | demo->vertices.vi_attrs[1].offsetInBytes = sizeof(float) * 3; |
Chia-I Wu | 99621bc | 2014-10-08 11:52:22 +0800 | [diff] [blame] | 949 | } |
| 950 | |
Chia-I Wu | f838506 | 2015-01-04 16:27:24 +0800 | [diff] [blame] | 951 | static void demo_prepare_descriptor_layout(struct demo *demo) |
Chia-I Wu | b043fe3 | 2014-10-06 15:30:33 +0800 | [diff] [blame] | 952 | { |
Courtney Goeltzenleuchter | 382489d | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 953 | const VkDescriptorSetLayoutBinding layout_binding = { |
Courtney Goeltzenleuchter | ad87081 | 2015-04-15 15:29:59 -0600 | [diff] [blame] | 954 | .descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, |
Chia-I Wu | d3114a2 | 2015-05-25 16:22:52 +0800 | [diff] [blame] | 955 | .arraySize = DEMO_TEXTURE_COUNT, |
Tony Barbour | 8205d90 | 2015-04-16 15:59:00 -0600 | [diff] [blame] | 956 | .stageFlags = VK_SHADER_STAGE_FRAGMENT_BIT, |
Chia-I Wu | 310eece | 2015-03-27 12:56:09 +0800 | [diff] [blame] | 957 | .pImmutableSamplers = NULL, |
Chia-I Wu | b043fe3 | 2014-10-06 15:30:33 +0800 | [diff] [blame] | 958 | }; |
Courtney Goeltzenleuchter | 382489d | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 959 | const VkDescriptorSetLayoutCreateInfo descriptor_layout = { |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 960 | .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO, |
Chia-I Wu | fc9d913 | 2015-03-26 15:04:41 +0800 | [diff] [blame] | 961 | .pNext = NULL, |
| 962 | .count = 1, |
| 963 | .pBinding = &layout_binding, |
| 964 | }; |
Tony Barbour | 22a3086 | 2015-04-22 09:02:32 -0600 | [diff] [blame] | 965 | VkResult U_ASSERT_ONLY err; |
Chia-I Wu | b043fe3 | 2014-10-06 15:30:33 +0800 | [diff] [blame] | 966 | |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 967 | err = vkCreateDescriptorSetLayout(demo->device, |
Chia-I Wu | 7732cb2 | 2015-03-26 15:27:55 +0800 | [diff] [blame] | 968 | &descriptor_layout, &demo->desc_layout); |
| 969 | assert(!err); |
| 970 | |
Mark Lobodzinski | 556f721 | 2015-04-17 14:11:39 -0500 | [diff] [blame] | 971 | const VkPipelineLayoutCreateInfo pPipelineLayoutCreateInfo = { |
| 972 | .sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO, |
| 973 | .pNext = NULL, |
| 974 | .descriptorSetCount = 1, |
| 975 | .pSetLayouts = &demo->desc_layout, |
| 976 | }; |
| 977 | |
| 978 | err = vkCreatePipelineLayout(demo->device, |
| 979 | &pPipelineLayoutCreateInfo, |
| 980 | &demo->pipeline_layout); |
Chia-I Wu | b043fe3 | 2014-10-06 15:30:33 +0800 | [diff] [blame] | 981 | assert(!err); |
Chia-I Wu | b043fe3 | 2014-10-06 15:30:33 +0800 | [diff] [blame] | 982 | } |
| 983 | |
Chia-I Wu | 76cd422 | 2015-07-08 13:34:24 +0800 | [diff] [blame] | 984 | static void demo_prepare_render_pass(struct demo *demo) |
| 985 | { |
Chia-I Wu | c278df8 | 2015-07-07 11:50:03 +0800 | [diff] [blame] | 986 | const VkAttachmentDescription attachments[2] = { |
| 987 | [0] = { |
| 988 | .sType = VK_STRUCTURE_TYPE_ATTACHMENT_DESCRIPTION, |
| 989 | .pNext = NULL, |
| 990 | .format = demo->format, |
| 991 | .samples = 1, |
| 992 | .loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR, |
| 993 | .storeOp = VK_ATTACHMENT_STORE_OP_STORE, |
| 994 | .stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE, |
| 995 | .stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE, |
| 996 | .initialLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL, |
| 997 | .finalLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL, |
| 998 | }, |
| 999 | [1] = { |
| 1000 | .sType = VK_STRUCTURE_TYPE_ATTACHMENT_DESCRIPTION, |
| 1001 | .pNext = NULL, |
| 1002 | .format = demo->depth.format, |
| 1003 | .samples = 1, |
| 1004 | .loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR, |
| 1005 | .storeOp = VK_ATTACHMENT_STORE_OP_DONT_CARE, |
| 1006 | .stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE, |
| 1007 | .stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE, |
| 1008 | .initialLayout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL, |
| 1009 | .finalLayout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL, |
| 1010 | }, |
Chia-I Wu | 76cd422 | 2015-07-08 13:34:24 +0800 | [diff] [blame] | 1011 | }; |
Chia-I Wu | c278df8 | 2015-07-07 11:50:03 +0800 | [diff] [blame] | 1012 | const VkAttachmentReference color_reference = { |
| 1013 | .attachment = 0, |
| 1014 | .layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL, |
| 1015 | }; |
| 1016 | const VkSubpassDescription subpass = { |
| 1017 | .sType = VK_STRUCTURE_TYPE_SUBPASS_DESCRIPTION, |
| 1018 | .pNext = NULL, |
| 1019 | .pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS, |
| 1020 | .flags = 0, |
| 1021 | .inputCount = 0, |
| 1022 | .inputAttachments = NULL, |
| 1023 | .colorCount = 1, |
| 1024 | .colorAttachments = &color_reference, |
| 1025 | .resolveAttachments = NULL, |
| 1026 | .depthStencilAttachment = { |
| 1027 | .attachment = 1, |
| 1028 | .layout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL, |
| 1029 | }, |
| 1030 | .preserveCount = 0, |
| 1031 | .preserveAttachments = NULL, |
| 1032 | }; |
Chia-I Wu | 76cd422 | 2015-07-08 13:34:24 +0800 | [diff] [blame] | 1033 | const VkRenderPassCreateInfo rp_info = { |
| 1034 | .sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO, |
| 1035 | .pNext = NULL, |
Chia-I Wu | c278df8 | 2015-07-07 11:50:03 +0800 | [diff] [blame] | 1036 | .attachmentCount = 2, |
| 1037 | .pAttachments = attachments, |
| 1038 | .subpassCount = 1, |
| 1039 | .pSubpasses = &subpass, |
| 1040 | .dependencyCount = 0, |
| 1041 | .pDependencies = NULL, |
Chia-I Wu | 76cd422 | 2015-07-08 13:34:24 +0800 | [diff] [blame] | 1042 | }; |
Chia-I Wu | c278df8 | 2015-07-07 11:50:03 +0800 | [diff] [blame] | 1043 | VkResult U_ASSERT_ONLY err; |
Chia-I Wu | 76cd422 | 2015-07-08 13:34:24 +0800 | [diff] [blame] | 1044 | |
| 1045 | err = vkCreateRenderPass(demo->device, &rp_info, &demo->render_pass); |
| 1046 | assert(!err); |
| 1047 | } |
| 1048 | |
Courtney Goeltzenleuchter | 382489d | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 1049 | static VkShader demo_prepare_shader(struct demo *demo, |
Tony Barbour | 8205d90 | 2015-04-16 15:59:00 -0600 | [diff] [blame] | 1050 | VkShaderStage stage, |
Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 1051 | const void *code, |
Mark Lobodzinski | e2d07a5 | 2015-01-29 08:55:56 -0600 | [diff] [blame] | 1052 | size_t size) |
Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 1053 | { |
Courtney Goeltzenleuchter | 2d034fd | 2015-06-28 13:01:17 -0600 | [diff] [blame] | 1054 | VkShaderModuleCreateInfo moduleCreateInfo; |
| 1055 | VkShaderCreateInfo shaderCreateInfo; |
| 1056 | VkShaderModule shaderModule; |
Courtney Goeltzenleuchter | 382489d | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 1057 | VkShader shader; |
| 1058 | VkResult err; |
Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 1059 | |
Courtney Goeltzenleuchter | 2d034fd | 2015-06-28 13:01:17 -0600 | [diff] [blame] | 1060 | |
| 1061 | moduleCreateInfo.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO; |
| 1062 | moduleCreateInfo.pNext = NULL; |
| 1063 | |
| 1064 | shaderCreateInfo.sType = VK_STRUCTURE_TYPE_SHADER_CREATE_INFO; |
| 1065 | shaderCreateInfo.pNext = NULL; |
Courtney Goeltzenleuchter | 3f2606d | 2014-10-13 17:51:58 -0600 | [diff] [blame] | 1066 | |
Cody Northrop | 75db032 | 2015-05-28 11:27:16 -0600 | [diff] [blame] | 1067 | if (!demo->use_glsl) { |
Courtney Goeltzenleuchter | 2d034fd | 2015-06-28 13:01:17 -0600 | [diff] [blame] | 1068 | moduleCreateInfo.codeSize = size; |
| 1069 | moduleCreateInfo.pCode = code; |
| 1070 | moduleCreateInfo.flags = 0; |
| 1071 | err = vkCreateShaderModule(demo->device, &moduleCreateInfo, &shaderModule); |
| 1072 | if (err) { |
| 1073 | free((void *) moduleCreateInfo.pCode); |
| 1074 | } |
Courtney Goeltzenleuchter | 3f2606d | 2014-10-13 17:51:58 -0600 | [diff] [blame] | 1075 | |
Courtney Goeltzenleuchter | 2d034fd | 2015-06-28 13:01:17 -0600 | [diff] [blame] | 1076 | shaderCreateInfo.flags = 0; |
| 1077 | shaderCreateInfo.module = shaderModule; |
Piers Daniell | 1cf7fe1 | 2015-07-16 09:35:35 -0600 | [diff] [blame^] | 1078 | shaderCreateInfo.pName = "main"; |
Courtney Goeltzenleuchter | 2d034fd | 2015-06-28 13:01:17 -0600 | [diff] [blame] | 1079 | err = vkCreateShader(demo->device, &shaderCreateInfo, &shader); |
Cody Northrop | 75db032 | 2015-05-28 11:27:16 -0600 | [diff] [blame] | 1080 | } else { |
| 1081 | // Create fake SPV structure to feed GLSL |
| 1082 | // to the driver "under the covers" |
Courtney Goeltzenleuchter | 2d034fd | 2015-06-28 13:01:17 -0600 | [diff] [blame] | 1083 | moduleCreateInfo.codeSize = 3 * sizeof(uint32_t) + size + 1; |
| 1084 | moduleCreateInfo.pCode = malloc(moduleCreateInfo.codeSize); |
| 1085 | moduleCreateInfo.flags = 0; |
Courtney Goeltzenleuchter | 3f2606d | 2014-10-13 17:51:58 -0600 | [diff] [blame] | 1086 | |
Cody Northrop | 75db032 | 2015-05-28 11:27:16 -0600 | [diff] [blame] | 1087 | /* try version 0 first: VkShaderStage followed by GLSL */ |
Courtney Goeltzenleuchter | 2d034fd | 2015-06-28 13:01:17 -0600 | [diff] [blame] | 1088 | ((uint32_t *) moduleCreateInfo.pCode)[0] = ICD_SPV_MAGIC; |
| 1089 | ((uint32_t *) moduleCreateInfo.pCode)[1] = 0; |
| 1090 | ((uint32_t *) moduleCreateInfo.pCode)[2] = stage; |
| 1091 | memcpy(((uint32_t *) moduleCreateInfo.pCode + 3), code, size + 1); |
Cody Northrop | 75db032 | 2015-05-28 11:27:16 -0600 | [diff] [blame] | 1092 | |
Courtney Goeltzenleuchter | 2d034fd | 2015-06-28 13:01:17 -0600 | [diff] [blame] | 1093 | err = vkCreateShaderModule(demo->device, &moduleCreateInfo, &shaderModule); |
Cody Northrop | 75db032 | 2015-05-28 11:27:16 -0600 | [diff] [blame] | 1094 | if (err) { |
Courtney Goeltzenleuchter | 2d034fd | 2015-06-28 13:01:17 -0600 | [diff] [blame] | 1095 | free((void *) moduleCreateInfo.pCode); |
Cody Northrop | 75db032 | 2015-05-28 11:27:16 -0600 | [diff] [blame] | 1096 | } |
Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 1097 | |
Courtney Goeltzenleuchter | 2d034fd | 2015-06-28 13:01:17 -0600 | [diff] [blame] | 1098 | shaderCreateInfo.flags = 0; |
| 1099 | shaderCreateInfo.module = shaderModule; |
Piers Daniell | 1cf7fe1 | 2015-07-16 09:35:35 -0600 | [diff] [blame^] | 1100 | shaderCreateInfo.pName = "main"; |
Courtney Goeltzenleuchter | 2d034fd | 2015-06-28 13:01:17 -0600 | [diff] [blame] | 1101 | err = vkCreateShader(demo->device, &shaderCreateInfo, &shader); |
| 1102 | } |
Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 1103 | return shader; |
| 1104 | } |
| 1105 | |
Cody Northrop | 75db032 | 2015-05-28 11:27:16 -0600 | [diff] [blame] | 1106 | char *demo_read_spv(const char *filename, size_t *psize) |
| 1107 | { |
| 1108 | long int size; |
| 1109 | void *shader_code; |
Tony Barbour | 9687cb1 | 2015-07-14 13:34:05 -0600 | [diff] [blame] | 1110 | size_t retVal; |
Cody Northrop | 75db032 | 2015-05-28 11:27:16 -0600 | [diff] [blame] | 1111 | |
| 1112 | FILE *fp = fopen(filename, "rb"); |
| 1113 | if (!fp) return NULL; |
| 1114 | |
| 1115 | fseek(fp, 0L, SEEK_END); |
| 1116 | size = ftell(fp); |
| 1117 | |
| 1118 | fseek(fp, 0L, SEEK_SET); |
| 1119 | |
| 1120 | shader_code = malloc(size); |
Tony Barbour | 9687cb1 | 2015-07-14 13:34:05 -0600 | [diff] [blame] | 1121 | retVal = fread(shader_code, size, 1, fp); |
| 1122 | if (!retVal) return NULL; |
Cody Northrop | 75db032 | 2015-05-28 11:27:16 -0600 | [diff] [blame] | 1123 | |
| 1124 | *psize = size; |
| 1125 | |
| 1126 | return shader_code; |
| 1127 | } |
| 1128 | |
Courtney Goeltzenleuchter | 382489d | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 1129 | static VkShader demo_prepare_vs(struct demo *demo) |
Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 1130 | { |
Cody Northrop | 75db032 | 2015-05-28 11:27:16 -0600 | [diff] [blame] | 1131 | if (!demo->use_glsl) { |
| 1132 | void *vertShaderCode; |
| 1133 | size_t size; |
| 1134 | |
| 1135 | vertShaderCode = demo_read_spv("tri-vert.spv", &size); |
| 1136 | |
| 1137 | return demo_prepare_shader(demo, VK_SHADER_STAGE_VERTEX, |
| 1138 | vertShaderCode, size); |
| 1139 | } else { |
| 1140 | static const char *vertShaderText = |
Mark Lobodzinski | ba4d2f0 | 2015-04-06 15:24:40 -0500 | [diff] [blame] | 1141 | "#version 140\n" |
Courtney Goeltzenleuchter | f5cdad0 | 2015-03-31 16:36:30 -0600 | [diff] [blame] | 1142 | "#extension GL_ARB_separate_shader_objects : enable\n" |
| 1143 | "#extension GL_ARB_shading_language_420pack : enable\n" |
| 1144 | "layout (location = 0) in vec4 pos;\n" |
| 1145 | "layout (location = 1) in vec2 attr;\n" |
Chia-I Wu | f5caeb0 | 2014-10-25 12:11:27 +0800 | [diff] [blame] | 1146 | "out vec2 texcoord;\n" |
Courtney Goeltzenleuchter | 3f2606d | 2014-10-13 17:51:58 -0600 | [diff] [blame] | 1147 | "void main() {\n" |
Chia-I Wu | f5caeb0 | 2014-10-25 12:11:27 +0800 | [diff] [blame] | 1148 | " texcoord = attr;\n" |
| 1149 | " gl_Position = pos;\n" |
Courtney Goeltzenleuchter | 3f2606d | 2014-10-13 17:51:58 -0600 | [diff] [blame] | 1150 | "}\n"; |
Courtney Goeltzenleuchter | ef7301b | 2014-09-17 13:17:12 -0600 | [diff] [blame] | 1151 | |
Cody Northrop | 75db032 | 2015-05-28 11:27:16 -0600 | [diff] [blame] | 1152 | return demo_prepare_shader(demo, VK_SHADER_STAGE_VERTEX, |
| 1153 | (const void *) vertShaderText, |
| 1154 | strlen(vertShaderText)); |
| 1155 | } |
Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 1156 | } |
| 1157 | |
Courtney Goeltzenleuchter | 382489d | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 1158 | static VkShader demo_prepare_fs(struct demo *demo) |
Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 1159 | { |
Cody Northrop | 75db032 | 2015-05-28 11:27:16 -0600 | [diff] [blame] | 1160 | if (!demo->use_glsl) { |
| 1161 | void *fragShaderCode; |
| 1162 | size_t size; |
Courtney Goeltzenleuchter | ef7301b | 2014-09-17 13:17:12 -0600 | [diff] [blame] | 1163 | |
Cody Northrop | 75db032 | 2015-05-28 11:27:16 -0600 | [diff] [blame] | 1164 | fragShaderCode = demo_read_spv("tri-frag.spv", &size); |
| 1165 | |
| 1166 | return demo_prepare_shader(demo, VK_SHADER_STAGE_FRAGMENT, |
| 1167 | fragShaderCode, size); |
| 1168 | } else { |
| 1169 | static const char *fragShaderText = |
| 1170 | "#version 140\n" |
| 1171 | "#extension GL_ARB_separate_shader_objects : enable\n" |
| 1172 | "#extension GL_ARB_shading_language_420pack : enable\n" |
| 1173 | "layout (binding = 0) uniform sampler2D tex;\n" |
| 1174 | "layout (location = 0) in vec2 texcoord;\n" |
| 1175 | "layout (location = 0) out vec4 uFragColor;\n" |
| 1176 | "void main() {\n" |
| 1177 | " uFragColor = texture(tex, texcoord);\n" |
| 1178 | "}\n"; |
| 1179 | |
| 1180 | return demo_prepare_shader(demo, VK_SHADER_STAGE_FRAGMENT, |
| 1181 | (const void *) fragShaderText, |
| 1182 | strlen(fragShaderText)); |
| 1183 | } |
Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 1184 | } |
| 1185 | |
| 1186 | static void demo_prepare_pipeline(struct demo *demo) |
| 1187 | { |
Courtney Goeltzenleuchter | 382489d | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 1188 | VkGraphicsPipelineCreateInfo pipeline; |
Jon Ashburn | 0d60d27 | 2015-07-09 15:02:25 -0600 | [diff] [blame] | 1189 | VkPipelineCacheCreateInfo pipelineCache; |
Mark Lobodzinski | 0e0fb5c | 2015-06-23 15:11:57 -0600 | [diff] [blame] | 1190 | |
Tony Barbour | e307f58 | 2015-07-10 15:29:03 -0600 | [diff] [blame] | 1191 | VkPipelineVertexInputStateCreateInfo vi; |
| 1192 | VkPipelineInputAssemblyStateCreateInfo ia; |
| 1193 | VkPipelineRasterStateCreateInfo rs; |
| 1194 | VkPipelineColorBlendStateCreateInfo cb; |
| 1195 | VkPipelineDepthStencilStateCreateInfo ds; |
| 1196 | VkPipelineViewportStateCreateInfo vp; |
| 1197 | VkPipelineMultisampleStateCreateInfo ms; |
Mark Lobodzinski | 0e0fb5c | 2015-06-23 15:11:57 -0600 | [diff] [blame] | 1198 | |
Tony Barbour | 22a3086 | 2015-04-22 09:02:32 -0600 | [diff] [blame] | 1199 | VkResult U_ASSERT_ONLY err; |
Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 1200 | |
| 1201 | memset(&pipeline, 0, sizeof(pipeline)); |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1202 | pipeline.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO; |
Mark Lobodzinski | 556f721 | 2015-04-17 14:11:39 -0500 | [diff] [blame] | 1203 | pipeline.layout = demo->pipeline_layout; |
Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 1204 | |
Chia-I Wu | 8d29d02 | 2014-10-08 12:14:39 +0800 | [diff] [blame] | 1205 | vi = demo->vertices.vi; |
| 1206 | |
Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 1207 | memset(&ia, 0, sizeof(ia)); |
Tony Barbour | e307f58 | 2015-07-10 15:29:03 -0600 | [diff] [blame] | 1208 | ia.sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO; |
Tony Barbour | 8205d90 | 2015-04-16 15:59:00 -0600 | [diff] [blame] | 1209 | ia.topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST; |
Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 1210 | |
| 1211 | memset(&rs, 0, sizeof(rs)); |
Tony Barbour | e307f58 | 2015-07-10 15:29:03 -0600 | [diff] [blame] | 1212 | rs.sType = VK_STRUCTURE_TYPE_PIPELINE_RASTER_STATE_CREATE_INFO; |
Tony Barbour | 8205d90 | 2015-04-16 15:59:00 -0600 | [diff] [blame] | 1213 | rs.fillMode = VK_FILL_MODE_SOLID; |
Chia-I Wu | c414ba8 | 2015-04-22 15:44:24 +0800 | [diff] [blame] | 1214 | rs.cullMode = VK_CULL_MODE_BACK; |
| 1215 | rs.frontFace = VK_FRONT_FACE_CW; |
Chia-I Wu | e2504cb | 2015-04-22 14:20:52 +0800 | [diff] [blame] | 1216 | rs.depthClipEnable = VK_TRUE; |
Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 1217 | |
| 1218 | memset(&cb, 0, sizeof(cb)); |
Tony Barbour | e307f58 | 2015-07-10 15:29:03 -0600 | [diff] [blame] | 1219 | cb.sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO; |
| 1220 | VkPipelineColorBlendAttachmentState att_state[1]; |
Tony Barbour | fa6cac7 | 2015-01-16 14:27:35 -0700 | [diff] [blame] | 1221 | memset(att_state, 0, sizeof(att_state)); |
Tony Barbour | fa6cac7 | 2015-01-16 14:27:35 -0700 | [diff] [blame] | 1222 | att_state[0].channelWriteMask = 0xf; |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1223 | att_state[0].blendEnable = VK_FALSE; |
Tony Barbour | fa6cac7 | 2015-01-16 14:27:35 -0700 | [diff] [blame] | 1224 | cb.attachmentCount = 1; |
| 1225 | cb.pAttachments = att_state; |
Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 1226 | |
Tony Barbour | fa6cac7 | 2015-01-16 14:27:35 -0700 | [diff] [blame] | 1227 | memset(&vp, 0, sizeof(vp)); |
Tony Barbour | e307f58 | 2015-07-10 15:29:03 -0600 | [diff] [blame] | 1228 | vp.sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO; |
Tony Barbour | 8205d90 | 2015-04-16 15:59:00 -0600 | [diff] [blame] | 1229 | vp.viewportCount = 1; |
Tony Barbour | fa6cac7 | 2015-01-16 14:27:35 -0700 | [diff] [blame] | 1230 | |
| 1231 | memset(&ds, 0, sizeof(ds)); |
Tony Barbour | e307f58 | 2015-07-10 15:29:03 -0600 | [diff] [blame] | 1232 | ds.sType = VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO; |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1233 | ds.depthTestEnable = VK_TRUE; |
| 1234 | ds.depthWriteEnable = VK_TRUE; |
Tony Barbour | 8205d90 | 2015-04-16 15:59:00 -0600 | [diff] [blame] | 1235 | ds.depthCompareOp = VK_COMPARE_OP_LESS_EQUAL; |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1236 | ds.depthBoundsEnable = VK_FALSE; |
| 1237 | ds.back.stencilFailOp = VK_STENCIL_OP_KEEP; |
| 1238 | ds.back.stencilPassOp = VK_STENCIL_OP_KEEP; |
Tony Barbour | 8205d90 | 2015-04-16 15:59:00 -0600 | [diff] [blame] | 1239 | ds.back.stencilCompareOp = VK_COMPARE_OP_ALWAYS; |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1240 | ds.stencilTestEnable = VK_FALSE; |
Tony Barbour | fa6cac7 | 2015-01-16 14:27:35 -0700 | [diff] [blame] | 1241 | ds.front = ds.back; |
Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 1242 | |
Tony Barbour | fa6cac7 | 2015-01-16 14:27:35 -0700 | [diff] [blame] | 1243 | memset(&ms, 0, sizeof(ms)); |
Tony Barbour | e307f58 | 2015-07-10 15:29:03 -0600 | [diff] [blame] | 1244 | ms.sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO; |
Tony Barbour | fa6cac7 | 2015-01-16 14:27:35 -0700 | [diff] [blame] | 1245 | ms.sampleMask = 1; |
Tony Barbour | e094edf | 2015-06-26 10:18:34 -0600 | [diff] [blame] | 1246 | ms.rasterSamples = 1; |
Chia-I Wu | b043fe3 | 2014-10-06 15:30:33 +0800 | [diff] [blame] | 1247 | |
Mark Lobodzinski | 0e0fb5c | 2015-06-23 15:11:57 -0600 | [diff] [blame] | 1248 | // Two stages: vs and fs |
| 1249 | pipeline.stageCount = 2; |
| 1250 | VkPipelineShaderStageCreateInfo shaderStages[2]; |
| 1251 | memset(&shaderStages, 0, 2 * sizeof(VkPipelineShaderStageCreateInfo)); |
| 1252 | |
| 1253 | shaderStages[0].sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO; |
| 1254 | shaderStages[0].stage = VK_SHADER_STAGE_VERTEX; |
| 1255 | shaderStages[0].shader = demo_prepare_vs(demo); |
Mark Lobodzinski | 0e0fb5c | 2015-06-23 15:11:57 -0600 | [diff] [blame] | 1256 | |
| 1257 | shaderStages[1].sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO; |
| 1258 | shaderStages[1].stage = VK_SHADER_STAGE_FRAGMENT; |
| 1259 | shaderStages[1].shader = demo_prepare_fs(demo); |
| 1260 | |
Tony Barbour | e307f58 | 2015-07-10 15:29:03 -0600 | [diff] [blame] | 1261 | pipeline.pVertexInputState = &vi; |
| 1262 | pipeline.pInputAssemblyState = &ia; |
| 1263 | pipeline.pRasterState = &rs; |
| 1264 | pipeline.pColorBlendState = &cb; |
| 1265 | pipeline.pMultisampleState = &ms; |
| 1266 | pipeline.pViewportState = &vp; |
| 1267 | pipeline.pDepthStencilState = &ds; |
| 1268 | pipeline.pStages = shaderStages; |
Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 1269 | |
Jon Ashburn | 0d60d27 | 2015-07-09 15:02:25 -0600 | [diff] [blame] | 1270 | memset(&pipelineCache, 0, sizeof(pipelineCache)); |
| 1271 | pipelineCache.sType = VK_STRUCTURE_TYPE_PIPELINE_CACHE_CREATE_INFO; |
| 1272 | |
| 1273 | err = vkCreatePipelineCache(demo->device, &pipelineCache, &demo->pipelineCache); |
| 1274 | assert(!err); |
| 1275 | err = vkCreateGraphicsPipelines(demo->device, demo->pipelineCache, 1, &pipeline, &demo->pipeline); |
Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 1276 | assert(!err); |
| 1277 | |
Mark Lobodzinski | 0e0fb5c | 2015-06-23 15:11:57 -0600 | [diff] [blame] | 1278 | for (uint32_t i = 0; i < pipeline.stageCount; i++) { |
Tony Barbour | de4124d | 2015-07-03 10:33:54 -0600 | [diff] [blame] | 1279 | vkDestroyShader(demo->device, shaderStages[i].shader); |
Mark Lobodzinski | 0e0fb5c | 2015-06-23 15:11:57 -0600 | [diff] [blame] | 1280 | } |
Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 1281 | } |
| 1282 | |
| 1283 | static void demo_prepare_dynamic_states(struct demo *demo) |
| 1284 | { |
Tony Barbour | de4124d | 2015-07-03 10:33:54 -0600 | [diff] [blame] | 1285 | VkDynamicViewportStateCreateInfo viewport_create; |
| 1286 | VkDynamicRasterStateCreateInfo raster; |
| 1287 | VkDynamicColorBlendStateCreateInfo color_blend; |
| 1288 | VkDynamicDepthStencilStateCreateInfo depth_stencil; |
Tony Barbour | 22a3086 | 2015-04-22 09:02:32 -0600 | [diff] [blame] | 1289 | VkResult U_ASSERT_ONLY err; |
Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 1290 | |
Tony Barbour | fa6cac7 | 2015-01-16 14:27:35 -0700 | [diff] [blame] | 1291 | memset(&viewport_create, 0, sizeof(viewport_create)); |
Tony Barbour | de4124d | 2015-07-03 10:33:54 -0600 | [diff] [blame] | 1292 | viewport_create.sType = VK_STRUCTURE_TYPE_DYNAMIC_VIEWPORT_STATE_CREATE_INFO; |
Courtney Goeltzenleuchter | c6e32f9 | 2015-02-11 14:13:34 -0700 | [diff] [blame] | 1293 | viewport_create.viewportAndScissorCount = 1; |
Courtney Goeltzenleuchter | 382489d | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 1294 | VkViewport viewport; |
Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 1295 | memset(&viewport, 0, sizeof(viewport)); |
Mark Lobodzinski | e2d07a5 | 2015-01-29 08:55:56 -0600 | [diff] [blame] | 1296 | viewport.height = (float) demo->height; |
Courtney Goeltzenleuchter | c6e32f9 | 2015-02-11 14:13:34 -0700 | [diff] [blame] | 1297 | viewport.width = (float) demo->width; |
Mark Lobodzinski | e2d07a5 | 2015-01-29 08:55:56 -0600 | [diff] [blame] | 1298 | viewport.minDepth = (float) 0.0f; |
| 1299 | viewport.maxDepth = (float) 1.0f; |
Piers Daniell | 886be47 | 2015-02-23 16:23:13 -0700 | [diff] [blame] | 1300 | viewport_create.pViewports = &viewport; |
Chris Forbes | 2951d7d | 2015-06-22 17:21:59 +1200 | [diff] [blame] | 1301 | VkRect2D scissor; |
Piers Daniell | 886be47 | 2015-02-23 16:23:13 -0700 | [diff] [blame] | 1302 | memset(&scissor, 0, sizeof(scissor)); |
Courtney Goeltzenleuchter | c6e32f9 | 2015-02-11 14:13:34 -0700 | [diff] [blame] | 1303 | scissor.extent.width = demo->width; |
| 1304 | scissor.extent.height = demo->height; |
| 1305 | scissor.offset.x = 0; |
| 1306 | scissor.offset.y = 0; |
Courtney Goeltzenleuchter | c6e32f9 | 2015-02-11 14:13:34 -0700 | [diff] [blame] | 1307 | viewport_create.pScissors = &scissor; |
Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 1308 | |
| 1309 | memset(&raster, 0, sizeof(raster)); |
Tony Barbour | de4124d | 2015-07-03 10:33:54 -0600 | [diff] [blame] | 1310 | raster.sType = VK_STRUCTURE_TYPE_DYNAMIC_RASTER_STATE_CREATE_INFO; |
Piers Daniell | 886be47 | 2015-02-23 16:23:13 -0700 | [diff] [blame] | 1311 | raster.lineWidth = 1.0; |
Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 1312 | |
| 1313 | memset(&color_blend, 0, sizeof(color_blend)); |
Tony Barbour | de4124d | 2015-07-03 10:33:54 -0600 | [diff] [blame] | 1314 | color_blend.sType = VK_STRUCTURE_TYPE_DYNAMIC_COLOR_BLEND_STATE_CREATE_INFO; |
Piers Daniell | 886be47 | 2015-02-23 16:23:13 -0700 | [diff] [blame] | 1315 | color_blend.blendConst[0] = 1.0f; |
| 1316 | color_blend.blendConst[1] = 1.0f; |
| 1317 | color_blend.blendConst[2] = 1.0f; |
| 1318 | color_blend.blendConst[3] = 1.0f; |
Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 1319 | |
| 1320 | memset(&depth_stencil, 0, sizeof(depth_stencil)); |
Tony Barbour | de4124d | 2015-07-03 10:33:54 -0600 | [diff] [blame] | 1321 | depth_stencil.sType = VK_STRUCTURE_TYPE_DYNAMIC_DEPTH_STENCIL_STATE_CREATE_INFO; |
Mark Lobodzinski | 4405fbf | 2015-06-12 11:14:17 -0600 | [diff] [blame] | 1322 | depth_stencil.minDepthBounds = 0.0f; |
| 1323 | depth_stencil.maxDepthBounds = 1.0f; |
Tony Barbour | fa6cac7 | 2015-01-16 14:27:35 -0700 | [diff] [blame] | 1324 | depth_stencil.stencilBackRef = 0; |
| 1325 | depth_stencil.stencilFrontRef = 0; |
| 1326 | depth_stencil.stencilReadMask = 0xff; |
| 1327 | depth_stencil.stencilWriteMask = 0xff; |
Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 1328 | |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1329 | err = vkCreateDynamicViewportState(demo->device, &viewport_create, &demo->viewport); |
Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 1330 | assert(!err); |
| 1331 | |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1332 | err = vkCreateDynamicRasterState(demo->device, &raster, &demo->raster); |
Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 1333 | assert(!err); |
| 1334 | |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1335 | err = vkCreateDynamicColorBlendState(demo->device, |
Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 1336 | &color_blend, &demo->color_blend); |
| 1337 | assert(!err); |
| 1338 | |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1339 | err = vkCreateDynamicDepthStencilState(demo->device, |
Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 1340 | &depth_stencil, &demo->depth_stencil); |
| 1341 | assert(!err); |
| 1342 | } |
| 1343 | |
Chia-I Wu | 8d24b3b | 2015-03-26 13:14:16 +0800 | [diff] [blame] | 1344 | static void demo_prepare_descriptor_pool(struct demo *demo) |
Chia-I Wu | f838506 | 2015-01-04 16:27:24 +0800 | [diff] [blame] | 1345 | { |
Courtney Goeltzenleuchter | 382489d | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 1346 | const VkDescriptorTypeCount type_count = { |
Courtney Goeltzenleuchter | ad87081 | 2015-04-15 15:29:59 -0600 | [diff] [blame] | 1347 | .type = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, |
Chia-I Wu | f838506 | 2015-01-04 16:27:24 +0800 | [diff] [blame] | 1348 | .count = DEMO_TEXTURE_COUNT, |
| 1349 | }; |
Courtney Goeltzenleuchter | 382489d | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 1350 | const VkDescriptorPoolCreateInfo descriptor_pool = { |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1351 | .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO, |
Chia-I Wu | f838506 | 2015-01-04 16:27:24 +0800 | [diff] [blame] | 1352 | .pNext = NULL, |
| 1353 | .count = 1, |
| 1354 | .pTypeCount = &type_count, |
| 1355 | }; |
Tony Barbour | 22a3086 | 2015-04-22 09:02:32 -0600 | [diff] [blame] | 1356 | VkResult U_ASSERT_ONLY err; |
Chia-I Wu | f838506 | 2015-01-04 16:27:24 +0800 | [diff] [blame] | 1357 | |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1358 | err = vkCreateDescriptorPool(demo->device, |
| 1359 | VK_DESCRIPTOR_POOL_USAGE_ONE_SHOT, 1, |
Chia-I Wu | 8d24b3b | 2015-03-26 13:14:16 +0800 | [diff] [blame] | 1360 | &descriptor_pool, &demo->desc_pool); |
Chia-I Wu | f838506 | 2015-01-04 16:27:24 +0800 | [diff] [blame] | 1361 | assert(!err); |
| 1362 | } |
| 1363 | |
| 1364 | static void demo_prepare_descriptor_set(struct demo *demo) |
| 1365 | { |
Chia-I Wu | 8cd8ecd | 2015-05-25 16:27:55 +0800 | [diff] [blame] | 1366 | VkDescriptorInfo tex_descs[DEMO_TEXTURE_COUNT]; |
| 1367 | VkWriteDescriptorSet write; |
Tony Barbour | 22a3086 | 2015-04-22 09:02:32 -0600 | [diff] [blame] | 1368 | VkResult U_ASSERT_ONLY err; |
Chia-I Wu | f838506 | 2015-01-04 16:27:24 +0800 | [diff] [blame] | 1369 | uint32_t count; |
Mark Lobodzinski | e2d07a5 | 2015-01-29 08:55:56 -0600 | [diff] [blame] | 1370 | uint32_t i; |
Chia-I Wu | f838506 | 2015-01-04 16:27:24 +0800 | [diff] [blame] | 1371 | |
Mike Stroyan | 230e625 | 2015-04-17 12:36:38 -0600 | [diff] [blame] | 1372 | err = vkAllocDescriptorSets(demo->device, demo->desc_pool, |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1373 | VK_DESCRIPTOR_SET_USAGE_STATIC, |
Chia-I Wu | f838506 | 2015-01-04 16:27:24 +0800 | [diff] [blame] | 1374 | 1, &demo->desc_layout, |
| 1375 | &demo->desc_set, &count); |
| 1376 | assert(!err && count == 1); |
| 1377 | |
Chia-I Wu | 8cd8ecd | 2015-05-25 16:27:55 +0800 | [diff] [blame] | 1378 | memset(&tex_descs, 0, sizeof(tex_descs)); |
| 1379 | for (i = 0; i < DEMO_TEXTURE_COUNT; i++) { |
| 1380 | tex_descs[i].sampler = demo->textures[i].sampler; |
| 1381 | tex_descs[i].imageView = demo->textures[i].view; |
| 1382 | tex_descs[i].imageLayout = VK_IMAGE_LAYOUT_GENERAL; |
| 1383 | } |
| 1384 | |
| 1385 | memset(&write, 0, sizeof(write)); |
| 1386 | write.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET; |
| 1387 | write.destSet = demo->desc_set; |
| 1388 | write.count = DEMO_TEXTURE_COUNT; |
| 1389 | write.descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER; |
| 1390 | write.pDescriptors = tex_descs; |
| 1391 | |
| 1392 | err = vkUpdateDescriptorSets(demo->device, 1, &write, 0, NULL); |
| 1393 | assert(!err); |
Chia-I Wu | f838506 | 2015-01-04 16:27:24 +0800 | [diff] [blame] | 1394 | } |
| 1395 | |
Chia-I Wu | 76cd422 | 2015-07-08 13:34:24 +0800 | [diff] [blame] | 1396 | static void demo_prepare_framebuffers(struct demo *demo) |
| 1397 | { |
Chia-I Wu | c278df8 | 2015-07-07 11:50:03 +0800 | [diff] [blame] | 1398 | VkAttachmentBindInfo attachments[2] = { |
| 1399 | [0] = { |
Tony Barbour | de4124d | 2015-07-03 10:33:54 -0600 | [diff] [blame] | 1400 | .view.handle = VK_NULL_HANDLE, |
Chia-I Wu | c278df8 | 2015-07-07 11:50:03 +0800 | [diff] [blame] | 1401 | .layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL, |
| 1402 | }, |
| 1403 | [1] = { |
| 1404 | .view = demo->depth.view, |
| 1405 | .layout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL, |
| 1406 | }, |
Chia-I Wu | 76cd422 | 2015-07-08 13:34:24 +0800 | [diff] [blame] | 1407 | }; |
| 1408 | const VkFramebufferCreateInfo fb_info = { |
| 1409 | .sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO, |
| 1410 | .pNext = NULL, |
Chia-I Wu | c278df8 | 2015-07-07 11:50:03 +0800 | [diff] [blame] | 1411 | .renderPass = demo->render_pass, |
| 1412 | .attachmentCount = 2, |
| 1413 | .pAttachments = attachments, |
Chia-I Wu | 76cd422 | 2015-07-08 13:34:24 +0800 | [diff] [blame] | 1414 | .width = demo->width, |
| 1415 | .height = demo->height, |
| 1416 | .layers = 1, |
| 1417 | }; |
| 1418 | VkResult U_ASSERT_ONLY err; |
| 1419 | uint32_t i; |
| 1420 | |
| 1421 | for (i = 0; i < DEMO_BUFFER_COUNT; i++) { |
Chia-I Wu | c278df8 | 2015-07-07 11:50:03 +0800 | [diff] [blame] | 1422 | attachments[0].view = demo->buffers[i].view; |
Chia-I Wu | 76cd422 | 2015-07-08 13:34:24 +0800 | [diff] [blame] | 1423 | err = vkCreateFramebuffer(demo->device, &fb_info, &demo->framebuffers[i]); |
| 1424 | assert(!err); |
| 1425 | } |
| 1426 | } |
| 1427 | |
Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 1428 | static void demo_prepare(struct demo *demo) |
| 1429 | { |
Cody Northrop | 18ea11b | 2015-07-09 18:08:32 -0600 | [diff] [blame] | 1430 | VkResult U_ASSERT_ONLY err; |
| 1431 | |
| 1432 | const VkCmdPoolCreateInfo cmd_pool_info = { |
| 1433 | .sType = VK_STRUCTURE_TYPE_CMD_POOL_CREATE_INFO, |
| 1434 | .pNext = NULL, |
| 1435 | .queueFamilyIndex = demo->graphics_queue_node_index, |
| 1436 | .flags = 0, |
| 1437 | }; |
| 1438 | err = vkCreateCommandPool(demo->device, &cmd_pool_info, &demo->cmd_pool); |
| 1439 | assert(!err); |
| 1440 | |
Courtney Goeltzenleuchter | 382489d | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 1441 | const VkCmdBufferCreateInfo cmd = { |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1442 | .sType = VK_STRUCTURE_TYPE_CMD_BUFFER_CREATE_INFO, |
Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 1443 | .pNext = NULL, |
Cody Northrop | 18ea11b | 2015-07-09 18:08:32 -0600 | [diff] [blame] | 1444 | .cmdPool = demo->cmd_pool, |
Chia-I Wu | 88eaa3b | 2015-06-26 15:34:39 +0800 | [diff] [blame] | 1445 | .level = VK_CMD_BUFFER_LEVEL_PRIMARY, |
Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 1446 | .flags = 0, |
| 1447 | }; |
Courtney Goeltzenleuchter | 633f9c5 | 2015-04-30 10:58:33 -0600 | [diff] [blame] | 1448 | err = vkCreateCommandBuffer(demo->device, &cmd, &demo->draw_cmd); |
| 1449 | assert(!err); |
| 1450 | |
Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 1451 | demo_prepare_buffers(demo); |
Chia-I Wu | 9ae87c9 | 2014-10-07 14:15:01 +0800 | [diff] [blame] | 1452 | demo_prepare_depth(demo); |
Chia-I Wu | b043fe3 | 2014-10-06 15:30:33 +0800 | [diff] [blame] | 1453 | demo_prepare_textures(demo); |
Chia-I Wu | 99621bc | 2014-10-08 11:52:22 +0800 | [diff] [blame] | 1454 | demo_prepare_vertices(demo); |
Chia-I Wu | f838506 | 2015-01-04 16:27:24 +0800 | [diff] [blame] | 1455 | demo_prepare_descriptor_layout(demo); |
Chia-I Wu | 76cd422 | 2015-07-08 13:34:24 +0800 | [diff] [blame] | 1456 | demo_prepare_render_pass(demo); |
Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 1457 | demo_prepare_pipeline(demo); |
| 1458 | demo_prepare_dynamic_states(demo); |
| 1459 | |
Chia-I Wu | 8d24b3b | 2015-03-26 13:14:16 +0800 | [diff] [blame] | 1460 | demo_prepare_descriptor_pool(demo); |
Chia-I Wu | f838506 | 2015-01-04 16:27:24 +0800 | [diff] [blame] | 1461 | demo_prepare_descriptor_set(demo); |
Chia-I Wu | 76cd422 | 2015-07-08 13:34:24 +0800 | [diff] [blame] | 1462 | |
| 1463 | demo_prepare_framebuffers(demo); |
| 1464 | |
Courtney Goeltzenleuchter | 633f9c5 | 2015-04-30 10:58:33 -0600 | [diff] [blame] | 1465 | demo->prepared = true; |
Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 1466 | } |
| 1467 | |
Ian Elliott | e14e9f9 | 2015-04-16 15:23:05 -0600 | [diff] [blame] | 1468 | #ifdef _WIN32 |
| 1469 | static void demo_run(struct demo *demo) |
| 1470 | { |
Courtney Goeltzenleuchter | 633f9c5 | 2015-04-30 10:58:33 -0600 | [diff] [blame] | 1471 | if (!demo->prepared) |
| 1472 | return; |
Ian Elliott | e14e9f9 | 2015-04-16 15:23:05 -0600 | [diff] [blame] | 1473 | demo_draw(demo); |
| 1474 | } |
| 1475 | |
| 1476 | // On MS-Windows, make this a global, so it's available to WndProc() |
| 1477 | struct demo demo; |
| 1478 | |
| 1479 | // MS-Windows event handling function: |
| 1480 | LRESULT CALLBACK WndProc(HWND hWnd, |
| 1481 | UINT uMsg, |
| 1482 | WPARAM wParam, |
| 1483 | LPARAM lParam) |
| 1484 | { |
Ian Elliott | 4e19ed0 | 2015-04-28 10:52:52 -0600 | [diff] [blame] | 1485 | char tmp_str[] = APP_LONG_NAME; |
Ian Elliott | e14e9f9 | 2015-04-16 15:23:05 -0600 | [diff] [blame] | 1486 | |
| 1487 | switch(uMsg) |
| 1488 | { |
Ian Elliott | e36b208 | 2015-07-06 14:27:58 -0600 | [diff] [blame] | 1489 | case WM_CREATE: |
Ian Elliott | e14e9f9 | 2015-04-16 15:23:05 -0600 | [diff] [blame] | 1490 | return 0; |
Ian Elliott | e36b208 | 2015-07-06 14:27:58 -0600 | [diff] [blame] | 1491 | case WM_CLOSE: |
Ian Elliott | e14e9f9 | 2015-04-16 15:23:05 -0600 | [diff] [blame] | 1492 | PostQuitMessage(0); |
| 1493 | return 0; |
Ian Elliott | e36b208 | 2015-07-06 14:27:58 -0600 | [diff] [blame] | 1494 | case WM_PAINT: |
Ian Elliott | e14e9f9 | 2015-04-16 15:23:05 -0600 | [diff] [blame] | 1495 | demo_run(&demo); |
| 1496 | return 0; |
| 1497 | default: |
| 1498 | break; |
| 1499 | } |
| 1500 | return (DefWindowProc(hWnd, uMsg, wParam, lParam)); |
| 1501 | } |
| 1502 | |
| 1503 | static void demo_create_window(struct demo *demo) |
| 1504 | { |
| 1505 | WNDCLASSEX win_class; |
| 1506 | |
| 1507 | // Initialize the window class structure: |
| 1508 | win_class.cbSize = sizeof(WNDCLASSEX); |
| 1509 | win_class.style = CS_HREDRAW | CS_VREDRAW; |
| 1510 | win_class.lpfnWndProc = WndProc; |
| 1511 | win_class.cbClsExtra = 0; |
| 1512 | win_class.cbWndExtra = 0; |
| 1513 | win_class.hInstance = demo->connection; // hInstance |
| 1514 | win_class.hIcon = LoadIcon(NULL, IDI_APPLICATION); |
| 1515 | win_class.hCursor = LoadCursor(NULL, IDC_ARROW); |
| 1516 | win_class.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH); |
| 1517 | win_class.lpszMenuName = NULL; |
| 1518 | win_class.lpszClassName = demo->name; |
| 1519 | win_class.hIconSm = LoadIcon(NULL, IDI_WINLOGO); |
| 1520 | // Register window class: |
| 1521 | if (!RegisterClassEx(&win_class)) { |
| 1522 | // It didn't work, so try to give a useful error: |
| 1523 | printf("Unexpected error trying to start the application!\n"); |
| 1524 | fflush(stdout); |
| 1525 | exit(1); |
| 1526 | } |
| 1527 | // Create window with the registered class: |
Mike Stroyan | 7eef574 | 2015-06-15 14:19:19 -0600 | [diff] [blame] | 1528 | RECT wr = { 0, 0, demo->width, demo->height }; |
| 1529 | AdjustWindowRect(&wr, WS_OVERLAPPEDWINDOW, FALSE); |
Ian Elliott | e14e9f9 | 2015-04-16 15:23:05 -0600 | [diff] [blame] | 1530 | demo->window = CreateWindowEx(0, |
| 1531 | demo->name, // class name |
| 1532 | demo->name, // app name |
| 1533 | WS_OVERLAPPEDWINDOW | // window style |
| 1534 | WS_VISIBLE | |
| 1535 | WS_SYSMENU, |
| 1536 | 100,100, // x/y coords |
Mike Stroyan | 7eef574 | 2015-06-15 14:19:19 -0600 | [diff] [blame] | 1537 | wr.right-wr.left, // width |
| 1538 | wr.bottom-wr.top, // height |
Ian Elliott | e14e9f9 | 2015-04-16 15:23:05 -0600 | [diff] [blame] | 1539 | NULL, // handle to parent |
| 1540 | NULL, // handle to menu |
| 1541 | demo->connection, // hInstance |
| 1542 | NULL); // no extra parameters |
| 1543 | if (!demo->window) { |
| 1544 | // It didn't work, so try to give a useful error: |
| 1545 | printf("Cannot create a window in which to draw!\n"); |
| 1546 | fflush(stdout); |
| 1547 | exit(1); |
| 1548 | } |
| 1549 | } |
| 1550 | #else // _WIN32 |
| 1551 | |
Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 1552 | static void demo_handle_event(struct demo *demo, |
| 1553 | const xcb_generic_event_t *event) |
| 1554 | { |
| 1555 | switch (event->response_type & 0x7f) { |
| 1556 | case XCB_EXPOSE: |
| 1557 | demo_draw(demo); |
| 1558 | break; |
Courtney Goeltzenleuchter | ca69805 | 2014-11-07 15:17:03 -0700 | [diff] [blame] | 1559 | case XCB_CLIENT_MESSAGE: |
| 1560 | if((*(xcb_client_message_event_t*)event).data.data32[0] == |
| 1561 | (*demo->atom_wm_delete_window).atom) { |
| 1562 | demo->quit = true; |
| 1563 | } |
| 1564 | break; |
Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 1565 | case XCB_KEY_RELEASE: |
| 1566 | { |
| 1567 | const xcb_key_release_event_t *key = |
| 1568 | (const xcb_key_release_event_t *) event; |
| 1569 | |
| 1570 | if (key->detail == 0x9) |
| 1571 | demo->quit = true; |
| 1572 | } |
| 1573 | break; |
Courtney Goeltzenleuchter | ca69805 | 2014-11-07 15:17:03 -0700 | [diff] [blame] | 1574 | case XCB_DESTROY_NOTIFY: |
| 1575 | demo->quit = true; |
| 1576 | break; |
Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 1577 | default: |
| 1578 | break; |
| 1579 | } |
| 1580 | } |
| 1581 | |
| 1582 | static void demo_run(struct demo *demo) |
| 1583 | { |
| 1584 | xcb_flush(demo->connection); |
| 1585 | |
| 1586 | while (!demo->quit) { |
| 1587 | xcb_generic_event_t *event; |
| 1588 | |
| 1589 | event = xcb_wait_for_event(demo->connection); |
| 1590 | if (event) { |
| 1591 | demo_handle_event(demo, event); |
| 1592 | free(event); |
| 1593 | } |
| 1594 | } |
| 1595 | } |
| 1596 | |
| 1597 | static void demo_create_window(struct demo *demo) |
| 1598 | { |
| 1599 | uint32_t value_mask, value_list[32]; |
| 1600 | |
| 1601 | demo->window = xcb_generate_id(demo->connection); |
| 1602 | |
| 1603 | value_mask = XCB_CW_BACK_PIXEL | XCB_CW_EVENT_MASK; |
| 1604 | value_list[0] = demo->screen->black_pixel; |
| 1605 | value_list[1] = XCB_EVENT_MASK_KEY_RELEASE | |
Courtney Goeltzenleuchter | ca69805 | 2014-11-07 15:17:03 -0700 | [diff] [blame] | 1606 | XCB_EVENT_MASK_EXPOSURE | |
| 1607 | XCB_EVENT_MASK_STRUCTURE_NOTIFY; |
Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 1608 | |
| 1609 | xcb_create_window(demo->connection, |
| 1610 | XCB_COPY_FROM_PARENT, |
| 1611 | demo->window, demo->screen->root, |
| 1612 | 0, 0, demo->width, demo->height, 0, |
| 1613 | XCB_WINDOW_CLASS_INPUT_OUTPUT, |
| 1614 | demo->screen->root_visual, |
| 1615 | value_mask, value_list); |
| 1616 | |
Courtney Goeltzenleuchter | ca69805 | 2014-11-07 15:17:03 -0700 | [diff] [blame] | 1617 | /* Magic code that will send notification when window is destroyed */ |
| 1618 | xcb_intern_atom_cookie_t cookie = xcb_intern_atom(demo->connection, 1, 12, |
| 1619 | "WM_PROTOCOLS"); |
| 1620 | xcb_intern_atom_reply_t* reply = xcb_intern_atom_reply(demo->connection, cookie, 0); |
| 1621 | |
| 1622 | xcb_intern_atom_cookie_t cookie2 = xcb_intern_atom(demo->connection, 0, 16, "WM_DELETE_WINDOW"); |
| 1623 | demo->atom_wm_delete_window = xcb_intern_atom_reply(demo->connection, cookie2, 0); |
| 1624 | |
| 1625 | xcb_change_property(demo->connection, XCB_PROP_MODE_REPLACE, |
| 1626 | demo->window, (*reply).atom, 4, 32, 1, |
| 1627 | &(*demo->atom_wm_delete_window).atom); |
| 1628 | free(reply); |
| 1629 | |
Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 1630 | xcb_map_window(demo->connection, demo->window); |
| 1631 | } |
Ian Elliott | e14e9f9 | 2015-04-16 15:23:05 -0600 | [diff] [blame] | 1632 | #endif // _WIN32 |
Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 1633 | |
Ian Elliott | e36b208 | 2015-07-06 14:27:58 -0600 | [diff] [blame] | 1634 | /* |
| 1635 | * Return 1 (true) if all layer names specified in check_names |
| 1636 | * can be found in given layer properties. |
| 1637 | */ |
| 1638 | static VkBool32 demo_check_layers(uint32_t check_count, char **check_names, |
| 1639 | uint32_t layer_count, VkLayerProperties *layers) |
| 1640 | { |
| 1641 | for (uint32_t i = 0; i < check_count; i++) { |
| 1642 | VkBool32 found = 0; |
| 1643 | for (uint32_t j = 0; j < layer_count; j++) { |
| 1644 | if (!strcmp(check_names[i], layers[j].layerName)) { |
| 1645 | found = 1; |
| 1646 | } |
| 1647 | } |
| 1648 | if (!found) { |
| 1649 | fprintf(stderr, "Cannot find layer: %s\n", check_names[i]); |
| 1650 | return 0; |
| 1651 | } |
| 1652 | } |
| 1653 | return 1; |
| 1654 | } |
| 1655 | |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1656 | static void demo_init_vk(struct demo *demo) |
Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 1657 | { |
Tobin Ehlis | 3536b44 | 2015-04-16 18:04:57 -0600 | [diff] [blame] | 1658 | VkResult err; |
Courtney Goeltzenleuchter | 18061cd | 2015-06-29 15:39:26 -0600 | [diff] [blame] | 1659 | char *extension_names[64]; |
| 1660 | char *layer_names[64]; |
Courtney Goeltzenleuchter | 1c7c65d | 2015-06-10 17:39:03 -0600 | [diff] [blame] | 1661 | VkExtensionProperties *instance_extensions; |
Courtney Goeltzenleuchter | 18061cd | 2015-06-29 15:39:26 -0600 | [diff] [blame] | 1662 | VkLayerProperties *instance_layers; |
| 1663 | VkLayerProperties *device_layers; |
Courtney Goeltzenleuchter | 1c7c65d | 2015-06-10 17:39:03 -0600 | [diff] [blame] | 1664 | uint32_t instance_extension_count = 0; |
Courtney Goeltzenleuchter | 18061cd | 2015-06-29 15:39:26 -0600 | [diff] [blame] | 1665 | uint32_t instance_layer_count = 0; |
| 1666 | uint32_t enabled_extension_count = 0; |
| 1667 | uint32_t enabled_layer_count = 0; |
| 1668 | |
Ian Elliott | e36b208 | 2015-07-06 14:27:58 -0600 | [diff] [blame] | 1669 | char *instance_validation_layers[] = { |
| 1670 | "MemTracker", |
| 1671 | }; |
| 1672 | |
| 1673 | char *device_validation_layers[] = { |
| 1674 | "MemTracker", |
| 1675 | }; |
| 1676 | |
Courtney Goeltzenleuchter | 18061cd | 2015-06-29 15:39:26 -0600 | [diff] [blame] | 1677 | /* Look for validation layers */ |
Courtney Goeltzenleuchter | 1f41f54 | 2015-07-09 11:44:38 -0600 | [diff] [blame] | 1678 | VkBool32 validation_found = 0; |
Courtney Goeltzenleuchter | 18061cd | 2015-06-29 15:39:26 -0600 | [diff] [blame] | 1679 | err = vkGetGlobalLayerProperties(&instance_layer_count, NULL); |
Tobin Ehlis | 3536b44 | 2015-04-16 18:04:57 -0600 | [diff] [blame] | 1680 | assert(!err); |
| 1681 | |
Courtney Goeltzenleuchter | 18061cd | 2015-06-29 15:39:26 -0600 | [diff] [blame] | 1682 | instance_layers = malloc(sizeof(VkLayerProperties) * instance_layer_count); |
| 1683 | err = vkGetGlobalLayerProperties(&instance_layer_count, instance_layers); |
| 1684 | assert(!err); |
Ian Elliott | e36b208 | 2015-07-06 14:27:58 -0600 | [diff] [blame] | 1685 | |
| 1686 | if (demo->validate) { |
| 1687 | validation_found = demo_check_layers(ARRAY_SIZE(instance_validation_layers), instance_validation_layers, |
| 1688 | instance_layer_count, instance_layers); |
| 1689 | if (!validation_found) { |
| 1690 | ERR_EXIT("vkGetGlobalLayerProperties failed to find" |
| 1691 | "required validation layer.\n\n" |
| 1692 | "Please look at the Getting Started guide for additional " |
| 1693 | "information.\n", |
| 1694 | "vkCreateInstance Failure"); |
Courtney Goeltzenleuchter | 1c7c65d | 2015-06-10 17:39:03 -0600 | [diff] [blame] | 1695 | } |
Ian Elliott | e36b208 | 2015-07-06 14:27:58 -0600 | [diff] [blame] | 1696 | enabled_layer_count = ARRAY_SIZE(instance_validation_layers); |
Courtney Goeltzenleuchter | 18061cd | 2015-06-29 15:39:26 -0600 | [diff] [blame] | 1697 | } |
| 1698 | |
| 1699 | err = vkGetGlobalExtensionProperties(NULL, &instance_extension_count, NULL); |
| 1700 | assert(!err); |
| 1701 | |
Courtney Goeltzenleuchter | 1f41f54 | 2015-07-09 11:44:38 -0600 | [diff] [blame] | 1702 | VkBool32 WSIextFound = 0; |
Courtney Goeltzenleuchter | 18061cd | 2015-06-29 15:39:26 -0600 | [diff] [blame] | 1703 | memset(extension_names, 0, sizeof(extension_names)); |
| 1704 | instance_extensions = malloc(sizeof(VkExtensionProperties) * instance_extension_count); |
| 1705 | err = vkGetGlobalExtensionProperties(NULL, &instance_extension_count, instance_extensions); |
| 1706 | assert(!err); |
| 1707 | for (uint32_t i = 0; i < instance_extension_count; i++) { |
Ian Elliott | e36b208 | 2015-07-06 14:27:58 -0600 | [diff] [blame] | 1708 | if (!strcmp("VK_WSI_swapchain", instance_extensions[i].extName)) { |
Courtney Goeltzenleuchter | 18061cd | 2015-06-29 15:39:26 -0600 | [diff] [blame] | 1709 | WSIextFound = 1; |
Ian Elliott | e36b208 | 2015-07-06 14:27:58 -0600 | [diff] [blame] | 1710 | extension_names[enabled_extension_count++] = "VK_WSI_swapchain"; |
Courtney Goeltzenleuchter | 18061cd | 2015-06-29 15:39:26 -0600 | [diff] [blame] | 1711 | } |
| 1712 | if (!strcmp(DEBUG_REPORT_EXTENSION_NAME, instance_extensions[i].extName)) { |
| 1713 | if (demo->validate) { |
| 1714 | extension_names[enabled_extension_count++] = DEBUG_REPORT_EXTENSION_NAME; |
| 1715 | } |
| 1716 | } |
| 1717 | assert(enabled_extension_count < 64); |
Tobin Ehlis | 3536b44 | 2015-04-16 18:04:57 -0600 | [diff] [blame] | 1718 | } |
Courtney Goeltzenleuchter | 1c7c65d | 2015-06-10 17:39:03 -0600 | [diff] [blame] | 1719 | if (!WSIextFound) { |
Tony Barbour | 426b905 | 2015-06-24 16:06:58 -0600 | [diff] [blame] | 1720 | ERR_EXIT("vkGetGlobalExtensionProperties failed to find the " |
Ian Elliott | e36b208 | 2015-07-06 14:27:58 -0600 | [diff] [blame] | 1721 | "\"VK_WSI_swapchain\" extension.\n\nDo you have a compatible " |
Ian Elliott | 3b375cf | 2015-04-28 13:22:33 -0600 | [diff] [blame] | 1722 | "Vulkan installable client driver (ICD) installed?\nPlease " |
| 1723 | "look at the Getting Started guide for additional " |
| 1724 | "information.\n", |
| 1725 | "vkCreateInstance Failure"); |
| 1726 | } |
Courtney Goeltzenleuchter | 382489d | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 1727 | const VkApplicationInfo app = { |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1728 | .sType = VK_STRUCTURE_TYPE_APPLICATION_INFO, |
Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 1729 | .pNext = NULL, |
Ian Elliott | 4e19ed0 | 2015-04-28 10:52:52 -0600 | [diff] [blame] | 1730 | .pAppName = APP_SHORT_NAME, |
Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 1731 | .appVersion = 0, |
Ian Elliott | 4e19ed0 | 2015-04-28 10:52:52 -0600 | [diff] [blame] | 1732 | .pEngineName = APP_SHORT_NAME, |
Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 1733 | .engineVersion = 0, |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1734 | .apiVersion = VK_API_VERSION, |
Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 1735 | }; |
Courtney Goeltzenleuchter | 18061cd | 2015-06-29 15:39:26 -0600 | [diff] [blame] | 1736 | VkInstanceCreateInfo inst_info = { |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1737 | .sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO, |
Jon Ashburn | 29669a4 | 2015-04-04 14:52:07 -0600 | [diff] [blame] | 1738 | .pNext = NULL, |
| 1739 | .pAppInfo = &app, |
| 1740 | .pAllocCb = NULL, |
Courtney Goeltzenleuchter | 18061cd | 2015-06-29 15:39:26 -0600 | [diff] [blame] | 1741 | .layerCount = enabled_layer_count, |
| 1742 | .ppEnabledLayerNames = (const char *const*) layer_names, |
| 1743 | .extensionCount = enabled_extension_count, |
| 1744 | .ppEnabledExtensionNames = (const char *const*) extension_names, |
Jon Ashburn | 29669a4 | 2015-04-04 14:52:07 -0600 | [diff] [blame] | 1745 | }; |
Courtney Goeltzenleuchter | ddcb619 | 2015-04-14 18:48:46 -0600 | [diff] [blame] | 1746 | const VkDeviceQueueCreateInfo queue = { |
Chris Forbes | fa6d36e | 2015-07-11 19:11:39 +1200 | [diff] [blame] | 1747 | .queueFamilyIndex = 0, |
Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 1748 | .queueCount = 1, |
| 1749 | }; |
Mark Lobodzinski | e2d07a5 | 2015-01-29 08:55:56 -0600 | [diff] [blame] | 1750 | uint32_t gpu_count; |
Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 1751 | |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1752 | err = vkCreateInstance(&inst_info, &demo->inst); |
Ian Elliott | caa9f27 | 2015-04-28 11:35:02 -0600 | [diff] [blame] | 1753 | if (err == VK_ERROR_INCOMPATIBLE_DRIVER) { |
| 1754 | ERR_EXIT("Cannot find a compatible Vulkan installable client driver " |
Ian Elliott | 3b375cf | 2015-04-28 13:22:33 -0600 | [diff] [blame] | 1755 | "(ICD).\n\nPlease look at the Getting Started guide for " |
Ian Elliott | caa9f27 | 2015-04-28 11:35:02 -0600 | [diff] [blame] | 1756 | "additional information.\n", |
| 1757 | "vkCreateInstance Failure"); |
Courtney Goeltzenleuchter | 18061cd | 2015-06-29 15:39:26 -0600 | [diff] [blame] | 1758 | } else if (err == VK_ERROR_INVALID_EXTENSION) { |
| 1759 | ERR_EXIT("Cannot find a specified extension library" |
| 1760 | ".\nMake sure your layers path is set appropriately\n", |
| 1761 | "vkCreateInstance Failure"); |
Ian Elliott | caa9f27 | 2015-04-28 11:35:02 -0600 | [diff] [blame] | 1762 | } else if (err) { |
Ian Elliott | 3b375cf | 2015-04-28 13:22:33 -0600 | [diff] [blame] | 1763 | ERR_EXIT("vkCreateInstance failed.\n\nDo you have a compatible Vulkan " |
| 1764 | "installable client driver (ICD) installed?\nPlease look at " |
Ian Elliott | caa9f27 | 2015-04-28 11:35:02 -0600 | [diff] [blame] | 1765 | "the Getting Started guide for additional information.\n", |
| 1766 | "vkCreateInstance Failure"); |
Ian Elliott | dfe55f7 | 2015-04-03 15:24:55 -0600 | [diff] [blame] | 1767 | } |
Jon Ashburn | 29669a4 | 2015-04-04 14:52:07 -0600 | [diff] [blame] | 1768 | |
Courtney Goeltzenleuchter | 18061cd | 2015-06-29 15:39:26 -0600 | [diff] [blame] | 1769 | free(instance_layers); |
| 1770 | free(instance_extensions); |
| 1771 | |
Jon Ashburn | 07b309a | 2015-04-15 11:31:12 -0600 | [diff] [blame] | 1772 | gpu_count = 1; |
| 1773 | err = vkEnumeratePhysicalDevices(demo->inst, &gpu_count, &demo->gpu); |
Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 1774 | assert(!err && gpu_count == 1); |
| 1775 | |
Courtney Goeltzenleuchter | 18061cd | 2015-06-29 15:39:26 -0600 | [diff] [blame] | 1776 | /* Look for validation layers */ |
| 1777 | validation_found = 0; |
| 1778 | enabled_layer_count = 0; |
| 1779 | uint32_t device_layer_count = 0; |
| 1780 | err = vkGetPhysicalDeviceLayerProperties(demo->gpu, &device_layer_count, NULL); |
| 1781 | assert(!err); |
| 1782 | |
Courtney Goeltzenleuchter | 18061cd | 2015-06-29 15:39:26 -0600 | [diff] [blame] | 1783 | device_layers = malloc(sizeof(VkLayerProperties) * device_layer_count); |
| 1784 | err = vkGetPhysicalDeviceLayerProperties(demo->gpu, &device_layer_count, device_layers); |
| 1785 | assert(!err); |
Ian Elliott | e36b208 | 2015-07-06 14:27:58 -0600 | [diff] [blame] | 1786 | |
| 1787 | if (demo->validate) { |
| 1788 | validation_found = demo_check_layers(ARRAY_SIZE(device_validation_layers), device_validation_layers, |
| 1789 | device_layer_count, device_layers); |
| 1790 | if (!validation_found) { |
| 1791 | ERR_EXIT("vkGetPhysicalDeviceLayerProperties failed to find" |
| 1792 | "a required validation layer.\n\n" |
| 1793 | "Please look at the Getting Started guide for additional " |
| 1794 | "information.\n", |
| 1795 | "vkCreateDevice Failure"); |
Courtney Goeltzenleuchter | 18061cd | 2015-06-29 15:39:26 -0600 | [diff] [blame] | 1796 | } |
Ian Elliott | e36b208 | 2015-07-06 14:27:58 -0600 | [diff] [blame] | 1797 | enabled_layer_count = ARRAY_SIZE(device_validation_layers); |
Courtney Goeltzenleuchter | 18061cd | 2015-06-29 15:39:26 -0600 | [diff] [blame] | 1798 | } |
Ian Elliott | e36b208 | 2015-07-06 14:27:58 -0600 | [diff] [blame] | 1799 | |
| 1800 | uint32_t device_extension_count = 0; |
| 1801 | VkExtensionProperties *device_extensions = NULL; |
| 1802 | err = vkGetPhysicalDeviceExtensionProperties( |
| 1803 | demo->gpu, NULL, &device_extension_count, NULL); |
| 1804 | assert(!err); |
| 1805 | |
| 1806 | WSIextFound = 0; |
| 1807 | enabled_extension_count = 0; |
| 1808 | memset(extension_names, 0, sizeof(extension_names)); |
| 1809 | device_extensions = malloc(sizeof(VkExtensionProperties) * device_extension_count); |
| 1810 | err = vkGetPhysicalDeviceExtensionProperties( |
| 1811 | demo->gpu, NULL, &device_extension_count, device_extensions); |
| 1812 | assert(!err); |
| 1813 | |
| 1814 | for (uint32_t i = 0; i < device_extension_count; i++) { |
| 1815 | if (!strcmp("VK_WSI_device_swapchain", device_extensions[i].extName)) { |
| 1816 | WSIextFound = 1; |
| 1817 | extension_names[enabled_extension_count++] = "VK_WSI_device_swapchain"; |
| 1818 | } |
| 1819 | assert(enabled_extension_count < 64); |
| 1820 | } |
| 1821 | if (!WSIextFound) { |
| 1822 | ERR_EXIT("vkGetPhysicalDeviceExtensionProperties failed to find the " |
| 1823 | "\"VK_WSI_device_swapchain\" extension.\n\nDo you have a compatible " |
| 1824 | "Vulkan installable client driver (ICD) installed?\nPlease " |
| 1825 | "look at the Getting Started guide for additional " |
Courtney Goeltzenleuchter | 18061cd | 2015-06-29 15:39:26 -0600 | [diff] [blame] | 1826 | "information.\n", |
| 1827 | "vkCreateInstance Failure"); |
| 1828 | } |
| 1829 | |
Courtney Goeltzenleuchter | 18061cd | 2015-06-29 15:39:26 -0600 | [diff] [blame] | 1830 | VkDeviceCreateInfo device = { |
| 1831 | .sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO, |
| 1832 | .pNext = NULL, |
| 1833 | .queueRecordCount = 1, |
| 1834 | .pRequestedQueues = &queue, |
| 1835 | .layerCount = enabled_layer_count, |
Ian Elliott | e36b208 | 2015-07-06 14:27:58 -0600 | [diff] [blame] | 1836 | .ppEnabledLayerNames = (const char *const*) ((demo->validate) ? device_validation_layers : NULL), |
| 1837 | .extensionCount = enabled_extension_count, |
| 1838 | .ppEnabledExtensionNames = (const char *const*) extension_names, |
Courtney Goeltzenleuchter | 18061cd | 2015-06-29 15:39:26 -0600 | [diff] [blame] | 1839 | .flags = 0, |
| 1840 | }; |
| 1841 | |
| 1842 | if (demo->validate) { |
Courtney Goeltzenleuchter | a4c8c71 | 2015-07-12 14:35:22 -0600 | [diff] [blame] | 1843 | demo->dbgCreateMsgCallback = (PFN_vkDbgCreateMsgCallback) vkGetInstanceProcAddr(demo->inst, "vkDbgCreateMsgCallback"); |
Courtney Goeltzenleuchter | 18061cd | 2015-06-29 15:39:26 -0600 | [diff] [blame] | 1844 | if (!demo->dbgCreateMsgCallback) { |
| 1845 | ERR_EXIT("GetProcAddr: Unable to find vkDbgCreateMsgCallback\n", |
| 1846 | "vkGetProcAddr Failure"); |
| 1847 | } |
| 1848 | err = demo->dbgCreateMsgCallback( |
| 1849 | demo->inst, |
| 1850 | VK_DBG_REPORT_ERROR_BIT | VK_DBG_REPORT_WARN_BIT, |
| 1851 | dbgFunc, NULL, |
| 1852 | &demo->msg_callback); |
| 1853 | switch (err) { |
| 1854 | case VK_SUCCESS: |
| 1855 | break; |
| 1856 | case VK_ERROR_INVALID_POINTER: |
| 1857 | ERR_EXIT("dbgCreateMsgCallback: Invalid pointer\n", |
| 1858 | "dbgCreateMsgCallback Failure"); |
| 1859 | break; |
| 1860 | case VK_ERROR_OUT_OF_HOST_MEMORY: |
| 1861 | ERR_EXIT("dbgCreateMsgCallback: out of host memory\n", |
| 1862 | "dbgCreateMsgCallback Failure"); |
| 1863 | break; |
| 1864 | default: |
| 1865 | ERR_EXIT("dbgCreateMsgCallback: unknown failure\n", |
| 1866 | "dbgCreateMsgCallback Failure"); |
| 1867 | break; |
| 1868 | } |
| 1869 | } |
| 1870 | |
Ian Elliott | e36b208 | 2015-07-06 14:27:58 -0600 | [diff] [blame] | 1871 | |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1872 | err = vkCreateDevice(demo->gpu, &device, &demo->device); |
Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 1873 | assert(!err); |
| 1874 | |
Ian Elliott | e36b208 | 2015-07-06 14:27:58 -0600 | [diff] [blame] | 1875 | GET_INSTANCE_PROC_ADDR(demo->inst, GetPhysicalDeviceSurfaceSupportWSI); |
| 1876 | GET_DEVICE_PROC_ADDR(demo->device, GetSurfaceInfoWSI); |
Ian Elliott | 1b6de09 | 2015-06-22 15:07:49 -0600 | [diff] [blame] | 1877 | GET_DEVICE_PROC_ADDR(demo->device, CreateSwapChainWSI); |
| 1878 | GET_DEVICE_PROC_ADDR(demo->device, CreateSwapChainWSI); |
| 1879 | GET_DEVICE_PROC_ADDR(demo->device, DestroySwapChainWSI); |
| 1880 | GET_DEVICE_PROC_ADDR(demo->device, GetSwapChainInfoWSI); |
Ian Elliott | e36b208 | 2015-07-06 14:27:58 -0600 | [diff] [blame] | 1881 | GET_DEVICE_PROC_ADDR(demo->device, AcquireNextImageWSI); |
Ian Elliott | 1b6de09 | 2015-06-22 15:07:49 -0600 | [diff] [blame] | 1882 | GET_DEVICE_PROC_ADDR(demo->device, QueuePresentWSI); |
| 1883 | |
Tony Barbour | 426b905 | 2015-06-24 16:06:58 -0600 | [diff] [blame] | 1884 | err = vkGetPhysicalDeviceProperties(demo->gpu, &demo->gpu_props); |
Courtney Goeltzenleuchter | bfccf41 | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 1885 | |
Piers Daniell | 1cf7fe1 | 2015-07-16 09:35:35 -0600 | [diff] [blame^] | 1886 | err = vkGetPhysicalDeviceQueueCount(demo->gpu, &demo->queue_count); |
Courtney Goeltzenleuchter | bfccf41 | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 1887 | assert(!err); |
| 1888 | |
Piers Daniell | 1cf7fe1 | 2015-07-16 09:35:35 -0600 | [diff] [blame^] | 1889 | demo->queue_props = (VkPhysicalDeviceQueueProperties *) malloc(demo->queue_count * sizeof(VkPhysicalDeviceQueueProperties)); |
| 1890 | err = vkGetPhysicalDeviceQueueProperties(demo->gpu, demo->queue_count, demo->queue_props); |
Courtney Goeltzenleuchter | f316806 | 2015-03-05 18:09:39 -0700 | [diff] [blame] | 1891 | assert(!err); |
Piers Daniell | 1cf7fe1 | 2015-07-16 09:35:35 -0600 | [diff] [blame^] | 1892 | assert(demo->queue_count >= 1); |
Courtney Goeltzenleuchter | f316806 | 2015-03-05 18:09:39 -0700 | [diff] [blame] | 1893 | |
Courtney Goeltzenleuchter | 18061cd | 2015-06-29 15:39:26 -0600 | [diff] [blame] | 1894 | // Graphics queue and MemMgr queue can be separate. |
| 1895 | // TODO: Add support for separate queues, including synchronization, |
| 1896 | // and appropriate tracking for QueueSubmit |
Piers Daniell | 1cf7fe1 | 2015-07-16 09:35:35 -0600 | [diff] [blame^] | 1897 | } |
| 1898 | |
| 1899 | static void demo_init_vk_wsi(struct demo *demo) |
| 1900 | { |
| 1901 | VkResult err; |
| 1902 | uint32_t i; |
Ian Elliott | e36b208 | 2015-07-06 14:27:58 -0600 | [diff] [blame] | 1903 | |
| 1904 | // Construct the WSI surface description: |
| 1905 | demo->surface_description.sType = VK_STRUCTURE_TYPE_SURFACE_DESCRIPTION_WINDOW_WSI; |
| 1906 | demo->surface_description.pNext = NULL; |
| 1907 | #ifdef _WIN32 |
| 1908 | demo->surface_description.platform = VK_PLATFORM_WIN32_WSI; |
| 1909 | demo->surface_description.pPlatformHandle = demo->connection; |
| 1910 | demo->surface_description.pPlatformWindow = demo->window; |
| 1911 | #else // _WIN32 |
| 1912 | demo->platform_handle_xcb.connection = demo->connection; |
| 1913 | demo->platform_handle_xcb.root = demo->screen->root; |
| 1914 | demo->surface_description.platform = VK_PLATFORM_XCB_WSI; |
| 1915 | demo->surface_description.pPlatformHandle = &demo->platform_handle_xcb; |
| 1916 | demo->surface_description.pPlatformWindow = &demo->window; |
| 1917 | #endif // _WIN32 |
| 1918 | |
| 1919 | // Iterate over each queue to learn whether it supports presenting to WSI: |
Piers Daniell | 1cf7fe1 | 2015-07-16 09:35:35 -0600 | [diff] [blame^] | 1920 | VkBool32* supportsPresent = (VkBool32 *)malloc(demo->queue_count * sizeof(VkBool32)); |
| 1921 | for (i = 0; i < demo->queue_count; i++) { |
Ian Elliott | e36b208 | 2015-07-06 14:27:58 -0600 | [diff] [blame] | 1922 | demo->fpGetPhysicalDeviceSurfaceSupportWSI(demo->gpu, i, |
| 1923 | (VkSurfaceDescriptionWSI *) &demo->surface_description, |
| 1924 | &supportsPresent[i]); |
Courtney Goeltzenleuchter | f316806 | 2015-03-05 18:09:39 -0700 | [diff] [blame] | 1925 | } |
Ian Elliott | e36b208 | 2015-07-06 14:27:58 -0600 | [diff] [blame] | 1926 | |
| 1927 | // Search for a graphics and a present queue in the array of queue |
| 1928 | // families, try to find one that supports both |
| 1929 | uint32_t graphicsQueueNodeIndex = UINT32_MAX; |
| 1930 | uint32_t presentQueueNodeIndex = UINT32_MAX; |
Piers Daniell | 1cf7fe1 | 2015-07-16 09:35:35 -0600 | [diff] [blame^] | 1931 | for (i = 0; i < demo->queue_count; i++) { |
Ian Elliott | e36b208 | 2015-07-06 14:27:58 -0600 | [diff] [blame] | 1932 | if ((demo->queue_props[i].queueFlags & VK_QUEUE_GRAPHICS_BIT) != 0) { |
| 1933 | if (graphicsQueueNodeIndex == UINT32_MAX) { |
| 1934 | graphicsQueueNodeIndex = i; |
| 1935 | } |
| 1936 | |
| 1937 | if (supportsPresent[i] == VK_TRUE) { |
| 1938 | graphicsQueueNodeIndex = i; |
| 1939 | presentQueueNodeIndex = i; |
| 1940 | break; |
| 1941 | } |
| 1942 | } |
| 1943 | } |
| 1944 | if (presentQueueNodeIndex == UINT32_MAX) { |
| 1945 | // If didn't find a queue that supports both graphics and present, then |
| 1946 | // find a separate present queue. |
Piers Daniell | 1cf7fe1 | 2015-07-16 09:35:35 -0600 | [diff] [blame^] | 1947 | for (uint32_t i = 0; i < demo->queue_count; ++i) { |
Ian Elliott | e36b208 | 2015-07-06 14:27:58 -0600 | [diff] [blame] | 1948 | if (supportsPresent[i] == VK_TRUE) { |
| 1949 | presentQueueNodeIndex = i; |
| 1950 | break; |
| 1951 | } |
| 1952 | } |
| 1953 | } |
| 1954 | free(supportsPresent); |
| 1955 | |
| 1956 | // Generate error if could not find both a graphics and a present queue |
| 1957 | if (graphicsQueueNodeIndex == UINT32_MAX || presentQueueNodeIndex == UINT32_MAX) { |
| 1958 | ERR_EXIT("Could not find a graphics and a present queue\n", |
| 1959 | "WSI Initialization Failure"); |
| 1960 | } |
| 1961 | |
| 1962 | // TODO: Add support for separate queues, including presentation, |
| 1963 | // synchronization, and appropriate tracking for QueueSubmit |
| 1964 | // While it is possible for an application to use a separate graphics and a |
| 1965 | // present queues, this demo program assumes it is only using one: |
| 1966 | if (graphicsQueueNodeIndex != presentQueueNodeIndex) { |
| 1967 | ERR_EXIT("Could not find a common graphics and a present queue\n", |
| 1968 | "WSI Initialization Failure"); |
| 1969 | } |
| 1970 | |
| 1971 | demo->graphics_queue_node_index = graphicsQueueNodeIndex; |
Courtney Goeltzenleuchter | f316806 | 2015-03-05 18:09:39 -0700 | [diff] [blame] | 1972 | |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1973 | err = vkGetDeviceQueue(demo->device, demo->graphics_queue_node_index, |
Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 1974 | 0, &demo->queue); |
| 1975 | assert(!err); |
Ian Elliott | 32536f9 | 2015-04-21 16:41:02 -0600 | [diff] [blame] | 1976 | |
Ian Elliott | e36b208 | 2015-07-06 14:27:58 -0600 | [diff] [blame] | 1977 | // Get the list of VkFormat's that are supported: |
| 1978 | size_t formatsSize; |
| 1979 | err = demo->fpGetSurfaceInfoWSI(demo->device, |
| 1980 | (VkSurfaceDescriptionWSI *) &demo->surface_description, |
| 1981 | VK_SURFACE_INFO_TYPE_FORMATS_WSI, |
| 1982 | &formatsSize, NULL); |
| 1983 | assert(!err); |
| 1984 | VkSurfaceFormatPropertiesWSI *surfFormats = (VkSurfaceFormatPropertiesWSI *)malloc(formatsSize); |
| 1985 | err = demo->fpGetSurfaceInfoWSI(demo->device, |
| 1986 | (VkSurfaceDescriptionWSI *) &demo->surface_description, |
| 1987 | VK_SURFACE_INFO_TYPE_FORMATS_WSI, |
| 1988 | &formatsSize, surfFormats); |
| 1989 | assert(!err); |
| 1990 | // If the format list includes just one entry of VK_FORMAT_UNDEFINED, |
| 1991 | // the surface has no preferred format. Otherwise, at least one |
| 1992 | // supported format will be returned. |
| 1993 | size_t formatCount = formatsSize / sizeof(VkSurfaceFormatPropertiesWSI); |
| 1994 | if (formatCount == 1 && surfFormats[0].format == VK_FORMAT_UNDEFINED) |
| 1995 | { |
| 1996 | demo->format = VK_FORMAT_B8G8R8A8_UNORM; |
| 1997 | } |
| 1998 | else |
| 1999 | { |
| 2000 | assert(formatCount >= 1); |
| 2001 | demo->format = surfFormats[0].format; |
| 2002 | } |
Ian Elliott | 32536f9 | 2015-04-21 16:41:02 -0600 | [diff] [blame] | 2003 | |
Mark Lobodzinski | 7234629 | 2015-07-02 16:49:40 -0600 | [diff] [blame] | 2004 | // Get Memory information and properties |
| 2005 | err = vkGetPhysicalDeviceMemoryProperties(demo->gpu, &demo->memory_properties); |
| 2006 | assert(!err); |
Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 2007 | } |
| 2008 | |
| 2009 | static void demo_init_connection(struct demo *demo) |
| 2010 | { |
Ian Elliott | e14e9f9 | 2015-04-16 15:23:05 -0600 | [diff] [blame] | 2011 | #ifndef _WIN32 |
Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 2012 | const xcb_setup_t *setup; |
| 2013 | xcb_screen_iterator_t iter; |
| 2014 | int scr; |
| 2015 | |
| 2016 | demo->connection = xcb_connect(NULL, &scr); |
Ian Elliott | dfe55f7 | 2015-04-03 15:24:55 -0600 | [diff] [blame] | 2017 | if (demo->connection == NULL) { |
| 2018 | printf("Cannot find a compatible Vulkan installable client driver " |
| 2019 | "(ICD).\nExiting ...\n"); |
| 2020 | fflush(stdout); |
| 2021 | exit(1); |
| 2022 | } |
Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 2023 | |
| 2024 | setup = xcb_get_setup(demo->connection); |
| 2025 | iter = xcb_setup_roots_iterator(setup); |
| 2026 | while (scr-- > 0) |
| 2027 | xcb_screen_next(&iter); |
| 2028 | |
| 2029 | demo->screen = iter.data; |
Ian Elliott | e14e9f9 | 2015-04-16 15:23:05 -0600 | [diff] [blame] | 2030 | #endif // _WIN32 |
Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 2031 | } |
| 2032 | |
Ian Elliott | e14e9f9 | 2015-04-16 15:23:05 -0600 | [diff] [blame] | 2033 | #ifdef _WIN32 |
| 2034 | static void demo_init(struct demo *demo, HINSTANCE hInstance, LPSTR pCmdLine) |
| 2035 | #else // _WIN32 |
Courtney Goeltzenleuchter | f113a95 | 2015-02-25 11:46:58 -0700 | [diff] [blame] | 2036 | 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] | 2037 | #endif // _WIN32 |
Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 2038 | { |
Ian Elliott | e14e9f9 | 2015-04-16 15:23:05 -0600 | [diff] [blame] | 2039 | bool argv_error = false; |
| 2040 | |
Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 2041 | memset(demo, 0, sizeof(*demo)); |
| 2042 | |
Ian Elliott | e14e9f9 | 2015-04-16 15:23:05 -0600 | [diff] [blame] | 2043 | #ifdef _WIN32 |
| 2044 | demo->connection = hInstance; |
Ian Elliott | 4e19ed0 | 2015-04-28 10:52:52 -0600 | [diff] [blame] | 2045 | strncpy(demo->name, APP_SHORT_NAME, APP_NAME_STR_LEN); |
Ian Elliott | e14e9f9 | 2015-04-16 15:23:05 -0600 | [diff] [blame] | 2046 | |
| 2047 | if (strncmp(pCmdLine, "--use_staging", strlen("--use_staging")) == 0) |
| 2048 | demo->use_staging_buffer = true; |
Cody Northrop | 75db032 | 2015-05-28 11:27:16 -0600 | [diff] [blame] | 2049 | else if (strncmp(pCmdLine, "--use_glsl", strlen("--use_glsl")) == 0) |
| 2050 | demo->use_glsl = true; |
Ian Elliott | e14e9f9 | 2015-04-16 15:23:05 -0600 | [diff] [blame] | 2051 | else if (strlen(pCmdLine) != 0) { |
| 2052 | fprintf(stderr, "Do not recognize argument \"%s\".\n", pCmdLine); |
| 2053 | argv_error = true; |
| 2054 | } |
| 2055 | #else // _WIN32 |
Courtney Goeltzenleuchter | f113a95 | 2015-02-25 11:46:58 -0700 | [diff] [blame] | 2056 | for (int i = 0; i < argc; i++) { |
| 2057 | if (strncmp(argv[i], "--use_staging", strlen("--use_staging")) == 0) |
| 2058 | demo->use_staging_buffer = true; |
Cody Northrop | 75db032 | 2015-05-28 11:27:16 -0600 | [diff] [blame] | 2059 | else if (strncmp(argv[i], "--use_glsl", strlen("--use_glsl")) == 0) |
| 2060 | demo->use_glsl = true; |
Courtney Goeltzenleuchter | f113a95 | 2015-02-25 11:46:58 -0700 | [diff] [blame] | 2061 | } |
Ian Elliott | e14e9f9 | 2015-04-16 15:23:05 -0600 | [diff] [blame] | 2062 | #endif // _WIN32 |
| 2063 | if (argv_error) { |
Ian Elliott | 4e19ed0 | 2015-04-28 10:52:52 -0600 | [diff] [blame] | 2064 | fprintf(stderr, "Usage:\n %s [--use_staging]\n", APP_SHORT_NAME); |
Ian Elliott | e14e9f9 | 2015-04-16 15:23:05 -0600 | [diff] [blame] | 2065 | fflush(stderr); |
| 2066 | exit(1); |
| 2067 | } |
Courtney Goeltzenleuchter | f113a95 | 2015-02-25 11:46:58 -0700 | [diff] [blame] | 2068 | |
Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 2069 | demo_init_connection(demo); |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 2070 | demo_init_vk(demo); |
Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 2071 | |
| 2072 | demo->width = 300; |
| 2073 | demo->height = 300; |
Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 2074 | } |
| 2075 | |
| 2076 | static void demo_cleanup(struct demo *demo) |
| 2077 | { |
Mark Lobodzinski | 2318261 | 2015-05-29 09:32:35 -0500 | [diff] [blame] | 2078 | uint32_t i; |
Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 2079 | |
Tony Barbour | de4124d | 2015-07-03 10:33:54 -0600 | [diff] [blame] | 2080 | for (i = 0; i < DEMO_BUFFER_COUNT; i++) { |
| 2081 | vkDestroyFramebuffer(demo->device, demo->framebuffers[i]); |
| 2082 | } |
Tony Barbour | b857d31 | 2015-07-10 10:50:45 -0600 | [diff] [blame] | 2083 | vkFreeDescriptorSets(demo->device, demo->desc_pool, 1, &demo->desc_set); |
Tony Barbour | de4124d | 2015-07-03 10:33:54 -0600 | [diff] [blame] | 2084 | vkDestroyDescriptorPool(demo->device, demo->desc_pool); |
Chia-I Wu | f838506 | 2015-01-04 16:27:24 +0800 | [diff] [blame] | 2085 | |
Courtney Goeltzenleuchter | 633f9c5 | 2015-04-30 10:58:33 -0600 | [diff] [blame] | 2086 | if (demo->setup_cmd) { |
Tony Barbour | de4124d | 2015-07-03 10:33:54 -0600 | [diff] [blame] | 2087 | vkDestroyCommandBuffer(demo->device, demo->setup_cmd); |
Courtney Goeltzenleuchter | 633f9c5 | 2015-04-30 10:58:33 -0600 | [diff] [blame] | 2088 | } |
Tony Barbour | de4124d | 2015-07-03 10:33:54 -0600 | [diff] [blame] | 2089 | vkDestroyCommandBuffer(demo->device, demo->draw_cmd); |
Piers Daniell | 1cf7fe1 | 2015-07-16 09:35:35 -0600 | [diff] [blame^] | 2090 | vkDestroyCommandPool(demo->device, demo->cmd_pool); |
Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 2091 | |
Tony Barbour | de4124d | 2015-07-03 10:33:54 -0600 | [diff] [blame] | 2092 | vkDestroyDynamicViewportState(demo->device, demo->viewport); |
| 2093 | vkDestroyDynamicRasterState(demo->device, demo->raster); |
| 2094 | vkDestroyDynamicColorBlendState(demo->device, demo->color_blend); |
| 2095 | vkDestroyDynamicDepthStencilState(demo->device, demo->depth_stencil); |
Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 2096 | |
Tony Barbour | de4124d | 2015-07-03 10:33:54 -0600 | [diff] [blame] | 2097 | vkDestroyPipeline(demo->device, demo->pipeline); |
| 2098 | vkDestroyRenderPass(demo->device, demo->render_pass); |
| 2099 | vkDestroyPipelineLayout(demo->device, demo->pipeline_layout); |
| 2100 | vkDestroyDescriptorSetLayout(demo->device, demo->desc_layout); |
Chia-I Wu | b043fe3 | 2014-10-06 15:30:33 +0800 | [diff] [blame] | 2101 | |
Tony Barbour | de4124d | 2015-07-03 10:33:54 -0600 | [diff] [blame] | 2102 | vkDestroyBuffer(demo->device, demo->vertices.buf); |
Mark Lobodzinski | 2318261 | 2015-05-29 09:32:35 -0500 | [diff] [blame] | 2103 | vkFreeMemory(demo->device, demo->vertices.mem); |
Chia-I Wu | 99621bc | 2014-10-08 11:52:22 +0800 | [diff] [blame] | 2104 | |
Chia-I Wu | b043fe3 | 2014-10-06 15:30:33 +0800 | [diff] [blame] | 2105 | for (i = 0; i < DEMO_TEXTURE_COUNT; i++) { |
Tony Barbour | de4124d | 2015-07-03 10:33:54 -0600 | [diff] [blame] | 2106 | vkDestroyImageView(demo->device, demo->textures[i].view); |
| 2107 | vkDestroyImage(demo->device, demo->textures[i].image); |
Mark Lobodzinski | 2318261 | 2015-05-29 09:32:35 -0500 | [diff] [blame] | 2108 | vkFreeMemory(demo->device, demo->textures[i].mem); |
Tony Barbour | de4124d | 2015-07-03 10:33:54 -0600 | [diff] [blame] | 2109 | vkDestroySampler(demo->device, demo->textures[i].sampler); |
Chia-I Wu | b043fe3 | 2014-10-06 15:30:33 +0800 | [diff] [blame] | 2110 | } |
| 2111 | |
Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 2112 | for (i = 0; i < DEMO_BUFFER_COUNT; i++) { |
Tony Barbour | de4124d | 2015-07-03 10:33:54 -0600 | [diff] [blame] | 2113 | vkDestroyAttachmentView(demo->device, demo->buffers[i].view); |
Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 2114 | } |
Tony Barbour | de4124d | 2015-07-03 10:33:54 -0600 | [diff] [blame] | 2115 | |
| 2116 | vkDestroyAttachmentView(demo->device, demo->depth.view); |
| 2117 | vkDestroyImage(demo->device, demo->depth.image); |
| 2118 | vkFreeMemory(demo->device, demo->depth.mem); |
| 2119 | |
Ian Elliott | e36b208 | 2015-07-06 14:27:58 -0600 | [diff] [blame] | 2120 | demo->fpDestroySwapChainWSI(demo->device, demo->swap_chain); |
| 2121 | free(demo->buffers); |
Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 2122 | |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 2123 | vkDestroyDevice(demo->device); |
| 2124 | vkDestroyInstance(demo->inst); |
Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 2125 | |
Ian Elliott | e14e9f9 | 2015-04-16 15:23:05 -0600 | [diff] [blame] | 2126 | #ifndef _WIN32 |
Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 2127 | xcb_destroy_window(demo->connection, demo->window); |
| 2128 | xcb_disconnect(demo->connection); |
Ian Elliott | e14e9f9 | 2015-04-16 15:23:05 -0600 | [diff] [blame] | 2129 | #endif // _WIN32 |
Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 2130 | } |
| 2131 | |
Ian Elliott | e14e9f9 | 2015-04-16 15:23:05 -0600 | [diff] [blame] | 2132 | #ifdef _WIN32 |
| 2133 | int APIENTRY WinMain(HINSTANCE hInstance, |
| 2134 | HINSTANCE hPrevInstance, |
| 2135 | LPSTR pCmdLine, |
| 2136 | int nCmdShow) |
| 2137 | { |
| 2138 | MSG msg; // message |
| 2139 | bool done; // flag saying when app is complete |
| 2140 | |
| 2141 | demo_init(&demo, hInstance, pCmdLine); |
| 2142 | demo_create_window(&demo); |
Piers Daniell | 1cf7fe1 | 2015-07-16 09:35:35 -0600 | [diff] [blame^] | 2143 | demo_init_vk_wsi(&demo); |
Ian Elliott | e14e9f9 | 2015-04-16 15:23:05 -0600 | [diff] [blame] | 2144 | |
| 2145 | demo_prepare(&demo); |
| 2146 | |
| 2147 | done = false; //initialize loop condition variable |
| 2148 | /* main message loop*/ |
| 2149 | while(!done) |
| 2150 | { |
Ian Elliott | 421107f | 2015-04-28 15:50:36 -0600 | [diff] [blame] | 2151 | PeekMessage(&msg, NULL, 0, 0, PM_REMOVE); |
Ian Elliott | e14e9f9 | 2015-04-16 15:23:05 -0600 | [diff] [blame] | 2152 | if (msg.message == WM_QUIT) //check for a quit message |
| 2153 | { |
| 2154 | done = true; //if found, quit app |
| 2155 | } |
| 2156 | else |
| 2157 | { |
| 2158 | /* Translate and dispatch to event queue*/ |
| 2159 | TranslateMessage(&msg); |
| 2160 | DispatchMessage(&msg); |
| 2161 | } |
| 2162 | } |
| 2163 | |
| 2164 | demo_cleanup(&demo); |
| 2165 | |
Tony Barbour | a938abb | 2015-04-22 11:36:22 -0600 | [diff] [blame] | 2166 | return (int) msg.wParam; |
Ian Elliott | e14e9f9 | 2015-04-16 15:23:05 -0600 | [diff] [blame] | 2167 | } |
| 2168 | #else // _WIN32 |
Courtney Goeltzenleuchter | f113a95 | 2015-02-25 11:46:58 -0700 | [diff] [blame] | 2169 | int main(const int argc, const char *argv[]) |
Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 2170 | { |
| 2171 | struct demo demo; |
| 2172 | |
Courtney Goeltzenleuchter | f113a95 | 2015-02-25 11:46:58 -0700 | [diff] [blame] | 2173 | demo_init(&demo, argc, argv); |
Chia-I Wu | 5b66aa5 | 2015-04-16 22:02:10 +0800 | [diff] [blame] | 2174 | demo_create_window(&demo); |
Piers Daniell | 1cf7fe1 | 2015-07-16 09:35:35 -0600 | [diff] [blame^] | 2175 | demo_init_vk_wsi(&demo); |
Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 2176 | |
| 2177 | demo_prepare(&demo); |
Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 2178 | demo_run(&demo); |
| 2179 | |
| 2180 | demo_cleanup(&demo); |
| 2181 | |
Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 2182 | return 0; |
| 2183 | } |
Ian Elliott | e14e9f9 | 2015-04-16 15:23:05 -0600 | [diff] [blame] | 2184 | #endif // _WIN32 |