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; |
| 144 | VkAttachmentView view; |
| 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; |
Chia-I Wu | c278df8 | 2015-07-07 11:50:03 +0800 | [diff] [blame] | 197 | VkAttachmentView 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 | |
Tony Barbour | 248080b | 2015-07-23 10:34:15 -0600 | [diff] [blame] | 454 | err = vkDestroySemaphore(demo->device, presentCompleteSemaphore); |
Ian Elliott | e36b208 | 2015-07-06 14:27:58 -0600 | [diff] [blame] | 455 | assert(!err); |
Chia-I Wu | 5b66aa5 | 2015-04-16 22:02:10 +0800 | [diff] [blame] | 456 | |
| 457 | err = vkQueueWaitIdle(demo->queue); |
| 458 | assert(err == VK_SUCCESS); |
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 | }, |
Ian Elliott | e36b208 | 2015-07-06 14:27:58 -0600 | [diff] [blame] | 545 | .preTransform = preTransform, |
Chia-I Wu | 5b66aa5 | 2015-04-16 22:02:10 +0800 | [diff] [blame] | 546 | .imageArraySize = 1, |
Ian Elliott | 7fe115d | 2015-08-07 15:56:59 -0600 | [diff] [blame] | 547 | .sharingMode = VK_SHARING_MODE_EXCLUSIVE, |
| 548 | .queueFamilyCount = 0, |
| 549 | .pQueueFamilyIndices = NULL, |
Ian Elliott | e36b208 | 2015-07-06 14:27:58 -0600 | [diff] [blame] | 550 | .presentMode = swapChainPresentMode, |
| 551 | .oldSwapChain.handle = 0, |
| 552 | .clipped = true, |
Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 553 | }; |
Mark Lobodzinski | e2d07a5 | 2015-01-29 08:55:56 -0600 | [diff] [blame] | 554 | uint32_t i; |
Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 555 | |
Jon Ashburn | cedc15f | 2015-05-21 18:13:33 -0600 | [diff] [blame] | 556 | err = demo->fpCreateSwapChainWSI(demo->device, &swap_chain, &demo->swap_chain); |
Chia-I Wu | 5b66aa5 | 2015-04-16 22:02:10 +0800 | [diff] [blame] | 557 | assert(!err); |
| 558 | |
Ian Elliott | 7fe115d | 2015-08-07 15:56:59 -0600 | [diff] [blame] | 559 | err = demo->fpGetSwapChainImagesWSI(demo->device, demo->swap_chain, |
| 560 | &demo->swapChainImageCount, NULL); |
Ian Elliott | e36b208 | 2015-07-06 14:27:58 -0600 | [diff] [blame] | 561 | assert(!err); |
Chia-I Wu | 5b66aa5 | 2015-04-16 22:02:10 +0800 | [diff] [blame] | 562 | |
Ian Elliott | 7fe115d | 2015-08-07 15:56:59 -0600 | [diff] [blame] | 563 | VkImage* swapChainImages = |
| 564 | (VkImage*)malloc(demo->swapChainImageCount * sizeof(VkImage)); |
Ian Elliott | e36b208 | 2015-07-06 14:27:58 -0600 | [diff] [blame] | 565 | assert(swapChainImages); |
Ian Elliott | 7fe115d | 2015-08-07 15:56:59 -0600 | [diff] [blame] | 566 | err = demo->fpGetSwapChainImagesWSI(demo->device, demo->swap_chain, |
| 567 | &demo->swapChainImageCount, |
| 568 | swapChainImages); |
Ian Elliott | e36b208 | 2015-07-06 14:27:58 -0600 | [diff] [blame] | 569 | assert(!err); |
| 570 | |
Ian Elliott | e36b208 | 2015-07-06 14:27:58 -0600 | [diff] [blame] | 571 | demo->buffers = (SwapChainBuffers*)malloc(sizeof(SwapChainBuffers)*demo->swapChainImageCount); |
| 572 | assert(demo->buffers); |
| 573 | |
| 574 | for (i = 0; i < demo->swapChainImageCount; i++) { |
Chia-I Wu | c278df8 | 2015-07-07 11:50:03 +0800 | [diff] [blame] | 575 | VkAttachmentViewCreateInfo color_attachment_view = { |
| 576 | .sType = VK_STRUCTURE_TYPE_ATTACHMENT_VIEW_CREATE_INFO, |
Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 577 | .pNext = NULL, |
| 578 | .format = demo->format, |
| 579 | .mipLevel = 0, |
| 580 | .baseArraySlice = 0, |
| 581 | .arraySize = 1, |
| 582 | }; |
| 583 | |
Ian Elliott | 7fe115d | 2015-08-07 15:56:59 -0600 | [diff] [blame] | 584 | demo->buffers[i].image = swapChainImages[i]; |
Mark Lobodzinski | 97dcd04 | 2015-04-16 08:52:00 -0500 | [diff] [blame] | 585 | |
Courtney Goeltzenleuchter | bfccf41 | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 586 | demo_set_image_layout(demo, demo->buffers[i].image, |
malnasse | 4b8ba4d | 2015-06-03 17:28:38 -0400 | [diff] [blame] | 587 | VK_IMAGE_ASPECT_COLOR, |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 588 | VK_IMAGE_LAYOUT_UNDEFINED, |
| 589 | VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL); |
Courtney Goeltzenleuchter | bfccf41 | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 590 | |
Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 591 | color_attachment_view.image = demo->buffers[i].image; |
| 592 | |
Chia-I Wu | c278df8 | 2015-07-07 11:50:03 +0800 | [diff] [blame] | 593 | err = vkCreateAttachmentView(demo->device, |
Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 594 | &color_attachment_view, &demo->buffers[i].view); |
| 595 | assert(!err); |
| 596 | } |
Piers Daniell | 886be47 | 2015-02-23 16:23:13 -0700 | [diff] [blame] | 597 | |
| 598 | demo->current_buffer = 0; |
Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 599 | } |
| 600 | |
Chia-I Wu | 9ae87c9 | 2014-10-07 14:15:01 +0800 | [diff] [blame] | 601 | static void demo_prepare_depth(struct demo *demo) |
| 602 | { |
Tony Barbour | 8205d90 | 2015-04-16 15:59:00 -0600 | [diff] [blame] | 603 | const VkFormat depth_format = VK_FORMAT_D16_UNORM; |
Courtney Goeltzenleuchter | 382489d | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 604 | const VkImageCreateInfo image = { |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 605 | .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO, |
Chia-I Wu | 9ae87c9 | 2014-10-07 14:15:01 +0800 | [diff] [blame] | 606 | .pNext = NULL, |
Tony Barbour | 8205d90 | 2015-04-16 15:59:00 -0600 | [diff] [blame] | 607 | .imageType = VK_IMAGE_TYPE_2D, |
Chia-I Wu | 9ae87c9 | 2014-10-07 14:15:01 +0800 | [diff] [blame] | 608 | .format = depth_format, |
| 609 | .extent = { demo->width, demo->height, 1 }, |
| 610 | .mipLevels = 1, |
| 611 | .arraySize = 1, |
| 612 | .samples = 1, |
Tony Barbour | 8205d90 | 2015-04-16 15:59:00 -0600 | [diff] [blame] | 613 | .tiling = VK_IMAGE_TILING_OPTIMAL, |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 614 | .usage = VK_IMAGE_USAGE_DEPTH_STENCIL_BIT, |
Chia-I Wu | 9ae87c9 | 2014-10-07 14:15:01 +0800 | [diff] [blame] | 615 | .flags = 0, |
| 616 | }; |
Courtney Goeltzenleuchter | ddcb619 | 2015-04-14 18:48:46 -0600 | [diff] [blame] | 617 | VkMemoryAllocInfo mem_alloc = { |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 618 | .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOC_INFO, |
Mark Lobodzinski | 97dcd04 | 2015-04-16 08:52:00 -0500 | [diff] [blame] | 619 | .pNext = NULL, |
Chia-I Wu | 9ae87c9 | 2014-10-07 14:15:01 +0800 | [diff] [blame] | 620 | .allocationSize = 0, |
Mark Lobodzinski | 7234629 | 2015-07-02 16:49:40 -0600 | [diff] [blame] | 621 | .memoryTypeIndex = 0, |
Chia-I Wu | 9ae87c9 | 2014-10-07 14:15:01 +0800 | [diff] [blame] | 622 | }; |
Chia-I Wu | c278df8 | 2015-07-07 11:50:03 +0800 | [diff] [blame] | 623 | VkAttachmentViewCreateInfo view = { |
| 624 | .sType = VK_STRUCTURE_TYPE_ATTACHMENT_VIEW_CREATE_INFO, |
Chia-I Wu | 9ae87c9 | 2014-10-07 14:15:01 +0800 | [diff] [blame] | 625 | .pNext = NULL, |
Tony Barbour | de4124d | 2015-07-03 10:33:54 -0600 | [diff] [blame] | 626 | .image.handle = VK_NULL_HANDLE, |
Piers Daniell | 1cf7fe1 | 2015-07-16 09:35:35 -0600 | [diff] [blame] | 627 | .format = depth_format, |
Chia-I Wu | 9ae87c9 | 2014-10-07 14:15:01 +0800 | [diff] [blame] | 628 | .mipLevel = 0, |
| 629 | .baseArraySlice = 0, |
| 630 | .arraySize = 1, |
| 631 | .flags = 0, |
| 632 | }; |
Jon Ashburn | a9ae383 | 2015-01-16 09:37:43 -0700 | [diff] [blame] | 633 | |
Mark Lobodzinski | 2318261 | 2015-05-29 09:32:35 -0500 | [diff] [blame] | 634 | VkMemoryRequirements mem_reqs; |
Tony Barbour | 22a3086 | 2015-04-22 09:02:32 -0600 | [diff] [blame] | 635 | VkResult U_ASSERT_ONLY err; |
Chia-I Wu | 9ae87c9 | 2014-10-07 14:15:01 +0800 | [diff] [blame] | 636 | |
| 637 | demo->depth.format = depth_format; |
| 638 | |
| 639 | /* create image */ |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 640 | err = vkCreateImage(demo->device, &image, |
Chia-I Wu | 9ae87c9 | 2014-10-07 14:15:01 +0800 | [diff] [blame] | 641 | &demo->depth.image); |
| 642 | assert(!err); |
| 643 | |
Mark Lobodzinski | 7234629 | 2015-07-02 16:49:40 -0600 | [diff] [blame] | 644 | /* get memory requirements for this object */ |
Tony Barbour | de4124d | 2015-07-03 10:33:54 -0600 | [diff] [blame] | 645 | err = vkGetImageMemoryRequirements(demo->device, demo->depth.image, |
| 646 | &mem_reqs); |
Mark Lobodzinski | 7234629 | 2015-07-02 16:49:40 -0600 | [diff] [blame] | 647 | |
| 648 | /* select memory size and type */ |
Mark Lobodzinski | 2318261 | 2015-05-29 09:32:35 -0500 | [diff] [blame] | 649 | mem_alloc.allocationSize = mem_reqs.size; |
Mark Lobodzinski | 7234629 | 2015-07-02 16:49:40 -0600 | [diff] [blame] | 650 | err = memory_type_from_properties(demo, |
| 651 | mem_reqs.memoryTypeBits, |
| 652 | VK_MEMORY_PROPERTY_DEVICE_ONLY, |
| 653 | &mem_alloc.memoryTypeIndex); |
| 654 | assert(!err); |
Chia-I Wu | 9ae87c9 | 2014-10-07 14:15:01 +0800 | [diff] [blame] | 655 | |
Mark Lobodzinski | 2318261 | 2015-05-29 09:32:35 -0500 | [diff] [blame] | 656 | /* allocate memory */ |
| 657 | err = vkAllocMemory(demo->device, &mem_alloc, &demo->depth.mem); |
| 658 | assert(!err); |
Chia-I Wu | 9ae87c9 | 2014-10-07 14:15:01 +0800 | [diff] [blame] | 659 | |
Mark Lobodzinski | 2318261 | 2015-05-29 09:32:35 -0500 | [diff] [blame] | 660 | /* bind memory */ |
Tony Barbour | de4124d | 2015-07-03 10:33:54 -0600 | [diff] [blame] | 661 | err = vkBindImageMemory(demo->device, demo->depth.image, |
| 662 | demo->depth.mem, 0); |
Mark Lobodzinski | 2318261 | 2015-05-29 09:32:35 -0500 | [diff] [blame] | 663 | assert(!err); |
Chia-I Wu | 9ae87c9 | 2014-10-07 14:15:01 +0800 | [diff] [blame] | 664 | |
Courtney Goeltzenleuchter | bfccf41 | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 665 | demo_set_image_layout(demo, demo->depth.image, |
malnasse | 4b8ba4d | 2015-06-03 17:28:38 -0400 | [diff] [blame] | 666 | VK_IMAGE_ASPECT_DEPTH, |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 667 | VK_IMAGE_LAYOUT_UNDEFINED, |
| 668 | VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL); |
Courtney Goeltzenleuchter | bfccf41 | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 669 | |
Chia-I Wu | 9ae87c9 | 2014-10-07 14:15:01 +0800 | [diff] [blame] | 670 | /* create image view */ |
| 671 | view.image = demo->depth.image; |
Chia-I Wu | c278df8 | 2015-07-07 11:50:03 +0800 | [diff] [blame] | 672 | err = vkCreateAttachmentView(demo->device, &view, &demo->depth.view); |
Chia-I Wu | 9ae87c9 | 2014-10-07 14:15:01 +0800 | [diff] [blame] | 673 | assert(!err); |
Chia-I Wu | 9ae87c9 | 2014-10-07 14:15:01 +0800 | [diff] [blame] | 674 | } |
| 675 | |
Courtney Goeltzenleuchter | 372e13c | 2015-02-13 17:52:46 -0700 | [diff] [blame] | 676 | static void demo_prepare_texture_image(struct demo *demo, |
| 677 | const uint32_t *tex_colors, |
Courtney Goeltzenleuchter | bfccf41 | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 678 | struct texture_object *tex_obj, |
Courtney Goeltzenleuchter | 382489d | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 679 | VkImageTiling tiling, |
Courtney Goeltzenleuchter | cb67a32 | 2015-04-21 09:31:23 -0600 | [diff] [blame] | 680 | VkImageUsageFlags usage, |
Courtney Goeltzenleuchter | 382489d | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 681 | VkFlags mem_props) |
Chia-I Wu | b043fe3 | 2014-10-06 15:30:33 +0800 | [diff] [blame] | 682 | { |
Tony Barbour | 8205d90 | 2015-04-16 15:59:00 -0600 | [diff] [blame] | 683 | const VkFormat tex_format = VK_FORMAT_B8G8R8A8_UNORM; |
Mark Lobodzinski | e2d07a5 | 2015-01-29 08:55:56 -0600 | [diff] [blame] | 684 | const int32_t tex_width = 2; |
| 685 | const int32_t tex_height = 2; |
Tony Barbour | 22a3086 | 2015-04-22 09:02:32 -0600 | [diff] [blame] | 686 | VkResult U_ASSERT_ONLY err; |
Chia-I Wu | b043fe3 | 2014-10-06 15:30:33 +0800 | [diff] [blame] | 687 | |
Courtney Goeltzenleuchter | bfccf41 | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 688 | tex_obj->tex_width = tex_width; |
| 689 | tex_obj->tex_height = tex_height; |
Courtney Goeltzenleuchter | 372e13c | 2015-02-13 17:52:46 -0700 | [diff] [blame] | 690 | |
Courtney Goeltzenleuchter | 382489d | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 691 | const VkImageCreateInfo image_create_info = { |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 692 | .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO, |
Courtney Goeltzenleuchter | 3226a6f | 2015-02-11 18:17:22 -0700 | [diff] [blame] | 693 | .pNext = NULL, |
Tony Barbour | 8205d90 | 2015-04-16 15:59:00 -0600 | [diff] [blame] | 694 | .imageType = VK_IMAGE_TYPE_2D, |
Courtney Goeltzenleuchter | 372e13c | 2015-02-13 17:52:46 -0700 | [diff] [blame] | 695 | .format = tex_format, |
| 696 | .extent = { tex_width, tex_height, 1 }, |
| 697 | .mipLevels = 1, |
| 698 | .arraySize = 1, |
| 699 | .samples = 1, |
| 700 | .tiling = tiling, |
Courtney Goeltzenleuchter | cb67a32 | 2015-04-21 09:31:23 -0600 | [diff] [blame] | 701 | .usage = usage, |
Courtney Goeltzenleuchter | 372e13c | 2015-02-13 17:52:46 -0700 | [diff] [blame] | 702 | .flags = 0, |
| 703 | }; |
Courtney Goeltzenleuchter | ddcb619 | 2015-04-14 18:48:46 -0600 | [diff] [blame] | 704 | VkMemoryAllocInfo mem_alloc = { |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 705 | .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOC_INFO, |
Mark Lobodzinski | 97dcd04 | 2015-04-16 08:52:00 -0500 | [diff] [blame] | 706 | .pNext = NULL, |
Courtney Goeltzenleuchter | 372e13c | 2015-02-13 17:52:46 -0700 | [diff] [blame] | 707 | .allocationSize = 0, |
Mark Lobodzinski | 7234629 | 2015-07-02 16:49:40 -0600 | [diff] [blame] | 708 | .memoryTypeIndex = 0, |
Courtney Goeltzenleuchter | 3226a6f | 2015-02-11 18:17:22 -0700 | [diff] [blame] | 709 | }; |
| 710 | |
Mark Lobodzinski | 2318261 | 2015-05-29 09:32:35 -0500 | [diff] [blame] | 711 | VkMemoryRequirements mem_reqs; |
Courtney Goeltzenleuchter | 372e13c | 2015-02-13 17:52:46 -0700 | [diff] [blame] | 712 | |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 713 | err = vkCreateImage(demo->device, &image_create_info, |
Courtney Goeltzenleuchter | bfccf41 | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 714 | &tex_obj->image); |
Courtney Goeltzenleuchter | 3226a6f | 2015-02-11 18:17:22 -0700 | [diff] [blame] | 715 | assert(!err); |
| 716 | |
Tony Barbour | de4124d | 2015-07-03 10:33:54 -0600 | [diff] [blame] | 717 | err = vkGetImageMemoryRequirements(demo->device, tex_obj->image, &mem_reqs); |
Mark Lobodzinski | 7234629 | 2015-07-02 16:49:40 -0600 | [diff] [blame] | 718 | |
| 719 | mem_alloc.allocationSize = mem_reqs.size; |
| 720 | err = memory_type_from_properties(demo, mem_reqs.memoryTypeBits, mem_props, &mem_alloc.memoryTypeIndex); |
| 721 | assert(!err); |
Courtney Goeltzenleuchter | 3226a6f | 2015-02-11 18:17:22 -0700 | [diff] [blame] | 722 | |
Mark Lobodzinski | 2318261 | 2015-05-29 09:32:35 -0500 | [diff] [blame] | 723 | /* allocate memory */ |
| 724 | err = vkAllocMemory(demo->device, &mem_alloc, &tex_obj->mem); |
| 725 | assert(!err); |
Courtney Goeltzenleuchter | 3226a6f | 2015-02-11 18:17:22 -0700 | [diff] [blame] | 726 | |
Mark Lobodzinski | 2318261 | 2015-05-29 09:32:35 -0500 | [diff] [blame] | 727 | /* bind memory */ |
Tony Barbour | de4124d | 2015-07-03 10:33:54 -0600 | [diff] [blame] | 728 | err = vkBindImageMemory(demo->device, tex_obj->image, |
Mark Lobodzinski | 2318261 | 2015-05-29 09:32:35 -0500 | [diff] [blame] | 729 | tex_obj->mem, 0); |
| 730 | assert(!err); |
Courtney Goeltzenleuchter | 3226a6f | 2015-02-11 18:17:22 -0700 | [diff] [blame] | 731 | |
Tony Barbour | 8205d90 | 2015-04-16 15:59:00 -0600 | [diff] [blame] | 732 | if (mem_props & VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT) { |
Courtney Goeltzenleuchter | 382489d | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 733 | const VkImageSubresource subres = { |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 734 | .aspect = VK_IMAGE_ASPECT_COLOR, |
Courtney Goeltzenleuchter | 3226a6f | 2015-02-11 18:17:22 -0700 | [diff] [blame] | 735 | .mipLevel = 0, |
| 736 | .arraySlice = 0, |
| 737 | }; |
Courtney Goeltzenleuchter | 382489d | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 738 | VkSubresourceLayout layout; |
Courtney Goeltzenleuchter | 3226a6f | 2015-02-11 18:17:22 -0700 | [diff] [blame] | 739 | void *data; |
| 740 | int32_t x, y; |
| 741 | |
Tony Barbour | 426b905 | 2015-06-24 16:06:58 -0600 | [diff] [blame] | 742 | err = vkGetImageSubresourceLayout(demo->device, tex_obj->image, &subres, &layout); |
| 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 | err = vkMapMemory(demo->device, tex_obj->mem, 0, 0, 0, &data); |
Courtney Goeltzenleuchter | 3226a6f | 2015-02-11 18:17:22 -0700 | [diff] [blame] | 746 | assert(!err); |
| 747 | |
| 748 | for (y = 0; y < tex_height; y++) { |
| 749 | uint32_t *row = (uint32_t *) ((char *) data + layout.rowPitch * y); |
| 750 | for (x = 0; x < tex_width; x++) |
Courtney Goeltzenleuchter | 372e13c | 2015-02-13 17:52:46 -0700 | [diff] [blame] | 751 | row[x] = tex_colors[(x & 1) ^ (y & 1)]; |
Courtney Goeltzenleuchter | 3226a6f | 2015-02-11 18:17:22 -0700 | [diff] [blame] | 752 | } |
| 753 | |
Mark Lobodzinski | 2318261 | 2015-05-29 09:32:35 -0500 | [diff] [blame] | 754 | err = vkUnmapMemory(demo->device, tex_obj->mem); |
Courtney Goeltzenleuchter | 3226a6f | 2015-02-11 18:17:22 -0700 | [diff] [blame] | 755 | assert(!err); |
Courtney Goeltzenleuchter | 372e13c | 2015-02-13 17:52:46 -0700 | [diff] [blame] | 756 | } |
Courtney Goeltzenleuchter | bfccf41 | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 757 | |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 758 | tex_obj->imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL; |
Courtney Goeltzenleuchter | bfccf41 | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 759 | demo_set_image_layout(demo, tex_obj->image, |
malnasse | 4b8ba4d | 2015-06-03 17:28:38 -0400 | [diff] [blame] | 760 | VK_IMAGE_ASPECT_COLOR, |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 761 | VK_IMAGE_LAYOUT_UNDEFINED, |
Courtney Goeltzenleuchter | bfccf41 | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 762 | tex_obj->imageLayout); |
| 763 | /* 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] | 764 | } |
| 765 | |
Mark Lobodzinski | cf26e07 | 2015-04-16 11:44:05 -0500 | [diff] [blame] | 766 | 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] | 767 | { |
| 768 | /* clean up staging resources */ |
Tony Barbour | de4124d | 2015-07-03 10:33:54 -0600 | [diff] [blame] | 769 | vkDestroyImage(demo->device, tex_obj->image); |
Courtney Goeltzenleuchter | a063d9b | 2015-06-10 16:16:22 -0600 | [diff] [blame] | 770 | vkFreeMemory(demo->device, tex_obj->mem); |
Courtney Goeltzenleuchter | 372e13c | 2015-02-13 17:52:46 -0700 | [diff] [blame] | 771 | } |
| 772 | |
| 773 | static void demo_prepare_textures(struct demo *demo) |
| 774 | { |
Tony Barbour | 8205d90 | 2015-04-16 15:59:00 -0600 | [diff] [blame] | 775 | const VkFormat tex_format = VK_FORMAT_B8G8R8A8_UNORM; |
Courtney Goeltzenleuchter | 382489d | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 776 | VkFormatProperties props; |
Courtney Goeltzenleuchter | 372e13c | 2015-02-13 17:52:46 -0700 | [diff] [blame] | 777 | const uint32_t tex_colors[DEMO_TEXTURE_COUNT][2] = { |
| 778 | { 0xffff0000, 0xff00ff00 }, |
| 779 | }; |
Tony Barbour | 22a3086 | 2015-04-22 09:02:32 -0600 | [diff] [blame] | 780 | VkResult U_ASSERT_ONLY err; |
Courtney Goeltzenleuchter | 372e13c | 2015-02-13 17:52:46 -0700 | [diff] [blame] | 781 | uint32_t i; |
| 782 | |
Courtney Goeltzenleuchter | ab36aa6 | 2015-07-12 12:40:29 -0600 | [diff] [blame] | 783 | err = vkGetPhysicalDeviceFormatProperties(demo->gpu, tex_format, &props); |
Courtney Goeltzenleuchter | 372e13c | 2015-02-13 17:52:46 -0700 | [diff] [blame] | 784 | assert(!err); |
| 785 | |
| 786 | for (i = 0; i < DEMO_TEXTURE_COUNT; i++) { |
Tony Barbour | 8205d90 | 2015-04-16 15:59:00 -0600 | [diff] [blame] | 787 | 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] | 788 | /* Device can texture using linear textures */ |
| 789 | demo_prepare_texture_image(demo, tex_colors[i], &demo->textures[i], |
Courtney Goeltzenleuchter | cb67a32 | 2015-04-21 09:31:23 -0600 | [diff] [blame] | 790 | 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] | 791 | } else if (props.optimalTilingFeatures & VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT){ |
Courtney Goeltzenleuchter | 372e13c | 2015-02-13 17:52:46 -0700 | [diff] [blame] | 792 | /* Must use staging buffer to copy linear texture to optimized */ |
Courtney Goeltzenleuchter | bfccf41 | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 793 | struct texture_object staging_texture; |
Courtney Goeltzenleuchter | 372e13c | 2015-02-13 17:52:46 -0700 | [diff] [blame] | 794 | |
| 795 | memset(&staging_texture, 0, sizeof(staging_texture)); |
| 796 | demo_prepare_texture_image(demo, tex_colors[i], &staging_texture, |
Courtney Goeltzenleuchter | cb67a32 | 2015-04-21 09:31:23 -0600 | [diff] [blame] | 797 | 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] | 798 | |
| 799 | demo_prepare_texture_image(demo, tex_colors[i], &demo->textures[i], |
Courtney Goeltzenleuchter | cb67a32 | 2015-04-21 09:31:23 -0600 | [diff] [blame] | 800 | VK_IMAGE_TILING_OPTIMAL, |
| 801 | (VK_IMAGE_USAGE_TRANSFER_DESTINATION_BIT | VK_IMAGE_USAGE_SAMPLED_BIT), |
| 802 | VK_MEMORY_PROPERTY_DEVICE_ONLY); |
Courtney Goeltzenleuchter | 372e13c | 2015-02-13 17:52:46 -0700 | [diff] [blame] | 803 | |
Courtney Goeltzenleuchter | bfccf41 | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 804 | demo_set_image_layout(demo, staging_texture.image, |
malnasse | 4b8ba4d | 2015-06-03 17:28:38 -0400 | [diff] [blame] | 805 | VK_IMAGE_ASPECT_COLOR, |
Courtney Goeltzenleuchter | bfccf41 | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 806 | staging_texture.imageLayout, |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 807 | VK_IMAGE_LAYOUT_TRANSFER_SOURCE_OPTIMAL); |
Courtney Goeltzenleuchter | 372e13c | 2015-02-13 17:52:46 -0700 | [diff] [blame] | 808 | |
Courtney Goeltzenleuchter | bfccf41 | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 809 | demo_set_image_layout(demo, demo->textures[i].image, |
malnasse | 4b8ba4d | 2015-06-03 17:28:38 -0400 | [diff] [blame] | 810 | VK_IMAGE_ASPECT_COLOR, |
Courtney Goeltzenleuchter | bfccf41 | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 811 | demo->textures[i].imageLayout, |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 812 | VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL); |
Courtney Goeltzenleuchter | 372e13c | 2015-02-13 17:52:46 -0700 | [diff] [blame] | 813 | |
Courtney Goeltzenleuchter | 382489d | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 814 | VkImageCopy copy_region = { |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 815 | .srcSubresource = { VK_IMAGE_ASPECT_COLOR, 0, 0 }, |
Courtney Goeltzenleuchter | 372e13c | 2015-02-13 17:52:46 -0700 | [diff] [blame] | 816 | .srcOffset = { 0, 0, 0 }, |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 817 | .destSubresource = { VK_IMAGE_ASPECT_COLOR, 0, 0 }, |
Courtney Goeltzenleuchter | 372e13c | 2015-02-13 17:52:46 -0700 | [diff] [blame] | 818 | .destOffset = { 0, 0, 0 }, |
| 819 | .extent = { staging_texture.tex_width, staging_texture.tex_height, 1 }, |
| 820 | }; |
Courtney Goeltzenleuchter | 633f9c5 | 2015-04-30 10:58:33 -0600 | [diff] [blame] | 821 | vkCmdCopyImage(demo->setup_cmd, |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 822 | staging_texture.image, VK_IMAGE_LAYOUT_TRANSFER_SOURCE_OPTIMAL, |
| 823 | demo->textures[i].image, VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL, |
Courtney Goeltzenleuchter | 51cbf30 | 2015-03-25 11:25:10 -0600 | [diff] [blame] | 824 | 1, ©_region); |
Courtney Goeltzenleuchter | 372e13c | 2015-02-13 17:52:46 -0700 | [diff] [blame] | 825 | |
Courtney Goeltzenleuchter | bfccf41 | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 826 | demo_set_image_layout(demo, demo->textures[i].image, |
malnasse | 4b8ba4d | 2015-06-03 17:28:38 -0400 | [diff] [blame] | 827 | VK_IMAGE_ASPECT_COLOR, |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 828 | VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL, |
Courtney Goeltzenleuchter | bfccf41 | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 829 | demo->textures[i].imageLayout); |
Courtney Goeltzenleuchter | 372e13c | 2015-02-13 17:52:46 -0700 | [diff] [blame] | 830 | |
Courtney Goeltzenleuchter | bfccf41 | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 831 | demo_flush_init_cmd(demo); |
Courtney Goeltzenleuchter | 372e13c | 2015-02-13 17:52:46 -0700 | [diff] [blame] | 832 | |
Courtney Goeltzenleuchter | 876629f | 2015-04-21 09:30:03 -0600 | [diff] [blame] | 833 | demo_destroy_texture_image(demo, &staging_texture); |
Courtney Goeltzenleuchter | 372e13c | 2015-02-13 17:52:46 -0700 | [diff] [blame] | 834 | } else { |
Tony Barbour | 8205d90 | 2015-04-16 15:59:00 -0600 | [diff] [blame] | 835 | /* Can't support VK_FORMAT_B8G8R8A8_UNORM !? */ |
Piers Daniell | 886be47 | 2015-02-23 16:23:13 -0700 | [diff] [blame] | 836 | assert(!"No support for B8G8R8A8_UNORM as texture image format"); |
Courtney Goeltzenleuchter | 372e13c | 2015-02-13 17:52:46 -0700 | [diff] [blame] | 837 | } |
Courtney Goeltzenleuchter | 3226a6f | 2015-02-11 18:17:22 -0700 | [diff] [blame] | 838 | |
Courtney Goeltzenleuchter | 382489d | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 839 | const VkSamplerCreateInfo sampler = { |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 840 | .sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO, |
Chia-I Wu | b043fe3 | 2014-10-06 15:30:33 +0800 | [diff] [blame] | 841 | .pNext = NULL, |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 842 | .magFilter = VK_TEX_FILTER_NEAREST, |
| 843 | .minFilter = VK_TEX_FILTER_NEAREST, |
Tony Barbour | 8205d90 | 2015-04-16 15:59:00 -0600 | [diff] [blame] | 844 | .mipMode = VK_TEX_MIPMAP_MODE_BASE, |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 845 | .addressU = VK_TEX_ADDRESS_WRAP, |
| 846 | .addressV = VK_TEX_ADDRESS_WRAP, |
| 847 | .addressW = VK_TEX_ADDRESS_WRAP, |
Chia-I Wu | b043fe3 | 2014-10-06 15:30:33 +0800 | [diff] [blame] | 848 | .mipLodBias = 0.0f, |
Courtney Goeltzenleuchter | bc9c816 | 2015-02-13 18:20:24 -0700 | [diff] [blame] | 849 | .maxAnisotropy = 1, |
Tony Barbour | 8205d90 | 2015-04-16 15:59:00 -0600 | [diff] [blame] | 850 | .compareOp = VK_COMPARE_OP_NEVER, |
Chia-I Wu | b043fe3 | 2014-10-06 15:30:33 +0800 | [diff] [blame] | 851 | .minLod = 0.0f, |
| 852 | .maxLod = 0.0f, |
Tony Barbour | 2c4e7c7 | 2015-06-25 16:56:44 -0600 | [diff] [blame] | 853 | .borderColor = VK_BORDER_COLOR_FLOAT_OPAQUE_WHITE, |
Cody Northrop | 0bf4e1d | 2015-08-11 15:50:55 -0600 | [diff] [blame] | 854 | .texelCoords = VK_FALSE, |
Chia-I Wu | b043fe3 | 2014-10-06 15:30:33 +0800 | [diff] [blame] | 855 | }; |
Courtney Goeltzenleuchter | 382489d | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 856 | VkImageViewCreateInfo view = { |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 857 | .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO, |
Chia-I Wu | b043fe3 | 2014-10-06 15:30:33 +0800 | [diff] [blame] | 858 | .pNext = NULL, |
Tony Barbour | de4124d | 2015-07-03 10:33:54 -0600 | [diff] [blame] | 859 | .image.handle = VK_NULL_HANDLE, |
Tony Barbour | 8205d90 | 2015-04-16 15:59:00 -0600 | [diff] [blame] | 860 | .viewType = VK_IMAGE_VIEW_TYPE_2D, |
Courtney Goeltzenleuchter | 372e13c | 2015-02-13 17:52:46 -0700 | [diff] [blame] | 861 | .format = tex_format, |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 862 | .channels = { VK_CHANNEL_SWIZZLE_R, |
| 863 | VK_CHANNEL_SWIZZLE_G, |
| 864 | VK_CHANNEL_SWIZZLE_B, |
| 865 | VK_CHANNEL_SWIZZLE_A, }, |
| 866 | .subresourceRange = { VK_IMAGE_ASPECT_COLOR, 0, 1, 0, 1 }, |
Chia-I Wu | b043fe3 | 2014-10-06 15:30:33 +0800 | [diff] [blame] | 867 | }; |
Jon Ashburn | a9ae383 | 2015-01-16 09:37:43 -0700 | [diff] [blame] | 868 | |
Chia-I Wu | b043fe3 | 2014-10-06 15:30:33 +0800 | [diff] [blame] | 869 | /* create sampler */ |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 870 | err = vkCreateSampler(demo->device, &sampler, |
Chia-I Wu | b043fe3 | 2014-10-06 15:30:33 +0800 | [diff] [blame] | 871 | &demo->textures[i].sampler); |
| 872 | assert(!err); |
| 873 | |
Chia-I Wu | b043fe3 | 2014-10-06 15:30:33 +0800 | [diff] [blame] | 874 | /* create image view */ |
| 875 | view.image = demo->textures[i].image; |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 876 | err = vkCreateImageView(demo->device, &view, |
Courtney Goeltzenleuchter | 3226a6f | 2015-02-11 18:17:22 -0700 | [diff] [blame] | 877 | &demo->textures[i].view); |
Chia-I Wu | b043fe3 | 2014-10-06 15:30:33 +0800 | [diff] [blame] | 878 | assert(!err); |
Chia-I Wu | b043fe3 | 2014-10-06 15:30:33 +0800 | [diff] [blame] | 879 | } |
| 880 | } |
| 881 | |
Chia-I Wu | 99621bc | 2014-10-08 11:52:22 +0800 | [diff] [blame] | 882 | static void demo_prepare_vertices(struct demo *demo) |
| 883 | { |
| 884 | const float vb[3][5] = { |
| 885 | /* position texcoord */ |
Chia-I Wu | e2504cb | 2015-04-22 14:20:52 +0800 | [diff] [blame] | 886 | { -1.0f, -1.0f, 0.2f, 0.0f, 0.0f }, |
| 887 | { 1.0f, -1.0f, 0.25f, 1.0f, 0.0f }, |
Chia-I Wu | 99621bc | 2014-10-08 11:52:22 +0800 | [diff] [blame] | 888 | { 0.0f, 1.0f, 1.0f, 0.5f, 1.0f }, |
| 889 | }; |
Courtney Goeltzenleuchter | ddcb619 | 2015-04-14 18:48:46 -0600 | [diff] [blame] | 890 | const VkBufferCreateInfo buf_info = { |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 891 | .sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO, |
Chia-I Wu | 714df45 | 2015-01-01 07:55:04 +0800 | [diff] [blame] | 892 | .pNext = NULL, |
| 893 | .size = sizeof(vb), |
Courtney Goeltzenleuchter | ad87081 | 2015-04-15 15:29:59 -0600 | [diff] [blame] | 894 | .usage = VK_BUFFER_USAGE_VERTEX_BUFFER_BIT, |
Chia-I Wu | 714df45 | 2015-01-01 07:55:04 +0800 | [diff] [blame] | 895 | .flags = 0, |
| 896 | }; |
Courtney Goeltzenleuchter | ddcb619 | 2015-04-14 18:48:46 -0600 | [diff] [blame] | 897 | VkMemoryAllocInfo mem_alloc = { |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 898 | .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOC_INFO, |
Mark Lobodzinski | 97dcd04 | 2015-04-16 08:52:00 -0500 | [diff] [blame] | 899 | .pNext = NULL, |
Chia-I Wu | 714df45 | 2015-01-01 07:55:04 +0800 | [diff] [blame] | 900 | .allocationSize = 0, |
Mark Lobodzinski | 7234629 | 2015-07-02 16:49:40 -0600 | [diff] [blame] | 901 | .memoryTypeIndex = 0, |
Chia-I Wu | 99621bc | 2014-10-08 11:52:22 +0800 | [diff] [blame] | 902 | }; |
Mark Lobodzinski | 2318261 | 2015-05-29 09:32:35 -0500 | [diff] [blame] | 903 | VkMemoryRequirements mem_reqs; |
Tony Barbour | 22a3086 | 2015-04-22 09:02:32 -0600 | [diff] [blame] | 904 | VkResult U_ASSERT_ONLY err; |
Chia-I Wu | 99621bc | 2014-10-08 11:52:22 +0800 | [diff] [blame] | 905 | void *data; |
| 906 | |
| 907 | memset(&demo->vertices, 0, sizeof(demo->vertices)); |
| 908 | |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 909 | err = vkCreateBuffer(demo->device, &buf_info, &demo->vertices.buf); |
Chia-I Wu | 714df45 | 2015-01-01 07:55:04 +0800 | [diff] [blame] | 910 | assert(!err); |
| 911 | |
Tony Barbour | de4124d | 2015-07-03 10:33:54 -0600 | [diff] [blame] | 912 | err = vkGetBufferMemoryRequirements(demo->device, |
| 913 | demo->vertices.buf, &mem_reqs); |
Chia-I Wu | 714df45 | 2015-01-01 07:55:04 +0800 | [diff] [blame] | 914 | |
Mark Lobodzinski | 7234629 | 2015-07-02 16:49:40 -0600 | [diff] [blame] | 915 | mem_alloc.allocationSize = mem_reqs.size; |
| 916 | err = memory_type_from_properties(demo, |
| 917 | mem_reqs.memoryTypeBits, |
| 918 | VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT, |
| 919 | &mem_alloc.memoryTypeIndex); |
| 920 | assert(!err); |
Chia-I Wu | 99621bc | 2014-10-08 11:52:22 +0800 | [diff] [blame] | 921 | |
Mark Lobodzinski | 2318261 | 2015-05-29 09:32:35 -0500 | [diff] [blame] | 922 | err = vkAllocMemory(demo->device, &mem_alloc, &demo->vertices.mem); |
| 923 | assert(!err); |
Chia-I Wu | 99621bc | 2014-10-08 11:52:22 +0800 | [diff] [blame] | 924 | |
Mark Lobodzinski | 2318261 | 2015-05-29 09:32:35 -0500 | [diff] [blame] | 925 | err = vkMapMemory(demo->device, demo->vertices.mem, 0, 0, 0, &data); |
| 926 | assert(!err); |
Chia-I Wu | 99621bc | 2014-10-08 11:52:22 +0800 | [diff] [blame] | 927 | |
Mark Lobodzinski | 2318261 | 2015-05-29 09:32:35 -0500 | [diff] [blame] | 928 | memcpy(data, vb, sizeof(vb)); |
Chia-I Wu | 99621bc | 2014-10-08 11:52:22 +0800 | [diff] [blame] | 929 | |
Mark Lobodzinski | 2318261 | 2015-05-29 09:32:35 -0500 | [diff] [blame] | 930 | err = vkUnmapMemory(demo->device, demo->vertices.mem); |
| 931 | assert(!err); |
| 932 | |
Tony Barbour | de4124d | 2015-07-03 10:33:54 -0600 | [diff] [blame] | 933 | err = vkBindBufferMemory(demo->device, demo->vertices.buf, |
Mark Lobodzinski | 2318261 | 2015-05-29 09:32:35 -0500 | [diff] [blame] | 934 | demo->vertices.mem, 0); |
| 935 | assert(!err); |
Chia-I Wu | 714df45 | 2015-01-01 07:55:04 +0800 | [diff] [blame] | 936 | |
Mark Lobodzinski | 0e0fb5c | 2015-06-23 15:11:57 -0600 | [diff] [blame] | 937 | 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] | 938 | demo->vertices.vi.pNext = NULL; |
| 939 | demo->vertices.vi.bindingCount = 1; |
| 940 | demo->vertices.vi.pVertexBindingDescriptions = demo->vertices.vi_bindings; |
| 941 | demo->vertices.vi.attributeCount = 2; |
| 942 | demo->vertices.vi.pVertexAttributeDescriptions = demo->vertices.vi_attrs; |
| 943 | |
Courtney Goeltzenleuchter | f5cdad0 | 2015-03-31 16:36:30 -0600 | [diff] [blame] | 944 | demo->vertices.vi_bindings[0].binding = VERTEX_BUFFER_BIND_ID; |
Chia-I Wu | 8d29d02 | 2014-10-08 12:14:39 +0800 | [diff] [blame] | 945 | demo->vertices.vi_bindings[0].strideInBytes = sizeof(vb[0]); |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 946 | 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] | 947 | |
Courtney Goeltzenleuchter | f5cdad0 | 2015-03-31 16:36:30 -0600 | [diff] [blame] | 948 | demo->vertices.vi_attrs[0].binding = VERTEX_BUFFER_BIND_ID; |
| 949 | demo->vertices.vi_attrs[0].location = 0; |
Tony Barbour | 8205d90 | 2015-04-16 15:59:00 -0600 | [diff] [blame] | 950 | demo->vertices.vi_attrs[0].format = VK_FORMAT_R32G32B32_SFLOAT; |
Chia-I Wu | 8d29d02 | 2014-10-08 12:14:39 +0800 | [diff] [blame] | 951 | demo->vertices.vi_attrs[0].offsetInBytes = 0; |
| 952 | |
Courtney Goeltzenleuchter | f5cdad0 | 2015-03-31 16:36:30 -0600 | [diff] [blame] | 953 | demo->vertices.vi_attrs[1].binding = VERTEX_BUFFER_BIND_ID; |
| 954 | demo->vertices.vi_attrs[1].location = 1; |
Tony Barbour | 8205d90 | 2015-04-16 15:59:00 -0600 | [diff] [blame] | 955 | demo->vertices.vi_attrs[1].format = VK_FORMAT_R32G32_SFLOAT; |
Chia-I Wu | 8d29d02 | 2014-10-08 12:14:39 +0800 | [diff] [blame] | 956 | demo->vertices.vi_attrs[1].offsetInBytes = sizeof(float) * 3; |
Chia-I Wu | 99621bc | 2014-10-08 11:52:22 +0800 | [diff] [blame] | 957 | } |
| 958 | |
Chia-I Wu | f838506 | 2015-01-04 16:27:24 +0800 | [diff] [blame] | 959 | static void demo_prepare_descriptor_layout(struct demo *demo) |
Chia-I Wu | b043fe3 | 2014-10-06 15:30:33 +0800 | [diff] [blame] | 960 | { |
Courtney Goeltzenleuchter | 382489d | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 961 | const VkDescriptorSetLayoutBinding layout_binding = { |
Courtney Goeltzenleuchter | ad87081 | 2015-04-15 15:29:59 -0600 | [diff] [blame] | 962 | .descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, |
Chia-I Wu | d3114a2 | 2015-05-25 16:22:52 +0800 | [diff] [blame] | 963 | .arraySize = DEMO_TEXTURE_COUNT, |
Tony Barbour | 8205d90 | 2015-04-16 15:59:00 -0600 | [diff] [blame] | 964 | .stageFlags = VK_SHADER_STAGE_FRAGMENT_BIT, |
Chia-I Wu | 310eece | 2015-03-27 12:56:09 +0800 | [diff] [blame] | 965 | .pImmutableSamplers = NULL, |
Chia-I Wu | b043fe3 | 2014-10-06 15:30:33 +0800 | [diff] [blame] | 966 | }; |
Courtney Goeltzenleuchter | 382489d | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 967 | const VkDescriptorSetLayoutCreateInfo descriptor_layout = { |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 968 | .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO, |
Chia-I Wu | fc9d913 | 2015-03-26 15:04:41 +0800 | [diff] [blame] | 969 | .pNext = NULL, |
| 970 | .count = 1, |
| 971 | .pBinding = &layout_binding, |
| 972 | }; |
Tony Barbour | 22a3086 | 2015-04-22 09:02:32 -0600 | [diff] [blame] | 973 | VkResult U_ASSERT_ONLY err; |
Chia-I Wu | b043fe3 | 2014-10-06 15:30:33 +0800 | [diff] [blame] | 974 | |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 975 | err = vkCreateDescriptorSetLayout(demo->device, |
Chia-I Wu | 7732cb2 | 2015-03-26 15:27:55 +0800 | [diff] [blame] | 976 | &descriptor_layout, &demo->desc_layout); |
| 977 | assert(!err); |
| 978 | |
Mark Lobodzinski | 556f721 | 2015-04-17 14:11:39 -0500 | [diff] [blame] | 979 | const VkPipelineLayoutCreateInfo pPipelineLayoutCreateInfo = { |
| 980 | .sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO, |
| 981 | .pNext = NULL, |
| 982 | .descriptorSetCount = 1, |
| 983 | .pSetLayouts = &demo->desc_layout, |
| 984 | }; |
| 985 | |
| 986 | err = vkCreatePipelineLayout(demo->device, |
| 987 | &pPipelineLayoutCreateInfo, |
| 988 | &demo->pipeline_layout); |
Chia-I Wu | b043fe3 | 2014-10-06 15:30:33 +0800 | [diff] [blame] | 989 | assert(!err); |
Chia-I Wu | b043fe3 | 2014-10-06 15:30:33 +0800 | [diff] [blame] | 990 | } |
| 991 | |
Chia-I Wu | 76cd422 | 2015-07-08 13:34:24 +0800 | [diff] [blame] | 992 | static void demo_prepare_render_pass(struct demo *demo) |
| 993 | { |
Chia-I Wu | c278df8 | 2015-07-07 11:50:03 +0800 | [diff] [blame] | 994 | const VkAttachmentDescription attachments[2] = { |
| 995 | [0] = { |
| 996 | .sType = VK_STRUCTURE_TYPE_ATTACHMENT_DESCRIPTION, |
| 997 | .pNext = NULL, |
| 998 | .format = demo->format, |
| 999 | .samples = 1, |
| 1000 | .loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR, |
| 1001 | .storeOp = VK_ATTACHMENT_STORE_OP_STORE, |
| 1002 | .stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE, |
| 1003 | .stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE, |
| 1004 | .initialLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL, |
| 1005 | .finalLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL, |
| 1006 | }, |
| 1007 | [1] = { |
| 1008 | .sType = VK_STRUCTURE_TYPE_ATTACHMENT_DESCRIPTION, |
| 1009 | .pNext = NULL, |
| 1010 | .format = demo->depth.format, |
| 1011 | .samples = 1, |
| 1012 | .loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR, |
| 1013 | .storeOp = VK_ATTACHMENT_STORE_OP_DONT_CARE, |
| 1014 | .stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE, |
| 1015 | .stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE, |
| 1016 | .initialLayout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL, |
| 1017 | .finalLayout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL, |
| 1018 | }, |
Chia-I Wu | 76cd422 | 2015-07-08 13:34:24 +0800 | [diff] [blame] | 1019 | }; |
Chia-I Wu | c278df8 | 2015-07-07 11:50:03 +0800 | [diff] [blame] | 1020 | const VkAttachmentReference color_reference = { |
| 1021 | .attachment = 0, |
| 1022 | .layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL, |
| 1023 | }; |
| 1024 | const VkSubpassDescription subpass = { |
| 1025 | .sType = VK_STRUCTURE_TYPE_SUBPASS_DESCRIPTION, |
| 1026 | .pNext = NULL, |
| 1027 | .pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS, |
| 1028 | .flags = 0, |
| 1029 | .inputCount = 0, |
Cody Northrop | 6de6b0b | 2015-08-04 11:16:41 -0600 | [diff] [blame] | 1030 | .pInputAttachments = NULL, |
Chia-I Wu | c278df8 | 2015-07-07 11:50:03 +0800 | [diff] [blame] | 1031 | .colorCount = 1, |
Cody Northrop | 6de6b0b | 2015-08-04 11:16:41 -0600 | [diff] [blame] | 1032 | .pColorAttachments = &color_reference, |
| 1033 | .pResolveAttachments = NULL, |
Chia-I Wu | c278df8 | 2015-07-07 11:50:03 +0800 | [diff] [blame] | 1034 | .depthStencilAttachment = { |
| 1035 | .attachment = 1, |
| 1036 | .layout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL, |
| 1037 | }, |
| 1038 | .preserveCount = 0, |
Cody Northrop | 6de6b0b | 2015-08-04 11:16:41 -0600 | [diff] [blame] | 1039 | .pPreserveAttachments = NULL, |
Chia-I Wu | c278df8 | 2015-07-07 11:50:03 +0800 | [diff] [blame] | 1040 | }; |
Chia-I Wu | 76cd422 | 2015-07-08 13:34:24 +0800 | [diff] [blame] | 1041 | const VkRenderPassCreateInfo rp_info = { |
| 1042 | .sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO, |
| 1043 | .pNext = NULL, |
Chia-I Wu | c278df8 | 2015-07-07 11:50:03 +0800 | [diff] [blame] | 1044 | .attachmentCount = 2, |
| 1045 | .pAttachments = attachments, |
| 1046 | .subpassCount = 1, |
| 1047 | .pSubpasses = &subpass, |
| 1048 | .dependencyCount = 0, |
| 1049 | .pDependencies = NULL, |
Chia-I Wu | 76cd422 | 2015-07-08 13:34:24 +0800 | [diff] [blame] | 1050 | }; |
Chia-I Wu | c278df8 | 2015-07-07 11:50:03 +0800 | [diff] [blame] | 1051 | VkResult U_ASSERT_ONLY err; |
Chia-I Wu | 76cd422 | 2015-07-08 13:34:24 +0800 | [diff] [blame] | 1052 | |
| 1053 | err = vkCreateRenderPass(demo->device, &rp_info, &demo->render_pass); |
| 1054 | assert(!err); |
| 1055 | } |
| 1056 | |
Courtney Goeltzenleuchter | 382489d | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 1057 | static VkShader demo_prepare_shader(struct demo *demo, |
Tony Barbour | 8205d90 | 2015-04-16 15:59:00 -0600 | [diff] [blame] | 1058 | VkShaderStage stage, |
Mark Lobodzinski | a43e24c | 2015-08-10 13:40:32 -0600 | [diff] [blame] | 1059 | VkShaderModule* pShaderModule, |
Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 1060 | const void *code, |
Mark Lobodzinski | e2d07a5 | 2015-01-29 08:55:56 -0600 | [diff] [blame] | 1061 | size_t size) |
Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 1062 | { |
Courtney Goeltzenleuchter | 2d034fd | 2015-06-28 13:01:17 -0600 | [diff] [blame] | 1063 | VkShaderModuleCreateInfo moduleCreateInfo; |
| 1064 | VkShaderCreateInfo shaderCreateInfo; |
Courtney Goeltzenleuchter | 382489d | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 1065 | VkShader shader; |
| 1066 | VkResult err; |
Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 1067 | |
Courtney Goeltzenleuchter | 2d034fd | 2015-06-28 13:01:17 -0600 | [diff] [blame] | 1068 | |
| 1069 | moduleCreateInfo.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO; |
| 1070 | moduleCreateInfo.pNext = NULL; |
| 1071 | |
| 1072 | shaderCreateInfo.sType = VK_STRUCTURE_TYPE_SHADER_CREATE_INFO; |
| 1073 | shaderCreateInfo.pNext = NULL; |
Courtney Goeltzenleuchter | 3f2606d | 2014-10-13 17:51:58 -0600 | [diff] [blame] | 1074 | |
Cody Northrop | 75db032 | 2015-05-28 11:27:16 -0600 | [diff] [blame] | 1075 | if (!demo->use_glsl) { |
Courtney Goeltzenleuchter | 2d034fd | 2015-06-28 13:01:17 -0600 | [diff] [blame] | 1076 | moduleCreateInfo.codeSize = size; |
| 1077 | moduleCreateInfo.pCode = code; |
| 1078 | moduleCreateInfo.flags = 0; |
Mark Lobodzinski | a43e24c | 2015-08-10 13:40:32 -0600 | [diff] [blame] | 1079 | err = vkCreateShaderModule(demo->device, &moduleCreateInfo, pShaderModule); |
Courtney Goeltzenleuchter | 2d034fd | 2015-06-28 13:01:17 -0600 | [diff] [blame] | 1080 | if (err) { |
| 1081 | free((void *) moduleCreateInfo.pCode); |
| 1082 | } |
Courtney Goeltzenleuchter | 3f2606d | 2014-10-13 17:51:58 -0600 | [diff] [blame] | 1083 | |
Courtney Goeltzenleuchter | 2d034fd | 2015-06-28 13:01:17 -0600 | [diff] [blame] | 1084 | shaderCreateInfo.flags = 0; |
Mark Lobodzinski | a43e24c | 2015-08-10 13:40:32 -0600 | [diff] [blame] | 1085 | shaderCreateInfo.module = *pShaderModule; |
Piers Daniell | 1cf7fe1 | 2015-07-16 09:35:35 -0600 | [diff] [blame] | 1086 | shaderCreateInfo.pName = "main"; |
Cody Northrop | 87ae5e1 | 2015-08-24 15:11:10 -0600 | [diff] [blame] | 1087 | shaderCreateInfo.stage = stage; |
Courtney Goeltzenleuchter | 2d034fd | 2015-06-28 13:01:17 -0600 | [diff] [blame] | 1088 | err = vkCreateShader(demo->device, &shaderCreateInfo, &shader); |
Mark Lobodzinski | 8a19064 | 2015-08-07 10:17:13 -0600 | [diff] [blame] | 1089 | assert(!err); |
Cody Northrop | 75db032 | 2015-05-28 11:27:16 -0600 | [diff] [blame] | 1090 | } else { |
| 1091 | // Create fake SPV structure to feed GLSL |
| 1092 | // to the driver "under the covers" |
Courtney Goeltzenleuchter | 2d034fd | 2015-06-28 13:01:17 -0600 | [diff] [blame] | 1093 | moduleCreateInfo.codeSize = 3 * sizeof(uint32_t) + size + 1; |
| 1094 | moduleCreateInfo.pCode = malloc(moduleCreateInfo.codeSize); |
| 1095 | moduleCreateInfo.flags = 0; |
Courtney Goeltzenleuchter | 3f2606d | 2014-10-13 17:51:58 -0600 | [diff] [blame] | 1096 | |
Cody Northrop | 75db032 | 2015-05-28 11:27:16 -0600 | [diff] [blame] | 1097 | /* try version 0 first: VkShaderStage followed by GLSL */ |
Courtney Goeltzenleuchter | 2d034fd | 2015-06-28 13:01:17 -0600 | [diff] [blame] | 1098 | ((uint32_t *) moduleCreateInfo.pCode)[0] = ICD_SPV_MAGIC; |
| 1099 | ((uint32_t *) moduleCreateInfo.pCode)[1] = 0; |
| 1100 | ((uint32_t *) moduleCreateInfo.pCode)[2] = stage; |
| 1101 | memcpy(((uint32_t *) moduleCreateInfo.pCode + 3), code, size + 1); |
Cody Northrop | 75db032 | 2015-05-28 11:27:16 -0600 | [diff] [blame] | 1102 | |
Mark Lobodzinski | a43e24c | 2015-08-10 13:40:32 -0600 | [diff] [blame] | 1103 | err = vkCreateShaderModule(demo->device, &moduleCreateInfo, pShaderModule); |
Cody Northrop | 75db032 | 2015-05-28 11:27:16 -0600 | [diff] [blame] | 1104 | if (err) { |
Courtney Goeltzenleuchter | 2d034fd | 2015-06-28 13:01:17 -0600 | [diff] [blame] | 1105 | free((void *) moduleCreateInfo.pCode); |
Cody Northrop | 75db032 | 2015-05-28 11:27:16 -0600 | [diff] [blame] | 1106 | } |
Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 1107 | |
Courtney Goeltzenleuchter | 2d034fd | 2015-06-28 13:01:17 -0600 | [diff] [blame] | 1108 | shaderCreateInfo.flags = 0; |
Mark Lobodzinski | a43e24c | 2015-08-10 13:40:32 -0600 | [diff] [blame] | 1109 | shaderCreateInfo.module = *pShaderModule; |
Piers Daniell | 1cf7fe1 | 2015-07-16 09:35:35 -0600 | [diff] [blame] | 1110 | shaderCreateInfo.pName = "main"; |
Cody Northrop | 87ae5e1 | 2015-08-24 15:11:10 -0600 | [diff] [blame] | 1111 | shaderCreateInfo.stage = stage; |
Courtney Goeltzenleuchter | 2d034fd | 2015-06-28 13:01:17 -0600 | [diff] [blame] | 1112 | err = vkCreateShader(demo->device, &shaderCreateInfo, &shader); |
Mark Lobodzinski | 8a19064 | 2015-08-07 10:17:13 -0600 | [diff] [blame] | 1113 | assert(!err); |
Courtney Goeltzenleuchter | 2d034fd | 2015-06-28 13:01:17 -0600 | [diff] [blame] | 1114 | } |
Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 1115 | return shader; |
| 1116 | } |
| 1117 | |
Cody Northrop | 75db032 | 2015-05-28 11:27:16 -0600 | [diff] [blame] | 1118 | char *demo_read_spv(const char *filename, size_t *psize) |
| 1119 | { |
| 1120 | long int size; |
| 1121 | void *shader_code; |
Tony Barbour | 9687cb1 | 2015-07-14 13:34:05 -0600 | [diff] [blame] | 1122 | size_t retVal; |
Cody Northrop | 75db032 | 2015-05-28 11:27:16 -0600 | [diff] [blame] | 1123 | |
| 1124 | FILE *fp = fopen(filename, "rb"); |
| 1125 | if (!fp) return NULL; |
| 1126 | |
| 1127 | fseek(fp, 0L, SEEK_END); |
| 1128 | size = ftell(fp); |
| 1129 | |
| 1130 | fseek(fp, 0L, SEEK_SET); |
| 1131 | |
| 1132 | shader_code = malloc(size); |
Tony Barbour | 9687cb1 | 2015-07-14 13:34:05 -0600 | [diff] [blame] | 1133 | retVal = fread(shader_code, size, 1, fp); |
| 1134 | if (!retVal) return NULL; |
Cody Northrop | 75db032 | 2015-05-28 11:27:16 -0600 | [diff] [blame] | 1135 | |
| 1136 | *psize = size; |
| 1137 | |
| 1138 | return shader_code; |
| 1139 | } |
| 1140 | |
Courtney Goeltzenleuchter | 382489d | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 1141 | static VkShader demo_prepare_vs(struct demo *demo) |
Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 1142 | { |
Cody Northrop | 75db032 | 2015-05-28 11:27:16 -0600 | [diff] [blame] | 1143 | if (!demo->use_glsl) { |
| 1144 | void *vertShaderCode; |
| 1145 | size_t size; |
| 1146 | |
| 1147 | vertShaderCode = demo_read_spv("tri-vert.spv", &size); |
| 1148 | |
| 1149 | return demo_prepare_shader(demo, VK_SHADER_STAGE_VERTEX, |
Mark Lobodzinski | a43e24c | 2015-08-10 13:40:32 -0600 | [diff] [blame] | 1150 | &demo->vert_shader_module, |
| 1151 | vertShaderCode, size); |
Cody Northrop | 75db032 | 2015-05-28 11:27:16 -0600 | [diff] [blame] | 1152 | } else { |
| 1153 | static const char *vertShaderText = |
Mark Lobodzinski | ba4d2f0 | 2015-04-06 15:24:40 -0500 | [diff] [blame] | 1154 | "#version 140\n" |
Courtney Goeltzenleuchter | f5cdad0 | 2015-03-31 16:36:30 -0600 | [diff] [blame] | 1155 | "#extension GL_ARB_separate_shader_objects : enable\n" |
| 1156 | "#extension GL_ARB_shading_language_420pack : enable\n" |
| 1157 | "layout (location = 0) in vec4 pos;\n" |
| 1158 | "layout (location = 1) in vec2 attr;\n" |
Chia-I Wu | f5caeb0 | 2014-10-25 12:11:27 +0800 | [diff] [blame] | 1159 | "out vec2 texcoord;\n" |
Courtney Goeltzenleuchter | 3f2606d | 2014-10-13 17:51:58 -0600 | [diff] [blame] | 1160 | "void main() {\n" |
Chia-I Wu | f5caeb0 | 2014-10-25 12:11:27 +0800 | [diff] [blame] | 1161 | " texcoord = attr;\n" |
| 1162 | " gl_Position = pos;\n" |
Courtney Goeltzenleuchter | 3f2606d | 2014-10-13 17:51:58 -0600 | [diff] [blame] | 1163 | "}\n"; |
Courtney Goeltzenleuchter | ef7301b | 2014-09-17 13:17:12 -0600 | [diff] [blame] | 1164 | |
Cody Northrop | 75db032 | 2015-05-28 11:27:16 -0600 | [diff] [blame] | 1165 | return demo_prepare_shader(demo, VK_SHADER_STAGE_VERTEX, |
Mark Lobodzinski | a43e24c | 2015-08-10 13:40:32 -0600 | [diff] [blame] | 1166 | &demo->vert_shader_module, |
Cody Northrop | 75db032 | 2015-05-28 11:27:16 -0600 | [diff] [blame] | 1167 | (const void *) vertShaderText, |
| 1168 | strlen(vertShaderText)); |
| 1169 | } |
Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 1170 | } |
| 1171 | |
Courtney Goeltzenleuchter | 382489d | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 1172 | static VkShader demo_prepare_fs(struct demo *demo) |
Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 1173 | { |
Cody Northrop | 75db032 | 2015-05-28 11:27:16 -0600 | [diff] [blame] | 1174 | if (!demo->use_glsl) { |
| 1175 | void *fragShaderCode; |
| 1176 | size_t size; |
Courtney Goeltzenleuchter | ef7301b | 2014-09-17 13:17:12 -0600 | [diff] [blame] | 1177 | |
Cody Northrop | 75db032 | 2015-05-28 11:27:16 -0600 | [diff] [blame] | 1178 | fragShaderCode = demo_read_spv("tri-frag.spv", &size); |
| 1179 | |
| 1180 | return demo_prepare_shader(demo, VK_SHADER_STAGE_FRAGMENT, |
Mark Lobodzinski | a43e24c | 2015-08-10 13:40:32 -0600 | [diff] [blame] | 1181 | &demo->frag_shader_module, |
| 1182 | fragShaderCode, size); |
Cody Northrop | 75db032 | 2015-05-28 11:27:16 -0600 | [diff] [blame] | 1183 | } else { |
| 1184 | static const char *fragShaderText = |
| 1185 | "#version 140\n" |
| 1186 | "#extension GL_ARB_separate_shader_objects : enable\n" |
| 1187 | "#extension GL_ARB_shading_language_420pack : enable\n" |
| 1188 | "layout (binding = 0) uniform sampler2D tex;\n" |
| 1189 | "layout (location = 0) in vec2 texcoord;\n" |
| 1190 | "layout (location = 0) out vec4 uFragColor;\n" |
| 1191 | "void main() {\n" |
| 1192 | " uFragColor = texture(tex, texcoord);\n" |
| 1193 | "}\n"; |
| 1194 | |
| 1195 | return demo_prepare_shader(demo, VK_SHADER_STAGE_FRAGMENT, |
Mark Lobodzinski | a43e24c | 2015-08-10 13:40:32 -0600 | [diff] [blame] | 1196 | &demo->frag_shader_module, |
Cody Northrop | 75db032 | 2015-05-28 11:27:16 -0600 | [diff] [blame] | 1197 | (const void *) fragShaderText, |
| 1198 | strlen(fragShaderText)); |
| 1199 | } |
Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 1200 | } |
| 1201 | |
| 1202 | static void demo_prepare_pipeline(struct demo *demo) |
| 1203 | { |
Courtney Goeltzenleuchter | 382489d | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 1204 | VkGraphicsPipelineCreateInfo pipeline; |
Jon Ashburn | 0d60d27 | 2015-07-09 15:02:25 -0600 | [diff] [blame] | 1205 | VkPipelineCacheCreateInfo pipelineCache; |
Mark Lobodzinski | 0e0fb5c | 2015-06-23 15:11:57 -0600 | [diff] [blame] | 1206 | |
Tony Barbour | e307f58 | 2015-07-10 15:29:03 -0600 | [diff] [blame] | 1207 | VkPipelineVertexInputStateCreateInfo vi; |
| 1208 | VkPipelineInputAssemblyStateCreateInfo ia; |
| 1209 | VkPipelineRasterStateCreateInfo rs; |
| 1210 | VkPipelineColorBlendStateCreateInfo cb; |
| 1211 | VkPipelineDepthStencilStateCreateInfo ds; |
| 1212 | VkPipelineViewportStateCreateInfo vp; |
| 1213 | VkPipelineMultisampleStateCreateInfo ms; |
Mark Lobodzinski | 0e0fb5c | 2015-06-23 15:11:57 -0600 | [diff] [blame] | 1214 | |
Tony Barbour | 22a3086 | 2015-04-22 09:02:32 -0600 | [diff] [blame] | 1215 | VkResult U_ASSERT_ONLY err; |
Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 1216 | |
| 1217 | memset(&pipeline, 0, sizeof(pipeline)); |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1218 | pipeline.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO; |
Mark Lobodzinski | 556f721 | 2015-04-17 14:11:39 -0500 | [diff] [blame] | 1219 | pipeline.layout = demo->pipeline_layout; |
Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 1220 | |
Chia-I Wu | 8d29d02 | 2014-10-08 12:14:39 +0800 | [diff] [blame] | 1221 | vi = demo->vertices.vi; |
| 1222 | |
Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 1223 | memset(&ia, 0, sizeof(ia)); |
Tony Barbour | e307f58 | 2015-07-10 15:29:03 -0600 | [diff] [blame] | 1224 | ia.sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO; |
Tony Barbour | 8205d90 | 2015-04-16 15:59:00 -0600 | [diff] [blame] | 1225 | ia.topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST; |
Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 1226 | |
| 1227 | memset(&rs, 0, sizeof(rs)); |
Tony Barbour | e307f58 | 2015-07-10 15:29:03 -0600 | [diff] [blame] | 1228 | rs.sType = VK_STRUCTURE_TYPE_PIPELINE_RASTER_STATE_CREATE_INFO; |
Tony Barbour | 8205d90 | 2015-04-16 15:59:00 -0600 | [diff] [blame] | 1229 | rs.fillMode = VK_FILL_MODE_SOLID; |
Chia-I Wu | c414ba8 | 2015-04-22 15:44:24 +0800 | [diff] [blame] | 1230 | rs.cullMode = VK_CULL_MODE_BACK; |
| 1231 | rs.frontFace = VK_FRONT_FACE_CW; |
Chia-I Wu | e2504cb | 2015-04-22 14:20:52 +0800 | [diff] [blame] | 1232 | rs.depthClipEnable = VK_TRUE; |
Cody Northrop | f5bd225 | 2015-08-17 11:10:49 -0600 | [diff] [blame] | 1233 | rs.rasterizerDiscardEnable = VK_FALSE; |
| 1234 | rs.depthBiasEnable = VK_FALSE; |
Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 1235 | |
| 1236 | memset(&cb, 0, sizeof(cb)); |
Tony Barbour | e307f58 | 2015-07-10 15:29:03 -0600 | [diff] [blame] | 1237 | cb.sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO; |
| 1238 | VkPipelineColorBlendAttachmentState att_state[1]; |
Tony Barbour | fa6cac7 | 2015-01-16 14:27:35 -0700 | [diff] [blame] | 1239 | memset(att_state, 0, sizeof(att_state)); |
Tony Barbour | fa6cac7 | 2015-01-16 14:27:35 -0700 | [diff] [blame] | 1240 | att_state[0].channelWriteMask = 0xf; |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1241 | att_state[0].blendEnable = VK_FALSE; |
Tony Barbour | fa6cac7 | 2015-01-16 14:27:35 -0700 | [diff] [blame] | 1242 | cb.attachmentCount = 1; |
| 1243 | cb.pAttachments = att_state; |
Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 1244 | |
Tony Barbour | fa6cac7 | 2015-01-16 14:27:35 -0700 | [diff] [blame] | 1245 | memset(&vp, 0, sizeof(vp)); |
Tony Barbour | e307f58 | 2015-07-10 15:29:03 -0600 | [diff] [blame] | 1246 | vp.sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO; |
Tony Barbour | 8205d90 | 2015-04-16 15:59:00 -0600 | [diff] [blame] | 1247 | vp.viewportCount = 1; |
Tony Barbour | fa6cac7 | 2015-01-16 14:27:35 -0700 | [diff] [blame] | 1248 | |
| 1249 | memset(&ds, 0, sizeof(ds)); |
Tony Barbour | e307f58 | 2015-07-10 15:29:03 -0600 | [diff] [blame] | 1250 | ds.sType = VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO; |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1251 | ds.depthTestEnable = VK_TRUE; |
| 1252 | ds.depthWriteEnable = VK_TRUE; |
Tony Barbour | 8205d90 | 2015-04-16 15:59:00 -0600 | [diff] [blame] | 1253 | ds.depthCompareOp = VK_COMPARE_OP_LESS_EQUAL; |
Cody Northrop | e4bc694 | 2015-08-26 10:01:32 -0600 | [diff] [blame] | 1254 | ds.depthBoundsTestEnable = VK_FALSE; |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1255 | ds.back.stencilFailOp = VK_STENCIL_OP_KEEP; |
| 1256 | ds.back.stencilPassOp = VK_STENCIL_OP_KEEP; |
Tony Barbour | 8205d90 | 2015-04-16 15:59:00 -0600 | [diff] [blame] | 1257 | ds.back.stencilCompareOp = VK_COMPARE_OP_ALWAYS; |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1258 | ds.stencilTestEnable = VK_FALSE; |
Tony Barbour | fa6cac7 | 2015-01-16 14:27:35 -0700 | [diff] [blame] | 1259 | ds.front = ds.back; |
Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 1260 | |
Tony Barbour | fa6cac7 | 2015-01-16 14:27:35 -0700 | [diff] [blame] | 1261 | memset(&ms, 0, sizeof(ms)); |
Tony Barbour | e307f58 | 2015-07-10 15:29:03 -0600 | [diff] [blame] | 1262 | ms.sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO; |
Cody Northrop | e9825b7 | 2015-08-04 14:34:54 -0600 | [diff] [blame] | 1263 | ms.pSampleMask = NULL; |
Tony Barbour | e094edf | 2015-06-26 10:18:34 -0600 | [diff] [blame] | 1264 | ms.rasterSamples = 1; |
Chia-I Wu | b043fe3 | 2014-10-06 15:30:33 +0800 | [diff] [blame] | 1265 | |
Mark Lobodzinski | 0e0fb5c | 2015-06-23 15:11:57 -0600 | [diff] [blame] | 1266 | // Two stages: vs and fs |
| 1267 | pipeline.stageCount = 2; |
| 1268 | VkPipelineShaderStageCreateInfo shaderStages[2]; |
| 1269 | memset(&shaderStages, 0, 2 * sizeof(VkPipelineShaderStageCreateInfo)); |
| 1270 | |
| 1271 | shaderStages[0].sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO; |
| 1272 | shaderStages[0].stage = VK_SHADER_STAGE_VERTEX; |
| 1273 | shaderStages[0].shader = demo_prepare_vs(demo); |
Mark Lobodzinski | 0e0fb5c | 2015-06-23 15:11:57 -0600 | [diff] [blame] | 1274 | |
| 1275 | shaderStages[1].sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO; |
| 1276 | shaderStages[1].stage = VK_SHADER_STAGE_FRAGMENT; |
| 1277 | shaderStages[1].shader = demo_prepare_fs(demo); |
| 1278 | |
Tony Barbour | e307f58 | 2015-07-10 15:29:03 -0600 | [diff] [blame] | 1279 | pipeline.pVertexInputState = &vi; |
| 1280 | pipeline.pInputAssemblyState = &ia; |
| 1281 | pipeline.pRasterState = &rs; |
| 1282 | pipeline.pColorBlendState = &cb; |
| 1283 | pipeline.pMultisampleState = &ms; |
| 1284 | pipeline.pViewportState = &vp; |
| 1285 | pipeline.pDepthStencilState = &ds; |
| 1286 | pipeline.pStages = shaderStages; |
Tony Barbour | d31ab43 | 2015-08-06 14:32:54 -0600 | [diff] [blame] | 1287 | pipeline.renderPass = demo->render_pass; |
Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 1288 | |
Jon Ashburn | 0d60d27 | 2015-07-09 15:02:25 -0600 | [diff] [blame] | 1289 | memset(&pipelineCache, 0, sizeof(pipelineCache)); |
| 1290 | pipelineCache.sType = VK_STRUCTURE_TYPE_PIPELINE_CACHE_CREATE_INFO; |
| 1291 | |
| 1292 | err = vkCreatePipelineCache(demo->device, &pipelineCache, &demo->pipelineCache); |
| 1293 | assert(!err); |
| 1294 | err = vkCreateGraphicsPipelines(demo->device, demo->pipelineCache, 1, &pipeline, &demo->pipeline); |
Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 1295 | assert(!err); |
| 1296 | |
Mark Lobodzinski | 8a19064 | 2015-08-07 10:17:13 -0600 | [diff] [blame] | 1297 | err = vkDestroyPipelineCache(demo->device, demo->pipelineCache); |
| 1298 | assert(!err); |
| 1299 | |
Mark Lobodzinski | 0e0fb5c | 2015-06-23 15:11:57 -0600 | [diff] [blame] | 1300 | for (uint32_t i = 0; i < pipeline.stageCount; i++) { |
Tony Barbour | de4124d | 2015-07-03 10:33:54 -0600 | [diff] [blame] | 1301 | vkDestroyShader(demo->device, shaderStages[i].shader); |
Mark Lobodzinski | 0e0fb5c | 2015-06-23 15:11:57 -0600 | [diff] [blame] | 1302 | } |
Mark Lobodzinski | a43e24c | 2015-08-10 13:40:32 -0600 | [diff] [blame] | 1303 | vkDestroyShaderModule(demo->device, demo->frag_shader_module); |
| 1304 | vkDestroyShaderModule(demo->device, demo->vert_shader_module); |
Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 1305 | } |
| 1306 | |
| 1307 | static void demo_prepare_dynamic_states(struct demo *demo) |
| 1308 | { |
Tony Barbour | de4124d | 2015-07-03 10:33:54 -0600 | [diff] [blame] | 1309 | VkDynamicViewportStateCreateInfo viewport_create; |
Cody Northrop | e4bc694 | 2015-08-26 10:01:32 -0600 | [diff] [blame] | 1310 | VkDynamicLineWidthStateCreateInfo line_width; |
| 1311 | VkDynamicDepthBiasStateCreateInfo depth_bias; |
| 1312 | VkDynamicBlendStateCreateInfo blend; |
| 1313 | VkDynamicDepthBoundsStateCreateInfo depth_bounds; |
Cody Northrop | 2605cb0 | 2015-08-18 15:21:16 -0600 | [diff] [blame] | 1314 | VkDynamicStencilStateCreateInfo stencil; |
Tony Barbour | 22a3086 | 2015-04-22 09:02:32 -0600 | [diff] [blame] | 1315 | VkResult U_ASSERT_ONLY err; |
Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 1316 | |
Tony Barbour | fa6cac7 | 2015-01-16 14:27:35 -0700 | [diff] [blame] | 1317 | memset(&viewport_create, 0, sizeof(viewport_create)); |
Tony Barbour | de4124d | 2015-07-03 10:33:54 -0600 | [diff] [blame] | 1318 | viewport_create.sType = VK_STRUCTURE_TYPE_DYNAMIC_VIEWPORT_STATE_CREATE_INFO; |
Courtney Goeltzenleuchter | c6e32f9 | 2015-02-11 14:13:34 -0700 | [diff] [blame] | 1319 | viewport_create.viewportAndScissorCount = 1; |
Courtney Goeltzenleuchter | 382489d | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 1320 | VkViewport viewport; |
Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 1321 | memset(&viewport, 0, sizeof(viewport)); |
Mark Lobodzinski | e2d07a5 | 2015-01-29 08:55:56 -0600 | [diff] [blame] | 1322 | viewport.height = (float) demo->height; |
Courtney Goeltzenleuchter | c6e32f9 | 2015-02-11 14:13:34 -0700 | [diff] [blame] | 1323 | viewport.width = (float) demo->width; |
Mark Lobodzinski | e2d07a5 | 2015-01-29 08:55:56 -0600 | [diff] [blame] | 1324 | viewport.minDepth = (float) 0.0f; |
| 1325 | viewport.maxDepth = (float) 1.0f; |
Piers Daniell | 886be47 | 2015-02-23 16:23:13 -0700 | [diff] [blame] | 1326 | viewport_create.pViewports = &viewport; |
Chris Forbes | 2951d7d | 2015-06-22 17:21:59 +1200 | [diff] [blame] | 1327 | VkRect2D scissor; |
Piers Daniell | 886be47 | 2015-02-23 16:23:13 -0700 | [diff] [blame] | 1328 | memset(&scissor, 0, sizeof(scissor)); |
Courtney Goeltzenleuchter | c6e32f9 | 2015-02-11 14:13:34 -0700 | [diff] [blame] | 1329 | scissor.extent.width = demo->width; |
| 1330 | scissor.extent.height = demo->height; |
| 1331 | scissor.offset.x = 0; |
| 1332 | scissor.offset.y = 0; |
Courtney Goeltzenleuchter | c6e32f9 | 2015-02-11 14:13:34 -0700 | [diff] [blame] | 1333 | viewport_create.pScissors = &scissor; |
Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 1334 | |
Cody Northrop | e4bc694 | 2015-08-26 10:01:32 -0600 | [diff] [blame] | 1335 | memset(&line_width, 0, sizeof(line_width)); |
| 1336 | line_width.sType = VK_STRUCTURE_TYPE_DYNAMIC_LINE_WIDTH_STATE_CREATE_INFO; |
| 1337 | line_width.lineWidth = 1.0; |
Cody Northrop | f5bd225 | 2015-08-17 11:10:49 -0600 | [diff] [blame] | 1338 | |
Cody Northrop | e4bc694 | 2015-08-26 10:01:32 -0600 | [diff] [blame] | 1339 | memset(&depth_bias, 0, sizeof(depth_bias)); |
| 1340 | depth_bias.sType = VK_STRUCTURE_TYPE_DYNAMIC_DEPTH_BIAS_STATE_CREATE_INFO; |
| 1341 | depth_bias.depthBias = 0.0f; |
| 1342 | depth_bias.depthBiasClamp = 0.0f; |
| 1343 | depth_bias.slopeScaledDepthBias = 0.0f; |
Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 1344 | |
Cody Northrop | e4bc694 | 2015-08-26 10:01:32 -0600 | [diff] [blame] | 1345 | memset(&blend, 0, sizeof(blend)); |
| 1346 | blend.sType = VK_STRUCTURE_TYPE_DYNAMIC_BLEND_STATE_CREATE_INFO; |
| 1347 | blend.blendConst[0] = 1.0f; |
| 1348 | blend.blendConst[1] = 1.0f; |
| 1349 | blend.blendConst[2] = 1.0f; |
| 1350 | blend.blendConst[3] = 1.0f; |
Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 1351 | |
Cody Northrop | e4bc694 | 2015-08-26 10:01:32 -0600 | [diff] [blame] | 1352 | memset(&depth_bounds, 0, sizeof(depth_bounds)); |
| 1353 | depth_bounds.sType = VK_STRUCTURE_TYPE_DYNAMIC_DEPTH_BOUNDS_STATE_CREATE_INFO; |
| 1354 | depth_bounds.minDepthBounds = 0.0f; |
| 1355 | depth_bounds.maxDepthBounds = 1.0f; |
Cody Northrop | 2605cb0 | 2015-08-18 15:21:16 -0600 | [diff] [blame] | 1356 | |
| 1357 | memset(&stencil, 0, sizeof(stencil)); |
| 1358 | stencil.sType = VK_STRUCTURE_TYPE_DYNAMIC_STENCIL_STATE_CREATE_INFO; |
| 1359 | stencil.stencilReference = 0; |
Cody Northrop | e4bc694 | 2015-08-26 10:01:32 -0600 | [diff] [blame] | 1360 | stencil.stencilCompareMask = 0xff; |
Cody Northrop | 2605cb0 | 2015-08-18 15:21:16 -0600 | [diff] [blame] | 1361 | stencil.stencilWriteMask = 0xff; |
Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 1362 | |
Cody Northrop | e4bc694 | 2015-08-26 10:01:32 -0600 | [diff] [blame] | 1363 | err = vkCreateDynamicViewportState(demo->device, &viewport_create, &demo->dynamic_viewport); |
Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 1364 | assert(!err); |
| 1365 | |
Cody Northrop | e4bc694 | 2015-08-26 10:01:32 -0600 | [diff] [blame] | 1366 | err = vkCreateDynamicLineWidthState(demo->device, &line_width, &demo->dynamic_line_width); |
Cody Northrop | f5bd225 | 2015-08-17 11:10:49 -0600 | [diff] [blame] | 1367 | assert(!err); |
| 1368 | |
Cody Northrop | e4bc694 | 2015-08-26 10:01:32 -0600 | [diff] [blame] | 1369 | err = vkCreateDynamicDepthBiasState(demo->device, &depth_bias, &demo->dynamic_depth_bias); |
Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 1370 | assert(!err); |
| 1371 | |
Cody Northrop | e4bc694 | 2015-08-26 10:01:32 -0600 | [diff] [blame] | 1372 | err = vkCreateDynamicBlendState(demo->device, |
| 1373 | &blend, &demo->dynamic_blend); |
Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 1374 | assert(!err); |
| 1375 | |
Cody Northrop | e4bc694 | 2015-08-26 10:01:32 -0600 | [diff] [blame] | 1376 | err = vkCreateDynamicDepthBoundsState(demo->device, |
| 1377 | &depth_bounds, &demo->dynamic_depth_bounds); |
Cody Northrop | 2605cb0 | 2015-08-18 15:21:16 -0600 | [diff] [blame] | 1378 | assert(!err); |
| 1379 | |
| 1380 | err = vkCreateDynamicStencilState(demo->device, |
| 1381 | &stencil, &stencil, &demo->dynamic_stencil); |
Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 1382 | assert(!err); |
| 1383 | } |
| 1384 | |
Chia-I Wu | 8d24b3b | 2015-03-26 13:14:16 +0800 | [diff] [blame] | 1385 | static void demo_prepare_descriptor_pool(struct demo *demo) |
Chia-I Wu | f838506 | 2015-01-04 16:27:24 +0800 | [diff] [blame] | 1386 | { |
Courtney Goeltzenleuchter | 382489d | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 1387 | const VkDescriptorTypeCount type_count = { |
Courtney Goeltzenleuchter | ad87081 | 2015-04-15 15:29:59 -0600 | [diff] [blame] | 1388 | .type = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, |
Chia-I Wu | f838506 | 2015-01-04 16:27:24 +0800 | [diff] [blame] | 1389 | .count = DEMO_TEXTURE_COUNT, |
| 1390 | }; |
Courtney Goeltzenleuchter | 382489d | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 1391 | const VkDescriptorPoolCreateInfo descriptor_pool = { |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1392 | .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO, |
Chia-I Wu | f838506 | 2015-01-04 16:27:24 +0800 | [diff] [blame] | 1393 | .pNext = NULL, |
| 1394 | .count = 1, |
| 1395 | .pTypeCount = &type_count, |
| 1396 | }; |
Tony Barbour | 22a3086 | 2015-04-22 09:02:32 -0600 | [diff] [blame] | 1397 | VkResult U_ASSERT_ONLY err; |
Chia-I Wu | f838506 | 2015-01-04 16:27:24 +0800 | [diff] [blame] | 1398 | |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1399 | err = vkCreateDescriptorPool(demo->device, |
| 1400 | VK_DESCRIPTOR_POOL_USAGE_ONE_SHOT, 1, |
Chia-I Wu | 8d24b3b | 2015-03-26 13:14:16 +0800 | [diff] [blame] | 1401 | &descriptor_pool, &demo->desc_pool); |
Chia-I Wu | f838506 | 2015-01-04 16:27:24 +0800 | [diff] [blame] | 1402 | assert(!err); |
| 1403 | } |
| 1404 | |
| 1405 | static void demo_prepare_descriptor_set(struct demo *demo) |
| 1406 | { |
Chia-I Wu | 8cd8ecd | 2015-05-25 16:27:55 +0800 | [diff] [blame] | 1407 | VkDescriptorInfo tex_descs[DEMO_TEXTURE_COUNT]; |
| 1408 | VkWriteDescriptorSet write; |
Tony Barbour | 22a3086 | 2015-04-22 09:02:32 -0600 | [diff] [blame] | 1409 | VkResult U_ASSERT_ONLY err; |
Mark Lobodzinski | e2d07a5 | 2015-01-29 08:55:56 -0600 | [diff] [blame] | 1410 | uint32_t i; |
Chia-I Wu | f838506 | 2015-01-04 16:27:24 +0800 | [diff] [blame] | 1411 | |
Mike Stroyan | 230e625 | 2015-04-17 12:36:38 -0600 | [diff] [blame] | 1412 | err = vkAllocDescriptorSets(demo->device, demo->desc_pool, |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1413 | VK_DESCRIPTOR_SET_USAGE_STATIC, |
Chia-I Wu | f838506 | 2015-01-04 16:27:24 +0800 | [diff] [blame] | 1414 | 1, &demo->desc_layout, |
Cody Northrop | c8aa4a5 | 2015-08-03 12:47:29 -0600 | [diff] [blame] | 1415 | &demo->desc_set); |
| 1416 | assert(!err); |
Chia-I Wu | f838506 | 2015-01-04 16:27:24 +0800 | [diff] [blame] | 1417 | |
Chia-I Wu | 8cd8ecd | 2015-05-25 16:27:55 +0800 | [diff] [blame] | 1418 | memset(&tex_descs, 0, sizeof(tex_descs)); |
| 1419 | for (i = 0; i < DEMO_TEXTURE_COUNT; i++) { |
| 1420 | tex_descs[i].sampler = demo->textures[i].sampler; |
| 1421 | tex_descs[i].imageView = demo->textures[i].view; |
| 1422 | tex_descs[i].imageLayout = VK_IMAGE_LAYOUT_GENERAL; |
| 1423 | } |
| 1424 | |
| 1425 | memset(&write, 0, sizeof(write)); |
| 1426 | write.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET; |
| 1427 | write.destSet = demo->desc_set; |
| 1428 | write.count = DEMO_TEXTURE_COUNT; |
| 1429 | write.descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER; |
| 1430 | write.pDescriptors = tex_descs; |
| 1431 | |
| 1432 | err = vkUpdateDescriptorSets(demo->device, 1, &write, 0, NULL); |
| 1433 | assert(!err); |
Chia-I Wu | f838506 | 2015-01-04 16:27:24 +0800 | [diff] [blame] | 1434 | } |
| 1435 | |
Chia-I Wu | 76cd422 | 2015-07-08 13:34:24 +0800 | [diff] [blame] | 1436 | static void demo_prepare_framebuffers(struct demo *demo) |
| 1437 | { |
Cody Northrop | f110c6e | 2015-08-04 10:47:08 -0600 | [diff] [blame] | 1438 | VkAttachmentView attachments[2]; |
| 1439 | attachments[1] = demo->depth.view; |
| 1440 | |
Chia-I Wu | 76cd422 | 2015-07-08 13:34:24 +0800 | [diff] [blame] | 1441 | const VkFramebufferCreateInfo fb_info = { |
| 1442 | .sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO, |
| 1443 | .pNext = NULL, |
Chia-I Wu | c278df8 | 2015-07-07 11:50:03 +0800 | [diff] [blame] | 1444 | .renderPass = demo->render_pass, |
| 1445 | .attachmentCount = 2, |
| 1446 | .pAttachments = attachments, |
Chia-I Wu | 76cd422 | 2015-07-08 13:34:24 +0800 | [diff] [blame] | 1447 | .width = demo->width, |
| 1448 | .height = demo->height, |
| 1449 | .layers = 1, |
| 1450 | }; |
| 1451 | VkResult U_ASSERT_ONLY err; |
| 1452 | uint32_t i; |
| 1453 | |
| 1454 | for (i = 0; i < DEMO_BUFFER_COUNT; i++) { |
Cody Northrop | f110c6e | 2015-08-04 10:47:08 -0600 | [diff] [blame] | 1455 | attachments[0]= demo->buffers[i].view; |
Chia-I Wu | 76cd422 | 2015-07-08 13:34:24 +0800 | [diff] [blame] | 1456 | err = vkCreateFramebuffer(demo->device, &fb_info, &demo->framebuffers[i]); |
| 1457 | assert(!err); |
| 1458 | } |
| 1459 | } |
| 1460 | |
Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 1461 | static void demo_prepare(struct demo *demo) |
| 1462 | { |
Cody Northrop | 18ea11b | 2015-07-09 18:08:32 -0600 | [diff] [blame] | 1463 | VkResult U_ASSERT_ONLY err; |
| 1464 | |
| 1465 | const VkCmdPoolCreateInfo cmd_pool_info = { |
| 1466 | .sType = VK_STRUCTURE_TYPE_CMD_POOL_CREATE_INFO, |
| 1467 | .pNext = NULL, |
| 1468 | .queueFamilyIndex = demo->graphics_queue_node_index, |
| 1469 | .flags = 0, |
| 1470 | }; |
| 1471 | err = vkCreateCommandPool(demo->device, &cmd_pool_info, &demo->cmd_pool); |
| 1472 | assert(!err); |
| 1473 | |
Courtney Goeltzenleuchter | 382489d | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 1474 | const VkCmdBufferCreateInfo cmd = { |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1475 | .sType = VK_STRUCTURE_TYPE_CMD_BUFFER_CREATE_INFO, |
Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 1476 | .pNext = NULL, |
Cody Northrop | 18ea11b | 2015-07-09 18:08:32 -0600 | [diff] [blame] | 1477 | .cmdPool = demo->cmd_pool, |
Chia-I Wu | 88eaa3b | 2015-06-26 15:34:39 +0800 | [diff] [blame] | 1478 | .level = VK_CMD_BUFFER_LEVEL_PRIMARY, |
Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 1479 | .flags = 0, |
| 1480 | }; |
Courtney Goeltzenleuchter | 633f9c5 | 2015-04-30 10:58:33 -0600 | [diff] [blame] | 1481 | err = vkCreateCommandBuffer(demo->device, &cmd, &demo->draw_cmd); |
| 1482 | assert(!err); |
| 1483 | |
Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 1484 | demo_prepare_buffers(demo); |
Chia-I Wu | 9ae87c9 | 2014-10-07 14:15:01 +0800 | [diff] [blame] | 1485 | demo_prepare_depth(demo); |
Chia-I Wu | b043fe3 | 2014-10-06 15:30:33 +0800 | [diff] [blame] | 1486 | demo_prepare_textures(demo); |
Chia-I Wu | 99621bc | 2014-10-08 11:52:22 +0800 | [diff] [blame] | 1487 | demo_prepare_vertices(demo); |
Chia-I Wu | f838506 | 2015-01-04 16:27:24 +0800 | [diff] [blame] | 1488 | demo_prepare_descriptor_layout(demo); |
Chia-I Wu | 76cd422 | 2015-07-08 13:34:24 +0800 | [diff] [blame] | 1489 | demo_prepare_render_pass(demo); |
Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 1490 | demo_prepare_pipeline(demo); |
| 1491 | demo_prepare_dynamic_states(demo); |
| 1492 | |
Chia-I Wu | 8d24b3b | 2015-03-26 13:14:16 +0800 | [diff] [blame] | 1493 | demo_prepare_descriptor_pool(demo); |
Chia-I Wu | f838506 | 2015-01-04 16:27:24 +0800 | [diff] [blame] | 1494 | demo_prepare_descriptor_set(demo); |
Chia-I Wu | 76cd422 | 2015-07-08 13:34:24 +0800 | [diff] [blame] | 1495 | |
| 1496 | demo_prepare_framebuffers(demo); |
| 1497 | |
Courtney Goeltzenleuchter | 633f9c5 | 2015-04-30 10:58:33 -0600 | [diff] [blame] | 1498 | demo->prepared = true; |
Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 1499 | } |
| 1500 | |
Ian Elliott | e14e9f9 | 2015-04-16 15:23:05 -0600 | [diff] [blame] | 1501 | #ifdef _WIN32 |
| 1502 | static void demo_run(struct demo *demo) |
| 1503 | { |
Courtney Goeltzenleuchter | 633f9c5 | 2015-04-30 10:58:33 -0600 | [diff] [blame] | 1504 | if (!demo->prepared) |
| 1505 | return; |
Ian Elliott | e14e9f9 | 2015-04-16 15:23:05 -0600 | [diff] [blame] | 1506 | demo_draw(demo); |
| 1507 | } |
| 1508 | |
| 1509 | // On MS-Windows, make this a global, so it's available to WndProc() |
| 1510 | struct demo demo; |
| 1511 | |
| 1512 | // MS-Windows event handling function: |
| 1513 | LRESULT CALLBACK WndProc(HWND hWnd, |
| 1514 | UINT uMsg, |
| 1515 | WPARAM wParam, |
| 1516 | LPARAM lParam) |
| 1517 | { |
Ian Elliott | 4e19ed0 | 2015-04-28 10:52:52 -0600 | [diff] [blame] | 1518 | char tmp_str[] = APP_LONG_NAME; |
Ian Elliott | e14e9f9 | 2015-04-16 15:23:05 -0600 | [diff] [blame] | 1519 | |
| 1520 | switch(uMsg) |
| 1521 | { |
Ian Elliott | e36b208 | 2015-07-06 14:27:58 -0600 | [diff] [blame] | 1522 | case WM_CREATE: |
Ian Elliott | e14e9f9 | 2015-04-16 15:23:05 -0600 | [diff] [blame] | 1523 | return 0; |
Ian Elliott | e36b208 | 2015-07-06 14:27:58 -0600 | [diff] [blame] | 1524 | case WM_CLOSE: |
Ian Elliott | e14e9f9 | 2015-04-16 15:23:05 -0600 | [diff] [blame] | 1525 | PostQuitMessage(0); |
| 1526 | return 0; |
Ian Elliott | e36b208 | 2015-07-06 14:27:58 -0600 | [diff] [blame] | 1527 | case WM_PAINT: |
Ian Elliott | e14e9f9 | 2015-04-16 15:23:05 -0600 | [diff] [blame] | 1528 | demo_run(&demo); |
| 1529 | return 0; |
| 1530 | default: |
| 1531 | break; |
| 1532 | } |
| 1533 | return (DefWindowProc(hWnd, uMsg, wParam, lParam)); |
| 1534 | } |
| 1535 | |
| 1536 | static void demo_create_window(struct demo *demo) |
| 1537 | { |
| 1538 | WNDCLASSEX win_class; |
| 1539 | |
| 1540 | // Initialize the window class structure: |
| 1541 | win_class.cbSize = sizeof(WNDCLASSEX); |
| 1542 | win_class.style = CS_HREDRAW | CS_VREDRAW; |
| 1543 | win_class.lpfnWndProc = WndProc; |
| 1544 | win_class.cbClsExtra = 0; |
| 1545 | win_class.cbWndExtra = 0; |
| 1546 | win_class.hInstance = demo->connection; // hInstance |
| 1547 | win_class.hIcon = LoadIcon(NULL, IDI_APPLICATION); |
| 1548 | win_class.hCursor = LoadCursor(NULL, IDC_ARROW); |
| 1549 | win_class.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH); |
| 1550 | win_class.lpszMenuName = NULL; |
| 1551 | win_class.lpszClassName = demo->name; |
| 1552 | win_class.hIconSm = LoadIcon(NULL, IDI_WINLOGO); |
| 1553 | // Register window class: |
| 1554 | if (!RegisterClassEx(&win_class)) { |
| 1555 | // It didn't work, so try to give a useful error: |
| 1556 | printf("Unexpected error trying to start the application!\n"); |
| 1557 | fflush(stdout); |
| 1558 | exit(1); |
| 1559 | } |
| 1560 | // Create window with the registered class: |
Mike Stroyan | 7eef574 | 2015-06-15 14:19:19 -0600 | [diff] [blame] | 1561 | RECT wr = { 0, 0, demo->width, demo->height }; |
| 1562 | AdjustWindowRect(&wr, WS_OVERLAPPEDWINDOW, FALSE); |
Ian Elliott | e14e9f9 | 2015-04-16 15:23:05 -0600 | [diff] [blame] | 1563 | demo->window = CreateWindowEx(0, |
| 1564 | demo->name, // class name |
| 1565 | demo->name, // app name |
| 1566 | WS_OVERLAPPEDWINDOW | // window style |
| 1567 | WS_VISIBLE | |
| 1568 | WS_SYSMENU, |
| 1569 | 100,100, // x/y coords |
Mike Stroyan | 7eef574 | 2015-06-15 14:19:19 -0600 | [diff] [blame] | 1570 | wr.right-wr.left, // width |
| 1571 | wr.bottom-wr.top, // height |
Ian Elliott | e14e9f9 | 2015-04-16 15:23:05 -0600 | [diff] [blame] | 1572 | NULL, // handle to parent |
| 1573 | NULL, // handle to menu |
| 1574 | demo->connection, // hInstance |
| 1575 | NULL); // no extra parameters |
| 1576 | if (!demo->window) { |
| 1577 | // It didn't work, so try to give a useful error: |
| 1578 | printf("Cannot create a window in which to draw!\n"); |
| 1579 | fflush(stdout); |
| 1580 | exit(1); |
| 1581 | } |
| 1582 | } |
| 1583 | #else // _WIN32 |
| 1584 | |
Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 1585 | static void demo_handle_event(struct demo *demo, |
| 1586 | const xcb_generic_event_t *event) |
| 1587 | { |
| 1588 | switch (event->response_type & 0x7f) { |
| 1589 | case XCB_EXPOSE: |
| 1590 | demo_draw(demo); |
| 1591 | break; |
Courtney Goeltzenleuchter | ca69805 | 2014-11-07 15:17:03 -0700 | [diff] [blame] | 1592 | case XCB_CLIENT_MESSAGE: |
| 1593 | if((*(xcb_client_message_event_t*)event).data.data32[0] == |
| 1594 | (*demo->atom_wm_delete_window).atom) { |
| 1595 | demo->quit = true; |
| 1596 | } |
| 1597 | break; |
Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 1598 | case XCB_KEY_RELEASE: |
| 1599 | { |
| 1600 | const xcb_key_release_event_t *key = |
| 1601 | (const xcb_key_release_event_t *) event; |
| 1602 | |
| 1603 | if (key->detail == 0x9) |
| 1604 | demo->quit = true; |
| 1605 | } |
| 1606 | break; |
Courtney Goeltzenleuchter | ca69805 | 2014-11-07 15:17:03 -0700 | [diff] [blame] | 1607 | case XCB_DESTROY_NOTIFY: |
| 1608 | demo->quit = true; |
| 1609 | break; |
Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 1610 | default: |
| 1611 | break; |
| 1612 | } |
| 1613 | } |
| 1614 | |
| 1615 | static void demo_run(struct demo *demo) |
| 1616 | { |
| 1617 | xcb_flush(demo->connection); |
| 1618 | |
| 1619 | while (!demo->quit) { |
| 1620 | xcb_generic_event_t *event; |
| 1621 | |
| 1622 | event = xcb_wait_for_event(demo->connection); |
| 1623 | if (event) { |
| 1624 | demo_handle_event(demo, event); |
| 1625 | free(event); |
| 1626 | } |
| 1627 | } |
| 1628 | } |
| 1629 | |
| 1630 | static void demo_create_window(struct demo *demo) |
| 1631 | { |
| 1632 | uint32_t value_mask, value_list[32]; |
| 1633 | |
| 1634 | demo->window = xcb_generate_id(demo->connection); |
| 1635 | |
| 1636 | value_mask = XCB_CW_BACK_PIXEL | XCB_CW_EVENT_MASK; |
| 1637 | value_list[0] = demo->screen->black_pixel; |
| 1638 | value_list[1] = XCB_EVENT_MASK_KEY_RELEASE | |
Courtney Goeltzenleuchter | ca69805 | 2014-11-07 15:17:03 -0700 | [diff] [blame] | 1639 | XCB_EVENT_MASK_EXPOSURE | |
| 1640 | XCB_EVENT_MASK_STRUCTURE_NOTIFY; |
Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 1641 | |
| 1642 | xcb_create_window(demo->connection, |
| 1643 | XCB_COPY_FROM_PARENT, |
| 1644 | demo->window, demo->screen->root, |
| 1645 | 0, 0, demo->width, demo->height, 0, |
| 1646 | XCB_WINDOW_CLASS_INPUT_OUTPUT, |
| 1647 | demo->screen->root_visual, |
| 1648 | value_mask, value_list); |
| 1649 | |
Courtney Goeltzenleuchter | ca69805 | 2014-11-07 15:17:03 -0700 | [diff] [blame] | 1650 | /* Magic code that will send notification when window is destroyed */ |
| 1651 | xcb_intern_atom_cookie_t cookie = xcb_intern_atom(demo->connection, 1, 12, |
| 1652 | "WM_PROTOCOLS"); |
| 1653 | xcb_intern_atom_reply_t* reply = xcb_intern_atom_reply(demo->connection, cookie, 0); |
| 1654 | |
| 1655 | xcb_intern_atom_cookie_t cookie2 = xcb_intern_atom(demo->connection, 0, 16, "WM_DELETE_WINDOW"); |
| 1656 | demo->atom_wm_delete_window = xcb_intern_atom_reply(demo->connection, cookie2, 0); |
| 1657 | |
| 1658 | xcb_change_property(demo->connection, XCB_PROP_MODE_REPLACE, |
| 1659 | demo->window, (*reply).atom, 4, 32, 1, |
| 1660 | &(*demo->atom_wm_delete_window).atom); |
| 1661 | free(reply); |
| 1662 | |
Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 1663 | xcb_map_window(demo->connection, demo->window); |
| 1664 | } |
Ian Elliott | e14e9f9 | 2015-04-16 15:23:05 -0600 | [diff] [blame] | 1665 | #endif // _WIN32 |
Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 1666 | |
Ian Elliott | e36b208 | 2015-07-06 14:27:58 -0600 | [diff] [blame] | 1667 | /* |
| 1668 | * Return 1 (true) if all layer names specified in check_names |
| 1669 | * can be found in given layer properties. |
| 1670 | */ |
| 1671 | static VkBool32 demo_check_layers(uint32_t check_count, char **check_names, |
| 1672 | uint32_t layer_count, VkLayerProperties *layers) |
| 1673 | { |
| 1674 | for (uint32_t i = 0; i < check_count; i++) { |
| 1675 | VkBool32 found = 0; |
| 1676 | for (uint32_t j = 0; j < layer_count; j++) { |
| 1677 | if (!strcmp(check_names[i], layers[j].layerName)) { |
| 1678 | found = 1; |
| 1679 | } |
| 1680 | } |
| 1681 | if (!found) { |
| 1682 | fprintf(stderr, "Cannot find layer: %s\n", check_names[i]); |
| 1683 | return 0; |
| 1684 | } |
| 1685 | } |
| 1686 | return 1; |
| 1687 | } |
| 1688 | |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1689 | static void demo_init_vk(struct demo *demo) |
Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 1690 | { |
Tobin Ehlis | 3536b44 | 2015-04-16 18:04:57 -0600 | [diff] [blame] | 1691 | VkResult err; |
Courtney Goeltzenleuchter | 18061cd | 2015-06-29 15:39:26 -0600 | [diff] [blame] | 1692 | char *extension_names[64]; |
| 1693 | char *layer_names[64]; |
Courtney Goeltzenleuchter | 1c7c65d | 2015-06-10 17:39:03 -0600 | [diff] [blame] | 1694 | VkExtensionProperties *instance_extensions; |
Courtney Goeltzenleuchter | 18061cd | 2015-06-29 15:39:26 -0600 | [diff] [blame] | 1695 | VkLayerProperties *instance_layers; |
| 1696 | VkLayerProperties *device_layers; |
Courtney Goeltzenleuchter | 1c7c65d | 2015-06-10 17:39:03 -0600 | [diff] [blame] | 1697 | uint32_t instance_extension_count = 0; |
Courtney Goeltzenleuchter | 18061cd | 2015-06-29 15:39:26 -0600 | [diff] [blame] | 1698 | uint32_t instance_layer_count = 0; |
| 1699 | uint32_t enabled_extension_count = 0; |
| 1700 | uint32_t enabled_layer_count = 0; |
| 1701 | |
Ian Elliott | e36b208 | 2015-07-06 14:27:58 -0600 | [diff] [blame] | 1702 | char *instance_validation_layers[] = { |
| 1703 | "MemTracker", |
| 1704 | }; |
| 1705 | |
| 1706 | char *device_validation_layers[] = { |
| 1707 | "MemTracker", |
| 1708 | }; |
| 1709 | |
Courtney Goeltzenleuchter | 18061cd | 2015-06-29 15:39:26 -0600 | [diff] [blame] | 1710 | /* Look for validation layers */ |
Courtney Goeltzenleuchter | 1f41f54 | 2015-07-09 11:44:38 -0600 | [diff] [blame] | 1711 | VkBool32 validation_found = 0; |
Courtney Goeltzenleuchter | 18061cd | 2015-06-29 15:39:26 -0600 | [diff] [blame] | 1712 | err = vkGetGlobalLayerProperties(&instance_layer_count, NULL); |
Tobin Ehlis | 3536b44 | 2015-04-16 18:04:57 -0600 | [diff] [blame] | 1713 | assert(!err); |
| 1714 | |
Courtney Goeltzenleuchter | 18061cd | 2015-06-29 15:39:26 -0600 | [diff] [blame] | 1715 | instance_layers = malloc(sizeof(VkLayerProperties) * instance_layer_count); |
| 1716 | err = vkGetGlobalLayerProperties(&instance_layer_count, instance_layers); |
| 1717 | assert(!err); |
Ian Elliott | e36b208 | 2015-07-06 14:27:58 -0600 | [diff] [blame] | 1718 | |
| 1719 | if (demo->validate) { |
| 1720 | validation_found = demo_check_layers(ARRAY_SIZE(instance_validation_layers), instance_validation_layers, |
| 1721 | instance_layer_count, instance_layers); |
| 1722 | if (!validation_found) { |
| 1723 | ERR_EXIT("vkGetGlobalLayerProperties failed to find" |
| 1724 | "required validation layer.\n\n" |
| 1725 | "Please look at the Getting Started guide for additional " |
| 1726 | "information.\n", |
| 1727 | "vkCreateInstance Failure"); |
Courtney Goeltzenleuchter | 1c7c65d | 2015-06-10 17:39:03 -0600 | [diff] [blame] | 1728 | } |
Ian Elliott | e36b208 | 2015-07-06 14:27:58 -0600 | [diff] [blame] | 1729 | enabled_layer_count = ARRAY_SIZE(instance_validation_layers); |
Courtney Goeltzenleuchter | 18061cd | 2015-06-29 15:39:26 -0600 | [diff] [blame] | 1730 | } |
| 1731 | |
| 1732 | err = vkGetGlobalExtensionProperties(NULL, &instance_extension_count, NULL); |
| 1733 | assert(!err); |
| 1734 | |
Courtney Goeltzenleuchter | 1f41f54 | 2015-07-09 11:44:38 -0600 | [diff] [blame] | 1735 | VkBool32 WSIextFound = 0; |
Courtney Goeltzenleuchter | 18061cd | 2015-06-29 15:39:26 -0600 | [diff] [blame] | 1736 | memset(extension_names, 0, sizeof(extension_names)); |
| 1737 | instance_extensions = malloc(sizeof(VkExtensionProperties) * instance_extension_count); |
| 1738 | err = vkGetGlobalExtensionProperties(NULL, &instance_extension_count, instance_extensions); |
| 1739 | assert(!err); |
| 1740 | for (uint32_t i = 0; i < instance_extension_count; i++) { |
Ian Elliott | e36b208 | 2015-07-06 14:27:58 -0600 | [diff] [blame] | 1741 | if (!strcmp("VK_WSI_swapchain", instance_extensions[i].extName)) { |
Courtney Goeltzenleuchter | 18061cd | 2015-06-29 15:39:26 -0600 | [diff] [blame] | 1742 | WSIextFound = 1; |
Ian Elliott | e36b208 | 2015-07-06 14:27:58 -0600 | [diff] [blame] | 1743 | extension_names[enabled_extension_count++] = "VK_WSI_swapchain"; |
Courtney Goeltzenleuchter | 18061cd | 2015-06-29 15:39:26 -0600 | [diff] [blame] | 1744 | } |
Courtney Goeltzenleuchter | 846298c | 2015-07-30 11:32:46 -0600 | [diff] [blame] | 1745 | if (!strcmp(VK_DEBUG_REPORT_EXTENSION_NAME, instance_extensions[i].extName)) { |
Courtney Goeltzenleuchter | 18061cd | 2015-06-29 15:39:26 -0600 | [diff] [blame] | 1746 | if (demo->validate) { |
Courtney Goeltzenleuchter | 846298c | 2015-07-30 11:32:46 -0600 | [diff] [blame] | 1747 | extension_names[enabled_extension_count++] = VK_DEBUG_REPORT_EXTENSION_NAME; |
Courtney Goeltzenleuchter | 18061cd | 2015-06-29 15:39:26 -0600 | [diff] [blame] | 1748 | } |
| 1749 | } |
| 1750 | assert(enabled_extension_count < 64); |
Tobin Ehlis | 3536b44 | 2015-04-16 18:04:57 -0600 | [diff] [blame] | 1751 | } |
Courtney Goeltzenleuchter | 1c7c65d | 2015-06-10 17:39:03 -0600 | [diff] [blame] | 1752 | if (!WSIextFound) { |
Tony Barbour | 426b905 | 2015-06-24 16:06:58 -0600 | [diff] [blame] | 1753 | ERR_EXIT("vkGetGlobalExtensionProperties failed to find the " |
Ian Elliott | e36b208 | 2015-07-06 14:27:58 -0600 | [diff] [blame] | 1754 | "\"VK_WSI_swapchain\" extension.\n\nDo you have a compatible " |
Ian Elliott | 3b375cf | 2015-04-28 13:22:33 -0600 | [diff] [blame] | 1755 | "Vulkan installable client driver (ICD) installed?\nPlease " |
| 1756 | "look at the Getting Started guide for additional " |
| 1757 | "information.\n", |
| 1758 | "vkCreateInstance Failure"); |
| 1759 | } |
Courtney Goeltzenleuchter | 382489d | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 1760 | const VkApplicationInfo app = { |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1761 | .sType = VK_STRUCTURE_TYPE_APPLICATION_INFO, |
Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 1762 | .pNext = NULL, |
Ian Elliott | 4e19ed0 | 2015-04-28 10:52:52 -0600 | [diff] [blame] | 1763 | .pAppName = APP_SHORT_NAME, |
Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 1764 | .appVersion = 0, |
Ian Elliott | 4e19ed0 | 2015-04-28 10:52:52 -0600 | [diff] [blame] | 1765 | .pEngineName = APP_SHORT_NAME, |
Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 1766 | .engineVersion = 0, |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1767 | .apiVersion = VK_API_VERSION, |
Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 1768 | }; |
Courtney Goeltzenleuchter | 18061cd | 2015-06-29 15:39:26 -0600 | [diff] [blame] | 1769 | VkInstanceCreateInfo inst_info = { |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1770 | .sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO, |
Jon Ashburn | 29669a4 | 2015-04-04 14:52:07 -0600 | [diff] [blame] | 1771 | .pNext = NULL, |
| 1772 | .pAppInfo = &app, |
| 1773 | .pAllocCb = NULL, |
Courtney Goeltzenleuchter | 18061cd | 2015-06-29 15:39:26 -0600 | [diff] [blame] | 1774 | .layerCount = enabled_layer_count, |
| 1775 | .ppEnabledLayerNames = (const char *const*) layer_names, |
| 1776 | .extensionCount = enabled_extension_count, |
| 1777 | .ppEnabledExtensionNames = (const char *const*) extension_names, |
Jon Ashburn | 29669a4 | 2015-04-04 14:52:07 -0600 | [diff] [blame] | 1778 | }; |
Courtney Goeltzenleuchter | ddcb619 | 2015-04-14 18:48:46 -0600 | [diff] [blame] | 1779 | const VkDeviceQueueCreateInfo queue = { |
Chris Forbes | fa6d36e | 2015-07-11 19:11:39 +1200 | [diff] [blame] | 1780 | .queueFamilyIndex = 0, |
Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 1781 | .queueCount = 1, |
| 1782 | }; |
Mark Lobodzinski | e2d07a5 | 2015-01-29 08:55:56 -0600 | [diff] [blame] | 1783 | uint32_t gpu_count; |
Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 1784 | |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1785 | err = vkCreateInstance(&inst_info, &demo->inst); |
Ian Elliott | caa9f27 | 2015-04-28 11:35:02 -0600 | [diff] [blame] | 1786 | if (err == VK_ERROR_INCOMPATIBLE_DRIVER) { |
| 1787 | ERR_EXIT("Cannot find a compatible Vulkan installable client driver " |
Ian Elliott | 3b375cf | 2015-04-28 13:22:33 -0600 | [diff] [blame] | 1788 | "(ICD).\n\nPlease look at the Getting Started guide for " |
Ian Elliott | caa9f27 | 2015-04-28 11:35:02 -0600 | [diff] [blame] | 1789 | "additional information.\n", |
| 1790 | "vkCreateInstance Failure"); |
Courtney Goeltzenleuchter | 18061cd | 2015-06-29 15:39:26 -0600 | [diff] [blame] | 1791 | } else if (err == VK_ERROR_INVALID_EXTENSION) { |
| 1792 | ERR_EXIT("Cannot find a specified extension library" |
| 1793 | ".\nMake sure your layers path is set appropriately\n", |
| 1794 | "vkCreateInstance Failure"); |
Ian Elliott | caa9f27 | 2015-04-28 11:35:02 -0600 | [diff] [blame] | 1795 | } else if (err) { |
Ian Elliott | 3b375cf | 2015-04-28 13:22:33 -0600 | [diff] [blame] | 1796 | ERR_EXIT("vkCreateInstance failed.\n\nDo you have a compatible Vulkan " |
| 1797 | "installable client driver (ICD) installed?\nPlease look at " |
Ian Elliott | caa9f27 | 2015-04-28 11:35:02 -0600 | [diff] [blame] | 1798 | "the Getting Started guide for additional information.\n", |
| 1799 | "vkCreateInstance Failure"); |
Ian Elliott | dfe55f7 | 2015-04-03 15:24:55 -0600 | [diff] [blame] | 1800 | } |
Jon Ashburn | 29669a4 | 2015-04-04 14:52:07 -0600 | [diff] [blame] | 1801 | |
Courtney Goeltzenleuchter | 18061cd | 2015-06-29 15:39:26 -0600 | [diff] [blame] | 1802 | free(instance_layers); |
| 1803 | free(instance_extensions); |
| 1804 | |
Jon Ashburn | 07b309a | 2015-04-15 11:31:12 -0600 | [diff] [blame] | 1805 | gpu_count = 1; |
| 1806 | err = vkEnumeratePhysicalDevices(demo->inst, &gpu_count, &demo->gpu); |
Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 1807 | assert(!err && gpu_count == 1); |
| 1808 | |
Courtney Goeltzenleuchter | 18061cd | 2015-06-29 15:39:26 -0600 | [diff] [blame] | 1809 | /* Look for validation layers */ |
| 1810 | validation_found = 0; |
| 1811 | enabled_layer_count = 0; |
| 1812 | uint32_t device_layer_count = 0; |
| 1813 | err = vkGetPhysicalDeviceLayerProperties(demo->gpu, &device_layer_count, NULL); |
| 1814 | assert(!err); |
| 1815 | |
Courtney Goeltzenleuchter | 18061cd | 2015-06-29 15:39:26 -0600 | [diff] [blame] | 1816 | device_layers = malloc(sizeof(VkLayerProperties) * device_layer_count); |
| 1817 | err = vkGetPhysicalDeviceLayerProperties(demo->gpu, &device_layer_count, device_layers); |
| 1818 | assert(!err); |
Ian Elliott | e36b208 | 2015-07-06 14:27:58 -0600 | [diff] [blame] | 1819 | |
| 1820 | if (demo->validate) { |
| 1821 | validation_found = demo_check_layers(ARRAY_SIZE(device_validation_layers), device_validation_layers, |
| 1822 | device_layer_count, device_layers); |
| 1823 | if (!validation_found) { |
| 1824 | ERR_EXIT("vkGetPhysicalDeviceLayerProperties failed to find" |
| 1825 | "a required validation layer.\n\n" |
| 1826 | "Please look at the Getting Started guide for additional " |
| 1827 | "information.\n", |
| 1828 | "vkCreateDevice Failure"); |
Courtney Goeltzenleuchter | 18061cd | 2015-06-29 15:39:26 -0600 | [diff] [blame] | 1829 | } |
Ian Elliott | e36b208 | 2015-07-06 14:27:58 -0600 | [diff] [blame] | 1830 | enabled_layer_count = ARRAY_SIZE(device_validation_layers); |
Courtney Goeltzenleuchter | 18061cd | 2015-06-29 15:39:26 -0600 | [diff] [blame] | 1831 | } |
Ian Elliott | e36b208 | 2015-07-06 14:27:58 -0600 | [diff] [blame] | 1832 | |
| 1833 | uint32_t device_extension_count = 0; |
| 1834 | VkExtensionProperties *device_extensions = NULL; |
| 1835 | err = vkGetPhysicalDeviceExtensionProperties( |
| 1836 | demo->gpu, NULL, &device_extension_count, NULL); |
| 1837 | assert(!err); |
| 1838 | |
| 1839 | WSIextFound = 0; |
| 1840 | enabled_extension_count = 0; |
| 1841 | memset(extension_names, 0, sizeof(extension_names)); |
| 1842 | device_extensions = malloc(sizeof(VkExtensionProperties) * device_extension_count); |
| 1843 | err = vkGetPhysicalDeviceExtensionProperties( |
| 1844 | demo->gpu, NULL, &device_extension_count, device_extensions); |
| 1845 | assert(!err); |
| 1846 | |
| 1847 | for (uint32_t i = 0; i < device_extension_count; i++) { |
| 1848 | if (!strcmp("VK_WSI_device_swapchain", device_extensions[i].extName)) { |
| 1849 | WSIextFound = 1; |
| 1850 | extension_names[enabled_extension_count++] = "VK_WSI_device_swapchain"; |
| 1851 | } |
| 1852 | assert(enabled_extension_count < 64); |
| 1853 | } |
| 1854 | if (!WSIextFound) { |
| 1855 | ERR_EXIT("vkGetPhysicalDeviceExtensionProperties failed to find the " |
| 1856 | "\"VK_WSI_device_swapchain\" extension.\n\nDo you have a compatible " |
| 1857 | "Vulkan installable client driver (ICD) installed?\nPlease " |
| 1858 | "look at the Getting Started guide for additional " |
Courtney Goeltzenleuchter | 18061cd | 2015-06-29 15:39:26 -0600 | [diff] [blame] | 1859 | "information.\n", |
| 1860 | "vkCreateInstance Failure"); |
| 1861 | } |
| 1862 | |
Courtney Goeltzenleuchter | 18061cd | 2015-06-29 15:39:26 -0600 | [diff] [blame] | 1863 | VkDeviceCreateInfo device = { |
| 1864 | .sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO, |
| 1865 | .pNext = NULL, |
| 1866 | .queueRecordCount = 1, |
| 1867 | .pRequestedQueues = &queue, |
| 1868 | .layerCount = enabled_layer_count, |
Ian Elliott | e36b208 | 2015-07-06 14:27:58 -0600 | [diff] [blame] | 1869 | .ppEnabledLayerNames = (const char *const*) ((demo->validate) ? device_validation_layers : NULL), |
| 1870 | .extensionCount = enabled_extension_count, |
| 1871 | .ppEnabledExtensionNames = (const char *const*) extension_names, |
Courtney Goeltzenleuchter | 18061cd | 2015-06-29 15:39:26 -0600 | [diff] [blame] | 1872 | }; |
| 1873 | |
| 1874 | if (demo->validate) { |
Courtney Goeltzenleuchter | a4c8c71 | 2015-07-12 14:35:22 -0600 | [diff] [blame] | 1875 | demo->dbgCreateMsgCallback = (PFN_vkDbgCreateMsgCallback) vkGetInstanceProcAddr(demo->inst, "vkDbgCreateMsgCallback"); |
Courtney Goeltzenleuchter | 18061cd | 2015-06-29 15:39:26 -0600 | [diff] [blame] | 1876 | if (!demo->dbgCreateMsgCallback) { |
| 1877 | ERR_EXIT("GetProcAddr: Unable to find vkDbgCreateMsgCallback\n", |
| 1878 | "vkGetProcAddr Failure"); |
| 1879 | } |
| 1880 | err = demo->dbgCreateMsgCallback( |
| 1881 | demo->inst, |
| 1882 | VK_DBG_REPORT_ERROR_BIT | VK_DBG_REPORT_WARN_BIT, |
| 1883 | dbgFunc, NULL, |
| 1884 | &demo->msg_callback); |
| 1885 | switch (err) { |
| 1886 | case VK_SUCCESS: |
| 1887 | break; |
| 1888 | case VK_ERROR_INVALID_POINTER: |
| 1889 | ERR_EXIT("dbgCreateMsgCallback: Invalid pointer\n", |
| 1890 | "dbgCreateMsgCallback Failure"); |
| 1891 | break; |
| 1892 | case VK_ERROR_OUT_OF_HOST_MEMORY: |
| 1893 | ERR_EXIT("dbgCreateMsgCallback: out of host memory\n", |
| 1894 | "dbgCreateMsgCallback Failure"); |
| 1895 | break; |
| 1896 | default: |
| 1897 | ERR_EXIT("dbgCreateMsgCallback: unknown failure\n", |
| 1898 | "dbgCreateMsgCallback Failure"); |
| 1899 | break; |
| 1900 | } |
| 1901 | } |
| 1902 | |
Ian Elliott | e36b208 | 2015-07-06 14:27:58 -0600 | [diff] [blame] | 1903 | |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1904 | err = vkCreateDevice(demo->gpu, &device, &demo->device); |
Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 1905 | assert(!err); |
| 1906 | |
Ian Elliott | e36b208 | 2015-07-06 14:27:58 -0600 | [diff] [blame] | 1907 | GET_INSTANCE_PROC_ADDR(demo->inst, GetPhysicalDeviceSurfaceSupportWSI); |
Ian Elliott | 7fe115d | 2015-08-07 15:56:59 -0600 | [diff] [blame] | 1908 | GET_DEVICE_PROC_ADDR(demo->device, GetSurfacePropertiesWSI); |
| 1909 | GET_DEVICE_PROC_ADDR(demo->device, GetSurfaceFormatsWSI); |
| 1910 | GET_DEVICE_PROC_ADDR(demo->device, GetSurfacePresentModesWSI); |
Ian Elliott | 1b6de09 | 2015-06-22 15:07:49 -0600 | [diff] [blame] | 1911 | GET_DEVICE_PROC_ADDR(demo->device, CreateSwapChainWSI); |
| 1912 | GET_DEVICE_PROC_ADDR(demo->device, CreateSwapChainWSI); |
| 1913 | GET_DEVICE_PROC_ADDR(demo->device, DestroySwapChainWSI); |
Ian Elliott | 7fe115d | 2015-08-07 15:56:59 -0600 | [diff] [blame] | 1914 | GET_DEVICE_PROC_ADDR(demo->device, GetSwapChainImagesWSI); |
Ian Elliott | e36b208 | 2015-07-06 14:27:58 -0600 | [diff] [blame] | 1915 | GET_DEVICE_PROC_ADDR(demo->device, AcquireNextImageWSI); |
Ian Elliott | 1b6de09 | 2015-06-22 15:07:49 -0600 | [diff] [blame] | 1916 | GET_DEVICE_PROC_ADDR(demo->device, QueuePresentWSI); |
| 1917 | |
Tony Barbour | 426b905 | 2015-06-24 16:06:58 -0600 | [diff] [blame] | 1918 | err = vkGetPhysicalDeviceProperties(demo->gpu, &demo->gpu_props); |
Courtney Goeltzenleuchter | bfccf41 | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 1919 | |
Cody Northrop | ef72e2a | 2015-08-03 17:04:53 -0600 | [diff] [blame] | 1920 | // Query with NULL data to get count |
| 1921 | err = vkGetPhysicalDeviceQueueFamilyProperties(demo->gpu, &demo->queue_count, NULL); |
Courtney Goeltzenleuchter | bfccf41 | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 1922 | assert(!err); |
| 1923 | |
Cody Northrop | ef72e2a | 2015-08-03 17:04:53 -0600 | [diff] [blame] | 1924 | demo->queue_props = (VkQueueFamilyProperties *) malloc(demo->queue_count * sizeof(VkQueueFamilyProperties)); |
| 1925 | err = vkGetPhysicalDeviceQueueFamilyProperties(demo->gpu, &demo->queue_count, demo->queue_props); |
Courtney Goeltzenleuchter | f316806 | 2015-03-05 18:09:39 -0700 | [diff] [blame] | 1926 | assert(!err); |
Piers Daniell | 1cf7fe1 | 2015-07-16 09:35:35 -0600 | [diff] [blame] | 1927 | assert(demo->queue_count >= 1); |
Courtney Goeltzenleuchter | f316806 | 2015-03-05 18:09:39 -0700 | [diff] [blame] | 1928 | |
Courtney Goeltzenleuchter | 18061cd | 2015-06-29 15:39:26 -0600 | [diff] [blame] | 1929 | // Graphics queue and MemMgr queue can be separate. |
| 1930 | // TODO: Add support for separate queues, including synchronization, |
| 1931 | // and appropriate tracking for QueueSubmit |
Piers Daniell | 1cf7fe1 | 2015-07-16 09:35:35 -0600 | [diff] [blame] | 1932 | } |
| 1933 | |
| 1934 | static void demo_init_vk_wsi(struct demo *demo) |
| 1935 | { |
| 1936 | VkResult err; |
| 1937 | uint32_t i; |
Ian Elliott | e36b208 | 2015-07-06 14:27:58 -0600 | [diff] [blame] | 1938 | |
| 1939 | // Construct the WSI surface description: |
| 1940 | demo->surface_description.sType = VK_STRUCTURE_TYPE_SURFACE_DESCRIPTION_WINDOW_WSI; |
| 1941 | demo->surface_description.pNext = NULL; |
| 1942 | #ifdef _WIN32 |
| 1943 | demo->surface_description.platform = VK_PLATFORM_WIN32_WSI; |
| 1944 | demo->surface_description.pPlatformHandle = demo->connection; |
| 1945 | demo->surface_description.pPlatformWindow = demo->window; |
| 1946 | #else // _WIN32 |
| 1947 | demo->platform_handle_xcb.connection = demo->connection; |
| 1948 | demo->platform_handle_xcb.root = demo->screen->root; |
| 1949 | demo->surface_description.platform = VK_PLATFORM_XCB_WSI; |
| 1950 | demo->surface_description.pPlatformHandle = &demo->platform_handle_xcb; |
| 1951 | demo->surface_description.pPlatformWindow = &demo->window; |
| 1952 | #endif // _WIN32 |
| 1953 | |
| 1954 | // Iterate over each queue to learn whether it supports presenting to WSI: |
Piers Daniell | 1cf7fe1 | 2015-07-16 09:35:35 -0600 | [diff] [blame] | 1955 | VkBool32* supportsPresent = (VkBool32 *)malloc(demo->queue_count * sizeof(VkBool32)); |
| 1956 | for (i = 0; i < demo->queue_count; i++) { |
Ian Elliott | e36b208 | 2015-07-06 14:27:58 -0600 | [diff] [blame] | 1957 | demo->fpGetPhysicalDeviceSurfaceSupportWSI(demo->gpu, i, |
| 1958 | (VkSurfaceDescriptionWSI *) &demo->surface_description, |
| 1959 | &supportsPresent[i]); |
Courtney Goeltzenleuchter | f316806 | 2015-03-05 18:09:39 -0700 | [diff] [blame] | 1960 | } |
Ian Elliott | e36b208 | 2015-07-06 14:27:58 -0600 | [diff] [blame] | 1961 | |
| 1962 | // Search for a graphics and a present queue in the array of queue |
| 1963 | // families, try to find one that supports both |
| 1964 | uint32_t graphicsQueueNodeIndex = UINT32_MAX; |
| 1965 | uint32_t presentQueueNodeIndex = UINT32_MAX; |
Piers Daniell | 1cf7fe1 | 2015-07-16 09:35:35 -0600 | [diff] [blame] | 1966 | for (i = 0; i < demo->queue_count; i++) { |
Ian Elliott | e36b208 | 2015-07-06 14:27:58 -0600 | [diff] [blame] | 1967 | if ((demo->queue_props[i].queueFlags & VK_QUEUE_GRAPHICS_BIT) != 0) { |
| 1968 | if (graphicsQueueNodeIndex == UINT32_MAX) { |
| 1969 | graphicsQueueNodeIndex = i; |
| 1970 | } |
| 1971 | |
| 1972 | if (supportsPresent[i] == VK_TRUE) { |
| 1973 | graphicsQueueNodeIndex = i; |
| 1974 | presentQueueNodeIndex = i; |
| 1975 | break; |
| 1976 | } |
| 1977 | } |
| 1978 | } |
| 1979 | if (presentQueueNodeIndex == UINT32_MAX) { |
| 1980 | // If didn't find a queue that supports both graphics and present, then |
| 1981 | // find a separate present queue. |
Piers Daniell | 1cf7fe1 | 2015-07-16 09:35:35 -0600 | [diff] [blame] | 1982 | for (uint32_t i = 0; i < demo->queue_count; ++i) { |
Ian Elliott | e36b208 | 2015-07-06 14:27:58 -0600 | [diff] [blame] | 1983 | if (supportsPresent[i] == VK_TRUE) { |
| 1984 | presentQueueNodeIndex = i; |
| 1985 | break; |
| 1986 | } |
| 1987 | } |
| 1988 | } |
| 1989 | free(supportsPresent); |
| 1990 | |
| 1991 | // Generate error if could not find both a graphics and a present queue |
| 1992 | if (graphicsQueueNodeIndex == UINT32_MAX || presentQueueNodeIndex == UINT32_MAX) { |
| 1993 | ERR_EXIT("Could not find a graphics and a present queue\n", |
| 1994 | "WSI Initialization Failure"); |
| 1995 | } |
| 1996 | |
| 1997 | // TODO: Add support for separate queues, including presentation, |
| 1998 | // synchronization, and appropriate tracking for QueueSubmit |
| 1999 | // While it is possible for an application to use a separate graphics and a |
| 2000 | // present queues, this demo program assumes it is only using one: |
| 2001 | if (graphicsQueueNodeIndex != presentQueueNodeIndex) { |
| 2002 | ERR_EXIT("Could not find a common graphics and a present queue\n", |
| 2003 | "WSI Initialization Failure"); |
| 2004 | } |
| 2005 | |
| 2006 | demo->graphics_queue_node_index = graphicsQueueNodeIndex; |
Courtney Goeltzenleuchter | f316806 | 2015-03-05 18:09:39 -0700 | [diff] [blame] | 2007 | |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 2008 | err = vkGetDeviceQueue(demo->device, demo->graphics_queue_node_index, |
Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 2009 | 0, &demo->queue); |
| 2010 | assert(!err); |
Ian Elliott | 32536f9 | 2015-04-21 16:41:02 -0600 | [diff] [blame] | 2011 | |
Ian Elliott | e36b208 | 2015-07-06 14:27:58 -0600 | [diff] [blame] | 2012 | // Get the list of VkFormat's that are supported: |
Ian Elliott | 7fe115d | 2015-08-07 15:56:59 -0600 | [diff] [blame] | 2013 | uint32_t formatCount; |
| 2014 | err = demo->fpGetSurfaceFormatsWSI(demo->device, |
Ian Elliott | e36b208 | 2015-07-06 14:27:58 -0600 | [diff] [blame] | 2015 | (VkSurfaceDescriptionWSI *) &demo->surface_description, |
Ian Elliott | 7fe115d | 2015-08-07 15:56:59 -0600 | [diff] [blame] | 2016 | &formatCount, NULL); |
Ian Elliott | e36b208 | 2015-07-06 14:27:58 -0600 | [diff] [blame] | 2017 | assert(!err); |
Ian Elliott | 7fe115d | 2015-08-07 15:56:59 -0600 | [diff] [blame] | 2018 | VkSurfaceFormatWSI *surfFormats = |
| 2019 | (VkSurfaceFormatWSI *)malloc(formatCount * sizeof(VkSurfaceFormatWSI)); |
| 2020 | err = demo->fpGetSurfaceFormatsWSI(demo->device, |
Ian Elliott | e36b208 | 2015-07-06 14:27:58 -0600 | [diff] [blame] | 2021 | (VkSurfaceDescriptionWSI *) &demo->surface_description, |
Ian Elliott | 7fe115d | 2015-08-07 15:56:59 -0600 | [diff] [blame] | 2022 | &formatCount, surfFormats); |
Ian Elliott | e36b208 | 2015-07-06 14:27:58 -0600 | [diff] [blame] | 2023 | assert(!err); |
| 2024 | // If the format list includes just one entry of VK_FORMAT_UNDEFINED, |
| 2025 | // the surface has no preferred format. Otherwise, at least one |
| 2026 | // supported format will be returned. |
Ian Elliott | e36b208 | 2015-07-06 14:27:58 -0600 | [diff] [blame] | 2027 | if (formatCount == 1 && surfFormats[0].format == VK_FORMAT_UNDEFINED) |
| 2028 | { |
| 2029 | demo->format = VK_FORMAT_B8G8R8A8_UNORM; |
| 2030 | } |
| 2031 | else |
| 2032 | { |
| 2033 | assert(formatCount >= 1); |
| 2034 | demo->format = surfFormats[0].format; |
| 2035 | } |
Ian Elliott | 7fe115d | 2015-08-07 15:56:59 -0600 | [diff] [blame] | 2036 | demo->color_space = surfFormats[0].colorSpace; |
Ian Elliott | 32536f9 | 2015-04-21 16:41:02 -0600 | [diff] [blame] | 2037 | |
Mark Lobodzinski | 7234629 | 2015-07-02 16:49:40 -0600 | [diff] [blame] | 2038 | // Get Memory information and properties |
| 2039 | err = vkGetPhysicalDeviceMemoryProperties(demo->gpu, &demo->memory_properties); |
| 2040 | assert(!err); |
Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 2041 | } |
| 2042 | |
| 2043 | static void demo_init_connection(struct demo *demo) |
| 2044 | { |
Ian Elliott | e14e9f9 | 2015-04-16 15:23:05 -0600 | [diff] [blame] | 2045 | #ifndef _WIN32 |
Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 2046 | const xcb_setup_t *setup; |
| 2047 | xcb_screen_iterator_t iter; |
| 2048 | int scr; |
| 2049 | |
| 2050 | demo->connection = xcb_connect(NULL, &scr); |
Ian Elliott | dfe55f7 | 2015-04-03 15:24:55 -0600 | [diff] [blame] | 2051 | if (demo->connection == NULL) { |
| 2052 | printf("Cannot find a compatible Vulkan installable client driver " |
| 2053 | "(ICD).\nExiting ...\n"); |
| 2054 | fflush(stdout); |
| 2055 | exit(1); |
| 2056 | } |
Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 2057 | |
| 2058 | setup = xcb_get_setup(demo->connection); |
| 2059 | iter = xcb_setup_roots_iterator(setup); |
| 2060 | while (scr-- > 0) |
| 2061 | xcb_screen_next(&iter); |
| 2062 | |
| 2063 | demo->screen = iter.data; |
Ian Elliott | e14e9f9 | 2015-04-16 15:23:05 -0600 | [diff] [blame] | 2064 | #endif // _WIN32 |
Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 2065 | } |
| 2066 | |
Ian Elliott | e14e9f9 | 2015-04-16 15:23:05 -0600 | [diff] [blame] | 2067 | #ifdef _WIN32 |
| 2068 | static void demo_init(struct demo *demo, HINSTANCE hInstance, LPSTR pCmdLine) |
| 2069 | #else // _WIN32 |
Courtney Goeltzenleuchter | f113a95 | 2015-02-25 11:46:58 -0700 | [diff] [blame] | 2070 | 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] | 2071 | #endif // _WIN32 |
Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 2072 | { |
Ian Elliott | e14e9f9 | 2015-04-16 15:23:05 -0600 | [diff] [blame] | 2073 | bool argv_error = false; |
| 2074 | |
Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 2075 | memset(demo, 0, sizeof(*demo)); |
| 2076 | |
Ian Elliott | e14e9f9 | 2015-04-16 15:23:05 -0600 | [diff] [blame] | 2077 | #ifdef _WIN32 |
| 2078 | demo->connection = hInstance; |
Ian Elliott | 4e19ed0 | 2015-04-28 10:52:52 -0600 | [diff] [blame] | 2079 | strncpy(demo->name, APP_SHORT_NAME, APP_NAME_STR_LEN); |
Ian Elliott | e14e9f9 | 2015-04-16 15:23:05 -0600 | [diff] [blame] | 2080 | |
| 2081 | if (strncmp(pCmdLine, "--use_staging", strlen("--use_staging")) == 0) |
| 2082 | demo->use_staging_buffer = true; |
Cody Northrop | 75db032 | 2015-05-28 11:27:16 -0600 | [diff] [blame] | 2083 | else if (strncmp(pCmdLine, "--use_glsl", strlen("--use_glsl")) == 0) |
| 2084 | demo->use_glsl = true; |
Ian Elliott | e14e9f9 | 2015-04-16 15:23:05 -0600 | [diff] [blame] | 2085 | else if (strlen(pCmdLine) != 0) { |
| 2086 | fprintf(stderr, "Do not recognize argument \"%s\".\n", pCmdLine); |
| 2087 | argv_error = true; |
| 2088 | } |
| 2089 | #else // _WIN32 |
Courtney Goeltzenleuchter | f113a95 | 2015-02-25 11:46:58 -0700 | [diff] [blame] | 2090 | for (int i = 0; i < argc; i++) { |
| 2091 | if (strncmp(argv[i], "--use_staging", strlen("--use_staging")) == 0) |
| 2092 | demo->use_staging_buffer = true; |
Cody Northrop | 75db032 | 2015-05-28 11:27:16 -0600 | [diff] [blame] | 2093 | else if (strncmp(argv[i], "--use_glsl", strlen("--use_glsl")) == 0) |
| 2094 | demo->use_glsl = true; |
Courtney Goeltzenleuchter | f113a95 | 2015-02-25 11:46:58 -0700 | [diff] [blame] | 2095 | } |
Ian Elliott | e14e9f9 | 2015-04-16 15:23:05 -0600 | [diff] [blame] | 2096 | #endif // _WIN32 |
| 2097 | if (argv_error) { |
Ian Elliott | 4e19ed0 | 2015-04-28 10:52:52 -0600 | [diff] [blame] | 2098 | fprintf(stderr, "Usage:\n %s [--use_staging]\n", APP_SHORT_NAME); |
Ian Elliott | e14e9f9 | 2015-04-16 15:23:05 -0600 | [diff] [blame] | 2099 | fflush(stderr); |
| 2100 | exit(1); |
| 2101 | } |
Courtney Goeltzenleuchter | f113a95 | 2015-02-25 11:46:58 -0700 | [diff] [blame] | 2102 | |
Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 2103 | demo_init_connection(demo); |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 2104 | demo_init_vk(demo); |
Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 2105 | |
| 2106 | demo->width = 300; |
| 2107 | demo->height = 300; |
Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 2108 | } |
| 2109 | |
| 2110 | static void demo_cleanup(struct demo *demo) |
| 2111 | { |
Mark Lobodzinski | 2318261 | 2015-05-29 09:32:35 -0500 | [diff] [blame] | 2112 | uint32_t i; |
Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 2113 | |
Tony Barbour | de4124d | 2015-07-03 10:33:54 -0600 | [diff] [blame] | 2114 | for (i = 0; i < DEMO_BUFFER_COUNT; i++) { |
| 2115 | vkDestroyFramebuffer(demo->device, demo->framebuffers[i]); |
| 2116 | } |
Tony Barbour | b857d31 | 2015-07-10 10:50:45 -0600 | [diff] [blame] | 2117 | vkFreeDescriptorSets(demo->device, demo->desc_pool, 1, &demo->desc_set); |
Tony Barbour | de4124d | 2015-07-03 10:33:54 -0600 | [diff] [blame] | 2118 | vkDestroyDescriptorPool(demo->device, demo->desc_pool); |
Chia-I Wu | f838506 | 2015-01-04 16:27:24 +0800 | [diff] [blame] | 2119 | |
Courtney Goeltzenleuchter | 633f9c5 | 2015-04-30 10:58:33 -0600 | [diff] [blame] | 2120 | if (demo->setup_cmd) { |
Tony Barbour | de4124d | 2015-07-03 10:33:54 -0600 | [diff] [blame] | 2121 | vkDestroyCommandBuffer(demo->device, demo->setup_cmd); |
Courtney Goeltzenleuchter | 633f9c5 | 2015-04-30 10:58:33 -0600 | [diff] [blame] | 2122 | } |
Tony Barbour | de4124d | 2015-07-03 10:33:54 -0600 | [diff] [blame] | 2123 | vkDestroyCommandBuffer(demo->device, demo->draw_cmd); |
Piers Daniell | 1cf7fe1 | 2015-07-16 09:35:35 -0600 | [diff] [blame] | 2124 | vkDestroyCommandPool(demo->device, demo->cmd_pool); |
Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 2125 | |
Cody Northrop | e4bc694 | 2015-08-26 10:01:32 -0600 | [diff] [blame] | 2126 | vkDestroyDynamicViewportState(demo->device, demo->dynamic_viewport); |
| 2127 | vkDestroyDynamicLineWidthState(demo->device, demo->dynamic_line_width); |
| 2128 | vkDestroyDynamicDepthBiasState(demo->device, demo->dynamic_depth_bias); |
| 2129 | vkDestroyDynamicBlendState(demo->device, demo->dynamic_blend); |
| 2130 | vkDestroyDynamicDepthBoundsState(demo->device, demo->dynamic_depth_bounds); |
Cody Northrop | 2605cb0 | 2015-08-18 15:21:16 -0600 | [diff] [blame] | 2131 | vkDestroyDynamicStencilState(demo->device, demo->dynamic_stencil); |
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 | vkDestroyPipeline(demo->device, demo->pipeline); |
| 2134 | vkDestroyRenderPass(demo->device, demo->render_pass); |
| 2135 | vkDestroyPipelineLayout(demo->device, demo->pipeline_layout); |
| 2136 | vkDestroyDescriptorSetLayout(demo->device, demo->desc_layout); |
Chia-I Wu | b043fe3 | 2014-10-06 15:30:33 +0800 | [diff] [blame] | 2137 | |
Tony Barbour | de4124d | 2015-07-03 10:33:54 -0600 | [diff] [blame] | 2138 | vkDestroyBuffer(demo->device, demo->vertices.buf); |
Mark Lobodzinski | 2318261 | 2015-05-29 09:32:35 -0500 | [diff] [blame] | 2139 | vkFreeMemory(demo->device, demo->vertices.mem); |
Chia-I Wu | 99621bc | 2014-10-08 11:52:22 +0800 | [diff] [blame] | 2140 | |
Chia-I Wu | b043fe3 | 2014-10-06 15:30:33 +0800 | [diff] [blame] | 2141 | for (i = 0; i < DEMO_TEXTURE_COUNT; i++) { |
Tony Barbour | de4124d | 2015-07-03 10:33:54 -0600 | [diff] [blame] | 2142 | vkDestroyImageView(demo->device, demo->textures[i].view); |
| 2143 | vkDestroyImage(demo->device, demo->textures[i].image); |
Mark Lobodzinski | 2318261 | 2015-05-29 09:32:35 -0500 | [diff] [blame] | 2144 | vkFreeMemory(demo->device, demo->textures[i].mem); |
Tony Barbour | de4124d | 2015-07-03 10:33:54 -0600 | [diff] [blame] | 2145 | vkDestroySampler(demo->device, demo->textures[i].sampler); |
Chia-I Wu | b043fe3 | 2014-10-06 15:30:33 +0800 | [diff] [blame] | 2146 | } |
| 2147 | |
Mark Lobodzinski | 8a19064 | 2015-08-07 10:17:13 -0600 | [diff] [blame] | 2148 | for (i = 0; i < demo->swapChainImageCount; i++) { |
Tony Barbour | de4124d | 2015-07-03 10:33:54 -0600 | [diff] [blame] | 2149 | vkDestroyAttachmentView(demo->device, demo->buffers[i].view); |
Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 2150 | } |
Tony Barbour | de4124d | 2015-07-03 10:33:54 -0600 | [diff] [blame] | 2151 | |
| 2152 | vkDestroyAttachmentView(demo->device, demo->depth.view); |
| 2153 | vkDestroyImage(demo->device, demo->depth.image); |
| 2154 | vkFreeMemory(demo->device, demo->depth.mem); |
| 2155 | |
Ian Elliott | e36b208 | 2015-07-06 14:27:58 -0600 | [diff] [blame] | 2156 | demo->fpDestroySwapChainWSI(demo->device, demo->swap_chain); |
| 2157 | free(demo->buffers); |
Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 2158 | |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 2159 | vkDestroyDevice(demo->device); |
| 2160 | vkDestroyInstance(demo->inst); |
Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 2161 | |
Ian Elliott | e14e9f9 | 2015-04-16 15:23:05 -0600 | [diff] [blame] | 2162 | #ifndef _WIN32 |
Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 2163 | xcb_destroy_window(demo->connection, demo->window); |
| 2164 | xcb_disconnect(demo->connection); |
Ian Elliott | e14e9f9 | 2015-04-16 15:23:05 -0600 | [diff] [blame] | 2165 | #endif // _WIN32 |
Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 2166 | } |
| 2167 | |
Ian Elliott | e14e9f9 | 2015-04-16 15:23:05 -0600 | [diff] [blame] | 2168 | #ifdef _WIN32 |
| 2169 | int APIENTRY WinMain(HINSTANCE hInstance, |
| 2170 | HINSTANCE hPrevInstance, |
| 2171 | LPSTR pCmdLine, |
| 2172 | int nCmdShow) |
| 2173 | { |
| 2174 | MSG msg; // message |
| 2175 | bool done; // flag saying when app is complete |
| 2176 | |
| 2177 | demo_init(&demo, hInstance, pCmdLine); |
| 2178 | demo_create_window(&demo); |
Piers Daniell | 1cf7fe1 | 2015-07-16 09:35:35 -0600 | [diff] [blame] | 2179 | demo_init_vk_wsi(&demo); |
Ian Elliott | e14e9f9 | 2015-04-16 15:23:05 -0600 | [diff] [blame] | 2180 | |
| 2181 | demo_prepare(&demo); |
| 2182 | |
| 2183 | done = false; //initialize loop condition variable |
| 2184 | /* main message loop*/ |
| 2185 | while(!done) |
| 2186 | { |
Ian Elliott | 421107f | 2015-04-28 15:50:36 -0600 | [diff] [blame] | 2187 | PeekMessage(&msg, NULL, 0, 0, PM_REMOVE); |
Ian Elliott | e14e9f9 | 2015-04-16 15:23:05 -0600 | [diff] [blame] | 2188 | if (msg.message == WM_QUIT) //check for a quit message |
| 2189 | { |
| 2190 | done = true; //if found, quit app |
| 2191 | } |
| 2192 | else |
| 2193 | { |
| 2194 | /* Translate and dispatch to event queue*/ |
| 2195 | TranslateMessage(&msg); |
| 2196 | DispatchMessage(&msg); |
| 2197 | } |
| 2198 | } |
| 2199 | |
| 2200 | demo_cleanup(&demo); |
| 2201 | |
Tony Barbour | a938abb | 2015-04-22 11:36:22 -0600 | [diff] [blame] | 2202 | return (int) msg.wParam; |
Ian Elliott | e14e9f9 | 2015-04-16 15:23:05 -0600 | [diff] [blame] | 2203 | } |
| 2204 | #else // _WIN32 |
Courtney Goeltzenleuchter | f113a95 | 2015-02-25 11:46:58 -0700 | [diff] [blame] | 2205 | int main(const int argc, const char *argv[]) |
Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 2206 | { |
| 2207 | struct demo demo; |
| 2208 | |
Courtney Goeltzenleuchter | f113a95 | 2015-02-25 11:46:58 -0700 | [diff] [blame] | 2209 | demo_init(&demo, argc, argv); |
Chia-I Wu | 5b66aa5 | 2015-04-16 22:02:10 +0800 | [diff] [blame] | 2210 | demo_create_window(&demo); |
Piers Daniell | 1cf7fe1 | 2015-07-16 09:35:35 -0600 | [diff] [blame] | 2211 | demo_init_vk_wsi(&demo); |
Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 2212 | |
| 2213 | demo_prepare(&demo); |
Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 2214 | demo_run(&demo); |
| 2215 | |
| 2216 | demo_cleanup(&demo); |
| 2217 | |
Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 2218 | return 0; |
| 2219 | } |
Ian Elliott | e14e9f9 | 2015-04-16 15:23:05 -0600 | [diff] [blame] | 2220 | #endif // _WIN32 |