Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1 | // VK tests |
Chia-I Wu | f1e2e99 | 2014-12-27 14:12:52 +0800 | [diff] [blame] | 2 | // |
| 3 | // Copyright (C) 2014 LunarG, Inc. |
| 4 | // |
| 5 | // Permission is hereby granted, free of charge, to any person obtaining a |
| 6 | // copy of this software and associated documentation files (the "Software"), |
| 7 | // to deal in the Software without restriction, including without limitation |
| 8 | // the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 9 | // and/or sell copies of the Software, and to permit persons to whom the |
| 10 | // Software is furnished to do so, subject to the following conditions: |
| 11 | // |
| 12 | // The above copyright notice and this permission notice shall be included |
| 13 | // in all copies or substantial portions of the Software. |
| 14 | // |
| 15 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 16 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 17 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 18 | // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 19 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 20 | // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
| 21 | // DEALINGS IN THE SOFTWARE. |
| 22 | |
| 23 | #include <iostream> |
| 24 | #include <string.h> // memset(), memcmp() |
Courtney Goeltzenleuchter | 992fb4f | 2015-04-19 19:07:33 -0600 | [diff] [blame] | 25 | #include <assert.h> |
Tony Barbour | 67e9915 | 2015-07-10 14:10:27 -0600 | [diff] [blame] | 26 | #include <stdarg.h> |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 27 | #include "vktestbinding.h" |
Chia-I Wu | f1e2e99 | 2014-12-27 14:12:52 +0800 | [diff] [blame] | 28 | |
| 29 | namespace { |
Chia-I Wu | f1e2e99 | 2014-12-27 14:12:52 +0800 | [diff] [blame] | 30 | |
Chia-I Wu | f8f074f | 2015-07-03 10:58:57 +0800 | [diff] [blame] | 31 | #define NON_DISPATCHABLE_HANDLE_INIT(create_func, dev, ...) \ |
| 32 | do { \ |
| 33 | handle_type handle; \ |
| 34 | if (EXPECT(create_func(dev.handle(), __VA_ARGS__, &handle) == VK_SUCCESS)) \ |
| 35 | NonDispHandle::init(dev.handle(), handle); \ |
| 36 | } while (0) |
| 37 | |
Mark Lobodzinski | 2141f65 | 2015-09-07 13:59:43 -0600 | [diff] [blame] | 38 | #define NON_DISPATCHABLE_HANDLE_DTOR(cls, destroy_func) \ |
Chia-I Wu | d9e8e82 | 2015-07-03 11:45:55 +0800 | [diff] [blame] | 39 | cls::~cls() \ |
| 40 | { \ |
| 41 | if (initialized()) \ |
Mark Lobodzinski | 2141f65 | 2015-09-07 13:59:43 -0600 | [diff] [blame] | 42 | destroy_func(device(), handle()); \ |
Chia-I Wu | d9e8e82 | 2015-07-03 11:45:55 +0800 | [diff] [blame] | 43 | } |
| 44 | |
Chia-I Wu | f1e2e99 | 2014-12-27 14:12:52 +0800 | [diff] [blame] | 45 | #define STRINGIFY(x) #x |
| 46 | #define EXPECT(expr) ((expr) ? true : expect_failure(STRINGIFY(expr), __FILE__, __LINE__, __FUNCTION__)) |
Mark Lobodzinski | 40f7f40 | 2015-04-16 11:44:05 -0500 | [diff] [blame] | 47 | |
Chia-I Wu | f1e2e99 | 2014-12-27 14:12:52 +0800 | [diff] [blame] | 48 | |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 49 | vk_testing::ErrorCallback error_callback; |
Chia-I Wu | f1e2e99 | 2014-12-27 14:12:52 +0800 | [diff] [blame] | 50 | |
| 51 | bool expect_failure(const char *expr, const char *file, unsigned int line, const char *function) |
| 52 | { |
| 53 | if (error_callback) { |
| 54 | error_callback(expr, file, line, function); |
| 55 | } else { |
| 56 | std::cerr << file << ":" << line << ": " << function << |
| 57 | ": Expectation `" << expr << "' failed.\n"; |
| 58 | } |
| 59 | |
| 60 | return false; |
| 61 | } |
| 62 | |
| 63 | template<class T, class S> |
Chia-I Wu | d9e8e82 | 2015-07-03 11:45:55 +0800 | [diff] [blame] | 64 | std::vector<T> make_handles(const std::vector<S> &v) |
| 65 | { |
| 66 | std::vector<T> handles; |
| 67 | handles.reserve(v.size()); |
| 68 | for (typename std::vector<S>::const_iterator it = v.begin(); it != v.end(); it++) |
| 69 | handles.push_back((*it)->handle()); |
| 70 | return handles; |
| 71 | } |
| 72 | |
Chia-I Wu | 681d7a0 | 2015-07-03 13:44:34 +0800 | [diff] [blame] | 73 | VkMemoryAllocInfo get_resource_alloc_info(const vk_testing::Device &dev, const VkMemoryRequirements &reqs, VkMemoryPropertyFlags mem_props) |
Chia-I Wu | f1e2e99 | 2014-12-27 14:12:52 +0800 | [diff] [blame] | 74 | { |
Chia-I Wu | 681d7a0 | 2015-07-03 13:44:34 +0800 | [diff] [blame] | 75 | VkMemoryAllocInfo info = vk_testing::DeviceMemory::alloc_info(reqs.size, 0); |
| 76 | dev.phy().set_memory_type(reqs.memoryTypeBits, &info, mem_props); |
Chia-I Wu | f1e2e99 | 2014-12-27 14:12:52 +0800 | [diff] [blame] | 77 | |
| 78 | return info; |
| 79 | } |
Chia-I Wu | 681d7a0 | 2015-07-03 13:44:34 +0800 | [diff] [blame] | 80 | |
Chia-I Wu | f1e2e99 | 2014-12-27 14:12:52 +0800 | [diff] [blame] | 81 | } // namespace |
| 82 | |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 83 | namespace vk_testing { |
Chia-I Wu | f1e2e99 | 2014-12-27 14:12:52 +0800 | [diff] [blame] | 84 | |
| 85 | void set_error_callback(ErrorCallback callback) |
| 86 | { |
| 87 | error_callback = callback; |
| 88 | } |
| 89 | |
Chia-I Wu | 999f048 | 2015-07-03 10:32:05 +0800 | [diff] [blame] | 90 | VkPhysicalDeviceProperties PhysicalDevice::properties() const |
Chia-I Wu | f1e2e99 | 2014-12-27 14:12:52 +0800 | [diff] [blame] | 91 | { |
Tony Barbour | 59a4732 | 2015-06-24 16:06:58 -0600 | [diff] [blame] | 92 | VkPhysicalDeviceProperties info; |
| 93 | |
Courtney Goeltzenleuchter | 06d8947 | 2015-10-20 16:40:38 -0600 | [diff] [blame] | 94 | vkGetPhysicalDeviceProperties(handle(), &info); |
Tony Barbour | 59a4732 | 2015-06-24 16:06:58 -0600 | [diff] [blame] | 95 | |
| 96 | return info; |
Chia-I Wu | f1e2e99 | 2014-12-27 14:12:52 +0800 | [diff] [blame] | 97 | } |
| 98 | |
Cody Northrop | d080288 | 2015-08-03 17:04:53 -0600 | [diff] [blame] | 99 | std::vector<VkQueueFamilyProperties> PhysicalDevice::queue_properties() const |
Chia-I Wu | f1e2e99 | 2014-12-27 14:12:52 +0800 | [diff] [blame] | 100 | { |
Cody Northrop | d080288 | 2015-08-03 17:04:53 -0600 | [diff] [blame] | 101 | std::vector<VkQueueFamilyProperties> info; |
Tony Barbour | 59a4732 | 2015-06-24 16:06:58 -0600 | [diff] [blame] | 102 | uint32_t count; |
| 103 | |
Cody Northrop | d080288 | 2015-08-03 17:04:53 -0600 | [diff] [blame] | 104 | // Call once with NULL data to receive count |
Courtney Goeltzenleuchter | 06d8947 | 2015-10-20 16:40:38 -0600 | [diff] [blame] | 105 | vkGetPhysicalDeviceQueueFamilyProperties(handle(), &count, NULL); |
| 106 | info.resize(count); |
| 107 | vkGetPhysicalDeviceQueueFamilyProperties(handle(), &count, info.data()); |
Tony Barbour | 59a4732 | 2015-06-24 16:06:58 -0600 | [diff] [blame] | 108 | |
| 109 | return info; |
Chia-I Wu | f1e2e99 | 2014-12-27 14:12:52 +0800 | [diff] [blame] | 110 | } |
| 111 | |
Chia-I Wu | 999f048 | 2015-07-03 10:32:05 +0800 | [diff] [blame] | 112 | VkPhysicalDeviceMemoryProperties PhysicalDevice::memory_properties() const |
Chia-I Wu | f1e2e99 | 2014-12-27 14:12:52 +0800 | [diff] [blame] | 113 | { |
Tony Barbour | 59a4732 | 2015-06-24 16:06:58 -0600 | [diff] [blame] | 114 | VkPhysicalDeviceMemoryProperties info; |
| 115 | |
Courtney Goeltzenleuchter | 06d8947 | 2015-10-20 16:40:38 -0600 | [diff] [blame] | 116 | vkGetPhysicalDeviceMemoryProperties(handle(), &info); |
Mark Lobodzinski | b3fbcd9 | 2015-07-02 16:49:40 -0600 | [diff] [blame] | 117 | |
Tony Barbour | 59a4732 | 2015-06-24 16:06:58 -0600 | [diff] [blame] | 118 | return info; |
Chia-I Wu | f1e2e99 | 2014-12-27 14:12:52 +0800 | [diff] [blame] | 119 | } |
| 120 | |
Courtney Goeltzenleuchter | 110fdf9 | 2015-06-29 15:39:26 -0600 | [diff] [blame] | 121 | /* |
| 122 | * Return list of Global layers available |
| 123 | */ |
| 124 | std::vector<VkLayerProperties> GetGlobalLayers() |
Chia-I Wu | f1e2e99 | 2014-12-27 14:12:52 +0800 | [diff] [blame] | 125 | { |
Courtney Goeltzenleuchter | 110fdf9 | 2015-06-29 15:39:26 -0600 | [diff] [blame] | 126 | VkResult err; |
Courtney Goeltzenleuchter | 110fdf9 | 2015-06-29 15:39:26 -0600 | [diff] [blame] | 127 | std::vector<VkLayerProperties> layers; |
Courtney Goeltzenleuchter | cd69eee | 2015-07-06 09:10:47 -0600 | [diff] [blame] | 128 | uint32_t layer_count; |
Courtney Goeltzenleuchter | 110fdf9 | 2015-06-29 15:39:26 -0600 | [diff] [blame] | 129 | |
Courtney Goeltzenleuchter | cd69eee | 2015-07-06 09:10:47 -0600 | [diff] [blame] | 130 | do { |
| 131 | layer_count = 0; |
Courtney Goeltzenleuchter | 35985f6 | 2015-09-14 17:22:16 -0600 | [diff] [blame] | 132 | err = vkEnumerateInstanceLayerProperties(&layer_count, NULL); |
Courtney Goeltzenleuchter | cd69eee | 2015-07-06 09:10:47 -0600 | [diff] [blame] | 133 | |
| 134 | if (err == VK_SUCCESS) { |
| 135 | layers.reserve(layer_count); |
Courtney Goeltzenleuchter | 35985f6 | 2015-09-14 17:22:16 -0600 | [diff] [blame] | 136 | err = vkEnumerateInstanceLayerProperties(&layer_count, layers.data()); |
Courtney Goeltzenleuchter | cd69eee | 2015-07-06 09:10:47 -0600 | [diff] [blame] | 137 | } |
| 138 | } while (err == VK_INCOMPLETE); |
| 139 | |
Courtney Goeltzenleuchter | 110fdf9 | 2015-06-29 15:39:26 -0600 | [diff] [blame] | 140 | assert(err == VK_SUCCESS); |
| 141 | |
| 142 | return layers; |
Chia-I Wu | f1e2e99 | 2014-12-27 14:12:52 +0800 | [diff] [blame] | 143 | } |
| 144 | |
Courtney Goeltzenleuchter | 110fdf9 | 2015-06-29 15:39:26 -0600 | [diff] [blame] | 145 | /* |
| 146 | * Return list of Global extensions provided by the ICD / Loader |
| 147 | */ |
| 148 | std::vector<VkExtensionProperties> GetGlobalExtensions() |
Chia-I Wu | f1e2e99 | 2014-12-27 14:12:52 +0800 | [diff] [blame] | 149 | { |
Courtney Goeltzenleuchter | 110fdf9 | 2015-06-29 15:39:26 -0600 | [diff] [blame] | 150 | return GetGlobalExtensions(NULL); |
| 151 | } |
| 152 | |
| 153 | /* |
| 154 | * Return list of Global extensions provided by the specified layer |
| 155 | * If pLayerName is NULL, will return extensions implemented by the loader / ICDs |
| 156 | */ |
| 157 | std::vector<VkExtensionProperties> GetGlobalExtensions(const char *pLayerName) |
| 158 | { |
Courtney Goeltzenleuchter | cd69eee | 2015-07-06 09:10:47 -0600 | [diff] [blame] | 159 | std::vector<VkExtensionProperties> exts; |
| 160 | uint32_t ext_count; |
Courtney Goeltzenleuchter | 110fdf9 | 2015-06-29 15:39:26 -0600 | [diff] [blame] | 161 | VkResult err; |
| 162 | |
Courtney Goeltzenleuchter | cd69eee | 2015-07-06 09:10:47 -0600 | [diff] [blame] | 163 | do { |
| 164 | ext_count = 0; |
Courtney Goeltzenleuchter | 35985f6 | 2015-09-14 17:22:16 -0600 | [diff] [blame] | 165 | err = vkEnumerateInstanceExtensionProperties(pLayerName, &ext_count, NULL); |
Courtney Goeltzenleuchter | 110fdf9 | 2015-06-29 15:39:26 -0600 | [diff] [blame] | 166 | |
Courtney Goeltzenleuchter | cd69eee | 2015-07-06 09:10:47 -0600 | [diff] [blame] | 167 | if (err == VK_SUCCESS) { |
Courtney Goeltzenleuchter | 381f3a2 | 2015-07-06 15:45:58 -0600 | [diff] [blame] | 168 | exts.resize(ext_count); |
Courtney Goeltzenleuchter | 35985f6 | 2015-09-14 17:22:16 -0600 | [diff] [blame] | 169 | err = vkEnumerateInstanceExtensionProperties(pLayerName, &ext_count, exts.data()); |
Courtney Goeltzenleuchter | cd69eee | 2015-07-06 09:10:47 -0600 | [diff] [blame] | 170 | } |
| 171 | } while (err == VK_INCOMPLETE); |
| 172 | |
Courtney Goeltzenleuchter | 110fdf9 | 2015-06-29 15:39:26 -0600 | [diff] [blame] | 173 | assert(err == VK_SUCCESS); |
| 174 | |
| 175 | return exts; |
| 176 | } |
| 177 | |
| 178 | /* |
| 179 | * Return list of PhysicalDevice extensions provided by the ICD / Loader |
| 180 | */ |
Chia-I Wu | 999f048 | 2015-07-03 10:32:05 +0800 | [diff] [blame] | 181 | std::vector<VkExtensionProperties> PhysicalDevice::extensions() const |
Courtney Goeltzenleuchter | 110fdf9 | 2015-06-29 15:39:26 -0600 | [diff] [blame] | 182 | { |
| 183 | return extensions(NULL); |
| 184 | } |
| 185 | |
| 186 | /* |
| 187 | * Return list of PhysicalDevice extensions provided by the specified layer |
| 188 | * If pLayerName is NULL, will return extensions for ICD / loader. |
| 189 | */ |
Chia-I Wu | 999f048 | 2015-07-03 10:32:05 +0800 | [diff] [blame] | 190 | std::vector<VkExtensionProperties> PhysicalDevice::extensions(const char *pLayerName) const |
Courtney Goeltzenleuchter | 110fdf9 | 2015-06-29 15:39:26 -0600 | [diff] [blame] | 191 | { |
Courtney Goeltzenleuchter | cd69eee | 2015-07-06 09:10:47 -0600 | [diff] [blame] | 192 | std::vector<VkExtensionProperties> exts; |
Courtney Goeltzenleuchter | 110fdf9 | 2015-06-29 15:39:26 -0600 | [diff] [blame] | 193 | VkResult err; |
| 194 | |
Courtney Goeltzenleuchter | cd69eee | 2015-07-06 09:10:47 -0600 | [diff] [blame] | 195 | do { |
| 196 | uint32_t extCount = 0; |
Courtney Goeltzenleuchter | 35985f6 | 2015-09-14 17:22:16 -0600 | [diff] [blame] | 197 | err = vkEnumerateDeviceExtensionProperties(handle(), pLayerName, &extCount, NULL); |
Courtney Goeltzenleuchter | 110fdf9 | 2015-06-29 15:39:26 -0600 | [diff] [blame] | 198 | |
Courtney Goeltzenleuchter | cd69eee | 2015-07-06 09:10:47 -0600 | [diff] [blame] | 199 | if (err == VK_SUCCESS) { |
Ian Elliott | 1a3845b | 2015-07-06 14:33:04 -0600 | [diff] [blame] | 200 | exts.resize(extCount); |
Courtney Goeltzenleuchter | 35985f6 | 2015-09-14 17:22:16 -0600 | [diff] [blame] | 201 | err = vkEnumerateDeviceExtensionProperties(handle(), pLayerName, &extCount, exts.data()); |
Courtney Goeltzenleuchter | cd69eee | 2015-07-06 09:10:47 -0600 | [diff] [blame] | 202 | } |
| 203 | } while (err == VK_INCOMPLETE); |
| 204 | |
Courtney Goeltzenleuchter | 110fdf9 | 2015-06-29 15:39:26 -0600 | [diff] [blame] | 205 | assert(err == VK_SUCCESS); |
Chia-I Wu | f1e2e99 | 2014-12-27 14:12:52 +0800 | [diff] [blame] | 206 | |
| 207 | return exts; |
| 208 | } |
| 209 | |
Courtney Goeltzenleuchter | 1d2f0dd | 2015-10-22 11:03:31 -0600 | [diff] [blame] | 210 | bool PhysicalDevice::set_memory_type(const uint32_t type_bits, VkMemoryAllocInfo *info, const VkFlags properties, const VkFlags forbid) const |
Mark Lobodzinski | b3fbcd9 | 2015-07-02 16:49:40 -0600 | [diff] [blame] | 211 | { |
| 212 | uint32_t type_mask = type_bits; |
| 213 | // Search memtypes to find first index with those properties |
| 214 | for (uint32_t i = 0; i < 32; i++) { |
| 215 | if ((type_mask & 1) == 1) { |
| 216 | // Type is available, does it match user properties? |
Mike Stroyan | 713b2d7 | 2015-08-04 10:49:29 -0600 | [diff] [blame] | 217 | if ((memory_properties_.memoryTypes[i].propertyFlags & properties) == properties && |
| 218 | (memory_properties_.memoryTypes[i].propertyFlags & forbid) == 0) { |
Mark Lobodzinski | b3fbcd9 | 2015-07-02 16:49:40 -0600 | [diff] [blame] | 219 | info->memoryTypeIndex = i; |
Courtney Goeltzenleuchter | 1d2f0dd | 2015-10-22 11:03:31 -0600 | [diff] [blame] | 220 | return true; |
Mark Lobodzinski | b3fbcd9 | 2015-07-02 16:49:40 -0600 | [diff] [blame] | 221 | } |
| 222 | } |
| 223 | type_mask >>= 1; |
| 224 | } |
| 225 | // No memory types matched, return failure |
Courtney Goeltzenleuchter | 1d2f0dd | 2015-10-22 11:03:31 -0600 | [diff] [blame] | 226 | return false; |
Mark Lobodzinski | b3fbcd9 | 2015-07-02 16:49:40 -0600 | [diff] [blame] | 227 | } |
| 228 | |
Courtney Goeltzenleuchter | cd69eee | 2015-07-06 09:10:47 -0600 | [diff] [blame] | 229 | /* |
| 230 | * Return list of PhysicalDevice layers |
| 231 | */ |
Chia-I Wu | 999f048 | 2015-07-03 10:32:05 +0800 | [diff] [blame] | 232 | std::vector<VkLayerProperties> PhysicalDevice::layers() const |
Courtney Goeltzenleuchter | cd69eee | 2015-07-06 09:10:47 -0600 | [diff] [blame] | 233 | { |
| 234 | std::vector<VkLayerProperties> layer_props; |
| 235 | VkResult err; |
| 236 | |
| 237 | do { |
| 238 | uint32_t layer_count = 0; |
Courtney Goeltzenleuchter | 35985f6 | 2015-09-14 17:22:16 -0600 | [diff] [blame] | 239 | err = vkEnumerateDeviceLayerProperties(handle(), &layer_count, NULL); |
Courtney Goeltzenleuchter | cd69eee | 2015-07-06 09:10:47 -0600 | [diff] [blame] | 240 | |
| 241 | if (err == VK_SUCCESS) { |
| 242 | layer_props.reserve(layer_count); |
Courtney Goeltzenleuchter | 35985f6 | 2015-09-14 17:22:16 -0600 | [diff] [blame] | 243 | err = vkEnumerateDeviceLayerProperties(handle(), &layer_count, layer_props.data()); |
Courtney Goeltzenleuchter | cd69eee | 2015-07-06 09:10:47 -0600 | [diff] [blame] | 244 | } |
| 245 | } while (err == VK_INCOMPLETE); |
| 246 | |
| 247 | assert(err == VK_SUCCESS); |
| 248 | |
| 249 | return layer_props; |
| 250 | } |
| 251 | |
Chia-I Wu | f1e2e99 | 2014-12-27 14:12:52 +0800 | [diff] [blame] | 252 | Device::~Device() |
| 253 | { |
| 254 | if (!initialized()) |
| 255 | return; |
| 256 | |
| 257 | for (int i = 0; i < QUEUE_COUNT; i++) { |
| 258 | for (std::vector<Queue *>::iterator it = queues_[i].begin(); it != queues_[i].end(); it++) |
| 259 | delete *it; |
| 260 | queues_[i].clear(); |
| 261 | } |
| 262 | |
Mark Lobodzinski | 2141f65 | 2015-09-07 13:59:43 -0600 | [diff] [blame] | 263 | vkDestroyDevice(handle()); |
Chia-I Wu | f1e2e99 | 2014-12-27 14:12:52 +0800 | [diff] [blame] | 264 | } |
| 265 | |
Courtney Goeltzenleuchter | 5bac609 | 2015-07-07 11:47:33 -0600 | [diff] [blame] | 266 | void Device::init(std::vector<const char *> &layers, std::vector<const char *> &extensions) |
Chia-I Wu | f1e2e99 | 2014-12-27 14:12:52 +0800 | [diff] [blame] | 267 | { |
| 268 | // request all queues |
Cody Northrop | d080288 | 2015-08-03 17:04:53 -0600 | [diff] [blame] | 269 | const std::vector<VkQueueFamilyProperties> queue_props = phy_.queue_properties(); |
Courtney Goeltzenleuchter | 95487bc | 2015-04-14 18:48:46 -0600 | [diff] [blame] | 270 | std::vector<VkDeviceQueueCreateInfo> queue_info; |
Chia-I Wu | f1e2e99 | 2014-12-27 14:12:52 +0800 | [diff] [blame] | 271 | queue_info.reserve(queue_props.size()); |
| 272 | for (int i = 0; i < queue_props.size(); i++) { |
Courtney Goeltzenleuchter | 95487bc | 2015-04-14 18:48:46 -0600 | [diff] [blame] | 273 | VkDeviceQueueCreateInfo qi = {}; |
Courtney Goeltzenleuchter | 73db5bf | 2015-09-16 16:23:55 -0600 | [diff] [blame] | 274 | qi.sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO; |
| 275 | qi.pNext = NULL; |
Chris Forbes | 9e50dab | 2015-07-11 19:11:39 +1200 | [diff] [blame] | 276 | qi.queueFamilyIndex = i; |
Chia-I Wu | f1e2e99 | 2014-12-27 14:12:52 +0800 | [diff] [blame] | 277 | qi.queueCount = queue_props[i].queueCount; |
Courtney Goeltzenleuchter | 1a606eb | 2015-10-23 10:37:02 -0600 | [diff] [blame] | 278 | std::vector<float> queue_priorities (qi.queueCount, 0.0); |
| 279 | qi.pQueuePriorities = queue_priorities.data(); |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 280 | if (queue_props[i].queueFlags & VK_QUEUE_GRAPHICS_BIT) { |
Courtney Goeltzenleuchter | 18248e6 | 2015-03-05 18:09:39 -0700 | [diff] [blame] | 281 | graphics_queue_node_index_ = i; |
| 282 | } |
Chia-I Wu | f1e2e99 | 2014-12-27 14:12:52 +0800 | [diff] [blame] | 283 | queue_info.push_back(qi); |
| 284 | } |
| 285 | |
Courtney Goeltzenleuchter | 95487bc | 2015-04-14 18:48:46 -0600 | [diff] [blame] | 286 | VkDeviceCreateInfo dev_info = {}; |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 287 | dev_info.sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO; |
Courtney Goeltzenleuchter | f579fa6 | 2015-06-10 17:39:03 -0600 | [diff] [blame] | 288 | dev_info.pNext = NULL; |
Courtney Goeltzenleuchter | 7831e96 | 2015-10-15 16:58:44 -0600 | [diff] [blame] | 289 | dev_info.requestedQueueCount = queue_info.size(); |
Tony Barbour | 482c609 | 2015-07-27 09:37:48 -0600 | [diff] [blame] | 290 | dev_info.pRequestedQueues = queue_info.data(); |
Courtney Goeltzenleuchter | 5bac609 | 2015-07-07 11:47:33 -0600 | [diff] [blame] | 291 | dev_info.layerCount = layers.size(); |
Tony Barbour | 482c609 | 2015-07-27 09:37:48 -0600 | [diff] [blame] | 292 | dev_info.ppEnabledLayerNames = layers.data(); |
Courtney Goeltzenleuchter | f579fa6 | 2015-06-10 17:39:03 -0600 | [diff] [blame] | 293 | dev_info.extensionCount = extensions.size(); |
Tony Barbour | 482c609 | 2015-07-27 09:37:48 -0600 | [diff] [blame] | 294 | dev_info.ppEnabledExtensionNames = extensions.data(); |
Chia-I Wu | f1e2e99 | 2014-12-27 14:12:52 +0800 | [diff] [blame] | 295 | |
| 296 | init(dev_info); |
| 297 | } |
| 298 | |
Courtney Goeltzenleuchter | 95487bc | 2015-04-14 18:48:46 -0600 | [diff] [blame] | 299 | void Device::init(const VkDeviceCreateInfo &info) |
Chia-I Wu | f1e2e99 | 2014-12-27 14:12:52 +0800 | [diff] [blame] | 300 | { |
Chia-I Wu | f368b60 | 2015-07-03 10:41:20 +0800 | [diff] [blame] | 301 | VkDevice dev; |
| 302 | |
| 303 | if (EXPECT(vkCreateDevice(phy_.handle(), &info, &dev) == VK_SUCCESS)) |
| 304 | Handle::init(dev); |
Chia-I Wu | f1e2e99 | 2014-12-27 14:12:52 +0800 | [diff] [blame] | 305 | |
| 306 | init_queues(); |
Chia-I Wu | f1e2e99 | 2014-12-27 14:12:52 +0800 | [diff] [blame] | 307 | init_formats(); |
| 308 | } |
| 309 | |
| 310 | void Device::init_queues() |
| 311 | { |
Courtney Goeltzenleuchter | 18248e6 | 2015-03-05 18:09:39 -0700 | [diff] [blame] | 312 | uint32_t queue_node_count; |
Chia-I Wu | f1e2e99 | 2014-12-27 14:12:52 +0800 | [diff] [blame] | 313 | |
Cody Northrop | d080288 | 2015-08-03 17:04:53 -0600 | [diff] [blame] | 314 | // Call with NULL data to get count |
Courtney Goeltzenleuchter | 06d8947 | 2015-10-20 16:40:38 -0600 | [diff] [blame] | 315 | vkGetPhysicalDeviceQueueFamilyProperties(phy_.handle(), &queue_node_count, NULL); |
Courtney Goeltzenleuchter | 18248e6 | 2015-03-05 18:09:39 -0700 | [diff] [blame] | 316 | EXPECT(queue_node_count >= 1); |
| 317 | |
Cody Northrop | d080288 | 2015-08-03 17:04:53 -0600 | [diff] [blame] | 318 | VkQueueFamilyProperties* queue_props = new VkQueueFamilyProperties[queue_node_count]; |
Courtney Goeltzenleuchter | 18248e6 | 2015-03-05 18:09:39 -0700 | [diff] [blame] | 319 | |
Courtney Goeltzenleuchter | 06d8947 | 2015-10-20 16:40:38 -0600 | [diff] [blame] | 320 | vkGetPhysicalDeviceQueueFamilyProperties(phy_.handle(), &queue_node_count, queue_props); |
Courtney Goeltzenleuchter | 18248e6 | 2015-03-05 18:09:39 -0700 | [diff] [blame] | 321 | |
Tony Barbour | 7ea6aa2 | 2015-05-22 09:44:58 -0600 | [diff] [blame] | 322 | for (uint32_t i = 0; i < queue_node_count; i++) { |
Courtney Goeltzenleuchter | fb4efc6 | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 323 | VkQueue queue; |
Courtney Goeltzenleuchter | 18248e6 | 2015-03-05 18:09:39 -0700 | [diff] [blame] | 324 | |
Tony Barbour | 7ea6aa2 | 2015-05-22 09:44:58 -0600 | [diff] [blame] | 325 | for (uint32_t j = 0; j < queue_props[i].queueCount; j++) { |
Mark Lobodzinski | 40f7f40 | 2015-04-16 11:44:05 -0500 | [diff] [blame] | 326 | // TODO: Need to add support for separate MEMMGR and work queues, including synchronization |
Courtney Goeltzenleuchter | 06d8947 | 2015-10-20 16:40:38 -0600 | [diff] [blame] | 327 | vkGetDeviceQueue(handle(), i, j, &queue); |
Courtney Goeltzenleuchter | 18248e6 | 2015-03-05 18:09:39 -0700 | [diff] [blame] | 328 | |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 329 | if (queue_props[i].queueFlags & VK_QUEUE_GRAPHICS_BIT) { |
Tony Barbour | fb21ea3 | 2015-07-23 10:35:30 -0600 | [diff] [blame] | 330 | queues_[GRAPHICS].push_back(new Queue(queue, i)); |
Courtney Goeltzenleuchter | 18248e6 | 2015-03-05 18:09:39 -0700 | [diff] [blame] | 331 | } |
| 332 | |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 333 | if (queue_props[i].queueFlags & VK_QUEUE_COMPUTE_BIT) { |
Tony Barbour | fb21ea3 | 2015-07-23 10:35:30 -0600 | [diff] [blame] | 334 | queues_[COMPUTE].push_back(new Queue(queue, i)); |
Courtney Goeltzenleuchter | 18248e6 | 2015-03-05 18:09:39 -0700 | [diff] [blame] | 335 | } |
| 336 | |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 337 | if (queue_props[i].queueFlags & VK_QUEUE_DMA_BIT) { |
Tony Barbour | fb21ea3 | 2015-07-23 10:35:30 -0600 | [diff] [blame] | 338 | queues_[DMA].push_back(new Queue(queue, i)); |
Courtney Goeltzenleuchter | 18248e6 | 2015-03-05 18:09:39 -0700 | [diff] [blame] | 339 | } |
Chia-I Wu | f1e2e99 | 2014-12-27 14:12:52 +0800 | [diff] [blame] | 340 | } |
| 341 | } |
| 342 | |
Chris Forbes | 0203879 | 2015-06-04 10:49:27 +1200 | [diff] [blame] | 343 | delete[] queue_props; |
Tony Barbour | 3d69c9e | 2015-05-20 16:53:31 -0600 | [diff] [blame] | 344 | |
Chia-I Wu | f1e2e99 | 2014-12-27 14:12:52 +0800 | [diff] [blame] | 345 | EXPECT(!queues_[GRAPHICS].empty() || !queues_[COMPUTE].empty()); |
| 346 | } |
| 347 | |
Chia-I Wu | f1e2e99 | 2014-12-27 14:12:52 +0800 | [diff] [blame] | 348 | void Device::init_formats() |
| 349 | { |
Tony Barbour | d1c3572 | 2015-04-16 15:59:00 -0600 | [diff] [blame] | 350 | for (int f = VK_FORMAT_BEGIN_RANGE; f <= VK_FORMAT_END_RANGE; f++) { |
Courtney Goeltzenleuchter | fb4efc6 | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 351 | const VkFormat fmt = static_cast<VkFormat>(f); |
| 352 | const VkFormatProperties props = format_properties(fmt); |
Chia-I Wu | f1e2e99 | 2014-12-27 14:12:52 +0800 | [diff] [blame] | 353 | |
Jeremy Hayes | a058eee | 2015-01-23 08:51:43 -0700 | [diff] [blame] | 354 | if (props.linearTilingFeatures) { |
Tony Barbour | d1c3572 | 2015-04-16 15:59:00 -0600 | [diff] [blame] | 355 | const Format tmp = { fmt, VK_IMAGE_TILING_LINEAR, props.linearTilingFeatures }; |
Jeremy Hayes | a058eee | 2015-01-23 08:51:43 -0700 | [diff] [blame] | 356 | formats_.push_back(tmp); |
| 357 | } |
Chia-I Wu | f1e2e99 | 2014-12-27 14:12:52 +0800 | [diff] [blame] | 358 | |
Jeremy Hayes | a058eee | 2015-01-23 08:51:43 -0700 | [diff] [blame] | 359 | if (props.optimalTilingFeatures) { |
Tony Barbour | d1c3572 | 2015-04-16 15:59:00 -0600 | [diff] [blame] | 360 | const Format tmp = { fmt, VK_IMAGE_TILING_OPTIMAL, props.optimalTilingFeatures }; |
Jeremy Hayes | a058eee | 2015-01-23 08:51:43 -0700 | [diff] [blame] | 361 | formats_.push_back(tmp); |
Chia-I Wu | f1e2e99 | 2014-12-27 14:12:52 +0800 | [diff] [blame] | 362 | } |
| 363 | } |
| 364 | |
| 365 | EXPECT(!formats_.empty()); |
| 366 | } |
| 367 | |
Courtney Goeltzenleuchter | fb4efc6 | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 368 | VkFormatProperties Device::format_properties(VkFormat format) |
Chia-I Wu | f1e2e99 | 2014-12-27 14:12:52 +0800 | [diff] [blame] | 369 | { |
Courtney Goeltzenleuchter | fb4efc6 | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 370 | VkFormatProperties data; |
Courtney Goeltzenleuchter | 06d8947 | 2015-10-20 16:40:38 -0600 | [diff] [blame] | 371 | vkGetPhysicalDeviceFormatProperties(phy().handle(), format, &data); |
Chia-I Wu | f1e2e99 | 2014-12-27 14:12:52 +0800 | [diff] [blame] | 372 | |
| 373 | return data; |
| 374 | } |
| 375 | |
| 376 | void Device::wait() |
| 377 | { |
Chia-I Wu | f368b60 | 2015-07-03 10:41:20 +0800 | [diff] [blame] | 378 | EXPECT(vkDeviceWaitIdle(handle()) == VK_SUCCESS); |
Chia-I Wu | f1e2e99 | 2014-12-27 14:12:52 +0800 | [diff] [blame] | 379 | } |
| 380 | |
Courtney Goeltzenleuchter | fb4efc6 | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 381 | VkResult Device::wait(const std::vector<const Fence *> &fences, bool wait_all, uint64_t timeout) |
Chia-I Wu | f1e2e99 | 2014-12-27 14:12:52 +0800 | [diff] [blame] | 382 | { |
Chia-I Wu | d9e8e82 | 2015-07-03 11:45:55 +0800 | [diff] [blame] | 383 | const std::vector<VkFence> fence_handles = make_handles<VkFence>(fences); |
Tony Barbour | 482c609 | 2015-07-27 09:37:48 -0600 | [diff] [blame] | 384 | VkResult err = vkWaitForFences(handle(), fence_handles.size(), fence_handles.data(), wait_all, timeout); |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 385 | EXPECT(err == VK_SUCCESS || err == VK_TIMEOUT); |
Chia-I Wu | f1e2e99 | 2014-12-27 14:12:52 +0800 | [diff] [blame] | 386 | |
| 387 | return err; |
| 388 | } |
| 389 | |
Mark Lobodzinski | 2141f65 | 2015-09-07 13:59:43 -0600 | [diff] [blame] | 390 | void Device::update_descriptor_sets(const std::vector<VkWriteDescriptorSet> &writes, const std::vector<VkCopyDescriptorSet> &copies) |
Chia-I Wu | 9d00ed7 | 2015-05-25 16:27:55 +0800 | [diff] [blame] | 391 | { |
Mark Lobodzinski | 2141f65 | 2015-09-07 13:59:43 -0600 | [diff] [blame] | 392 | vkUpdateDescriptorSets(handle(), writes.size(), writes.data(), copies.size(), copies.data()); |
Chia-I Wu | 9d00ed7 | 2015-05-25 16:27:55 +0800 | [diff] [blame] | 393 | } |
| 394 | |
Courtney Goeltzenleuchter | 97b7523 | 2015-04-07 17:13:38 -0600 | [diff] [blame] | 395 | void Queue::submit(const std::vector<const CmdBuffer *> &cmds, Fence &fence) |
Chia-I Wu | f1e2e99 | 2014-12-27 14:12:52 +0800 | [diff] [blame] | 396 | { |
Chia-I Wu | be2b917 | 2015-07-03 11:49:42 +0800 | [diff] [blame] | 397 | const std::vector<VkCmdBuffer> cmd_handles = make_handles<VkCmdBuffer>(cmds); |
Courtney Goeltzenleuchter | 806c700 | 2015-10-27 11:22:14 -0600 | [diff] [blame^] | 398 | VkSubmitInfo submit_info; |
| 399 | submit_info.waitSemCount = 0; |
| 400 | submit_info.pWaitSemaphores = NULL; |
| 401 | submit_info.cmdBufferCount = (uint32_t)cmd_handles.size(); |
| 402 | submit_info.pCommandBuffers = cmd_handles.data(); |
| 403 | submit_info.signalSemCount = 0; |
| 404 | submit_info.pSignalSemaphores = NULL; |
Courtney Goeltzenleuchter | 646b907 | 2015-10-20 18:04:07 -0600 | [diff] [blame] | 405 | |
| 406 | EXPECT(vkQueueSubmit(handle(), 1, &submit_info, fence.handle()) == VK_SUCCESS); |
Chia-I Wu | f1e2e99 | 2014-12-27 14:12:52 +0800 | [diff] [blame] | 407 | } |
| 408 | |
Courtney Goeltzenleuchter | 97b7523 | 2015-04-07 17:13:38 -0600 | [diff] [blame] | 409 | void Queue::submit(const CmdBuffer &cmd, Fence &fence) |
Chia-I Wu | f1e2e99 | 2014-12-27 14:12:52 +0800 | [diff] [blame] | 410 | { |
Courtney Goeltzenleuchter | 97b7523 | 2015-04-07 17:13:38 -0600 | [diff] [blame] | 411 | submit(std::vector<const CmdBuffer*>(1, &cmd), fence); |
Chia-I Wu | f1e2e99 | 2014-12-27 14:12:52 +0800 | [diff] [blame] | 412 | } |
| 413 | |
Courtney Goeltzenleuchter | 97b7523 | 2015-04-07 17:13:38 -0600 | [diff] [blame] | 414 | void Queue::submit(const CmdBuffer &cmd) |
Chia-I Wu | f1e2e99 | 2014-12-27 14:12:52 +0800 | [diff] [blame] | 415 | { |
| 416 | Fence fence; |
Courtney Goeltzenleuchter | 97b7523 | 2015-04-07 17:13:38 -0600 | [diff] [blame] | 417 | submit(cmd, fence); |
Chia-I Wu | f1e2e99 | 2014-12-27 14:12:52 +0800 | [diff] [blame] | 418 | } |
| 419 | |
Chia-I Wu | f1e2e99 | 2014-12-27 14:12:52 +0800 | [diff] [blame] | 420 | void Queue::wait() |
| 421 | { |
Chia-I Wu | df12ffd | 2015-07-03 10:53:18 +0800 | [diff] [blame] | 422 | EXPECT(vkQueueWaitIdle(handle()) == VK_SUCCESS); |
Chia-I Wu | f1e2e99 | 2014-12-27 14:12:52 +0800 | [diff] [blame] | 423 | } |
| 424 | |
Courtney Goeltzenleuchter | ebb9584 | 2015-03-25 17:14:29 -0600 | [diff] [blame] | 425 | void Queue::signal_semaphore(Semaphore &sem) |
Chia-I Wu | f1e2e99 | 2014-12-27 14:12:52 +0800 | [diff] [blame] | 426 | { |
Chia-I Wu | 6b1c248 | 2015-07-03 11:49:42 +0800 | [diff] [blame] | 427 | EXPECT(vkQueueSignalSemaphore(handle(), sem.handle()) == VK_SUCCESS); |
Chia-I Wu | f1e2e99 | 2014-12-27 14:12:52 +0800 | [diff] [blame] | 428 | } |
| 429 | |
Courtney Goeltzenleuchter | ebb9584 | 2015-03-25 17:14:29 -0600 | [diff] [blame] | 430 | void Queue::wait_semaphore(Semaphore &sem) |
Chia-I Wu | f1e2e99 | 2014-12-27 14:12:52 +0800 | [diff] [blame] | 431 | { |
Chia-I Wu | 6b1c248 | 2015-07-03 11:49:42 +0800 | [diff] [blame] | 432 | EXPECT(vkQueueWaitSemaphore(handle(), sem.handle()) == VK_SUCCESS); |
Chia-I Wu | f1e2e99 | 2014-12-27 14:12:52 +0800 | [diff] [blame] | 433 | } |
| 434 | |
Chia-I Wu | f8f074f | 2015-07-03 10:58:57 +0800 | [diff] [blame] | 435 | DeviceMemory::~DeviceMemory() |
Chia-I Wu | f1e2e99 | 2014-12-27 14:12:52 +0800 | [diff] [blame] | 436 | { |
Chia-I Wu | f8f074f | 2015-07-03 10:58:57 +0800 | [diff] [blame] | 437 | if (initialized()) |
Mark Lobodzinski | 2141f65 | 2015-09-07 13:59:43 -0600 | [diff] [blame] | 438 | vkFreeMemory(device(), handle()); |
Chia-I Wu | f1e2e99 | 2014-12-27 14:12:52 +0800 | [diff] [blame] | 439 | } |
| 440 | |
Chia-I Wu | f8f074f | 2015-07-03 10:58:57 +0800 | [diff] [blame] | 441 | void DeviceMemory::init(const Device &dev, const VkMemoryAllocInfo &info) |
Chia-I Wu | f1e2e99 | 2014-12-27 14:12:52 +0800 | [diff] [blame] | 442 | { |
Chia-I Wu | f8f074f | 2015-07-03 10:58:57 +0800 | [diff] [blame] | 443 | NON_DISPATCHABLE_HANDLE_INIT(vkAllocMemory, dev, &info); |
Chia-I Wu | f1e2e99 | 2014-12-27 14:12:52 +0800 | [diff] [blame] | 444 | } |
| 445 | |
Chia-I Wu | f8f074f | 2015-07-03 10:58:57 +0800 | [diff] [blame] | 446 | const void *DeviceMemory::map(VkFlags flags) const |
Chia-I Wu | f1e2e99 | 2014-12-27 14:12:52 +0800 | [diff] [blame] | 447 | { |
| 448 | void *data; |
Chia-I Wu | f8f074f | 2015-07-03 10:58:57 +0800 | [diff] [blame] | 449 | if (!EXPECT(vkMapMemory(device(), handle(), 0 ,0, flags, &data) == VK_SUCCESS)) |
Chia-I Wu | f1e2e99 | 2014-12-27 14:12:52 +0800 | [diff] [blame] | 450 | data = NULL; |
| 451 | |
| 452 | return data; |
| 453 | } |
| 454 | |
Chia-I Wu | f8f074f | 2015-07-03 10:58:57 +0800 | [diff] [blame] | 455 | void *DeviceMemory::map(VkFlags flags) |
Chia-I Wu | f1e2e99 | 2014-12-27 14:12:52 +0800 | [diff] [blame] | 456 | { |
| 457 | void *data; |
Chia-I Wu | f8f074f | 2015-07-03 10:58:57 +0800 | [diff] [blame] | 458 | if (!EXPECT(vkMapMemory(device(), handle(), 0, 0, flags, &data) == VK_SUCCESS)) |
Chia-I Wu | f1e2e99 | 2014-12-27 14:12:52 +0800 | [diff] [blame] | 459 | data = NULL; |
| 460 | |
| 461 | return data; |
| 462 | } |
| 463 | |
Chia-I Wu | f8f074f | 2015-07-03 10:58:57 +0800 | [diff] [blame] | 464 | void DeviceMemory::unmap() const |
Chia-I Wu | f1e2e99 | 2014-12-27 14:12:52 +0800 | [diff] [blame] | 465 | { |
Mark Lobodzinski | 2141f65 | 2015-09-07 13:59:43 -0600 | [diff] [blame] | 466 | vkUnmapMemory(device(), handle()); |
Chia-I Wu | f1e2e99 | 2014-12-27 14:12:52 +0800 | [diff] [blame] | 467 | } |
| 468 | |
Tony Barbour | 67e9915 | 2015-07-10 14:10:27 -0600 | [diff] [blame] | 469 | NON_DISPATCHABLE_HANDLE_DTOR(Fence, vkDestroyFence) |
Chia-I Wu | d9e8e82 | 2015-07-03 11:45:55 +0800 | [diff] [blame] | 470 | |
Courtney Goeltzenleuchter | fb4efc6 | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 471 | void Fence::init(const Device &dev, const VkFenceCreateInfo &info) |
Chia-I Wu | f1e2e99 | 2014-12-27 14:12:52 +0800 | [diff] [blame] | 472 | { |
Chia-I Wu | d9e8e82 | 2015-07-03 11:45:55 +0800 | [diff] [blame] | 473 | NON_DISPATCHABLE_HANDLE_INIT(vkCreateFence, dev, &info); |
Chia-I Wu | f1e2e99 | 2014-12-27 14:12:52 +0800 | [diff] [blame] | 474 | } |
| 475 | |
Tony Barbour | 67e9915 | 2015-07-10 14:10:27 -0600 | [diff] [blame] | 476 | NON_DISPATCHABLE_HANDLE_DTOR(Semaphore, vkDestroySemaphore) |
Chia-I Wu | 6b1c248 | 2015-07-03 11:49:42 +0800 | [diff] [blame] | 477 | |
Courtney Goeltzenleuchter | fb4efc6 | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 478 | void Semaphore::init(const Device &dev, const VkSemaphoreCreateInfo &info) |
Chia-I Wu | f1e2e99 | 2014-12-27 14:12:52 +0800 | [diff] [blame] | 479 | { |
Chia-I Wu | 6b1c248 | 2015-07-03 11:49:42 +0800 | [diff] [blame] | 480 | NON_DISPATCHABLE_HANDLE_INIT(vkCreateSemaphore, dev, &info); |
Chia-I Wu | f1e2e99 | 2014-12-27 14:12:52 +0800 | [diff] [blame] | 481 | } |
| 482 | |
Tony Barbour | 67e9915 | 2015-07-10 14:10:27 -0600 | [diff] [blame] | 483 | NON_DISPATCHABLE_HANDLE_DTOR(Event, vkDestroyEvent) |
Chia-I Wu | c5c9799 | 2015-07-03 11:49:42 +0800 | [diff] [blame] | 484 | |
Courtney Goeltzenleuchter | fb4efc6 | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 485 | void Event::init(const Device &dev, const VkEventCreateInfo &info) |
Chia-I Wu | f1e2e99 | 2014-12-27 14:12:52 +0800 | [diff] [blame] | 486 | { |
Chia-I Wu | c5c9799 | 2015-07-03 11:49:42 +0800 | [diff] [blame] | 487 | NON_DISPATCHABLE_HANDLE_INIT(vkCreateEvent, dev, &info); |
Chia-I Wu | f1e2e99 | 2014-12-27 14:12:52 +0800 | [diff] [blame] | 488 | } |
| 489 | |
| 490 | void Event::set() |
| 491 | { |
Chia-I Wu | c5c9799 | 2015-07-03 11:49:42 +0800 | [diff] [blame] | 492 | EXPECT(vkSetEvent(device(), handle()) == VK_SUCCESS); |
Chia-I Wu | f1e2e99 | 2014-12-27 14:12:52 +0800 | [diff] [blame] | 493 | } |
| 494 | |
| 495 | void Event::reset() |
| 496 | { |
Chia-I Wu | c5c9799 | 2015-07-03 11:49:42 +0800 | [diff] [blame] | 497 | EXPECT(vkResetEvent(device(), handle()) == VK_SUCCESS); |
Chia-I Wu | f1e2e99 | 2014-12-27 14:12:52 +0800 | [diff] [blame] | 498 | } |
| 499 | |
Tony Barbour | 67e9915 | 2015-07-10 14:10:27 -0600 | [diff] [blame] | 500 | NON_DISPATCHABLE_HANDLE_DTOR(QueryPool, vkDestroyQueryPool) |
Chia-I Wu | 1b7d476 | 2015-07-03 11:49:42 +0800 | [diff] [blame] | 501 | |
Courtney Goeltzenleuchter | fb4efc6 | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 502 | void QueryPool::init(const Device &dev, const VkQueryPoolCreateInfo &info) |
Chia-I Wu | f1e2e99 | 2014-12-27 14:12:52 +0800 | [diff] [blame] | 503 | { |
Chia-I Wu | 1b7d476 | 2015-07-03 11:49:42 +0800 | [diff] [blame] | 504 | NON_DISPATCHABLE_HANDLE_INIT(vkCreateQueryPool, dev, &info); |
Chia-I Wu | f1e2e99 | 2014-12-27 14:12:52 +0800 | [diff] [blame] | 505 | } |
| 506 | |
Courtney Goeltzenleuchter | fb4efc6 | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 507 | VkResult QueryPool::results(uint32_t start, uint32_t count, size_t size, void *data) |
Chia-I Wu | f1e2e99 | 2014-12-27 14:12:52 +0800 | [diff] [blame] | 508 | { |
| 509 | size_t tmp = size; |
Chia-I Wu | 1b7d476 | 2015-07-03 11:49:42 +0800 | [diff] [blame] | 510 | VkResult err = vkGetQueryPoolResults(device(), handle(), start, count, &tmp, data, 0); |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 511 | if (err == VK_SUCCESS) { |
Chia-I Wu | f1e2e99 | 2014-12-27 14:12:52 +0800 | [diff] [blame] | 512 | if (!EXPECT(tmp == size)) |
| 513 | memset(data, 0, size); |
| 514 | } else { |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 515 | EXPECT(err == VK_NOT_READY); |
Chia-I Wu | f1e2e99 | 2014-12-27 14:12:52 +0800 | [diff] [blame] | 516 | } |
| 517 | |
| 518 | return err; |
| 519 | } |
| 520 | |
Tony Barbour | 67e9915 | 2015-07-10 14:10:27 -0600 | [diff] [blame] | 521 | NON_DISPATCHABLE_HANDLE_DTOR(Buffer, vkDestroyBuffer) |
Chia-I Wu | 1a28fe0 | 2015-01-01 07:55:04 +0800 | [diff] [blame] | 522 | |
Chia-I Wu | 681d7a0 | 2015-07-03 13:44:34 +0800 | [diff] [blame] | 523 | void Buffer::init(const Device &dev, const VkBufferCreateInfo &info, VkMemoryPropertyFlags mem_props) |
Tony Barbour | 4c97d7a | 2015-04-22 15:10:33 -0600 | [diff] [blame] | 524 | { |
| 525 | init_no_mem(dev, info); |
Chia-I Wu | 681d7a0 | 2015-07-03 13:44:34 +0800 | [diff] [blame] | 526 | |
| 527 | internal_mem_.init(dev, get_resource_alloc_info(dev, memory_requirements(), mem_props)); |
| 528 | bind_memory(internal_mem_, 0); |
Tony Barbour | 4c97d7a | 2015-04-22 15:10:33 -0600 | [diff] [blame] | 529 | } |
| 530 | |
Courtney Goeltzenleuchter | 95487bc | 2015-04-14 18:48:46 -0600 | [diff] [blame] | 531 | void Buffer::init_no_mem(const Device &dev, const VkBufferCreateInfo &info) |
Chia-I Wu | 1a28fe0 | 2015-01-01 07:55:04 +0800 | [diff] [blame] | 532 | { |
Chia-I Wu | 681d7a0 | 2015-07-03 13:44:34 +0800 | [diff] [blame] | 533 | NON_DISPATCHABLE_HANDLE_INIT(vkCreateBuffer, dev, &info); |
Chia-I Wu | 1a28fe0 | 2015-01-01 07:55:04 +0800 | [diff] [blame] | 534 | create_info_ = info; |
| 535 | } |
| 536 | |
Chia-I Wu | 681d7a0 | 2015-07-03 13:44:34 +0800 | [diff] [blame] | 537 | VkMemoryRequirements Buffer::memory_requirements() const |
Mark Lobodzinski | 942b172 | 2015-05-11 17:21:15 -0500 | [diff] [blame] | 538 | { |
Chia-I Wu | 681d7a0 | 2015-07-03 13:44:34 +0800 | [diff] [blame] | 539 | VkMemoryRequirements reqs; |
| 540 | |
Courtney Goeltzenleuchter | 06d8947 | 2015-10-20 16:40:38 -0600 | [diff] [blame] | 541 | vkGetBufferMemoryRequirements(device(), handle(), &reqs); |
Chia-I Wu | 681d7a0 | 2015-07-03 13:44:34 +0800 | [diff] [blame] | 542 | |
| 543 | return reqs; |
| 544 | } |
| 545 | |
| 546 | void Buffer::bind_memory(const DeviceMemory &mem, VkDeviceSize mem_offset) |
| 547 | { |
Tony Barbour | 67e9915 | 2015-07-10 14:10:27 -0600 | [diff] [blame] | 548 | EXPECT(vkBindBufferMemory(device(), handle(), mem.handle(), mem_offset) == VK_SUCCESS); |
Mark Lobodzinski | 942b172 | 2015-05-11 17:21:15 -0500 | [diff] [blame] | 549 | } |
| 550 | |
Tony Barbour | 67e9915 | 2015-07-10 14:10:27 -0600 | [diff] [blame] | 551 | NON_DISPATCHABLE_HANDLE_DTOR(BufferView, vkDestroyBufferView) |
Chia-I Wu | 3158bf3 | 2015-07-03 11:49:42 +0800 | [diff] [blame] | 552 | |
Courtney Goeltzenleuchter | 95487bc | 2015-04-14 18:48:46 -0600 | [diff] [blame] | 553 | void BufferView::init(const Device &dev, const VkBufferViewCreateInfo &info) |
Chia-I Wu | 1a28fe0 | 2015-01-01 07:55:04 +0800 | [diff] [blame] | 554 | { |
Chia-I Wu | 3158bf3 | 2015-07-03 11:49:42 +0800 | [diff] [blame] | 555 | NON_DISPATCHABLE_HANDLE_INIT(vkCreateBufferView, dev, &info); |
Chia-I Wu | 1a28fe0 | 2015-01-01 07:55:04 +0800 | [diff] [blame] | 556 | } |
| 557 | |
Tony Barbour | 67e9915 | 2015-07-10 14:10:27 -0600 | [diff] [blame] | 558 | NON_DISPATCHABLE_HANDLE_DTOR(Image, vkDestroyImage) |
Chia-I Wu | f1e2e99 | 2014-12-27 14:12:52 +0800 | [diff] [blame] | 559 | |
Chia-I Wu | 681d7a0 | 2015-07-03 13:44:34 +0800 | [diff] [blame] | 560 | void Image::init(const Device &dev, const VkImageCreateInfo &info, VkMemoryPropertyFlags mem_props) |
Tony Barbour | 4c97d7a | 2015-04-22 15:10:33 -0600 | [diff] [blame] | 561 | { |
| 562 | init_no_mem(dev, info); |
Chia-I Wu | 681d7a0 | 2015-07-03 13:44:34 +0800 | [diff] [blame] | 563 | |
| 564 | internal_mem_.init(dev, get_resource_alloc_info(dev, memory_requirements(), mem_props)); |
| 565 | bind_memory(internal_mem_, 0); |
Tony Barbour | 4c97d7a | 2015-04-22 15:10:33 -0600 | [diff] [blame] | 566 | } |
| 567 | |
Courtney Goeltzenleuchter | fb4efc6 | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 568 | void Image::init_no_mem(const Device &dev, const VkImageCreateInfo &info) |
Chia-I Wu | f1e2e99 | 2014-12-27 14:12:52 +0800 | [diff] [blame] | 569 | { |
Chia-I Wu | 681d7a0 | 2015-07-03 13:44:34 +0800 | [diff] [blame] | 570 | NON_DISPATCHABLE_HANDLE_INIT(vkCreateImage, dev, &info); |
Chia-I Wu | f1e2e99 | 2014-12-27 14:12:52 +0800 | [diff] [blame] | 571 | init_info(dev, info); |
| 572 | } |
| 573 | |
Courtney Goeltzenleuchter | fb4efc6 | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 574 | void Image::init_info(const Device &dev, const VkImageCreateInfo &info) |
Chia-I Wu | f1e2e99 | 2014-12-27 14:12:52 +0800 | [diff] [blame] | 575 | { |
| 576 | create_info_ = info; |
| 577 | |
| 578 | for (std::vector<Device::Format>::const_iterator it = dev.formats().begin(); it != dev.formats().end(); it++) { |
| 579 | if (memcmp(&it->format, &create_info_.format, sizeof(it->format)) == 0 && it->tiling == create_info_.tiling) { |
| 580 | format_features_ = it->features; |
| 581 | break; |
| 582 | } |
| 583 | } |
| 584 | } |
| 585 | |
Chia-I Wu | 681d7a0 | 2015-07-03 13:44:34 +0800 | [diff] [blame] | 586 | VkMemoryRequirements Image::memory_requirements() const |
Chia-I Wu | 1a28fe0 | 2015-01-01 07:55:04 +0800 | [diff] [blame] | 587 | { |
Chia-I Wu | 681d7a0 | 2015-07-03 13:44:34 +0800 | [diff] [blame] | 588 | VkMemoryRequirements reqs; |
| 589 | |
Courtney Goeltzenleuchter | 06d8947 | 2015-10-20 16:40:38 -0600 | [diff] [blame] | 590 | vkGetImageMemoryRequirements(device(), handle(), &reqs); |
Chia-I Wu | 681d7a0 | 2015-07-03 13:44:34 +0800 | [diff] [blame] | 591 | |
| 592 | return reqs; |
| 593 | } |
| 594 | |
| 595 | void Image::bind_memory(const DeviceMemory &mem, VkDeviceSize mem_offset) |
| 596 | { |
Tony Barbour | 67e9915 | 2015-07-10 14:10:27 -0600 | [diff] [blame] | 597 | EXPECT(vkBindImageMemory(device(), handle(), mem.handle(), mem_offset) == VK_SUCCESS); |
Chia-I Wu | 1a28fe0 | 2015-01-01 07:55:04 +0800 | [diff] [blame] | 598 | } |
| 599 | |
Courtney Goeltzenleuchter | fb4efc6 | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 600 | VkSubresourceLayout Image::subresource_layout(const VkImageSubresource &subres) const |
Chia-I Wu | f1e2e99 | 2014-12-27 14:12:52 +0800 | [diff] [blame] | 601 | { |
Courtney Goeltzenleuchter | fb4efc6 | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 602 | VkSubresourceLayout data; |
Chia-I Wu | f1e2e99 | 2014-12-27 14:12:52 +0800 | [diff] [blame] | 603 | size_t size = sizeof(data); |
Courtney Goeltzenleuchter | 06d8947 | 2015-10-20 16:40:38 -0600 | [diff] [blame] | 604 | vkGetImageSubresourceLayout(device(), handle(), &subres, &data); |
| 605 | if (size != sizeof(data)) |
Chia-I Wu | f1e2e99 | 2014-12-27 14:12:52 +0800 | [diff] [blame] | 606 | memset(&data, 0, sizeof(data)); |
| 607 | |
| 608 | return data; |
| 609 | } |
| 610 | |
Courtney Goeltzenleuchter | 01ee1ca | 2015-09-10 16:41:13 -0600 | [diff] [blame] | 611 | VkSubresourceLayout Image::subresource_layout(const VkImageSubresourceCopy &subrescopy) const |
| 612 | { |
| 613 | VkSubresourceLayout data; |
Courtney Goeltzenleuchter | 8367ce0 | 2015-10-16 09:46:00 -0600 | [diff] [blame] | 614 | VkImageSubresource subres = subresource(image_aspect(subrescopy.aspect), subrescopy.mipLevel, subrescopy.baseArrayLayer); |
Courtney Goeltzenleuchter | 01ee1ca | 2015-09-10 16:41:13 -0600 | [diff] [blame] | 615 | size_t size = sizeof(data); |
Courtney Goeltzenleuchter | 06d8947 | 2015-10-20 16:40:38 -0600 | [diff] [blame] | 616 | vkGetImageSubresourceLayout(device(), handle(), &subres, &data); |
| 617 | if (size != sizeof(data)) |
Courtney Goeltzenleuchter | 01ee1ca | 2015-09-10 16:41:13 -0600 | [diff] [blame] | 618 | memset(&data, 0, sizeof(data)); |
| 619 | |
| 620 | return data; |
| 621 | } |
| 622 | |
Chia-I Wu | f1e2e99 | 2014-12-27 14:12:52 +0800 | [diff] [blame] | 623 | bool Image::transparent() const |
| 624 | { |
Tony Barbour | d1c3572 | 2015-04-16 15:59:00 -0600 | [diff] [blame] | 625 | return (create_info_.tiling == VK_IMAGE_TILING_LINEAR && |
Chia-I Wu | f1e2e99 | 2014-12-27 14:12:52 +0800 | [diff] [blame] | 626 | create_info_.samples == 1 && |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 627 | !(create_info_.usage & (VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT | |
Courtney Goeltzenleuchter | 660f0ca | 2015-09-10 14:14:11 -0600 | [diff] [blame] | 628 | VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT))); |
Chia-I Wu | f1e2e99 | 2014-12-27 14:12:52 +0800 | [diff] [blame] | 629 | } |
| 630 | |
Tony Barbour | 67e9915 | 2015-07-10 14:10:27 -0600 | [diff] [blame] | 631 | NON_DISPATCHABLE_HANDLE_DTOR(ImageView, vkDestroyImageView) |
Chia-I Wu | 3158bf3 | 2015-07-03 11:49:42 +0800 | [diff] [blame] | 632 | |
Courtney Goeltzenleuchter | fb4efc6 | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 633 | void ImageView::init(const Device &dev, const VkImageViewCreateInfo &info) |
Chia-I Wu | f1e2e99 | 2014-12-27 14:12:52 +0800 | [diff] [blame] | 634 | { |
Chia-I Wu | 3158bf3 | 2015-07-03 11:49:42 +0800 | [diff] [blame] | 635 | NON_DISPATCHABLE_HANDLE_INIT(vkCreateImageView, dev, &info); |
Chia-I Wu | f1e2e99 | 2014-12-27 14:12:52 +0800 | [diff] [blame] | 636 | } |
| 637 | |
Tony Barbour | 67e9915 | 2015-07-10 14:10:27 -0600 | [diff] [blame] | 638 | NON_DISPATCHABLE_HANDLE_DTOR(ShaderModule, vkDestroyShaderModule) |
Chia-I Wu | 4d0c792 | 2015-07-03 11:49:42 +0800 | [diff] [blame] | 639 | |
Courtney Goeltzenleuchter | ee4027d | 2015-06-28 13:01:17 -0600 | [diff] [blame] | 640 | void ShaderModule::init(const Device &dev, const VkShaderModuleCreateInfo &info) |
| 641 | { |
Chia-I Wu | 4d0c792 | 2015-07-03 11:49:42 +0800 | [diff] [blame] | 642 | NON_DISPATCHABLE_HANDLE_INIT(vkCreateShaderModule, dev, &info); |
Courtney Goeltzenleuchter | ee4027d | 2015-06-28 13:01:17 -0600 | [diff] [blame] | 643 | } |
| 644 | |
| 645 | VkResult ShaderModule::init_try(const Device &dev, const VkShaderModuleCreateInfo &info) |
| 646 | { |
Chia-I Wu | 4d0c792 | 2015-07-03 11:49:42 +0800 | [diff] [blame] | 647 | VkShaderModule mod; |
| 648 | |
| 649 | VkResult err = vkCreateShaderModule(dev.handle(), &info, &mod); |
Courtney Goeltzenleuchter | ee4027d | 2015-06-28 13:01:17 -0600 | [diff] [blame] | 650 | if (err == VK_SUCCESS) |
Chia-I Wu | 4d0c792 | 2015-07-03 11:49:42 +0800 | [diff] [blame] | 651 | NonDispHandle::init(dev.handle(), mod); |
Courtney Goeltzenleuchter | ee4027d | 2015-06-28 13:01:17 -0600 | [diff] [blame] | 652 | |
| 653 | return err; |
| 654 | } |
| 655 | |
Tony Barbour | 67e9915 | 2015-07-10 14:10:27 -0600 | [diff] [blame] | 656 | NON_DISPATCHABLE_HANDLE_DTOR(Shader, vkDestroyShader) |
Chia-I Wu | 4d0c792 | 2015-07-03 11:49:42 +0800 | [diff] [blame] | 657 | |
Courtney Goeltzenleuchter | fb4efc6 | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 658 | void Shader::init(const Device &dev, const VkShaderCreateInfo &info) |
Chia-I Wu | f1e2e99 | 2014-12-27 14:12:52 +0800 | [diff] [blame] | 659 | { |
Chia-I Wu | 4d0c792 | 2015-07-03 11:49:42 +0800 | [diff] [blame] | 660 | NON_DISPATCHABLE_HANDLE_INIT(vkCreateShader, dev, &info); |
Chia-I Wu | f1e2e99 | 2014-12-27 14:12:52 +0800 | [diff] [blame] | 661 | } |
| 662 | |
Courtney Goeltzenleuchter | fb4efc6 | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 663 | VkResult Shader::init_try(const Device &dev, const VkShaderCreateInfo &info) |
Chia-I Wu | f1e2e99 | 2014-12-27 14:12:52 +0800 | [diff] [blame] | 664 | { |
Courtney Goeltzenleuchter | fb4efc6 | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 665 | VkShader sh; |
Chia-I Wu | 4d0c792 | 2015-07-03 11:49:42 +0800 | [diff] [blame] | 666 | |
Chia-I Wu | f368b60 | 2015-07-03 10:41:20 +0800 | [diff] [blame] | 667 | VkResult err = vkCreateShader(dev.handle(), &info, &sh); |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 668 | if (err == VK_SUCCESS) |
Chia-I Wu | 4d0c792 | 2015-07-03 11:49:42 +0800 | [diff] [blame] | 669 | NonDispHandle::init(dev.handle(), sh); |
Chia-I Wu | f1e2e99 | 2014-12-27 14:12:52 +0800 | [diff] [blame] | 670 | |
| 671 | return err; |
| 672 | } |
| 673 | |
Tony Barbour | 67e9915 | 2015-07-10 14:10:27 -0600 | [diff] [blame] | 674 | NON_DISPATCHABLE_HANDLE_DTOR(Pipeline, vkDestroyPipeline) |
Chia-I Wu | 2ff72fd | 2015-07-03 11:49:42 +0800 | [diff] [blame] | 675 | |
Courtney Goeltzenleuchter | fb4efc6 | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 676 | void Pipeline::init(const Device &dev, const VkGraphicsPipelineCreateInfo &info) |
Chia-I Wu | f1e2e99 | 2014-12-27 14:12:52 +0800 | [diff] [blame] | 677 | { |
Jon Ashburn | c669cc6 | 2015-07-09 15:02:25 -0600 | [diff] [blame] | 678 | VkPipelineCache cache; |
| 679 | VkPipelineCacheCreateInfo ci; |
| 680 | memset((void *) &ci, 0, sizeof(VkPipelineCacheCreateInfo)); |
| 681 | ci.sType = VK_STRUCTURE_TYPE_PIPELINE_CACHE_CREATE_INFO; |
Chia-I Wu | f368b60 | 2015-07-03 10:41:20 +0800 | [diff] [blame] | 682 | VkResult err = vkCreatePipelineCache(dev.handle(), &ci, &cache); |
Jon Ashburn | c669cc6 | 2015-07-09 15:02:25 -0600 | [diff] [blame] | 683 | if (err == VK_SUCCESS) { |
Chia-I Wu | 2ff72fd | 2015-07-03 11:49:42 +0800 | [diff] [blame] | 684 | NON_DISPATCHABLE_HANDLE_INIT(vkCreateGraphicsPipelines, dev, cache, 1, &info); |
Chia-I Wu | f368b60 | 2015-07-03 10:41:20 +0800 | [diff] [blame] | 685 | vkDestroyPipelineCache(dev.handle(), cache); |
Jon Ashburn | c669cc6 | 2015-07-09 15:02:25 -0600 | [diff] [blame] | 686 | } |
Chia-I Wu | f1e2e99 | 2014-12-27 14:12:52 +0800 | [diff] [blame] | 687 | } |
| 688 | |
Chris Forbes | 95292b1 | 2015-05-25 11:13:26 +1200 | [diff] [blame] | 689 | VkResult Pipeline::init_try(const Device &dev, const VkGraphicsPipelineCreateInfo &info) |
| 690 | { |
| 691 | VkPipeline pipe; |
Jon Ashburn | c669cc6 | 2015-07-09 15:02:25 -0600 | [diff] [blame] | 692 | VkPipelineCache cache; |
| 693 | VkPipelineCacheCreateInfo ci; |
Jon Ashburn | c669cc6 | 2015-07-09 15:02:25 -0600 | [diff] [blame] | 694 | memset((void *) &ci, 0, sizeof(VkPipelineCacheCreateInfo)); |
| 695 | ci.sType = VK_STRUCTURE_TYPE_PIPELINE_CACHE_CREATE_INFO; |
Chia-I Wu | f368b60 | 2015-07-03 10:41:20 +0800 | [diff] [blame] | 696 | VkResult err = vkCreatePipelineCache(dev.handle(), &ci, &cache); |
| 697 | EXPECT(err == VK_SUCCESS); |
Chris Forbes | 95292b1 | 2015-05-25 11:13:26 +1200 | [diff] [blame] | 698 | if (err == VK_SUCCESS) { |
Chia-I Wu | f368b60 | 2015-07-03 10:41:20 +0800 | [diff] [blame] | 699 | err = vkCreateGraphicsPipelines(dev.handle(), cache, 1, &info, &pipe); |
Jon Ashburn | c669cc6 | 2015-07-09 15:02:25 -0600 | [diff] [blame] | 700 | if (err == VK_SUCCESS) { |
Chia-I Wu | 2ff72fd | 2015-07-03 11:49:42 +0800 | [diff] [blame] | 701 | NonDispHandle::init(dev.handle(), pipe); |
Jon Ashburn | c669cc6 | 2015-07-09 15:02:25 -0600 | [diff] [blame] | 702 | } |
Mike Stroyan | d1c84a5 | 2015-08-18 14:40:24 -0600 | [diff] [blame] | 703 | vkDestroyPipelineCache(dev.handle(), cache); |
Chris Forbes | 95292b1 | 2015-05-25 11:13:26 +1200 | [diff] [blame] | 704 | } |
| 705 | |
| 706 | return err; |
| 707 | } |
| 708 | |
Courtney Goeltzenleuchter | fb4efc6 | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 709 | void Pipeline::init(const Device &dev, const VkComputePipelineCreateInfo &info) |
Chia-I Wu | f1e2e99 | 2014-12-27 14:12:52 +0800 | [diff] [blame] | 710 | { |
Jon Ashburn | c669cc6 | 2015-07-09 15:02:25 -0600 | [diff] [blame] | 711 | VkPipelineCache cache; |
| 712 | VkPipelineCacheCreateInfo ci; |
| 713 | memset((void *) &ci, 0, sizeof(VkPipelineCacheCreateInfo)); |
| 714 | ci.sType = VK_STRUCTURE_TYPE_PIPELINE_CACHE_CREATE_INFO; |
Chia-I Wu | f368b60 | 2015-07-03 10:41:20 +0800 | [diff] [blame] | 715 | VkResult err = vkCreatePipelineCache(dev.handle(), &ci, &cache); |
Jon Ashburn | c669cc6 | 2015-07-09 15:02:25 -0600 | [diff] [blame] | 716 | if (err == VK_SUCCESS) { |
Chia-I Wu | 2ff72fd | 2015-07-03 11:49:42 +0800 | [diff] [blame] | 717 | NON_DISPATCHABLE_HANDLE_INIT(vkCreateComputePipelines, dev, cache, 1, &info); |
Chia-I Wu | f368b60 | 2015-07-03 10:41:20 +0800 | [diff] [blame] | 718 | vkDestroyPipelineCache(dev.handle(), cache); |
Jon Ashburn | c669cc6 | 2015-07-09 15:02:25 -0600 | [diff] [blame] | 719 | } |
Chia-I Wu | f1e2e99 | 2014-12-27 14:12:52 +0800 | [diff] [blame] | 720 | } |
| 721 | |
Tony Barbour | 67e9915 | 2015-07-10 14:10:27 -0600 | [diff] [blame] | 722 | NON_DISPATCHABLE_HANDLE_DTOR(PipelineLayout, vkDestroyPipelineLayout) |
Chia-I Wu | fd46e7d | 2015-07-03 11:49:42 +0800 | [diff] [blame] | 723 | |
| 724 | void PipelineLayout::init(const Device &dev, VkPipelineLayoutCreateInfo &info, const std::vector<const DescriptorSetLayout *> &layouts) |
| 725 | { |
Chia-I Wu | afdfd7f | 2015-07-03 11:49:42 +0800 | [diff] [blame] | 726 | const std::vector<VkDescriptorSetLayout> layout_handles = make_handles<VkDescriptorSetLayout>(layouts); |
Tony Barbour | 482c609 | 2015-07-27 09:37:48 -0600 | [diff] [blame] | 727 | info.pSetLayouts = layout_handles.data(); |
Chia-I Wu | fd46e7d | 2015-07-03 11:49:42 +0800 | [diff] [blame] | 728 | |
| 729 | NON_DISPATCHABLE_HANDLE_INIT(vkCreatePipelineLayout, dev, &info); |
| 730 | } |
| 731 | |
Tony Barbour | 67e9915 | 2015-07-10 14:10:27 -0600 | [diff] [blame] | 732 | NON_DISPATCHABLE_HANDLE_DTOR(Sampler, vkDestroySampler) |
Chia-I Wu | 8c721c6 | 2015-07-03 11:49:42 +0800 | [diff] [blame] | 733 | |
Courtney Goeltzenleuchter | fb4efc6 | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 734 | void Sampler::init(const Device &dev, const VkSamplerCreateInfo &info) |
Chia-I Wu | f1e2e99 | 2014-12-27 14:12:52 +0800 | [diff] [blame] | 735 | { |
Chia-I Wu | 8c721c6 | 2015-07-03 11:49:42 +0800 | [diff] [blame] | 736 | NON_DISPATCHABLE_HANDLE_INIT(vkCreateSampler, dev, &info); |
Chia-I Wu | f1e2e99 | 2014-12-27 14:12:52 +0800 | [diff] [blame] | 737 | } |
| 738 | |
Tony Barbour | 67e9915 | 2015-07-10 14:10:27 -0600 | [diff] [blame] | 739 | NON_DISPATCHABLE_HANDLE_DTOR(DescriptorSetLayout, vkDestroyDescriptorSetLayout) |
Chia-I Wu | afdfd7f | 2015-07-03 11:49:42 +0800 | [diff] [blame] | 740 | |
Courtney Goeltzenleuchter | fb4efc6 | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 741 | void DescriptorSetLayout::init(const Device &dev, const VkDescriptorSetLayoutCreateInfo &info) |
Chia-I Wu | f1e2e99 | 2014-12-27 14:12:52 +0800 | [diff] [blame] | 742 | { |
Chia-I Wu | afdfd7f | 2015-07-03 11:49:42 +0800 | [diff] [blame] | 743 | NON_DISPATCHABLE_HANDLE_INIT(vkCreateDescriptorSetLayout, dev, &info); |
Chia-I Wu | f1e2e99 | 2014-12-27 14:12:52 +0800 | [diff] [blame] | 744 | } |
| 745 | |
Tony Barbour | 67e9915 | 2015-07-10 14:10:27 -0600 | [diff] [blame] | 746 | NON_DISPATCHABLE_HANDLE_DTOR(DescriptorPool, vkDestroyDescriptorPool) |
Chia-I Wu | afdfd7f | 2015-07-03 11:49:42 +0800 | [diff] [blame] | 747 | |
Courtney Goeltzenleuchter | fe908d3 | 2015-09-16 16:12:45 -0600 | [diff] [blame] | 748 | void DescriptorPool::init(const Device &dev, const VkDescriptorPoolCreateInfo &info) |
Chia-I Wu | f1e2e99 | 2014-12-27 14:12:52 +0800 | [diff] [blame] | 749 | { |
Courtney Goeltzenleuchter | bee18a9 | 2015-10-23 14:21:05 -0600 | [diff] [blame] | 750 | setDynamicUsage(info.flags & VK_DESCRIPTOR_POOL_CREATE_FREE_DESCRIPTOR_SET_BIT); |
Courtney Goeltzenleuchter | fe908d3 | 2015-09-16 16:12:45 -0600 | [diff] [blame] | 751 | NON_DISPATCHABLE_HANDLE_INIT(vkCreateDescriptorPool, dev, &info); |
Chia-I Wu | f1e2e99 | 2014-12-27 14:12:52 +0800 | [diff] [blame] | 752 | } |
| 753 | |
Chia-I Wu | fae40bc | 2015-03-26 15:23:52 +0800 | [diff] [blame] | 754 | void DescriptorPool::reset() |
Chia-I Wu | f1e2e99 | 2014-12-27 14:12:52 +0800 | [diff] [blame] | 755 | { |
Courtney Goeltzenleuchter | bee18a9 | 2015-10-23 14:21:05 -0600 | [diff] [blame] | 756 | EXPECT(vkResetDescriptorPool(device(), handle(), 0) == VK_SUCCESS); |
Chia-I Wu | f1e2e99 | 2014-12-27 14:12:52 +0800 | [diff] [blame] | 757 | } |
| 758 | |
Courtney Goeltzenleuchter | bee18a9 | 2015-10-23 14:21:05 -0600 | [diff] [blame] | 759 | std::vector<DescriptorSet *> DescriptorPool::alloc_sets(const Device &dev, const std::vector<const DescriptorSetLayout *> &layouts) |
Chia-I Wu | f1e2e99 | 2014-12-27 14:12:52 +0800 | [diff] [blame] | 760 | { |
Chia-I Wu | afdfd7f | 2015-07-03 11:49:42 +0800 | [diff] [blame] | 761 | const std::vector<VkDescriptorSetLayout> layout_handles = make_handles<VkDescriptorSetLayout>(layouts); |
Chia-I Wu | 11078b0 | 2015-01-04 16:27:24 +0800 | [diff] [blame] | 762 | |
Chia-I Wu | afdfd7f | 2015-07-03 11:49:42 +0800 | [diff] [blame] | 763 | std::vector<VkDescriptorSet> set_handles; |
| 764 | set_handles.resize(layout_handles.size()); |
Chia-I Wu | 11078b0 | 2015-01-04 16:27:24 +0800 | [diff] [blame] | 765 | |
Courtney Goeltzenleuchter | bee18a9 | 2015-10-23 14:21:05 -0600 | [diff] [blame] | 766 | VkDescriptorSetAllocInfo alloc_info = {}; |
| 767 | alloc_info.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOC_INFO; |
| 768 | alloc_info.count = layout_handles.size(); |
| 769 | alloc_info.descriptorPool = handle(); |
| 770 | alloc_info.pSetLayouts = layout_handles.data(); |
| 771 | VkResult err = vkAllocDescriptorSets(device(), &alloc_info, set_handles.data()); |
Cody Northrop | 1e4f802 | 2015-08-03 12:47:29 -0600 | [diff] [blame] | 772 | EXPECT(err == VK_SUCCESS); |
Chia-I Wu | 11078b0 | 2015-01-04 16:27:24 +0800 | [diff] [blame] | 773 | |
| 774 | std::vector<DescriptorSet *> sets; |
Chia-I Wu | afdfd7f | 2015-07-03 11:49:42 +0800 | [diff] [blame] | 775 | for (std::vector<VkDescriptorSet>::const_iterator it = set_handles.begin(); it != set_handles.end(); it++) { |
Chia-I Wu | 11078b0 | 2015-01-04 16:27:24 +0800 | [diff] [blame] | 776 | // do descriptor sets need memories bound? |
Cody Northrop | cdc72a4 | 2015-10-08 11:39:25 -0600 | [diff] [blame] | 777 | DescriptorSet *descriptorSet = new DescriptorSet(dev, this, *it); |
Mark Lobodzinski | 40f7f40 | 2015-04-16 11:44:05 -0500 | [diff] [blame] | 778 | sets.push_back(descriptorSet); |
Chia-I Wu | 11078b0 | 2015-01-04 16:27:24 +0800 | [diff] [blame] | 779 | } |
Chia-I Wu | 11078b0 | 2015-01-04 16:27:24 +0800 | [diff] [blame] | 780 | return sets; |
| 781 | } |
| 782 | |
Courtney Goeltzenleuchter | bee18a9 | 2015-10-23 14:21:05 -0600 | [diff] [blame] | 783 | std::vector<DescriptorSet *> DescriptorPool::alloc_sets(const Device &dev, const DescriptorSetLayout &layout, uint32_t count) |
Chia-I Wu | 11078b0 | 2015-01-04 16:27:24 +0800 | [diff] [blame] | 784 | { |
Courtney Goeltzenleuchter | bee18a9 | 2015-10-23 14:21:05 -0600 | [diff] [blame] | 785 | return alloc_sets(dev, std::vector<const DescriptorSetLayout *>(count, &layout)); |
Chia-I Wu | 11078b0 | 2015-01-04 16:27:24 +0800 | [diff] [blame] | 786 | } |
| 787 | |
Courtney Goeltzenleuchter | bee18a9 | 2015-10-23 14:21:05 -0600 | [diff] [blame] | 788 | DescriptorSet *DescriptorPool::alloc_sets(const Device &dev, const DescriptorSetLayout &layout) |
Chia-I Wu | 11078b0 | 2015-01-04 16:27:24 +0800 | [diff] [blame] | 789 | { |
Courtney Goeltzenleuchter | bee18a9 | 2015-10-23 14:21:05 -0600 | [diff] [blame] | 790 | std::vector<DescriptorSet *> set = alloc_sets(dev, layout, 1); |
Chia-I Wu | 11078b0 | 2015-01-04 16:27:24 +0800 | [diff] [blame] | 791 | return (set.empty()) ? NULL : set[0]; |
| 792 | } |
| 793 | |
Tony Barbour | 67e9915 | 2015-07-10 14:10:27 -0600 | [diff] [blame] | 794 | DescriptorSet::~DescriptorSet() |
| 795 | { |
| 796 | if (initialized()) { |
Cody Northrop | cdc72a4 | 2015-10-08 11:39:25 -0600 | [diff] [blame] | 797 | // Only call vkFree* on sets allocated from pool with usage *_DYNAMIC |
| 798 | if (containing_pool_->getDynamicUsage()) { |
| 799 | VkDescriptorSet sets[1] = { handle() }; |
| 800 | EXPECT(vkFreeDescriptorSets(device(), containing_pool_->GetObj(), 1, sets) == VK_SUCCESS); |
| 801 | } |
Tony Barbour | 67e9915 | 2015-07-10 14:10:27 -0600 | [diff] [blame] | 802 | } |
| 803 | } |
Chia-I Wu | afdfd7f | 2015-07-03 11:49:42 +0800 | [diff] [blame] | 804 | |
Courtney Goeltzenleuchter | ee5d80b | 2015-07-10 19:50:17 -0600 | [diff] [blame] | 805 | NON_DISPATCHABLE_HANDLE_DTOR(CmdPool, vkDestroyCommandPool) |
| 806 | |
| 807 | void CmdPool::init(const Device &dev, const VkCmdPoolCreateInfo &info) |
| 808 | { |
| 809 | NON_DISPATCHABLE_HANDLE_INIT(vkCreateCommandPool, dev, &info); |
| 810 | } |
| 811 | |
| 812 | |
Chia-I Wu | be2b917 | 2015-07-03 11:49:42 +0800 | [diff] [blame] | 813 | CmdBuffer::~CmdBuffer() |
| 814 | { |
Courtney Goeltzenleuchter | bee18a9 | 2015-10-23 14:21:05 -0600 | [diff] [blame] | 815 | if (initialized()) { |
| 816 | VkCmdBuffer cmds[] = { handle() }; |
| 817 | vkFreeCommandBuffers(dev_handle_, cmd_pool_, 1, cmds); |
| 818 | } |
Chia-I Wu | be2b917 | 2015-07-03 11:49:42 +0800 | [diff] [blame] | 819 | } |
| 820 | |
Courtney Goeltzenleuchter | bee18a9 | 2015-10-23 14:21:05 -0600 | [diff] [blame] | 821 | void CmdBuffer::init(const Device &dev, const VkCmdBufferAllocInfo &info) |
Chia-I Wu | f1e2e99 | 2014-12-27 14:12:52 +0800 | [diff] [blame] | 822 | { |
Chia-I Wu | be2b917 | 2015-07-03 11:49:42 +0800 | [diff] [blame] | 823 | VkCmdBuffer cmd; |
| 824 | |
Courtney Goeltzenleuchter | d8e68bb | 2015-07-13 12:53:32 -0600 | [diff] [blame] | 825 | // Make sure cmdPool is set |
| 826 | assert(info.cmdPool); |
| 827 | |
Courtney Goeltzenleuchter | bee18a9 | 2015-10-23 14:21:05 -0600 | [diff] [blame] | 828 | if (EXPECT(vkAllocCommandBuffers(dev.handle(), &info, &cmd) == VK_SUCCESS)) { |
Chia-I Wu | be2b917 | 2015-07-03 11:49:42 +0800 | [diff] [blame] | 829 | Handle::init(cmd); |
| 830 | dev_handle_ = dev.handle(); |
Courtney Goeltzenleuchter | bee18a9 | 2015-10-23 14:21:05 -0600 | [diff] [blame] | 831 | cmd_pool_ = info.cmdPool; |
Chia-I Wu | be2b917 | 2015-07-03 11:49:42 +0800 | [diff] [blame] | 832 | } |
Chia-I Wu | f1e2e99 | 2014-12-27 14:12:52 +0800 | [diff] [blame] | 833 | } |
| 834 | |
Courtney Goeltzenleuchter | fb4efc6 | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 835 | void CmdBuffer::begin(const VkCmdBufferBeginInfo *info) |
Chia-I Wu | f1e2e99 | 2014-12-27 14:12:52 +0800 | [diff] [blame] | 836 | { |
Chia-I Wu | be2b917 | 2015-07-03 11:49:42 +0800 | [diff] [blame] | 837 | EXPECT(vkBeginCommandBuffer(handle(), info) == VK_SUCCESS); |
Jeremy Hayes | d65ae08 | 2015-01-14 16:17:08 -0700 | [diff] [blame] | 838 | } |
| 839 | |
Chia-I Wu | f1e2e99 | 2014-12-27 14:12:52 +0800 | [diff] [blame] | 840 | void CmdBuffer::begin() |
| 841 | { |
Courtney Goeltzenleuchter | fb4efc6 | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 842 | VkCmdBufferBeginInfo info = {}; |
Courtney Goeltzenleuchter | 04bb5f8 | 2015-10-21 18:11:04 -0600 | [diff] [blame] | 843 | info.flags = VK_CMD_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT; |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 844 | info.sType = VK_STRUCTURE_TYPE_CMD_BUFFER_BEGIN_INFO; |
Cody Northrop | cc09b9e | 2015-08-11 11:35:58 -0600 | [diff] [blame] | 845 | info.renderPass = VK_NULL_HANDLE; |
| 846 | info.subpass = 0; |
| 847 | info.framebuffer = VK_NULL_HANDLE; |
Jeremy Hayes | d65ae08 | 2015-01-14 16:17:08 -0700 | [diff] [blame] | 848 | |
| 849 | begin(&info); |
Chia-I Wu | f1e2e99 | 2014-12-27 14:12:52 +0800 | [diff] [blame] | 850 | } |
| 851 | |
| 852 | void CmdBuffer::end() |
| 853 | { |
Chia-I Wu | be2b917 | 2015-07-03 11:49:42 +0800 | [diff] [blame] | 854 | EXPECT(vkEndCommandBuffer(handle()) == VK_SUCCESS); |
Chia-I Wu | f1e2e99 | 2014-12-27 14:12:52 +0800 | [diff] [blame] | 855 | } |
| 856 | |
Cody Northrop | e62183e | 2015-07-09 18:08:05 -0600 | [diff] [blame] | 857 | void CmdBuffer::reset(VkCmdBufferResetFlags flags) |
Chia-I Wu | f1e2e99 | 2014-12-27 14:12:52 +0800 | [diff] [blame] | 858 | { |
Cody Northrop | e62183e | 2015-07-09 18:08:05 -0600 | [diff] [blame] | 859 | EXPECT(vkResetCommandBuffer(handle(), flags) == VK_SUCCESS); |
Chia-I Wu | f1e2e99 | 2014-12-27 14:12:52 +0800 | [diff] [blame] | 860 | } |
| 861 | |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 862 | }; // namespace vk_testing |