Karl Schultz | 6addd81 | 2016-02-02 17:17:23 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2015-2016 The Khronos Group Inc. |
| 3 | * Copyright (c) 2015-2016 Valve Corporation |
| 4 | * Copyright (c) 2015-2016 LunarG, Inc. |
| 5 | * |
Jon Ashburn | 3ebf125 | 2016-04-19 11:30:31 -0600 | [diff] [blame] | 6 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | * you may not use this file except in compliance with the License. |
| 8 | * You may obtain a copy of the License at |
Karl Schultz | 6addd81 | 2016-02-02 17:17:23 -0700 | [diff] [blame] | 9 | * |
Jon Ashburn | 3ebf125 | 2016-04-19 11:30:31 -0600 | [diff] [blame] | 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
Karl Schultz | 6addd81 | 2016-02-02 17:17:23 -0700 | [diff] [blame] | 11 | * |
Jon Ashburn | 3ebf125 | 2016-04-19 11:30:31 -0600 | [diff] [blame] | 12 | * Unless required by applicable law or agreed to in writing, software |
| 13 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | * See the License for the specific language governing permissions and |
| 16 | * limitations under the License. |
Karl Schultz | 6addd81 | 2016-02-02 17:17:23 -0700 | [diff] [blame] | 17 | * |
| 18 | * Author: Courtney Goeltzenleuchter <courtney@LunarG.com> |
| 19 | * Author: Tony Barbour <tony@LunarG.com> |
| 20 | */ |
Chia-I Wu | f1e2e99 | 2014-12-27 14:12:52 +0800 | [diff] [blame] | 21 | |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 22 | #include "vktestbinding.h" |
Mark Lobodzinski | 722841d | 2016-09-07 16:34:56 -0600 | [diff] [blame] | 23 | #include <assert.h> |
| 24 | #include <iostream> |
| 25 | #include <stdarg.h> |
Mark Lobodzinski | 64318ba | 2017-01-26 13:34:13 -0700 | [diff] [blame^] | 26 | #include <string.h> // memset(), memcmp() |
Chia-I Wu | f1e2e99 | 2014-12-27 14:12:52 +0800 | [diff] [blame] | 27 | |
| 28 | namespace { |
Chia-I Wu | f1e2e99 | 2014-12-27 14:12:52 +0800 | [diff] [blame] | 29 | |
Mark Lobodzinski | 64318ba | 2017-01-26 13:34:13 -0700 | [diff] [blame^] | 30 | #define NON_DISPATCHABLE_HANDLE_INIT(create_func, dev, ...) \ |
| 31 | do { \ |
| 32 | handle_type handle; \ |
| 33 | if (EXPECT(create_func(dev.handle(), __VA_ARGS__, NULL, &handle) == VK_SUCCESS)) \ |
| 34 | NonDispHandle::init(dev.handle(), handle); \ |
Chia-I Wu | f8f074f | 2015-07-03 10:58:57 +0800 | [diff] [blame] | 35 | } while (0) |
| 36 | |
Mark Lobodzinski | 64318ba | 2017-01-26 13:34:13 -0700 | [diff] [blame^] | 37 | #define NON_DISPATCHABLE_HANDLE_DTOR(cls, destroy_func) \ |
| 38 | cls::~cls() { \ |
| 39 | if (initialized()) destroy_func(device(), handle(), NULL); \ |
Chia-I Wu | d9e8e82 | 2015-07-03 11:45:55 +0800 | [diff] [blame] | 40 | } |
| 41 | |
Chia-I Wu | f1e2e99 | 2014-12-27 14:12:52 +0800 | [diff] [blame] | 42 | #define STRINGIFY(x) #x |
Mark Lobodzinski | 722841d | 2016-09-07 16:34:56 -0600 | [diff] [blame] | 43 | #define EXPECT(expr) ((expr) ? true : expect_failure(STRINGIFY(expr), __FILE__, __LINE__, __FUNCTION__)) |
Chia-I Wu | f1e2e99 | 2014-12-27 14:12:52 +0800 | [diff] [blame] | 44 | |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 45 | vk_testing::ErrorCallback error_callback; |
Chia-I Wu | f1e2e99 | 2014-12-27 14:12:52 +0800 | [diff] [blame] | 46 | |
Mark Lobodzinski | 722841d | 2016-09-07 16:34:56 -0600 | [diff] [blame] | 47 | bool expect_failure(const char *expr, const char *file, unsigned int line, const char *function) { |
Chia-I Wu | f1e2e99 | 2014-12-27 14:12:52 +0800 | [diff] [blame] | 48 | if (error_callback) { |
| 49 | error_callback(expr, file, line, function); |
| 50 | } else { |
Mark Lobodzinski | 722841d | 2016-09-07 16:34:56 -0600 | [diff] [blame] | 51 | std::cerr << file << ":" << line << ": " << function << ": Expectation `" << expr << "' failed.\n"; |
Chia-I Wu | f1e2e99 | 2014-12-27 14:12:52 +0800 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | return false; |
| 55 | } |
| 56 | |
Mark Lobodzinski | 64318ba | 2017-01-26 13:34:13 -0700 | [diff] [blame^] | 57 | template <class T, class S> |
| 58 | std::vector<T> make_handles(const std::vector<S> &v) { |
Chia-I Wu | d9e8e82 | 2015-07-03 11:45:55 +0800 | [diff] [blame] | 59 | std::vector<T> handles; |
| 60 | handles.reserve(v.size()); |
Mark Lobodzinski | 64318ba | 2017-01-26 13:34:13 -0700 | [diff] [blame^] | 61 | for (typename std::vector<S>::const_iterator it = v.begin(); it != v.end(); it++) handles.push_back((*it)->handle()); |
Chia-I Wu | d9e8e82 | 2015-07-03 11:45:55 +0800 | [diff] [blame] | 62 | return handles; |
| 63 | } |
| 64 | |
Mark Lobodzinski | 722841d | 2016-09-07 16:34:56 -0600 | [diff] [blame] | 65 | VkMemoryAllocateInfo get_resource_alloc_info(const vk_testing::Device &dev, const VkMemoryRequirements &reqs, |
Karl Schultz | 6addd81 | 2016-02-02 17:17:23 -0700 | [diff] [blame] | 66 | VkMemoryPropertyFlags mem_props) { |
Mark Lobodzinski | 722841d | 2016-09-07 16:34:56 -0600 | [diff] [blame] | 67 | VkMemoryAllocateInfo info = vk_testing::DeviceMemory::alloc_info(reqs.size, 0); |
Chia-I Wu | 681d7a0 | 2015-07-03 13:44:34 +0800 | [diff] [blame] | 68 | dev.phy().set_memory_type(reqs.memoryTypeBits, &info, mem_props); |
Chia-I Wu | f1e2e99 | 2014-12-27 14:12:52 +0800 | [diff] [blame] | 69 | |
| 70 | return info; |
| 71 | } |
Chia-I Wu | 681d7a0 | 2015-07-03 13:44:34 +0800 | [diff] [blame] | 72 | |
Mark Lobodzinski | 64318ba | 2017-01-26 13:34:13 -0700 | [diff] [blame^] | 73 | } // namespace |
Chia-I Wu | f1e2e99 | 2014-12-27 14:12:52 +0800 | [diff] [blame] | 74 | |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 75 | namespace vk_testing { |
Chia-I Wu | f1e2e99 | 2014-12-27 14:12:52 +0800 | [diff] [blame] | 76 | |
Karl Schultz | 6addd81 | 2016-02-02 17:17:23 -0700 | [diff] [blame] | 77 | void set_error_callback(ErrorCallback callback) { error_callback = callback; } |
Chia-I Wu | f1e2e99 | 2014-12-27 14:12:52 +0800 | [diff] [blame] | 78 | |
Karl Schultz | 6addd81 | 2016-02-02 17:17:23 -0700 | [diff] [blame] | 79 | VkPhysicalDeviceProperties PhysicalDevice::properties() const { |
Tony Barbour | 59a4732 | 2015-06-24 16:06:58 -0600 | [diff] [blame] | 80 | VkPhysicalDeviceProperties info; |
| 81 | |
Courtney Goeltzenleuchter | 06d8947 | 2015-10-20 16:40:38 -0600 | [diff] [blame] | 82 | vkGetPhysicalDeviceProperties(handle(), &info); |
Tony Barbour | 59a4732 | 2015-06-24 16:06:58 -0600 | [diff] [blame] | 83 | |
| 84 | return info; |
Chia-I Wu | f1e2e99 | 2014-12-27 14:12:52 +0800 | [diff] [blame] | 85 | } |
| 86 | |
Karl Schultz | 6addd81 | 2016-02-02 17:17:23 -0700 | [diff] [blame] | 87 | std::vector<VkQueueFamilyProperties> PhysicalDevice::queue_properties() const { |
Cody Northrop | d080288 | 2015-08-03 17:04:53 -0600 | [diff] [blame] | 88 | std::vector<VkQueueFamilyProperties> info; |
Tony Barbour | 59a4732 | 2015-06-24 16:06:58 -0600 | [diff] [blame] | 89 | uint32_t count; |
| 90 | |
Cody Northrop | d080288 | 2015-08-03 17:04:53 -0600 | [diff] [blame] | 91 | // Call once with NULL data to receive count |
Courtney Goeltzenleuchter | 06d8947 | 2015-10-20 16:40:38 -0600 | [diff] [blame] | 92 | vkGetPhysicalDeviceQueueFamilyProperties(handle(), &count, NULL); |
| 93 | info.resize(count); |
| 94 | vkGetPhysicalDeviceQueueFamilyProperties(handle(), &count, info.data()); |
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 | |
Karl Schultz | 6addd81 | 2016-02-02 17:17:23 -0700 | [diff] [blame] | 99 | VkPhysicalDeviceMemoryProperties PhysicalDevice::memory_properties() const { |
Tony Barbour | 59a4732 | 2015-06-24 16:06:58 -0600 | [diff] [blame] | 100 | VkPhysicalDeviceMemoryProperties info; |
| 101 | |
Courtney Goeltzenleuchter | 06d8947 | 2015-10-20 16:40:38 -0600 | [diff] [blame] | 102 | vkGetPhysicalDeviceMemoryProperties(handle(), &info); |
Mark Lobodzinski | b3fbcd9 | 2015-07-02 16:49:40 -0600 | [diff] [blame] | 103 | |
Tony Barbour | 59a4732 | 2015-06-24 16:06:58 -0600 | [diff] [blame] | 104 | return info; |
Chia-I Wu | f1e2e99 | 2014-12-27 14:12:52 +0800 | [diff] [blame] | 105 | } |
| 106 | |
Chris Forbes | f9cfe18 | 2016-04-04 17:22:42 +1200 | [diff] [blame] | 107 | VkPhysicalDeviceFeatures PhysicalDevice::features() const { |
| 108 | VkPhysicalDeviceFeatures features; |
| 109 | vkGetPhysicalDeviceFeatures(handle(), &features); |
| 110 | return features; |
| 111 | } |
| 112 | |
Courtney Goeltzenleuchter | 110fdf9 | 2015-06-29 15:39:26 -0600 | [diff] [blame] | 113 | /* |
| 114 | * Return list of Global layers available |
| 115 | */ |
Karl Schultz | 6addd81 | 2016-02-02 17:17:23 -0700 | [diff] [blame] | 116 | std::vector<VkLayerProperties> GetGlobalLayers() { |
Courtney Goeltzenleuchter | 110fdf9 | 2015-06-29 15:39:26 -0600 | [diff] [blame] | 117 | VkResult err; |
Courtney Goeltzenleuchter | 110fdf9 | 2015-06-29 15:39:26 -0600 | [diff] [blame] | 118 | std::vector<VkLayerProperties> layers; |
Courtney Goeltzenleuchter | cd69eee | 2015-07-06 09:10:47 -0600 | [diff] [blame] | 119 | uint32_t layer_count; |
Courtney Goeltzenleuchter | 110fdf9 | 2015-06-29 15:39:26 -0600 | [diff] [blame] | 120 | |
Courtney Goeltzenleuchter | cd69eee | 2015-07-06 09:10:47 -0600 | [diff] [blame] | 121 | do { |
| 122 | layer_count = 0; |
Courtney Goeltzenleuchter | 35985f6 | 2015-09-14 17:22:16 -0600 | [diff] [blame] | 123 | err = vkEnumerateInstanceLayerProperties(&layer_count, NULL); |
Courtney Goeltzenleuchter | cd69eee | 2015-07-06 09:10:47 -0600 | [diff] [blame] | 124 | |
| 125 | if (err == VK_SUCCESS) { |
| 126 | layers.reserve(layer_count); |
Mark Lobodzinski | 722841d | 2016-09-07 16:34:56 -0600 | [diff] [blame] | 127 | err = vkEnumerateInstanceLayerProperties(&layer_count, layers.data()); |
Courtney Goeltzenleuchter | cd69eee | 2015-07-06 09:10:47 -0600 | [diff] [blame] | 128 | } |
| 129 | } while (err == VK_INCOMPLETE); |
| 130 | |
Courtney Goeltzenleuchter | 110fdf9 | 2015-06-29 15:39:26 -0600 | [diff] [blame] | 131 | assert(err == VK_SUCCESS); |
| 132 | |
| 133 | return layers; |
Chia-I Wu | f1e2e99 | 2014-12-27 14:12:52 +0800 | [diff] [blame] | 134 | } |
| 135 | |
Courtney Goeltzenleuchter | 110fdf9 | 2015-06-29 15:39:26 -0600 | [diff] [blame] | 136 | /* |
| 137 | * Return list of Global extensions provided by the ICD / Loader |
| 138 | */ |
Mark Lobodzinski | 722841d | 2016-09-07 16:34:56 -0600 | [diff] [blame] | 139 | std::vector<VkExtensionProperties> GetGlobalExtensions() { return GetGlobalExtensions(NULL); } |
Courtney Goeltzenleuchter | 110fdf9 | 2015-06-29 15:39:26 -0600 | [diff] [blame] | 140 | |
| 141 | /* |
| 142 | * Return list of Global extensions provided by the specified layer |
Karl Schultz | 6addd81 | 2016-02-02 17:17:23 -0700 | [diff] [blame] | 143 | * If pLayerName is NULL, will return extensions implemented by the loader / |
| 144 | * ICDs |
Courtney Goeltzenleuchter | 110fdf9 | 2015-06-29 15:39:26 -0600 | [diff] [blame] | 145 | */ |
Karl Schultz | 6addd81 | 2016-02-02 17:17:23 -0700 | [diff] [blame] | 146 | std::vector<VkExtensionProperties> GetGlobalExtensions(const char *pLayerName) { |
Courtney Goeltzenleuchter | cd69eee | 2015-07-06 09:10:47 -0600 | [diff] [blame] | 147 | std::vector<VkExtensionProperties> exts; |
| 148 | uint32_t ext_count; |
Courtney Goeltzenleuchter | 110fdf9 | 2015-06-29 15:39:26 -0600 | [diff] [blame] | 149 | VkResult err; |
| 150 | |
Courtney Goeltzenleuchter | cd69eee | 2015-07-06 09:10:47 -0600 | [diff] [blame] | 151 | do { |
| 152 | ext_count = 0; |
Mark Lobodzinski | 722841d | 2016-09-07 16:34:56 -0600 | [diff] [blame] | 153 | err = vkEnumerateInstanceExtensionProperties(pLayerName, &ext_count, NULL); |
Courtney Goeltzenleuchter | 110fdf9 | 2015-06-29 15:39:26 -0600 | [diff] [blame] | 154 | |
Courtney Goeltzenleuchter | cd69eee | 2015-07-06 09:10:47 -0600 | [diff] [blame] | 155 | if (err == VK_SUCCESS) { |
Courtney Goeltzenleuchter | 381f3a2 | 2015-07-06 15:45:58 -0600 | [diff] [blame] | 156 | exts.resize(ext_count); |
Mark Lobodzinski | 722841d | 2016-09-07 16:34:56 -0600 | [diff] [blame] | 157 | err = vkEnumerateInstanceExtensionProperties(pLayerName, &ext_count, exts.data()); |
Courtney Goeltzenleuchter | cd69eee | 2015-07-06 09:10:47 -0600 | [diff] [blame] | 158 | } |
| 159 | } while (err == VK_INCOMPLETE); |
| 160 | |
Courtney Goeltzenleuchter | 110fdf9 | 2015-06-29 15:39:26 -0600 | [diff] [blame] | 161 | assert(err == VK_SUCCESS); |
| 162 | |
| 163 | return exts; |
| 164 | } |
| 165 | |
| 166 | /* |
| 167 | * Return list of PhysicalDevice extensions provided by the ICD / Loader |
| 168 | */ |
Mark Lobodzinski | 722841d | 2016-09-07 16:34:56 -0600 | [diff] [blame] | 169 | std::vector<VkExtensionProperties> PhysicalDevice::extensions() const { return extensions(NULL); } |
Courtney Goeltzenleuchter | 110fdf9 | 2015-06-29 15:39:26 -0600 | [diff] [blame] | 170 | |
| 171 | /* |
| 172 | * Return list of PhysicalDevice extensions provided by the specified layer |
| 173 | * If pLayerName is NULL, will return extensions for ICD / loader. |
| 174 | */ |
Mark Lobodzinski | 722841d | 2016-09-07 16:34:56 -0600 | [diff] [blame] | 175 | std::vector<VkExtensionProperties> PhysicalDevice::extensions(const char *pLayerName) const { |
Courtney Goeltzenleuchter | cd69eee | 2015-07-06 09:10:47 -0600 | [diff] [blame] | 176 | std::vector<VkExtensionProperties> exts; |
Courtney Goeltzenleuchter | 110fdf9 | 2015-06-29 15:39:26 -0600 | [diff] [blame] | 177 | VkResult err; |
| 178 | |
Courtney Goeltzenleuchter | cd69eee | 2015-07-06 09:10:47 -0600 | [diff] [blame] | 179 | do { |
| 180 | uint32_t extCount = 0; |
Mark Lobodzinski | 722841d | 2016-09-07 16:34:56 -0600 | [diff] [blame] | 181 | err = vkEnumerateDeviceExtensionProperties(handle(), pLayerName, &extCount, NULL); |
Courtney Goeltzenleuchter | 110fdf9 | 2015-06-29 15:39:26 -0600 | [diff] [blame] | 182 | |
Courtney Goeltzenleuchter | cd69eee | 2015-07-06 09:10:47 -0600 | [diff] [blame] | 183 | if (err == VK_SUCCESS) { |
Ian Elliott | 1a3845b | 2015-07-06 14:33:04 -0600 | [diff] [blame] | 184 | exts.resize(extCount); |
Mark Lobodzinski | 722841d | 2016-09-07 16:34:56 -0600 | [diff] [blame] | 185 | err = vkEnumerateDeviceExtensionProperties(handle(), pLayerName, &extCount, exts.data()); |
Courtney Goeltzenleuchter | cd69eee | 2015-07-06 09:10:47 -0600 | [diff] [blame] | 186 | } |
| 187 | } while (err == VK_INCOMPLETE); |
| 188 | |
Courtney Goeltzenleuchter | 110fdf9 | 2015-06-29 15:39:26 -0600 | [diff] [blame] | 189 | assert(err == VK_SUCCESS); |
Chia-I Wu | f1e2e99 | 2014-12-27 14:12:52 +0800 | [diff] [blame] | 190 | |
| 191 | return exts; |
| 192 | } |
| 193 | |
Mark Lobodzinski | 722841d | 2016-09-07 16:34:56 -0600 | [diff] [blame] | 194 | bool PhysicalDevice::set_memory_type(const uint32_t type_bits, VkMemoryAllocateInfo *info, const VkFlags properties, |
Karl Schultz | 6addd81 | 2016-02-02 17:17:23 -0700 | [diff] [blame] | 195 | const VkFlags forbid) const { |
| 196 | uint32_t type_mask = type_bits; |
| 197 | // Search memtypes to find first index with those properties |
| 198 | for (uint32_t i = 0; i < memory_properties_.memoryTypeCount; i++) { |
| 199 | if ((type_mask & 1) == 1) { |
| 200 | // Type is available, does it match user properties? |
Mark Lobodzinski | 722841d | 2016-09-07 16:34:56 -0600 | [diff] [blame] | 201 | if ((memory_properties_.memoryTypes[i].propertyFlags & properties) == properties && |
| 202 | (memory_properties_.memoryTypes[i].propertyFlags & forbid) == 0) { |
Karl Schultz | 6addd81 | 2016-02-02 17:17:23 -0700 | [diff] [blame] | 203 | info->memoryTypeIndex = i; |
| 204 | return true; |
| 205 | } |
| 206 | } |
| 207 | type_mask >>= 1; |
| 208 | } |
| 209 | // No memory types matched, return failure |
| 210 | return false; |
Mark Lobodzinski | b3fbcd9 | 2015-07-02 16:49:40 -0600 | [diff] [blame] | 211 | } |
| 212 | |
Courtney Goeltzenleuchter | cd69eee | 2015-07-06 09:10:47 -0600 | [diff] [blame] | 213 | /* |
| 214 | * Return list of PhysicalDevice layers |
| 215 | */ |
Karl Schultz | 6addd81 | 2016-02-02 17:17:23 -0700 | [diff] [blame] | 216 | std::vector<VkLayerProperties> PhysicalDevice::layers() const { |
Courtney Goeltzenleuchter | cd69eee | 2015-07-06 09:10:47 -0600 | [diff] [blame] | 217 | std::vector<VkLayerProperties> layer_props; |
| 218 | VkResult err; |
| 219 | |
| 220 | do { |
| 221 | uint32_t layer_count = 0; |
Courtney Goeltzenleuchter | 35985f6 | 2015-09-14 17:22:16 -0600 | [diff] [blame] | 222 | err = vkEnumerateDeviceLayerProperties(handle(), &layer_count, NULL); |
Courtney Goeltzenleuchter | cd69eee | 2015-07-06 09:10:47 -0600 | [diff] [blame] | 223 | |
| 224 | if (err == VK_SUCCESS) { |
| 225 | layer_props.reserve(layer_count); |
Mark Lobodzinski | 722841d | 2016-09-07 16:34:56 -0600 | [diff] [blame] | 226 | err = vkEnumerateDeviceLayerProperties(handle(), &layer_count, layer_props.data()); |
Courtney Goeltzenleuchter | cd69eee | 2015-07-06 09:10:47 -0600 | [diff] [blame] | 227 | } |
| 228 | } while (err == VK_INCOMPLETE); |
| 229 | |
| 230 | assert(err == VK_SUCCESS); |
| 231 | |
| 232 | return layer_props; |
| 233 | } |
| 234 | |
Karl Schultz | 6addd81 | 2016-02-02 17:17:23 -0700 | [diff] [blame] | 235 | Device::~Device() { |
Mark Lobodzinski | 64318ba | 2017-01-26 13:34:13 -0700 | [diff] [blame^] | 236 | if (!initialized()) return; |
Chia-I Wu | f1e2e99 | 2014-12-27 14:12:52 +0800 | [diff] [blame] | 237 | |
| 238 | for (int i = 0; i < QUEUE_COUNT; i++) { |
Mark Lobodzinski | 64318ba | 2017-01-26 13:34:13 -0700 | [diff] [blame^] | 239 | for (std::vector<Queue *>::iterator it = queues_[i].begin(); it != queues_[i].end(); it++) delete *it; |
Chia-I Wu | f1e2e99 | 2014-12-27 14:12:52 +0800 | [diff] [blame] | 240 | queues_[i].clear(); |
| 241 | } |
| 242 | |
Chia-I Wu | f7458c5 | 2015-10-26 21:10:41 +0800 | [diff] [blame] | 243 | vkDestroyDevice(handle(), NULL); |
Chia-I Wu | f1e2e99 | 2014-12-27 14:12:52 +0800 | [diff] [blame] | 244 | } |
| 245 | |
Tony Barbour | 53f7e89 | 2016-08-09 13:44:00 -0600 | [diff] [blame] | 246 | void Device::init(std::vector<const char *> &extensions, VkPhysicalDeviceFeatures *features) { |
Chia-I Wu | f1e2e99 | 2014-12-27 14:12:52 +0800 | [diff] [blame] | 247 | // request all queues |
Mark Lobodzinski | 722841d | 2016-09-07 16:34:56 -0600 | [diff] [blame] | 248 | const std::vector<VkQueueFamilyProperties> queue_props = phy_.queue_properties(); |
Courtney Goeltzenleuchter | 95487bc | 2015-04-14 18:48:46 -0600 | [diff] [blame] | 249 | std::vector<VkDeviceQueueCreateInfo> queue_info; |
Chia-I Wu | f1e2e99 | 2014-12-27 14:12:52 +0800 | [diff] [blame] | 250 | queue_info.reserve(queue_props.size()); |
Mark Lobodzinski | 53d023a | 2016-02-01 09:06:25 -0700 | [diff] [blame] | 251 | |
| 252 | std::vector<std::vector<float>> queue_priorities; |
| 253 | |
Mark Young | 93ecb1d | 2016-01-13 13:47:16 -0700 | [diff] [blame] | 254 | for (uint32_t i = 0; i < (uint32_t)queue_props.size(); i++) { |
Courtney Goeltzenleuchter | 95487bc | 2015-04-14 18:48:46 -0600 | [diff] [blame] | 255 | VkDeviceQueueCreateInfo qi = {}; |
Karl Schultz | 6addd81 | 2016-02-02 17:17:23 -0700 | [diff] [blame] | 256 | qi.sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO; |
| 257 | qi.pNext = NULL; |
| 258 | qi.queueFamilyIndex = i; |
| 259 | qi.queueCount = queue_props[i].queueCount; |
Mark Lobodzinski | 53d023a | 2016-02-01 09:06:25 -0700 | [diff] [blame] | 260 | |
Dustin Graves | 3c0bd58 | 2016-04-06 10:16:05 -0600 | [diff] [blame] | 261 | queue_priorities.emplace_back(qi.queueCount, 0.0f); |
Mark Lobodzinski | 53d023a | 2016-02-01 09:06:25 -0700 | [diff] [blame] | 262 | |
| 263 | qi.pQueuePriorities = queue_priorities[i].data(); |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 264 | if (queue_props[i].queueFlags & VK_QUEUE_GRAPHICS_BIT) { |
Courtney Goeltzenleuchter | 18248e6 | 2015-03-05 18:09:39 -0700 | [diff] [blame] | 265 | graphics_queue_node_index_ = i; |
| 266 | } |
Chia-I Wu | f1e2e99 | 2014-12-27 14:12:52 +0800 | [diff] [blame] | 267 | queue_info.push_back(qi); |
| 268 | } |
| 269 | |
Courtney Goeltzenleuchter | 95487bc | 2015-04-14 18:48:46 -0600 | [diff] [blame] | 270 | VkDeviceCreateInfo dev_info = {}; |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 271 | dev_info.sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO; |
Courtney Goeltzenleuchter | f579fa6 | 2015-06-10 17:39:03 -0600 | [diff] [blame] | 272 | dev_info.pNext = NULL; |
Chia-I Wu | 0212448 | 2015-11-06 06:42:02 +0800 | [diff] [blame] | 273 | dev_info.queueCreateInfoCount = queue_info.size(); |
| 274 | dev_info.pQueueCreateInfos = queue_info.data(); |
Tony Barbour | 4c70d10 | 2016-08-08 16:06:56 -0600 | [diff] [blame] | 275 | dev_info.enabledLayerCount = 0; |
| 276 | dev_info.ppEnabledLayerNames = NULL; |
Jon Ashburn | f19916e | 2016-01-11 13:12:43 -0700 | [diff] [blame] | 277 | dev_info.enabledExtensionCount = extensions.size(); |
Tony Barbour | 482c609 | 2015-07-27 09:37:48 -0600 | [diff] [blame] | 278 | dev_info.ppEnabledExtensionNames = extensions.data(); |
Chia-I Wu | f1e2e99 | 2014-12-27 14:12:52 +0800 | [diff] [blame] | 279 | |
Tony Barbour | 53f7e89 | 2016-08-09 13:44:00 -0600 | [diff] [blame] | 280 | VkPhysicalDeviceFeatures all_features; |
| 281 | if (features) { |
| 282 | dev_info.pEnabledFeatures = features; |
| 283 | } else { |
| 284 | // request all supportable features enabled |
| 285 | all_features = phy().features(); |
| 286 | dev_info.pEnabledFeatures = &all_features; |
| 287 | } |
Chris Forbes | f9cfe18 | 2016-04-04 17:22:42 +1200 | [diff] [blame] | 288 | |
Chia-I Wu | f1e2e99 | 2014-12-27 14:12:52 +0800 | [diff] [blame] | 289 | init(dev_info); |
| 290 | } |
| 291 | |
Karl Schultz | 6addd81 | 2016-02-02 17:17:23 -0700 | [diff] [blame] | 292 | void Device::init(const VkDeviceCreateInfo &info) { |
Chia-I Wu | f368b60 | 2015-07-03 10:41:20 +0800 | [diff] [blame] | 293 | VkDevice dev; |
| 294 | |
Mark Lobodzinski | 64318ba | 2017-01-26 13:34:13 -0700 | [diff] [blame^] | 295 | if (EXPECT(vkCreateDevice(phy_.handle(), &info, NULL, &dev) == VK_SUCCESS)) Handle::init(dev); |
Chia-I Wu | f1e2e99 | 2014-12-27 14:12:52 +0800 | [diff] [blame] | 296 | |
| 297 | init_queues(); |
Chia-I Wu | f1e2e99 | 2014-12-27 14:12:52 +0800 | [diff] [blame] | 298 | init_formats(); |
| 299 | } |
| 300 | |
Karl Schultz | 6addd81 | 2016-02-02 17:17:23 -0700 | [diff] [blame] | 301 | void Device::init_queues() { |
Courtney Goeltzenleuchter | 18248e6 | 2015-03-05 18:09:39 -0700 | [diff] [blame] | 302 | uint32_t queue_node_count; |
Chia-I Wu | f1e2e99 | 2014-12-27 14:12:52 +0800 | [diff] [blame] | 303 | |
Cody Northrop | d080288 | 2015-08-03 17:04:53 -0600 | [diff] [blame] | 304 | // Call with NULL data to get count |
Mark Lobodzinski | 722841d | 2016-09-07 16:34:56 -0600 | [diff] [blame] | 305 | vkGetPhysicalDeviceQueueFamilyProperties(phy_.handle(), &queue_node_count, NULL); |
Courtney Goeltzenleuchter | 18248e6 | 2015-03-05 18:09:39 -0700 | [diff] [blame] | 306 | EXPECT(queue_node_count >= 1); |
| 307 | |
Mark Lobodzinski | 722841d | 2016-09-07 16:34:56 -0600 | [diff] [blame] | 308 | VkQueueFamilyProperties *queue_props = new VkQueueFamilyProperties[queue_node_count]; |
Courtney Goeltzenleuchter | 18248e6 | 2015-03-05 18:09:39 -0700 | [diff] [blame] | 309 | |
Mark Lobodzinski | 722841d | 2016-09-07 16:34:56 -0600 | [diff] [blame] | 310 | vkGetPhysicalDeviceQueueFamilyProperties(phy_.handle(), &queue_node_count, queue_props); |
Courtney Goeltzenleuchter | 18248e6 | 2015-03-05 18:09:39 -0700 | [diff] [blame] | 311 | |
Tony Barbour | 7ea6aa2 | 2015-05-22 09:44:58 -0600 | [diff] [blame] | 312 | for (uint32_t i = 0; i < queue_node_count; i++) { |
Courtney Goeltzenleuchter | fb4efc6 | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 313 | VkQueue queue; |
Courtney Goeltzenleuchter | 18248e6 | 2015-03-05 18:09:39 -0700 | [diff] [blame] | 314 | |
Tony Barbour | 7ea6aa2 | 2015-05-22 09:44:58 -0600 | [diff] [blame] | 315 | for (uint32_t j = 0; j < queue_props[i].queueCount; j++) { |
Karl Schultz | 6addd81 | 2016-02-02 17:17:23 -0700 | [diff] [blame] | 316 | // TODO: Need to add support for separate MEMMGR and work queues, |
| 317 | // including synchronization |
Courtney Goeltzenleuchter | 06d8947 | 2015-10-20 16:40:38 -0600 | [diff] [blame] | 318 | vkGetDeviceQueue(handle(), i, j, &queue); |
Courtney Goeltzenleuchter | 18248e6 | 2015-03-05 18:09:39 -0700 | [diff] [blame] | 319 | |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 320 | if (queue_props[i].queueFlags & VK_QUEUE_GRAPHICS_BIT) { |
Tony Barbour | fb21ea3 | 2015-07-23 10:35:30 -0600 | [diff] [blame] | 321 | queues_[GRAPHICS].push_back(new Queue(queue, i)); |
Courtney Goeltzenleuchter | 18248e6 | 2015-03-05 18:09:39 -0700 | [diff] [blame] | 322 | } |
| 323 | |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 324 | if (queue_props[i].queueFlags & VK_QUEUE_COMPUTE_BIT) { |
Tony Barbour | fb21ea3 | 2015-07-23 10:35:30 -0600 | [diff] [blame] | 325 | queues_[COMPUTE].push_back(new Queue(queue, i)); |
Courtney Goeltzenleuchter | 18248e6 | 2015-03-05 18:09:39 -0700 | [diff] [blame] | 326 | } |
| 327 | |
Chia-I Wu | 1b99bb2 | 2015-10-27 19:25:11 +0800 | [diff] [blame] | 328 | if (queue_props[i].queueFlags & VK_QUEUE_TRANSFER_BIT) { |
Tony Barbour | fb21ea3 | 2015-07-23 10:35:30 -0600 | [diff] [blame] | 329 | queues_[DMA].push_back(new Queue(queue, i)); |
Courtney Goeltzenleuchter | 18248e6 | 2015-03-05 18:09:39 -0700 | [diff] [blame] | 330 | } |
Chia-I Wu | f1e2e99 | 2014-12-27 14:12:52 +0800 | [diff] [blame] | 331 | } |
| 332 | } |
| 333 | |
Chris Forbes | 0203879 | 2015-06-04 10:49:27 +1200 | [diff] [blame] | 334 | delete[] queue_props; |
Tony Barbour | 3d69c9e | 2015-05-20 16:53:31 -0600 | [diff] [blame] | 335 | |
Chia-I Wu | f1e2e99 | 2014-12-27 14:12:52 +0800 | [diff] [blame] | 336 | EXPECT(!queues_[GRAPHICS].empty() || !queues_[COMPUTE].empty()); |
| 337 | } |
| 338 | |
Karl Schultz | 6addd81 | 2016-02-02 17:17:23 -0700 | [diff] [blame] | 339 | void Device::init_formats() { |
Tony Barbour | d1c3572 | 2015-04-16 15:59:00 -0600 | [diff] [blame] | 340 | 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] | 341 | const VkFormat fmt = static_cast<VkFormat>(f); |
| 342 | const VkFormatProperties props = format_properties(fmt); |
Chia-I Wu | f1e2e99 | 2014-12-27 14:12:52 +0800 | [diff] [blame] | 343 | |
Jeremy Hayes | a058eee | 2015-01-23 08:51:43 -0700 | [diff] [blame] | 344 | if (props.linearTilingFeatures) { |
Mark Lobodzinski | 722841d | 2016-09-07 16:34:56 -0600 | [diff] [blame] | 345 | const Format tmp = {fmt, VK_IMAGE_TILING_LINEAR, props.linearTilingFeatures}; |
Jeremy Hayes | a058eee | 2015-01-23 08:51:43 -0700 | [diff] [blame] | 346 | formats_.push_back(tmp); |
| 347 | } |
Chia-I Wu | f1e2e99 | 2014-12-27 14:12:52 +0800 | [diff] [blame] | 348 | |
Jeremy Hayes | a058eee | 2015-01-23 08:51:43 -0700 | [diff] [blame] | 349 | if (props.optimalTilingFeatures) { |
Mark Lobodzinski | 722841d | 2016-09-07 16:34:56 -0600 | [diff] [blame] | 350 | const Format tmp = {fmt, VK_IMAGE_TILING_OPTIMAL, props.optimalTilingFeatures}; |
Jeremy Hayes | a058eee | 2015-01-23 08:51:43 -0700 | [diff] [blame] | 351 | formats_.push_back(tmp); |
Chia-I Wu | f1e2e99 | 2014-12-27 14:12:52 +0800 | [diff] [blame] | 352 | } |
| 353 | } |
| 354 | |
| 355 | EXPECT(!formats_.empty()); |
| 356 | } |
| 357 | |
Karl Schultz | 6addd81 | 2016-02-02 17:17:23 -0700 | [diff] [blame] | 358 | VkFormatProperties Device::format_properties(VkFormat format) { |
Courtney Goeltzenleuchter | fb4efc6 | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 359 | VkFormatProperties data; |
Courtney Goeltzenleuchter | 06d8947 | 2015-10-20 16:40:38 -0600 | [diff] [blame] | 360 | vkGetPhysicalDeviceFormatProperties(phy().handle(), format, &data); |
Chia-I Wu | f1e2e99 | 2014-12-27 14:12:52 +0800 | [diff] [blame] | 361 | |
| 362 | return data; |
| 363 | } |
| 364 | |
Karl Schultz | 6addd81 | 2016-02-02 17:17:23 -0700 | [diff] [blame] | 365 | void Device::wait() { EXPECT(vkDeviceWaitIdle(handle()) == VK_SUCCESS); } |
Chia-I Wu | f1e2e99 | 2014-12-27 14:12:52 +0800 | [diff] [blame] | 366 | |
Mark Lobodzinski | 722841d | 2016-09-07 16:34:56 -0600 | [diff] [blame] | 367 | VkResult Device::wait(const std::vector<const Fence *> &fences, bool wait_all, uint64_t timeout) { |
Chia-I Wu | d9e8e82 | 2015-07-03 11:45:55 +0800 | [diff] [blame] | 368 | const std::vector<VkFence> fence_handles = make_handles<VkFence>(fences); |
Mark Lobodzinski | 722841d | 2016-09-07 16:34:56 -0600 | [diff] [blame] | 369 | 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] | 370 | EXPECT(err == VK_SUCCESS || err == VK_TIMEOUT); |
Chia-I Wu | f1e2e99 | 2014-12-27 14:12:52 +0800 | [diff] [blame] | 371 | |
| 372 | return err; |
| 373 | } |
| 374 | |
Mark Lobodzinski | 722841d | 2016-09-07 16:34:56 -0600 | [diff] [blame] | 375 | void Device::update_descriptor_sets(const std::vector<VkWriteDescriptorSet> &writes, |
| 376 | const std::vector<VkCopyDescriptorSet> &copies) { |
| 377 | vkUpdateDescriptorSets(handle(), writes.size(), writes.data(), copies.size(), copies.data()); |
Chia-I Wu | 9d00ed7 | 2015-05-25 16:27:55 +0800 | [diff] [blame] | 378 | } |
| 379 | |
Mark Lobodzinski | 722841d | 2016-09-07 16:34:56 -0600 | [diff] [blame] | 380 | void Queue::submit(const std::vector<const CommandBuffer *> &cmds, Fence &fence) { |
| 381 | const std::vector<VkCommandBuffer> cmd_handles = make_handles<VkCommandBuffer>(cmds); |
Courtney Goeltzenleuchter | 806c700 | 2015-10-27 11:22:14 -0600 | [diff] [blame] | 382 | VkSubmitInfo submit_info; |
Chia-I Wu | f9be13c | 2015-10-26 20:37:06 +0800 | [diff] [blame] | 383 | submit_info.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO; |
| 384 | submit_info.pNext = NULL; |
Chia-I Wu | d50a7d7 | 2015-10-26 20:48:51 +0800 | [diff] [blame] | 385 | submit_info.waitSemaphoreCount = 0; |
Courtney Goeltzenleuchter | 806c700 | 2015-10-27 11:22:14 -0600 | [diff] [blame] | 386 | submit_info.pWaitSemaphores = NULL; |
Jon Ashburn | 7f9716c | 2015-12-30 16:42:50 -0700 | [diff] [blame] | 387 | submit_info.pWaitDstStageMask = NULL; |
Chia-I Wu | d50a7d7 | 2015-10-26 20:48:51 +0800 | [diff] [blame] | 388 | submit_info.commandBufferCount = (uint32_t)cmd_handles.size(); |
Courtney Goeltzenleuchter | 806c700 | 2015-10-27 11:22:14 -0600 | [diff] [blame] | 389 | submit_info.pCommandBuffers = cmd_handles.data(); |
Chia-I Wu | d50a7d7 | 2015-10-26 20:48:51 +0800 | [diff] [blame] | 390 | submit_info.signalSemaphoreCount = 0; |
Courtney Goeltzenleuchter | 806c700 | 2015-10-27 11:22:14 -0600 | [diff] [blame] | 391 | submit_info.pSignalSemaphores = NULL; |
Courtney Goeltzenleuchter | 646b907 | 2015-10-20 18:04:07 -0600 | [diff] [blame] | 392 | |
Mark Lobodzinski | 722841d | 2016-09-07 16:34:56 -0600 | [diff] [blame] | 393 | EXPECT(vkQueueSubmit(handle(), 1, &submit_info, fence.handle()) == VK_SUCCESS); |
Chia-I Wu | f1e2e99 | 2014-12-27 14:12:52 +0800 | [diff] [blame] | 394 | } |
| 395 | |
Mark Lobodzinski | 722841d | 2016-09-07 16:34:56 -0600 | [diff] [blame] | 396 | void Queue::submit(const CommandBuffer &cmd, Fence &fence) { submit(std::vector<const CommandBuffer *>(1, &cmd), fence); } |
Chia-I Wu | f1e2e99 | 2014-12-27 14:12:52 +0800 | [diff] [blame] | 397 | |
Karl Schultz | 6addd81 | 2016-02-02 17:17:23 -0700 | [diff] [blame] | 398 | void Queue::submit(const CommandBuffer &cmd) { |
Chia-I Wu | f1e2e99 | 2014-12-27 14:12:52 +0800 | [diff] [blame] | 399 | Fence fence; |
Courtney Goeltzenleuchter | 97b7523 | 2015-04-07 17:13:38 -0600 | [diff] [blame] | 400 | submit(cmd, fence); |
Chia-I Wu | f1e2e99 | 2014-12-27 14:12:52 +0800 | [diff] [blame] | 401 | } |
| 402 | |
Karl Schultz | 6addd81 | 2016-02-02 17:17:23 -0700 | [diff] [blame] | 403 | void Queue::wait() { EXPECT(vkQueueWaitIdle(handle()) == VK_SUCCESS); } |
Chia-I Wu | f1e2e99 | 2014-12-27 14:12:52 +0800 | [diff] [blame] | 404 | |
Karl Schultz | 6addd81 | 2016-02-02 17:17:23 -0700 | [diff] [blame] | 405 | DeviceMemory::~DeviceMemory() { |
Mark Lobodzinski | 64318ba | 2017-01-26 13:34:13 -0700 | [diff] [blame^] | 406 | if (initialized()) vkFreeMemory(device(), handle(), NULL); |
Chia-I Wu | f1e2e99 | 2014-12-27 14:12:52 +0800 | [diff] [blame] | 407 | } |
| 408 | |
Karl Schultz | 6addd81 | 2016-02-02 17:17:23 -0700 | [diff] [blame] | 409 | void DeviceMemory::init(const Device &dev, const VkMemoryAllocateInfo &info) { |
Chia-I Wu | 3432a0c | 2015-10-27 18:04:07 +0800 | [diff] [blame] | 410 | NON_DISPATCHABLE_HANDLE_INIT(vkAllocateMemory, dev, &info); |
Chia-I Wu | f1e2e99 | 2014-12-27 14:12:52 +0800 | [diff] [blame] | 411 | } |
| 412 | |
Karl Schultz | 6addd81 | 2016-02-02 17:17:23 -0700 | [diff] [blame] | 413 | const void *DeviceMemory::map(VkFlags flags) const { |
Chia-I Wu | f1e2e99 | 2014-12-27 14:12:52 +0800 | [diff] [blame] | 414 | void *data; |
Mark Lobodzinski | 64318ba | 2017-01-26 13:34:13 -0700 | [diff] [blame^] | 415 | if (!EXPECT(vkMapMemory(device(), handle(), 0, VK_WHOLE_SIZE, flags, &data) == VK_SUCCESS)) data = NULL; |
Chia-I Wu | f1e2e99 | 2014-12-27 14:12:52 +0800 | [diff] [blame] | 416 | |
| 417 | return data; |
| 418 | } |
| 419 | |
Karl Schultz | 6addd81 | 2016-02-02 17:17:23 -0700 | [diff] [blame] | 420 | void *DeviceMemory::map(VkFlags flags) { |
Chia-I Wu | f1e2e99 | 2014-12-27 14:12:52 +0800 | [diff] [blame] | 421 | void *data; |
Mark Lobodzinski | 64318ba | 2017-01-26 13:34:13 -0700 | [diff] [blame^] | 422 | if (!EXPECT(vkMapMemory(device(), handle(), 0, VK_WHOLE_SIZE, flags, &data) == VK_SUCCESS)) data = NULL; |
Chia-I Wu | f1e2e99 | 2014-12-27 14:12:52 +0800 | [diff] [blame] | 423 | |
| 424 | return data; |
| 425 | } |
| 426 | |
Karl Schultz | 6addd81 | 2016-02-02 17:17:23 -0700 | [diff] [blame] | 427 | void DeviceMemory::unmap() const { vkUnmapMemory(device(), handle()); } |
Chia-I Wu | f1e2e99 | 2014-12-27 14:12:52 +0800 | [diff] [blame] | 428 | |
Tony Barbour | 67e9915 | 2015-07-10 14:10:27 -0600 | [diff] [blame] | 429 | NON_DISPATCHABLE_HANDLE_DTOR(Fence, vkDestroyFence) |
Chia-I Wu | d9e8e82 | 2015-07-03 11:45:55 +0800 | [diff] [blame] | 430 | |
Mark Lobodzinski | 722841d | 2016-09-07 16:34:56 -0600 | [diff] [blame] | 431 | void Fence::init(const Device &dev, const VkFenceCreateInfo &info) { NON_DISPATCHABLE_HANDLE_INIT(vkCreateFence, dev, &info); } |
Chia-I Wu | f1e2e99 | 2014-12-27 14:12:52 +0800 | [diff] [blame] | 432 | |
Tony Barbour | 67e9915 | 2015-07-10 14:10:27 -0600 | [diff] [blame] | 433 | NON_DISPATCHABLE_HANDLE_DTOR(Semaphore, vkDestroySemaphore) |
Chia-I Wu | 6b1c248 | 2015-07-03 11:49:42 +0800 | [diff] [blame] | 434 | |
Karl Schultz | 6addd81 | 2016-02-02 17:17:23 -0700 | [diff] [blame] | 435 | void Semaphore::init(const Device &dev, const VkSemaphoreCreateInfo &info) { |
Chia-I Wu | 6b1c248 | 2015-07-03 11:49:42 +0800 | [diff] [blame] | 436 | NON_DISPATCHABLE_HANDLE_INIT(vkCreateSemaphore, dev, &info); |
Chia-I Wu | f1e2e99 | 2014-12-27 14:12:52 +0800 | [diff] [blame] | 437 | } |
| 438 | |
Tony Barbour | 67e9915 | 2015-07-10 14:10:27 -0600 | [diff] [blame] | 439 | NON_DISPATCHABLE_HANDLE_DTOR(Event, vkDestroyEvent) |
Chia-I Wu | c5c9799 | 2015-07-03 11:49:42 +0800 | [diff] [blame] | 440 | |
Mark Lobodzinski | 722841d | 2016-09-07 16:34:56 -0600 | [diff] [blame] | 441 | void Event::init(const Device &dev, const VkEventCreateInfo &info) { NON_DISPATCHABLE_HANDLE_INIT(vkCreateEvent, dev, &info); } |
Chia-I Wu | f1e2e99 | 2014-12-27 14:12:52 +0800 | [diff] [blame] | 442 | |
Karl Schultz | 6addd81 | 2016-02-02 17:17:23 -0700 | [diff] [blame] | 443 | void Event::set() { EXPECT(vkSetEvent(device(), handle()) == VK_SUCCESS); } |
Chia-I Wu | f1e2e99 | 2014-12-27 14:12:52 +0800 | [diff] [blame] | 444 | |
Karl Schultz | 6addd81 | 2016-02-02 17:17:23 -0700 | [diff] [blame] | 445 | void Event::reset() { EXPECT(vkResetEvent(device(), handle()) == VK_SUCCESS); } |
Chia-I Wu | f1e2e99 | 2014-12-27 14:12:52 +0800 | [diff] [blame] | 446 | |
Tony Barbour | 67e9915 | 2015-07-10 14:10:27 -0600 | [diff] [blame] | 447 | NON_DISPATCHABLE_HANDLE_DTOR(QueryPool, vkDestroyQueryPool) |
Chia-I Wu | 1b7d476 | 2015-07-03 11:49:42 +0800 | [diff] [blame] | 448 | |
Karl Schultz | 6addd81 | 2016-02-02 17:17:23 -0700 | [diff] [blame] | 449 | void QueryPool::init(const Device &dev, const VkQueryPoolCreateInfo &info) { |
Chia-I Wu | 1b7d476 | 2015-07-03 11:49:42 +0800 | [diff] [blame] | 450 | NON_DISPATCHABLE_HANDLE_INIT(vkCreateQueryPool, dev, &info); |
Chia-I Wu | f1e2e99 | 2014-12-27 14:12:52 +0800 | [diff] [blame] | 451 | } |
| 452 | |
Mark Lobodzinski | 722841d | 2016-09-07 16:34:56 -0600 | [diff] [blame] | 453 | VkResult QueryPool::results(uint32_t first, uint32_t count, size_t size, void *data, size_t stride) { |
| 454 | VkResult err = vkGetQueryPoolResults(device(), handle(), first, count, size, data, stride, 0); |
Chia-I Wu | ccc93a7 | 2015-10-26 18:36:20 +0800 | [diff] [blame] | 455 | EXPECT(err == VK_SUCCESS || err == VK_NOT_READY); |
Chia-I Wu | f1e2e99 | 2014-12-27 14:12:52 +0800 | [diff] [blame] | 456 | |
| 457 | return err; |
| 458 | } |
| 459 | |
Tony Barbour | 67e9915 | 2015-07-10 14:10:27 -0600 | [diff] [blame] | 460 | NON_DISPATCHABLE_HANDLE_DTOR(Buffer, vkDestroyBuffer) |
Chia-I Wu | 1a28fe0 | 2015-01-01 07:55:04 +0800 | [diff] [blame] | 461 | |
Mark Lobodzinski | 722841d | 2016-09-07 16:34:56 -0600 | [diff] [blame] | 462 | void Buffer::init(const Device &dev, const VkBufferCreateInfo &info, VkMemoryPropertyFlags mem_props) { |
Tony Barbour | 4c97d7a | 2015-04-22 15:10:33 -0600 | [diff] [blame] | 463 | init_no_mem(dev, info); |
Chia-I Wu | 681d7a0 | 2015-07-03 13:44:34 +0800 | [diff] [blame] | 464 | |
Mark Lobodzinski | 722841d | 2016-09-07 16:34:56 -0600 | [diff] [blame] | 465 | internal_mem_.init(dev, get_resource_alloc_info(dev, memory_requirements(), mem_props)); |
Chia-I Wu | 681d7a0 | 2015-07-03 13:44:34 +0800 | [diff] [blame] | 466 | bind_memory(internal_mem_, 0); |
Tony Barbour | 4c97d7a | 2015-04-22 15:10:33 -0600 | [diff] [blame] | 467 | } |
| 468 | |
Karl Schultz | 6addd81 | 2016-02-02 17:17:23 -0700 | [diff] [blame] | 469 | void Buffer::init_no_mem(const Device &dev, const VkBufferCreateInfo &info) { |
Chia-I Wu | 681d7a0 | 2015-07-03 13:44:34 +0800 | [diff] [blame] | 470 | NON_DISPATCHABLE_HANDLE_INIT(vkCreateBuffer, dev, &info); |
Chia-I Wu | 1a28fe0 | 2015-01-01 07:55:04 +0800 | [diff] [blame] | 471 | create_info_ = info; |
| 472 | } |
| 473 | |
Karl Schultz | 6addd81 | 2016-02-02 17:17:23 -0700 | [diff] [blame] | 474 | VkMemoryRequirements Buffer::memory_requirements() const { |
Chia-I Wu | 681d7a0 | 2015-07-03 13:44:34 +0800 | [diff] [blame] | 475 | VkMemoryRequirements reqs; |
| 476 | |
Courtney Goeltzenleuchter | 06d8947 | 2015-10-20 16:40:38 -0600 | [diff] [blame] | 477 | vkGetBufferMemoryRequirements(device(), handle(), &reqs); |
Chia-I Wu | 681d7a0 | 2015-07-03 13:44:34 +0800 | [diff] [blame] | 478 | |
| 479 | return reqs; |
| 480 | } |
| 481 | |
Karl Schultz | 6addd81 | 2016-02-02 17:17:23 -0700 | [diff] [blame] | 482 | void Buffer::bind_memory(const DeviceMemory &mem, VkDeviceSize mem_offset) { |
Mark Lobodzinski | 722841d | 2016-09-07 16:34:56 -0600 | [diff] [blame] | 483 | EXPECT(vkBindBufferMemory(device(), handle(), mem.handle(), mem_offset) == VK_SUCCESS); |
Mark Lobodzinski | 942b172 | 2015-05-11 17:21:15 -0500 | [diff] [blame] | 484 | } |
| 485 | |
Tony Barbour | 67e9915 | 2015-07-10 14:10:27 -0600 | [diff] [blame] | 486 | NON_DISPATCHABLE_HANDLE_DTOR(BufferView, vkDestroyBufferView) |
Chia-I Wu | 3158bf3 | 2015-07-03 11:49:42 +0800 | [diff] [blame] | 487 | |
Karl Schultz | 6addd81 | 2016-02-02 17:17:23 -0700 | [diff] [blame] | 488 | void BufferView::init(const Device &dev, const VkBufferViewCreateInfo &info) { |
Chia-I Wu | 3158bf3 | 2015-07-03 11:49:42 +0800 | [diff] [blame] | 489 | NON_DISPATCHABLE_HANDLE_INIT(vkCreateBufferView, dev, &info); |
Chia-I Wu | 1a28fe0 | 2015-01-01 07:55:04 +0800 | [diff] [blame] | 490 | } |
| 491 | |
Tony Barbour | 67e9915 | 2015-07-10 14:10:27 -0600 | [diff] [blame] | 492 | NON_DISPATCHABLE_HANDLE_DTOR(Image, vkDestroyImage) |
Chia-I Wu | f1e2e99 | 2014-12-27 14:12:52 +0800 | [diff] [blame] | 493 | |
Mark Lobodzinski | 722841d | 2016-09-07 16:34:56 -0600 | [diff] [blame] | 494 | void Image::init(const Device &dev, const VkImageCreateInfo &info, VkMemoryPropertyFlags mem_props) { |
Tony Barbour | 4c97d7a | 2015-04-22 15:10:33 -0600 | [diff] [blame] | 495 | init_no_mem(dev, info); |
Chia-I Wu | 681d7a0 | 2015-07-03 13:44:34 +0800 | [diff] [blame] | 496 | |
Karl Schultz | b5bc11e | 2016-05-04 08:36:08 -0600 | [diff] [blame] | 497 | if (initialized()) { |
Mark Lobodzinski | 722841d | 2016-09-07 16:34:56 -0600 | [diff] [blame] | 498 | internal_mem_.init(dev, get_resource_alloc_info(dev, memory_requirements(), mem_props)); |
Karl Schultz | b5bc11e | 2016-05-04 08:36:08 -0600 | [diff] [blame] | 499 | bind_memory(internal_mem_, 0); |
| 500 | } |
Tony Barbour | 4c97d7a | 2015-04-22 15:10:33 -0600 | [diff] [blame] | 501 | } |
| 502 | |
Karl Schultz | 6addd81 | 2016-02-02 17:17:23 -0700 | [diff] [blame] | 503 | void Image::init_no_mem(const Device &dev, const VkImageCreateInfo &info) { |
Chia-I Wu | 681d7a0 | 2015-07-03 13:44:34 +0800 | [diff] [blame] | 504 | NON_DISPATCHABLE_HANDLE_INIT(vkCreateImage, dev, &info); |
Karl Schultz | b5bc11e | 2016-05-04 08:36:08 -0600 | [diff] [blame] | 505 | if (initialized()) { |
| 506 | init_info(dev, info); |
| 507 | } |
Chia-I Wu | f1e2e99 | 2014-12-27 14:12:52 +0800 | [diff] [blame] | 508 | } |
| 509 | |
Karl Schultz | 6addd81 | 2016-02-02 17:17:23 -0700 | [diff] [blame] | 510 | void Image::init_info(const Device &dev, const VkImageCreateInfo &info) { |
Chia-I Wu | f1e2e99 | 2014-12-27 14:12:52 +0800 | [diff] [blame] | 511 | create_info_ = info; |
| 512 | |
Mark Lobodzinski | 722841d | 2016-09-07 16:34:56 -0600 | [diff] [blame] | 513 | for (std::vector<Device::Format>::const_iterator it = dev.formats().begin(); it != dev.formats().end(); it++) { |
| 514 | if (memcmp(&it->format, &create_info_.format, sizeof(it->format)) == 0 && it->tiling == create_info_.tiling) { |
Chia-I Wu | f1e2e99 | 2014-12-27 14:12:52 +0800 | [diff] [blame] | 515 | format_features_ = it->features; |
| 516 | break; |
| 517 | } |
| 518 | } |
| 519 | } |
| 520 | |
Karl Schultz | 6addd81 | 2016-02-02 17:17:23 -0700 | [diff] [blame] | 521 | VkMemoryRequirements Image::memory_requirements() const { |
Chia-I Wu | 681d7a0 | 2015-07-03 13:44:34 +0800 | [diff] [blame] | 522 | VkMemoryRequirements reqs; |
| 523 | |
Courtney Goeltzenleuchter | 06d8947 | 2015-10-20 16:40:38 -0600 | [diff] [blame] | 524 | vkGetImageMemoryRequirements(device(), handle(), &reqs); |
Chia-I Wu | 681d7a0 | 2015-07-03 13:44:34 +0800 | [diff] [blame] | 525 | |
| 526 | return reqs; |
| 527 | } |
| 528 | |
Karl Schultz | 6addd81 | 2016-02-02 17:17:23 -0700 | [diff] [blame] | 529 | void Image::bind_memory(const DeviceMemory &mem, VkDeviceSize mem_offset) { |
Mark Lobodzinski | 722841d | 2016-09-07 16:34:56 -0600 | [diff] [blame] | 530 | EXPECT(vkBindImageMemory(device(), handle(), mem.handle(), mem_offset) == VK_SUCCESS); |
Chia-I Wu | 1a28fe0 | 2015-01-01 07:55:04 +0800 | [diff] [blame] | 531 | } |
| 532 | |
Mark Lobodzinski | 722841d | 2016-09-07 16:34:56 -0600 | [diff] [blame] | 533 | VkSubresourceLayout Image::subresource_layout(const VkImageSubresource &subres) const { |
Courtney Goeltzenleuchter | fb4efc6 | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 534 | VkSubresourceLayout data; |
Chia-I Wu | f1e2e99 | 2014-12-27 14:12:52 +0800 | [diff] [blame] | 535 | size_t size = sizeof(data); |
Courtney Goeltzenleuchter | 06d8947 | 2015-10-20 16:40:38 -0600 | [diff] [blame] | 536 | vkGetImageSubresourceLayout(device(), handle(), &subres, &data); |
Mark Lobodzinski | 64318ba | 2017-01-26 13:34:13 -0700 | [diff] [blame^] | 537 | if (size != sizeof(data)) memset(&data, 0, sizeof(data)); |
Chia-I Wu | f1e2e99 | 2014-12-27 14:12:52 +0800 | [diff] [blame] | 538 | |
| 539 | return data; |
| 540 | } |
| 541 | |
Mark Lobodzinski | 722841d | 2016-09-07 16:34:56 -0600 | [diff] [blame] | 542 | VkSubresourceLayout Image::subresource_layout(const VkImageSubresourceLayers &subrescopy) const { |
Courtney Goeltzenleuchter | 01ee1ca | 2015-09-10 16:41:13 -0600 | [diff] [blame] | 543 | VkSubresourceLayout data; |
Mark Lobodzinski | 722841d | 2016-09-07 16:34:56 -0600 | [diff] [blame] | 544 | VkImageSubresource subres = subresource(subrescopy.aspectMask, subrescopy.mipLevel, subrescopy.baseArrayLayer); |
Courtney Goeltzenleuchter | 01ee1ca | 2015-09-10 16:41:13 -0600 | [diff] [blame] | 545 | size_t size = sizeof(data); |
Courtney Goeltzenleuchter | 06d8947 | 2015-10-20 16:40:38 -0600 | [diff] [blame] | 546 | vkGetImageSubresourceLayout(device(), handle(), &subres, &data); |
Mark Lobodzinski | 64318ba | 2017-01-26 13:34:13 -0700 | [diff] [blame^] | 547 | if (size != sizeof(data)) memset(&data, 0, sizeof(data)); |
Courtney Goeltzenleuchter | 01ee1ca | 2015-09-10 16:41:13 -0600 | [diff] [blame] | 548 | |
| 549 | return data; |
| 550 | } |
| 551 | |
Karl Schultz | 6addd81 | 2016-02-02 17:17:23 -0700 | [diff] [blame] | 552 | bool Image::transparent() const { |
Mark Lobodzinski | 722841d | 2016-09-07 16:34:56 -0600 | [diff] [blame] | 553 | return (create_info_.tiling == VK_IMAGE_TILING_LINEAR && create_info_.samples == VK_SAMPLE_COUNT_1_BIT && |
| 554 | !(create_info_.usage & (VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT | VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT))); |
Chia-I Wu | f1e2e99 | 2014-12-27 14:12:52 +0800 | [diff] [blame] | 555 | } |
| 556 | |
Tony Barbour | 67e9915 | 2015-07-10 14:10:27 -0600 | [diff] [blame] | 557 | NON_DISPATCHABLE_HANDLE_DTOR(ImageView, vkDestroyImageView) |
Chia-I Wu | 3158bf3 | 2015-07-03 11:49:42 +0800 | [diff] [blame] | 558 | |
Karl Schultz | 6addd81 | 2016-02-02 17:17:23 -0700 | [diff] [blame] | 559 | void ImageView::init(const Device &dev, const VkImageViewCreateInfo &info) { |
Chia-I Wu | 3158bf3 | 2015-07-03 11:49:42 +0800 | [diff] [blame] | 560 | NON_DISPATCHABLE_HANDLE_INIT(vkCreateImageView, dev, &info); |
Chia-I Wu | f1e2e99 | 2014-12-27 14:12:52 +0800 | [diff] [blame] | 561 | } |
| 562 | |
Tony Barbour | 67e9915 | 2015-07-10 14:10:27 -0600 | [diff] [blame] | 563 | NON_DISPATCHABLE_HANDLE_DTOR(ShaderModule, vkDestroyShaderModule) |
Chia-I Wu | 4d0c792 | 2015-07-03 11:49:42 +0800 | [diff] [blame] | 564 | |
Mark Lobodzinski | 722841d | 2016-09-07 16:34:56 -0600 | [diff] [blame] | 565 | void ShaderModule::init(const Device &dev, const VkShaderModuleCreateInfo &info) { |
Chia-I Wu | 4d0c792 | 2015-07-03 11:49:42 +0800 | [diff] [blame] | 566 | NON_DISPATCHABLE_HANDLE_INIT(vkCreateShaderModule, dev, &info); |
Courtney Goeltzenleuchter | ee4027d | 2015-06-28 13:01:17 -0600 | [diff] [blame] | 567 | } |
| 568 | |
Mark Lobodzinski | 722841d | 2016-09-07 16:34:56 -0600 | [diff] [blame] | 569 | VkResult ShaderModule::init_try(const Device &dev, const VkShaderModuleCreateInfo &info) { |
Chia-I Wu | 4d0c792 | 2015-07-03 11:49:42 +0800 | [diff] [blame] | 570 | VkShaderModule mod; |
| 571 | |
Chia-I Wu | f7458c5 | 2015-10-26 21:10:41 +0800 | [diff] [blame] | 572 | VkResult err = vkCreateShaderModule(dev.handle(), &info, NULL, &mod); |
Mark Lobodzinski | 64318ba | 2017-01-26 13:34:13 -0700 | [diff] [blame^] | 573 | if (err == VK_SUCCESS) NonDispHandle::init(dev.handle(), mod); |
Courtney Goeltzenleuchter | ee4027d | 2015-06-28 13:01:17 -0600 | [diff] [blame] | 574 | |
| 575 | return err; |
| 576 | } |
| 577 | |
Tony Barbour | 67e9915 | 2015-07-10 14:10:27 -0600 | [diff] [blame] | 578 | NON_DISPATCHABLE_HANDLE_DTOR(Pipeline, vkDestroyPipeline) |
Chia-I Wu | 2ff72fd | 2015-07-03 11:49:42 +0800 | [diff] [blame] | 579 | |
Mark Lobodzinski | 722841d | 2016-09-07 16:34:56 -0600 | [diff] [blame] | 580 | void Pipeline::init(const Device &dev, const VkGraphicsPipelineCreateInfo &info) { |
Jon Ashburn | c669cc6 | 2015-07-09 15:02:25 -0600 | [diff] [blame] | 581 | VkPipelineCache cache; |
| 582 | VkPipelineCacheCreateInfo ci; |
Karl Schultz | 6addd81 | 2016-02-02 17:17:23 -0700 | [diff] [blame] | 583 | memset((void *)&ci, 0, sizeof(VkPipelineCacheCreateInfo)); |
Jon Ashburn | c669cc6 | 2015-07-09 15:02:25 -0600 | [diff] [blame] | 584 | ci.sType = VK_STRUCTURE_TYPE_PIPELINE_CACHE_CREATE_INFO; |
Chia-I Wu | f7458c5 | 2015-10-26 21:10:41 +0800 | [diff] [blame] | 585 | VkResult err = vkCreatePipelineCache(dev.handle(), &ci, NULL, &cache); |
Jon Ashburn | c669cc6 | 2015-07-09 15:02:25 -0600 | [diff] [blame] | 586 | if (err == VK_SUCCESS) { |
Mark Lobodzinski | 722841d | 2016-09-07 16:34:56 -0600 | [diff] [blame] | 587 | NON_DISPATCHABLE_HANDLE_INIT(vkCreateGraphicsPipelines, dev, cache, 1, &info); |
Chia-I Wu | f7458c5 | 2015-10-26 21:10:41 +0800 | [diff] [blame] | 588 | vkDestroyPipelineCache(dev.handle(), cache, NULL); |
Jon Ashburn | c669cc6 | 2015-07-09 15:02:25 -0600 | [diff] [blame] | 589 | } |
Chia-I Wu | f1e2e99 | 2014-12-27 14:12:52 +0800 | [diff] [blame] | 590 | } |
| 591 | |
Mark Lobodzinski | 722841d | 2016-09-07 16:34:56 -0600 | [diff] [blame] | 592 | VkResult Pipeline::init_try(const Device &dev, const VkGraphicsPipelineCreateInfo &info) { |
Chris Forbes | 95292b1 | 2015-05-25 11:13:26 +1200 | [diff] [blame] | 593 | VkPipeline pipe; |
Jon Ashburn | c669cc6 | 2015-07-09 15:02:25 -0600 | [diff] [blame] | 594 | VkPipelineCache cache; |
| 595 | VkPipelineCacheCreateInfo ci; |
Karl Schultz | 6addd81 | 2016-02-02 17:17:23 -0700 | [diff] [blame] | 596 | memset((void *)&ci, 0, sizeof(VkPipelineCacheCreateInfo)); |
Jon Ashburn | c669cc6 | 2015-07-09 15:02:25 -0600 | [diff] [blame] | 597 | ci.sType = VK_STRUCTURE_TYPE_PIPELINE_CACHE_CREATE_INFO; |
Chia-I Wu | f7458c5 | 2015-10-26 21:10:41 +0800 | [diff] [blame] | 598 | VkResult err = vkCreatePipelineCache(dev.handle(), &ci, NULL, &cache); |
Chia-I Wu | f368b60 | 2015-07-03 10:41:20 +0800 | [diff] [blame] | 599 | EXPECT(err == VK_SUCCESS); |
Chris Forbes | 95292b1 | 2015-05-25 11:13:26 +1200 | [diff] [blame] | 600 | if (err == VK_SUCCESS) { |
Mark Lobodzinski | 722841d | 2016-09-07 16:34:56 -0600 | [diff] [blame] | 601 | err = vkCreateGraphicsPipelines(dev.handle(), cache, 1, &info, NULL, &pipe); |
Jon Ashburn | c669cc6 | 2015-07-09 15:02:25 -0600 | [diff] [blame] | 602 | if (err == VK_SUCCESS) { |
Chia-I Wu | 2ff72fd | 2015-07-03 11:49:42 +0800 | [diff] [blame] | 603 | NonDispHandle::init(dev.handle(), pipe); |
Jon Ashburn | c669cc6 | 2015-07-09 15:02:25 -0600 | [diff] [blame] | 604 | } |
Chia-I Wu | f7458c5 | 2015-10-26 21:10:41 +0800 | [diff] [blame] | 605 | vkDestroyPipelineCache(dev.handle(), cache, NULL); |
Chris Forbes | 95292b1 | 2015-05-25 11:13:26 +1200 | [diff] [blame] | 606 | } |
| 607 | |
| 608 | return err; |
| 609 | } |
| 610 | |
Mark Lobodzinski | 722841d | 2016-09-07 16:34:56 -0600 | [diff] [blame] | 611 | void Pipeline::init(const Device &dev, const VkComputePipelineCreateInfo &info) { |
Jon Ashburn | c669cc6 | 2015-07-09 15:02:25 -0600 | [diff] [blame] | 612 | VkPipelineCache cache; |
| 613 | VkPipelineCacheCreateInfo ci; |
Karl Schultz | 6addd81 | 2016-02-02 17:17:23 -0700 | [diff] [blame] | 614 | memset((void *)&ci, 0, sizeof(VkPipelineCacheCreateInfo)); |
Jon Ashburn | c669cc6 | 2015-07-09 15:02:25 -0600 | [diff] [blame] | 615 | ci.sType = VK_STRUCTURE_TYPE_PIPELINE_CACHE_CREATE_INFO; |
Chia-I Wu | f7458c5 | 2015-10-26 21:10:41 +0800 | [diff] [blame] | 616 | VkResult err = vkCreatePipelineCache(dev.handle(), &ci, NULL, &cache); |
Jon Ashburn | c669cc6 | 2015-07-09 15:02:25 -0600 | [diff] [blame] | 617 | if (err == VK_SUCCESS) { |
Mark Lobodzinski | 722841d | 2016-09-07 16:34:56 -0600 | [diff] [blame] | 618 | NON_DISPATCHABLE_HANDLE_INIT(vkCreateComputePipelines, dev, cache, 1, &info); |
Chia-I Wu | f7458c5 | 2015-10-26 21:10:41 +0800 | [diff] [blame] | 619 | vkDestroyPipelineCache(dev.handle(), cache, NULL); |
Jon Ashburn | c669cc6 | 2015-07-09 15:02:25 -0600 | [diff] [blame] | 620 | } |
Chia-I Wu | f1e2e99 | 2014-12-27 14:12:52 +0800 | [diff] [blame] | 621 | } |
| 622 | |
Tony Barbour | 67e9915 | 2015-07-10 14:10:27 -0600 | [diff] [blame] | 623 | NON_DISPATCHABLE_HANDLE_DTOR(PipelineLayout, vkDestroyPipelineLayout) |
Chia-I Wu | fd46e7d | 2015-07-03 11:49:42 +0800 | [diff] [blame] | 624 | |
Mark Lobodzinski | 722841d | 2016-09-07 16:34:56 -0600 | [diff] [blame] | 625 | void PipelineLayout::init(const Device &dev, VkPipelineLayoutCreateInfo &info, |
| 626 | const std::vector<const DescriptorSetLayout *> &layouts) { |
| 627 | const std::vector<VkDescriptorSetLayout> layout_handles = make_handles<VkDescriptorSetLayout>(layouts); |
Tony Barbour | 482c609 | 2015-07-27 09:37:48 -0600 | [diff] [blame] | 628 | info.pSetLayouts = layout_handles.data(); |
Chia-I Wu | fd46e7d | 2015-07-03 11:49:42 +0800 | [diff] [blame] | 629 | |
| 630 | NON_DISPATCHABLE_HANDLE_INIT(vkCreatePipelineLayout, dev, &info); |
| 631 | } |
| 632 | |
Tony Barbour | 67e9915 | 2015-07-10 14:10:27 -0600 | [diff] [blame] | 633 | NON_DISPATCHABLE_HANDLE_DTOR(Sampler, vkDestroySampler) |
Chia-I Wu | 8c721c6 | 2015-07-03 11:49:42 +0800 | [diff] [blame] | 634 | |
Karl Schultz | 6addd81 | 2016-02-02 17:17:23 -0700 | [diff] [blame] | 635 | void Sampler::init(const Device &dev, const VkSamplerCreateInfo &info) { |
Chia-I Wu | 8c721c6 | 2015-07-03 11:49:42 +0800 | [diff] [blame] | 636 | NON_DISPATCHABLE_HANDLE_INIT(vkCreateSampler, dev, &info); |
Chia-I Wu | f1e2e99 | 2014-12-27 14:12:52 +0800 | [diff] [blame] | 637 | } |
| 638 | |
Tony Barbour | 67e9915 | 2015-07-10 14:10:27 -0600 | [diff] [blame] | 639 | NON_DISPATCHABLE_HANDLE_DTOR(DescriptorSetLayout, vkDestroyDescriptorSetLayout) |
Chia-I Wu | afdfd7f | 2015-07-03 11:49:42 +0800 | [diff] [blame] | 640 | |
Mark Lobodzinski | 722841d | 2016-09-07 16:34:56 -0600 | [diff] [blame] | 641 | void DescriptorSetLayout::init(const Device &dev, const VkDescriptorSetLayoutCreateInfo &info) { |
Chia-I Wu | afdfd7f | 2015-07-03 11:49:42 +0800 | [diff] [blame] | 642 | NON_DISPATCHABLE_HANDLE_INIT(vkCreateDescriptorSetLayout, dev, &info); |
Chia-I Wu | f1e2e99 | 2014-12-27 14:12:52 +0800 | [diff] [blame] | 643 | } |
| 644 | |
Tony Barbour | 67e9915 | 2015-07-10 14:10:27 -0600 | [diff] [blame] | 645 | NON_DISPATCHABLE_HANDLE_DTOR(DescriptorPool, vkDestroyDescriptorPool) |
Chia-I Wu | afdfd7f | 2015-07-03 11:49:42 +0800 | [diff] [blame] | 646 | |
Mark Lobodzinski | 722841d | 2016-09-07 16:34:56 -0600 | [diff] [blame] | 647 | void DescriptorPool::init(const Device &dev, const VkDescriptorPoolCreateInfo &info) { |
| 648 | setDynamicUsage(info.flags & VK_DESCRIPTOR_POOL_CREATE_FREE_DESCRIPTOR_SET_BIT); |
Courtney Goeltzenleuchter | fe908d3 | 2015-09-16 16:12:45 -0600 | [diff] [blame] | 649 | NON_DISPATCHABLE_HANDLE_INIT(vkCreateDescriptorPool, dev, &info); |
Chia-I Wu | f1e2e99 | 2014-12-27 14:12:52 +0800 | [diff] [blame] | 650 | } |
| 651 | |
Mark Lobodzinski | 722841d | 2016-09-07 16:34:56 -0600 | [diff] [blame] | 652 | void DescriptorPool::reset() { EXPECT(vkResetDescriptorPool(device(), handle(), 0) == VK_SUCCESS); } |
Chia-I Wu | f1e2e99 | 2014-12-27 14:12:52 +0800 | [diff] [blame] | 653 | |
Mark Lobodzinski | 722841d | 2016-09-07 16:34:56 -0600 | [diff] [blame] | 654 | std::vector<DescriptorSet *> DescriptorPool::alloc_sets(const Device &dev, |
| 655 | const std::vector<const DescriptorSetLayout *> &layouts) { |
| 656 | const std::vector<VkDescriptorSetLayout> layout_handles = make_handles<VkDescriptorSetLayout>(layouts); |
Chia-I Wu | 11078b0 | 2015-01-04 16:27:24 +0800 | [diff] [blame] | 657 | |
Chia-I Wu | afdfd7f | 2015-07-03 11:49:42 +0800 | [diff] [blame] | 658 | std::vector<VkDescriptorSet> set_handles; |
| 659 | set_handles.resize(layout_handles.size()); |
Chia-I Wu | 11078b0 | 2015-01-04 16:27:24 +0800 | [diff] [blame] | 660 | |
Chia-I Wu | 3432a0c | 2015-10-27 18:04:07 +0800 | [diff] [blame] | 661 | VkDescriptorSetAllocateInfo alloc_info = {}; |
Chia-I Wu | 00ce540 | 2015-11-10 16:21:09 +0800 | [diff] [blame] | 662 | alloc_info.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO; |
Jon Ashburn | f19916e | 2016-01-11 13:12:43 -0700 | [diff] [blame] | 663 | alloc_info.descriptorSetCount = layout_handles.size(); |
Courtney Goeltzenleuchter | bee18a9 | 2015-10-23 14:21:05 -0600 | [diff] [blame] | 664 | alloc_info.descriptorPool = handle(); |
| 665 | alloc_info.pSetLayouts = layout_handles.data(); |
Mark Lobodzinski | 722841d | 2016-09-07 16:34:56 -0600 | [diff] [blame] | 666 | VkResult err = vkAllocateDescriptorSets(device(), &alloc_info, set_handles.data()); |
Cody Northrop | 1e4f802 | 2015-08-03 12:47:29 -0600 | [diff] [blame] | 667 | EXPECT(err == VK_SUCCESS); |
Chia-I Wu | 11078b0 | 2015-01-04 16:27:24 +0800 | [diff] [blame] | 668 | |
| 669 | std::vector<DescriptorSet *> sets; |
Mark Lobodzinski | 722841d | 2016-09-07 16:34:56 -0600 | [diff] [blame] | 670 | 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] | 671 | // do descriptor sets need memories bound? |
Cody Northrop | cdc72a4 | 2015-10-08 11:39:25 -0600 | [diff] [blame] | 672 | DescriptorSet *descriptorSet = new DescriptorSet(dev, this, *it); |
Mark Lobodzinski | 40f7f40 | 2015-04-16 11:44:05 -0500 | [diff] [blame] | 673 | sets.push_back(descriptorSet); |
Chia-I Wu | 11078b0 | 2015-01-04 16:27:24 +0800 | [diff] [blame] | 674 | } |
Chia-I Wu | 11078b0 | 2015-01-04 16:27:24 +0800 | [diff] [blame] | 675 | return sets; |
| 676 | } |
| 677 | |
Mark Lobodzinski | 722841d | 2016-09-07 16:34:56 -0600 | [diff] [blame] | 678 | std::vector<DescriptorSet *> DescriptorPool::alloc_sets(const Device &dev, const DescriptorSetLayout &layout, uint32_t count) { |
| 679 | return alloc_sets(dev, std::vector<const DescriptorSetLayout *>(count, &layout)); |
Chia-I Wu | 11078b0 | 2015-01-04 16:27:24 +0800 | [diff] [blame] | 680 | } |
| 681 | |
Mark Lobodzinski | 722841d | 2016-09-07 16:34:56 -0600 | [diff] [blame] | 682 | DescriptorSet *DescriptorPool::alloc_sets(const Device &dev, const DescriptorSetLayout &layout) { |
Courtney Goeltzenleuchter | bee18a9 | 2015-10-23 14:21:05 -0600 | [diff] [blame] | 683 | std::vector<DescriptorSet *> set = alloc_sets(dev, layout, 1); |
Chia-I Wu | 11078b0 | 2015-01-04 16:27:24 +0800 | [diff] [blame] | 684 | return (set.empty()) ? NULL : set[0]; |
| 685 | } |
| 686 | |
Karl Schultz | 6addd81 | 2016-02-02 17:17:23 -0700 | [diff] [blame] | 687 | DescriptorSet::~DescriptorSet() { |
Tony Barbour | 67e9915 | 2015-07-10 14:10:27 -0600 | [diff] [blame] | 688 | if (initialized()) { |
Cody Northrop | cdc72a4 | 2015-10-08 11:39:25 -0600 | [diff] [blame] | 689 | // Only call vkFree* on sets allocated from pool with usage *_DYNAMIC |
| 690 | if (containing_pool_->getDynamicUsage()) { |
Karl Schultz | 6addd81 | 2016-02-02 17:17:23 -0700 | [diff] [blame] | 691 | VkDescriptorSet sets[1] = {handle()}; |
Mark Lobodzinski | 722841d | 2016-09-07 16:34:56 -0600 | [diff] [blame] | 692 | EXPECT(vkFreeDescriptorSets(device(), containing_pool_->GetObj(), 1, sets) == VK_SUCCESS); |
Cody Northrop | cdc72a4 | 2015-10-08 11:39:25 -0600 | [diff] [blame] | 693 | } |
Tony Barbour | 67e9915 | 2015-07-10 14:10:27 -0600 | [diff] [blame] | 694 | } |
| 695 | } |
Chia-I Wu | afdfd7f | 2015-07-03 11:49:42 +0800 | [diff] [blame] | 696 | |
Chia-I Wu | 3432a0c | 2015-10-27 18:04:07 +0800 | [diff] [blame] | 697 | NON_DISPATCHABLE_HANDLE_DTOR(CommandPool, vkDestroyCommandPool) |
Courtney Goeltzenleuchter | ee5d80b | 2015-07-10 19:50:17 -0600 | [diff] [blame] | 698 | |
Karl Schultz | 6addd81 | 2016-02-02 17:17:23 -0700 | [diff] [blame] | 699 | void CommandPool::init(const Device &dev, const VkCommandPoolCreateInfo &info) { |
Courtney Goeltzenleuchter | ee5d80b | 2015-07-10 19:50:17 -0600 | [diff] [blame] | 700 | NON_DISPATCHABLE_HANDLE_INIT(vkCreateCommandPool, dev, &info); |
| 701 | } |
| 702 | |
Karl Schultz | 6addd81 | 2016-02-02 17:17:23 -0700 | [diff] [blame] | 703 | CommandBuffer::~CommandBuffer() { |
Courtney Goeltzenleuchter | bee18a9 | 2015-10-23 14:21:05 -0600 | [diff] [blame] | 704 | if (initialized()) { |
Karl Schultz | 6addd81 | 2016-02-02 17:17:23 -0700 | [diff] [blame] | 705 | VkCommandBuffer cmds[] = {handle()}; |
Courtney Goeltzenleuchter | bee18a9 | 2015-10-23 14:21:05 -0600 | [diff] [blame] | 706 | vkFreeCommandBuffers(dev_handle_, cmd_pool_, 1, cmds); |
| 707 | } |
Chia-I Wu | be2b917 | 2015-07-03 11:49:42 +0800 | [diff] [blame] | 708 | } |
| 709 | |
Mark Lobodzinski | 722841d | 2016-09-07 16:34:56 -0600 | [diff] [blame] | 710 | void CommandBuffer::init(const Device &dev, const VkCommandBufferAllocateInfo &info) { |
Chia-I Wu | 3432a0c | 2015-10-27 18:04:07 +0800 | [diff] [blame] | 711 | VkCommandBuffer cmd; |
Chia-I Wu | be2b917 | 2015-07-03 11:49:42 +0800 | [diff] [blame] | 712 | |
Chia-I Wu | 3432a0c | 2015-10-27 18:04:07 +0800 | [diff] [blame] | 713 | // Make sure commandPool is set |
| 714 | assert(info.commandPool); |
Courtney Goeltzenleuchter | d8e68bb | 2015-07-13 12:53:32 -0600 | [diff] [blame] | 715 | |
Mark Lobodzinski | 722841d | 2016-09-07 16:34:56 -0600 | [diff] [blame] | 716 | if (EXPECT(vkAllocateCommandBuffers(dev.handle(), &info, &cmd) == VK_SUCCESS)) { |
Chia-I Wu | be2b917 | 2015-07-03 11:49:42 +0800 | [diff] [blame] | 717 | Handle::init(cmd); |
| 718 | dev_handle_ = dev.handle(); |
Chia-I Wu | 3432a0c | 2015-10-27 18:04:07 +0800 | [diff] [blame] | 719 | cmd_pool_ = info.commandPool; |
Chia-I Wu | be2b917 | 2015-07-03 11:49:42 +0800 | [diff] [blame] | 720 | } |
Chia-I Wu | f1e2e99 | 2014-12-27 14:12:52 +0800 | [diff] [blame] | 721 | } |
| 722 | |
Mark Lobodzinski | 722841d | 2016-09-07 16:34:56 -0600 | [diff] [blame] | 723 | void CommandBuffer::begin(const VkCommandBufferBeginInfo *info) { EXPECT(vkBeginCommandBuffer(handle(), info) == VK_SUCCESS); } |
Jeremy Hayes | d65ae08 | 2015-01-14 16:17:08 -0700 | [diff] [blame] | 724 | |
Karl Schultz | 6addd81 | 2016-02-02 17:17:23 -0700 | [diff] [blame] | 725 | void CommandBuffer::begin() { |
Chia-I Wu | 3432a0c | 2015-10-27 18:04:07 +0800 | [diff] [blame] | 726 | VkCommandBufferBeginInfo info = {}; |
Jon Ashburn | f19916e | 2016-01-11 13:12:43 -0700 | [diff] [blame] | 727 | VkCommandBufferInheritanceInfo hinfo = {}; |
Chia-I Wu | 3432a0c | 2015-10-27 18:04:07 +0800 | [diff] [blame] | 728 | info.flags = VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT; |
| 729 | info.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO; |
Jon Ashburn | f19916e | 2016-01-11 13:12:43 -0700 | [diff] [blame] | 730 | info.pInheritanceInfo = &hinfo; |
| 731 | hinfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_INHERITANCE_INFO; |
| 732 | hinfo.pNext = NULL; |
| 733 | hinfo.renderPass = VK_NULL_HANDLE; |
| 734 | hinfo.subpass = 0; |
| 735 | hinfo.framebuffer = VK_NULL_HANDLE; |
| 736 | hinfo.occlusionQueryEnable = VK_FALSE; |
| 737 | hinfo.queryFlags = 0; |
| 738 | hinfo.pipelineStatistics = 0; |
Jeremy Hayes | d65ae08 | 2015-01-14 16:17:08 -0700 | [diff] [blame] | 739 | |
| 740 | begin(&info); |
Chia-I Wu | f1e2e99 | 2014-12-27 14:12:52 +0800 | [diff] [blame] | 741 | } |
| 742 | |
Mark Lobodzinski | 722841d | 2016-09-07 16:34:56 -0600 | [diff] [blame] | 743 | void CommandBuffer::end() { EXPECT(vkEndCommandBuffer(handle()) == VK_SUCCESS); } |
Chia-I Wu | f1e2e99 | 2014-12-27 14:12:52 +0800 | [diff] [blame] | 744 | |
Mark Lobodzinski | 722841d | 2016-09-07 16:34:56 -0600 | [diff] [blame] | 745 | void CommandBuffer::reset(VkCommandBufferResetFlags flags) { EXPECT(vkResetCommandBuffer(handle(), flags) == VK_SUCCESS); } |
Chia-I Wu | f1e2e99 | 2014-12-27 14:12:52 +0800 | [diff] [blame] | 746 | |
Mark Lobodzinski | 64318ba | 2017-01-26 13:34:13 -0700 | [diff] [blame^] | 747 | }; // namespace vk_testing |