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