| Chia-I Wu | f5caeb0 | 2014-10-25 12:11:27 +0800 | [diff] [blame] | 1 | /* | 
| Karl Schultz | 481756e | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 2 | * Copyright (c) 2015-2016 The Khronos Group Inc. | 
|  | 3 | * Copyright (c) 2015-2016 Valve Corporation | 
|  | 4 | * Copyright (c) 2015-2016 LunarG, Inc. | 
| Ian Elliott | 7595eee | 2015-04-28 10:33:11 -0600 | [diff] [blame] | 5 | * | 
| Karl Schultz | 481756e | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy | 
|  | 7 | * of this software and/or associated documentation files (the "Materials"), to | 
|  | 8 | * deal in the Materials without restriction, including without limitation the | 
|  | 9 | * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or | 
|  | 10 | * sell copies of the Materials, and to permit persons to whom the Materials are | 
|  | 11 | * furnished to do so, subject to the following conditions: | 
| Ian Elliott | 7595eee | 2015-04-28 10:33:11 -0600 | [diff] [blame] | 12 | * | 
| Karl Schultz | 481756e | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 13 | * The above copyright notice(s) and this permission notice shall be included in | 
|  | 14 | * all copies or substantial portions of the Materials. | 
| Ian Elliott | 7595eee | 2015-04-28 10:33:11 -0600 | [diff] [blame] | 15 | * | 
| Karl Schultz | 481756e | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 16 | * THE MATERIALS ARE PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | 
| Ian Elliott | 7595eee | 2015-04-28 10:33:11 -0600 | [diff] [blame] | 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | 
| Karl Schultz | 481756e | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. | 
|  | 19 | * | 
|  | 20 | * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, | 
|  | 21 | * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR | 
|  | 22 | * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE MATERIALS OR THE | 
|  | 23 | * USE OR OTHER DEALINGS IN THE MATERIALS. | 
| Courtney Goeltzenleuchter | 96cd795 | 2015-10-30 11:14:30 -0600 | [diff] [blame] | 24 | * | 
|  | 25 | * Author: Chia-I Wu <olvaffe@gmail.com> | 
|  | 26 | * Author: Cody Northrop <cody@lunarg.com> | 
|  | 27 | * Author: Courtney Goeltzenleuchter <courtney@LunarG.com> | 
|  | 28 | * Author: Ian Elliott <ian@LunarG.com> | 
|  | 29 | * Author: Jon Ashburn <jon@lunarg.com> | 
|  | 30 | * Author: Piers Daniell <pdaniell@nvidia.com> | 
| Ian Elliott | 7595eee | 2015-04-28 10:33:11 -0600 | [diff] [blame] | 31 | */ | 
|  | 32 | /* | 
| Chia-I Wu | f5caeb0 | 2014-10-25 12:11:27 +0800 | [diff] [blame] | 33 | * Draw a textured triangle with depth testing.  This is written against Intel | 
|  | 34 | * ICD.  It does not do state transition nor object memory binding like it | 
|  | 35 | * should.  It also does no error checking. | 
|  | 36 | */ | 
|  | 37 |  | 
| Mark Young | b8b6f0c | 2016-01-26 15:09:10 -0700 | [diff] [blame] | 38 | #ifndef _MSC_VER | 
|  | 39 | #define _ISOC11_SOURCE /* for aligned_alloc() */ | 
|  | 40 | #endif | 
|  | 41 |  | 
| Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 42 | #include <stdio.h> | 
|  | 43 | #include <stdlib.h> | 
|  | 44 | #include <string.h> | 
|  | 45 | #include <stdbool.h> | 
|  | 46 | #include <assert.h> | 
| Karl Schultz | 8b100ca | 2016-03-25 14:31:16 -0600 | [diff] [blame] | 47 | #include <signal.h> | 
| Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 48 |  | 
| Ian Elliott | e14e9f9 | 2015-04-16 15:23:05 -0600 | [diff] [blame] | 49 | #ifdef _WIN32 | 
|  | 50 | #pragma comment(linker, "/subsystem:windows") | 
| Ian Elliott | e14e9f9 | 2015-04-16 15:23:05 -0600 | [diff] [blame] | 51 | #define APP_NAME_STR_LEN 80 | 
| Ian Elliott | e14e9f9 | 2015-04-16 15:23:05 -0600 | [diff] [blame] | 52 | #endif // _WIN32 | 
|  | 53 |  | 
| David Pinedo | 329ca9e | 2015-11-06 12:54:48 -0700 | [diff] [blame] | 54 | #include <vulkan/vulkan.h> | 
| Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 55 |  | 
| Chia-I Wu | b043fe3 | 2014-10-06 15:30:33 +0800 | [diff] [blame] | 56 | #define DEMO_TEXTURE_COUNT 1 | 
| Courtney Goeltzenleuchter | f5cdad0 | 2015-03-31 16:36:30 -0600 | [diff] [blame] | 57 | #define VERTEX_BUFFER_BIND_ID 0 | 
| Ian Elliott | 4e19ed0 | 2015-04-28 10:52:52 -0600 | [diff] [blame] | 58 | #define APP_SHORT_NAME "tri" | 
|  | 59 | #define APP_LONG_NAME "The Vulkan Triangle Demo Program" | 
| Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 60 |  | 
| Ian Elliott | e36b208 | 2015-07-06 14:27:58 -0600 | [diff] [blame] | 61 | #define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0])) | 
|  | 62 |  | 
| Tony Barbour | 22a3086 | 2015-04-22 09:02:32 -0600 | [diff] [blame] | 63 | #if defined(NDEBUG) && defined(__GNUC__) | 
|  | 64 | #define U_ASSERT_ONLY __attribute__((unused)) | 
|  | 65 | #else | 
|  | 66 | #define U_ASSERT_ONLY | 
|  | 67 | #endif | 
|  | 68 |  | 
| Ian Elliott | caa9f27 | 2015-04-28 11:35:02 -0600 | [diff] [blame] | 69 | #ifdef _WIN32 | 
| Karl Schultz | 481756e | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 70 | #define ERR_EXIT(err_msg, err_class)                                           \ | 
|  | 71 | do {                                                                       \ | 
|  | 72 | MessageBox(NULL, err_msg, err_class, MB_OK);                           \ | 
|  | 73 | exit(1);                                                               \ | 
|  | 74 | } while (0) | 
|  | 75 | #else // _WIN32 | 
| Ian Elliott | caa9f27 | 2015-04-28 11:35:02 -0600 | [diff] [blame] | 76 |  | 
| Karl Schultz | 481756e | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 77 | #define ERR_EXIT(err_msg, err_class)                                           \ | 
|  | 78 | do {                                                                       \ | 
|  | 79 | printf(err_msg);                                                       \ | 
|  | 80 | fflush(stdout);                                                        \ | 
|  | 81 | exit(1);                                                               \ | 
|  | 82 | } while (0) | 
| Ian Elliott | caa9f27 | 2015-04-28 11:35:02 -0600 | [diff] [blame] | 83 | #endif // _WIN32 | 
| Tony Barbour | 22a3086 | 2015-04-22 09:02:32 -0600 | [diff] [blame] | 84 |  | 
| Karl Schultz | 481756e | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 85 | #define GET_INSTANCE_PROC_ADDR(inst, entrypoint)                               \ | 
|  | 86 | {                                                                          \ | 
|  | 87 | demo->fp##entrypoint =                                                 \ | 
|  | 88 | (PFN_vk##entrypoint)vkGetInstanceProcAddr(inst, "vk" #entrypoint); \ | 
|  | 89 | if (demo->fp##entrypoint == NULL) {                                    \ | 
|  | 90 | ERR_EXIT("vkGetInstanceProcAddr failed to find vk" #entrypoint,    \ | 
|  | 91 | "vkGetInstanceProcAddr Failure");                         \ | 
|  | 92 | }                                                                      \ | 
|  | 93 | } | 
| Ian Elliott | e36b208 | 2015-07-06 14:27:58 -0600 | [diff] [blame] | 94 |  | 
| Karl Schultz | 481756e | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 95 | #define GET_DEVICE_PROC_ADDR(dev, entrypoint)                                  \ | 
|  | 96 | {                                                                          \ | 
|  | 97 | demo->fp##entrypoint =                                                 \ | 
|  | 98 | (PFN_vk##entrypoint)vkGetDeviceProcAddr(dev, "vk" #entrypoint);    \ | 
|  | 99 | if (demo->fp##entrypoint == NULL) {                                    \ | 
|  | 100 | ERR_EXIT("vkGetDeviceProcAddr failed to find vk" #entrypoint,      \ | 
|  | 101 | "vkGetDeviceProcAddr Failure");                           \ | 
|  | 102 | }                                                                      \ | 
|  | 103 | } | 
| Ian Elliott | 1b6de09 | 2015-06-22 15:07:49 -0600 | [diff] [blame] | 104 |  | 
| Courtney Goeltzenleuchter | bfccf41 | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 105 | struct texture_object { | 
| Courtney Goeltzenleuchter | 382489d | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 106 | VkSampler sampler; | 
| Courtney Goeltzenleuchter | 372e13c | 2015-02-13 17:52:46 -0700 | [diff] [blame] | 107 |  | 
| Courtney Goeltzenleuchter | 382489d | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 108 | VkImage image; | 
|  | 109 | VkImageLayout imageLayout; | 
| Courtney Goeltzenleuchter | bfccf41 | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 110 |  | 
| Mark Lobodzinski | 2318261 | 2015-05-29 09:32:35 -0500 | [diff] [blame] | 111 | VkDeviceMemory mem; | 
| Courtney Goeltzenleuchter | 382489d | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 112 | VkImageView view; | 
| Courtney Goeltzenleuchter | 372e13c | 2015-02-13 17:52:46 -0700 | [diff] [blame] | 113 | int32_t tex_width, tex_height; | 
|  | 114 | }; | 
|  | 115 |  | 
| Karl Schultz | 501fdc2 | 2016-03-24 18:43:41 -0600 | [diff] [blame] | 116 | static int validation_error = 0; | 
|  | 117 |  | 
| Karl Schultz | 481756e | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 118 | VKAPI_ATTR VkBool32 VKAPI_CALL | 
|  | 119 | dbgFunc(VkFlags msgFlags, VkDebugReportObjectTypeEXT objType, | 
|  | 120 | uint64_t srcObject, size_t location, int32_t msgCode, | 
|  | 121 | const char *pLayerPrefix, const char *pMsg, void *pUserData) { | 
|  | 122 | char *message = (char *)malloc(strlen(pMsg) + 100); | 
| Courtney Goeltzenleuchter | 18061cd | 2015-06-29 15:39:26 -0600 | [diff] [blame] | 123 |  | 
| Karl Schultz | 481756e | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 124 | assert(message); | 
| Courtney Goeltzenleuchter | 18061cd | 2015-06-29 15:39:26 -0600 | [diff] [blame] | 125 |  | 
| Karl Schultz | 501fdc2 | 2016-03-24 18:43:41 -0600 | [diff] [blame] | 126 | validation_error = 1; | 
|  | 127 |  | 
| Courtney Goeltzenleuchter | acb1359 | 2015-12-09 15:48:16 -0700 | [diff] [blame] | 128 | if (msgFlags & VK_DEBUG_REPORT_ERROR_BIT_EXT) { | 
| Karl Schultz | 481756e | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 129 | sprintf(message, "ERROR: [%s] Code %d : %s", pLayerPrefix, msgCode, | 
|  | 130 | pMsg); | 
| Mark Lobodzinski | 5c13d4d | 2016-02-11 09:26:16 -0700 | [diff] [blame] | 131 | } else if (msgFlags & VK_DEBUG_REPORT_WARNING_BIT_EXT) { | 
| Karl Schultz | 481756e | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 132 | sprintf(message, "WARNING: [%s] Code %d : %s", pLayerPrefix, msgCode, | 
|  | 133 | pMsg); | 
| Courtney Goeltzenleuchter | 18061cd | 2015-06-29 15:39:26 -0600 | [diff] [blame] | 134 | } else { | 
| Courtney Goeltzenleuchter | 0d6857f | 2015-09-04 13:52:24 -0600 | [diff] [blame] | 135 | return false; | 
| Courtney Goeltzenleuchter | 18061cd | 2015-06-29 15:39:26 -0600 | [diff] [blame] | 136 | } | 
|  | 137 |  | 
|  | 138 | #ifdef _WIN32 | 
|  | 139 | MessageBox(NULL, message, "Alert", MB_OK); | 
|  | 140 | #else | 
| Karl Schultz | 481756e | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 141 | printf("%s\n", message); | 
| Courtney Goeltzenleuchter | 18061cd | 2015-06-29 15:39:26 -0600 | [diff] [blame] | 142 | fflush(stdout); | 
|  | 143 | #endif | 
|  | 144 | free(message); | 
| Courtney Goeltzenleuchter | e4c4313 | 2015-09-08 16:57:22 -0600 | [diff] [blame] | 145 |  | 
|  | 146 | /* | 
|  | 147 | * false indicates that layer should not bail-out of an | 
|  | 148 | * API call that had validation failures. This may mean that the | 
|  | 149 | * app dies inside the driver due to invalid parameter(s). | 
|  | 150 | * That's what would happen without validation layers, so we'll | 
|  | 151 | * keep that behavior here. | 
|  | 152 | */ | 
|  | 153 | return false; | 
| Courtney Goeltzenleuchter | 18061cd | 2015-06-29 15:39:26 -0600 | [diff] [blame] | 154 | } | 
|  | 155 |  | 
| Karl Schultz | 5b10b81 | 2016-03-25 15:35:18 -0600 | [diff] [blame] | 156 | VKAPI_ATTR VkBool32 VKAPI_CALL | 
|  | 157 | BreakCallback(VkFlags msgFlags, VkDebugReportObjectTypeEXT objType, | 
|  | 158 | uint64_t srcObject, size_t location, int32_t msgCode, | 
|  | 159 | const char *pLayerPrefix, const char *pMsg, | 
|  | 160 | void *pUserData) { | 
| Karl Schultz | 7c5f63e | 2016-03-25 14:25:16 -0600 | [diff] [blame] | 161 | #ifndef WIN32 | 
|  | 162 | raise(SIGTRAP); | 
|  | 163 | #else | 
|  | 164 | DebugBreak(); | 
|  | 165 | #endif | 
|  | 166 |  | 
|  | 167 | return false; | 
|  | 168 | } | 
|  | 169 |  | 
| Ian Elliott | 338dedb | 2015-08-21 15:09:33 -0600 | [diff] [blame] | 170 | typedef struct _SwapchainBuffers { | 
| Ian Elliott | e36b208 | 2015-07-06 14:27:58 -0600 | [diff] [blame] | 171 | VkImage image; | 
| Chia-I Wu | 1f85191 | 2015-10-27 18:04:07 +0800 | [diff] [blame] | 172 | VkCommandBuffer cmd; | 
| Courtney Goeltzenleuchter | 1856d6f | 2015-09-01 17:30:39 -0600 | [diff] [blame] | 173 | VkImageView view; | 
| Ian Elliott | 338dedb | 2015-08-21 15:09:33 -0600 | [diff] [blame] | 174 | } SwapchainBuffers; | 
| Ian Elliott | e36b208 | 2015-07-06 14:27:58 -0600 | [diff] [blame] | 175 |  | 
| Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 176 | struct demo { | 
| Ian Elliott | e14e9f9 | 2015-04-16 15:23:05 -0600 | [diff] [blame] | 177 | #ifdef _WIN32 | 
|  | 178 | #define APP_NAME_STR_LEN 80 | 
|  | 179 | HINSTANCE connection;        // hInstance - Windows Instance | 
|  | 180 | char name[APP_NAME_STR_LEN]; // Name to put on the window/icon | 
| Karl Schultz | 481756e | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 181 | HWND window;                 // hWnd - window handle | 
|  | 182 | #else                            // _WIN32 | 
| Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 183 | xcb_connection_t *connection; | 
|  | 184 | xcb_screen_t *screen; | 
| Chia-I Wu | 5b66aa5 | 2015-04-16 22:02:10 +0800 | [diff] [blame] | 185 | xcb_window_t window; | 
|  | 186 | xcb_intern_atom_reply_t *atom_wm_delete_window; | 
| Karl Schultz | 481756e | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 187 | #endif                           // _WIN32 | 
| Ian Elliott | b5fad79 | 2015-11-20 11:55:46 -0700 | [diff] [blame] | 188 | VkSurfaceKHR surface; | 
|  | 189 | bool prepared; | 
| Ian Elliott | e14e9f9 | 2015-04-16 15:23:05 -0600 | [diff] [blame] | 190 | bool use_staging_buffer; | 
| Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 191 |  | 
| Courtney Goeltzenleuchter | 382489d | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 192 | VkInstance inst; | 
| Tony Barbour | 8205d90 | 2015-04-16 15:59:00 -0600 | [diff] [blame] | 193 | VkPhysicalDevice gpu; | 
| Courtney Goeltzenleuchter | 382489d | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 194 | VkDevice device; | 
|  | 195 | VkQueue queue; | 
| Tony Barbour | 426b905 | 2015-06-24 16:06:58 -0600 | [diff] [blame] | 196 | VkPhysicalDeviceProperties gpu_props; | 
| Cody Northrop | ef72e2a | 2015-08-03 17:04:53 -0600 | [diff] [blame] | 197 | VkQueueFamilyProperties *queue_props; | 
| Courtney Goeltzenleuchter | f316806 | 2015-03-05 18:09:39 -0700 | [diff] [blame] | 198 | uint32_t graphics_queue_node_index; | 
| Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 199 |  | 
| Tony Barbour | 3d03a4a | 2016-01-22 14:36:40 -0700 | [diff] [blame] | 200 | uint32_t enabled_extension_count; | 
|  | 201 | uint32_t enabled_layer_count; | 
|  | 202 | char *extension_names[64]; | 
|  | 203 | char *device_validation_layers[64]; | 
|  | 204 |  | 
| Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 205 | int width, height; | 
| Courtney Goeltzenleuchter | 382489d | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 206 | VkFormat format; | 
| Ian Elliott | 338dedb | 2015-08-21 15:09:33 -0600 | [diff] [blame] | 207 | VkColorSpaceKHR color_space; | 
| Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 208 |  | 
| Karl Schultz | 481756e | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 209 | PFN_vkGetPhysicalDeviceSurfaceSupportKHR | 
|  | 210 | fpGetPhysicalDeviceSurfaceSupportKHR; | 
|  | 211 | PFN_vkGetPhysicalDeviceSurfaceCapabilitiesKHR | 
|  | 212 | fpGetPhysicalDeviceSurfaceCapabilitiesKHR; | 
|  | 213 | PFN_vkGetPhysicalDeviceSurfaceFormatsKHR | 
|  | 214 | fpGetPhysicalDeviceSurfaceFormatsKHR; | 
|  | 215 | PFN_vkGetPhysicalDeviceSurfacePresentModesKHR | 
|  | 216 | fpGetPhysicalDeviceSurfacePresentModesKHR; | 
| Ian Elliott | 338dedb | 2015-08-21 15:09:33 -0600 | [diff] [blame] | 217 | PFN_vkCreateSwapchainKHR fpCreateSwapchainKHR; | 
|  | 218 | PFN_vkDestroySwapchainKHR fpDestroySwapchainKHR; | 
|  | 219 | PFN_vkGetSwapchainImagesKHR fpGetSwapchainImagesKHR; | 
|  | 220 | PFN_vkAcquireNextImageKHR fpAcquireNextImageKHR; | 
|  | 221 | PFN_vkQueuePresentKHR fpQueuePresentKHR; | 
| Ian Elliott | 338dedb | 2015-08-21 15:09:33 -0600 | [diff] [blame] | 222 | uint32_t swapchainImageCount; | 
| Ian Elliott | e2688a5 | 2015-10-16 18:02:43 -0600 | [diff] [blame] | 223 | VkSwapchainKHR swapchain; | 
| Ian Elliott | 338dedb | 2015-08-21 15:09:33 -0600 | [diff] [blame] | 224 | SwapchainBuffers *buffers; | 
| Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 225 |  | 
| Chia-I Wu | 1f85191 | 2015-10-27 18:04:07 +0800 | [diff] [blame] | 226 | VkCommandPool cmd_pool; | 
| Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 227 |  | 
| Chia-I Wu | b043fe3 | 2014-10-06 15:30:33 +0800 | [diff] [blame] | 228 | struct { | 
| Courtney Goeltzenleuchter | 382489d | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 229 | VkFormat format; | 
| Chia-I Wu | 9ae87c9 | 2014-10-07 14:15:01 +0800 | [diff] [blame] | 230 |  | 
| Courtney Goeltzenleuchter | 382489d | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 231 | VkImage image; | 
| Mark Lobodzinski | 2318261 | 2015-05-29 09:32:35 -0500 | [diff] [blame] | 232 | VkDeviceMemory mem; | 
| Courtney Goeltzenleuchter | 1856d6f | 2015-09-01 17:30:39 -0600 | [diff] [blame] | 233 | VkImageView view; | 
| Chia-I Wu | 9ae87c9 | 2014-10-07 14:15:01 +0800 | [diff] [blame] | 234 | } depth; | 
|  | 235 |  | 
| Courtney Goeltzenleuchter | bfccf41 | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 236 | struct texture_object textures[DEMO_TEXTURE_COUNT]; | 
| Chia-I Wu | b043fe3 | 2014-10-06 15:30:33 +0800 | [diff] [blame] | 237 |  | 
| Chia-I Wu | 99621bc | 2014-10-08 11:52:22 +0800 | [diff] [blame] | 238 | struct { | 
| Courtney Goeltzenleuchter | 382489d | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 239 | VkBuffer buf; | 
| Mark Lobodzinski | 2318261 | 2015-05-29 09:32:35 -0500 | [diff] [blame] | 240 | VkDeviceMemory mem; | 
| Chia-I Wu | 8d29d02 | 2014-10-08 12:14:39 +0800 | [diff] [blame] | 241 |  | 
| Mark Lobodzinski | 0e0fb5c | 2015-06-23 15:11:57 -0600 | [diff] [blame] | 242 | VkPipelineVertexInputStateCreateInfo vi; | 
| Courtney Goeltzenleuchter | 382489d | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 243 | VkVertexInputBindingDescription vi_bindings[1]; | 
|  | 244 | VkVertexInputAttributeDescription vi_attrs[2]; | 
| Chia-I Wu | 99621bc | 2014-10-08 11:52:22 +0800 | [diff] [blame] | 245 | } vertices; | 
|  | 246 |  | 
| Karl Schultz | 481756e | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 247 | VkCommandBuffer setup_cmd; // Command Buffer for initialization commands | 
| Chia-I Wu | 1f85191 | 2015-10-27 18:04:07 +0800 | [diff] [blame] | 248 | VkCommandBuffer draw_cmd;  // Command Buffer for drawing commands | 
| Mark Lobodzinski | 556f721 | 2015-04-17 14:11:39 -0500 | [diff] [blame] | 249 | VkPipelineLayout pipeline_layout; | 
| Courtney Goeltzenleuchter | 382489d | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 250 | VkDescriptorSetLayout desc_layout; | 
| Jon Ashburn | 0d60d27 | 2015-07-09 15:02:25 -0600 | [diff] [blame] | 251 | VkPipelineCache pipelineCache; | 
| Chia-I Wu | 76cd422 | 2015-07-08 13:34:24 +0800 | [diff] [blame] | 252 | VkRenderPass render_pass; | 
| Courtney Goeltzenleuchter | 382489d | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 253 | VkPipeline pipeline; | 
| Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 254 |  | 
| Mark Lobodzinski | a43e24c | 2015-08-10 13:40:32 -0600 | [diff] [blame] | 255 | VkShaderModule vert_shader_module; | 
|  | 256 | VkShaderModule frag_shader_module; | 
|  | 257 |  | 
| Courtney Goeltzenleuchter | 382489d | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 258 | VkDescriptorPool desc_pool; | 
|  | 259 | VkDescriptorSet desc_set; | 
| Chia-I Wu | f838506 | 2015-01-04 16:27:24 +0800 | [diff] [blame] | 260 |  | 
| Tony Barbour | 5aabff5 | 2015-10-08 14:26:24 -0600 | [diff] [blame] | 261 | VkFramebuffer *framebuffers; | 
| Chia-I Wu | 76cd422 | 2015-07-08 13:34:24 +0800 | [diff] [blame] | 262 |  | 
| Mark Lobodzinski | 7234629 | 2015-07-02 16:49:40 -0600 | [diff] [blame] | 263 | VkPhysicalDeviceMemoryProperties memory_properties; | 
|  | 264 |  | 
| Karl Schultz | 501fdc2 | 2016-03-24 18:43:41 -0600 | [diff] [blame] | 265 | int32_t curFrame; | 
|  | 266 | int32_t frameCount; | 
| Courtney Goeltzenleuchter | 18061cd | 2015-06-29 15:39:26 -0600 | [diff] [blame] | 267 | bool validate; | 
| Karl Schultz | 501fdc2 | 2016-03-24 18:43:41 -0600 | [diff] [blame] | 268 | bool use_break; | 
| Courtney Goeltzenleuchter | acb1359 | 2015-12-09 15:48:16 -0700 | [diff] [blame] | 269 | PFN_vkCreateDebugReportCallbackEXT CreateDebugReportCallback; | 
|  | 270 | PFN_vkDestroyDebugReportCallbackEXT DestroyDebugReportCallback; | 
|  | 271 | VkDebugReportCallbackEXT msg_callback; | 
| Mark Lobodzinski | 96b8241 | 2016-02-22 09:32:55 -0700 | [diff] [blame] | 272 | PFN_vkDebugReportMessageEXT DebugReportMessage; | 
| Courtney Goeltzenleuchter | 18061cd | 2015-06-29 15:39:26 -0600 | [diff] [blame] | 273 |  | 
| Tony Barbour | 4a6692d | 2015-10-08 13:45:45 -0600 | [diff] [blame] | 274 | float depthStencil; | 
|  | 275 | float depthIncrement; | 
|  | 276 |  | 
| Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 277 | bool quit; | 
| Mark Lobodzinski | e2d07a5 | 2015-01-29 08:55:56 -0600 | [diff] [blame] | 278 | uint32_t current_buffer; | 
| Piers Daniell | 1cf7fe1 | 2015-07-16 09:35:35 -0600 | [diff] [blame] | 279 | uint32_t queue_count; | 
| Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 280 | }; | 
|  | 281 |  | 
| Ian Elliott | e2688a5 | 2015-10-16 18:02:43 -0600 | [diff] [blame] | 282 | // Forward declaration: | 
|  | 283 | static void demo_resize(struct demo *demo); | 
|  | 284 |  | 
| Karl Schultz | 481756e | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 285 | static bool memory_type_from_properties(struct demo *demo, uint32_t typeBits, | 
|  | 286 | VkFlags requirements_mask, | 
|  | 287 | uint32_t *typeIndex) { | 
|  | 288 | // Search memtypes to find first index with those properties | 
|  | 289 | for (uint32_t i = 0; i < 32; i++) { | 
|  | 290 | if ((typeBits & 1) == 1) { | 
|  | 291 | // Type is available, does it match user properties? | 
|  | 292 | if ((demo->memory_properties.memoryTypes[i].propertyFlags & | 
|  | 293 | requirements_mask) == requirements_mask) { | 
|  | 294 | *typeIndex = i; | 
|  | 295 | return true; | 
|  | 296 | } | 
|  | 297 | } | 
|  | 298 | typeBits >>= 1; | 
|  | 299 | } | 
|  | 300 | // No memory types matched, return failure | 
|  | 301 | return false; | 
| Mark Lobodzinski | 7234629 | 2015-07-02 16:49:40 -0600 | [diff] [blame] | 302 | } | 
|  | 303 |  | 
| Karl Schultz | 481756e | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 304 | static void demo_flush_init_cmd(struct demo *demo) { | 
| Tony Barbour | 22a3086 | 2015-04-22 09:02:32 -0600 | [diff] [blame] | 305 | VkResult U_ASSERT_ONLY err; | 
| Courtney Goeltzenleuchter | bfccf41 | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 306 |  | 
| Courtney Goeltzenleuchter | 633f9c5 | 2015-04-30 10:58:33 -0600 | [diff] [blame] | 307 | if (demo->setup_cmd == VK_NULL_HANDLE) | 
| Courtney Goeltzenleuchter | bfccf41 | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 308 | return; | 
|  | 309 |  | 
| Courtney Goeltzenleuchter | 633f9c5 | 2015-04-30 10:58:33 -0600 | [diff] [blame] | 310 | err = vkEndCommandBuffer(demo->setup_cmd); | 
| Courtney Goeltzenleuchter | bfccf41 | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 311 | assert(!err); | 
|  | 312 |  | 
| Karl Schultz | 481756e | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 313 | const VkCommandBuffer cmd_bufs[] = {demo->setup_cmd}; | 
| Tony Barbour | de4124d | 2015-07-03 10:33:54 -0600 | [diff] [blame] | 314 | VkFence nullFence = {VK_NULL_HANDLE}; | 
| Karl Schultz | 481756e | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 315 | VkSubmitInfo submit_info = {.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO, | 
|  | 316 | .pNext = NULL, | 
|  | 317 | .waitSemaphoreCount = 0, | 
|  | 318 | .pWaitSemaphores = NULL, | 
|  | 319 | .pWaitDstStageMask = NULL, | 
|  | 320 | .commandBufferCount = 1, | 
|  | 321 | .pCommandBuffers = cmd_bufs, | 
|  | 322 | .signalSemaphoreCount = 0, | 
|  | 323 | .pSignalSemaphores = NULL}; | 
| Courtney Goeltzenleuchter | bfccf41 | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 324 |  | 
| Courtney Goeltzenleuchter | 3ec3162 | 2015-10-20 18:04:07 -0600 | [diff] [blame] | 325 | err = vkQueueSubmit(demo->queue, 1, &submit_info, nullFence); | 
| Courtney Goeltzenleuchter | bfccf41 | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 326 | assert(!err); | 
|  | 327 |  | 
| Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 328 | err = vkQueueWaitIdle(demo->queue); | 
| Courtney Goeltzenleuchter | bfccf41 | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 329 | assert(!err); | 
|  | 330 |  | 
| Courtney Goeltzenleuchter | 831c183 | 2015-10-23 14:21:05 -0600 | [diff] [blame] | 331 | vkFreeCommandBuffers(demo->device, demo->cmd_pool, 1, cmd_bufs); | 
| Courtney Goeltzenleuchter | 633f9c5 | 2015-04-30 10:58:33 -0600 | [diff] [blame] | 332 | demo->setup_cmd = VK_NULL_HANDLE; | 
| Courtney Goeltzenleuchter | bfccf41 | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 333 | } | 
|  | 334 |  | 
| Karl Schultz | 481756e | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 335 | static void demo_set_image_layout(struct demo *demo, VkImage image, | 
|  | 336 | VkImageAspectFlags aspectMask, | 
|  | 337 | VkImageLayout old_image_layout, | 
| Mark Lobodzinski | acae053 | 2016-02-22 08:57:55 -0700 | [diff] [blame] | 338 | VkImageLayout new_image_layout, | 
|  | 339 | VkAccessFlagBits srcAccessMask) { | 
|  | 340 |  | 
| Tony Barbour | 22a3086 | 2015-04-22 09:02:32 -0600 | [diff] [blame] | 341 | VkResult U_ASSERT_ONLY err; | 
| Courtney Goeltzenleuchter | bfccf41 | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 342 |  | 
| Courtney Goeltzenleuchter | 633f9c5 | 2015-04-30 10:58:33 -0600 | [diff] [blame] | 343 | if (demo->setup_cmd == VK_NULL_HANDLE) { | 
| Chia-I Wu | 1f85191 | 2015-10-27 18:04:07 +0800 | [diff] [blame] | 344 | const VkCommandBufferAllocateInfo cmd = { | 
| Chia-I Wu | c1f5e40 | 2015-11-10 16:21:09 +0800 | [diff] [blame] | 345 | .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO, | 
| Courtney Goeltzenleuchter | bfccf41 | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 346 | .pNext = NULL, | 
| Chia-I Wu | 1f85191 | 2015-10-27 18:04:07 +0800 | [diff] [blame] | 347 | .commandPool = demo->cmd_pool, | 
|  | 348 | .level = VK_COMMAND_BUFFER_LEVEL_PRIMARY, | 
| Jon Ashburn | a4ae48b | 2016-01-11 13:12:43 -0700 | [diff] [blame] | 349 | .commandBufferCount = 1, | 
| Courtney Goeltzenleuchter | bfccf41 | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 350 | }; | 
|  | 351 |  | 
| Chia-I Wu | 1f85191 | 2015-10-27 18:04:07 +0800 | [diff] [blame] | 352 | err = vkAllocateCommandBuffers(demo->device, &cmd, &demo->setup_cmd); | 
| Courtney Goeltzenleuchter | bfccf41 | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 353 | assert(!err); | 
|  | 354 |  | 
| Jon Ashburn | a4ae48b | 2016-01-11 13:12:43 -0700 | [diff] [blame] | 355 | VkCommandBufferInheritanceInfo cmd_buf_hinfo = { | 
|  | 356 | .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_INHERITANCE_INFO, | 
| Courtney Goeltzenleuchter | bfccf41 | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 357 | .pNext = NULL, | 
| Chia-I Wu | e420a33 | 2015-10-26 20:04:44 +0800 | [diff] [blame] | 358 | .renderPass = VK_NULL_HANDLE, | 
| Cody Northrop | 16898b0 | 2015-08-11 11:35:58 -0600 | [diff] [blame] | 359 | .subpass = 0, | 
| Chia-I Wu | e420a33 | 2015-10-26 20:04:44 +0800 | [diff] [blame] | 360 | .framebuffer = VK_NULL_HANDLE, | 
| Chia-I Wu | fdc1cd0 | 2015-11-11 10:18:12 +0800 | [diff] [blame] | 361 | .occlusionQueryEnable = VK_FALSE, | 
|  | 362 | .queryFlags = 0, | 
|  | 363 | .pipelineStatistics = 0, | 
| Courtney Goeltzenleuchter | bfccf41 | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 364 | }; | 
| Jon Ashburn | a4ae48b | 2016-01-11 13:12:43 -0700 | [diff] [blame] | 365 | VkCommandBufferBeginInfo cmd_buf_info = { | 
|  | 366 | .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO, | 
|  | 367 | .pNext = NULL, | 
|  | 368 | .flags = 0, | 
|  | 369 | .pInheritanceInfo = &cmd_buf_hinfo, | 
|  | 370 | }; | 
| Courtney Goeltzenleuchter | 633f9c5 | 2015-04-30 10:58:33 -0600 | [diff] [blame] | 371 | err = vkBeginCommandBuffer(demo->setup_cmd, &cmd_buf_info); | 
| Tony Barbour | c109827 | 2015-10-23 10:53:30 -0600 | [diff] [blame] | 372 | assert(!err); | 
| Courtney Goeltzenleuchter | bfccf41 | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 373 | } | 
|  | 374 |  | 
| Courtney Goeltzenleuchter | 382489d | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 375 | VkImageMemoryBarrier image_memory_barrier = { | 
| Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 376 | .sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER, | 
| Courtney Goeltzenleuchter | bfccf41 | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 377 | .pNext = NULL, | 
| Mark Lobodzinski | acae053 | 2016-02-22 08:57:55 -0700 | [diff] [blame] | 378 | .srcAccessMask = srcAccessMask, | 
| Chia-I Wu | 989de84 | 2015-10-27 19:54:37 +0800 | [diff] [blame] | 379 | .dstAccessMask = 0, | 
| Courtney Goeltzenleuchter | bfccf41 | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 380 | .oldLayout = old_image_layout, | 
|  | 381 | .newLayout = new_image_layout, | 
|  | 382 | .image = image, | 
| Karl Schultz | 481756e | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 383 | .subresourceRange = {aspectMask, 0, 1, 0, 1}}; | 
| Courtney Goeltzenleuchter | bfccf41 | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 384 |  | 
| Chia-I Wu | 1f85191 | 2015-10-27 18:04:07 +0800 | [diff] [blame] | 385 | if (new_image_layout == VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL) { | 
| Courtney Goeltzenleuchter | bfccf41 | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 386 | /* Make sure anything that was copying from this image has completed */ | 
| Chia-I Wu | 989de84 | 2015-10-27 19:54:37 +0800 | [diff] [blame] | 387 | image_memory_barrier.dstAccessMask = VK_ACCESS_TRANSFER_READ_BIT; | 
| Courtney Goeltzenleuchter | bfccf41 | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 388 | } | 
|  | 389 |  | 
| Tony Barbour | b7c243a | 2015-11-11 16:15:54 -0700 | [diff] [blame] | 390 | if (new_image_layout == VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL) { | 
| Karl Schultz | 481756e | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 391 | image_memory_barrier.dstAccessMask = | 
|  | 392 | VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT; | 
|  | 393 | } | 
| Tony Barbour | b7c243a | 2015-11-11 16:15:54 -0700 | [diff] [blame] | 394 |  | 
|  | 395 | if (new_image_layout == VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL) { | 
| Karl Schultz | 481756e | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 396 | image_memory_barrier.dstAccessMask = | 
|  | 397 | VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT; | 
| Tony Barbour | b7c243a | 2015-11-11 16:15:54 -0700 | [diff] [blame] | 398 | } | 
|  | 399 |  | 
| Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 400 | if (new_image_layout == VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL) { | 
| Courtney Goeltzenleuchter | bfccf41 | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 401 | /* Make sure any Copy or CPU writes to image are flushed */ | 
| Karl Schultz | 481756e | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 402 | image_memory_barrier.dstAccessMask = | 
|  | 403 | VK_ACCESS_SHADER_READ_BIT | VK_ACCESS_INPUT_ATTACHMENT_READ_BIT; | 
| Courtney Goeltzenleuchter | bfccf41 | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 404 | } | 
|  | 405 |  | 
| Courtney Goeltzenleuchter | 382489d | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 406 | VkImageMemoryBarrier *pmemory_barrier = &image_memory_barrier; | 
| Courtney Goeltzenleuchter | bfccf41 | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 407 |  | 
| Tony Barbour | c2e987e | 2015-06-29 16:20:35 -0600 | [diff] [blame] | 408 | VkPipelineStageFlags src_stages = VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT; | 
|  | 409 | VkPipelineStageFlags dest_stages = VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT; | 
| Courtney Goeltzenleuchter | bfccf41 | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 410 |  | 
| Karl Schultz | 481756e | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 411 | vkCmdPipelineBarrier(demo->setup_cmd, src_stages, dest_stages, 0, 0, NULL, | 
|  | 412 | 0, NULL, 1, pmemory_barrier); | 
| Courtney Goeltzenleuchter | bfccf41 | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 413 | } | 
|  | 414 |  | 
| Karl Schultz | 481756e | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 415 | static void demo_draw_build_cmd(struct demo *demo) { | 
|  | 416 | const VkCommandBufferInheritanceInfo cmd_buf_hinfo = { | 
|  | 417 | .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_INHERITANCE_INFO, | 
|  | 418 | .pNext = NULL, | 
|  | 419 | .renderPass = VK_NULL_HANDLE, | 
|  | 420 | .subpass = 0, | 
|  | 421 | .framebuffer = VK_NULL_HANDLE, | 
|  | 422 | .occlusionQueryEnable = VK_FALSE, | 
|  | 423 | .queryFlags = 0, | 
|  | 424 | .pipelineStatistics = 0, | 
|  | 425 | }; | 
| Chia-I Wu | 1f85191 | 2015-10-27 18:04:07 +0800 | [diff] [blame] | 426 | const VkCommandBufferBeginInfo cmd_buf_info = { | 
|  | 427 | .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO, | 
| Courtney Goeltzenleuchter | e3b0f3a | 2015-04-03 15:25:24 -0600 | [diff] [blame] | 428 | .pNext = NULL, | 
| Courtney Goeltzenleuchter | a32436b | 2015-10-21 18:11:04 -0600 | [diff] [blame] | 429 | .flags = 0, | 
| Jon Ashburn | a4ae48b | 2016-01-11 13:12:43 -0700 | [diff] [blame] | 430 | .pInheritanceInfo = &cmd_buf_hinfo, | 
| Jon Ashburn | 53d27af | 2014-12-31 17:08:35 -0700 | [diff] [blame] | 431 | }; | 
| Chia-I Wu | c278df8 | 2015-07-07 11:50:03 +0800 | [diff] [blame] | 432 | const VkClearValue clear_values[2] = { | 
| Karl Schultz | 481756e | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 433 | [0] = {.color.float32 = {0.2f, 0.2f, 0.2f, 0.2f}}, | 
|  | 434 | [1] = {.depthStencil = {demo->depthStencil, 0}}, | 
| Chia-I Wu | c278df8 | 2015-07-07 11:50:03 +0800 | [diff] [blame] | 435 | }; | 
|  | 436 | const VkRenderPassBeginInfo rp_begin = { | 
|  | 437 | .sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO, | 
|  | 438 | .pNext = NULL, | 
| Chia-I Wu | 76cd422 | 2015-07-08 13:34:24 +0800 | [diff] [blame] | 439 | .renderPass = demo->render_pass, | 
|  | 440 | .framebuffer = demo->framebuffers[demo->current_buffer], | 
| Chia-I Wu | c278df8 | 2015-07-07 11:50:03 +0800 | [diff] [blame] | 441 | .renderArea.offset.x = 0, | 
|  | 442 | .renderArea.offset.y = 0, | 
|  | 443 | .renderArea.extent.width = demo->width, | 
|  | 444 | .renderArea.extent.height = demo->height, | 
| Cody Northrop | c332eef | 2015-08-04 11:51:03 -0600 | [diff] [blame] | 445 | .clearValueCount = 2, | 
|  | 446 | .pClearValues = clear_values, | 
| Jon Ashburn | 3325d6b | 2015-01-02 18:24:05 -0700 | [diff] [blame] | 447 | }; | 
| Chia-I Wu | 76cd422 | 2015-07-08 13:34:24 +0800 | [diff] [blame] | 448 | VkResult U_ASSERT_ONLY err; | 
| Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 449 |  | 
| Courtney Goeltzenleuchter | 633f9c5 | 2015-04-30 10:58:33 -0600 | [diff] [blame] | 450 | err = vkBeginCommandBuffer(demo->draw_cmd, &cmd_buf_info); | 
| Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 451 | assert(!err); | 
|  | 452 |  | 
| Chia-I Wu | c51b121 | 2015-10-27 19:25:11 +0800 | [diff] [blame] | 453 | vkCmdBeginRenderPass(demo->draw_cmd, &rp_begin, VK_SUBPASS_CONTENTS_INLINE); | 
| Courtney Goeltzenleuchter | 633f9c5 | 2015-04-30 10:58:33 -0600 | [diff] [blame] | 454 | vkCmdBindPipeline(demo->draw_cmd, VK_PIPELINE_BIND_POINT_GRAPHICS, | 
| Karl Schultz | 481756e | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 455 | demo->pipeline); | 
|  | 456 | vkCmdBindDescriptorSets(demo->draw_cmd, VK_PIPELINE_BIND_POINT_GRAPHICS, | 
|  | 457 | demo->pipeline_layout, 0, 1, &demo->desc_set, 0, | 
|  | 458 | NULL); | 
| Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 459 |  | 
| Courtney Goeltzenleuchter | 09772bb | 2015-09-17 15:06:17 -0600 | [diff] [blame] | 460 | VkViewport viewport; | 
|  | 461 | memset(&viewport, 0, sizeof(viewport)); | 
| Karl Schultz | 481756e | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 462 | viewport.height = (float)demo->height; | 
|  | 463 | viewport.width = (float)demo->width; | 
|  | 464 | viewport.minDepth = (float)0.0f; | 
|  | 465 | viewport.maxDepth = (float)1.0f; | 
| Jon Ashburn | f251652 | 2015-12-30 14:06:55 -0700 | [diff] [blame] | 466 | vkCmdSetViewport(demo->draw_cmd, 0, 1, &viewport); | 
| Courtney Goeltzenleuchter | 09772bb | 2015-09-17 15:06:17 -0600 | [diff] [blame] | 467 |  | 
|  | 468 | VkRect2D scissor; | 
|  | 469 | memset(&scissor, 0, sizeof(scissor)); | 
|  | 470 | scissor.extent.width = demo->width; | 
|  | 471 | scissor.extent.height = demo->height; | 
|  | 472 | scissor.offset.x = 0; | 
|  | 473 | scissor.offset.y = 0; | 
| Jon Ashburn | f251652 | 2015-12-30 14:06:55 -0700 | [diff] [blame] | 474 | vkCmdSetScissor(demo->draw_cmd, 0, 1, &scissor); | 
| Courtney Goeltzenleuchter | 09772bb | 2015-09-17 15:06:17 -0600 | [diff] [blame] | 475 |  | 
| Tony Barbour | 8205d90 | 2015-04-16 15:59:00 -0600 | [diff] [blame] | 476 | VkDeviceSize offsets[1] = {0}; | 
| Karl Schultz | 481756e | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 477 | vkCmdBindVertexBuffers(demo->draw_cmd, VERTEX_BUFFER_BIND_ID, 1, | 
|  | 478 | &demo->vertices.buf, offsets); | 
| Chia-I Wu | 3b04af5 | 2014-11-08 10:48:20 +0800 | [diff] [blame] | 479 |  | 
| Courtney Goeltzenleuchter | 4ff11cc | 2015-09-23 12:31:50 -0600 | [diff] [blame] | 480 | vkCmdDraw(demo->draw_cmd, 3, 1, 0, 0); | 
| Chia-I Wu | 88eaa3b | 2015-06-26 15:34:39 +0800 | [diff] [blame] | 481 | vkCmdEndRenderPass(demo->draw_cmd); | 
| Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 482 |  | 
| Tony Barbour | 40292ae | 2015-10-21 10:14:48 -0600 | [diff] [blame] | 483 | VkImageMemoryBarrier prePresentBarrier = { | 
|  | 484 | .sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER, | 
|  | 485 | .pNext = NULL, | 
| Chia-I Wu | 989de84 | 2015-10-27 19:54:37 +0800 | [diff] [blame] | 486 | .srcAccessMask = VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT, | 
| Tony Barbour | 0313c5b | 2016-01-05 09:59:05 -0700 | [diff] [blame] | 487 | .dstAccessMask = VK_ACCESS_MEMORY_READ_BIT, | 
| Tony Barbour | 40292ae | 2015-10-21 10:14:48 -0600 | [diff] [blame] | 488 | .oldLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL, | 
| Ian Elliott | 64070a8 | 2015-11-17 17:29:40 -0700 | [diff] [blame] | 489 | .newLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR, | 
| Tony Barbour | 40292ae | 2015-10-21 10:14:48 -0600 | [diff] [blame] | 490 | .srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED, | 
| Chia-I Wu | 1f85191 | 2015-10-27 18:04:07 +0800 | [diff] [blame] | 491 | .dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED, | 
| Karl Schultz | 481756e | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 492 | .subresourceRange = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1}}; | 
| Tony Barbour | 40292ae | 2015-10-21 10:14:48 -0600 | [diff] [blame] | 493 |  | 
|  | 494 | prePresentBarrier.image = demo->buffers[demo->current_buffer].image; | 
|  | 495 | VkImageMemoryBarrier *pmemory_barrier = &prePresentBarrier; | 
| Karl Schultz | 481756e | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 496 | vkCmdPipelineBarrier(demo->draw_cmd, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT, | 
|  | 497 | VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT, 0, 0, NULL, 0, | 
|  | 498 | NULL, 1, pmemory_barrier); | 
| Tony Barbour | 40292ae | 2015-10-21 10:14:48 -0600 | [diff] [blame] | 499 |  | 
| Courtney Goeltzenleuchter | 633f9c5 | 2015-04-30 10:58:33 -0600 | [diff] [blame] | 500 | err = vkEndCommandBuffer(demo->draw_cmd); | 
| Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 501 | assert(!err); | 
|  | 502 | } | 
|  | 503 |  | 
| Karl Schultz | 481756e | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 504 | static void demo_draw(struct demo *demo) { | 
| Tony Barbour | 22a3086 | 2015-04-22 09:02:32 -0600 | [diff] [blame] | 505 | VkResult U_ASSERT_ONLY err; | 
| Ian Elliott | e36b208 | 2015-07-06 14:27:58 -0600 | [diff] [blame] | 506 | VkSemaphore presentCompleteSemaphore; | 
|  | 507 | VkSemaphoreCreateInfo presentCompleteSemaphoreCreateInfo = { | 
|  | 508 | .sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO, | 
|  | 509 | .pNext = NULL, | 
| Mark Lobodzinski | 2a25307 | 2015-10-08 10:44:07 -0600 | [diff] [blame] | 510 | .flags = 0, | 
| Ian Elliott | e36b208 | 2015-07-06 14:27:58 -0600 | [diff] [blame] | 511 | }; | 
| Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 512 |  | 
| Karl Schultz | 481756e | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 513 | err = vkCreateSemaphore(demo->device, &presentCompleteSemaphoreCreateInfo, | 
|  | 514 | NULL, &presentCompleteSemaphore); | 
| Ian Elliott | e36b208 | 2015-07-06 14:27:58 -0600 | [diff] [blame] | 515 | assert(!err); | 
|  | 516 |  | 
|  | 517 | // Get the index of the next available swapchain image: | 
| Karl Schultz | 481756e | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 518 | err = demo->fpAcquireNextImageKHR(demo->device, demo->swapchain, UINT64_MAX, | 
| Ian Elliott | e36b208 | 2015-07-06 14:27:58 -0600 | [diff] [blame] | 519 | presentCompleteSemaphore, | 
| Karl Schultz | 481756e | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 520 | (VkFence)0, // TODO: Show use of fence | 
| Ian Elliott | e36b208 | 2015-07-06 14:27:58 -0600 | [diff] [blame] | 521 | &demo->current_buffer); | 
| Ian Elliott | e2688a5 | 2015-10-16 18:02:43 -0600 | [diff] [blame] | 522 | if (err == VK_ERROR_OUT_OF_DATE_KHR) { | 
|  | 523 | // demo->swapchain is out of date (e.g. the window was resized) and | 
|  | 524 | // must be recreated: | 
|  | 525 | demo_resize(demo); | 
|  | 526 | demo_draw(demo); | 
| Chia-I Wu | 69f4012 | 2015-10-26 21:10:41 +0800 | [diff] [blame] | 527 | vkDestroySemaphore(demo->device, presentCompleteSemaphore, NULL); | 
| Ian Elliott | e2688a5 | 2015-10-16 18:02:43 -0600 | [diff] [blame] | 528 | return; | 
|  | 529 | } else if (err == VK_SUBOPTIMAL_KHR) { | 
|  | 530 | // demo->swapchain is not as optimal as it could be, but the platform's | 
|  | 531 | // presentation engine will still present the image correctly. | 
|  | 532 | } else { | 
|  | 533 | assert(!err); | 
|  | 534 | } | 
| Ian Elliott | e36b208 | 2015-07-06 14:27:58 -0600 | [diff] [blame] | 535 |  | 
| Tony Barbour | 40292ae | 2015-10-21 10:14:48 -0600 | [diff] [blame] | 536 | // Assume the command buffer has been run on current_buffer before so | 
|  | 537 | // we need to set the image layout back to COLOR_ATTACHMENT_OPTIMAL | 
|  | 538 | demo_set_image_layout(demo, demo->buffers[demo->current_buffer].image, | 
| Karl Schultz | 481756e | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 539 | VK_IMAGE_ASPECT_COLOR_BIT, | 
|  | 540 | VK_IMAGE_LAYOUT_PRESENT_SRC_KHR, | 
| Mark Lobodzinski | acae053 | 2016-02-22 08:57:55 -0700 | [diff] [blame] | 541 | VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL, | 
|  | 542 | 0); | 
| Tony Barbour | 40292ae | 2015-10-21 10:14:48 -0600 | [diff] [blame] | 543 | demo_flush_init_cmd(demo); | 
|  | 544 |  | 
| Courtney Goeltzenleuchter | 3ec3162 | 2015-10-20 18:04:07 -0600 | [diff] [blame] | 545 | // Wait for the present complete semaphore to be signaled to ensure | 
|  | 546 | // that the image won't be rendered to until the presentation | 
|  | 547 | // engine has fully released ownership to the application, and it is | 
|  | 548 | // okay to render to the image. | 
|  | 549 |  | 
| Karl Schultz | 481756e | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 550 | // FIXME/TODO: DEAL WITH VK_IMAGE_LAYOUT_PRESENT_SRC_KHR | 
| Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 551 | demo_draw_build_cmd(demo); | 
| Chia-I Wu | e420a33 | 2015-10-26 20:04:44 +0800 | [diff] [blame] | 552 | VkFence nullFence = VK_NULL_HANDLE; | 
| Karl Schultz | 481756e | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 553 | VkPipelineStageFlags pipe_stage_flags = | 
|  | 554 | VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT; | 
|  | 555 | VkSubmitInfo submit_info = {.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO, | 
|  | 556 | .pNext = NULL, | 
|  | 557 | .waitSemaphoreCount = 1, | 
|  | 558 | .pWaitSemaphores = &presentCompleteSemaphore, | 
|  | 559 | .pWaitDstStageMask = &pipe_stage_flags, | 
|  | 560 | .commandBufferCount = 1, | 
|  | 561 | .pCommandBuffers = &demo->draw_cmd, | 
|  | 562 | .signalSemaphoreCount = 0, | 
|  | 563 | .pSignalSemaphores = NULL}; | 
| Courtney Goeltzenleuchter | 3ec3162 | 2015-10-20 18:04:07 -0600 | [diff] [blame] | 564 |  | 
|  | 565 | err = vkQueueSubmit(demo->queue, 1, &submit_info, nullFence); | 
| Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 566 | assert(!err); | 
|  | 567 |  | 
| Ian Elliott | 338dedb | 2015-08-21 15:09:33 -0600 | [diff] [blame] | 568 | VkPresentInfoKHR present = { | 
|  | 569 | .sType = VK_STRUCTURE_TYPE_PRESENT_INFO_KHR, | 
| Ian Elliott | e36b208 | 2015-07-06 14:27:58 -0600 | [diff] [blame] | 570 | .pNext = NULL, | 
| Ian Elliott | 338dedb | 2015-08-21 15:09:33 -0600 | [diff] [blame] | 571 | .swapchainCount = 1, | 
| Ian Elliott | b5fad79 | 2015-11-20 11:55:46 -0700 | [diff] [blame] | 572 | .pSwapchains = &demo->swapchain, | 
|  | 573 | .pImageIndices = &demo->current_buffer, | 
| Ian Elliott | e36b208 | 2015-07-06 14:27:58 -0600 | [diff] [blame] | 574 | }; | 
|  | 575 |  | 
| Karl Schultz | 481756e | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 576 | // TBD/TODO: SHOULD THE "present" PARAMETER BE "const" IN THE HEADER? | 
| Ian Elliott | 338dedb | 2015-08-21 15:09:33 -0600 | [diff] [blame] | 577 | err = demo->fpQueuePresentKHR(demo->queue, &present); | 
| Ian Elliott | e2688a5 | 2015-10-16 18:02:43 -0600 | [diff] [blame] | 578 | if (err == VK_ERROR_OUT_OF_DATE_KHR) { | 
|  | 579 | // demo->swapchain is out of date (e.g. the window was resized) and | 
|  | 580 | // must be recreated: | 
|  | 581 | demo_resize(demo); | 
|  | 582 | } else if (err == VK_SUBOPTIMAL_KHR) { | 
|  | 583 | // demo->swapchain is not as optimal as it could be, but the platform's | 
|  | 584 | // presentation engine will still present the image correctly. | 
|  | 585 | } else { | 
|  | 586 | assert(!err); | 
|  | 587 | } | 
| Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 588 |  | 
| Chia-I Wu | 5b66aa5 | 2015-04-16 22:02:10 +0800 | [diff] [blame] | 589 | err = vkQueueWaitIdle(demo->queue); | 
|  | 590 | assert(err == VK_SUCCESS); | 
| Mike Stroyan | e187cc4 | 2015-08-28 10:58:06 -0600 | [diff] [blame] | 591 |  | 
| Chia-I Wu | 69f4012 | 2015-10-26 21:10:41 +0800 | [diff] [blame] | 592 | vkDestroySemaphore(demo->device, presentCompleteSemaphore, NULL); | 
| Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 593 | } | 
|  | 594 |  | 
| Karl Schultz | 481756e | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 595 | static void demo_prepare_buffers(struct demo *demo) { | 
| Ian Elliott | e36b208 | 2015-07-06 14:27:58 -0600 | [diff] [blame] | 596 | VkResult U_ASSERT_ONLY err; | 
| Ian Elliott | e2688a5 | 2015-10-16 18:02:43 -0600 | [diff] [blame] | 597 | VkSwapchainKHR oldSwapchain = demo->swapchain; | 
| Ian Elliott | e36b208 | 2015-07-06 14:27:58 -0600 | [diff] [blame] | 598 |  | 
| Ian Elliott | b5fad79 | 2015-11-20 11:55:46 -0700 | [diff] [blame] | 599 | // Check the surface capabilities and formats | 
|  | 600 | VkSurfaceCapabilitiesKHR surfCapabilities; | 
| Karl Schultz | 481756e | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 601 | err = demo->fpGetPhysicalDeviceSurfaceCapabilitiesKHR( | 
|  | 602 | demo->gpu, demo->surface, &surfCapabilities); | 
| Ian Elliott | e36b208 | 2015-07-06 14:27:58 -0600 | [diff] [blame] | 603 | assert(!err); | 
|  | 604 |  | 
| Ian Elliott | 7fe115d | 2015-08-07 15:56:59 -0600 | [diff] [blame] | 605 | uint32_t presentModeCount; | 
| Karl Schultz | 481756e | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 606 | err = demo->fpGetPhysicalDeviceSurfacePresentModesKHR( | 
|  | 607 | demo->gpu, demo->surface, &presentModeCount, NULL); | 
| Ian Elliott | e36b208 | 2015-07-06 14:27:58 -0600 | [diff] [blame] | 608 | assert(!err); | 
| Ian Elliott | 338dedb | 2015-08-21 15:09:33 -0600 | [diff] [blame] | 609 | VkPresentModeKHR *presentModes = | 
|  | 610 | (VkPresentModeKHR *)malloc(presentModeCount * sizeof(VkPresentModeKHR)); | 
| Ian Elliott | 7fe115d | 2015-08-07 15:56:59 -0600 | [diff] [blame] | 611 | assert(presentModes); | 
| Karl Schultz | 481756e | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 612 | err = demo->fpGetPhysicalDeviceSurfacePresentModesKHR( | 
|  | 613 | demo->gpu, demo->surface, &presentModeCount, presentModes); | 
| Ian Elliott | e36b208 | 2015-07-06 14:27:58 -0600 | [diff] [blame] | 614 | assert(!err); | 
|  | 615 |  | 
| Ian Elliott | 338dedb | 2015-08-21 15:09:33 -0600 | [diff] [blame] | 616 | VkExtent2D swapchainExtent; | 
| Ian Elliott | e36b208 | 2015-07-06 14:27:58 -0600 | [diff] [blame] | 617 | // width and height are either both -1, or both not -1. | 
| Karl Schultz | 481756e | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 618 | if (surfCapabilities.currentExtent.width == (uint32_t)-1) { | 
| Ian Elliott | e36b208 | 2015-07-06 14:27:58 -0600 | [diff] [blame] | 619 | // If the surface size is undefined, the size is set to | 
|  | 620 | // the size of the images requested. | 
| Ian Elliott | 338dedb | 2015-08-21 15:09:33 -0600 | [diff] [blame] | 621 | swapchainExtent.width = demo->width; | 
|  | 622 | swapchainExtent.height = demo->height; | 
| Karl Schultz | 481756e | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 623 | } else { | 
| Ian Elliott | e36b208 | 2015-07-06 14:27:58 -0600 | [diff] [blame] | 624 | // If the surface size is defined, the swap chain size must match | 
| Ian Elliott | b5fad79 | 2015-11-20 11:55:46 -0700 | [diff] [blame] | 625 | swapchainExtent = surfCapabilities.currentExtent; | 
|  | 626 | demo->width = surfCapabilities.currentExtent.width; | 
|  | 627 | demo->height = surfCapabilities.currentExtent.height; | 
| Ian Elliott | e36b208 | 2015-07-06 14:27:58 -0600 | [diff] [blame] | 628 | } | 
|  | 629 |  | 
| Ian Elliott | 338dedb | 2015-08-21 15:09:33 -0600 | [diff] [blame] | 630 | VkPresentModeKHR swapchainPresentMode = VK_PRESENT_MODE_FIFO_KHR; | 
| Ian Elliott | e36b208 | 2015-07-06 14:27:58 -0600 | [diff] [blame] | 631 |  | 
|  | 632 | // Determine the number of VkImage's to use in the swap chain (we desire to | 
|  | 633 | // own only 1 image at a time, besides the images being displayed and | 
|  | 634 | // queued for display): | 
| Karl Schultz | 481756e | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 635 | uint32_t desiredNumberOfSwapchainImages = | 
|  | 636 | surfCapabilities.minImageCount + 1; | 
| Ian Elliott | b5fad79 | 2015-11-20 11:55:46 -0700 | [diff] [blame] | 637 | if ((surfCapabilities.maxImageCount > 0) && | 
| Karl Schultz | 481756e | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 638 | (desiredNumberOfSwapchainImages > surfCapabilities.maxImageCount)) { | 
| Ian Elliott | e36b208 | 2015-07-06 14:27:58 -0600 | [diff] [blame] | 639 | // Application must settle for fewer images than desired: | 
| Ian Elliott | b5fad79 | 2015-11-20 11:55:46 -0700 | [diff] [blame] | 640 | desiredNumberOfSwapchainImages = surfCapabilities.maxImageCount; | 
| Ian Elliott | e36b208 | 2015-07-06 14:27:58 -0600 | [diff] [blame] | 641 | } | 
|  | 642 |  | 
| Ian Elliott | b5fad79 | 2015-11-20 11:55:46 -0700 | [diff] [blame] | 643 | VkSurfaceTransformFlagsKHR preTransform; | 
| Karl Schultz | 481756e | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 644 | if (surfCapabilities.supportedTransforms & | 
|  | 645 | VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR) { | 
| Ian Elliott | da44dfe | 2015-12-11 15:52:12 -0700 | [diff] [blame] | 646 | preTransform = VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR; | 
| Ian Elliott | e36b208 | 2015-07-06 14:27:58 -0600 | [diff] [blame] | 647 | } else { | 
| Ian Elliott | b5fad79 | 2015-11-20 11:55:46 -0700 | [diff] [blame] | 648 | preTransform = surfCapabilities.currentTransform; | 
| Ian Elliott | e36b208 | 2015-07-06 14:27:58 -0600 | [diff] [blame] | 649 | } | 
|  | 650 |  | 
| Ian Elliott | e2688a5 | 2015-10-16 18:02:43 -0600 | [diff] [blame] | 651 | const VkSwapchainCreateInfoKHR swapchain = { | 
| Ian Elliott | 434d222 | 2015-09-22 10:20:23 -0600 | [diff] [blame] | 652 | .sType = VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR, | 
| Chia-I Wu | 5b66aa5 | 2015-04-16 22:02:10 +0800 | [diff] [blame] | 653 | .pNext = NULL, | 
| Ian Elliott | b5fad79 | 2015-11-20 11:55:46 -0700 | [diff] [blame] | 654 | .surface = demo->surface, | 
| Ian Elliott | 338dedb | 2015-08-21 15:09:33 -0600 | [diff] [blame] | 655 | .minImageCount = desiredNumberOfSwapchainImages, | 
| Chia-I Wu | 5b66aa5 | 2015-04-16 22:02:10 +0800 | [diff] [blame] | 656 | .imageFormat = demo->format, | 
| Ian Elliott | 7fe115d | 2015-08-07 15:56:59 -0600 | [diff] [blame] | 657 | .imageColorSpace = demo->color_space, | 
| Karl Schultz | 481756e | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 658 | .imageExtent = | 
|  | 659 | { | 
|  | 660 | .width = swapchainExtent.width, .height = swapchainExtent.height, | 
|  | 661 | }, | 
| Ian Elliott | b5fad79 | 2015-11-20 11:55:46 -0700 | [diff] [blame] | 662 | .imageUsage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT, | 
| Ian Elliott | e36b208 | 2015-07-06 14:27:58 -0600 | [diff] [blame] | 663 | .preTransform = preTransform, | 
| Ian Elliott | 3f07ab6 | 2015-12-28 15:25:33 -0700 | [diff] [blame] | 664 | .compositeAlpha = VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR, | 
| Ian Elliott | b5fad79 | 2015-11-20 11:55:46 -0700 | [diff] [blame] | 665 | .imageArrayLayers = 1, | 
|  | 666 | .imageSharingMode = VK_SHARING_MODE_EXCLUSIVE, | 
|  | 667 | .queueFamilyIndexCount = 0, | 
| Ian Elliott | 7fe115d | 2015-08-07 15:56:59 -0600 | [diff] [blame] | 668 | .pQueueFamilyIndices = NULL, | 
| Ian Elliott | 338dedb | 2015-08-21 15:09:33 -0600 | [diff] [blame] | 669 | .presentMode = swapchainPresentMode, | 
| Ian Elliott | e2688a5 | 2015-10-16 18:02:43 -0600 | [diff] [blame] | 670 | .oldSwapchain = oldSwapchain, | 
| Ian Elliott | e36b208 | 2015-07-06 14:27:58 -0600 | [diff] [blame] | 671 | .clipped = true, | 
| Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 672 | }; | 
| Mark Lobodzinski | e2d07a5 | 2015-01-29 08:55:56 -0600 | [diff] [blame] | 673 | uint32_t i; | 
| Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 674 |  | 
| Karl Schultz | 481756e | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 675 | err = demo->fpCreateSwapchainKHR(demo->device, &swapchain, NULL, | 
|  | 676 | &demo->swapchain); | 
| Chia-I Wu | 5b66aa5 | 2015-04-16 22:02:10 +0800 | [diff] [blame] | 677 | assert(!err); | 
|  | 678 |  | 
| Ian Elliott | e2688a5 | 2015-10-16 18:02:43 -0600 | [diff] [blame] | 679 | // If we just re-created an existing swapchain, we should destroy the old | 
|  | 680 | // swapchain at this point. | 
|  | 681 | // Note: destroying the swapchain also cleans up all its associated | 
|  | 682 | // presentable images once the platform is done with them. | 
| Chia-I Wu | e420a33 | 2015-10-26 20:04:44 +0800 | [diff] [blame] | 683 | if (oldSwapchain != VK_NULL_HANDLE) { | 
| Ian Elliott | b5fad79 | 2015-11-20 11:55:46 -0700 | [diff] [blame] | 684 | demo->fpDestroySwapchainKHR(demo->device, oldSwapchain, NULL); | 
| Ian Elliott | e2688a5 | 2015-10-16 18:02:43 -0600 | [diff] [blame] | 685 | } | 
|  | 686 |  | 
|  | 687 | err = demo->fpGetSwapchainImagesKHR(demo->device, demo->swapchain, | 
| Ian Elliott | 338dedb | 2015-08-21 15:09:33 -0600 | [diff] [blame] | 688 | &demo->swapchainImageCount, NULL); | 
| Ian Elliott | e36b208 | 2015-07-06 14:27:58 -0600 | [diff] [blame] | 689 | assert(!err); | 
| Chia-I Wu | 5b66aa5 | 2015-04-16 22:02:10 +0800 | [diff] [blame] | 690 |  | 
| Karl Schultz | 481756e | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 691 | VkImage *swapchainImages = | 
|  | 692 | (VkImage *)malloc(demo->swapchainImageCount * sizeof(VkImage)); | 
| Ian Elliott | 338dedb | 2015-08-21 15:09:33 -0600 | [diff] [blame] | 693 | assert(swapchainImages); | 
| Ian Elliott | e2688a5 | 2015-10-16 18:02:43 -0600 | [diff] [blame] | 694 | err = demo->fpGetSwapchainImagesKHR(demo->device, demo->swapchain, | 
| Ian Elliott | 338dedb | 2015-08-21 15:09:33 -0600 | [diff] [blame] | 695 | &demo->swapchainImageCount, | 
|  | 696 | swapchainImages); | 
| Ian Elliott | e36b208 | 2015-07-06 14:27:58 -0600 | [diff] [blame] | 697 | assert(!err); | 
|  | 698 |  | 
| Karl Schultz | 481756e | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 699 | demo->buffers = (SwapchainBuffers *)malloc(sizeof(SwapchainBuffers) * | 
|  | 700 | demo->swapchainImageCount); | 
| Ian Elliott | e36b208 | 2015-07-06 14:27:58 -0600 | [diff] [blame] | 701 | assert(demo->buffers); | 
|  | 702 |  | 
| Ian Elliott | 338dedb | 2015-08-21 15:09:33 -0600 | [diff] [blame] | 703 | for (i = 0; i < demo->swapchainImageCount; i++) { | 
| Courtney Goeltzenleuchter | 1856d6f | 2015-09-01 17:30:39 -0600 | [diff] [blame] | 704 | VkImageViewCreateInfo color_attachment_view = { | 
|  | 705 | .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO, | 
| Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 706 | .pNext = NULL, | 
|  | 707 | .format = demo->format, | 
| Karl Schultz | 481756e | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 708 | .components = | 
|  | 709 | { | 
|  | 710 | .r = VK_COMPONENT_SWIZZLE_R, | 
|  | 711 | .g = VK_COMPONENT_SWIZZLE_G, | 
|  | 712 | .b = VK_COMPONENT_SWIZZLE_B, | 
|  | 713 | .a = VK_COMPONENT_SWIZZLE_A, | 
|  | 714 | }, | 
|  | 715 | .subresourceRange = {.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT, | 
|  | 716 | .baseMipLevel = 0, | 
|  | 717 | .levelCount = 1, | 
|  | 718 | .baseArrayLayer = 0, | 
|  | 719 | .layerCount = 1}, | 
| Courtney Goeltzenleuchter | 1856d6f | 2015-09-01 17:30:39 -0600 | [diff] [blame] | 720 | .viewType = VK_IMAGE_VIEW_TYPE_2D, | 
|  | 721 | .flags = 0, | 
| Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 722 | }; | 
|  | 723 |  | 
| Ian Elliott | 338dedb | 2015-08-21 15:09:33 -0600 | [diff] [blame] | 724 | demo->buffers[i].image = swapchainImages[i]; | 
| Mark Lobodzinski | 97dcd04 | 2015-04-16 08:52:00 -0500 | [diff] [blame] | 725 |  | 
| Karl Schultz | 481756e | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 726 | // Render loop will expect image to have been used before and in | 
|  | 727 | // VK_IMAGE_LAYOUT_PRESENT_SRC_KHR | 
|  | 728 | // layout and will change to COLOR_ATTACHMENT_OPTIMAL, so init the image | 
|  | 729 | // to that state | 
|  | 730 | demo_set_image_layout( | 
|  | 731 | demo, demo->buffers[i].image, VK_IMAGE_ASPECT_COLOR_BIT, | 
| Mark Lobodzinski | acae053 | 2016-02-22 08:57:55 -0700 | [diff] [blame] | 732 | VK_IMAGE_LAYOUT_UNDEFINED, VK_IMAGE_LAYOUT_PRESENT_SRC_KHR, | 
|  | 733 | 0); | 
| Courtney Goeltzenleuchter | bfccf41 | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 734 |  | 
| Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 735 | color_attachment_view.image = demo->buffers[i].image; | 
|  | 736 |  | 
| Karl Schultz | 481756e | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 737 | err = vkCreateImageView(demo->device, &color_attachment_view, NULL, | 
|  | 738 | &demo->buffers[i].view); | 
| Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 739 | assert(!err); | 
|  | 740 | } | 
| Piers Daniell | 886be47 | 2015-02-23 16:23:13 -0700 | [diff] [blame] | 741 |  | 
|  | 742 | demo->current_buffer = 0; | 
| Karl Schultz | 481756e | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 743 |  | 
| Mark Young | 842f941 | 2016-01-25 14:43:50 -0700 | [diff] [blame] | 744 | if (NULL != presentModes) { | 
|  | 745 | free(presentModes); | 
|  | 746 | } | 
| Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 747 | } | 
|  | 748 |  | 
| Karl Schultz | 481756e | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 749 | static void demo_prepare_depth(struct demo *demo) { | 
| Tony Barbour | 8205d90 | 2015-04-16 15:59:00 -0600 | [diff] [blame] | 750 | const VkFormat depth_format = VK_FORMAT_D16_UNORM; | 
| Courtney Goeltzenleuchter | 382489d | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 751 | const VkImageCreateInfo image = { | 
| Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 752 | .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO, | 
| Chia-I Wu | 9ae87c9 | 2014-10-07 14:15:01 +0800 | [diff] [blame] | 753 | .pNext = NULL, | 
| Tony Barbour | 8205d90 | 2015-04-16 15:59:00 -0600 | [diff] [blame] | 754 | .imageType = VK_IMAGE_TYPE_2D, | 
| Chia-I Wu | 9ae87c9 | 2014-10-07 14:15:01 +0800 | [diff] [blame] | 755 | .format = depth_format, | 
| Karl Schultz | 481756e | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 756 | .extent = {demo->width, demo->height, 1}, | 
| Chia-I Wu | 9ae87c9 | 2014-10-07 14:15:01 +0800 | [diff] [blame] | 757 | .mipLevels = 1, | 
| Courtney Goeltzenleuchter | 2ebc234 | 2015-10-21 17:57:31 -0600 | [diff] [blame] | 758 | .arrayLayers = 1, | 
| Chia-I Wu | 3138d6a | 2015-10-31 00:31:16 +0800 | [diff] [blame] | 759 | .samples = VK_SAMPLE_COUNT_1_BIT, | 
| Tony Barbour | 8205d90 | 2015-04-16 15:59:00 -0600 | [diff] [blame] | 760 | .tiling = VK_IMAGE_TILING_OPTIMAL, | 
| Courtney Goeltzenleuchter | c3b8eea | 2015-09-10 14:14:11 -0600 | [diff] [blame] | 761 | .usage = VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT, | 
| Chia-I Wu | 9ae87c9 | 2014-10-07 14:15:01 +0800 | [diff] [blame] | 762 | .flags = 0, | 
|  | 763 | }; | 
| Chia-I Wu | 1f85191 | 2015-10-27 18:04:07 +0800 | [diff] [blame] | 764 | VkMemoryAllocateInfo mem_alloc = { | 
| Chia-I Wu | c1f5e40 | 2015-11-10 16:21:09 +0800 | [diff] [blame] | 765 | .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO, | 
| Mark Lobodzinski | 97dcd04 | 2015-04-16 08:52:00 -0500 | [diff] [blame] | 766 | .pNext = NULL, | 
| Chia-I Wu | 9ae87c9 | 2014-10-07 14:15:01 +0800 | [diff] [blame] | 767 | .allocationSize = 0, | 
| Mark Lobodzinski | 7234629 | 2015-07-02 16:49:40 -0600 | [diff] [blame] | 768 | .memoryTypeIndex = 0, | 
| Chia-I Wu | 9ae87c9 | 2014-10-07 14:15:01 +0800 | [diff] [blame] | 769 | }; | 
| Courtney Goeltzenleuchter | 1856d6f | 2015-09-01 17:30:39 -0600 | [diff] [blame] | 770 | VkImageViewCreateInfo view = { | 
|  | 771 | .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO, | 
| Chia-I Wu | 9ae87c9 | 2014-10-07 14:15:01 +0800 | [diff] [blame] | 772 | .pNext = NULL, | 
| Chia-I Wu | e420a33 | 2015-10-26 20:04:44 +0800 | [diff] [blame] | 773 | .image = VK_NULL_HANDLE, | 
| Piers Daniell | 1cf7fe1 | 2015-07-16 09:35:35 -0600 | [diff] [blame] | 774 | .format = depth_format, | 
| Karl Schultz | 481756e | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 775 | .subresourceRange = {.aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT, | 
|  | 776 | .baseMipLevel = 0, | 
|  | 777 | .levelCount = 1, | 
|  | 778 | .baseArrayLayer = 0, | 
|  | 779 | .layerCount = 1}, | 
| Chia-I Wu | 9ae87c9 | 2014-10-07 14:15:01 +0800 | [diff] [blame] | 780 | .flags = 0, | 
| Courtney Goeltzenleuchter | 1856d6f | 2015-09-01 17:30:39 -0600 | [diff] [blame] | 781 | .viewType = VK_IMAGE_VIEW_TYPE_2D, | 
| Chia-I Wu | 9ae87c9 | 2014-10-07 14:15:01 +0800 | [diff] [blame] | 782 | }; | 
| Jon Ashburn | a9ae383 | 2015-01-16 09:37:43 -0700 | [diff] [blame] | 783 |  | 
| Mark Lobodzinski | 2318261 | 2015-05-29 09:32:35 -0500 | [diff] [blame] | 784 | VkMemoryRequirements mem_reqs; | 
| Tony Barbour | 22a3086 | 2015-04-22 09:02:32 -0600 | [diff] [blame] | 785 | VkResult U_ASSERT_ONLY err; | 
| Courtney Goeltzenleuchter | 37a43a6 | 2015-10-22 11:03:31 -0600 | [diff] [blame] | 786 | bool U_ASSERT_ONLY pass; | 
| Chia-I Wu | 9ae87c9 | 2014-10-07 14:15:01 +0800 | [diff] [blame] | 787 |  | 
|  | 788 | demo->depth.format = depth_format; | 
|  | 789 |  | 
|  | 790 | /* create image */ | 
| Karl Schultz | 481756e | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 791 | err = vkCreateImage(demo->device, &image, NULL, &demo->depth.image); | 
| Chia-I Wu | 9ae87c9 | 2014-10-07 14:15:01 +0800 | [diff] [blame] | 792 | assert(!err); | 
|  | 793 |  | 
| Mark Lobodzinski | 7234629 | 2015-07-02 16:49:40 -0600 | [diff] [blame] | 794 | /* get memory requirements for this object */ | 
| Karl Schultz | 481756e | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 795 | vkGetImageMemoryRequirements(demo->device, demo->depth.image, &mem_reqs); | 
| Mark Lobodzinski | 7234629 | 2015-07-02 16:49:40 -0600 | [diff] [blame] | 796 |  | 
|  | 797 | /* select memory size and type */ | 
| Mark Lobodzinski | 2318261 | 2015-05-29 09:32:35 -0500 | [diff] [blame] | 798 | mem_alloc.allocationSize = mem_reqs.size; | 
| Karl Schultz | 481756e | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 799 | pass = memory_type_from_properties(demo, mem_reqs.memoryTypeBits, | 
|  | 800 | 0, /* No requirements */ | 
|  | 801 | &mem_alloc.memoryTypeIndex); | 
| Courtney Goeltzenleuchter | 37a43a6 | 2015-10-22 11:03:31 -0600 | [diff] [blame] | 802 | assert(pass); | 
| Chia-I Wu | 9ae87c9 | 2014-10-07 14:15:01 +0800 | [diff] [blame] | 803 |  | 
| Mark Lobodzinski | 2318261 | 2015-05-29 09:32:35 -0500 | [diff] [blame] | 804 | /* allocate memory */ | 
| Chia-I Wu | 1f85191 | 2015-10-27 18:04:07 +0800 | [diff] [blame] | 805 | err = vkAllocateMemory(demo->device, &mem_alloc, NULL, &demo->depth.mem); | 
| Mark Lobodzinski | 2318261 | 2015-05-29 09:32:35 -0500 | [diff] [blame] | 806 | assert(!err); | 
| Chia-I Wu | 9ae87c9 | 2014-10-07 14:15:01 +0800 | [diff] [blame] | 807 |  | 
| Mark Lobodzinski | 2318261 | 2015-05-29 09:32:35 -0500 | [diff] [blame] | 808 | /* bind memory */ | 
| Karl Schultz | 481756e | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 809 | err = | 
|  | 810 | vkBindImageMemory(demo->device, demo->depth.image, demo->depth.mem, 0); | 
| Mark Lobodzinski | 2318261 | 2015-05-29 09:32:35 -0500 | [diff] [blame] | 811 | assert(!err); | 
| Chia-I Wu | 9ae87c9 | 2014-10-07 14:15:01 +0800 | [diff] [blame] | 812 |  | 
| Karl Schultz | 481756e | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 813 | demo_set_image_layout(demo, demo->depth.image, VK_IMAGE_ASPECT_DEPTH_BIT, | 
|  | 814 | VK_IMAGE_LAYOUT_UNDEFINED, | 
| Mark Lobodzinski | acae053 | 2016-02-22 08:57:55 -0700 | [diff] [blame] | 815 | VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL, | 
|  | 816 | 0); | 
| Courtney Goeltzenleuchter | bfccf41 | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 817 |  | 
| Chia-I Wu | 9ae87c9 | 2014-10-07 14:15:01 +0800 | [diff] [blame] | 818 | /* create image view */ | 
|  | 819 | view.image = demo->depth.image; | 
| Chia-I Wu | 69f4012 | 2015-10-26 21:10:41 +0800 | [diff] [blame] | 820 | err = vkCreateImageView(demo->device, &view, NULL, &demo->depth.view); | 
| Chia-I Wu | 9ae87c9 | 2014-10-07 14:15:01 +0800 | [diff] [blame] | 821 | assert(!err); | 
| Chia-I Wu | 9ae87c9 | 2014-10-07 14:15:01 +0800 | [diff] [blame] | 822 | } | 
|  | 823 |  | 
| Karl Schultz | 481756e | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 824 | static void | 
|  | 825 | demo_prepare_texture_image(struct demo *demo, const uint32_t *tex_colors, | 
|  | 826 | struct texture_object *tex_obj, VkImageTiling tiling, | 
|  | 827 | VkImageUsageFlags usage, VkFlags required_props) { | 
| Tony Barbour | 8205d90 | 2015-04-16 15:59:00 -0600 | [diff] [blame] | 828 | const VkFormat tex_format = VK_FORMAT_B8G8R8A8_UNORM; | 
| Mark Lobodzinski | e2d07a5 | 2015-01-29 08:55:56 -0600 | [diff] [blame] | 829 | const int32_t tex_width = 2; | 
|  | 830 | const int32_t tex_height = 2; | 
| Tony Barbour | 22a3086 | 2015-04-22 09:02:32 -0600 | [diff] [blame] | 831 | VkResult U_ASSERT_ONLY err; | 
| Courtney Goeltzenleuchter | 37a43a6 | 2015-10-22 11:03:31 -0600 | [diff] [blame] | 832 | bool U_ASSERT_ONLY pass; | 
| Chia-I Wu | b043fe3 | 2014-10-06 15:30:33 +0800 | [diff] [blame] | 833 |  | 
| Courtney Goeltzenleuchter | bfccf41 | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 834 | tex_obj->tex_width = tex_width; | 
|  | 835 | tex_obj->tex_height = tex_height; | 
| Courtney Goeltzenleuchter | 372e13c | 2015-02-13 17:52:46 -0700 | [diff] [blame] | 836 |  | 
| Courtney Goeltzenleuchter | 382489d | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 837 | const VkImageCreateInfo image_create_info = { | 
| Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 838 | .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO, | 
| Courtney Goeltzenleuchter | 3226a6f | 2015-02-11 18:17:22 -0700 | [diff] [blame] | 839 | .pNext = NULL, | 
| Tony Barbour | 8205d90 | 2015-04-16 15:59:00 -0600 | [diff] [blame] | 840 | .imageType = VK_IMAGE_TYPE_2D, | 
| Courtney Goeltzenleuchter | 372e13c | 2015-02-13 17:52:46 -0700 | [diff] [blame] | 841 | .format = tex_format, | 
| Karl Schultz | 481756e | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 842 | .extent = {tex_width, tex_height, 1}, | 
| Courtney Goeltzenleuchter | 372e13c | 2015-02-13 17:52:46 -0700 | [diff] [blame] | 843 | .mipLevels = 1, | 
| Courtney Goeltzenleuchter | 2ebc234 | 2015-10-21 17:57:31 -0600 | [diff] [blame] | 844 | .arrayLayers = 1, | 
| Chia-I Wu | 3138d6a | 2015-10-31 00:31:16 +0800 | [diff] [blame] | 845 | .samples = VK_SAMPLE_COUNT_1_BIT, | 
| Courtney Goeltzenleuchter | 372e13c | 2015-02-13 17:52:46 -0700 | [diff] [blame] | 846 | .tiling = tiling, | 
| Courtney Goeltzenleuchter | cb67a32 | 2015-04-21 09:31:23 -0600 | [diff] [blame] | 847 | .usage = usage, | 
| Courtney Goeltzenleuchter | 372e13c | 2015-02-13 17:52:46 -0700 | [diff] [blame] | 848 | .flags = 0, | 
| Mark Lobodzinski | acae053 | 2016-02-22 08:57:55 -0700 | [diff] [blame] | 849 | .initialLayout = VK_IMAGE_LAYOUT_PREINITIALIZED | 
| Courtney Goeltzenleuchter | 372e13c | 2015-02-13 17:52:46 -0700 | [diff] [blame] | 850 | }; | 
| Chia-I Wu | 1f85191 | 2015-10-27 18:04:07 +0800 | [diff] [blame] | 851 | VkMemoryAllocateInfo mem_alloc = { | 
| Chia-I Wu | c1f5e40 | 2015-11-10 16:21:09 +0800 | [diff] [blame] | 852 | .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO, | 
| Mark Lobodzinski | 97dcd04 | 2015-04-16 08:52:00 -0500 | [diff] [blame] | 853 | .pNext = NULL, | 
| Courtney Goeltzenleuchter | 372e13c | 2015-02-13 17:52:46 -0700 | [diff] [blame] | 854 | .allocationSize = 0, | 
| Mark Lobodzinski | 7234629 | 2015-07-02 16:49:40 -0600 | [diff] [blame] | 855 | .memoryTypeIndex = 0, | 
| Courtney Goeltzenleuchter | 3226a6f | 2015-02-11 18:17:22 -0700 | [diff] [blame] | 856 | }; | 
|  | 857 |  | 
| Mark Lobodzinski | 2318261 | 2015-05-29 09:32:35 -0500 | [diff] [blame] | 858 | VkMemoryRequirements mem_reqs; | 
| Courtney Goeltzenleuchter | 372e13c | 2015-02-13 17:52:46 -0700 | [diff] [blame] | 859 |  | 
| Karl Schultz | 481756e | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 860 | err = | 
|  | 861 | vkCreateImage(demo->device, &image_create_info, NULL, &tex_obj->image); | 
| Courtney Goeltzenleuchter | 3226a6f | 2015-02-11 18:17:22 -0700 | [diff] [blame] | 862 | assert(!err); | 
|  | 863 |  | 
| Courtney Goeltzenleuchter | 01d2ae1 | 2015-10-20 16:40:38 -0600 | [diff] [blame] | 864 | vkGetImageMemoryRequirements(demo->device, tex_obj->image, &mem_reqs); | 
| Mark Lobodzinski | 7234629 | 2015-07-02 16:49:40 -0600 | [diff] [blame] | 865 |  | 
| Karl Schultz | 481756e | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 866 | mem_alloc.allocationSize = mem_reqs.size; | 
|  | 867 | pass = | 
|  | 868 | memory_type_from_properties(demo, mem_reqs.memoryTypeBits, | 
|  | 869 | required_props, &mem_alloc.memoryTypeIndex); | 
| Courtney Goeltzenleuchter | 37a43a6 | 2015-10-22 11:03:31 -0600 | [diff] [blame] | 870 | assert(pass); | 
| Courtney Goeltzenleuchter | 3226a6f | 2015-02-11 18:17:22 -0700 | [diff] [blame] | 871 |  | 
| Mark Lobodzinski | 2318261 | 2015-05-29 09:32:35 -0500 | [diff] [blame] | 872 | /* allocate memory */ | 
| Chia-I Wu | 1f85191 | 2015-10-27 18:04:07 +0800 | [diff] [blame] | 873 | err = vkAllocateMemory(demo->device, &mem_alloc, NULL, &tex_obj->mem); | 
| Mark Lobodzinski | 2318261 | 2015-05-29 09:32:35 -0500 | [diff] [blame] | 874 | assert(!err); | 
| Courtney Goeltzenleuchter | 3226a6f | 2015-02-11 18:17:22 -0700 | [diff] [blame] | 875 |  | 
| Mark Lobodzinski | 2318261 | 2015-05-29 09:32:35 -0500 | [diff] [blame] | 876 | /* bind memory */ | 
| Karl Schultz | 481756e | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 877 | err = vkBindImageMemory(demo->device, tex_obj->image, tex_obj->mem, 0); | 
| Mark Lobodzinski | 2318261 | 2015-05-29 09:32:35 -0500 | [diff] [blame] | 878 | assert(!err); | 
| Courtney Goeltzenleuchter | 3226a6f | 2015-02-11 18:17:22 -0700 | [diff] [blame] | 879 |  | 
| Tony Barbour | f1eceb9 | 2015-10-15 12:42:56 -0600 | [diff] [blame] | 880 | if (required_props & VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT) { | 
| Courtney Goeltzenleuchter | 382489d | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 881 | const VkImageSubresource subres = { | 
| Chia-I Wu | 195c9e1 | 2015-10-27 19:55:05 +0800 | [diff] [blame] | 882 | .aspectMask = VK_IMAGE_ASPECT_COLOR_BIT, | 
| Courtney Goeltzenleuchter | 3226a6f | 2015-02-11 18:17:22 -0700 | [diff] [blame] | 883 | .mipLevel = 0, | 
| Courtney Goeltzenleuchter | 3dee808 | 2015-09-10 16:38:41 -0600 | [diff] [blame] | 884 | .arrayLayer = 0, | 
| Courtney Goeltzenleuchter | 3226a6f | 2015-02-11 18:17:22 -0700 | [diff] [blame] | 885 | }; | 
| Courtney Goeltzenleuchter | 382489d | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 886 | VkSubresourceLayout layout; | 
| Courtney Goeltzenleuchter | 3226a6f | 2015-02-11 18:17:22 -0700 | [diff] [blame] | 887 | void *data; | 
|  | 888 | int32_t x, y; | 
|  | 889 |  | 
| Karl Schultz | 481756e | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 890 | vkGetImageSubresourceLayout(demo->device, tex_obj->image, &subres, | 
|  | 891 | &layout); | 
| Courtney Goeltzenleuchter | 3226a6f | 2015-02-11 18:17:22 -0700 | [diff] [blame] | 892 |  | 
| Karl Schultz | 481756e | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 893 | err = vkMapMemory(demo->device, tex_obj->mem, 0, | 
|  | 894 | mem_alloc.allocationSize, 0, &data); | 
| Courtney Goeltzenleuchter | 3226a6f | 2015-02-11 18:17:22 -0700 | [diff] [blame] | 895 | assert(!err); | 
|  | 896 |  | 
|  | 897 | for (y = 0; y < tex_height; y++) { | 
| Karl Schultz | 481756e | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 898 | uint32_t *row = (uint32_t *)((char *)data + layout.rowPitch * y); | 
| Courtney Goeltzenleuchter | 3226a6f | 2015-02-11 18:17:22 -0700 | [diff] [blame] | 899 | for (x = 0; x < tex_width; x++) | 
| Courtney Goeltzenleuchter | 372e13c | 2015-02-13 17:52:46 -0700 | [diff] [blame] | 900 | row[x] = tex_colors[(x & 1) ^ (y & 1)]; | 
| Courtney Goeltzenleuchter | 3226a6f | 2015-02-11 18:17:22 -0700 | [diff] [blame] | 901 | } | 
|  | 902 |  | 
| Mark Lobodzinski | 67b42b7 | 2015-09-07 13:59:43 -0600 | [diff] [blame] | 903 | vkUnmapMemory(demo->device, tex_obj->mem); | 
| Courtney Goeltzenleuchter | 372e13c | 2015-02-13 17:52:46 -0700 | [diff] [blame] | 904 | } | 
| Courtney Goeltzenleuchter | bfccf41 | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 905 |  | 
| Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 906 | tex_obj->imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL; | 
| Karl Schultz | 481756e | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 907 | demo_set_image_layout(demo, tex_obj->image, VK_IMAGE_ASPECT_COLOR_BIT, | 
| Mark Lobodzinski | acae053 | 2016-02-22 08:57:55 -0700 | [diff] [blame] | 908 | VK_IMAGE_LAYOUT_PREINITIALIZED, tex_obj->imageLayout, | 
|  | 909 | VK_ACCESS_HOST_WRITE_BIT); | 
| Karl Schultz | 481756e | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 910 | /* setting the image layout does not reference the actual memory so no need | 
|  | 911 | * to add a mem ref */ | 
| Courtney Goeltzenleuchter | 372e13c | 2015-02-13 17:52:46 -0700 | [diff] [blame] | 912 | } | 
|  | 913 |  | 
| Karl Schultz | 481756e | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 914 | static void demo_destroy_texture_image(struct demo *demo, | 
|  | 915 | struct texture_object *tex_obj) { | 
| Courtney Goeltzenleuchter | 372e13c | 2015-02-13 17:52:46 -0700 | [diff] [blame] | 916 | /* clean up staging resources */ | 
| Chia-I Wu | 69f4012 | 2015-10-26 21:10:41 +0800 | [diff] [blame] | 917 | vkDestroyImage(demo->device, tex_obj->image, NULL); | 
|  | 918 | vkFreeMemory(demo->device, tex_obj->mem, NULL); | 
| Courtney Goeltzenleuchter | 372e13c | 2015-02-13 17:52:46 -0700 | [diff] [blame] | 919 | } | 
|  | 920 |  | 
| Karl Schultz | 481756e | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 921 | static void demo_prepare_textures(struct demo *demo) { | 
| Tony Barbour | 8205d90 | 2015-04-16 15:59:00 -0600 | [diff] [blame] | 922 | const VkFormat tex_format = VK_FORMAT_B8G8R8A8_UNORM; | 
| Courtney Goeltzenleuchter | 382489d | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 923 | VkFormatProperties props; | 
| Courtney Goeltzenleuchter | 372e13c | 2015-02-13 17:52:46 -0700 | [diff] [blame] | 924 | const uint32_t tex_colors[DEMO_TEXTURE_COUNT][2] = { | 
| Karl Schultz | 481756e | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 925 | {0xffff0000, 0xff00ff00}, | 
| Courtney Goeltzenleuchter | 372e13c | 2015-02-13 17:52:46 -0700 | [diff] [blame] | 926 | }; | 
| Courtney Goeltzenleuchter | 372e13c | 2015-02-13 17:52:46 -0700 | [diff] [blame] | 927 | uint32_t i; | 
| Courtney Goeltzenleuchter | e92af7e | 2015-12-17 09:53:29 -0700 | [diff] [blame] | 928 | VkResult U_ASSERT_ONLY err; | 
| Courtney Goeltzenleuchter | 372e13c | 2015-02-13 17:52:46 -0700 | [diff] [blame] | 929 |  | 
| Courtney Goeltzenleuchter | 01d2ae1 | 2015-10-20 16:40:38 -0600 | [diff] [blame] | 930 | vkGetPhysicalDeviceFormatProperties(demo->gpu, tex_format, &props); | 
| Courtney Goeltzenleuchter | 372e13c | 2015-02-13 17:52:46 -0700 | [diff] [blame] | 931 |  | 
|  | 932 | for (i = 0; i < DEMO_TEXTURE_COUNT; i++) { | 
| Karl Schultz | 481756e | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 933 | if ((props.linearTilingFeatures & | 
|  | 934 | VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT) && | 
|  | 935 | !demo->use_staging_buffer) { | 
| Courtney Goeltzenleuchter | 372e13c | 2015-02-13 17:52:46 -0700 | [diff] [blame] | 936 | /* Device can texture using linear textures */ | 
|  | 937 | demo_prepare_texture_image(demo, tex_colors[i], &demo->textures[i], | 
| Karl Schultz | 481756e | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 938 | VK_IMAGE_TILING_LINEAR, | 
|  | 939 | VK_IMAGE_USAGE_SAMPLED_BIT, | 
|  | 940 | VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT); | 
|  | 941 | } else if (props.optimalTilingFeatures & | 
|  | 942 | VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT) { | 
| Courtney Goeltzenleuchter | 372e13c | 2015-02-13 17:52:46 -0700 | [diff] [blame] | 943 | /* Must use staging buffer to copy linear texture to optimized */ | 
| Courtney Goeltzenleuchter | bfccf41 | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 944 | struct texture_object staging_texture; | 
| Courtney Goeltzenleuchter | 372e13c | 2015-02-13 17:52:46 -0700 | [diff] [blame] | 945 |  | 
|  | 946 | memset(&staging_texture, 0, sizeof(staging_texture)); | 
|  | 947 | demo_prepare_texture_image(demo, tex_colors[i], &staging_texture, | 
| Karl Schultz | 481756e | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 948 | VK_IMAGE_TILING_LINEAR, | 
|  | 949 | VK_IMAGE_USAGE_TRANSFER_SRC_BIT, | 
|  | 950 | VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT); | 
| Courtney Goeltzenleuchter | 372e13c | 2015-02-13 17:52:46 -0700 | [diff] [blame] | 951 |  | 
| Karl Schultz | 481756e | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 952 | demo_prepare_texture_image( | 
|  | 953 | demo, tex_colors[i], &demo->textures[i], | 
|  | 954 | VK_IMAGE_TILING_OPTIMAL, | 
|  | 955 | (VK_IMAGE_USAGE_TRANSFER_DST_BIT | VK_IMAGE_USAGE_SAMPLED_BIT), | 
|  | 956 | VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT); | 
| Courtney Goeltzenleuchter | 372e13c | 2015-02-13 17:52:46 -0700 | [diff] [blame] | 957 |  | 
| Courtney Goeltzenleuchter | bfccf41 | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 958 | demo_set_image_layout(demo, staging_texture.image, | 
| Karl Schultz | 481756e | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 959 | VK_IMAGE_ASPECT_COLOR_BIT, | 
|  | 960 | staging_texture.imageLayout, | 
| Mark Lobodzinski | acae053 | 2016-02-22 08:57:55 -0700 | [diff] [blame] | 961 | VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, | 
|  | 962 | 0); | 
| Courtney Goeltzenleuchter | 372e13c | 2015-02-13 17:52:46 -0700 | [diff] [blame] | 963 |  | 
| Courtney Goeltzenleuchter | bfccf41 | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 964 | demo_set_image_layout(demo, demo->textures[i].image, | 
| Karl Schultz | 481756e | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 965 | VK_IMAGE_ASPECT_COLOR_BIT, | 
|  | 966 | demo->textures[i].imageLayout, | 
| Mark Lobodzinski | acae053 | 2016-02-22 08:57:55 -0700 | [diff] [blame] | 967 | VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, | 
|  | 968 | 0); | 
| Courtney Goeltzenleuchter | 372e13c | 2015-02-13 17:52:46 -0700 | [diff] [blame] | 969 |  | 
| Courtney Goeltzenleuchter | 382489d | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 970 | VkImageCopy copy_region = { | 
| Karl Schultz | 481756e | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 971 | .srcSubresource = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1}, | 
|  | 972 | .srcOffset = {0, 0, 0}, | 
|  | 973 | .dstSubresource = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1}, | 
|  | 974 | .dstOffset = {0, 0, 0}, | 
|  | 975 | .extent = {staging_texture.tex_width, | 
|  | 976 | staging_texture.tex_height, 1}, | 
| Courtney Goeltzenleuchter | 372e13c | 2015-02-13 17:52:46 -0700 | [diff] [blame] | 977 | }; | 
| Karl Schultz | 481756e | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 978 | vkCmdCopyImage( | 
|  | 979 | demo->setup_cmd, staging_texture.image, | 
|  | 980 | VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, demo->textures[i].image, | 
|  | 981 | VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, 1, ©_region); | 
| Courtney Goeltzenleuchter | 372e13c | 2015-02-13 17:52:46 -0700 | [diff] [blame] | 982 |  | 
| Courtney Goeltzenleuchter | bfccf41 | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 983 | demo_set_image_layout(demo, demo->textures[i].image, | 
| Karl Schultz | 481756e | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 984 | VK_IMAGE_ASPECT_COLOR_BIT, | 
|  | 985 | VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, | 
| Mark Lobodzinski | acae053 | 2016-02-22 08:57:55 -0700 | [diff] [blame] | 986 | demo->textures[i].imageLayout, | 
|  | 987 | 0); | 
| Courtney Goeltzenleuchter | 372e13c | 2015-02-13 17:52:46 -0700 | [diff] [blame] | 988 |  | 
| Courtney Goeltzenleuchter | bfccf41 | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 989 | demo_flush_init_cmd(demo); | 
| Courtney Goeltzenleuchter | 372e13c | 2015-02-13 17:52:46 -0700 | [diff] [blame] | 990 |  | 
| Courtney Goeltzenleuchter | 876629f | 2015-04-21 09:30:03 -0600 | [diff] [blame] | 991 | demo_destroy_texture_image(demo, &staging_texture); | 
| Courtney Goeltzenleuchter | 372e13c | 2015-02-13 17:52:46 -0700 | [diff] [blame] | 992 | } else { | 
| Tony Barbour | 8205d90 | 2015-04-16 15:59:00 -0600 | [diff] [blame] | 993 | /* Can't support VK_FORMAT_B8G8R8A8_UNORM !? */ | 
| Piers Daniell | 886be47 | 2015-02-23 16:23:13 -0700 | [diff] [blame] | 994 | assert(!"No support for B8G8R8A8_UNORM as texture image format"); | 
| Courtney Goeltzenleuchter | 372e13c | 2015-02-13 17:52:46 -0700 | [diff] [blame] | 995 | } | 
| Courtney Goeltzenleuchter | 3226a6f | 2015-02-11 18:17:22 -0700 | [diff] [blame] | 996 |  | 
| Courtney Goeltzenleuchter | 382489d | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 997 | const VkSamplerCreateInfo sampler = { | 
| Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 998 | .sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO, | 
| Chia-I Wu | b043fe3 | 2014-10-06 15:30:33 +0800 | [diff] [blame] | 999 | .pNext = NULL, | 
| Chia-I Wu | 3603b08 | 2015-10-26 16:49:32 +0800 | [diff] [blame] | 1000 | .magFilter = VK_FILTER_NEAREST, | 
|  | 1001 | .minFilter = VK_FILTER_NEAREST, | 
| Jon Ashburn | a4ae48b | 2016-01-11 13:12:43 -0700 | [diff] [blame] | 1002 | .mipmapMode = VK_SAMPLER_MIPMAP_MODE_NEAREST, | 
| Chia-I Wu | ce532f7 | 2015-10-26 17:32:47 +0800 | [diff] [blame] | 1003 | .addressModeU = VK_SAMPLER_ADDRESS_MODE_REPEAT, | 
|  | 1004 | .addressModeV = VK_SAMPLER_ADDRESS_MODE_REPEAT, | 
|  | 1005 | .addressModeW = VK_SAMPLER_ADDRESS_MODE_REPEAT, | 
| Chia-I Wu | b043fe3 | 2014-10-06 15:30:33 +0800 | [diff] [blame] | 1006 | .mipLodBias = 0.0f, | 
| Jon Ashburn | 0717ed5 | 2015-12-14 14:59:20 -0700 | [diff] [blame] | 1007 | .anisotropyEnable = VK_FALSE, | 
| Courtney Goeltzenleuchter | bc9c816 | 2015-02-13 18:20:24 -0700 | [diff] [blame] | 1008 | .maxAnisotropy = 1, | 
| Tony Barbour | 8205d90 | 2015-04-16 15:59:00 -0600 | [diff] [blame] | 1009 | .compareOp = VK_COMPARE_OP_NEVER, | 
| Chia-I Wu | b043fe3 | 2014-10-06 15:30:33 +0800 | [diff] [blame] | 1010 | .minLod = 0.0f, | 
|  | 1011 | .maxLod = 0.0f, | 
| Tony Barbour | 2c4e7c7 | 2015-06-25 16:56:44 -0600 | [diff] [blame] | 1012 | .borderColor = VK_BORDER_COLOR_FLOAT_OPAQUE_WHITE, | 
| Mark Lobodzinski | 513acdf | 2015-09-01 15:42:56 -0600 | [diff] [blame] | 1013 | .unnormalizedCoordinates = VK_FALSE, | 
| Chia-I Wu | b043fe3 | 2014-10-06 15:30:33 +0800 | [diff] [blame] | 1014 | }; | 
| Courtney Goeltzenleuchter | 382489d | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 1015 | VkImageViewCreateInfo view = { | 
| Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1016 | .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO, | 
| Chia-I Wu | b043fe3 | 2014-10-06 15:30:33 +0800 | [diff] [blame] | 1017 | .pNext = NULL, | 
| Chia-I Wu | e420a33 | 2015-10-26 20:04:44 +0800 | [diff] [blame] | 1018 | .image = VK_NULL_HANDLE, | 
| Tony Barbour | 8205d90 | 2015-04-16 15:59:00 -0600 | [diff] [blame] | 1019 | .viewType = VK_IMAGE_VIEW_TYPE_2D, | 
| Courtney Goeltzenleuchter | 372e13c | 2015-02-13 17:52:46 -0700 | [diff] [blame] | 1020 | .format = tex_format, | 
| Karl Schultz | 481756e | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 1021 | .components = | 
|  | 1022 | { | 
|  | 1023 | VK_COMPONENT_SWIZZLE_R, VK_COMPONENT_SWIZZLE_G, | 
|  | 1024 | VK_COMPONENT_SWIZZLE_B, VK_COMPONENT_SWIZZLE_A, | 
|  | 1025 | }, | 
|  | 1026 | .subresourceRange = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1}, | 
| Courtney Goeltzenleuchter | 1856d6f | 2015-09-01 17:30:39 -0600 | [diff] [blame] | 1027 | .flags = 0, | 
| Chia-I Wu | b043fe3 | 2014-10-06 15:30:33 +0800 | [diff] [blame] | 1028 | }; | 
| Jon Ashburn | a9ae383 | 2015-01-16 09:37:43 -0700 | [diff] [blame] | 1029 |  | 
| Chia-I Wu | b043fe3 | 2014-10-06 15:30:33 +0800 | [diff] [blame] | 1030 | /* create sampler */ | 
| Chia-I Wu | 69f4012 | 2015-10-26 21:10:41 +0800 | [diff] [blame] | 1031 | err = vkCreateSampler(demo->device, &sampler, NULL, | 
| Karl Schultz | 481756e | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 1032 | &demo->textures[i].sampler); | 
| Chia-I Wu | b043fe3 | 2014-10-06 15:30:33 +0800 | [diff] [blame] | 1033 | assert(!err); | 
|  | 1034 |  | 
| Chia-I Wu | b043fe3 | 2014-10-06 15:30:33 +0800 | [diff] [blame] | 1035 | /* create image view */ | 
|  | 1036 | view.image = demo->textures[i].image; | 
| Chia-I Wu | 69f4012 | 2015-10-26 21:10:41 +0800 | [diff] [blame] | 1037 | err = vkCreateImageView(demo->device, &view, NULL, | 
| Karl Schultz | 481756e | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 1038 | &demo->textures[i].view); | 
| Chia-I Wu | b043fe3 | 2014-10-06 15:30:33 +0800 | [diff] [blame] | 1039 | assert(!err); | 
| Chia-I Wu | b043fe3 | 2014-10-06 15:30:33 +0800 | [diff] [blame] | 1040 | } | 
|  | 1041 | } | 
|  | 1042 |  | 
| Karl Schultz | 481756e | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 1043 | static void demo_prepare_vertices(struct demo *demo) { | 
|  | 1044 | // clang-format off | 
| Chia-I Wu | 99621bc | 2014-10-08 11:52:22 +0800 | [diff] [blame] | 1045 | const float vb[3][5] = { | 
|  | 1046 | /*      position             texcoord */ | 
| Cody Northrop | a9efa05 | 2015-10-13 11:28:23 -0600 | [diff] [blame] | 1047 | { -1.0f, -1.0f,  0.25f,     0.0f, 0.0f }, | 
| Chia-I Wu | e2504cb | 2015-04-22 14:20:52 +0800 | [diff] [blame] | 1048 | {  1.0f, -1.0f,  0.25f,     1.0f, 0.0f }, | 
| Chia-I Wu | 99621bc | 2014-10-08 11:52:22 +0800 | [diff] [blame] | 1049 | {  0.0f,  1.0f,  1.0f,      0.5f, 1.0f }, | 
|  | 1050 | }; | 
| Karl Schultz | 481756e | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 1051 | // clang-format on | 
| Courtney Goeltzenleuchter | ddcb619 | 2015-04-14 18:48:46 -0600 | [diff] [blame] | 1052 | const VkBufferCreateInfo buf_info = { | 
| Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1053 | .sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO, | 
| Chia-I Wu | 714df45 | 2015-01-01 07:55:04 +0800 | [diff] [blame] | 1054 | .pNext = NULL, | 
|  | 1055 | .size = sizeof(vb), | 
| Courtney Goeltzenleuchter | ad87081 | 2015-04-15 15:29:59 -0600 | [diff] [blame] | 1056 | .usage = VK_BUFFER_USAGE_VERTEX_BUFFER_BIT, | 
| Chia-I Wu | 714df45 | 2015-01-01 07:55:04 +0800 | [diff] [blame] | 1057 | .flags = 0, | 
|  | 1058 | }; | 
| Chia-I Wu | 1f85191 | 2015-10-27 18:04:07 +0800 | [diff] [blame] | 1059 | VkMemoryAllocateInfo mem_alloc = { | 
| Chia-I Wu | c1f5e40 | 2015-11-10 16:21:09 +0800 | [diff] [blame] | 1060 | .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO, | 
| Mark Lobodzinski | 97dcd04 | 2015-04-16 08:52:00 -0500 | [diff] [blame] | 1061 | .pNext = NULL, | 
| Chia-I Wu | 714df45 | 2015-01-01 07:55:04 +0800 | [diff] [blame] | 1062 | .allocationSize = 0, | 
| Mark Lobodzinski | 7234629 | 2015-07-02 16:49:40 -0600 | [diff] [blame] | 1063 | .memoryTypeIndex = 0, | 
| Chia-I Wu | 99621bc | 2014-10-08 11:52:22 +0800 | [diff] [blame] | 1064 | }; | 
| Mark Lobodzinski | 2318261 | 2015-05-29 09:32:35 -0500 | [diff] [blame] | 1065 | VkMemoryRequirements mem_reqs; | 
| Tony Barbour | 22a3086 | 2015-04-22 09:02:32 -0600 | [diff] [blame] | 1066 | VkResult U_ASSERT_ONLY err; | 
| Courtney Goeltzenleuchter | 37a43a6 | 2015-10-22 11:03:31 -0600 | [diff] [blame] | 1067 | bool U_ASSERT_ONLY pass; | 
| Chia-I Wu | 99621bc | 2014-10-08 11:52:22 +0800 | [diff] [blame] | 1068 | void *data; | 
|  | 1069 |  | 
|  | 1070 | memset(&demo->vertices, 0, sizeof(demo->vertices)); | 
|  | 1071 |  | 
| Chia-I Wu | 69f4012 | 2015-10-26 21:10:41 +0800 | [diff] [blame] | 1072 | err = vkCreateBuffer(demo->device, &buf_info, NULL, &demo->vertices.buf); | 
| Chia-I Wu | 714df45 | 2015-01-01 07:55:04 +0800 | [diff] [blame] | 1073 | assert(!err); | 
|  | 1074 |  | 
| Karl Schultz | 481756e | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 1075 | vkGetBufferMemoryRequirements(demo->device, demo->vertices.buf, &mem_reqs); | 
| Tony Barbour | c109827 | 2015-10-23 10:53:30 -0600 | [diff] [blame] | 1076 | assert(!err); | 
| Chia-I Wu | 714df45 | 2015-01-01 07:55:04 +0800 | [diff] [blame] | 1077 |  | 
| Karl Schultz | 481756e | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 1078 | mem_alloc.allocationSize = mem_reqs.size; | 
|  | 1079 | pass = memory_type_from_properties(demo, mem_reqs.memoryTypeBits, | 
|  | 1080 | VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT, | 
|  | 1081 | &mem_alloc.memoryTypeIndex); | 
| Courtney Goeltzenleuchter | 37a43a6 | 2015-10-22 11:03:31 -0600 | [diff] [blame] | 1082 | assert(pass); | 
| Chia-I Wu | 99621bc | 2014-10-08 11:52:22 +0800 | [diff] [blame] | 1083 |  | 
| Chia-I Wu | 1f85191 | 2015-10-27 18:04:07 +0800 | [diff] [blame] | 1084 | err = vkAllocateMemory(demo->device, &mem_alloc, NULL, &demo->vertices.mem); | 
| Mark Lobodzinski | 2318261 | 2015-05-29 09:32:35 -0500 | [diff] [blame] | 1085 | assert(!err); | 
| Chia-I Wu | 99621bc | 2014-10-08 11:52:22 +0800 | [diff] [blame] | 1086 |  | 
| Karl Schultz | 481756e | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 1087 | err = vkMapMemory(demo->device, demo->vertices.mem, 0, | 
|  | 1088 | mem_alloc.allocationSize, 0, &data); | 
| Mark Lobodzinski | 2318261 | 2015-05-29 09:32:35 -0500 | [diff] [blame] | 1089 | assert(!err); | 
| Chia-I Wu | 99621bc | 2014-10-08 11:52:22 +0800 | [diff] [blame] | 1090 |  | 
| Mark Lobodzinski | 2318261 | 2015-05-29 09:32:35 -0500 | [diff] [blame] | 1091 | memcpy(data, vb, sizeof(vb)); | 
| Chia-I Wu | 99621bc | 2014-10-08 11:52:22 +0800 | [diff] [blame] | 1092 |  | 
| Mark Lobodzinski | 67b42b7 | 2015-09-07 13:59:43 -0600 | [diff] [blame] | 1093 | vkUnmapMemory(demo->device, demo->vertices.mem); | 
| Mark Lobodzinski | 2318261 | 2015-05-29 09:32:35 -0500 | [diff] [blame] | 1094 |  | 
| Tony Barbour | de4124d | 2015-07-03 10:33:54 -0600 | [diff] [blame] | 1095 | err = vkBindBufferMemory(demo->device, demo->vertices.buf, | 
| Karl Schultz | 481756e | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 1096 | demo->vertices.mem, 0); | 
| Mark Lobodzinski | 2318261 | 2015-05-29 09:32:35 -0500 | [diff] [blame] | 1097 | assert(!err); | 
| Chia-I Wu | 714df45 | 2015-01-01 07:55:04 +0800 | [diff] [blame] | 1098 |  | 
| Karl Schultz | 481756e | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 1099 | demo->vertices.vi.sType = | 
|  | 1100 | VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO; | 
| Chia-I Wu | 8d29d02 | 2014-10-08 12:14:39 +0800 | [diff] [blame] | 1101 | demo->vertices.vi.pNext = NULL; | 
| Chia-I Wu | 763a749 | 2015-10-26 20:48:51 +0800 | [diff] [blame] | 1102 | demo->vertices.vi.vertexBindingDescriptionCount = 1; | 
| Chia-I Wu | 8d29d02 | 2014-10-08 12:14:39 +0800 | [diff] [blame] | 1103 | demo->vertices.vi.pVertexBindingDescriptions = demo->vertices.vi_bindings; | 
| Chia-I Wu | 763a749 | 2015-10-26 20:48:51 +0800 | [diff] [blame] | 1104 | demo->vertices.vi.vertexAttributeDescriptionCount = 2; | 
| Chia-I Wu | 8d29d02 | 2014-10-08 12:14:39 +0800 | [diff] [blame] | 1105 | demo->vertices.vi.pVertexAttributeDescriptions = demo->vertices.vi_attrs; | 
|  | 1106 |  | 
| Courtney Goeltzenleuchter | f5cdad0 | 2015-03-31 16:36:30 -0600 | [diff] [blame] | 1107 | demo->vertices.vi_bindings[0].binding = VERTEX_BUFFER_BIND_ID; | 
| Chia-I Wu | 7e47070 | 2015-10-26 17:24:52 +0800 | [diff] [blame] | 1108 | demo->vertices.vi_bindings[0].stride = sizeof(vb[0]); | 
| Chia-I Wu | c51b121 | 2015-10-27 19:25:11 +0800 | [diff] [blame] | 1109 | demo->vertices.vi_bindings[0].inputRate = VK_VERTEX_INPUT_RATE_VERTEX; | 
| Chia-I Wu | 8d29d02 | 2014-10-08 12:14:39 +0800 | [diff] [blame] | 1110 |  | 
| Courtney Goeltzenleuchter | f5cdad0 | 2015-03-31 16:36:30 -0600 | [diff] [blame] | 1111 | demo->vertices.vi_attrs[0].binding = VERTEX_BUFFER_BIND_ID; | 
|  | 1112 | demo->vertices.vi_attrs[0].location = 0; | 
| Tony Barbour | 8205d90 | 2015-04-16 15:59:00 -0600 | [diff] [blame] | 1113 | demo->vertices.vi_attrs[0].format = VK_FORMAT_R32G32B32_SFLOAT; | 
| Chia-I Wu | 7e47070 | 2015-10-26 17:24:52 +0800 | [diff] [blame] | 1114 | demo->vertices.vi_attrs[0].offset = 0; | 
| Chia-I Wu | 8d29d02 | 2014-10-08 12:14:39 +0800 | [diff] [blame] | 1115 |  | 
| Courtney Goeltzenleuchter | f5cdad0 | 2015-03-31 16:36:30 -0600 | [diff] [blame] | 1116 | demo->vertices.vi_attrs[1].binding = VERTEX_BUFFER_BIND_ID; | 
|  | 1117 | demo->vertices.vi_attrs[1].location = 1; | 
| Tony Barbour | 8205d90 | 2015-04-16 15:59:00 -0600 | [diff] [blame] | 1118 | demo->vertices.vi_attrs[1].format = VK_FORMAT_R32G32_SFLOAT; | 
| Chia-I Wu | 7e47070 | 2015-10-26 17:24:52 +0800 | [diff] [blame] | 1119 | demo->vertices.vi_attrs[1].offset = sizeof(float) * 3; | 
| Chia-I Wu | 99621bc | 2014-10-08 11:52:22 +0800 | [diff] [blame] | 1120 | } | 
|  | 1121 |  | 
| Karl Schultz | 481756e | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 1122 | static void demo_prepare_descriptor_layout(struct demo *demo) { | 
| Courtney Goeltzenleuchter | 382489d | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 1123 | const VkDescriptorSetLayoutBinding layout_binding = { | 
| Chia-I Wu | b5689ee | 2015-10-31 00:31:16 +0800 | [diff] [blame] | 1124 | .binding = 0, | 
| Courtney Goeltzenleuchter | ad87081 | 2015-04-15 15:29:59 -0600 | [diff] [blame] | 1125 | .descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, | 
| Chia-I Wu | 045654f | 2015-11-06 06:42:02 +0800 | [diff] [blame] | 1126 | .descriptorCount = DEMO_TEXTURE_COUNT, | 
| Tony Barbour | 8205d90 | 2015-04-16 15:59:00 -0600 | [diff] [blame] | 1127 | .stageFlags = VK_SHADER_STAGE_FRAGMENT_BIT, | 
| Chia-I Wu | 310eece | 2015-03-27 12:56:09 +0800 | [diff] [blame] | 1128 | .pImmutableSamplers = NULL, | 
| Chia-I Wu | b043fe3 | 2014-10-06 15:30:33 +0800 | [diff] [blame] | 1129 | }; | 
| Courtney Goeltzenleuchter | 382489d | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 1130 | const VkDescriptorSetLayoutCreateInfo descriptor_layout = { | 
| Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1131 | .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO, | 
| Chia-I Wu | fc9d913 | 2015-03-26 15:04:41 +0800 | [diff] [blame] | 1132 | .pNext = NULL, | 
| Chia-I Wu | 763a749 | 2015-10-26 20:48:51 +0800 | [diff] [blame] | 1133 | .bindingCount = 1, | 
| Jon Ashburn | adb6135 | 2015-12-30 18:01:16 -0700 | [diff] [blame] | 1134 | .pBindings = &layout_binding, | 
| Chia-I Wu | fc9d913 | 2015-03-26 15:04:41 +0800 | [diff] [blame] | 1135 | }; | 
| Tony Barbour | 22a3086 | 2015-04-22 09:02:32 -0600 | [diff] [blame] | 1136 | VkResult U_ASSERT_ONLY err; | 
| Chia-I Wu | b043fe3 | 2014-10-06 15:30:33 +0800 | [diff] [blame] | 1137 |  | 
| Karl Schultz | 481756e | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 1138 | err = vkCreateDescriptorSetLayout(demo->device, &descriptor_layout, NULL, | 
|  | 1139 | &demo->desc_layout); | 
| Chia-I Wu | 7732cb2 | 2015-03-26 15:27:55 +0800 | [diff] [blame] | 1140 | assert(!err); | 
|  | 1141 |  | 
| Mark Lobodzinski | 556f721 | 2015-04-17 14:11:39 -0500 | [diff] [blame] | 1142 | const VkPipelineLayoutCreateInfo pPipelineLayoutCreateInfo = { | 
| Karl Schultz | 481756e | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 1143 | .sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO, | 
|  | 1144 | .pNext = NULL, | 
| Chia-I Wu | 763a749 | 2015-10-26 20:48:51 +0800 | [diff] [blame] | 1145 | .setLayoutCount = 1, | 
| Karl Schultz | 481756e | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 1146 | .pSetLayouts = &demo->desc_layout, | 
| Mark Lobodzinski | 556f721 | 2015-04-17 14:11:39 -0500 | [diff] [blame] | 1147 | }; | 
|  | 1148 |  | 
| Karl Schultz | 481756e | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 1149 | err = vkCreatePipelineLayout(demo->device, &pPipelineLayoutCreateInfo, NULL, | 
| Mark Lobodzinski | 556f721 | 2015-04-17 14:11:39 -0500 | [diff] [blame] | 1150 | &demo->pipeline_layout); | 
| Chia-I Wu | b043fe3 | 2014-10-06 15:30:33 +0800 | [diff] [blame] | 1151 | assert(!err); | 
| Chia-I Wu | b043fe3 | 2014-10-06 15:30:33 +0800 | [diff] [blame] | 1152 | } | 
|  | 1153 |  | 
| Karl Schultz | 481756e | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 1154 | static void demo_prepare_render_pass(struct demo *demo) { | 
| Chia-I Wu | c278df8 | 2015-07-07 11:50:03 +0800 | [diff] [blame] | 1155 | const VkAttachmentDescription attachments[2] = { | 
| Karl Schultz | 481756e | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 1156 | [0] = | 
|  | 1157 | { | 
|  | 1158 | .format = demo->format, | 
|  | 1159 | .samples = VK_SAMPLE_COUNT_1_BIT, | 
|  | 1160 | .loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR, | 
|  | 1161 | .storeOp = VK_ATTACHMENT_STORE_OP_STORE, | 
|  | 1162 | .stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE, | 
|  | 1163 | .stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE, | 
|  | 1164 | .initialLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL, | 
|  | 1165 | .finalLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL, | 
|  | 1166 | }, | 
|  | 1167 | [1] = | 
|  | 1168 | { | 
|  | 1169 | .format = demo->depth.format, | 
|  | 1170 | .samples = VK_SAMPLE_COUNT_1_BIT, | 
|  | 1171 | .loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR, | 
|  | 1172 | .storeOp = VK_ATTACHMENT_STORE_OP_DONT_CARE, | 
|  | 1173 | .stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE, | 
|  | 1174 | .stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE, | 
|  | 1175 | .initialLayout = | 
|  | 1176 | VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL, | 
|  | 1177 | .finalLayout = | 
|  | 1178 | VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL, | 
|  | 1179 | }, | 
| Chia-I Wu | 76cd422 | 2015-07-08 13:34:24 +0800 | [diff] [blame] | 1180 | }; | 
| Chia-I Wu | c278df8 | 2015-07-07 11:50:03 +0800 | [diff] [blame] | 1181 | const VkAttachmentReference color_reference = { | 
| Karl Schultz | 481756e | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 1182 | .attachment = 0, .layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL, | 
| Chia-I Wu | c278df8 | 2015-07-07 11:50:03 +0800 | [diff] [blame] | 1183 | }; | 
| Chia-I Wu | ce532f7 | 2015-10-26 17:32:47 +0800 | [diff] [blame] | 1184 | const VkAttachmentReference depth_reference = { | 
|  | 1185 | .attachment = 1, | 
|  | 1186 | .layout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL, | 
|  | 1187 | }; | 
| Chia-I Wu | c278df8 | 2015-07-07 11:50:03 +0800 | [diff] [blame] | 1188 | const VkSubpassDescription subpass = { | 
| Chia-I Wu | c278df8 | 2015-07-07 11:50:03 +0800 | [diff] [blame] | 1189 | .pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS, | 
|  | 1190 | .flags = 0, | 
| Chia-I Wu | 763a749 | 2015-10-26 20:48:51 +0800 | [diff] [blame] | 1191 | .inputAttachmentCount = 0, | 
| Cody Northrop | 6de6b0b | 2015-08-04 11:16:41 -0600 | [diff] [blame] | 1192 | .pInputAttachments = NULL, | 
| Chia-I Wu | 763a749 | 2015-10-26 20:48:51 +0800 | [diff] [blame] | 1193 | .colorAttachmentCount = 1, | 
| Cody Northrop | 6de6b0b | 2015-08-04 11:16:41 -0600 | [diff] [blame] | 1194 | .pColorAttachments = &color_reference, | 
|  | 1195 | .pResolveAttachments = NULL, | 
| Chia-I Wu | ce532f7 | 2015-10-26 17:32:47 +0800 | [diff] [blame] | 1196 | .pDepthStencilAttachment = &depth_reference, | 
| Chia-I Wu | 763a749 | 2015-10-26 20:48:51 +0800 | [diff] [blame] | 1197 | .preserveAttachmentCount = 0, | 
| Cody Northrop | 6de6b0b | 2015-08-04 11:16:41 -0600 | [diff] [blame] | 1198 | .pPreserveAttachments = NULL, | 
| Chia-I Wu | c278df8 | 2015-07-07 11:50:03 +0800 | [diff] [blame] | 1199 | }; | 
| Chia-I Wu | 76cd422 | 2015-07-08 13:34:24 +0800 | [diff] [blame] | 1200 | const VkRenderPassCreateInfo rp_info = { | 
|  | 1201 | .sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO, | 
|  | 1202 | .pNext = NULL, | 
| Chia-I Wu | c278df8 | 2015-07-07 11:50:03 +0800 | [diff] [blame] | 1203 | .attachmentCount = 2, | 
|  | 1204 | .pAttachments = attachments, | 
|  | 1205 | .subpassCount = 1, | 
|  | 1206 | .pSubpasses = &subpass, | 
|  | 1207 | .dependencyCount = 0, | 
|  | 1208 | .pDependencies = NULL, | 
| Chia-I Wu | 76cd422 | 2015-07-08 13:34:24 +0800 | [diff] [blame] | 1209 | }; | 
| Chia-I Wu | c278df8 | 2015-07-07 11:50:03 +0800 | [diff] [blame] | 1210 | VkResult U_ASSERT_ONLY err; | 
| Chia-I Wu | 76cd422 | 2015-07-08 13:34:24 +0800 | [diff] [blame] | 1211 |  | 
| Chia-I Wu | 69f4012 | 2015-10-26 21:10:41 +0800 | [diff] [blame] | 1212 | err = vkCreateRenderPass(demo->device, &rp_info, NULL, &demo->render_pass); | 
| Chia-I Wu | 76cd422 | 2015-07-08 13:34:24 +0800 | [diff] [blame] | 1213 | assert(!err); | 
|  | 1214 | } | 
|  | 1215 |  | 
| Karl Schultz | 481756e | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 1216 | static VkShaderModule | 
|  | 1217 | demo_prepare_shader_module(struct demo *demo, const void *code, size_t size) { | 
| Courtney Goeltzenleuchter | 2d034fd | 2015-06-28 13:01:17 -0600 | [diff] [blame] | 1218 | VkShaderModuleCreateInfo moduleCreateInfo; | 
| Chia-I Wu | 062ad15 | 2015-10-31 00:31:16 +0800 | [diff] [blame] | 1219 | VkShaderModule module; | 
| Tony Barbour | 4a6692d | 2015-10-08 13:45:45 -0600 | [diff] [blame] | 1220 | VkResult U_ASSERT_ONLY err; | 
| Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 1221 |  | 
| Courtney Goeltzenleuchter | 2d034fd | 2015-06-28 13:01:17 -0600 | [diff] [blame] | 1222 | moduleCreateInfo.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO; | 
|  | 1223 | moduleCreateInfo.pNext = NULL; | 
|  | 1224 |  | 
| Courtney Goeltzenleuchter | 8961c98 | 2015-10-30 15:19:27 -0600 | [diff] [blame] | 1225 | moduleCreateInfo.codeSize = size; | 
|  | 1226 | moduleCreateInfo.pCode = code; | 
|  | 1227 | moduleCreateInfo.flags = 0; | 
|  | 1228 | err = vkCreateShaderModule(demo->device, &moduleCreateInfo, NULL, &module); | 
|  | 1229 | assert(!err); | 
| Chia-I Wu | 062ad15 | 2015-10-31 00:31:16 +0800 | [diff] [blame] | 1230 |  | 
|  | 1231 | return module; | 
| Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 1232 | } | 
|  | 1233 |  | 
| Karl Schultz | 481756e | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 1234 | char *demo_read_spv(const char *filename, size_t *psize) { | 
| Cody Northrop | 75db032 | 2015-05-28 11:27:16 -0600 | [diff] [blame] | 1235 | long int size; | 
|  | 1236 | void *shader_code; | 
| Tony Barbour | 9687cb1 | 2015-07-14 13:34:05 -0600 | [diff] [blame] | 1237 | size_t retVal; | 
| Cody Northrop | 75db032 | 2015-05-28 11:27:16 -0600 | [diff] [blame] | 1238 |  | 
|  | 1239 | FILE *fp = fopen(filename, "rb"); | 
| Karl Schultz | 481756e | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 1240 | if (!fp) | 
|  | 1241 | return NULL; | 
| Cody Northrop | 75db032 | 2015-05-28 11:27:16 -0600 | [diff] [blame] | 1242 |  | 
|  | 1243 | fseek(fp, 0L, SEEK_END); | 
|  | 1244 | size = ftell(fp); | 
|  | 1245 |  | 
|  | 1246 | fseek(fp, 0L, SEEK_SET); | 
|  | 1247 |  | 
|  | 1248 | shader_code = malloc(size); | 
| Tony Barbour | 9687cb1 | 2015-07-14 13:34:05 -0600 | [diff] [blame] | 1249 | retVal = fread(shader_code, size, 1, fp); | 
| Karl Schultz | 481756e | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 1250 | if (!retVal) | 
|  | 1251 | return NULL; | 
| Cody Northrop | 75db032 | 2015-05-28 11:27:16 -0600 | [diff] [blame] | 1252 |  | 
|  | 1253 | *psize = size; | 
|  | 1254 |  | 
| Mike Stroyan | 8518025 | 2015-10-28 11:15:46 -0600 | [diff] [blame] | 1255 | fclose(fp); | 
| Cody Northrop | 75db032 | 2015-05-28 11:27:16 -0600 | [diff] [blame] | 1256 | return shader_code; | 
|  | 1257 | } | 
|  | 1258 |  | 
| Karl Schultz | 481756e | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 1259 | static VkShaderModule demo_prepare_vs(struct demo *demo) { | 
| Courtney Goeltzenleuchter | 8961c98 | 2015-10-30 15:19:27 -0600 | [diff] [blame] | 1260 | void *vertShaderCode; | 
| Tobin Ehlis | b08d487 | 2016-04-13 12:59:08 -0600 | [diff] [blame] | 1261 | size_t size = 0; | 
| Cody Northrop | 75db032 | 2015-05-28 11:27:16 -0600 | [diff] [blame] | 1262 |  | 
| Courtney Goeltzenleuchter | 8961c98 | 2015-10-30 15:19:27 -0600 | [diff] [blame] | 1263 | vertShaderCode = demo_read_spv("tri-vert.spv", &size); | 
| Cody Northrop | 75db032 | 2015-05-28 11:27:16 -0600 | [diff] [blame] | 1264 |  | 
| Karl Schultz | 481756e | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 1265 | demo->vert_shader_module = | 
|  | 1266 | demo_prepare_shader_module(demo, vertShaderCode, size); | 
| Courtney Goeltzenleuchter | ef7301b | 2014-09-17 13:17:12 -0600 | [diff] [blame] | 1267 |  | 
| Courtney Goeltzenleuchter | 8961c98 | 2015-10-30 15:19:27 -0600 | [diff] [blame] | 1268 | free(vertShaderCode); | 
| Chia-I Wu | 062ad15 | 2015-10-31 00:31:16 +0800 | [diff] [blame] | 1269 |  | 
|  | 1270 | return demo->vert_shader_module; | 
| Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 1271 | } | 
|  | 1272 |  | 
| Karl Schultz | 481756e | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 1273 | static VkShaderModule demo_prepare_fs(struct demo *demo) { | 
| Courtney Goeltzenleuchter | 8961c98 | 2015-10-30 15:19:27 -0600 | [diff] [blame] | 1274 | void *fragShaderCode; | 
|  | 1275 | size_t size; | 
| Courtney Goeltzenleuchter | ef7301b | 2014-09-17 13:17:12 -0600 | [diff] [blame] | 1276 |  | 
| Courtney Goeltzenleuchter | 8961c98 | 2015-10-30 15:19:27 -0600 | [diff] [blame] | 1277 | fragShaderCode = demo_read_spv("tri-frag.spv", &size); | 
| Cody Northrop | 75db032 | 2015-05-28 11:27:16 -0600 | [diff] [blame] | 1278 |  | 
| Karl Schultz | 481756e | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 1279 | demo->frag_shader_module = | 
|  | 1280 | demo_prepare_shader_module(demo, fragShaderCode, size); | 
| Courtney Goeltzenleuchter | 591ac87 | 2015-09-24 17:27:08 -0600 | [diff] [blame] | 1281 |  | 
| Courtney Goeltzenleuchter | 8961c98 | 2015-10-30 15:19:27 -0600 | [diff] [blame] | 1282 | free(fragShaderCode); | 
| Chia-I Wu | 062ad15 | 2015-10-31 00:31:16 +0800 | [diff] [blame] | 1283 |  | 
|  | 1284 | return demo->frag_shader_module; | 
| Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 1285 | } | 
|  | 1286 |  | 
| Karl Schultz | 481756e | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 1287 | static void demo_prepare_pipeline(struct demo *demo) { | 
| Courtney Goeltzenleuchter | 382489d | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 1288 | VkGraphicsPipelineCreateInfo pipeline; | 
| Jon Ashburn | 0d60d27 | 2015-07-09 15:02:25 -0600 | [diff] [blame] | 1289 | VkPipelineCacheCreateInfo pipelineCache; | 
| Mark Lobodzinski | 0e0fb5c | 2015-06-23 15:11:57 -0600 | [diff] [blame] | 1290 |  | 
| Karl Schultz | 481756e | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 1291 | VkPipelineVertexInputStateCreateInfo vi; | 
| Tony Barbour | e307f58 | 2015-07-10 15:29:03 -0600 | [diff] [blame] | 1292 | VkPipelineInputAssemblyStateCreateInfo ia; | 
| Karl Schultz | 481756e | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 1293 | VkPipelineRasterizationStateCreateInfo rs; | 
|  | 1294 | VkPipelineColorBlendStateCreateInfo cb; | 
|  | 1295 | VkPipelineDepthStencilStateCreateInfo ds; | 
|  | 1296 | VkPipelineViewportStateCreateInfo vp; | 
|  | 1297 | VkPipelineMultisampleStateCreateInfo ms; | 
|  | 1298 | VkDynamicState dynamicStateEnables[VK_DYNAMIC_STATE_RANGE_SIZE]; | 
|  | 1299 | VkPipelineDynamicStateCreateInfo dynamicState; | 
| Mark Lobodzinski | 0e0fb5c | 2015-06-23 15:11:57 -0600 | [diff] [blame] | 1300 |  | 
| Tony Barbour | 22a3086 | 2015-04-22 09:02:32 -0600 | [diff] [blame] | 1301 | VkResult U_ASSERT_ONLY err; | 
| Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 1302 |  | 
| Piers Daniell | 811eb74 | 2015-09-29 13:01:09 -0600 | [diff] [blame] | 1303 | memset(dynamicStateEnables, 0, sizeof dynamicStateEnables); | 
|  | 1304 | memset(&dynamicState, 0, sizeof dynamicState); | 
|  | 1305 | dynamicState.sType = VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO; | 
|  | 1306 | dynamicState.pDynamicStates = dynamicStateEnables; | 
|  | 1307 |  | 
| Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 1308 | memset(&pipeline, 0, sizeof(pipeline)); | 
| Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1309 | pipeline.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO; | 
| Mark Lobodzinski | 556f721 | 2015-04-17 14:11:39 -0500 | [diff] [blame] | 1310 | pipeline.layout = demo->pipeline_layout; | 
| Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 1311 |  | 
| Chia-I Wu | 8d29d02 | 2014-10-08 12:14:39 +0800 | [diff] [blame] | 1312 | vi = demo->vertices.vi; | 
|  | 1313 |  | 
| Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 1314 | memset(&ia, 0, sizeof(ia)); | 
| Tony Barbour | e307f58 | 2015-07-10 15:29:03 -0600 | [diff] [blame] | 1315 | ia.sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO; | 
| Tony Barbour | 8205d90 | 2015-04-16 15:59:00 -0600 | [diff] [blame] | 1316 | ia.topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST; | 
| Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 1317 |  | 
|  | 1318 | memset(&rs, 0, sizeof(rs)); | 
| Chia-I Wu | c51b121 | 2015-10-27 19:25:11 +0800 | [diff] [blame] | 1319 | rs.sType = VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO; | 
|  | 1320 | rs.polygonMode = VK_POLYGON_MODE_FILL; | 
| Chia-I Wu | ce532f7 | 2015-10-26 17:32:47 +0800 | [diff] [blame] | 1321 | rs.cullMode = VK_CULL_MODE_BACK_BIT; | 
| Chia-I Wu | 1f85191 | 2015-10-27 18:04:07 +0800 | [diff] [blame] | 1322 | rs.frontFace = VK_FRONT_FACE_CLOCKWISE; | 
| Courtney Goeltzenleuchter | c0f9fa7 | 2015-10-15 12:57:38 -0600 | [diff] [blame] | 1323 | rs.depthClampEnable = VK_FALSE; | 
| Cody Northrop | f5bd225 | 2015-08-17 11:10:49 -0600 | [diff] [blame] | 1324 | rs.rasterizerDiscardEnable = VK_FALSE; | 
|  | 1325 | rs.depthBiasEnable = VK_FALSE; | 
| Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 1326 |  | 
|  | 1327 | memset(&cb, 0, sizeof(cb)); | 
| Tony Barbour | e307f58 | 2015-07-10 15:29:03 -0600 | [diff] [blame] | 1328 | cb.sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO; | 
|  | 1329 | VkPipelineColorBlendAttachmentState att_state[1]; | 
| Tony Barbour | fa6cac7 | 2015-01-16 14:27:35 -0700 | [diff] [blame] | 1330 | memset(att_state, 0, sizeof(att_state)); | 
| Chia-I Wu | c51b121 | 2015-10-27 19:25:11 +0800 | [diff] [blame] | 1331 | att_state[0].colorWriteMask = 0xf; | 
| Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1332 | att_state[0].blendEnable = VK_FALSE; | 
| Tony Barbour | fa6cac7 | 2015-01-16 14:27:35 -0700 | [diff] [blame] | 1333 | cb.attachmentCount = 1; | 
|  | 1334 | cb.pAttachments = att_state; | 
| Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 1335 |  | 
| Tony Barbour | fa6cac7 | 2015-01-16 14:27:35 -0700 | [diff] [blame] | 1336 | memset(&vp, 0, sizeof(vp)); | 
| Tony Barbour | e307f58 | 2015-07-10 15:29:03 -0600 | [diff] [blame] | 1337 | vp.sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO; | 
| Tony Barbour | 8205d90 | 2015-04-16 15:59:00 -0600 | [diff] [blame] | 1338 | vp.viewportCount = 1; | 
| Karl Schultz | 481756e | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 1339 | dynamicStateEnables[dynamicState.dynamicStateCount++] = | 
|  | 1340 | VK_DYNAMIC_STATE_VIEWPORT; | 
| Piers Daniell | 811eb74 | 2015-09-29 13:01:09 -0600 | [diff] [blame] | 1341 | vp.scissorCount = 1; | 
| Karl Schultz | 481756e | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 1342 | dynamicStateEnables[dynamicState.dynamicStateCount++] = | 
|  | 1343 | VK_DYNAMIC_STATE_SCISSOR; | 
| Tony Barbour | fa6cac7 | 2015-01-16 14:27:35 -0700 | [diff] [blame] | 1344 |  | 
|  | 1345 | memset(&ds, 0, sizeof(ds)); | 
| Tony Barbour | e307f58 | 2015-07-10 15:29:03 -0600 | [diff] [blame] | 1346 | ds.sType = VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO; | 
| Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1347 | ds.depthTestEnable = VK_TRUE; | 
|  | 1348 | ds.depthWriteEnable = VK_TRUE; | 
| Chia-I Wu | 1f85191 | 2015-10-27 18:04:07 +0800 | [diff] [blame] | 1349 | ds.depthCompareOp = VK_COMPARE_OP_LESS_OR_EQUAL; | 
| Cody Northrop | e4bc694 | 2015-08-26 10:01:32 -0600 | [diff] [blame] | 1350 | ds.depthBoundsTestEnable = VK_FALSE; | 
| Chia-I Wu | c51b121 | 2015-10-27 19:25:11 +0800 | [diff] [blame] | 1351 | ds.back.failOp = VK_STENCIL_OP_KEEP; | 
|  | 1352 | ds.back.passOp = VK_STENCIL_OP_KEEP; | 
|  | 1353 | ds.back.compareOp = VK_COMPARE_OP_ALWAYS; | 
| Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1354 | ds.stencilTestEnable = VK_FALSE; | 
| Tony Barbour | fa6cac7 | 2015-01-16 14:27:35 -0700 | [diff] [blame] | 1355 | ds.front = ds.back; | 
| Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 1356 |  | 
| Tony Barbour | fa6cac7 | 2015-01-16 14:27:35 -0700 | [diff] [blame] | 1357 | memset(&ms, 0, sizeof(ms)); | 
| Tony Barbour | e307f58 | 2015-07-10 15:29:03 -0600 | [diff] [blame] | 1358 | ms.sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO; | 
| Cody Northrop | e9825b7 | 2015-08-04 14:34:54 -0600 | [diff] [blame] | 1359 | ms.pSampleMask = NULL; | 
| Chia-I Wu | 3138d6a | 2015-10-31 00:31:16 +0800 | [diff] [blame] | 1360 | ms.rasterizationSamples = VK_SAMPLE_COUNT_1_BIT; | 
| Chia-I Wu | b043fe3 | 2014-10-06 15:30:33 +0800 | [diff] [blame] | 1361 |  | 
| Mark Lobodzinski | 0e0fb5c | 2015-06-23 15:11:57 -0600 | [diff] [blame] | 1362 | // Two stages: vs and fs | 
|  | 1363 | pipeline.stageCount = 2; | 
|  | 1364 | VkPipelineShaderStageCreateInfo shaderStages[2]; | 
|  | 1365 | memset(&shaderStages, 0, 2 * sizeof(VkPipelineShaderStageCreateInfo)); | 
|  | 1366 |  | 
| Karl Schultz | 481756e | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 1367 | shaderStages[0].sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO; | 
|  | 1368 | shaderStages[0].stage = VK_SHADER_STAGE_VERTEX_BIT; | 
| Chia-I Wu | 062ad15 | 2015-10-31 00:31:16 +0800 | [diff] [blame] | 1369 | shaderStages[0].module = demo_prepare_vs(demo); | 
| Karl Schultz | 481756e | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 1370 | shaderStages[0].pName = "main"; | 
| Mark Lobodzinski | 0e0fb5c | 2015-06-23 15:11:57 -0600 | [diff] [blame] | 1371 |  | 
| Karl Schultz | 481756e | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 1372 | shaderStages[1].sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO; | 
|  | 1373 | shaderStages[1].stage = VK_SHADER_STAGE_FRAGMENT_BIT; | 
| Chia-I Wu | 062ad15 | 2015-10-31 00:31:16 +0800 | [diff] [blame] | 1374 | shaderStages[1].module = demo_prepare_fs(demo); | 
| Karl Schultz | 481756e | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 1375 | shaderStages[1].pName = "main"; | 
| Mark Lobodzinski | 0e0fb5c | 2015-06-23 15:11:57 -0600 | [diff] [blame] | 1376 |  | 
| Karl Schultz | 481756e | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 1377 | pipeline.pVertexInputState = &vi; | 
| Tony Barbour | e307f58 | 2015-07-10 15:29:03 -0600 | [diff] [blame] | 1378 | pipeline.pInputAssemblyState = &ia; | 
| Karl Schultz | 481756e | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 1379 | pipeline.pRasterizationState = &rs; | 
|  | 1380 | pipeline.pColorBlendState = &cb; | 
|  | 1381 | pipeline.pMultisampleState = &ms; | 
|  | 1382 | pipeline.pViewportState = &vp; | 
|  | 1383 | pipeline.pDepthStencilState = &ds; | 
|  | 1384 | pipeline.pStages = shaderStages; | 
|  | 1385 | pipeline.renderPass = demo->render_pass; | 
|  | 1386 | pipeline.pDynamicState = &dynamicState; | 
| Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 1387 |  | 
| Jon Ashburn | 0d60d27 | 2015-07-09 15:02:25 -0600 | [diff] [blame] | 1388 | memset(&pipelineCache, 0, sizeof(pipelineCache)); | 
|  | 1389 | pipelineCache.sType = VK_STRUCTURE_TYPE_PIPELINE_CACHE_CREATE_INFO; | 
|  | 1390 |  | 
| Karl Schultz | 481756e | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 1391 | err = vkCreatePipelineCache(demo->device, &pipelineCache, NULL, | 
|  | 1392 | &demo->pipelineCache); | 
| Jon Ashburn | 0d60d27 | 2015-07-09 15:02:25 -0600 | [diff] [blame] | 1393 | assert(!err); | 
| Karl Schultz | 481756e | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 1394 | err = vkCreateGraphicsPipelines(demo->device, demo->pipelineCache, 1, | 
|  | 1395 | &pipeline, NULL, &demo->pipeline); | 
| Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 1396 | assert(!err); | 
|  | 1397 |  | 
| Chia-I Wu | 69f4012 | 2015-10-26 21:10:41 +0800 | [diff] [blame] | 1398 | vkDestroyPipelineCache(demo->device, demo->pipelineCache, NULL); | 
| Mark Lobodzinski | 8a19064 | 2015-08-07 10:17:13 -0600 | [diff] [blame] | 1399 |  | 
| Chia-I Wu | 69f4012 | 2015-10-26 21:10:41 +0800 | [diff] [blame] | 1400 | vkDestroyShaderModule(demo->device, demo->frag_shader_module, NULL); | 
|  | 1401 | vkDestroyShaderModule(demo->device, demo->vert_shader_module, NULL); | 
| Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 1402 | } | 
|  | 1403 |  | 
| Karl Schultz | 481756e | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 1404 | static void demo_prepare_descriptor_pool(struct demo *demo) { | 
| Chia-I Wu | c51b121 | 2015-10-27 19:25:11 +0800 | [diff] [blame] | 1405 | const VkDescriptorPoolSize type_count = { | 
| Courtney Goeltzenleuchter | ad87081 | 2015-04-15 15:29:59 -0600 | [diff] [blame] | 1406 | .type = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, | 
| Chia-I Wu | 763a749 | 2015-10-26 20:48:51 +0800 | [diff] [blame] | 1407 | .descriptorCount = DEMO_TEXTURE_COUNT, | 
| Chia-I Wu | f838506 | 2015-01-04 16:27:24 +0800 | [diff] [blame] | 1408 | }; | 
| Courtney Goeltzenleuchter | 382489d | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 1409 | const VkDescriptorPoolCreateInfo descriptor_pool = { | 
| Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1410 | .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO, | 
| Chia-I Wu | f838506 | 2015-01-04 16:27:24 +0800 | [diff] [blame] | 1411 | .pNext = NULL, | 
| Courtney Goeltzenleuchter | d9e966a | 2015-09-16 16:12:45 -0600 | [diff] [blame] | 1412 | .maxSets = 1, | 
| Chia-I Wu | c51b121 | 2015-10-27 19:25:11 +0800 | [diff] [blame] | 1413 | .poolSizeCount = 1, | 
|  | 1414 | .pPoolSizes = &type_count, | 
| Chia-I Wu | f838506 | 2015-01-04 16:27:24 +0800 | [diff] [blame] | 1415 | }; | 
| Tony Barbour | 22a3086 | 2015-04-22 09:02:32 -0600 | [diff] [blame] | 1416 | VkResult U_ASSERT_ONLY err; | 
| Chia-I Wu | f838506 | 2015-01-04 16:27:24 +0800 | [diff] [blame] | 1417 |  | 
| Karl Schultz | 481756e | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 1418 | err = vkCreateDescriptorPool(demo->device, &descriptor_pool, NULL, | 
|  | 1419 | &demo->desc_pool); | 
| Chia-I Wu | f838506 | 2015-01-04 16:27:24 +0800 | [diff] [blame] | 1420 | assert(!err); | 
|  | 1421 | } | 
|  | 1422 |  | 
| Karl Schultz | 481756e | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 1423 | static void demo_prepare_descriptor_set(struct demo *demo) { | 
| Courtney Goeltzenleuchter | 34aa5c8 | 2015-10-23 13:38:14 -0600 | [diff] [blame] | 1424 | VkDescriptorImageInfo tex_descs[DEMO_TEXTURE_COUNT]; | 
| Chia-I Wu | 8cd8ecd | 2015-05-25 16:27:55 +0800 | [diff] [blame] | 1425 | VkWriteDescriptorSet write; | 
| Tony Barbour | 22a3086 | 2015-04-22 09:02:32 -0600 | [diff] [blame] | 1426 | VkResult U_ASSERT_ONLY err; | 
| Mark Lobodzinski | e2d07a5 | 2015-01-29 08:55:56 -0600 | [diff] [blame] | 1427 | uint32_t i; | 
| Chia-I Wu | f838506 | 2015-01-04 16:27:24 +0800 | [diff] [blame] | 1428 |  | 
| Chia-I Wu | 1f85191 | 2015-10-27 18:04:07 +0800 | [diff] [blame] | 1429 | VkDescriptorSetAllocateInfo alloc_info = { | 
| Chia-I Wu | c1f5e40 | 2015-11-10 16:21:09 +0800 | [diff] [blame] | 1430 | .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO, | 
| Courtney Goeltzenleuchter | 831c183 | 2015-10-23 14:21:05 -0600 | [diff] [blame] | 1431 | .pNext = NULL, | 
|  | 1432 | .descriptorPool = demo->desc_pool, | 
| Jon Ashburn | a4ae48b | 2016-01-11 13:12:43 -0700 | [diff] [blame] | 1433 | .descriptorSetCount = 1, | 
| Karl Schultz | 481756e | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 1434 | .pSetLayouts = &demo->desc_layout}; | 
| Chia-I Wu | 1f85191 | 2015-10-27 18:04:07 +0800 | [diff] [blame] | 1435 | err = vkAllocateDescriptorSets(demo->device, &alloc_info, &demo->desc_set); | 
| Cody Northrop | c8aa4a5 | 2015-08-03 12:47:29 -0600 | [diff] [blame] | 1436 | assert(!err); | 
| Chia-I Wu | f838506 | 2015-01-04 16:27:24 +0800 | [diff] [blame] | 1437 |  | 
| Chia-I Wu | 8cd8ecd | 2015-05-25 16:27:55 +0800 | [diff] [blame] | 1438 | memset(&tex_descs, 0, sizeof(tex_descs)); | 
|  | 1439 | for (i = 0; i < DEMO_TEXTURE_COUNT; i++) { | 
| Courtney Goeltzenleuchter | 34aa5c8 | 2015-10-23 13:38:14 -0600 | [diff] [blame] | 1440 | tex_descs[i].sampler = demo->textures[i].sampler; | 
|  | 1441 | tex_descs[i].imageView = demo->textures[i].view; | 
|  | 1442 | tex_descs[i].imageLayout = VK_IMAGE_LAYOUT_GENERAL; | 
| Chia-I Wu | 8cd8ecd | 2015-05-25 16:27:55 +0800 | [diff] [blame] | 1443 | } | 
|  | 1444 |  | 
|  | 1445 | memset(&write, 0, sizeof(write)); | 
|  | 1446 | write.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET; | 
| Chia-I Wu | 1f85191 | 2015-10-27 18:04:07 +0800 | [diff] [blame] | 1447 | write.dstSet = demo->desc_set; | 
| Chia-I Wu | 763a749 | 2015-10-26 20:48:51 +0800 | [diff] [blame] | 1448 | write.descriptorCount = DEMO_TEXTURE_COUNT; | 
| Chia-I Wu | 8cd8ecd | 2015-05-25 16:27:55 +0800 | [diff] [blame] | 1449 | write.descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER; | 
| Courtney Goeltzenleuchter | 34aa5c8 | 2015-10-23 13:38:14 -0600 | [diff] [blame] | 1450 | write.pImageInfo = tex_descs; | 
| Chia-I Wu | 8cd8ecd | 2015-05-25 16:27:55 +0800 | [diff] [blame] | 1451 |  | 
| Mark Lobodzinski | 67b42b7 | 2015-09-07 13:59:43 -0600 | [diff] [blame] | 1452 | vkUpdateDescriptorSets(demo->device, 1, &write, 0, NULL); | 
| Chia-I Wu | f838506 | 2015-01-04 16:27:24 +0800 | [diff] [blame] | 1453 | } | 
|  | 1454 |  | 
| Karl Schultz | 481756e | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 1455 | static void demo_prepare_framebuffers(struct demo *demo) { | 
| Courtney Goeltzenleuchter | 1856d6f | 2015-09-01 17:30:39 -0600 | [diff] [blame] | 1456 | VkImageView attachments[2]; | 
| Cody Northrop | f110c6e | 2015-08-04 10:47:08 -0600 | [diff] [blame] | 1457 | attachments[1] = demo->depth.view; | 
|  | 1458 |  | 
| Chia-I Wu | 76cd422 | 2015-07-08 13:34:24 +0800 | [diff] [blame] | 1459 | const VkFramebufferCreateInfo fb_info = { | 
| Karl Schultz | 481756e | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 1460 | .sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO, | 
|  | 1461 | .pNext = NULL, | 
|  | 1462 | .renderPass = demo->render_pass, | 
|  | 1463 | .attachmentCount = 2, | 
|  | 1464 | .pAttachments = attachments, | 
|  | 1465 | .width = demo->width, | 
|  | 1466 | .height = demo->height, | 
|  | 1467 | .layers = 1, | 
| Chia-I Wu | 76cd422 | 2015-07-08 13:34:24 +0800 | [diff] [blame] | 1468 | }; | 
|  | 1469 | VkResult U_ASSERT_ONLY err; | 
|  | 1470 | uint32_t i; | 
|  | 1471 |  | 
| Karl Schultz | 481756e | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 1472 | demo->framebuffers = (VkFramebuffer *)malloc(demo->swapchainImageCount * | 
|  | 1473 | sizeof(VkFramebuffer)); | 
| Tony Barbour | 5aabff5 | 2015-10-08 14:26:24 -0600 | [diff] [blame] | 1474 | assert(demo->framebuffers); | 
|  | 1475 |  | 
|  | 1476 | for (i = 0; i < demo->swapchainImageCount; i++) { | 
| Karl Schultz | 481756e | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 1477 | attachments[0] = demo->buffers[i].view; | 
|  | 1478 | err = vkCreateFramebuffer(demo->device, &fb_info, NULL, | 
|  | 1479 | &demo->framebuffers[i]); | 
| Chia-I Wu | 76cd422 | 2015-07-08 13:34:24 +0800 | [diff] [blame] | 1480 | assert(!err); | 
|  | 1481 | } | 
|  | 1482 | } | 
|  | 1483 |  | 
| Karl Schultz | 481756e | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 1484 | static void demo_prepare(struct demo *demo) { | 
| Cody Northrop | 18ea11b | 2015-07-09 18:08:32 -0600 | [diff] [blame] | 1485 | VkResult U_ASSERT_ONLY err; | 
|  | 1486 |  | 
| Chia-I Wu | 1f85191 | 2015-10-27 18:04:07 +0800 | [diff] [blame] | 1487 | const VkCommandPoolCreateInfo cmd_pool_info = { | 
|  | 1488 | .sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO, | 
| Cody Northrop | 18ea11b | 2015-07-09 18:08:32 -0600 | [diff] [blame] | 1489 | .pNext = NULL, | 
|  | 1490 | .queueFamilyIndex = demo->graphics_queue_node_index, | 
| Mark Lobodzinski | ac6b2c7 | 2015-12-14 15:47:36 -0700 | [diff] [blame] | 1491 | .flags = VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT, | 
| Cody Northrop | 18ea11b | 2015-07-09 18:08:32 -0600 | [diff] [blame] | 1492 | }; | 
| Karl Schultz | 481756e | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 1493 | err = vkCreateCommandPool(demo->device, &cmd_pool_info, NULL, | 
|  | 1494 | &demo->cmd_pool); | 
| Cody Northrop | 18ea11b | 2015-07-09 18:08:32 -0600 | [diff] [blame] | 1495 | assert(!err); | 
|  | 1496 |  | 
| Chia-I Wu | 1f85191 | 2015-10-27 18:04:07 +0800 | [diff] [blame] | 1497 | const VkCommandBufferAllocateInfo cmd = { | 
| Chia-I Wu | c1f5e40 | 2015-11-10 16:21:09 +0800 | [diff] [blame] | 1498 | .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO, | 
| Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 1499 | .pNext = NULL, | 
| Chia-I Wu | 1f85191 | 2015-10-27 18:04:07 +0800 | [diff] [blame] | 1500 | .commandPool = demo->cmd_pool, | 
|  | 1501 | .level = VK_COMMAND_BUFFER_LEVEL_PRIMARY, | 
| Jon Ashburn | a4ae48b | 2016-01-11 13:12:43 -0700 | [diff] [blame] | 1502 | .commandBufferCount = 1, | 
| Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 1503 | }; | 
| Chia-I Wu | 1f85191 | 2015-10-27 18:04:07 +0800 | [diff] [blame] | 1504 | err = vkAllocateCommandBuffers(demo->device, &cmd, &demo->draw_cmd); | 
| Courtney Goeltzenleuchter | 633f9c5 | 2015-04-30 10:58:33 -0600 | [diff] [blame] | 1505 | assert(!err); | 
|  | 1506 |  | 
| Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 1507 | demo_prepare_buffers(demo); | 
| Chia-I Wu | 9ae87c9 | 2014-10-07 14:15:01 +0800 | [diff] [blame] | 1508 | demo_prepare_depth(demo); | 
| Chia-I Wu | b043fe3 | 2014-10-06 15:30:33 +0800 | [diff] [blame] | 1509 | demo_prepare_textures(demo); | 
| Chia-I Wu | 99621bc | 2014-10-08 11:52:22 +0800 | [diff] [blame] | 1510 | demo_prepare_vertices(demo); | 
| Chia-I Wu | f838506 | 2015-01-04 16:27:24 +0800 | [diff] [blame] | 1511 | demo_prepare_descriptor_layout(demo); | 
| Chia-I Wu | 76cd422 | 2015-07-08 13:34:24 +0800 | [diff] [blame] | 1512 | demo_prepare_render_pass(demo); | 
| Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 1513 | demo_prepare_pipeline(demo); | 
| Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 1514 |  | 
| Chia-I Wu | 8d24b3b | 2015-03-26 13:14:16 +0800 | [diff] [blame] | 1515 | demo_prepare_descriptor_pool(demo); | 
| Chia-I Wu | f838506 | 2015-01-04 16:27:24 +0800 | [diff] [blame] | 1516 | demo_prepare_descriptor_set(demo); | 
| Chia-I Wu | 76cd422 | 2015-07-08 13:34:24 +0800 | [diff] [blame] | 1517 |  | 
|  | 1518 | demo_prepare_framebuffers(demo); | 
|  | 1519 |  | 
| Courtney Goeltzenleuchter | 633f9c5 | 2015-04-30 10:58:33 -0600 | [diff] [blame] | 1520 | demo->prepared = true; | 
| Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 1521 | } | 
|  | 1522 |  | 
| Ian Elliott | e14e9f9 | 2015-04-16 15:23:05 -0600 | [diff] [blame] | 1523 | #ifdef _WIN32 | 
| Karl Schultz | 481756e | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 1524 | static void demo_run(struct demo *demo) { | 
| Courtney Goeltzenleuchter | 633f9c5 | 2015-04-30 10:58:33 -0600 | [diff] [blame] | 1525 | if (!demo->prepared) | 
|  | 1526 | return; | 
| Ian Elliott | e14e9f9 | 2015-04-16 15:23:05 -0600 | [diff] [blame] | 1527 | demo_draw(demo); | 
| Tony Barbour | 4a6692d | 2015-10-08 13:45:45 -0600 | [diff] [blame] | 1528 |  | 
|  | 1529 | if (demo->depthStencil > 0.99f) | 
|  | 1530 | demo->depthIncrement = -0.001f; | 
|  | 1531 | if (demo->depthStencil < 0.8f) | 
|  | 1532 | demo->depthIncrement = 0.001f; | 
|  | 1533 |  | 
|  | 1534 | demo->depthStencil += demo->depthIncrement; | 
| Karl Schultz | 501fdc2 | 2016-03-24 18:43:41 -0600 | [diff] [blame] | 1535 |  | 
|  | 1536 | demo->curFrame++; | 
|  | 1537 | if (demo->frameCount != INT32_MAX && demo->curFrame == demo->frameCount) { | 
|  | 1538 | PostQuitMessage(validation_error); | 
|  | 1539 | } | 
| Ian Elliott | e14e9f9 | 2015-04-16 15:23:05 -0600 | [diff] [blame] | 1540 | } | 
|  | 1541 |  | 
|  | 1542 | // On MS-Windows, make this a global, so it's available to WndProc() | 
|  | 1543 | struct demo demo; | 
|  | 1544 |  | 
|  | 1545 | // MS-Windows event handling function: | 
| Karl Schultz | 481756e | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 1546 | LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) { | 
| Ian Elliott | 4e19ed0 | 2015-04-28 10:52:52 -0600 | [diff] [blame] | 1547 | char tmp_str[] = APP_LONG_NAME; | 
| Ian Elliott | e14e9f9 | 2015-04-16 15:23:05 -0600 | [diff] [blame] | 1548 |  | 
| Karl Schultz | 481756e | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 1549 | switch (uMsg) { | 
| Ian Elliott | e36b208 | 2015-07-06 14:27:58 -0600 | [diff] [blame] | 1550 | case WM_CREATE: | 
| Ian Elliott | e14e9f9 | 2015-04-16 15:23:05 -0600 | [diff] [blame] | 1551 | return 0; | 
| Ian Elliott | e36b208 | 2015-07-06 14:27:58 -0600 | [diff] [blame] | 1552 | case WM_CLOSE: | 
| Karl Schultz | 501fdc2 | 2016-03-24 18:43:41 -0600 | [diff] [blame] | 1553 | PostQuitMessage(validation_error); | 
| Ian Elliott | e14e9f9 | 2015-04-16 15:23:05 -0600 | [diff] [blame] | 1554 | return 0; | 
| Ian Elliott | e36b208 | 2015-07-06 14:27:58 -0600 | [diff] [blame] | 1555 | case WM_PAINT: | 
| Cody Northrop | 67ff0a4 | 2015-09-09 10:21:49 -0600 | [diff] [blame] | 1556 | if (demo.prepared) { | 
|  | 1557 | demo_run(&demo); | 
| Tony Barbour | 18b53e7 | 2015-10-20 12:49:46 -0600 | [diff] [blame] | 1558 | break; | 
| Cody Northrop | 67ff0a4 | 2015-09-09 10:21:49 -0600 | [diff] [blame] | 1559 | } | 
| Ian Elliott | f3f8082 | 2015-10-21 15:14:07 -0600 | [diff] [blame] | 1560 | case WM_SIZE: | 
| Piers Daniell | fdd5af6 | 2016-02-18 10:54:15 -0700 | [diff] [blame] | 1561 | // Resize the application to the new window size, except when | 
|  | 1562 | // it was minimized. Vulkan doesn't support images or swapchains | 
|  | 1563 | // with width=0 and height=0. | 
|  | 1564 | if (wParam != SIZE_MINIMIZED) { | 
|  | 1565 | demo.width = lParam & 0xffff; | 
|  | 1566 | demo.height = lParam & 0xffff0000 >> 16; | 
|  | 1567 | demo_resize(&demo); | 
|  | 1568 | } | 
| Ian Elliott | f3f8082 | 2015-10-21 15:14:07 -0600 | [diff] [blame] | 1569 | break; | 
| Ian Elliott | e14e9f9 | 2015-04-16 15:23:05 -0600 | [diff] [blame] | 1570 | default: | 
|  | 1571 | break; | 
|  | 1572 | } | 
|  | 1573 | return (DefWindowProc(hWnd, uMsg, wParam, lParam)); | 
|  | 1574 | } | 
|  | 1575 |  | 
| Karl Schultz | 481756e | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 1576 | static void demo_create_window(struct demo *demo) { | 
|  | 1577 | WNDCLASSEX win_class; | 
| Ian Elliott | e14e9f9 | 2015-04-16 15:23:05 -0600 | [diff] [blame] | 1578 |  | 
|  | 1579 | // Initialize the window class structure: | 
|  | 1580 | win_class.cbSize = sizeof(WNDCLASSEX); | 
|  | 1581 | win_class.style = CS_HREDRAW | CS_VREDRAW; | 
|  | 1582 | win_class.lpfnWndProc = WndProc; | 
|  | 1583 | win_class.cbClsExtra = 0; | 
|  | 1584 | win_class.cbWndExtra = 0; | 
|  | 1585 | win_class.hInstance = demo->connection; // hInstance | 
|  | 1586 | win_class.hIcon = LoadIcon(NULL, IDI_APPLICATION); | 
|  | 1587 | win_class.hCursor = LoadCursor(NULL, IDC_ARROW); | 
|  | 1588 | win_class.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH); | 
|  | 1589 | win_class.lpszMenuName = NULL; | 
|  | 1590 | win_class.lpszClassName = demo->name; | 
|  | 1591 | win_class.hIconSm = LoadIcon(NULL, IDI_WINLOGO); | 
|  | 1592 | // Register window class: | 
|  | 1593 | if (!RegisterClassEx(&win_class)) { | 
|  | 1594 | // It didn't work, so try to give a useful error: | 
|  | 1595 | printf("Unexpected error trying to start the application!\n"); | 
|  | 1596 | fflush(stdout); | 
|  | 1597 | exit(1); | 
|  | 1598 | } | 
|  | 1599 | // Create window with the registered class: | 
| Karl Schultz | 481756e | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 1600 | RECT wr = {0, 0, demo->width, demo->height}; | 
| Mike Stroyan | 7eef574 | 2015-06-15 14:19:19 -0600 | [diff] [blame] | 1601 | AdjustWindowRect(&wr, WS_OVERLAPPEDWINDOW, FALSE); | 
| Ian Elliott | e14e9f9 | 2015-04-16 15:23:05 -0600 | [diff] [blame] | 1602 | demo->window = CreateWindowEx(0, | 
|  | 1603 | demo->name,           // class name | 
|  | 1604 | demo->name,           // app name | 
|  | 1605 | WS_OVERLAPPEDWINDOW | // window style | 
| Karl Schultz | 481756e | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 1606 | WS_VISIBLE | WS_SYSMENU, | 
|  | 1607 | 100, 100,           // x/y coords | 
|  | 1608 | wr.right - wr.left, // width | 
|  | 1609 | wr.bottom - wr.top, // height | 
|  | 1610 | NULL,               // handle to parent | 
|  | 1611 | NULL,               // handle to menu | 
|  | 1612 | demo->connection,   // hInstance | 
|  | 1613 | NULL);              // no extra parameters | 
| Ian Elliott | e14e9f9 | 2015-04-16 15:23:05 -0600 | [diff] [blame] | 1614 | if (!demo->window) { | 
|  | 1615 | // It didn't work, so try to give a useful error: | 
|  | 1616 | printf("Cannot create a window in which to draw!\n"); | 
|  | 1617 | fflush(stdout); | 
|  | 1618 | exit(1); | 
|  | 1619 | } | 
|  | 1620 | } | 
|  | 1621 | #else  // _WIN32 | 
|  | 1622 |  | 
| Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 1623 | static void demo_handle_event(struct demo *demo, | 
| Karl Schultz | 481756e | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 1624 | const xcb_generic_event_t *event) { | 
| Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 1625 | switch (event->response_type & 0x7f) { | 
|  | 1626 | case XCB_EXPOSE: | 
|  | 1627 | demo_draw(demo); | 
|  | 1628 | break; | 
| Courtney Goeltzenleuchter | ca69805 | 2014-11-07 15:17:03 -0700 | [diff] [blame] | 1629 | case XCB_CLIENT_MESSAGE: | 
| Karl Schultz | 481756e | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 1630 | if ((*(xcb_client_message_event_t *)event).data.data32[0] == | 
|  | 1631 | (*demo->atom_wm_delete_window).atom) { | 
| Courtney Goeltzenleuchter | ca69805 | 2014-11-07 15:17:03 -0700 | [diff] [blame] | 1632 | demo->quit = true; | 
|  | 1633 | } | 
|  | 1634 | break; | 
| Karl Schultz | 481756e | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 1635 | case XCB_KEY_RELEASE: { | 
|  | 1636 | const xcb_key_release_event_t *key = | 
|  | 1637 | (const xcb_key_release_event_t *)event; | 
| Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 1638 |  | 
| Karl Schultz | 481756e | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 1639 | if (key->detail == 0x9) | 
|  | 1640 | demo->quit = true; | 
|  | 1641 | } break; | 
| Courtney Goeltzenleuchter | ca69805 | 2014-11-07 15:17:03 -0700 | [diff] [blame] | 1642 | case XCB_DESTROY_NOTIFY: | 
|  | 1643 | demo->quit = true; | 
|  | 1644 | break; | 
| Karl Schultz | 481756e | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 1645 | case XCB_CONFIGURE_NOTIFY: { | 
|  | 1646 | const xcb_configure_notify_event_t *cfg = | 
|  | 1647 | (const xcb_configure_notify_event_t *)event; | 
|  | 1648 | if ((demo->width != cfg->width) || (demo->height != cfg->height)) { | 
| Damien Leone | 3d77c30 | 2016-01-26 14:34:12 -0800 | [diff] [blame] | 1649 | demo->width = cfg->width; | 
|  | 1650 | demo->height = cfg->height; | 
| Karl Schultz | 481756e | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 1651 | demo_resize(demo); | 
| Ian Elliott | e2688a5 | 2015-10-16 18:02:43 -0600 | [diff] [blame] | 1652 | } | 
| Karl Schultz | 481756e | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 1653 | } break; | 
| Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 1654 | default: | 
|  | 1655 | break; | 
|  | 1656 | } | 
|  | 1657 | } | 
|  | 1658 |  | 
| Karl Schultz | 481756e | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 1659 | static void demo_run(struct demo *demo) { | 
| Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 1660 | xcb_flush(demo->connection); | 
|  | 1661 |  | 
|  | 1662 | while (!demo->quit) { | 
|  | 1663 | xcb_generic_event_t *event; | 
|  | 1664 |  | 
| Tony Barbour | 4a6692d | 2015-10-08 13:45:45 -0600 | [diff] [blame] | 1665 | event = xcb_poll_for_event(demo->connection); | 
| Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 1666 | if (event) { | 
|  | 1667 | demo_handle_event(demo, event); | 
|  | 1668 | free(event); | 
|  | 1669 | } | 
| Tony Barbour | 4a6692d | 2015-10-08 13:45:45 -0600 | [diff] [blame] | 1670 |  | 
|  | 1671 | demo_draw(demo); | 
|  | 1672 |  | 
|  | 1673 | if (demo->depthStencil > 0.99f) | 
|  | 1674 | demo->depthIncrement = -0.001f; | 
|  | 1675 | if (demo->depthStencil < 0.8f) | 
|  | 1676 | demo->depthIncrement = 0.001f; | 
|  | 1677 |  | 
|  | 1678 | demo->depthStencil += demo->depthIncrement; | 
|  | 1679 |  | 
|  | 1680 | // Wait for work to finish before updating MVP. | 
|  | 1681 | vkDeviceWaitIdle(demo->device); | 
| Karl Schultz | 501fdc2 | 2016-03-24 18:43:41 -0600 | [diff] [blame] | 1682 | demo->curFrame++; | 
|  | 1683 | if (demo->frameCount != INT32_MAX && demo->curFrame == demo->frameCount) | 
|  | 1684 | demo->quit = true; | 
| Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 1685 | } | 
|  | 1686 | } | 
|  | 1687 |  | 
| Karl Schultz | 481756e | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 1688 | static void demo_create_window(struct demo *demo) { | 
| Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 1689 | uint32_t value_mask, value_list[32]; | 
|  | 1690 |  | 
|  | 1691 | demo->window = xcb_generate_id(demo->connection); | 
|  | 1692 |  | 
|  | 1693 | value_mask = XCB_CW_BACK_PIXEL | XCB_CW_EVENT_MASK; | 
|  | 1694 | value_list[0] = demo->screen->black_pixel; | 
| Karl Schultz | 481756e | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 1695 | value_list[1] = XCB_EVENT_MASK_KEY_RELEASE | XCB_EVENT_MASK_EXPOSURE | | 
| Courtney Goeltzenleuchter | ca69805 | 2014-11-07 15:17:03 -0700 | [diff] [blame] | 1696 | XCB_EVENT_MASK_STRUCTURE_NOTIFY; | 
| Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 1697 |  | 
| Karl Schultz | 481756e | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 1698 | xcb_create_window(demo->connection, XCB_COPY_FROM_PARENT, demo->window, | 
|  | 1699 | demo->screen->root, 0, 0, demo->width, demo->height, 0, | 
|  | 1700 | XCB_WINDOW_CLASS_INPUT_OUTPUT, demo->screen->root_visual, | 
|  | 1701 | value_mask, value_list); | 
| Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 1702 |  | 
| Courtney Goeltzenleuchter | ca69805 | 2014-11-07 15:17:03 -0700 | [diff] [blame] | 1703 | /* Magic code that will send notification when window is destroyed */ | 
| Karl Schultz | 481756e | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 1704 | xcb_intern_atom_cookie_t cookie = | 
|  | 1705 | xcb_intern_atom(demo->connection, 1, 12, "WM_PROTOCOLS"); | 
|  | 1706 | xcb_intern_atom_reply_t *reply = | 
|  | 1707 | xcb_intern_atom_reply(demo->connection, cookie, 0); | 
| Courtney Goeltzenleuchter | ca69805 | 2014-11-07 15:17:03 -0700 | [diff] [blame] | 1708 |  | 
| Karl Schultz | 481756e | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 1709 | xcb_intern_atom_cookie_t cookie2 = | 
|  | 1710 | xcb_intern_atom(demo->connection, 0, 16, "WM_DELETE_WINDOW"); | 
|  | 1711 | demo->atom_wm_delete_window = | 
|  | 1712 | xcb_intern_atom_reply(demo->connection, cookie2, 0); | 
| Courtney Goeltzenleuchter | ca69805 | 2014-11-07 15:17:03 -0700 | [diff] [blame] | 1713 |  | 
| Karl Schultz | 481756e | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 1714 | xcb_change_property(demo->connection, XCB_PROP_MODE_REPLACE, demo->window, | 
|  | 1715 | (*reply).atom, 4, 32, 1, | 
| Courtney Goeltzenleuchter | ca69805 | 2014-11-07 15:17:03 -0700 | [diff] [blame] | 1716 | &(*demo->atom_wm_delete_window).atom); | 
|  | 1717 | free(reply); | 
|  | 1718 |  | 
| Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 1719 | xcb_map_window(demo->connection, demo->window); | 
|  | 1720 | } | 
| Ian Elliott | e14e9f9 | 2015-04-16 15:23:05 -0600 | [diff] [blame] | 1721 | #endif // _WIN32 | 
| Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 1722 |  | 
| Ian Elliott | e36b208 | 2015-07-06 14:27:58 -0600 | [diff] [blame] | 1723 | /* | 
|  | 1724 | * Return 1 (true) if all layer names specified in check_names | 
|  | 1725 | * can be found in given layer properties. | 
|  | 1726 | */ | 
|  | 1727 | static VkBool32 demo_check_layers(uint32_t check_count, char **check_names, | 
| Karl Schultz | 481756e | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 1728 | uint32_t layer_count, | 
|  | 1729 | VkLayerProperties *layers) { | 
| Ian Elliott | e36b208 | 2015-07-06 14:27:58 -0600 | [diff] [blame] | 1730 | for (uint32_t i = 0; i < check_count; i++) { | 
|  | 1731 | VkBool32 found = 0; | 
|  | 1732 | for (uint32_t j = 0; j < layer_count; j++) { | 
|  | 1733 | if (!strcmp(check_names[i], layers[j].layerName)) { | 
|  | 1734 | found = 1; | 
| Dustin Graves | beebf7d | 2016-01-27 13:48:06 -0700 | [diff] [blame] | 1735 | break; | 
| Ian Elliott | e36b208 | 2015-07-06 14:27:58 -0600 | [diff] [blame] | 1736 | } | 
|  | 1737 | } | 
|  | 1738 | if (!found) { | 
|  | 1739 | fprintf(stderr, "Cannot find layer: %s\n", check_names[i]); | 
|  | 1740 | return 0; | 
|  | 1741 | } | 
|  | 1742 | } | 
|  | 1743 | return 1; | 
|  | 1744 | } | 
| Derrick Owens | f83966f | 2016-01-21 14:46:16 -0500 | [diff] [blame] | 1745 |  | 
| Karl Schultz | 481756e | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 1746 | static void demo_init_vk(struct demo *demo) { | 
| Tobin Ehlis | 3536b44 | 2015-04-16 18:04:57 -0600 | [diff] [blame] | 1747 | VkResult err; | 
| Courtney Goeltzenleuchter | 1c7c65d | 2015-06-10 17:39:03 -0600 | [diff] [blame] | 1748 | uint32_t instance_extension_count = 0; | 
| Courtney Goeltzenleuchter | 18061cd | 2015-06-29 15:39:26 -0600 | [diff] [blame] | 1749 | uint32_t instance_layer_count = 0; | 
| Tony Barbour | 3d03a4a | 2016-01-22 14:36:40 -0700 | [diff] [blame] | 1750 | uint32_t device_validation_layer_count = 0; | 
| Jon Ashburn | 99ea74d | 2016-02-19 15:23:41 -0700 | [diff] [blame] | 1751 | char **instance_validation_layers = NULL; | 
| Tony Barbour | 3d03a4a | 2016-01-22 14:36:40 -0700 | [diff] [blame] | 1752 | demo->enabled_extension_count = 0; | 
|  | 1753 | demo->enabled_layer_count = 0; | 
| Courtney Goeltzenleuchter | 18061cd | 2015-06-29 15:39:26 -0600 | [diff] [blame] | 1754 |  | 
| Jon Ashburn | 99ea74d | 2016-02-19 15:23:41 -0700 | [diff] [blame] | 1755 | char *instance_validation_layers_alt1[] = { | 
|  | 1756 | "VK_LAYER_LUNARG_standard_validation" | 
| Ian Elliott | e36b208 | 2015-07-06 14:27:58 -0600 | [diff] [blame] | 1757 | }; | 
|  | 1758 |  | 
| Jon Ashburn | 99ea74d | 2016-02-19 15:23:41 -0700 | [diff] [blame] | 1759 | char *instance_validation_layers_alt2[] = { | 
| Mark Lobodzinski | 1c33357 | 2016-03-17 15:08:18 -0600 | [diff] [blame] | 1760 | "VK_LAYER_GOOGLE_threading",     "VK_LAYER_LUNARG_parameter_validation", | 
| Jon Ashburn | 99ea74d | 2016-02-19 15:23:41 -0700 | [diff] [blame] | 1761 | "VK_LAYER_LUNARG_device_limits", "VK_LAYER_LUNARG_object_tracker", | 
| Tobin Ehlis | 5b5e7bc | 2016-03-09 16:12:48 -0700 | [diff] [blame] | 1762 | "VK_LAYER_LUNARG_image",         "VK_LAYER_LUNARG_core_validation", | 
|  | 1763 | "VK_LAYER_LUNARG_swapchain",     "VK_LAYER_GOOGLE_unique_objects" | 
| Jon Ashburn | 99ea74d | 2016-02-19 15:23:41 -0700 | [diff] [blame] | 1764 | }; | 
| Ian Elliott | e36b208 | 2015-07-06 14:27:58 -0600 | [diff] [blame] | 1765 |  | 
| Courtney Goeltzenleuchter | 18061cd | 2015-06-29 15:39:26 -0600 | [diff] [blame] | 1766 | /* Look for validation layers */ | 
| Courtney Goeltzenleuchter | 1f41f54 | 2015-07-09 11:44:38 -0600 | [diff] [blame] | 1767 | VkBool32 validation_found = 0; | 
| Jon Ashburn | 99ea74d | 2016-02-19 15:23:41 -0700 | [diff] [blame] | 1768 | if (demo->validate) { | 
| Tobin Ehlis | 3536b44 | 2015-04-16 18:04:57 -0600 | [diff] [blame] | 1769 |  | 
| Jon Ashburn | 99ea74d | 2016-02-19 15:23:41 -0700 | [diff] [blame] | 1770 | err = vkEnumerateInstanceLayerProperties(&instance_layer_count, NULL); | 
| Dustin Graves | beebf7d | 2016-01-27 13:48:06 -0700 | [diff] [blame] | 1771 | assert(!err); | 
| Ian Elliott | e36b208 | 2015-07-06 14:27:58 -0600 | [diff] [blame] | 1772 |  | 
| Jon Ashburn | 99ea74d | 2016-02-19 15:23:41 -0700 | [diff] [blame] | 1773 | instance_validation_layers = instance_validation_layers_alt1; | 
|  | 1774 | if (instance_layer_count > 0) { | 
|  | 1775 | VkLayerProperties *instance_layers = | 
|  | 1776 | malloc(sizeof (VkLayerProperties) * instance_layer_count); | 
|  | 1777 | err = vkEnumerateInstanceLayerProperties(&instance_layer_count, | 
|  | 1778 | instance_layers); | 
|  | 1779 | assert(!err); | 
|  | 1780 |  | 
|  | 1781 |  | 
| Dustin Graves | beebf7d | 2016-01-27 13:48:06 -0700 | [diff] [blame] | 1782 | validation_found = demo_check_layers( | 
| Jon Ashburn | 99ea74d | 2016-02-19 15:23:41 -0700 | [diff] [blame] | 1783 | ARRAY_SIZE(instance_validation_layers_alt1), | 
|  | 1784 | instance_validation_layers, instance_layer_count, | 
|  | 1785 | instance_layers); | 
|  | 1786 | if (validation_found) { | 
|  | 1787 | demo->enabled_layer_count = ARRAY_SIZE(instance_validation_layers_alt1); | 
|  | 1788 | demo->device_validation_layers[0] = "VK_LAYER_LUNARG_standard_validation"; | 
|  | 1789 | device_validation_layer_count = 1; | 
|  | 1790 | } else { | 
|  | 1791 | // use alternative set of validation layers | 
|  | 1792 | instance_validation_layers = instance_validation_layers_alt2; | 
|  | 1793 | demo->enabled_layer_count = ARRAY_SIZE(instance_validation_layers_alt2); | 
|  | 1794 | validation_found = demo_check_layers( | 
|  | 1795 | ARRAY_SIZE(instance_validation_layers_alt2), | 
|  | 1796 | instance_validation_layers, instance_layer_count, | 
|  | 1797 | instance_layers); | 
|  | 1798 | device_validation_layer_count = | 
|  | 1799 | ARRAY_SIZE(instance_validation_layers_alt2); | 
|  | 1800 | for (uint32_t i = 0; i < device_validation_layer_count; i++) { | 
|  | 1801 | demo->device_validation_layers[i] = | 
|  | 1802 | instance_validation_layers[i]; | 
|  | 1803 | } | 
|  | 1804 | } | 
|  | 1805 | free(instance_layers); | 
| Courtney Goeltzenleuchter | 1c7c65d | 2015-06-10 17:39:03 -0600 | [diff] [blame] | 1806 | } | 
| Dustin Graves | beebf7d | 2016-01-27 13:48:06 -0700 | [diff] [blame] | 1807 |  | 
| Jon Ashburn | 99ea74d | 2016-02-19 15:23:41 -0700 | [diff] [blame] | 1808 | if (!validation_found) { | 
| Karl Schultz | 2a5eaf0 | 2016-04-07 09:40:30 -0600 | [diff] [blame] | 1809 | ERR_EXIT("vkEnumerateInstanceLayerProperties failed to find " | 
| Jon Ashburn | 99ea74d | 2016-02-19 15:23:41 -0700 | [diff] [blame] | 1810 | "required validation layer.\n\n" | 
|  | 1811 | "Please look at the Getting Started guide for additional " | 
|  | 1812 | "information.\n", | 
|  | 1813 | "vkCreateInstance Failure"); | 
|  | 1814 | } | 
| Dustin Graves | beebf7d | 2016-01-27 13:48:06 -0700 | [diff] [blame] | 1815 | } | 
|  | 1816 |  | 
|  | 1817 | /* Look for instance extensions */ | 
|  | 1818 | VkBool32 surfaceExtFound = 0; | 
|  | 1819 | VkBool32 platformSurfaceExtFound = 0; | 
|  | 1820 | memset(demo->extension_names, 0, sizeof(demo->extension_names)); | 
|  | 1821 |  | 
| Karl Schultz | 481756e | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 1822 | err = vkEnumerateInstanceExtensionProperties( | 
|  | 1823 | NULL, &instance_extension_count, NULL); | 
| Courtney Goeltzenleuchter | 18061cd | 2015-06-29 15:39:26 -0600 | [diff] [blame] | 1824 | assert(!err); | 
|  | 1825 |  | 
| Dustin Graves | beebf7d | 2016-01-27 13:48:06 -0700 | [diff] [blame] | 1826 | if (instance_extension_count > 0) { | 
|  | 1827 | VkExtensionProperties *instance_extensions = | 
|  | 1828 | malloc(sizeof(VkExtensionProperties) * instance_extension_count); | 
|  | 1829 | err = vkEnumerateInstanceExtensionProperties( | 
|  | 1830 | NULL, &instance_extension_count, instance_extensions); | 
|  | 1831 | assert(!err); | 
|  | 1832 | for (uint32_t i = 0; i < instance_extension_count; i++) { | 
|  | 1833 | if (!strcmp(VK_KHR_SURFACE_EXTENSION_NAME, | 
|  | 1834 | instance_extensions[i].extensionName)) { | 
|  | 1835 | surfaceExtFound = 1; | 
| Karl Schultz | 481756e | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 1836 | demo->extension_names[demo->enabled_extension_count++] = | 
| Dustin Graves | beebf7d | 2016-01-27 13:48:06 -0700 | [diff] [blame] | 1837 | VK_KHR_SURFACE_EXTENSION_NAME; | 
| Courtney Goeltzenleuchter | 18061cd | 2015-06-29 15:39:26 -0600 | [diff] [blame] | 1838 | } | 
| Dustin Graves | beebf7d | 2016-01-27 13:48:06 -0700 | [diff] [blame] | 1839 | #ifdef _WIN32 | 
|  | 1840 | if (!strcmp(VK_KHR_WIN32_SURFACE_EXTENSION_NAME, | 
|  | 1841 | instance_extensions[i].extensionName)) { | 
|  | 1842 | platformSurfaceExtFound = 1; | 
|  | 1843 | demo->extension_names[demo->enabled_extension_count++] = | 
|  | 1844 | VK_KHR_WIN32_SURFACE_EXTENSION_NAME; | 
|  | 1845 | } | 
|  | 1846 | #else  // _WIN32 | 
|  | 1847 | if (!strcmp(VK_KHR_XCB_SURFACE_EXTENSION_NAME, | 
|  | 1848 | instance_extensions[i].extensionName)) { | 
|  | 1849 | platformSurfaceExtFound = 1; | 
|  | 1850 | demo->extension_names[demo->enabled_extension_count++] = | 
|  | 1851 | VK_KHR_XCB_SURFACE_EXTENSION_NAME; | 
|  | 1852 | } | 
|  | 1853 | #endif // _WIN32 | 
|  | 1854 | if (!strcmp(VK_EXT_DEBUG_REPORT_EXTENSION_NAME, | 
|  | 1855 | instance_extensions[i].extensionName)) { | 
|  | 1856 | if (demo->validate) { | 
|  | 1857 | demo->extension_names[demo->enabled_extension_count++] = | 
|  | 1858 | VK_EXT_DEBUG_REPORT_EXTENSION_NAME; | 
|  | 1859 | } | 
|  | 1860 | } | 
|  | 1861 | assert(demo->enabled_extension_count < 64); | 
| Courtney Goeltzenleuchter | 18061cd | 2015-06-29 15:39:26 -0600 | [diff] [blame] | 1862 | } | 
| Dustin Graves | beebf7d | 2016-01-27 13:48:06 -0700 | [diff] [blame] | 1863 |  | 
|  | 1864 | free(instance_extensions); | 
| Tobin Ehlis | 3536b44 | 2015-04-16 18:04:57 -0600 | [diff] [blame] | 1865 | } | 
| Dustin Graves | beebf7d | 2016-01-27 13:48:06 -0700 | [diff] [blame] | 1866 |  | 
| Ian Elliott | f1d9013 | 2015-11-20 13:08:34 -0700 | [diff] [blame] | 1867 | if (!surfaceExtFound) { | 
| Karl Schultz | 481756e | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 1868 | ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find " | 
|  | 1869 | "the " VK_KHR_SURFACE_EXTENSION_NAME | 
|  | 1870 | " extension.\n\nDo you have a compatible " | 
| Ian Elliott | 3b375cf | 2015-04-28 13:22:33 -0600 | [diff] [blame] | 1871 | "Vulkan installable client driver (ICD) installed?\nPlease " | 
|  | 1872 | "look at the Getting Started guide for additional " | 
|  | 1873 | "information.\n", | 
|  | 1874 | "vkCreateInstance Failure"); | 
|  | 1875 | } | 
| Ian Elliott | f1d9013 | 2015-11-20 13:08:34 -0700 | [diff] [blame] | 1876 | if (!platformSurfaceExtFound) { | 
| Ian Elliott | f1d9013 | 2015-11-20 13:08:34 -0700 | [diff] [blame] | 1877 | #ifdef _WIN32 | 
| Karl Schultz | 481756e | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 1878 | ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find " | 
|  | 1879 | "the " VK_KHR_WIN32_SURFACE_EXTENSION_NAME | 
|  | 1880 | " extension.\n\nDo you have a compatible " | 
|  | 1881 | "Vulkan installable client driver (ICD) installed?\nPlease " | 
|  | 1882 | "look at the Getting Started guide for additional " | 
|  | 1883 | "information.\n", | 
|  | 1884 | "vkCreateInstance Failure"); | 
| Ian Elliott | f1d9013 | 2015-11-20 13:08:34 -0700 | [diff] [blame] | 1885 | #else  // _WIN32 | 
| Karl Schultz | 481756e | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 1886 | ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find " | 
|  | 1887 | "the " VK_KHR_XCB_SURFACE_EXTENSION_NAME | 
|  | 1888 | " extension.\n\nDo you have a compatible " | 
|  | 1889 | "Vulkan installable client driver (ICD) installed?\nPlease " | 
|  | 1890 | "look at the Getting Started guide for additional " | 
|  | 1891 | "information.\n", | 
|  | 1892 | "vkCreateInstance Failure"); | 
| Ian Elliott | f1d9013 | 2015-11-20 13:08:34 -0700 | [diff] [blame] | 1893 | #endif // _WIN32 | 
| Ian Elliott | f1d9013 | 2015-11-20 13:08:34 -0700 | [diff] [blame] | 1894 | } | 
| Courtney Goeltzenleuchter | 382489d | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 1895 | const VkApplicationInfo app = { | 
| Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1896 | .sType = VK_STRUCTURE_TYPE_APPLICATION_INFO, | 
| Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 1897 | .pNext = NULL, | 
| Chia-I Wu | 1f85191 | 2015-10-27 18:04:07 +0800 | [diff] [blame] | 1898 | .pApplicationName = APP_SHORT_NAME, | 
|  | 1899 | .applicationVersion = 0, | 
| Ian Elliott | 4e19ed0 | 2015-04-28 10:52:52 -0600 | [diff] [blame] | 1900 | .pEngineName = APP_SHORT_NAME, | 
| Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 1901 | .engineVersion = 0, | 
| Jon Ashburn | d3995c9 | 2016-03-22 13:57:46 -0600 | [diff] [blame] | 1902 | .apiVersion = VK_API_VERSION_1_0, | 
| Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 1903 | }; | 
| Courtney Goeltzenleuchter | 18061cd | 2015-06-29 15:39:26 -0600 | [diff] [blame] | 1904 | VkInstanceCreateInfo inst_info = { | 
| Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1905 | .sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO, | 
| Jon Ashburn | 29669a4 | 2015-04-04 14:52:07 -0600 | [diff] [blame] | 1906 | .pNext = NULL, | 
| Chia-I Wu | 1f85191 | 2015-10-27 18:04:07 +0800 | [diff] [blame] | 1907 | .pApplicationInfo = &app, | 
| Tony Barbour | 3d03a4a | 2016-01-22 14:36:40 -0700 | [diff] [blame] | 1908 | .enabledLayerCount = demo->enabled_layer_count, | 
| Karl Schultz | 481756e | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 1909 | .ppEnabledLayerNames = (const char *const *)instance_validation_layers, | 
| Tony Barbour | 3d03a4a | 2016-01-22 14:36:40 -0700 | [diff] [blame] | 1910 | .enabledExtensionCount = demo->enabled_extension_count, | 
| Karl Schultz | 481756e | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 1911 | .ppEnabledExtensionNames = (const char *const *)demo->extension_names, | 
| Jon Ashburn | 29669a4 | 2015-04-04 14:52:07 -0600 | [diff] [blame] | 1912 | }; | 
| Tony Barbour | 3d03a4a | 2016-01-22 14:36:40 -0700 | [diff] [blame] | 1913 |  | 
| Mark Lobodzinski | e2d07a5 | 2015-01-29 08:55:56 -0600 | [diff] [blame] | 1914 | uint32_t gpu_count; | 
| Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 1915 |  | 
| Tony Barbour | 3ec16a5 | 2016-03-07 15:48:06 -0700 | [diff] [blame] | 1916 | err = vkCreateInstance(&inst_info, NULL, &demo->inst); | 
| Ian Elliott | caa9f27 | 2015-04-28 11:35:02 -0600 | [diff] [blame] | 1917 | if (err == VK_ERROR_INCOMPATIBLE_DRIVER) { | 
|  | 1918 | ERR_EXIT("Cannot find a compatible Vulkan installable client driver " | 
| Ian Elliott | 3b375cf | 2015-04-28 13:22:33 -0600 | [diff] [blame] | 1919 | "(ICD).\n\nPlease look at the Getting Started guide for " | 
| Ian Elliott | caa9f27 | 2015-04-28 11:35:02 -0600 | [diff] [blame] | 1920 | "additional information.\n", | 
|  | 1921 | "vkCreateInstance Failure"); | 
| Courtney Goeltzenleuchter | ac544f3 | 2015-09-14 18:01:17 -0600 | [diff] [blame] | 1922 | } else if (err == VK_ERROR_EXTENSION_NOT_PRESENT) { | 
| Courtney Goeltzenleuchter | 18061cd | 2015-06-29 15:39:26 -0600 | [diff] [blame] | 1923 | ERR_EXIT("Cannot find a specified extension library" | 
|  | 1924 | ".\nMake sure your layers path is set appropriately\n", | 
|  | 1925 | "vkCreateInstance Failure"); | 
| Ian Elliott | caa9f27 | 2015-04-28 11:35:02 -0600 | [diff] [blame] | 1926 | } else if (err) { | 
| Ian Elliott | 3b375cf | 2015-04-28 13:22:33 -0600 | [diff] [blame] | 1927 | ERR_EXIT("vkCreateInstance failed.\n\nDo you have a compatible Vulkan " | 
|  | 1928 | "installable client driver (ICD) installed?\nPlease look at " | 
| Ian Elliott | caa9f27 | 2015-04-28 11:35:02 -0600 | [diff] [blame] | 1929 | "the Getting Started guide for additional information.\n", | 
|  | 1930 | "vkCreateInstance Failure"); | 
| Ian Elliott | dfe55f7 | 2015-04-03 15:24:55 -0600 | [diff] [blame] | 1931 | } | 
| Jon Ashburn | 29669a4 | 2015-04-04 14:52:07 -0600 | [diff] [blame] | 1932 |  | 
| Tobin Ehlis | 4f482a7 | 2015-09-07 15:16:39 -0600 | [diff] [blame] | 1933 | /* Make initial call to query gpu_count, then second call for gpu info*/ | 
|  | 1934 | err = vkEnumeratePhysicalDevices(demo->inst, &gpu_count, NULL); | 
|  | 1935 | assert(!err && gpu_count > 0); | 
| Dustin Graves | beebf7d | 2016-01-27 13:48:06 -0700 | [diff] [blame] | 1936 |  | 
|  | 1937 | if (gpu_count > 0) { | 
|  | 1938 | VkPhysicalDevice *physical_devices = | 
|  | 1939 | malloc(sizeof(VkPhysicalDevice) * gpu_count); | 
|  | 1940 | err = vkEnumeratePhysicalDevices(demo->inst, &gpu_count, | 
|  | 1941 | physical_devices); | 
|  | 1942 | assert(!err); | 
|  | 1943 | /* For tri demo we just grab the first physical device */ | 
|  | 1944 | demo->gpu = physical_devices[0]; | 
|  | 1945 | free(physical_devices); | 
|  | 1946 | } else { | 
|  | 1947 | ERR_EXIT("vkEnumeratePhysicalDevices reported zero accessible devices." | 
|  | 1948 | "\n\nDo you have a compatible Vulkan installable client" | 
|  | 1949 | " driver (ICD) installed?\nPlease look at the Getting Started" | 
|  | 1950 | " guide for additional information.\n", | 
|  | 1951 | "vkEnumeratePhysicalDevices Failure"); | 
|  | 1952 | } | 
| Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 1953 |  | 
| Courtney Goeltzenleuchter | 18061cd | 2015-06-29 15:39:26 -0600 | [diff] [blame] | 1954 | /* Look for validation layers */ | 
| Jon Ashburn | 99ea74d | 2016-02-19 15:23:41 -0700 | [diff] [blame] | 1955 | if (demo->validate) { | 
|  | 1956 | validation_found = 0; | 
|  | 1957 | demo->enabled_layer_count = 0; | 
|  | 1958 | uint32_t device_layer_count = 0; | 
|  | 1959 | err = | 
|  | 1960 | vkEnumerateDeviceLayerProperties(demo->gpu, &device_layer_count, NULL); | 
| Dustin Graves | beebf7d | 2016-01-27 13:48:06 -0700 | [diff] [blame] | 1961 | assert(!err); | 
| Ian Elliott | e36b208 | 2015-07-06 14:27:58 -0600 | [diff] [blame] | 1962 |  | 
| Jon Ashburn | 99ea74d | 2016-02-19 15:23:41 -0700 | [diff] [blame] | 1963 | if (device_layer_count > 0) { | 
|  | 1964 | VkLayerProperties *device_layers = | 
|  | 1965 | malloc(sizeof (VkLayerProperties) * device_layer_count); | 
|  | 1966 | err = vkEnumerateDeviceLayerProperties(demo->gpu, &device_layer_count, | 
|  | 1967 | device_layers); | 
|  | 1968 | assert(!err); | 
|  | 1969 |  | 
|  | 1970 |  | 
| Dustin Graves | beebf7d | 2016-01-27 13:48:06 -0700 | [diff] [blame] | 1971 | validation_found = demo_check_layers(device_validation_layer_count, | 
| Jon Ashburn | 99ea74d | 2016-02-19 15:23:41 -0700 | [diff] [blame] | 1972 | demo->device_validation_layers, | 
|  | 1973 | device_layer_count, | 
|  | 1974 | device_layers); | 
| Dustin Graves | beebf7d | 2016-01-27 13:48:06 -0700 | [diff] [blame] | 1975 | demo->enabled_layer_count = device_validation_layer_count; | 
| Jon Ashburn | 99ea74d | 2016-02-19 15:23:41 -0700 | [diff] [blame] | 1976 |  | 
|  | 1977 | free(device_layers); | 
| Courtney Goeltzenleuchter | 18061cd | 2015-06-29 15:39:26 -0600 | [diff] [blame] | 1978 | } | 
| Dustin Graves | beebf7d | 2016-01-27 13:48:06 -0700 | [diff] [blame] | 1979 |  | 
| Jon Ashburn | 99ea74d | 2016-02-19 15:23:41 -0700 | [diff] [blame] | 1980 | if (!validation_found) { | 
|  | 1981 | ERR_EXIT("vkEnumerateDeviceLayerProperties failed to find " | 
|  | 1982 | "a required validation layer.\n\n" | 
|  | 1983 | "Please look at the Getting Started guide for additional " | 
|  | 1984 | "information.\n", | 
|  | 1985 | "vkCreateDevice Failure"); | 
|  | 1986 | } | 
| Courtney Goeltzenleuchter | 18061cd | 2015-06-29 15:39:26 -0600 | [diff] [blame] | 1987 | } | 
| Ian Elliott | e36b208 | 2015-07-06 14:27:58 -0600 | [diff] [blame] | 1988 |  | 
| Jon Ashburn | 99ea74d | 2016-02-19 15:23:41 -0700 | [diff] [blame] | 1989 | /* Look for device extensions */ | 
| Ian Elliott | e36b208 | 2015-07-06 14:27:58 -0600 | [diff] [blame] | 1990 | uint32_t device_extension_count = 0; | 
| Dustin Graves | beebf7d | 2016-01-27 13:48:06 -0700 | [diff] [blame] | 1991 | VkBool32 swapchainExtFound = 0; | 
|  | 1992 | demo->enabled_extension_count = 0; | 
|  | 1993 | memset(demo->extension_names, 0, sizeof(demo->extension_names)); | 
|  | 1994 |  | 
| Karl Schultz | 481756e | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 1995 | err = vkEnumerateDeviceExtensionProperties(demo->gpu, NULL, | 
|  | 1996 | &device_extension_count, NULL); | 
| Ian Elliott | e36b208 | 2015-07-06 14:27:58 -0600 | [diff] [blame] | 1997 | assert(!err); | 
|  | 1998 |  | 
| Dustin Graves | beebf7d | 2016-01-27 13:48:06 -0700 | [diff] [blame] | 1999 | if (device_extension_count > 0) { | 
|  | 2000 | VkExtensionProperties *device_extensions = | 
|  | 2001 | malloc(sizeof(VkExtensionProperties) * device_extension_count); | 
|  | 2002 | err = vkEnumerateDeviceExtensionProperties( | 
|  | 2003 | demo->gpu, NULL, &device_extension_count, device_extensions); | 
|  | 2004 | assert(!err); | 
| Ian Elliott | e36b208 | 2015-07-06 14:27:58 -0600 | [diff] [blame] | 2005 |  | 
| Dustin Graves | beebf7d | 2016-01-27 13:48:06 -0700 | [diff] [blame] | 2006 | for (uint32_t i = 0; i < device_extension_count; i++) { | 
|  | 2007 | if (!strcmp(VK_KHR_SWAPCHAIN_EXTENSION_NAME, | 
|  | 2008 | device_extensions[i].extensionName)) { | 
|  | 2009 | swapchainExtFound = 1; | 
|  | 2010 | demo->extension_names[demo->enabled_extension_count++] = | 
|  | 2011 | VK_KHR_SWAPCHAIN_EXTENSION_NAME; | 
|  | 2012 | } | 
|  | 2013 | assert(demo->enabled_extension_count < 64); | 
| Ian Elliott | e36b208 | 2015-07-06 14:27:58 -0600 | [diff] [blame] | 2014 | } | 
| Dustin Graves | beebf7d | 2016-01-27 13:48:06 -0700 | [diff] [blame] | 2015 |  | 
|  | 2016 | free(device_extensions); | 
| Ian Elliott | e36b208 | 2015-07-06 14:27:58 -0600 | [diff] [blame] | 2017 | } | 
| Dustin Graves | beebf7d | 2016-01-27 13:48:06 -0700 | [diff] [blame] | 2018 |  | 
| Tony Barbour | d9955e4 | 2015-10-08 13:59:42 -0600 | [diff] [blame] | 2019 | if (!swapchainExtFound) { | 
| Karl Schultz | 481756e | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 2020 | ERR_EXIT("vkEnumerateDeviceExtensionProperties failed to find " | 
|  | 2021 | "the " VK_KHR_SWAPCHAIN_EXTENSION_NAME | 
|  | 2022 | " extension.\n\nDo you have a compatible " | 
| Ian Elliott | e36b208 | 2015-07-06 14:27:58 -0600 | [diff] [blame] | 2023 | "Vulkan installable client driver (ICD) installed?\nPlease " | 
|  | 2024 | "look at the Getting Started guide for additional " | 
| Courtney Goeltzenleuchter | 18061cd | 2015-06-29 15:39:26 -0600 | [diff] [blame] | 2025 | "information.\n", | 
|  | 2026 | "vkCreateInstance Failure"); | 
|  | 2027 | } | 
|  | 2028 |  | 
| Courtney Goeltzenleuchter | 18061cd | 2015-06-29 15:39:26 -0600 | [diff] [blame] | 2029 | if (demo->validate) { | 
| Karl Schultz | 481756e | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 2030 | demo->CreateDebugReportCallback = | 
|  | 2031 | (PFN_vkCreateDebugReportCallbackEXT)vkGetInstanceProcAddr( | 
|  | 2032 | demo->inst, "vkCreateDebugReportCallbackEXT"); | 
| Mark Lobodzinski | 96b8241 | 2016-02-22 09:32:55 -0700 | [diff] [blame] | 2033 | demo->DestroyDebugReportCallback = | 
|  | 2034 | (PFN_vkDestroyDebugReportCallbackEXT)vkGetInstanceProcAddr( | 
|  | 2035 | demo->inst, "vkDestroyDebugReportCallbackEXT"); | 
| Courtney Goeltzenleuchter | f6a6e22 | 2015-11-30 12:13:14 -0700 | [diff] [blame] | 2036 | if (!demo->CreateDebugReportCallback) { | 
| Karl Schultz | 481756e | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 2037 | ERR_EXIT( | 
|  | 2038 | "GetProcAddr: Unable to find vkCreateDebugReportCallbackEXT\n", | 
|  | 2039 | "vkGetProcAddr Failure"); | 
| Courtney Goeltzenleuchter | 18061cd | 2015-06-29 15:39:26 -0600 | [diff] [blame] | 2040 | } | 
| Mark Lobodzinski | 96b8241 | 2016-02-22 09:32:55 -0700 | [diff] [blame] | 2041 | if (!demo->DestroyDebugReportCallback) { | 
|  | 2042 | ERR_EXIT( | 
|  | 2043 | "GetProcAddr: Unable to find vkDestroyDebugReportCallbackEXT\n", | 
|  | 2044 | "vkGetProcAddr Failure"); | 
|  | 2045 | } | 
|  | 2046 | demo->DebugReportMessage = | 
|  | 2047 | (PFN_vkDebugReportMessageEXT)vkGetInstanceProcAddr( | 
|  | 2048 | demo->inst, "vkDebugReportMessageEXT"); | 
|  | 2049 | if (!demo->DebugReportMessage) { | 
|  | 2050 | ERR_EXIT("GetProcAddr: Unable to find vkDebugReportMessageEXT\n", | 
|  | 2051 | "vkGetProcAddr Failure"); | 
|  | 2052 | } | 
|  | 2053 |  | 
| Courtney Goeltzenleuchter | acb1359 | 2015-12-09 15:48:16 -0700 | [diff] [blame] | 2054 | VkDebugReportCallbackCreateInfoEXT dbgCreateInfo; | 
|  | 2055 | dbgCreateInfo.sType = VK_STRUCTURE_TYPE_DEBUG_REPORT_CREATE_INFO_EXT; | 
| Karl Schultz | 481756e | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 2056 | dbgCreateInfo.flags = | 
| Mark Lobodzinski | 5c13d4d | 2016-02-11 09:26:16 -0700 | [diff] [blame] | 2057 | VK_DEBUG_REPORT_ERROR_BIT_EXT | VK_DEBUG_REPORT_WARNING_BIT_EXT; | 
| Karl Schultz | 7c5f63e | 2016-03-25 14:25:16 -0600 | [diff] [blame] | 2058 | dbgCreateInfo.pfnCallback = demo->use_break ? BreakCallback : dbgFunc; | 
| Courtney Goeltzenleuchter | f6a6e22 | 2015-11-30 12:13:14 -0700 | [diff] [blame] | 2059 | dbgCreateInfo.pUserData = NULL; | 
|  | 2060 | dbgCreateInfo.pNext = NULL; | 
| Karl Schultz | 481756e | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 2061 | err = demo->CreateDebugReportCallback(demo->inst, &dbgCreateInfo, NULL, | 
|  | 2062 | &demo->msg_callback); | 
| Courtney Goeltzenleuchter | 18061cd | 2015-06-29 15:39:26 -0600 | [diff] [blame] | 2063 | switch (err) { | 
|  | 2064 | case VK_SUCCESS: | 
|  | 2065 | break; | 
| Courtney Goeltzenleuchter | 18061cd | 2015-06-29 15:39:26 -0600 | [diff] [blame] | 2066 | case VK_ERROR_OUT_OF_HOST_MEMORY: | 
| Courtney Goeltzenleuchter | f6a6e22 | 2015-11-30 12:13:14 -0700 | [diff] [blame] | 2067 | ERR_EXIT("CreateDebugReportCallback: out of host memory\n", | 
|  | 2068 | "CreateDebugReportCallback Failure"); | 
| Courtney Goeltzenleuchter | 18061cd | 2015-06-29 15:39:26 -0600 | [diff] [blame] | 2069 | break; | 
|  | 2070 | default: | 
| Courtney Goeltzenleuchter | f6a6e22 | 2015-11-30 12:13:14 -0700 | [diff] [blame] | 2071 | ERR_EXIT("CreateDebugReportCallback: unknown failure\n", | 
|  | 2072 | "CreateDebugReportCallback Failure"); | 
| Courtney Goeltzenleuchter | 18061cd | 2015-06-29 15:39:26 -0600 | [diff] [blame] | 2073 | break; | 
|  | 2074 | } | 
|  | 2075 | } | 
|  | 2076 |  | 
| Jon Ashburn | 250984c | 2015-11-17 15:22:07 -0700 | [diff] [blame] | 2077 | // Having these GIPA queries of device extension entry points both | 
|  | 2078 | // BEFORE and AFTER vkCreateDevice is a good test for the loader | 
| Ian Elliott | 64070a8 | 2015-11-17 17:29:40 -0700 | [diff] [blame] | 2079 | GET_INSTANCE_PROC_ADDR(demo->inst, GetPhysicalDeviceSurfaceCapabilitiesKHR); | 
|  | 2080 | GET_INSTANCE_PROC_ADDR(demo->inst, GetPhysicalDeviceSurfaceFormatsKHR); | 
|  | 2081 | GET_INSTANCE_PROC_ADDR(demo->inst, GetPhysicalDeviceSurfacePresentModesKHR); | 
| Ian Elliott | 338dedb | 2015-08-21 15:09:33 -0600 | [diff] [blame] | 2082 | GET_INSTANCE_PROC_ADDR(demo->inst, GetPhysicalDeviceSurfaceSupportKHR); | 
| Jon Ashburn | 250984c | 2015-11-17 15:22:07 -0700 | [diff] [blame] | 2083 | GET_INSTANCE_PROC_ADDR(demo->inst, CreateSwapchainKHR); | 
|  | 2084 | GET_INSTANCE_PROC_ADDR(demo->inst, DestroySwapchainKHR); | 
|  | 2085 | GET_INSTANCE_PROC_ADDR(demo->inst, GetSwapchainImagesKHR); | 
|  | 2086 | GET_INSTANCE_PROC_ADDR(demo->inst, AcquireNextImageKHR); | 
|  | 2087 | GET_INSTANCE_PROC_ADDR(demo->inst, QueuePresentKHR); | 
| Ian Elliott | 1b6de09 | 2015-06-22 15:07:49 -0600 | [diff] [blame] | 2088 |  | 
| Courtney Goeltzenleuchter | 01d2ae1 | 2015-10-20 16:40:38 -0600 | [diff] [blame] | 2089 | vkGetPhysicalDeviceProperties(demo->gpu, &demo->gpu_props); | 
| Courtney Goeltzenleuchter | bfccf41 | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 2090 |  | 
| Cody Northrop | ef72e2a | 2015-08-03 17:04:53 -0600 | [diff] [blame] | 2091 | // Query with NULL data to get count | 
| Karl Schultz | 481756e | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 2092 | vkGetPhysicalDeviceQueueFamilyProperties(demo->gpu, &demo->queue_count, | 
|  | 2093 | NULL); | 
| Courtney Goeltzenleuchter | bfccf41 | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 2094 |  | 
| Karl Schultz | 481756e | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 2095 | demo->queue_props = (VkQueueFamilyProperties *)malloc( | 
|  | 2096 | demo->queue_count * sizeof(VkQueueFamilyProperties)); | 
|  | 2097 | vkGetPhysicalDeviceQueueFamilyProperties(demo->gpu, &demo->queue_count, | 
|  | 2098 | demo->queue_props); | 
| Piers Daniell | 1cf7fe1 | 2015-07-16 09:35:35 -0600 | [diff] [blame] | 2099 | assert(demo->queue_count >= 1); | 
| Courtney Goeltzenleuchter | f316806 | 2015-03-05 18:09:39 -0700 | [diff] [blame] | 2100 |  | 
| Chris Forbes | a3c20a2 | 2016-03-15 10:12:48 +1300 | [diff] [blame] | 2101 | VkPhysicalDeviceFeatures features; | 
|  | 2102 | vkGetPhysicalDeviceFeatures(demo->gpu, &features); | 
|  | 2103 |  | 
|  | 2104 | if (!features.shaderClipDistance) { | 
|  | 2105 | ERR_EXIT("Required device feature `shaderClipDistance` not supported\n", | 
|  | 2106 | "GetPhysicalDeviceFeatures failure"); | 
|  | 2107 | } | 
|  | 2108 |  | 
| Courtney Goeltzenleuchter | 18061cd | 2015-06-29 15:39:26 -0600 | [diff] [blame] | 2109 | // Graphics queue and MemMgr queue can be separate. | 
|  | 2110 | // TODO: Add support for separate queues, including synchronization, | 
|  | 2111 | //       and appropriate tracking for QueueSubmit | 
| Piers Daniell | 1cf7fe1 | 2015-07-16 09:35:35 -0600 | [diff] [blame] | 2112 | } | 
|  | 2113 |  | 
| Karl Schultz | 481756e | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 2114 | static void demo_init_device(struct demo *demo) { | 
| Tony Barbour | 3d03a4a | 2016-01-22 14:36:40 -0700 | [diff] [blame] | 2115 | VkResult U_ASSERT_ONLY err; | 
|  | 2116 |  | 
| Karl Schultz | 481756e | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 2117 | float queue_priorities[1] = {0.0}; | 
| Tony Barbour | 3d03a4a | 2016-01-22 14:36:40 -0700 | [diff] [blame] | 2118 | const VkDeviceQueueCreateInfo queue = { | 
|  | 2119 | .sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO, | 
|  | 2120 | .pNext = NULL, | 
|  | 2121 | .queueFamilyIndex = demo->graphics_queue_node_index, | 
|  | 2122 | .queueCount = 1, | 
| Karl Schultz | 481756e | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 2123 | .pQueuePriorities = queue_priorities}; | 
| Tony Barbour | 3d03a4a | 2016-01-22 14:36:40 -0700 | [diff] [blame] | 2124 |  | 
| Chris Forbes | a3c20a2 | 2016-03-15 10:12:48 +1300 | [diff] [blame] | 2125 | VkPhysicalDeviceFeatures features = { | 
|  | 2126 | .shaderClipDistance = VK_TRUE, | 
|  | 2127 | }; | 
|  | 2128 |  | 
| Tony Barbour | 3d03a4a | 2016-01-22 14:36:40 -0700 | [diff] [blame] | 2129 | VkDeviceCreateInfo device = { | 
|  | 2130 | .sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO, | 
|  | 2131 | .pNext = NULL, | 
|  | 2132 | .queueCreateInfoCount = 1, | 
|  | 2133 | .pQueueCreateInfos = &queue, | 
|  | 2134 | .enabledLayerCount = demo->enabled_layer_count, | 
| Karl Schultz | 481756e | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 2135 | .ppEnabledLayerNames = | 
|  | 2136 | (const char *const *)((demo->validate) | 
|  | 2137 | ? demo->device_validation_layers | 
|  | 2138 | : NULL), | 
| Tony Barbour | 3d03a4a | 2016-01-22 14:36:40 -0700 | [diff] [blame] | 2139 | .enabledExtensionCount = demo->enabled_extension_count, | 
| Karl Schultz | 481756e | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 2140 | .ppEnabledExtensionNames = (const char *const *)demo->extension_names, | 
| Chris Forbes | a3c20a2 | 2016-03-15 10:12:48 +1300 | [diff] [blame] | 2141 | .pEnabledFeatures = &features, | 
| Tony Barbour | 3d03a4a | 2016-01-22 14:36:40 -0700 | [diff] [blame] | 2142 | }; | 
|  | 2143 |  | 
|  | 2144 | err = vkCreateDevice(demo->gpu, &device, NULL, &demo->device); | 
|  | 2145 | assert(!err); | 
|  | 2146 | } | 
|  | 2147 |  | 
| Karl Schultz | 481756e | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 2148 | static void demo_init_vk_swapchain(struct demo *demo) { | 
| Tony Barbour | 4a6692d | 2015-10-08 13:45:45 -0600 | [diff] [blame] | 2149 | VkResult U_ASSERT_ONLY err; | 
| Piers Daniell | 1cf7fe1 | 2015-07-16 09:35:35 -0600 | [diff] [blame] | 2150 | uint32_t i; | 
| Ian Elliott | e36b208 | 2015-07-06 14:27:58 -0600 | [diff] [blame] | 2151 |  | 
| Karl Schultz | 481756e | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 2152 | // Create a WSI surface for the window: | 
| Ian Elliott | e36b208 | 2015-07-06 14:27:58 -0600 | [diff] [blame] | 2153 | #ifdef _WIN32 | 
| Ian Elliott | da44dfe | 2015-12-11 15:52:12 -0700 | [diff] [blame] | 2154 | VkWin32SurfaceCreateInfoKHR createInfo; | 
|  | 2155 | createInfo.sType = VK_STRUCTURE_TYPE_WIN32_SURFACE_CREATE_INFO_KHR; | 
|  | 2156 | createInfo.pNext = NULL; | 
|  | 2157 | createInfo.flags = 0; | 
| Jon Ashburn | 09338b5 | 2015-12-24 17:08:01 -0700 | [diff] [blame] | 2158 | createInfo.hinstance = demo->connection; | 
|  | 2159 | createInfo.hwnd = demo->window; | 
| Ian Elliott | da44dfe | 2015-12-11 15:52:12 -0700 | [diff] [blame] | 2160 |  | 
| Karl Schultz | 481756e | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 2161 | err = | 
|  | 2162 | vkCreateWin32SurfaceKHR(demo->inst, &createInfo, NULL, &demo->surface); | 
| Ian Elliott | b5fad79 | 2015-11-20 11:55:46 -0700 | [diff] [blame] | 2163 |  | 
| Ian Elliott | e36b208 | 2015-07-06 14:27:58 -0600 | [diff] [blame] | 2164 | #else  // _WIN32 | 
| Ian Elliott | da44dfe | 2015-12-11 15:52:12 -0700 | [diff] [blame] | 2165 | VkXcbSurfaceCreateInfoKHR createInfo; | 
|  | 2166 | createInfo.sType = VK_STRUCTURE_TYPE_XCB_SURFACE_CREATE_INFO_KHR; | 
|  | 2167 | createInfo.pNext = NULL; | 
|  | 2168 | createInfo.flags = 0; | 
|  | 2169 | createInfo.connection = demo->connection; | 
|  | 2170 | createInfo.window = demo->window; | 
|  | 2171 |  | 
| Karl Schultz | 481756e | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 2172 | err = vkCreateXcbSurfaceKHR(demo->inst, &createInfo, NULL, &demo->surface); | 
| Ian Elliott | e36b208 | 2015-07-06 14:27:58 -0600 | [diff] [blame] | 2173 | #endif // _WIN32 | 
|  | 2174 |  | 
| Tony Barbour | d9955e4 | 2015-10-08 13:59:42 -0600 | [diff] [blame] | 2175 | // Iterate over each queue to learn whether it supports presenting: | 
| Karl Schultz | 481756e | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 2176 | VkBool32 *supportsPresent = | 
|  | 2177 | (VkBool32 *)malloc(demo->queue_count * sizeof(VkBool32)); | 
| Piers Daniell | 1cf7fe1 | 2015-07-16 09:35:35 -0600 | [diff] [blame] | 2178 | for (i = 0; i < demo->queue_count; i++) { | 
| Karl Schultz | 481756e | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 2179 | demo->fpGetPhysicalDeviceSurfaceSupportKHR(demo->gpu, i, demo->surface, | 
| Ian Elliott | e36b208 | 2015-07-06 14:27:58 -0600 | [diff] [blame] | 2180 | &supportsPresent[i]); | 
| Courtney Goeltzenleuchter | f316806 | 2015-03-05 18:09:39 -0700 | [diff] [blame] | 2181 | } | 
| Ian Elliott | e36b208 | 2015-07-06 14:27:58 -0600 | [diff] [blame] | 2182 |  | 
|  | 2183 | // Search for a graphics and a present queue in the array of queue | 
|  | 2184 | // families, try to find one that supports both | 
|  | 2185 | uint32_t graphicsQueueNodeIndex = UINT32_MAX; | 
| Karl Schultz | 481756e | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 2186 | uint32_t presentQueueNodeIndex = UINT32_MAX; | 
| Piers Daniell | 1cf7fe1 | 2015-07-16 09:35:35 -0600 | [diff] [blame] | 2187 | for (i = 0; i < demo->queue_count; i++) { | 
| Ian Elliott | e36b208 | 2015-07-06 14:27:58 -0600 | [diff] [blame] | 2188 | if ((demo->queue_props[i].queueFlags & VK_QUEUE_GRAPHICS_BIT) != 0) { | 
|  | 2189 | if (graphicsQueueNodeIndex == UINT32_MAX) { | 
|  | 2190 | graphicsQueueNodeIndex = i; | 
|  | 2191 | } | 
| Karl Schultz | 481756e | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 2192 |  | 
| Ian Elliott | e36b208 | 2015-07-06 14:27:58 -0600 | [diff] [blame] | 2193 | if (supportsPresent[i] == VK_TRUE) { | 
|  | 2194 | graphicsQueueNodeIndex = i; | 
|  | 2195 | presentQueueNodeIndex = i; | 
|  | 2196 | break; | 
|  | 2197 | } | 
|  | 2198 | } | 
|  | 2199 | } | 
|  | 2200 | if (presentQueueNodeIndex == UINT32_MAX) { | 
|  | 2201 | // If didn't find a queue that supports both graphics and present, then | 
|  | 2202 | // find a separate present queue. | 
| Piers Daniell | 1cf7fe1 | 2015-07-16 09:35:35 -0600 | [diff] [blame] | 2203 | for (uint32_t i = 0; i < demo->queue_count; ++i) { | 
| Ian Elliott | e36b208 | 2015-07-06 14:27:58 -0600 | [diff] [blame] | 2204 | if (supportsPresent[i] == VK_TRUE) { | 
|  | 2205 | presentQueueNodeIndex = i; | 
|  | 2206 | break; | 
|  | 2207 | } | 
|  | 2208 | } | 
|  | 2209 | } | 
|  | 2210 | free(supportsPresent); | 
|  | 2211 |  | 
|  | 2212 | // Generate error if could not find both a graphics and a present queue | 
| Karl Schultz | 481756e | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 2213 | if (graphicsQueueNodeIndex == UINT32_MAX || | 
|  | 2214 | presentQueueNodeIndex == UINT32_MAX) { | 
| Ian Elliott | e36b208 | 2015-07-06 14:27:58 -0600 | [diff] [blame] | 2215 | ERR_EXIT("Could not find a graphics and a present queue\n", | 
| Tony Barbour | d9955e4 | 2015-10-08 13:59:42 -0600 | [diff] [blame] | 2216 | "Swapchain Initialization Failure"); | 
| Ian Elliott | e36b208 | 2015-07-06 14:27:58 -0600 | [diff] [blame] | 2217 | } | 
|  | 2218 |  | 
|  | 2219 | // TODO: Add support for separate queues, including presentation, | 
| Ian Elliott | 8d2a2eb | 2015-11-23 12:48:15 -0700 | [diff] [blame] | 2220 | //       synchronization, and appropriate tracking for QueueSubmit. | 
|  | 2221 | // NOTE: While it is possible for an application to use a separate graphics | 
|  | 2222 | //       and a present queues, this demo program assumes it is only using | 
|  | 2223 | //       one: | 
| Ian Elliott | e36b208 | 2015-07-06 14:27:58 -0600 | [diff] [blame] | 2224 | if (graphicsQueueNodeIndex != presentQueueNodeIndex) { | 
|  | 2225 | 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] | 2226 | "Swapchain Initialization Failure"); | 
| Ian Elliott | e36b208 | 2015-07-06 14:27:58 -0600 | [diff] [blame] | 2227 | } | 
|  | 2228 |  | 
|  | 2229 | demo->graphics_queue_node_index = graphicsQueueNodeIndex; | 
| Courtney Goeltzenleuchter | f316806 | 2015-03-05 18:09:39 -0700 | [diff] [blame] | 2230 |  | 
| Tony Barbour | 3d03a4a | 2016-01-22 14:36:40 -0700 | [diff] [blame] | 2231 | demo_init_device(demo); | 
|  | 2232 |  | 
| Karl Schultz | 481756e | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 2233 | vkGetDeviceQueue(demo->device, demo->graphics_queue_node_index, 0, | 
|  | 2234 | &demo->queue); | 
| Ian Elliott | 32536f9 | 2015-04-21 16:41:02 -0600 | [diff] [blame] | 2235 |  | 
| Ian Elliott | e36b208 | 2015-07-06 14:27:58 -0600 | [diff] [blame] | 2236 | // Get the list of VkFormat's that are supported: | 
| Ian Elliott | 7fe115d | 2015-08-07 15:56:59 -0600 | [diff] [blame] | 2237 | uint32_t formatCount; | 
| Karl Schultz | 481756e | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 2238 | err = demo->fpGetPhysicalDeviceSurfaceFormatsKHR(demo->gpu, demo->surface, | 
| Ian Elliott | 8d2a2eb | 2015-11-23 12:48:15 -0700 | [diff] [blame] | 2239 | &formatCount, NULL); | 
| Ian Elliott | e36b208 | 2015-07-06 14:27:58 -0600 | [diff] [blame] | 2240 | assert(!err); | 
| Ian Elliott | 338dedb | 2015-08-21 15:09:33 -0600 | [diff] [blame] | 2241 | VkSurfaceFormatKHR *surfFormats = | 
|  | 2242 | (VkSurfaceFormatKHR *)malloc(formatCount * sizeof(VkSurfaceFormatKHR)); | 
| Karl Schultz | 481756e | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 2243 | err = demo->fpGetPhysicalDeviceSurfaceFormatsKHR(demo->gpu, demo->surface, | 
| Ian Elliott | 8d2a2eb | 2015-11-23 12:48:15 -0700 | [diff] [blame] | 2244 | &formatCount, surfFormats); | 
| Ian Elliott | e36b208 | 2015-07-06 14:27:58 -0600 | [diff] [blame] | 2245 | assert(!err); | 
|  | 2246 | // If the format list includes just one entry of VK_FORMAT_UNDEFINED, | 
|  | 2247 | // the surface has no preferred format.  Otherwise, at least one | 
|  | 2248 | // supported format will be returned. | 
| Karl Schultz | 481756e | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 2249 | if (formatCount == 1 && surfFormats[0].format == VK_FORMAT_UNDEFINED) { | 
| Ian Elliott | e36b208 | 2015-07-06 14:27:58 -0600 | [diff] [blame] | 2250 | demo->format = VK_FORMAT_B8G8R8A8_UNORM; | 
| Karl Schultz | 481756e | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 2251 | } else { | 
| Ian Elliott | e36b208 | 2015-07-06 14:27:58 -0600 | [diff] [blame] | 2252 | assert(formatCount >= 1); | 
|  | 2253 | demo->format = surfFormats[0].format; | 
|  | 2254 | } | 
| Ian Elliott | 7fe115d | 2015-08-07 15:56:59 -0600 | [diff] [blame] | 2255 | demo->color_space = surfFormats[0].colorSpace; | 
| Ian Elliott | 32536f9 | 2015-04-21 16:41:02 -0600 | [diff] [blame] | 2256 |  | 
| Karl Schultz | 501fdc2 | 2016-03-24 18:43:41 -0600 | [diff] [blame] | 2257 | demo->quit = false; | 
|  | 2258 | demo->curFrame = 0; | 
|  | 2259 |  | 
| Mark Lobodzinski | 7234629 | 2015-07-02 16:49:40 -0600 | [diff] [blame] | 2260 | // Get Memory information and properties | 
| Courtney Goeltzenleuchter | 01d2ae1 | 2015-10-20 16:40:38 -0600 | [diff] [blame] | 2261 | vkGetPhysicalDeviceMemoryProperties(demo->gpu, &demo->memory_properties); | 
| Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 2262 | } | 
|  | 2263 |  | 
| Karl Schultz | 481756e | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 2264 | static void demo_init_connection(struct demo *demo) { | 
| Ian Elliott | e14e9f9 | 2015-04-16 15:23:05 -0600 | [diff] [blame] | 2265 | #ifndef _WIN32 | 
| Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 2266 | const xcb_setup_t *setup; | 
|  | 2267 | xcb_screen_iterator_t iter; | 
|  | 2268 | int scr; | 
|  | 2269 |  | 
|  | 2270 | demo->connection = xcb_connect(NULL, &scr); | 
| Ian Elliott | dfe55f7 | 2015-04-03 15:24:55 -0600 | [diff] [blame] | 2271 | if (demo->connection == NULL) { | 
|  | 2272 | printf("Cannot find a compatible Vulkan installable client driver " | 
|  | 2273 | "(ICD).\nExiting ...\n"); | 
|  | 2274 | fflush(stdout); | 
|  | 2275 | exit(1); | 
|  | 2276 | } | 
| Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 2277 |  | 
|  | 2278 | setup = xcb_get_setup(demo->connection); | 
|  | 2279 | iter = xcb_setup_roots_iterator(setup); | 
|  | 2280 | while (scr-- > 0) | 
|  | 2281 | xcb_screen_next(&iter); | 
|  | 2282 |  | 
|  | 2283 | demo->screen = iter.data; | 
| Ian Elliott | e14e9f9 | 2015-04-16 15:23:05 -0600 | [diff] [blame] | 2284 | #endif // _WIN32 | 
| Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 2285 | } | 
|  | 2286 |  | 
| Courtney Goeltzenleuchter | f113a95 | 2015-02-25 11:46:58 -0700 | [diff] [blame] | 2287 | static void demo_init(struct demo *demo, const int argc, const char *argv[]) | 
| Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 2288 | { | 
|  | 2289 | memset(demo, 0, sizeof(*demo)); | 
| Karl Schultz | 501fdc2 | 2016-03-24 18:43:41 -0600 | [diff] [blame] | 2290 | demo->frameCount = INT32_MAX; | 
| Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 2291 |  | 
| Karl Schultz | 501fdc2 | 2016-03-24 18:43:41 -0600 | [diff] [blame] | 2292 | for (int i = 1; i < argc; i++) { | 
|  | 2293 | if (strcmp(argv[i], "--use_staging") == 0) { | 
| Courtney Goeltzenleuchter | f113a95 | 2015-02-25 11:46:58 -0700 | [diff] [blame] | 2294 | demo->use_staging_buffer = true; | 
| Karl Schultz | 501fdc2 | 2016-03-24 18:43:41 -0600 | [diff] [blame] | 2295 | continue; | 
|  | 2296 | } | 
|  | 2297 | if (strcmp(argv[i], "--break") == 0) { | 
|  | 2298 | demo->use_break = true; | 
|  | 2299 | continue; | 
|  | 2300 | } | 
|  | 2301 | if (strcmp(argv[i], "--validate") == 0) { | 
| Jon Ashburn | 99ea74d | 2016-02-19 15:23:41 -0700 | [diff] [blame] | 2302 | demo->validate = true; | 
| Karl Schultz | 501fdc2 | 2016-03-24 18:43:41 -0600 | [diff] [blame] | 2303 | continue; | 
|  | 2304 | } | 
|  | 2305 | if (strcmp(argv[i], "--c") == 0 && demo->frameCount == INT32_MAX && | 
|  | 2306 | i < argc - 1 && sscanf(argv[i + 1], "%d", &demo->frameCount) == 1 && | 
|  | 2307 | demo->frameCount >= 0) { | 
|  | 2308 | i++; | 
|  | 2309 | continue; | 
|  | 2310 | } | 
|  | 2311 |  | 
|  | 2312 | fprintf(stderr, "Usage:\n  %s [--use_staging] [--validate] [--break] " | 
|  | 2313 | "[--c <framecount>]\n", | 
|  | 2314 | APP_SHORT_NAME); | 
| Ian Elliott | e14e9f9 | 2015-04-16 15:23:05 -0600 | [diff] [blame] | 2315 | fflush(stderr); | 
|  | 2316 | exit(1); | 
|  | 2317 | } | 
| Courtney Goeltzenleuchter | f113a95 | 2015-02-25 11:46:58 -0700 | [diff] [blame] | 2318 |  | 
| Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 2319 | demo_init_connection(demo); | 
| Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 2320 | demo_init_vk(demo); | 
| Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 2321 |  | 
|  | 2322 | demo->width = 300; | 
|  | 2323 | demo->height = 300; | 
| Tony Barbour | 4a6692d | 2015-10-08 13:45:45 -0600 | [diff] [blame] | 2324 | demo->depthStencil = 1.0; | 
|  | 2325 | demo->depthIncrement = -0.01f; | 
| Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 2326 | } | 
|  | 2327 |  | 
| Karl Schultz | 481756e | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 2328 | static void demo_cleanup(struct demo *demo) { | 
| Mark Lobodzinski | 2318261 | 2015-05-29 09:32:35 -0500 | [diff] [blame] | 2329 | uint32_t i; | 
| Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 2330 |  | 
| Cody Northrop | 67ff0a4 | 2015-09-09 10:21:49 -0600 | [diff] [blame] | 2331 | demo->prepared = false; | 
|  | 2332 |  | 
| Tony Barbour | 5aabff5 | 2015-10-08 14:26:24 -0600 | [diff] [blame] | 2333 | for (i = 0; i < demo->swapchainImageCount; i++) { | 
| Chia-I Wu | 69f4012 | 2015-10-26 21:10:41 +0800 | [diff] [blame] | 2334 | vkDestroyFramebuffer(demo->device, demo->framebuffers[i], NULL); | 
| Tony Barbour | de4124d | 2015-07-03 10:33:54 -0600 | [diff] [blame] | 2335 | } | 
| Tony Barbour | 5aabff5 | 2015-10-08 14:26:24 -0600 | [diff] [blame] | 2336 | free(demo->framebuffers); | 
| Chia-I Wu | 69f4012 | 2015-10-26 21:10:41 +0800 | [diff] [blame] | 2337 | vkDestroyDescriptorPool(demo->device, demo->desc_pool, NULL); | 
| Chia-I Wu | f838506 | 2015-01-04 16:27:24 +0800 | [diff] [blame] | 2338 |  | 
| Courtney Goeltzenleuchter | 633f9c5 | 2015-04-30 10:58:33 -0600 | [diff] [blame] | 2339 | if (demo->setup_cmd) { | 
| Courtney Goeltzenleuchter | 831c183 | 2015-10-23 14:21:05 -0600 | [diff] [blame] | 2340 | vkFreeCommandBuffers(demo->device, demo->cmd_pool, 1, &demo->setup_cmd); | 
| Courtney Goeltzenleuchter | 633f9c5 | 2015-04-30 10:58:33 -0600 | [diff] [blame] | 2341 | } | 
| Courtney Goeltzenleuchter | 831c183 | 2015-10-23 14:21:05 -0600 | [diff] [blame] | 2342 | vkFreeCommandBuffers(demo->device, demo->cmd_pool, 1, &demo->draw_cmd); | 
| Chia-I Wu | 69f4012 | 2015-10-26 21:10:41 +0800 | [diff] [blame] | 2343 | vkDestroyCommandPool(demo->device, demo->cmd_pool, NULL); | 
| Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 2344 |  | 
| Chia-I Wu | 69f4012 | 2015-10-26 21:10:41 +0800 | [diff] [blame] | 2345 | vkDestroyPipeline(demo->device, demo->pipeline, NULL); | 
|  | 2346 | vkDestroyRenderPass(demo->device, demo->render_pass, NULL); | 
|  | 2347 | vkDestroyPipelineLayout(demo->device, demo->pipeline_layout, NULL); | 
|  | 2348 | vkDestroyDescriptorSetLayout(demo->device, demo->desc_layout, NULL); | 
| Chia-I Wu | b043fe3 | 2014-10-06 15:30:33 +0800 | [diff] [blame] | 2349 |  | 
| Chia-I Wu | 69f4012 | 2015-10-26 21:10:41 +0800 | [diff] [blame] | 2350 | vkDestroyBuffer(demo->device, demo->vertices.buf, NULL); | 
|  | 2351 | vkFreeMemory(demo->device, demo->vertices.mem, NULL); | 
| Chia-I Wu | 99621bc | 2014-10-08 11:52:22 +0800 | [diff] [blame] | 2352 |  | 
| Chia-I Wu | b043fe3 | 2014-10-06 15:30:33 +0800 | [diff] [blame] | 2353 | for (i = 0; i < DEMO_TEXTURE_COUNT; i++) { | 
| Chia-I Wu | 69f4012 | 2015-10-26 21:10:41 +0800 | [diff] [blame] | 2354 | vkDestroyImageView(demo->device, demo->textures[i].view, NULL); | 
|  | 2355 | vkDestroyImage(demo->device, demo->textures[i].image, NULL); | 
|  | 2356 | vkFreeMemory(demo->device, demo->textures[i].mem, NULL); | 
|  | 2357 | vkDestroySampler(demo->device, demo->textures[i].sampler, NULL); | 
| Chia-I Wu | b043fe3 | 2014-10-06 15:30:33 +0800 | [diff] [blame] | 2358 | } | 
|  | 2359 |  | 
| Ian Elliott | 338dedb | 2015-08-21 15:09:33 -0600 | [diff] [blame] | 2360 | for (i = 0; i < demo->swapchainImageCount; i++) { | 
| Chia-I Wu | 69f4012 | 2015-10-26 21:10:41 +0800 | [diff] [blame] | 2361 | vkDestroyImageView(demo->device, demo->buffers[i].view, NULL); | 
| Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 2362 | } | 
| Tony Barbour | de4124d | 2015-07-03 10:33:54 -0600 | [diff] [blame] | 2363 |  | 
| Chia-I Wu | 69f4012 | 2015-10-26 21:10:41 +0800 | [diff] [blame] | 2364 | vkDestroyImageView(demo->device, demo->depth.view, NULL); | 
|  | 2365 | vkDestroyImage(demo->device, demo->depth.image, NULL); | 
|  | 2366 | vkFreeMemory(demo->device, demo->depth.mem, NULL); | 
| Tony Barbour | de4124d | 2015-07-03 10:33:54 -0600 | [diff] [blame] | 2367 |  | 
| Ian Elliott | b5fad79 | 2015-11-20 11:55:46 -0700 | [diff] [blame] | 2368 | demo->fpDestroySwapchainKHR(demo->device, demo->swapchain, NULL); | 
| Ian Elliott | e36b208 | 2015-07-06 14:27:58 -0600 | [diff] [blame] | 2369 | free(demo->buffers); | 
| Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 2370 |  | 
| Chia-I Wu | 69f4012 | 2015-10-26 21:10:41 +0800 | [diff] [blame] | 2371 | vkDestroyDevice(demo->device, NULL); | 
| Mark Lobodzinski | 96b8241 | 2016-02-22 09:32:55 -0700 | [diff] [blame] | 2372 | if (demo->validate) { | 
|  | 2373 | demo->DestroyDebugReportCallback(demo->inst, demo->msg_callback, NULL); | 
|  | 2374 | } | 
| Ian Elliott | b5fad79 | 2015-11-20 11:55:46 -0700 | [diff] [blame] | 2375 | vkDestroySurfaceKHR(demo->inst, demo->surface, NULL); | 
| Tony Barbour | 3ec16a5 | 2016-03-07 15:48:06 -0700 | [diff] [blame] | 2376 | vkDestroyInstance(demo->inst, NULL); | 
| Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 2377 |  | 
| Courtney Goeltzenleuchter | 591ac87 | 2015-09-24 17:27:08 -0600 | [diff] [blame] | 2378 | free(demo->queue_props); | 
|  | 2379 |  | 
| Ian Elliott | e14e9f9 | 2015-04-16 15:23:05 -0600 | [diff] [blame] | 2380 | #ifndef _WIN32 | 
| Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 2381 | xcb_destroy_window(demo->connection, demo->window); | 
|  | 2382 | xcb_disconnect(demo->connection); | 
| Courtney Goeltzenleuchter | 591ac87 | 2015-09-24 17:27:08 -0600 | [diff] [blame] | 2383 | free(demo->atom_wm_delete_window); | 
| Ian Elliott | e14e9f9 | 2015-04-16 15:23:05 -0600 | [diff] [blame] | 2384 | #endif // _WIN32 | 
| Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 2385 | } | 
|  | 2386 |  | 
| Karl Schultz | 481756e | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 2387 | static void demo_resize(struct demo *demo) { | 
| Ian Elliott | e2688a5 | 2015-10-16 18:02:43 -0600 | [diff] [blame] | 2388 | uint32_t i; | 
|  | 2389 |  | 
| Mike Stroyan | b194be6 | 2015-10-28 09:46:57 -0600 | [diff] [blame] | 2390 | // Don't react to resize until after first initialization. | 
|  | 2391 | if (!demo->prepared) { | 
|  | 2392 | return; | 
|  | 2393 | } | 
| Ian Elliott | e2688a5 | 2015-10-16 18:02:43 -0600 | [diff] [blame] | 2394 | // In order to properly resize the window, we must re-create the swapchain | 
|  | 2395 | // AND redo the command buffers, etc. | 
|  | 2396 | // | 
|  | 2397 | // First, perform part of the demo_cleanup() function: | 
|  | 2398 | demo->prepared = false; | 
|  | 2399 |  | 
|  | 2400 | for (i = 0; i < demo->swapchainImageCount; i++) { | 
| Chia-I Wu | 69f4012 | 2015-10-26 21:10:41 +0800 | [diff] [blame] | 2401 | vkDestroyFramebuffer(demo->device, demo->framebuffers[i], NULL); | 
| Ian Elliott | e2688a5 | 2015-10-16 18:02:43 -0600 | [diff] [blame] | 2402 | } | 
|  | 2403 | free(demo->framebuffers); | 
| Chia-I Wu | 69f4012 | 2015-10-26 21:10:41 +0800 | [diff] [blame] | 2404 | vkDestroyDescriptorPool(demo->device, demo->desc_pool, NULL); | 
| Ian Elliott | e2688a5 | 2015-10-16 18:02:43 -0600 | [diff] [blame] | 2405 |  | 
|  | 2406 | if (demo->setup_cmd) { | 
| Ian Elliott | 74bb2eb | 2015-10-27 11:06:33 -0600 | [diff] [blame] | 2407 | vkFreeCommandBuffers(demo->device, demo->cmd_pool, 1, &demo->setup_cmd); | 
| Ian Elliott | e2688a5 | 2015-10-16 18:02:43 -0600 | [diff] [blame] | 2408 | } | 
| Ian Elliott | 74bb2eb | 2015-10-27 11:06:33 -0600 | [diff] [blame] | 2409 | vkFreeCommandBuffers(demo->device, demo->cmd_pool, 1, &demo->draw_cmd); | 
| Chia-I Wu | 69f4012 | 2015-10-26 21:10:41 +0800 | [diff] [blame] | 2410 | vkDestroyCommandPool(demo->device, demo->cmd_pool, NULL); | 
| Ian Elliott | e2688a5 | 2015-10-16 18:02:43 -0600 | [diff] [blame] | 2411 |  | 
| Chia-I Wu | 69f4012 | 2015-10-26 21:10:41 +0800 | [diff] [blame] | 2412 | vkDestroyPipeline(demo->device, demo->pipeline, NULL); | 
|  | 2413 | vkDestroyRenderPass(demo->device, demo->render_pass, NULL); | 
|  | 2414 | vkDestroyPipelineLayout(demo->device, demo->pipeline_layout, NULL); | 
|  | 2415 | vkDestroyDescriptorSetLayout(demo->device, demo->desc_layout, NULL); | 
| Ian Elliott | e2688a5 | 2015-10-16 18:02:43 -0600 | [diff] [blame] | 2416 |  | 
| Chia-I Wu | 69f4012 | 2015-10-26 21:10:41 +0800 | [diff] [blame] | 2417 | vkDestroyBuffer(demo->device, demo->vertices.buf, NULL); | 
|  | 2418 | vkFreeMemory(demo->device, demo->vertices.mem, NULL); | 
| Ian Elliott | e2688a5 | 2015-10-16 18:02:43 -0600 | [diff] [blame] | 2419 |  | 
|  | 2420 | for (i = 0; i < DEMO_TEXTURE_COUNT; i++) { | 
| Chia-I Wu | 69f4012 | 2015-10-26 21:10:41 +0800 | [diff] [blame] | 2421 | vkDestroyImageView(demo->device, demo->textures[i].view, NULL); | 
|  | 2422 | vkDestroyImage(demo->device, demo->textures[i].image, NULL); | 
|  | 2423 | vkFreeMemory(demo->device, demo->textures[i].mem, NULL); | 
|  | 2424 | vkDestroySampler(demo->device, demo->textures[i].sampler, NULL); | 
| Ian Elliott | e2688a5 | 2015-10-16 18:02:43 -0600 | [diff] [blame] | 2425 | } | 
|  | 2426 |  | 
|  | 2427 | for (i = 0; i < demo->swapchainImageCount; i++) { | 
| Chia-I Wu | 69f4012 | 2015-10-26 21:10:41 +0800 | [diff] [blame] | 2428 | vkDestroyImageView(demo->device, demo->buffers[i].view, NULL); | 
| Ian Elliott | e2688a5 | 2015-10-16 18:02:43 -0600 | [diff] [blame] | 2429 | } | 
|  | 2430 |  | 
| Chia-I Wu | 69f4012 | 2015-10-26 21:10:41 +0800 | [diff] [blame] | 2431 | vkDestroyImageView(demo->device, demo->depth.view, NULL); | 
|  | 2432 | vkDestroyImage(demo->device, demo->depth.image, NULL); | 
|  | 2433 | vkFreeMemory(demo->device, demo->depth.mem, NULL); | 
| Ian Elliott | e2688a5 | 2015-10-16 18:02:43 -0600 | [diff] [blame] | 2434 |  | 
| Ian Elliott | 74bb2eb | 2015-10-27 11:06:33 -0600 | [diff] [blame] | 2435 | free(demo->buffers); | 
|  | 2436 |  | 
| Ian Elliott | e2688a5 | 2015-10-16 18:02:43 -0600 | [diff] [blame] | 2437 | // Second, re-perform the demo_prepare() function, which will re-create the | 
|  | 2438 | // swapchain: | 
|  | 2439 | demo_prepare(demo); | 
|  | 2440 | } | 
|  | 2441 |  | 
| Ian Elliott | e14e9f9 | 2015-04-16 15:23:05 -0600 | [diff] [blame] | 2442 | #ifdef _WIN32 | 
| Karl Schultz | 501fdc2 | 2016-03-24 18:43:41 -0600 | [diff] [blame] | 2443 | // Include header required for parsing the command line options. | 
|  | 2444 | #include <shellapi.h> | 
|  | 2445 |  | 
|  | 2446 | int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, | 
| Karl Schultz | 481756e | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 2447 | LPSTR pCmdLine, int nCmdShow) { | 
|  | 2448 | MSG msg;   // message | 
|  | 2449 | bool done; // flag saying when app is complete | 
| Karl Schultz | 501fdc2 | 2016-03-24 18:43:41 -0600 | [diff] [blame] | 2450 | int argc; | 
|  | 2451 | char **argv; | 
| Ian Elliott | e14e9f9 | 2015-04-16 15:23:05 -0600 | [diff] [blame] | 2452 |  | 
| Karl Schultz | 501fdc2 | 2016-03-24 18:43:41 -0600 | [diff] [blame] | 2453 | // Use the CommandLine functions to get the command line arguments. | 
|  | 2454 | // Unfortunately, Microsoft outputs | 
|  | 2455 | // this information as wide characters for Unicode, and we simply want the | 
|  | 2456 | // Ascii version to be compatible | 
|  | 2457 | // with the non-Windows side.  So, we have to convert the information to | 
|  | 2458 | // Ascii character strings. | 
|  | 2459 | LPWSTR *commandLineArgs = CommandLineToArgvW(GetCommandLineW(), &argc); | 
|  | 2460 | if (NULL == commandLineArgs) { | 
|  | 2461 | argc = 0; | 
|  | 2462 | } | 
|  | 2463 |  | 
|  | 2464 | if (argc > 0) { | 
|  | 2465 | argv = (char **)malloc(sizeof(char *) * argc); | 
|  | 2466 | if (argv == NULL) { | 
|  | 2467 | argc = 0; | 
|  | 2468 | } else { | 
|  | 2469 | for (int iii = 0; iii < argc; iii++) { | 
|  | 2470 | size_t wideCharLen = wcslen(commandLineArgs[iii]); | 
|  | 2471 | size_t numConverted = 0; | 
|  | 2472 |  | 
|  | 2473 | argv[iii] = (char *)malloc(sizeof(char) * (wideCharLen + 1)); | 
|  | 2474 | if (argv[iii] != NULL) { | 
|  | 2475 | wcstombs_s(&numConverted, argv[iii], wideCharLen + 1, | 
|  | 2476 | commandLineArgs[iii], wideCharLen + 1); | 
|  | 2477 | } | 
|  | 2478 | } | 
|  | 2479 | } | 
|  | 2480 | } else { | 
|  | 2481 | argv = NULL; | 
|  | 2482 | } | 
|  | 2483 |  | 
|  | 2484 | demo_init(&demo, argc, argv); | 
|  | 2485 |  | 
|  | 2486 | // Free up the items we had to allocate for the command line arguments. | 
|  | 2487 | if (argc > 0 && argv != NULL) { | 
|  | 2488 | for (int iii = 0; iii < argc; iii++) { | 
|  | 2489 | if (argv[iii] != NULL) { | 
|  | 2490 | free(argv[iii]); | 
|  | 2491 | } | 
|  | 2492 | } | 
|  | 2493 | free(argv); | 
|  | 2494 | } | 
|  | 2495 |  | 
|  | 2496 | demo.connection = hInstance; | 
|  | 2497 | strncpy(demo.name, "tri", APP_NAME_STR_LEN); | 
| Ian Elliott | e14e9f9 | 2015-04-16 15:23:05 -0600 | [diff] [blame] | 2498 | demo_create_window(&demo); | 
| Tony Barbour | d9955e4 | 2015-10-08 13:59:42 -0600 | [diff] [blame] | 2499 | demo_init_vk_swapchain(&demo); | 
| Ian Elliott | e14e9f9 | 2015-04-16 15:23:05 -0600 | [diff] [blame] | 2500 |  | 
|  | 2501 | demo_prepare(&demo); | 
|  | 2502 |  | 
| Karl Schultz | 481756e | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 2503 | done = false; // initialize loop condition variable | 
| Ian Elliott | e14e9f9 | 2015-04-16 15:23:05 -0600 | [diff] [blame] | 2504 | /* main message loop*/ | 
| Karl Schultz | 481756e | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 2505 | while (!done) { | 
| Ian Elliott | 421107f | 2015-04-28 15:50:36 -0600 | [diff] [blame] | 2506 | PeekMessage(&msg, NULL, 0, 0, PM_REMOVE); | 
| Karl Schultz | 481756e | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 2507 | if (msg.message == WM_QUIT) // check for a quit message | 
| Ian Elliott | e14e9f9 | 2015-04-16 15:23:05 -0600 | [diff] [blame] | 2508 | { | 
| Karl Schultz | 481756e | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 2509 | done = true; // if found, quit app | 
|  | 2510 | } else { | 
| Ian Elliott | e14e9f9 | 2015-04-16 15:23:05 -0600 | [diff] [blame] | 2511 | /* Translate and dispatch to event queue*/ | 
| Karl Schultz | 481756e | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 2512 | TranslateMessage(&msg); | 
| Ian Elliott | e14e9f9 | 2015-04-16 15:23:05 -0600 | [diff] [blame] | 2513 | DispatchMessage(&msg); | 
|  | 2514 | } | 
| Tony Barbour | 18b53e7 | 2015-10-20 12:49:46 -0600 | [diff] [blame] | 2515 | RedrawWindow(demo.window, NULL, NULL, RDW_INTERNALPAINT); | 
| Ian Elliott | e14e9f9 | 2015-04-16 15:23:05 -0600 | [diff] [blame] | 2516 | } | 
|  | 2517 |  | 
|  | 2518 | demo_cleanup(&demo); | 
|  | 2519 |  | 
| Karl Schultz | 481756e | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 2520 | return (int)msg.wParam; | 
| Ian Elliott | e14e9f9 | 2015-04-16 15:23:05 -0600 | [diff] [blame] | 2521 | } | 
|  | 2522 | #else  // _WIN32 | 
| Karl Schultz | 481756e | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 2523 | int main(const int argc, const char *argv[]) { | 
| Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 2524 | struct demo demo; | 
|  | 2525 |  | 
| Courtney Goeltzenleuchter | f113a95 | 2015-02-25 11:46:58 -0700 | [diff] [blame] | 2526 | demo_init(&demo, argc, argv); | 
| Chia-I Wu | 5b66aa5 | 2015-04-16 22:02:10 +0800 | [diff] [blame] | 2527 | demo_create_window(&demo); | 
| Tony Barbour | d9955e4 | 2015-10-08 13:59:42 -0600 | [diff] [blame] | 2528 | demo_init_vk_swapchain(&demo); | 
| Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 2529 |  | 
|  | 2530 | demo_prepare(&demo); | 
| Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 2531 | demo_run(&demo); | 
|  | 2532 |  | 
|  | 2533 | demo_cleanup(&demo); | 
|  | 2534 |  | 
| Karl Schultz | 501fdc2 | 2016-03-24 18:43:41 -0600 | [diff] [blame] | 2535 | return validation_error; | 
| Chia-I Wu | c19795a | 2014-09-13 11:12:55 +0800 | [diff] [blame] | 2536 | } | 
| Ian Elliott | e14e9f9 | 2015-04-16 15:23:05 -0600 | [diff] [blame] | 2537 | #endif // _WIN32 |