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