blob: a3ab19bb749b9e3ce73b623b905b25be292cac28 [file] [log] [blame]
Karl Schultz6addd812016-02-02 17:17:23 -07001/*
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 Ashburn3ebf1252016-04-19 11:30:31 -06006 * 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 Schultz6addd812016-02-02 17:17:23 -07009 *
Jon Ashburn3ebf1252016-04-19 11:30:31 -060010 * http://www.apache.org/licenses/LICENSE-2.0
Karl Schultz6addd812016-02-02 17:17:23 -070011 *
Jon Ashburn3ebf1252016-04-19 11:30:31 -060012 * 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 Schultz6addd812016-02-02 17:17:23 -070017 *
18 * Author: Courtney Goeltzenleuchter <courtney@LunarG.com>
19 * Author: Tony Barbour <tony@LunarG.com>
20 */
Chia-I Wuf1e2e992014-12-27 14:12:52 +080021
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -060022#include "vktestbinding.h"
Mark Lobodzinski722841d2016-09-07 16:34:56 -060023#include <assert.h>
24#include <iostream>
25#include <stdarg.h>
Mark Lobodzinski64318ba2017-01-26 13:34:13 -070026#include <string.h> // memset(), memcmp()
Chia-I Wuf1e2e992014-12-27 14:12:52 +080027
28namespace {
Chia-I Wuf1e2e992014-12-27 14:12:52 +080029
Mark Lobodzinski64318ba2017-01-26 13:34:13 -070030#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 Wuf8f074f2015-07-03 10:58:57 +080035 } while (0)
36
Mark Lobodzinski64318ba2017-01-26 13:34:13 -070037#define NON_DISPATCHABLE_HANDLE_DTOR(cls, destroy_func) \
38 cls::~cls() { \
39 if (initialized()) destroy_func(device(), handle(), NULL); \
Chia-I Wud9e8e822015-07-03 11:45:55 +080040 }
41
Chia-I Wuf1e2e992014-12-27 14:12:52 +080042#define STRINGIFY(x) #x
Mark Lobodzinski722841d2016-09-07 16:34:56 -060043#define EXPECT(expr) ((expr) ? true : expect_failure(STRINGIFY(expr), __FILE__, __LINE__, __FUNCTION__))
Chia-I Wuf1e2e992014-12-27 14:12:52 +080044
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -060045vk_testing::ErrorCallback error_callback;
Chia-I Wuf1e2e992014-12-27 14:12:52 +080046
Mark Lobodzinski722841d2016-09-07 16:34:56 -060047bool expect_failure(const char *expr, const char *file, unsigned int line, const char *function) {
Chia-I Wuf1e2e992014-12-27 14:12:52 +080048 if (error_callback) {
49 error_callback(expr, file, line, function);
50 } else {
Mark Lobodzinski722841d2016-09-07 16:34:56 -060051 std::cerr << file << ":" << line << ": " << function << ": Expectation `" << expr << "' failed.\n";
Chia-I Wuf1e2e992014-12-27 14:12:52 +080052 }
53
54 return false;
55}
56
Mark Lobodzinski64318ba2017-01-26 13:34:13 -070057template <class T, class S>
58std::vector<T> make_handles(const std::vector<S> &v) {
Chia-I Wud9e8e822015-07-03 11:45:55 +080059 std::vector<T> handles;
60 handles.reserve(v.size());
Mark Lobodzinski64318ba2017-01-26 13:34:13 -070061 for (typename std::vector<S>::const_iterator it = v.begin(); it != v.end(); it++) handles.push_back((*it)->handle());
Chia-I Wud9e8e822015-07-03 11:45:55 +080062 return handles;
63}
64
Mark Lobodzinski722841d2016-09-07 16:34:56 -060065VkMemoryAllocateInfo get_resource_alloc_info(const vk_testing::Device &dev, const VkMemoryRequirements &reqs,
Karl Schultz6addd812016-02-02 17:17:23 -070066 VkMemoryPropertyFlags mem_props) {
Mark Lobodzinski722841d2016-09-07 16:34:56 -060067 VkMemoryAllocateInfo info = vk_testing::DeviceMemory::alloc_info(reqs.size, 0);
Chia-I Wu681d7a02015-07-03 13:44:34 +080068 dev.phy().set_memory_type(reqs.memoryTypeBits, &info, mem_props);
Chia-I Wuf1e2e992014-12-27 14:12:52 +080069
70 return info;
71}
Chia-I Wu681d7a02015-07-03 13:44:34 +080072
Mark Lobodzinski64318ba2017-01-26 13:34:13 -070073} // namespace
Chia-I Wuf1e2e992014-12-27 14:12:52 +080074
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -060075namespace vk_testing {
Chia-I Wuf1e2e992014-12-27 14:12:52 +080076
Karl Schultz6addd812016-02-02 17:17:23 -070077void set_error_callback(ErrorCallback callback) { error_callback = callback; }
Chia-I Wuf1e2e992014-12-27 14:12:52 +080078
Karl Schultz6addd812016-02-02 17:17:23 -070079VkPhysicalDeviceProperties PhysicalDevice::properties() const {
Tony Barbour59a47322015-06-24 16:06:58 -060080 VkPhysicalDeviceProperties info;
81
Courtney Goeltzenleuchter06d89472015-10-20 16:40:38 -060082 vkGetPhysicalDeviceProperties(handle(), &info);
Tony Barbour59a47322015-06-24 16:06:58 -060083
84 return info;
Chia-I Wuf1e2e992014-12-27 14:12:52 +080085}
86
Karl Schultz6addd812016-02-02 17:17:23 -070087std::vector<VkQueueFamilyProperties> PhysicalDevice::queue_properties() const {
Cody Northropd0802882015-08-03 17:04:53 -060088 std::vector<VkQueueFamilyProperties> info;
Tony Barbour59a47322015-06-24 16:06:58 -060089 uint32_t count;
90
Cody Northropd0802882015-08-03 17:04:53 -060091 // Call once with NULL data to receive count
Courtney Goeltzenleuchter06d89472015-10-20 16:40:38 -060092 vkGetPhysicalDeviceQueueFamilyProperties(handle(), &count, NULL);
93 info.resize(count);
94 vkGetPhysicalDeviceQueueFamilyProperties(handle(), &count, info.data());
Tony Barbour59a47322015-06-24 16:06:58 -060095
96 return info;
Chia-I Wuf1e2e992014-12-27 14:12:52 +080097}
98
Karl Schultz6addd812016-02-02 17:17:23 -070099VkPhysicalDeviceMemoryProperties PhysicalDevice::memory_properties() const {
Tony Barbour59a47322015-06-24 16:06:58 -0600100 VkPhysicalDeviceMemoryProperties info;
101
Courtney Goeltzenleuchter06d89472015-10-20 16:40:38 -0600102 vkGetPhysicalDeviceMemoryProperties(handle(), &info);
Mark Lobodzinskib3fbcd92015-07-02 16:49:40 -0600103
Tony Barbour59a47322015-06-24 16:06:58 -0600104 return info;
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800105}
106
Chris Forbesf9cfe182016-04-04 17:22:42 +1200107VkPhysicalDeviceFeatures PhysicalDevice::features() const {
108 VkPhysicalDeviceFeatures features;
109 vkGetPhysicalDeviceFeatures(handle(), &features);
110 return features;
111}
112
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -0600113/*
114 * Return list of Global layers available
115 */
Karl Schultz6addd812016-02-02 17:17:23 -0700116std::vector<VkLayerProperties> GetGlobalLayers() {
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -0600117 VkResult err;
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -0600118 std::vector<VkLayerProperties> layers;
Courtney Goeltzenleuchtercd69eee2015-07-06 09:10:47 -0600119 uint32_t layer_count;
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -0600120
Courtney Goeltzenleuchtercd69eee2015-07-06 09:10:47 -0600121 do {
122 layer_count = 0;
Courtney Goeltzenleuchter35985f62015-09-14 17:22:16 -0600123 err = vkEnumerateInstanceLayerProperties(&layer_count, NULL);
Courtney Goeltzenleuchtercd69eee2015-07-06 09:10:47 -0600124
125 if (err == VK_SUCCESS) {
126 layers.reserve(layer_count);
Mark Lobodzinski722841d2016-09-07 16:34:56 -0600127 err = vkEnumerateInstanceLayerProperties(&layer_count, layers.data());
Courtney Goeltzenleuchtercd69eee2015-07-06 09:10:47 -0600128 }
129 } while (err == VK_INCOMPLETE);
130
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -0600131 assert(err == VK_SUCCESS);
132
133 return layers;
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800134}
135
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -0600136/*
137 * Return list of Global extensions provided by the ICD / Loader
138 */
Mark Lobodzinski722841d2016-09-07 16:34:56 -0600139std::vector<VkExtensionProperties> GetGlobalExtensions() { return GetGlobalExtensions(NULL); }
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -0600140
141/*
142 * Return list of Global extensions provided by the specified layer
Karl Schultz6addd812016-02-02 17:17:23 -0700143 * If pLayerName is NULL, will return extensions implemented by the loader /
144 * ICDs
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -0600145 */
Karl Schultz6addd812016-02-02 17:17:23 -0700146std::vector<VkExtensionProperties> GetGlobalExtensions(const char *pLayerName) {
Courtney Goeltzenleuchtercd69eee2015-07-06 09:10:47 -0600147 std::vector<VkExtensionProperties> exts;
148 uint32_t ext_count;
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -0600149 VkResult err;
150
Courtney Goeltzenleuchtercd69eee2015-07-06 09:10:47 -0600151 do {
152 ext_count = 0;
Mark Lobodzinski722841d2016-09-07 16:34:56 -0600153 err = vkEnumerateInstanceExtensionProperties(pLayerName, &ext_count, NULL);
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -0600154
Courtney Goeltzenleuchtercd69eee2015-07-06 09:10:47 -0600155 if (err == VK_SUCCESS) {
Courtney Goeltzenleuchter381f3a22015-07-06 15:45:58 -0600156 exts.resize(ext_count);
Mark Lobodzinski722841d2016-09-07 16:34:56 -0600157 err = vkEnumerateInstanceExtensionProperties(pLayerName, &ext_count, exts.data());
Courtney Goeltzenleuchtercd69eee2015-07-06 09:10:47 -0600158 }
159 } while (err == VK_INCOMPLETE);
160
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -0600161 assert(err == VK_SUCCESS);
162
163 return exts;
164}
165
166/*
167 * Return list of PhysicalDevice extensions provided by the ICD / Loader
168 */
Mark Lobodzinski722841d2016-09-07 16:34:56 -0600169std::vector<VkExtensionProperties> PhysicalDevice::extensions() const { return extensions(NULL); }
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -0600170
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 Lobodzinski722841d2016-09-07 16:34:56 -0600175std::vector<VkExtensionProperties> PhysicalDevice::extensions(const char *pLayerName) const {
Courtney Goeltzenleuchtercd69eee2015-07-06 09:10:47 -0600176 std::vector<VkExtensionProperties> exts;
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -0600177 VkResult err;
178
Courtney Goeltzenleuchtercd69eee2015-07-06 09:10:47 -0600179 do {
180 uint32_t extCount = 0;
Mark Lobodzinski722841d2016-09-07 16:34:56 -0600181 err = vkEnumerateDeviceExtensionProperties(handle(), pLayerName, &extCount, NULL);
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -0600182
Courtney Goeltzenleuchtercd69eee2015-07-06 09:10:47 -0600183 if (err == VK_SUCCESS) {
Ian Elliott1a3845b2015-07-06 14:33:04 -0600184 exts.resize(extCount);
Mark Lobodzinski722841d2016-09-07 16:34:56 -0600185 err = vkEnumerateDeviceExtensionProperties(handle(), pLayerName, &extCount, exts.data());
Courtney Goeltzenleuchtercd69eee2015-07-06 09:10:47 -0600186 }
187 } while (err == VK_INCOMPLETE);
188
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -0600189 assert(err == VK_SUCCESS);
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800190
191 return exts;
192}
193
Mark Lobodzinski722841d2016-09-07 16:34:56 -0600194bool PhysicalDevice::set_memory_type(const uint32_t type_bits, VkMemoryAllocateInfo *info, const VkFlags properties,
Karl Schultz6addd812016-02-02 17:17:23 -0700195 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 Lobodzinski722841d2016-09-07 16:34:56 -0600201 if ((memory_properties_.memoryTypes[i].propertyFlags & properties) == properties &&
202 (memory_properties_.memoryTypes[i].propertyFlags & forbid) == 0) {
Karl Schultz6addd812016-02-02 17:17:23 -0700203 info->memoryTypeIndex = i;
204 return true;
205 }
206 }
207 type_mask >>= 1;
208 }
209 // No memory types matched, return failure
210 return false;
Mark Lobodzinskib3fbcd92015-07-02 16:49:40 -0600211}
212
Courtney Goeltzenleuchtercd69eee2015-07-06 09:10:47 -0600213/*
214 * Return list of PhysicalDevice layers
215 */
Karl Schultz6addd812016-02-02 17:17:23 -0700216std::vector<VkLayerProperties> PhysicalDevice::layers() const {
Courtney Goeltzenleuchtercd69eee2015-07-06 09:10:47 -0600217 std::vector<VkLayerProperties> layer_props;
218 VkResult err;
219
220 do {
221 uint32_t layer_count = 0;
Courtney Goeltzenleuchter35985f62015-09-14 17:22:16 -0600222 err = vkEnumerateDeviceLayerProperties(handle(), &layer_count, NULL);
Courtney Goeltzenleuchtercd69eee2015-07-06 09:10:47 -0600223
224 if (err == VK_SUCCESS) {
225 layer_props.reserve(layer_count);
Mark Lobodzinski722841d2016-09-07 16:34:56 -0600226 err = vkEnumerateDeviceLayerProperties(handle(), &layer_count, layer_props.data());
Courtney Goeltzenleuchtercd69eee2015-07-06 09:10:47 -0600227 }
228 } while (err == VK_INCOMPLETE);
229
230 assert(err == VK_SUCCESS);
231
232 return layer_props;
233}
234
Karl Schultz6addd812016-02-02 17:17:23 -0700235Device::~Device() {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700236 if (!initialized()) return;
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800237
238 for (int i = 0; i < QUEUE_COUNT; i++) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700239 for (std::vector<Queue *>::iterator it = queues_[i].begin(); it != queues_[i].end(); it++) delete *it;
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800240 queues_[i].clear();
241 }
242
Chia-I Wuf7458c52015-10-26 21:10:41 +0800243 vkDestroyDevice(handle(), NULL);
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800244}
245
Tony Barbour53f7e892016-08-09 13:44:00 -0600246void Device::init(std::vector<const char *> &extensions, VkPhysicalDeviceFeatures *features) {
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800247 // request all queues
Mark Lobodzinski722841d2016-09-07 16:34:56 -0600248 const std::vector<VkQueueFamilyProperties> queue_props = phy_.queue_properties();
Courtney Goeltzenleuchter95487bc2015-04-14 18:48:46 -0600249 std::vector<VkDeviceQueueCreateInfo> queue_info;
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800250 queue_info.reserve(queue_props.size());
Mark Lobodzinski53d023a2016-02-01 09:06:25 -0700251
252 std::vector<std::vector<float>> queue_priorities;
253
Mark Young93ecb1d2016-01-13 13:47:16 -0700254 for (uint32_t i = 0; i < (uint32_t)queue_props.size(); i++) {
Courtney Goeltzenleuchter95487bc2015-04-14 18:48:46 -0600255 VkDeviceQueueCreateInfo qi = {};
Karl Schultz6addd812016-02-02 17:17:23 -0700256 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 Lobodzinski53d023a2016-02-01 09:06:25 -0700260
Dustin Graves3c0bd582016-04-06 10:16:05 -0600261 queue_priorities.emplace_back(qi.queueCount, 0.0f);
Mark Lobodzinski53d023a2016-02-01 09:06:25 -0700262
263 qi.pQueuePriorities = queue_priorities[i].data();
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600264 if (queue_props[i].queueFlags & VK_QUEUE_GRAPHICS_BIT) {
Courtney Goeltzenleuchter18248e62015-03-05 18:09:39 -0700265 graphics_queue_node_index_ = i;
266 }
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800267 queue_info.push_back(qi);
268 }
269
Courtney Goeltzenleuchter95487bc2015-04-14 18:48:46 -0600270 VkDeviceCreateInfo dev_info = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600271 dev_info.sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO;
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600272 dev_info.pNext = NULL;
Chia-I Wu02124482015-11-06 06:42:02 +0800273 dev_info.queueCreateInfoCount = queue_info.size();
274 dev_info.pQueueCreateInfos = queue_info.data();
Tony Barbour4c70d102016-08-08 16:06:56 -0600275 dev_info.enabledLayerCount = 0;
276 dev_info.ppEnabledLayerNames = NULL;
Jon Ashburnf19916e2016-01-11 13:12:43 -0700277 dev_info.enabledExtensionCount = extensions.size();
Tony Barbour482c6092015-07-27 09:37:48 -0600278 dev_info.ppEnabledExtensionNames = extensions.data();
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800279
Tony Barbour53f7e892016-08-09 13:44:00 -0600280 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 Forbesf9cfe182016-04-04 17:22:42 +1200288
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800289 init(dev_info);
290}
291
Karl Schultz6addd812016-02-02 17:17:23 -0700292void Device::init(const VkDeviceCreateInfo &info) {
Chia-I Wuf368b602015-07-03 10:41:20 +0800293 VkDevice dev;
294
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700295 if (EXPECT(vkCreateDevice(phy_.handle(), &info, NULL, &dev) == VK_SUCCESS)) Handle::init(dev);
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800296
297 init_queues();
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800298 init_formats();
299}
300
Karl Schultz6addd812016-02-02 17:17:23 -0700301void Device::init_queues() {
Courtney Goeltzenleuchter18248e62015-03-05 18:09:39 -0700302 uint32_t queue_node_count;
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800303
Cody Northropd0802882015-08-03 17:04:53 -0600304 // Call with NULL data to get count
Mark Lobodzinski722841d2016-09-07 16:34:56 -0600305 vkGetPhysicalDeviceQueueFamilyProperties(phy_.handle(), &queue_node_count, NULL);
Courtney Goeltzenleuchter18248e62015-03-05 18:09:39 -0700306 EXPECT(queue_node_count >= 1);
307
Mark Lobodzinski722841d2016-09-07 16:34:56 -0600308 VkQueueFamilyProperties *queue_props = new VkQueueFamilyProperties[queue_node_count];
Courtney Goeltzenleuchter18248e62015-03-05 18:09:39 -0700309
Mark Lobodzinski722841d2016-09-07 16:34:56 -0600310 vkGetPhysicalDeviceQueueFamilyProperties(phy_.handle(), &queue_node_count, queue_props);
Courtney Goeltzenleuchter18248e62015-03-05 18:09:39 -0700311
Tony Barbour7ea6aa22015-05-22 09:44:58 -0600312 for (uint32_t i = 0; i < queue_node_count; i++) {
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600313 VkQueue queue;
Courtney Goeltzenleuchter18248e62015-03-05 18:09:39 -0700314
Tony Barbour7ea6aa22015-05-22 09:44:58 -0600315 for (uint32_t j = 0; j < queue_props[i].queueCount; j++) {
Karl Schultz6addd812016-02-02 17:17:23 -0700316 // TODO: Need to add support for separate MEMMGR and work queues,
317 // including synchronization
Courtney Goeltzenleuchter06d89472015-10-20 16:40:38 -0600318 vkGetDeviceQueue(handle(), i, j, &queue);
Courtney Goeltzenleuchter18248e62015-03-05 18:09:39 -0700319
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600320 if (queue_props[i].queueFlags & VK_QUEUE_GRAPHICS_BIT) {
Tony Barbourfb21ea32015-07-23 10:35:30 -0600321 queues_[GRAPHICS].push_back(new Queue(queue, i));
Courtney Goeltzenleuchter18248e62015-03-05 18:09:39 -0700322 }
323
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600324 if (queue_props[i].queueFlags & VK_QUEUE_COMPUTE_BIT) {
Tony Barbourfb21ea32015-07-23 10:35:30 -0600325 queues_[COMPUTE].push_back(new Queue(queue, i));
Courtney Goeltzenleuchter18248e62015-03-05 18:09:39 -0700326 }
327
Chia-I Wu1b99bb22015-10-27 19:25:11 +0800328 if (queue_props[i].queueFlags & VK_QUEUE_TRANSFER_BIT) {
Tony Barbourfb21ea32015-07-23 10:35:30 -0600329 queues_[DMA].push_back(new Queue(queue, i));
Courtney Goeltzenleuchter18248e62015-03-05 18:09:39 -0700330 }
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800331 }
332 }
333
Chris Forbes02038792015-06-04 10:49:27 +1200334 delete[] queue_props;
Tony Barbour3d69c9e2015-05-20 16:53:31 -0600335
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800336 EXPECT(!queues_[GRAPHICS].empty() || !queues_[COMPUTE].empty());
337}
338
Karl Schultz6addd812016-02-02 17:17:23 -0700339void Device::init_formats() {
Tony Barbourd1c35722015-04-16 15:59:00 -0600340 for (int f = VK_FORMAT_BEGIN_RANGE; f <= VK_FORMAT_END_RANGE; f++) {
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600341 const VkFormat fmt = static_cast<VkFormat>(f);
342 const VkFormatProperties props = format_properties(fmt);
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800343
Jeremy Hayesa058eee2015-01-23 08:51:43 -0700344 if (props.linearTilingFeatures) {
Mark Lobodzinski722841d2016-09-07 16:34:56 -0600345 const Format tmp = {fmt, VK_IMAGE_TILING_LINEAR, props.linearTilingFeatures};
Jeremy Hayesa058eee2015-01-23 08:51:43 -0700346 formats_.push_back(tmp);
347 }
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800348
Jeremy Hayesa058eee2015-01-23 08:51:43 -0700349 if (props.optimalTilingFeatures) {
Mark Lobodzinski722841d2016-09-07 16:34:56 -0600350 const Format tmp = {fmt, VK_IMAGE_TILING_OPTIMAL, props.optimalTilingFeatures};
Jeremy Hayesa058eee2015-01-23 08:51:43 -0700351 formats_.push_back(tmp);
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800352 }
353 }
354
355 EXPECT(!formats_.empty());
356}
357
Karl Schultz6addd812016-02-02 17:17:23 -0700358VkFormatProperties Device::format_properties(VkFormat format) {
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600359 VkFormatProperties data;
Courtney Goeltzenleuchter06d89472015-10-20 16:40:38 -0600360 vkGetPhysicalDeviceFormatProperties(phy().handle(), format, &data);
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800361
362 return data;
363}
364
Karl Schultz6addd812016-02-02 17:17:23 -0700365void Device::wait() { EXPECT(vkDeviceWaitIdle(handle()) == VK_SUCCESS); }
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800366
Mark Lobodzinski722841d2016-09-07 16:34:56 -0600367VkResult Device::wait(const std::vector<const Fence *> &fences, bool wait_all, uint64_t timeout) {
Chia-I Wud9e8e822015-07-03 11:45:55 +0800368 const std::vector<VkFence> fence_handles = make_handles<VkFence>(fences);
Mark Lobodzinski722841d2016-09-07 16:34:56 -0600369 VkResult err = vkWaitForFences(handle(), fence_handles.size(), fence_handles.data(), wait_all, timeout);
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600370 EXPECT(err == VK_SUCCESS || err == VK_TIMEOUT);
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800371
372 return err;
373}
374
Mark Lobodzinski722841d2016-09-07 16:34:56 -0600375void 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 Wu9d00ed72015-05-25 16:27:55 +0800378}
379
Mark Lobodzinski722841d2016-09-07 16:34:56 -0600380void Queue::submit(const std::vector<const CommandBuffer *> &cmds, Fence &fence) {
381 const std::vector<VkCommandBuffer> cmd_handles = make_handles<VkCommandBuffer>(cmds);
Courtney Goeltzenleuchter806c7002015-10-27 11:22:14 -0600382 VkSubmitInfo submit_info;
Chia-I Wuf9be13c2015-10-26 20:37:06 +0800383 submit_info.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
384 submit_info.pNext = NULL;
Chia-I Wud50a7d72015-10-26 20:48:51 +0800385 submit_info.waitSemaphoreCount = 0;
Courtney Goeltzenleuchter806c7002015-10-27 11:22:14 -0600386 submit_info.pWaitSemaphores = NULL;
Jon Ashburn7f9716c2015-12-30 16:42:50 -0700387 submit_info.pWaitDstStageMask = NULL;
Chia-I Wud50a7d72015-10-26 20:48:51 +0800388 submit_info.commandBufferCount = (uint32_t)cmd_handles.size();
Courtney Goeltzenleuchter806c7002015-10-27 11:22:14 -0600389 submit_info.pCommandBuffers = cmd_handles.data();
Chia-I Wud50a7d72015-10-26 20:48:51 +0800390 submit_info.signalSemaphoreCount = 0;
Courtney Goeltzenleuchter806c7002015-10-27 11:22:14 -0600391 submit_info.pSignalSemaphores = NULL;
Courtney Goeltzenleuchter646b9072015-10-20 18:04:07 -0600392
Mark Lobodzinski722841d2016-09-07 16:34:56 -0600393 EXPECT(vkQueueSubmit(handle(), 1, &submit_info, fence.handle()) == VK_SUCCESS);
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800394}
395
Mark Lobodzinski722841d2016-09-07 16:34:56 -0600396void Queue::submit(const CommandBuffer &cmd, Fence &fence) { submit(std::vector<const CommandBuffer *>(1, &cmd), fence); }
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800397
Karl Schultz6addd812016-02-02 17:17:23 -0700398void Queue::submit(const CommandBuffer &cmd) {
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800399 Fence fence;
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600400 submit(cmd, fence);
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800401}
402
Karl Schultz6addd812016-02-02 17:17:23 -0700403void Queue::wait() { EXPECT(vkQueueWaitIdle(handle()) == VK_SUCCESS); }
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800404
Karl Schultz6addd812016-02-02 17:17:23 -0700405DeviceMemory::~DeviceMemory() {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700406 if (initialized()) vkFreeMemory(device(), handle(), NULL);
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800407}
408
Karl Schultz6addd812016-02-02 17:17:23 -0700409void DeviceMemory::init(const Device &dev, const VkMemoryAllocateInfo &info) {
Chia-I Wu3432a0c2015-10-27 18:04:07 +0800410 NON_DISPATCHABLE_HANDLE_INIT(vkAllocateMemory, dev, &info);
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800411}
412
Karl Schultz6addd812016-02-02 17:17:23 -0700413const void *DeviceMemory::map(VkFlags flags) const {
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800414 void *data;
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700415 if (!EXPECT(vkMapMemory(device(), handle(), 0, VK_WHOLE_SIZE, flags, &data) == VK_SUCCESS)) data = NULL;
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800416
417 return data;
418}
419
Karl Schultz6addd812016-02-02 17:17:23 -0700420void *DeviceMemory::map(VkFlags flags) {
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800421 void *data;
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700422 if (!EXPECT(vkMapMemory(device(), handle(), 0, VK_WHOLE_SIZE, flags, &data) == VK_SUCCESS)) data = NULL;
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800423
424 return data;
425}
426
Karl Schultz6addd812016-02-02 17:17:23 -0700427void DeviceMemory::unmap() const { vkUnmapMemory(device(), handle()); }
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800428
Tony Barbour67e99152015-07-10 14:10:27 -0600429NON_DISPATCHABLE_HANDLE_DTOR(Fence, vkDestroyFence)
Chia-I Wud9e8e822015-07-03 11:45:55 +0800430
Mark Lobodzinski722841d2016-09-07 16:34:56 -0600431void Fence::init(const Device &dev, const VkFenceCreateInfo &info) { NON_DISPATCHABLE_HANDLE_INIT(vkCreateFence, dev, &info); }
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800432
Tony Barbour67e99152015-07-10 14:10:27 -0600433NON_DISPATCHABLE_HANDLE_DTOR(Semaphore, vkDestroySemaphore)
Chia-I Wu6b1c2482015-07-03 11:49:42 +0800434
Karl Schultz6addd812016-02-02 17:17:23 -0700435void Semaphore::init(const Device &dev, const VkSemaphoreCreateInfo &info) {
Chia-I Wu6b1c2482015-07-03 11:49:42 +0800436 NON_DISPATCHABLE_HANDLE_INIT(vkCreateSemaphore, dev, &info);
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800437}
438
Tony Barbour67e99152015-07-10 14:10:27 -0600439NON_DISPATCHABLE_HANDLE_DTOR(Event, vkDestroyEvent)
Chia-I Wuc5c97992015-07-03 11:49:42 +0800440
Mark Lobodzinski722841d2016-09-07 16:34:56 -0600441void Event::init(const Device &dev, const VkEventCreateInfo &info) { NON_DISPATCHABLE_HANDLE_INIT(vkCreateEvent, dev, &info); }
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800442
Karl Schultz6addd812016-02-02 17:17:23 -0700443void Event::set() { EXPECT(vkSetEvent(device(), handle()) == VK_SUCCESS); }
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800444
Karl Schultz6addd812016-02-02 17:17:23 -0700445void Event::reset() { EXPECT(vkResetEvent(device(), handle()) == VK_SUCCESS); }
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800446
Tony Barbour67e99152015-07-10 14:10:27 -0600447NON_DISPATCHABLE_HANDLE_DTOR(QueryPool, vkDestroyQueryPool)
Chia-I Wu1b7d4762015-07-03 11:49:42 +0800448
Karl Schultz6addd812016-02-02 17:17:23 -0700449void QueryPool::init(const Device &dev, const VkQueryPoolCreateInfo &info) {
Chia-I Wu1b7d4762015-07-03 11:49:42 +0800450 NON_DISPATCHABLE_HANDLE_INIT(vkCreateQueryPool, dev, &info);
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800451}
452
Mark Lobodzinski722841d2016-09-07 16:34:56 -0600453VkResult 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 Wuccc93a72015-10-26 18:36:20 +0800455 EXPECT(err == VK_SUCCESS || err == VK_NOT_READY);
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800456
457 return err;
458}
459
Tony Barbour67e99152015-07-10 14:10:27 -0600460NON_DISPATCHABLE_HANDLE_DTOR(Buffer, vkDestroyBuffer)
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800461
Mark Lobodzinski722841d2016-09-07 16:34:56 -0600462void Buffer::init(const Device &dev, const VkBufferCreateInfo &info, VkMemoryPropertyFlags mem_props) {
Tony Barbour4c97d7a2015-04-22 15:10:33 -0600463 init_no_mem(dev, info);
Chia-I Wu681d7a02015-07-03 13:44:34 +0800464
Mark Lobodzinski722841d2016-09-07 16:34:56 -0600465 internal_mem_.init(dev, get_resource_alloc_info(dev, memory_requirements(), mem_props));
Chia-I Wu681d7a02015-07-03 13:44:34 +0800466 bind_memory(internal_mem_, 0);
Tony Barbour4c97d7a2015-04-22 15:10:33 -0600467}
468
Karl Schultz6addd812016-02-02 17:17:23 -0700469void Buffer::init_no_mem(const Device &dev, const VkBufferCreateInfo &info) {
Chia-I Wu681d7a02015-07-03 13:44:34 +0800470 NON_DISPATCHABLE_HANDLE_INIT(vkCreateBuffer, dev, &info);
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800471 create_info_ = info;
472}
473
Karl Schultz6addd812016-02-02 17:17:23 -0700474VkMemoryRequirements Buffer::memory_requirements() const {
Chia-I Wu681d7a02015-07-03 13:44:34 +0800475 VkMemoryRequirements reqs;
476
Courtney Goeltzenleuchter06d89472015-10-20 16:40:38 -0600477 vkGetBufferMemoryRequirements(device(), handle(), &reqs);
Chia-I Wu681d7a02015-07-03 13:44:34 +0800478
479 return reqs;
480}
481
Karl Schultz6addd812016-02-02 17:17:23 -0700482void Buffer::bind_memory(const DeviceMemory &mem, VkDeviceSize mem_offset) {
Mark Lobodzinski722841d2016-09-07 16:34:56 -0600483 EXPECT(vkBindBufferMemory(device(), handle(), mem.handle(), mem_offset) == VK_SUCCESS);
Mark Lobodzinski942b1722015-05-11 17:21:15 -0500484}
485
Tony Barbour67e99152015-07-10 14:10:27 -0600486NON_DISPATCHABLE_HANDLE_DTOR(BufferView, vkDestroyBufferView)
Chia-I Wu3158bf32015-07-03 11:49:42 +0800487
Karl Schultz6addd812016-02-02 17:17:23 -0700488void BufferView::init(const Device &dev, const VkBufferViewCreateInfo &info) {
Chia-I Wu3158bf32015-07-03 11:49:42 +0800489 NON_DISPATCHABLE_HANDLE_INIT(vkCreateBufferView, dev, &info);
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800490}
491
Tony Barbour67e99152015-07-10 14:10:27 -0600492NON_DISPATCHABLE_HANDLE_DTOR(Image, vkDestroyImage)
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800493
Mark Lobodzinski722841d2016-09-07 16:34:56 -0600494void Image::init(const Device &dev, const VkImageCreateInfo &info, VkMemoryPropertyFlags mem_props) {
Tony Barbour4c97d7a2015-04-22 15:10:33 -0600495 init_no_mem(dev, info);
Chia-I Wu681d7a02015-07-03 13:44:34 +0800496
Karl Schultzb5bc11e2016-05-04 08:36:08 -0600497 if (initialized()) {
Mark Lobodzinski722841d2016-09-07 16:34:56 -0600498 internal_mem_.init(dev, get_resource_alloc_info(dev, memory_requirements(), mem_props));
Karl Schultzb5bc11e2016-05-04 08:36:08 -0600499 bind_memory(internal_mem_, 0);
500 }
Tony Barbour4c97d7a2015-04-22 15:10:33 -0600501}
502
Karl Schultz6addd812016-02-02 17:17:23 -0700503void Image::init_no_mem(const Device &dev, const VkImageCreateInfo &info) {
Chia-I Wu681d7a02015-07-03 13:44:34 +0800504 NON_DISPATCHABLE_HANDLE_INIT(vkCreateImage, dev, &info);
Karl Schultzb5bc11e2016-05-04 08:36:08 -0600505 if (initialized()) {
506 init_info(dev, info);
507 }
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800508}
509
Karl Schultz6addd812016-02-02 17:17:23 -0700510void Image::init_info(const Device &dev, const VkImageCreateInfo &info) {
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800511 create_info_ = info;
512
Mark Lobodzinski722841d2016-09-07 16:34:56 -0600513 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 Wuf1e2e992014-12-27 14:12:52 +0800515 format_features_ = it->features;
516 break;
517 }
518 }
519}
520
Karl Schultz6addd812016-02-02 17:17:23 -0700521VkMemoryRequirements Image::memory_requirements() const {
Chia-I Wu681d7a02015-07-03 13:44:34 +0800522 VkMemoryRequirements reqs;
523
Courtney Goeltzenleuchter06d89472015-10-20 16:40:38 -0600524 vkGetImageMemoryRequirements(device(), handle(), &reqs);
Chia-I Wu681d7a02015-07-03 13:44:34 +0800525
526 return reqs;
527}
528
Karl Schultz6addd812016-02-02 17:17:23 -0700529void Image::bind_memory(const DeviceMemory &mem, VkDeviceSize mem_offset) {
Mark Lobodzinski722841d2016-09-07 16:34:56 -0600530 EXPECT(vkBindImageMemory(device(), handle(), mem.handle(), mem_offset) == VK_SUCCESS);
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800531}
532
Mark Lobodzinski722841d2016-09-07 16:34:56 -0600533VkSubresourceLayout Image::subresource_layout(const VkImageSubresource &subres) const {
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600534 VkSubresourceLayout data;
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800535 size_t size = sizeof(data);
Courtney Goeltzenleuchter06d89472015-10-20 16:40:38 -0600536 vkGetImageSubresourceLayout(device(), handle(), &subres, &data);
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700537 if (size != sizeof(data)) memset(&data, 0, sizeof(data));
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800538
539 return data;
540}
541
Mark Lobodzinski722841d2016-09-07 16:34:56 -0600542VkSubresourceLayout Image::subresource_layout(const VkImageSubresourceLayers &subrescopy) const {
Courtney Goeltzenleuchter01ee1ca2015-09-10 16:41:13 -0600543 VkSubresourceLayout data;
Mark Lobodzinski722841d2016-09-07 16:34:56 -0600544 VkImageSubresource subres = subresource(subrescopy.aspectMask, subrescopy.mipLevel, subrescopy.baseArrayLayer);
Courtney Goeltzenleuchter01ee1ca2015-09-10 16:41:13 -0600545 size_t size = sizeof(data);
Courtney Goeltzenleuchter06d89472015-10-20 16:40:38 -0600546 vkGetImageSubresourceLayout(device(), handle(), &subres, &data);
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700547 if (size != sizeof(data)) memset(&data, 0, sizeof(data));
Courtney Goeltzenleuchter01ee1ca2015-09-10 16:41:13 -0600548
549 return data;
550}
551
Karl Schultz6addd812016-02-02 17:17:23 -0700552bool Image::transparent() const {
Mark Lobodzinski722841d2016-09-07 16:34:56 -0600553 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 Wuf1e2e992014-12-27 14:12:52 +0800555}
556
Tony Barbour67e99152015-07-10 14:10:27 -0600557NON_DISPATCHABLE_HANDLE_DTOR(ImageView, vkDestroyImageView)
Chia-I Wu3158bf32015-07-03 11:49:42 +0800558
Karl Schultz6addd812016-02-02 17:17:23 -0700559void ImageView::init(const Device &dev, const VkImageViewCreateInfo &info) {
Chia-I Wu3158bf32015-07-03 11:49:42 +0800560 NON_DISPATCHABLE_HANDLE_INIT(vkCreateImageView, dev, &info);
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800561}
562
Tony Barbour67e99152015-07-10 14:10:27 -0600563NON_DISPATCHABLE_HANDLE_DTOR(ShaderModule, vkDestroyShaderModule)
Chia-I Wu4d0c7922015-07-03 11:49:42 +0800564
Mark Lobodzinski722841d2016-09-07 16:34:56 -0600565void ShaderModule::init(const Device &dev, const VkShaderModuleCreateInfo &info) {
Chia-I Wu4d0c7922015-07-03 11:49:42 +0800566 NON_DISPATCHABLE_HANDLE_INIT(vkCreateShaderModule, dev, &info);
Courtney Goeltzenleuchteree4027d2015-06-28 13:01:17 -0600567}
568
Mark Lobodzinski722841d2016-09-07 16:34:56 -0600569VkResult ShaderModule::init_try(const Device &dev, const VkShaderModuleCreateInfo &info) {
Chia-I Wu4d0c7922015-07-03 11:49:42 +0800570 VkShaderModule mod;
571
Chia-I Wuf7458c52015-10-26 21:10:41 +0800572 VkResult err = vkCreateShaderModule(dev.handle(), &info, NULL, &mod);
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700573 if (err == VK_SUCCESS) NonDispHandle::init(dev.handle(), mod);
Courtney Goeltzenleuchteree4027d2015-06-28 13:01:17 -0600574
575 return err;
576}
577
Tony Barbour67e99152015-07-10 14:10:27 -0600578NON_DISPATCHABLE_HANDLE_DTOR(Pipeline, vkDestroyPipeline)
Chia-I Wu2ff72fd2015-07-03 11:49:42 +0800579
Mark Lobodzinski722841d2016-09-07 16:34:56 -0600580void Pipeline::init(const Device &dev, const VkGraphicsPipelineCreateInfo &info) {
Jon Ashburnc669cc62015-07-09 15:02:25 -0600581 VkPipelineCache cache;
582 VkPipelineCacheCreateInfo ci;
Karl Schultz6addd812016-02-02 17:17:23 -0700583 memset((void *)&ci, 0, sizeof(VkPipelineCacheCreateInfo));
Jon Ashburnc669cc62015-07-09 15:02:25 -0600584 ci.sType = VK_STRUCTURE_TYPE_PIPELINE_CACHE_CREATE_INFO;
Chia-I Wuf7458c52015-10-26 21:10:41 +0800585 VkResult err = vkCreatePipelineCache(dev.handle(), &ci, NULL, &cache);
Jon Ashburnc669cc62015-07-09 15:02:25 -0600586 if (err == VK_SUCCESS) {
Mark Lobodzinski722841d2016-09-07 16:34:56 -0600587 NON_DISPATCHABLE_HANDLE_INIT(vkCreateGraphicsPipelines, dev, cache, 1, &info);
Chia-I Wuf7458c52015-10-26 21:10:41 +0800588 vkDestroyPipelineCache(dev.handle(), cache, NULL);
Jon Ashburnc669cc62015-07-09 15:02:25 -0600589 }
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800590}
591
Mark Lobodzinski722841d2016-09-07 16:34:56 -0600592VkResult Pipeline::init_try(const Device &dev, const VkGraphicsPipelineCreateInfo &info) {
Chris Forbes95292b12015-05-25 11:13:26 +1200593 VkPipeline pipe;
Jon Ashburnc669cc62015-07-09 15:02:25 -0600594 VkPipelineCache cache;
595 VkPipelineCacheCreateInfo ci;
Karl Schultz6addd812016-02-02 17:17:23 -0700596 memset((void *)&ci, 0, sizeof(VkPipelineCacheCreateInfo));
Jon Ashburnc669cc62015-07-09 15:02:25 -0600597 ci.sType = VK_STRUCTURE_TYPE_PIPELINE_CACHE_CREATE_INFO;
Chia-I Wuf7458c52015-10-26 21:10:41 +0800598 VkResult err = vkCreatePipelineCache(dev.handle(), &ci, NULL, &cache);
Chia-I Wuf368b602015-07-03 10:41:20 +0800599 EXPECT(err == VK_SUCCESS);
Chris Forbes95292b12015-05-25 11:13:26 +1200600 if (err == VK_SUCCESS) {
Mark Lobodzinski722841d2016-09-07 16:34:56 -0600601 err = vkCreateGraphicsPipelines(dev.handle(), cache, 1, &info, NULL, &pipe);
Jon Ashburnc669cc62015-07-09 15:02:25 -0600602 if (err == VK_SUCCESS) {
Chia-I Wu2ff72fd2015-07-03 11:49:42 +0800603 NonDispHandle::init(dev.handle(), pipe);
Jon Ashburnc669cc62015-07-09 15:02:25 -0600604 }
Chia-I Wuf7458c52015-10-26 21:10:41 +0800605 vkDestroyPipelineCache(dev.handle(), cache, NULL);
Chris Forbes95292b12015-05-25 11:13:26 +1200606 }
607
608 return err;
609}
610
Mark Lobodzinski722841d2016-09-07 16:34:56 -0600611void Pipeline::init(const Device &dev, const VkComputePipelineCreateInfo &info) {
Jon Ashburnc669cc62015-07-09 15:02:25 -0600612 VkPipelineCache cache;
613 VkPipelineCacheCreateInfo ci;
Karl Schultz6addd812016-02-02 17:17:23 -0700614 memset((void *)&ci, 0, sizeof(VkPipelineCacheCreateInfo));
Jon Ashburnc669cc62015-07-09 15:02:25 -0600615 ci.sType = VK_STRUCTURE_TYPE_PIPELINE_CACHE_CREATE_INFO;
Chia-I Wuf7458c52015-10-26 21:10:41 +0800616 VkResult err = vkCreatePipelineCache(dev.handle(), &ci, NULL, &cache);
Jon Ashburnc669cc62015-07-09 15:02:25 -0600617 if (err == VK_SUCCESS) {
Mark Lobodzinski722841d2016-09-07 16:34:56 -0600618 NON_DISPATCHABLE_HANDLE_INIT(vkCreateComputePipelines, dev, cache, 1, &info);
Chia-I Wuf7458c52015-10-26 21:10:41 +0800619 vkDestroyPipelineCache(dev.handle(), cache, NULL);
Jon Ashburnc669cc62015-07-09 15:02:25 -0600620 }
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800621}
622
Tony Barbour67e99152015-07-10 14:10:27 -0600623NON_DISPATCHABLE_HANDLE_DTOR(PipelineLayout, vkDestroyPipelineLayout)
Chia-I Wufd46e7d2015-07-03 11:49:42 +0800624
Mark Lobodzinski722841d2016-09-07 16:34:56 -0600625void 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 Barbour482c6092015-07-27 09:37:48 -0600628 info.pSetLayouts = layout_handles.data();
Chia-I Wufd46e7d2015-07-03 11:49:42 +0800629
630 NON_DISPATCHABLE_HANDLE_INIT(vkCreatePipelineLayout, dev, &info);
631}
632
Tony Barbour67e99152015-07-10 14:10:27 -0600633NON_DISPATCHABLE_HANDLE_DTOR(Sampler, vkDestroySampler)
Chia-I Wu8c721c62015-07-03 11:49:42 +0800634
Karl Schultz6addd812016-02-02 17:17:23 -0700635void Sampler::init(const Device &dev, const VkSamplerCreateInfo &info) {
Chia-I Wu8c721c62015-07-03 11:49:42 +0800636 NON_DISPATCHABLE_HANDLE_INIT(vkCreateSampler, dev, &info);
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800637}
638
Tony Barbour67e99152015-07-10 14:10:27 -0600639NON_DISPATCHABLE_HANDLE_DTOR(DescriptorSetLayout, vkDestroyDescriptorSetLayout)
Chia-I Wuafdfd7f2015-07-03 11:49:42 +0800640
Mark Lobodzinski722841d2016-09-07 16:34:56 -0600641void DescriptorSetLayout::init(const Device &dev, const VkDescriptorSetLayoutCreateInfo &info) {
Chia-I Wuafdfd7f2015-07-03 11:49:42 +0800642 NON_DISPATCHABLE_HANDLE_INIT(vkCreateDescriptorSetLayout, dev, &info);
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800643}
644
Tony Barbour67e99152015-07-10 14:10:27 -0600645NON_DISPATCHABLE_HANDLE_DTOR(DescriptorPool, vkDestroyDescriptorPool)
Chia-I Wuafdfd7f2015-07-03 11:49:42 +0800646
Mark Lobodzinski722841d2016-09-07 16:34:56 -0600647void DescriptorPool::init(const Device &dev, const VkDescriptorPoolCreateInfo &info) {
648 setDynamicUsage(info.flags & VK_DESCRIPTOR_POOL_CREATE_FREE_DESCRIPTOR_SET_BIT);
Courtney Goeltzenleuchterfe908d32015-09-16 16:12:45 -0600649 NON_DISPATCHABLE_HANDLE_INIT(vkCreateDescriptorPool, dev, &info);
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800650}
651
Mark Lobodzinski722841d2016-09-07 16:34:56 -0600652void DescriptorPool::reset() { EXPECT(vkResetDescriptorPool(device(), handle(), 0) == VK_SUCCESS); }
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800653
Mark Lobodzinski722841d2016-09-07 16:34:56 -0600654std::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 Wu11078b02015-01-04 16:27:24 +0800657
Chia-I Wuafdfd7f2015-07-03 11:49:42 +0800658 std::vector<VkDescriptorSet> set_handles;
659 set_handles.resize(layout_handles.size());
Chia-I Wu11078b02015-01-04 16:27:24 +0800660
Chia-I Wu3432a0c2015-10-27 18:04:07 +0800661 VkDescriptorSetAllocateInfo alloc_info = {};
Chia-I Wu00ce5402015-11-10 16:21:09 +0800662 alloc_info.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO;
Jon Ashburnf19916e2016-01-11 13:12:43 -0700663 alloc_info.descriptorSetCount = layout_handles.size();
Courtney Goeltzenleuchterbee18a92015-10-23 14:21:05 -0600664 alloc_info.descriptorPool = handle();
665 alloc_info.pSetLayouts = layout_handles.data();
Mark Lobodzinski722841d2016-09-07 16:34:56 -0600666 VkResult err = vkAllocateDescriptorSets(device(), &alloc_info, set_handles.data());
Cody Northrop1e4f8022015-08-03 12:47:29 -0600667 EXPECT(err == VK_SUCCESS);
Chia-I Wu11078b02015-01-04 16:27:24 +0800668
669 std::vector<DescriptorSet *> sets;
Mark Lobodzinski722841d2016-09-07 16:34:56 -0600670 for (std::vector<VkDescriptorSet>::const_iterator it = set_handles.begin(); it != set_handles.end(); it++) {
Chia-I Wu11078b02015-01-04 16:27:24 +0800671 // do descriptor sets need memories bound?
Cody Northropcdc72a42015-10-08 11:39:25 -0600672 DescriptorSet *descriptorSet = new DescriptorSet(dev, this, *it);
Mark Lobodzinski40f7f402015-04-16 11:44:05 -0500673 sets.push_back(descriptorSet);
Chia-I Wu11078b02015-01-04 16:27:24 +0800674 }
Chia-I Wu11078b02015-01-04 16:27:24 +0800675 return sets;
676}
677
Mark Lobodzinski722841d2016-09-07 16:34:56 -0600678std::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 Wu11078b02015-01-04 16:27:24 +0800680}
681
Mark Lobodzinski722841d2016-09-07 16:34:56 -0600682DescriptorSet *DescriptorPool::alloc_sets(const Device &dev, const DescriptorSetLayout &layout) {
Courtney Goeltzenleuchterbee18a92015-10-23 14:21:05 -0600683 std::vector<DescriptorSet *> set = alloc_sets(dev, layout, 1);
Chia-I Wu11078b02015-01-04 16:27:24 +0800684 return (set.empty()) ? NULL : set[0];
685}
686
Karl Schultz6addd812016-02-02 17:17:23 -0700687DescriptorSet::~DescriptorSet() {
Tony Barbour67e99152015-07-10 14:10:27 -0600688 if (initialized()) {
Cody Northropcdc72a42015-10-08 11:39:25 -0600689 // Only call vkFree* on sets allocated from pool with usage *_DYNAMIC
690 if (containing_pool_->getDynamicUsage()) {
Karl Schultz6addd812016-02-02 17:17:23 -0700691 VkDescriptorSet sets[1] = {handle()};
Mark Lobodzinski722841d2016-09-07 16:34:56 -0600692 EXPECT(vkFreeDescriptorSets(device(), containing_pool_->GetObj(), 1, sets) == VK_SUCCESS);
Cody Northropcdc72a42015-10-08 11:39:25 -0600693 }
Tony Barbour67e99152015-07-10 14:10:27 -0600694 }
695}
Chia-I Wuafdfd7f2015-07-03 11:49:42 +0800696
Chia-I Wu3432a0c2015-10-27 18:04:07 +0800697NON_DISPATCHABLE_HANDLE_DTOR(CommandPool, vkDestroyCommandPool)
Courtney Goeltzenleuchteree5d80b2015-07-10 19:50:17 -0600698
Karl Schultz6addd812016-02-02 17:17:23 -0700699void CommandPool::init(const Device &dev, const VkCommandPoolCreateInfo &info) {
Courtney Goeltzenleuchteree5d80b2015-07-10 19:50:17 -0600700 NON_DISPATCHABLE_HANDLE_INIT(vkCreateCommandPool, dev, &info);
701}
702
Karl Schultz6addd812016-02-02 17:17:23 -0700703CommandBuffer::~CommandBuffer() {
Courtney Goeltzenleuchterbee18a92015-10-23 14:21:05 -0600704 if (initialized()) {
Karl Schultz6addd812016-02-02 17:17:23 -0700705 VkCommandBuffer cmds[] = {handle()};
Courtney Goeltzenleuchterbee18a92015-10-23 14:21:05 -0600706 vkFreeCommandBuffers(dev_handle_, cmd_pool_, 1, cmds);
707 }
Chia-I Wube2b9172015-07-03 11:49:42 +0800708}
709
Mark Lobodzinski722841d2016-09-07 16:34:56 -0600710void CommandBuffer::init(const Device &dev, const VkCommandBufferAllocateInfo &info) {
Chia-I Wu3432a0c2015-10-27 18:04:07 +0800711 VkCommandBuffer cmd;
Chia-I Wube2b9172015-07-03 11:49:42 +0800712
Chia-I Wu3432a0c2015-10-27 18:04:07 +0800713 // Make sure commandPool is set
714 assert(info.commandPool);
Courtney Goeltzenleuchterd8e68bb2015-07-13 12:53:32 -0600715
Mark Lobodzinski722841d2016-09-07 16:34:56 -0600716 if (EXPECT(vkAllocateCommandBuffers(dev.handle(), &info, &cmd) == VK_SUCCESS)) {
Chia-I Wube2b9172015-07-03 11:49:42 +0800717 Handle::init(cmd);
718 dev_handle_ = dev.handle();
Chia-I Wu3432a0c2015-10-27 18:04:07 +0800719 cmd_pool_ = info.commandPool;
Chia-I Wube2b9172015-07-03 11:49:42 +0800720 }
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800721}
722
Mark Lobodzinski722841d2016-09-07 16:34:56 -0600723void CommandBuffer::begin(const VkCommandBufferBeginInfo *info) { EXPECT(vkBeginCommandBuffer(handle(), info) == VK_SUCCESS); }
Jeremy Hayesd65ae082015-01-14 16:17:08 -0700724
Karl Schultz6addd812016-02-02 17:17:23 -0700725void CommandBuffer::begin() {
Chia-I Wu3432a0c2015-10-27 18:04:07 +0800726 VkCommandBufferBeginInfo info = {};
Jon Ashburnf19916e2016-01-11 13:12:43 -0700727 VkCommandBufferInheritanceInfo hinfo = {};
Chia-I Wu3432a0c2015-10-27 18:04:07 +0800728 info.flags = VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT;
729 info.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
Jon Ashburnf19916e2016-01-11 13:12:43 -0700730 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 Hayesd65ae082015-01-14 16:17:08 -0700739
740 begin(&info);
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800741}
742
Mark Lobodzinski722841d2016-09-07 16:34:56 -0600743void CommandBuffer::end() { EXPECT(vkEndCommandBuffer(handle()) == VK_SUCCESS); }
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800744
Mark Lobodzinski722841d2016-09-07 16:34:56 -0600745void CommandBuffer::reset(VkCommandBufferResetFlags flags) { EXPECT(vkResetCommandBuffer(handle(), flags) == VK_SUCCESS); }
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800746
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700747}; // namespace vk_testing