blob: b7a685923198c0ae7704ccb56addbe822e4528f1 [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"
Petr Krausb9659a02017-12-11 01:17:46 +010023#include <algorithm>
Mark Lobodzinski722841d2016-09-07 16:34:56 -060024#include <assert.h>
25#include <iostream>
26#include <stdarg.h>
Mark Lobodzinski64318ba2017-01-26 13:34:13 -070027#include <string.h> // memset(), memcmp()
Chia-I Wuf1e2e992014-12-27 14:12:52 +080028
29namespace {
Chia-I Wuf1e2e992014-12-27 14:12:52 +080030
Mark Lobodzinski64318ba2017-01-26 13:34:13 -070031#define NON_DISPATCHABLE_HANDLE_INIT(create_func, dev, ...) \
32 do { \
33 handle_type handle; \
34 if (EXPECT(create_func(dev.handle(), __VA_ARGS__, NULL, &handle) == VK_SUCCESS)) \
35 NonDispHandle::init(dev.handle(), handle); \
Chia-I Wuf8f074f2015-07-03 10:58:57 +080036 } while (0)
37
Mark Lobodzinski64318ba2017-01-26 13:34:13 -070038#define NON_DISPATCHABLE_HANDLE_DTOR(cls, destroy_func) \
39 cls::~cls() { \
40 if (initialized()) destroy_func(device(), handle(), NULL); \
Chia-I Wud9e8e822015-07-03 11:45:55 +080041 }
42
Chia-I Wuf1e2e992014-12-27 14:12:52 +080043#define STRINGIFY(x) #x
Mark Lobodzinski722841d2016-09-07 16:34:56 -060044#define EXPECT(expr) ((expr) ? true : expect_failure(STRINGIFY(expr), __FILE__, __LINE__, __FUNCTION__))
Chia-I Wuf1e2e992014-12-27 14:12:52 +080045
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -060046vk_testing::ErrorCallback error_callback;
Chia-I Wuf1e2e992014-12-27 14:12:52 +080047
Mark Lobodzinski722841d2016-09-07 16:34:56 -060048bool expect_failure(const char *expr, const char *file, unsigned int line, const char *function) {
Chia-I Wuf1e2e992014-12-27 14:12:52 +080049 if (error_callback) {
50 error_callback(expr, file, line, function);
51 } else {
Mark Lobodzinski722841d2016-09-07 16:34:56 -060052 std::cerr << file << ":" << line << ": " << function << ": Expectation `" << expr << "' failed.\n";
Chia-I Wuf1e2e992014-12-27 14:12:52 +080053 }
54
55 return false;
56}
57
Mark Lobodzinski64318ba2017-01-26 13:34:13 -070058template <class T, class S>
59std::vector<T> make_handles(const std::vector<S> &v) {
Chia-I Wud9e8e822015-07-03 11:45:55 +080060 std::vector<T> handles;
61 handles.reserve(v.size());
Mark Lobodzinski64318ba2017-01-26 13:34:13 -070062 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 +080063 return handles;
64}
65
Mark Lobodzinski64318ba2017-01-26 13:34:13 -070066} // namespace
Chia-I Wuf1e2e992014-12-27 14:12:52 +080067
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -060068namespace vk_testing {
Chia-I Wuf1e2e992014-12-27 14:12:52 +080069
Karl Schultz6addd812016-02-02 17:17:23 -070070void set_error_callback(ErrorCallback callback) { error_callback = callback; }
Chia-I Wuf1e2e992014-12-27 14:12:52 +080071
Karl Schultz6addd812016-02-02 17:17:23 -070072VkPhysicalDeviceProperties PhysicalDevice::properties() const {
Tony Barbour59a47322015-06-24 16:06:58 -060073 VkPhysicalDeviceProperties info;
74
Courtney Goeltzenleuchter06d89472015-10-20 16:40:38 -060075 vkGetPhysicalDeviceProperties(handle(), &info);
Tony Barbour59a47322015-06-24 16:06:58 -060076
77 return info;
Chia-I Wuf1e2e992014-12-27 14:12:52 +080078}
79
Karl Schultz6addd812016-02-02 17:17:23 -070080std::vector<VkQueueFamilyProperties> PhysicalDevice::queue_properties() const {
Cody Northropd0802882015-08-03 17:04:53 -060081 std::vector<VkQueueFamilyProperties> info;
Tony Barbour59a47322015-06-24 16:06:58 -060082 uint32_t count;
83
Cody Northropd0802882015-08-03 17:04:53 -060084 // Call once with NULL data to receive count
Courtney Goeltzenleuchter06d89472015-10-20 16:40:38 -060085 vkGetPhysicalDeviceQueueFamilyProperties(handle(), &count, NULL);
86 info.resize(count);
87 vkGetPhysicalDeviceQueueFamilyProperties(handle(), &count, info.data());
Tony Barbour59a47322015-06-24 16:06:58 -060088
89 return info;
Chia-I Wuf1e2e992014-12-27 14:12:52 +080090}
91
Karl Schultz6addd812016-02-02 17:17:23 -070092VkPhysicalDeviceMemoryProperties PhysicalDevice::memory_properties() const {
Tony Barbour59a47322015-06-24 16:06:58 -060093 VkPhysicalDeviceMemoryProperties info;
94
Courtney Goeltzenleuchter06d89472015-10-20 16:40:38 -060095 vkGetPhysicalDeviceMemoryProperties(handle(), &info);
Mark Lobodzinskib3fbcd92015-07-02 16:49:40 -060096
Tony Barbour59a47322015-06-24 16:06:58 -060097 return info;
Chia-I Wuf1e2e992014-12-27 14:12:52 +080098}
99
Chris Forbesf9cfe182016-04-04 17:22:42 +1200100VkPhysicalDeviceFeatures PhysicalDevice::features() const {
101 VkPhysicalDeviceFeatures features;
102 vkGetPhysicalDeviceFeatures(handle(), &features);
103 return features;
104}
105
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -0600106/*
107 * Return list of Global layers available
108 */
Karl Schultz6addd812016-02-02 17:17:23 -0700109std::vector<VkLayerProperties> GetGlobalLayers() {
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -0600110 VkResult err;
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -0600111 std::vector<VkLayerProperties> layers;
Courtney Goeltzenleuchtercd69eee2015-07-06 09:10:47 -0600112 uint32_t layer_count;
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -0600113
Courtney Goeltzenleuchtercd69eee2015-07-06 09:10:47 -0600114 do {
115 layer_count = 0;
Courtney Goeltzenleuchter35985f62015-09-14 17:22:16 -0600116 err = vkEnumerateInstanceLayerProperties(&layer_count, NULL);
Courtney Goeltzenleuchtercd69eee2015-07-06 09:10:47 -0600117
118 if (err == VK_SUCCESS) {
119 layers.reserve(layer_count);
Mark Lobodzinski722841d2016-09-07 16:34:56 -0600120 err = vkEnumerateInstanceLayerProperties(&layer_count, layers.data());
Courtney Goeltzenleuchtercd69eee2015-07-06 09:10:47 -0600121 }
122 } while (err == VK_INCOMPLETE);
123
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -0600124 assert(err == VK_SUCCESS);
125
126 return layers;
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800127}
128
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -0600129/*
130 * Return list of Global extensions provided by the ICD / Loader
131 */
Mark Lobodzinski722841d2016-09-07 16:34:56 -0600132std::vector<VkExtensionProperties> GetGlobalExtensions() { return GetGlobalExtensions(NULL); }
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -0600133
134/*
135 * Return list of Global extensions provided by the specified layer
Karl Schultz6addd812016-02-02 17:17:23 -0700136 * If pLayerName is NULL, will return extensions implemented by the loader /
137 * ICDs
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -0600138 */
Karl Schultz6addd812016-02-02 17:17:23 -0700139std::vector<VkExtensionProperties> GetGlobalExtensions(const char *pLayerName) {
Courtney Goeltzenleuchtercd69eee2015-07-06 09:10:47 -0600140 std::vector<VkExtensionProperties> exts;
141 uint32_t ext_count;
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -0600142 VkResult err;
143
Courtney Goeltzenleuchtercd69eee2015-07-06 09:10:47 -0600144 do {
145 ext_count = 0;
Mark Lobodzinski722841d2016-09-07 16:34:56 -0600146 err = vkEnumerateInstanceExtensionProperties(pLayerName, &ext_count, NULL);
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -0600147
Courtney Goeltzenleuchtercd69eee2015-07-06 09:10:47 -0600148 if (err == VK_SUCCESS) {
Courtney Goeltzenleuchter381f3a22015-07-06 15:45:58 -0600149 exts.resize(ext_count);
Mark Lobodzinski722841d2016-09-07 16:34:56 -0600150 err = vkEnumerateInstanceExtensionProperties(pLayerName, &ext_count, exts.data());
Courtney Goeltzenleuchtercd69eee2015-07-06 09:10:47 -0600151 }
152 } while (err == VK_INCOMPLETE);
153
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -0600154 assert(err == VK_SUCCESS);
155
156 return exts;
157}
158
159/*
160 * Return list of PhysicalDevice extensions provided by the ICD / Loader
161 */
Mark Lobodzinski722841d2016-09-07 16:34:56 -0600162std::vector<VkExtensionProperties> PhysicalDevice::extensions() const { return extensions(NULL); }
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -0600163
164/*
165 * Return list of PhysicalDevice extensions provided by the specified layer
166 * If pLayerName is NULL, will return extensions for ICD / loader.
167 */
Mark Lobodzinski722841d2016-09-07 16:34:56 -0600168std::vector<VkExtensionProperties> PhysicalDevice::extensions(const char *pLayerName) const {
Courtney Goeltzenleuchtercd69eee2015-07-06 09:10:47 -0600169 std::vector<VkExtensionProperties> exts;
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -0600170 VkResult err;
171
Courtney Goeltzenleuchtercd69eee2015-07-06 09:10:47 -0600172 do {
173 uint32_t extCount = 0;
Mark Lobodzinski722841d2016-09-07 16:34:56 -0600174 err = vkEnumerateDeviceExtensionProperties(handle(), pLayerName, &extCount, NULL);
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -0600175
Courtney Goeltzenleuchtercd69eee2015-07-06 09:10:47 -0600176 if (err == VK_SUCCESS) {
Ian Elliott1a3845b2015-07-06 14:33:04 -0600177 exts.resize(extCount);
Mark Lobodzinski722841d2016-09-07 16:34:56 -0600178 err = vkEnumerateDeviceExtensionProperties(handle(), pLayerName, &extCount, exts.data());
Courtney Goeltzenleuchtercd69eee2015-07-06 09:10:47 -0600179 }
180 } while (err == VK_INCOMPLETE);
181
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -0600182 assert(err == VK_SUCCESS);
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800183
184 return exts;
185}
186
Mark Lobodzinski722841d2016-09-07 16:34:56 -0600187bool PhysicalDevice::set_memory_type(const uint32_t type_bits, VkMemoryAllocateInfo *info, const VkFlags properties,
Karl Schultz6addd812016-02-02 17:17:23 -0700188 const VkFlags forbid) const {
189 uint32_t type_mask = type_bits;
190 // Search memtypes to find first index with those properties
191 for (uint32_t i = 0; i < memory_properties_.memoryTypeCount; i++) {
192 if ((type_mask & 1) == 1) {
193 // Type is available, does it match user properties?
Mark Lobodzinski722841d2016-09-07 16:34:56 -0600194 if ((memory_properties_.memoryTypes[i].propertyFlags & properties) == properties &&
195 (memory_properties_.memoryTypes[i].propertyFlags & forbid) == 0) {
Karl Schultz6addd812016-02-02 17:17:23 -0700196 info->memoryTypeIndex = i;
197 return true;
198 }
199 }
200 type_mask >>= 1;
201 }
202 // No memory types matched, return failure
203 return false;
Mark Lobodzinskib3fbcd92015-07-02 16:49:40 -0600204}
205
Courtney Goeltzenleuchtercd69eee2015-07-06 09:10:47 -0600206/*
207 * Return list of PhysicalDevice layers
208 */
Karl Schultz6addd812016-02-02 17:17:23 -0700209std::vector<VkLayerProperties> PhysicalDevice::layers() const {
Courtney Goeltzenleuchtercd69eee2015-07-06 09:10:47 -0600210 std::vector<VkLayerProperties> layer_props;
211 VkResult err;
212
213 do {
214 uint32_t layer_count = 0;
Courtney Goeltzenleuchter35985f62015-09-14 17:22:16 -0600215 err = vkEnumerateDeviceLayerProperties(handle(), &layer_count, NULL);
Courtney Goeltzenleuchtercd69eee2015-07-06 09:10:47 -0600216
217 if (err == VK_SUCCESS) {
218 layer_props.reserve(layer_count);
Mark Lobodzinski722841d2016-09-07 16:34:56 -0600219 err = vkEnumerateDeviceLayerProperties(handle(), &layer_count, layer_props.data());
Courtney Goeltzenleuchtercd69eee2015-07-06 09:10:47 -0600220 }
221 } while (err == VK_INCOMPLETE);
222
223 assert(err == VK_SUCCESS);
224
225 return layer_props;
226}
227
John Zulauf01da3ee2017-10-18 18:13:37 -0600228QueueCreateInfoArray::QueueCreateInfoArray(const std::vector<VkQueueFamilyProperties> &queue_props) : queue_info_(), queue_priorities_() {
229 queue_info_.reserve(queue_props.size());
230
Petr Kraus540a67d2017-12-11 00:35:33 +0100231 for (uint32_t i = 0; i < (uint32_t)queue_props.size(); ++i) {
232 if (queue_props[i].queueCount > 0) {
233 VkDeviceQueueCreateInfo qi = {};
234 qi.sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO;
235 qi.pNext = NULL;
236 qi.queueFamilyIndex = i;
237 qi.queueCount = queue_props[i].queueCount;
238 queue_priorities_.emplace_back(qi.queueCount, 0.0f);
239 qi.pQueuePriorities = queue_priorities_[i].data();
240 queue_info_.push_back(qi);
241 }
John Zulauf01da3ee2017-10-18 18:13:37 -0600242 }
243}
244
Karl Schultz6addd812016-02-02 17:17:23 -0700245Device::~Device() {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700246 if (!initialized()) return;
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800247
248 for (int i = 0; i < QUEUE_COUNT; i++) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700249 for (std::vector<Queue *>::iterator it = queues_[i].begin(); it != queues_[i].end(); it++) delete *it;
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800250 queues_[i].clear();
251 }
252
Chia-I Wuf7458c52015-10-26 21:10:41 +0800253 vkDestroyDevice(handle(), NULL);
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800254}
255
Tony Barbour53f7e892016-08-09 13:44:00 -0600256void Device::init(std::vector<const char *> &extensions, VkPhysicalDeviceFeatures *features) {
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800257 // request all queues
Mark Lobodzinski722841d2016-09-07 16:34:56 -0600258 const std::vector<VkQueueFamilyProperties> queue_props = phy_.queue_properties();
John Zulauf01da3ee2017-10-18 18:13:37 -0600259 QueueCreateInfoArray queue_info(phy_.queue_properties());
Mark Young93ecb1d2016-01-13 13:47:16 -0700260 for (uint32_t i = 0; i < (uint32_t)queue_props.size(); i++) {
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600261 if (queue_props[i].queueFlags & VK_QUEUE_GRAPHICS_BIT) {
Courtney Goeltzenleuchter18248e62015-03-05 18:09:39 -0700262 graphics_queue_node_index_ = i;
263 }
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800264 }
Tobin Ehlis04dea912017-11-14 12:10:11 -0700265 // Only request creation with queuefamilies that have at least one queue
266 std::vector<VkDeviceQueueCreateInfo> create_queue_infos;
267 auto qci = queue_info.data();
268 for (uint32_t j = 0; j < queue_info.size(); ++j) {
269 if (qci[j].queueCount) {
270 create_queue_infos.push_back(qci[j]);
271 }
272 }
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800273
Petr Krausb9659a02017-12-11 01:17:46 +0100274 enabled_extensions_ = extensions;
275
Courtney Goeltzenleuchter95487bc2015-04-14 18:48:46 -0600276 VkDeviceCreateInfo dev_info = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600277 dev_info.sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO;
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600278 dev_info.pNext = NULL;
Tobin Ehlis04dea912017-11-14 12:10:11 -0700279 dev_info.queueCreateInfoCount = create_queue_infos.size();
280 dev_info.pQueueCreateInfos = create_queue_infos.data();
Tony Barbour4c70d102016-08-08 16:06:56 -0600281 dev_info.enabledLayerCount = 0;
282 dev_info.ppEnabledLayerNames = NULL;
Jon Ashburnf19916e2016-01-11 13:12:43 -0700283 dev_info.enabledExtensionCount = extensions.size();
Tony Barbour482c6092015-07-27 09:37:48 -0600284 dev_info.ppEnabledExtensionNames = extensions.data();
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800285
Tony Barbour53f7e892016-08-09 13:44:00 -0600286 VkPhysicalDeviceFeatures all_features;
287 if (features) {
288 dev_info.pEnabledFeatures = features;
289 } else {
290 // request all supportable features enabled
291 all_features = phy().features();
292 dev_info.pEnabledFeatures = &all_features;
293 }
Chris Forbesf9cfe182016-04-04 17:22:42 +1200294
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800295 init(dev_info);
296}
297
Karl Schultz6addd812016-02-02 17:17:23 -0700298void Device::init(const VkDeviceCreateInfo &info) {
Chia-I Wuf368b602015-07-03 10:41:20 +0800299 VkDevice dev;
300
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700301 if (EXPECT(vkCreateDevice(phy_.handle(), &info, NULL, &dev) == VK_SUCCESS)) Handle::init(dev);
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800302
303 init_queues();
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800304 init_formats();
305}
306
Karl Schultz6addd812016-02-02 17:17:23 -0700307void Device::init_queues() {
Courtney Goeltzenleuchter18248e62015-03-05 18:09:39 -0700308 uint32_t queue_node_count;
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800309
Cody Northropd0802882015-08-03 17:04:53 -0600310 // Call with NULL data to get count
Mark Lobodzinski722841d2016-09-07 16:34:56 -0600311 vkGetPhysicalDeviceQueueFamilyProperties(phy_.handle(), &queue_node_count, NULL);
Courtney Goeltzenleuchter18248e62015-03-05 18:09:39 -0700312 EXPECT(queue_node_count >= 1);
313
Mark Lobodzinski722841d2016-09-07 16:34:56 -0600314 VkQueueFamilyProperties *queue_props = new VkQueueFamilyProperties[queue_node_count];
Courtney Goeltzenleuchter18248e62015-03-05 18:09:39 -0700315
Mark Lobodzinski722841d2016-09-07 16:34:56 -0600316 vkGetPhysicalDeviceQueueFamilyProperties(phy_.handle(), &queue_node_count, queue_props);
Courtney Goeltzenleuchter18248e62015-03-05 18:09:39 -0700317
Tony Barbour7ea6aa22015-05-22 09:44:58 -0600318 for (uint32_t i = 0; i < queue_node_count; i++) {
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600319 VkQueue queue;
Courtney Goeltzenleuchter18248e62015-03-05 18:09:39 -0700320
Tony Barbour7ea6aa22015-05-22 09:44:58 -0600321 for (uint32_t j = 0; j < queue_props[i].queueCount; j++) {
Karl Schultz6addd812016-02-02 17:17:23 -0700322 // TODO: Need to add support for separate MEMMGR and work queues,
323 // including synchronization
Courtney Goeltzenleuchter06d89472015-10-20 16:40:38 -0600324 vkGetDeviceQueue(handle(), i, j, &queue);
Courtney Goeltzenleuchter18248e62015-03-05 18:09:39 -0700325
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600326 if (queue_props[i].queueFlags & VK_QUEUE_GRAPHICS_BIT) {
Tony Barbourfb21ea32015-07-23 10:35:30 -0600327 queues_[GRAPHICS].push_back(new Queue(queue, i));
Courtney Goeltzenleuchter18248e62015-03-05 18:09:39 -0700328 }
329
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600330 if (queue_props[i].queueFlags & VK_QUEUE_COMPUTE_BIT) {
Tony Barbourfb21ea32015-07-23 10:35:30 -0600331 queues_[COMPUTE].push_back(new Queue(queue, i));
Courtney Goeltzenleuchter18248e62015-03-05 18:09:39 -0700332 }
333
Chia-I Wu1b99bb22015-10-27 19:25:11 +0800334 if (queue_props[i].queueFlags & VK_QUEUE_TRANSFER_BIT) {
Tony Barbourfb21ea32015-07-23 10:35:30 -0600335 queues_[DMA].push_back(new Queue(queue, i));
Courtney Goeltzenleuchter18248e62015-03-05 18:09:39 -0700336 }
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800337 }
338 }
339
Chris Forbes02038792015-06-04 10:49:27 +1200340 delete[] queue_props;
Tony Barbour3d69c9e2015-05-20 16:53:31 -0600341
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800342 EXPECT(!queues_[GRAPHICS].empty() || !queues_[COMPUTE].empty());
343}
344
Karl Schultz6addd812016-02-02 17:17:23 -0700345void Device::init_formats() {
Tony Barbourd1c35722015-04-16 15:59:00 -0600346 for (int f = VK_FORMAT_BEGIN_RANGE; f <= VK_FORMAT_END_RANGE; f++) {
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600347 const VkFormat fmt = static_cast<VkFormat>(f);
348 const VkFormatProperties props = format_properties(fmt);
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800349
Jeremy Hayesa058eee2015-01-23 08:51:43 -0700350 if (props.linearTilingFeatures) {
Mark Lobodzinski722841d2016-09-07 16:34:56 -0600351 const Format tmp = {fmt, VK_IMAGE_TILING_LINEAR, props.linearTilingFeatures};
Jeremy Hayesa058eee2015-01-23 08:51:43 -0700352 formats_.push_back(tmp);
353 }
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800354
Jeremy Hayesa058eee2015-01-23 08:51:43 -0700355 if (props.optimalTilingFeatures) {
Mark Lobodzinski722841d2016-09-07 16:34:56 -0600356 const Format tmp = {fmt, VK_IMAGE_TILING_OPTIMAL, props.optimalTilingFeatures};
Jeremy Hayesa058eee2015-01-23 08:51:43 -0700357 formats_.push_back(tmp);
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800358 }
359 }
360
361 EXPECT(!formats_.empty());
362}
363
Petr Krausb9659a02017-12-11 01:17:46 +0100364bool Device::IsEnbledExtension(const char *extension) {
365 const auto is_x = [&extension](const char *enabled_extension) { return strcmp(extension, enabled_extension) == 0; };
366 return std::any_of(enabled_extensions_.begin(), enabled_extensions_.end(), is_x);
367}
368
Karl Schultz6addd812016-02-02 17:17:23 -0700369VkFormatProperties Device::format_properties(VkFormat format) {
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600370 VkFormatProperties data;
Courtney Goeltzenleuchter06d89472015-10-20 16:40:38 -0600371 vkGetPhysicalDeviceFormatProperties(phy().handle(), format, &data);
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800372
373 return data;
374}
375
Karl Schultz6addd812016-02-02 17:17:23 -0700376void Device::wait() { EXPECT(vkDeviceWaitIdle(handle()) == VK_SUCCESS); }
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800377
Mark Lobodzinski722841d2016-09-07 16:34:56 -0600378VkResult Device::wait(const std::vector<const Fence *> &fences, bool wait_all, uint64_t timeout) {
Chia-I Wud9e8e822015-07-03 11:45:55 +0800379 const std::vector<VkFence> fence_handles = make_handles<VkFence>(fences);
Mark Lobodzinski722841d2016-09-07 16:34:56 -0600380 VkResult err = vkWaitForFences(handle(), fence_handles.size(), fence_handles.data(), wait_all, timeout);
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600381 EXPECT(err == VK_SUCCESS || err == VK_TIMEOUT);
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800382
383 return err;
384}
385
Mark Lobodzinski722841d2016-09-07 16:34:56 -0600386void Device::update_descriptor_sets(const std::vector<VkWriteDescriptorSet> &writes,
387 const std::vector<VkCopyDescriptorSet> &copies) {
388 vkUpdateDescriptorSets(handle(), writes.size(), writes.data(), copies.size(), copies.data());
Chia-I Wu9d00ed72015-05-25 16:27:55 +0800389}
390
Mark Lobodzinski722841d2016-09-07 16:34:56 -0600391void Queue::submit(const std::vector<const CommandBuffer *> &cmds, Fence &fence) {
392 const std::vector<VkCommandBuffer> cmd_handles = make_handles<VkCommandBuffer>(cmds);
Courtney Goeltzenleuchter806c7002015-10-27 11:22:14 -0600393 VkSubmitInfo submit_info;
Chia-I Wuf9be13c2015-10-26 20:37:06 +0800394 submit_info.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
395 submit_info.pNext = NULL;
Chia-I Wud50a7d72015-10-26 20:48:51 +0800396 submit_info.waitSemaphoreCount = 0;
Courtney Goeltzenleuchter806c7002015-10-27 11:22:14 -0600397 submit_info.pWaitSemaphores = NULL;
Jon Ashburn7f9716c2015-12-30 16:42:50 -0700398 submit_info.pWaitDstStageMask = NULL;
Chia-I Wud50a7d72015-10-26 20:48:51 +0800399 submit_info.commandBufferCount = (uint32_t)cmd_handles.size();
Courtney Goeltzenleuchter806c7002015-10-27 11:22:14 -0600400 submit_info.pCommandBuffers = cmd_handles.data();
Chia-I Wud50a7d72015-10-26 20:48:51 +0800401 submit_info.signalSemaphoreCount = 0;
Courtney Goeltzenleuchter806c7002015-10-27 11:22:14 -0600402 submit_info.pSignalSemaphores = NULL;
Courtney Goeltzenleuchter646b9072015-10-20 18:04:07 -0600403
Mark Lobodzinski722841d2016-09-07 16:34:56 -0600404 EXPECT(vkQueueSubmit(handle(), 1, &submit_info, fence.handle()) == VK_SUCCESS);
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800405}
406
Mark Lobodzinski722841d2016-09-07 16:34:56 -0600407void Queue::submit(const CommandBuffer &cmd, Fence &fence) { submit(std::vector<const CommandBuffer *>(1, &cmd), fence); }
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800408
Karl Schultz6addd812016-02-02 17:17:23 -0700409void Queue::submit(const CommandBuffer &cmd) {
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800410 Fence fence;
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600411 submit(cmd, fence);
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800412}
413
Karl Schultz6addd812016-02-02 17:17:23 -0700414void Queue::wait() { EXPECT(vkQueueWaitIdle(handle()) == VK_SUCCESS); }
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800415
Karl Schultz6addd812016-02-02 17:17:23 -0700416DeviceMemory::~DeviceMemory() {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700417 if (initialized()) vkFreeMemory(device(), handle(), NULL);
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800418}
419
Karl Schultz6addd812016-02-02 17:17:23 -0700420void DeviceMemory::init(const Device &dev, const VkMemoryAllocateInfo &info) {
Chia-I Wu3432a0c2015-10-27 18:04:07 +0800421 NON_DISPATCHABLE_HANDLE_INIT(vkAllocateMemory, dev, &info);
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800422}
423
Karl Schultz6addd812016-02-02 17:17:23 -0700424const void *DeviceMemory::map(VkFlags flags) const {
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800425 void *data;
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700426 if (!EXPECT(vkMapMemory(device(), handle(), 0, VK_WHOLE_SIZE, flags, &data) == VK_SUCCESS)) data = NULL;
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800427
428 return data;
429}
430
Karl Schultz6addd812016-02-02 17:17:23 -0700431void *DeviceMemory::map(VkFlags flags) {
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800432 void *data;
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700433 if (!EXPECT(vkMapMemory(device(), handle(), 0, VK_WHOLE_SIZE, flags, &data) == VK_SUCCESS)) data = NULL;
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800434
435 return data;
436}
437
Karl Schultz6addd812016-02-02 17:17:23 -0700438void DeviceMemory::unmap() const { vkUnmapMemory(device(), handle()); }
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800439
Mike Schuchardt8cacbb02017-10-26 14:06:38 -0600440VkMemoryAllocateInfo DeviceMemory::get_resource_alloc_info(const Device &dev, const VkMemoryRequirements &reqs,
441 VkMemoryPropertyFlags mem_props) {
Tobin Ehlisa3d60642017-11-14 10:02:46 -0700442 // Find appropriate memory type for given reqs
443 VkPhysicalDeviceMemoryProperties dev_mem_props = dev.phy().memory_properties();
444 uint32_t mem_type_index = 0;
445 for (mem_type_index = 0; mem_type_index < dev_mem_props.memoryTypeCount; ++mem_type_index) {
446 if (mem_props == (mem_props & dev_mem_props.memoryTypes[mem_type_index].propertyFlags)) break;
447 }
448 // If we exceeded types, then this device doesn't have the memory we need
449 assert(mem_type_index < dev_mem_props.memoryTypeCount);
450 VkMemoryAllocateInfo info = alloc_info(reqs.size, mem_type_index);
Mike Schuchardt7e567b32017-11-16 13:15:20 -0700451 EXPECT(dev.phy().set_memory_type(reqs.memoryTypeBits, &info, mem_props));
Mike Schuchardt8cacbb02017-10-26 14:06:38 -0600452 return info;
453}
454
Tony Barbour67e99152015-07-10 14:10:27 -0600455NON_DISPATCHABLE_HANDLE_DTOR(Fence, vkDestroyFence)
Chia-I Wud9e8e822015-07-03 11:45:55 +0800456
Mark Lobodzinski722841d2016-09-07 16:34:56 -0600457void Fence::init(const Device &dev, const VkFenceCreateInfo &info) { NON_DISPATCHABLE_HANDLE_INIT(vkCreateFence, dev, &info); }
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800458
Tony Barbour67e99152015-07-10 14:10:27 -0600459NON_DISPATCHABLE_HANDLE_DTOR(Semaphore, vkDestroySemaphore)
Chia-I Wu6b1c2482015-07-03 11:49:42 +0800460
Karl Schultz6addd812016-02-02 17:17:23 -0700461void Semaphore::init(const Device &dev, const VkSemaphoreCreateInfo &info) {
Chia-I Wu6b1c2482015-07-03 11:49:42 +0800462 NON_DISPATCHABLE_HANDLE_INIT(vkCreateSemaphore, dev, &info);
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800463}
464
Tony Barbour67e99152015-07-10 14:10:27 -0600465NON_DISPATCHABLE_HANDLE_DTOR(Event, vkDestroyEvent)
Chia-I Wuc5c97992015-07-03 11:49:42 +0800466
Mark Lobodzinski722841d2016-09-07 16:34:56 -0600467void Event::init(const Device &dev, const VkEventCreateInfo &info) { NON_DISPATCHABLE_HANDLE_INIT(vkCreateEvent, dev, &info); }
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800468
Karl Schultz6addd812016-02-02 17:17:23 -0700469void Event::set() { EXPECT(vkSetEvent(device(), handle()) == VK_SUCCESS); }
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800470
Karl Schultz6addd812016-02-02 17:17:23 -0700471void Event::reset() { EXPECT(vkResetEvent(device(), handle()) == VK_SUCCESS); }
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800472
Tony Barbour67e99152015-07-10 14:10:27 -0600473NON_DISPATCHABLE_HANDLE_DTOR(QueryPool, vkDestroyQueryPool)
Chia-I Wu1b7d4762015-07-03 11:49:42 +0800474
Karl Schultz6addd812016-02-02 17:17:23 -0700475void QueryPool::init(const Device &dev, const VkQueryPoolCreateInfo &info) {
Chia-I Wu1b7d4762015-07-03 11:49:42 +0800476 NON_DISPATCHABLE_HANDLE_INIT(vkCreateQueryPool, dev, &info);
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800477}
478
Mark Lobodzinski722841d2016-09-07 16:34:56 -0600479VkResult QueryPool::results(uint32_t first, uint32_t count, size_t size, void *data, size_t stride) {
480 VkResult err = vkGetQueryPoolResults(device(), handle(), first, count, size, data, stride, 0);
Chia-I Wuccc93a72015-10-26 18:36:20 +0800481 EXPECT(err == VK_SUCCESS || err == VK_NOT_READY);
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800482
483 return err;
484}
485
Tony Barbour67e99152015-07-10 14:10:27 -0600486NON_DISPATCHABLE_HANDLE_DTOR(Buffer, vkDestroyBuffer)
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800487
Mark Lobodzinski722841d2016-09-07 16:34:56 -0600488void Buffer::init(const Device &dev, const VkBufferCreateInfo &info, VkMemoryPropertyFlags mem_props) {
Tony Barbour4c97d7a2015-04-22 15:10:33 -0600489 init_no_mem(dev, info);
Chia-I Wu681d7a02015-07-03 13:44:34 +0800490
Mike Schuchardt8cacbb02017-10-26 14:06:38 -0600491 internal_mem_.init(dev, DeviceMemory::get_resource_alloc_info(dev, memory_requirements(), mem_props));
Chia-I Wu681d7a02015-07-03 13:44:34 +0800492 bind_memory(internal_mem_, 0);
Tony Barbour4c97d7a2015-04-22 15:10:33 -0600493}
494
Karl Schultz6addd812016-02-02 17:17:23 -0700495void Buffer::init_no_mem(const Device &dev, const VkBufferCreateInfo &info) {
Chia-I Wu681d7a02015-07-03 13:44:34 +0800496 NON_DISPATCHABLE_HANDLE_INIT(vkCreateBuffer, dev, &info);
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800497 create_info_ = info;
498}
499
Karl Schultz6addd812016-02-02 17:17:23 -0700500VkMemoryRequirements Buffer::memory_requirements() const {
Chia-I Wu681d7a02015-07-03 13:44:34 +0800501 VkMemoryRequirements reqs;
502
Courtney Goeltzenleuchter06d89472015-10-20 16:40:38 -0600503 vkGetBufferMemoryRequirements(device(), handle(), &reqs);
Chia-I Wu681d7a02015-07-03 13:44:34 +0800504
505 return reqs;
506}
507
Karl Schultz6addd812016-02-02 17:17:23 -0700508void Buffer::bind_memory(const DeviceMemory &mem, VkDeviceSize mem_offset) {
Mark Lobodzinski722841d2016-09-07 16:34:56 -0600509 EXPECT(vkBindBufferMemory(device(), handle(), mem.handle(), mem_offset) == VK_SUCCESS);
Mark Lobodzinski942b1722015-05-11 17:21:15 -0500510}
511
Tony Barbour67e99152015-07-10 14:10:27 -0600512NON_DISPATCHABLE_HANDLE_DTOR(BufferView, vkDestroyBufferView)
Chia-I Wu3158bf32015-07-03 11:49:42 +0800513
Karl Schultz6addd812016-02-02 17:17:23 -0700514void BufferView::init(const Device &dev, const VkBufferViewCreateInfo &info) {
Chia-I Wu3158bf32015-07-03 11:49:42 +0800515 NON_DISPATCHABLE_HANDLE_INIT(vkCreateBufferView, dev, &info);
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800516}
517
Tony Barbour67e99152015-07-10 14:10:27 -0600518NON_DISPATCHABLE_HANDLE_DTOR(Image, vkDestroyImage)
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800519
Mark Lobodzinski722841d2016-09-07 16:34:56 -0600520void Image::init(const Device &dev, const VkImageCreateInfo &info, VkMemoryPropertyFlags mem_props) {
Tony Barbour4c97d7a2015-04-22 15:10:33 -0600521 init_no_mem(dev, info);
Chia-I Wu681d7a02015-07-03 13:44:34 +0800522
Karl Schultzb5bc11e2016-05-04 08:36:08 -0600523 if (initialized()) {
Mike Schuchardt8cacbb02017-10-26 14:06:38 -0600524 internal_mem_.init(dev, DeviceMemory::get_resource_alloc_info(dev, memory_requirements(), mem_props));
Karl Schultzb5bc11e2016-05-04 08:36:08 -0600525 bind_memory(internal_mem_, 0);
526 }
Tony Barbour4c97d7a2015-04-22 15:10:33 -0600527}
528
Karl Schultz6addd812016-02-02 17:17:23 -0700529void Image::init_no_mem(const Device &dev, const VkImageCreateInfo &info) {
Chia-I Wu681d7a02015-07-03 13:44:34 +0800530 NON_DISPATCHABLE_HANDLE_INIT(vkCreateImage, dev, &info);
Karl Schultzb5bc11e2016-05-04 08:36:08 -0600531 if (initialized()) {
532 init_info(dev, info);
533 }
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800534}
535
Karl Schultz6addd812016-02-02 17:17:23 -0700536void Image::init_info(const Device &dev, const VkImageCreateInfo &info) {
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800537 create_info_ = info;
538
Mark Lobodzinski722841d2016-09-07 16:34:56 -0600539 for (std::vector<Device::Format>::const_iterator it = dev.formats().begin(); it != dev.formats().end(); it++) {
540 if (memcmp(&it->format, &create_info_.format, sizeof(it->format)) == 0 && it->tiling == create_info_.tiling) {
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800541 format_features_ = it->features;
542 break;
543 }
544 }
545}
546
Karl Schultz6addd812016-02-02 17:17:23 -0700547VkMemoryRequirements Image::memory_requirements() const {
Chia-I Wu681d7a02015-07-03 13:44:34 +0800548 VkMemoryRequirements reqs;
549
Courtney Goeltzenleuchter06d89472015-10-20 16:40:38 -0600550 vkGetImageMemoryRequirements(device(), handle(), &reqs);
Chia-I Wu681d7a02015-07-03 13:44:34 +0800551
552 return reqs;
553}
554
Karl Schultz6addd812016-02-02 17:17:23 -0700555void Image::bind_memory(const DeviceMemory &mem, VkDeviceSize mem_offset) {
Mark Lobodzinski722841d2016-09-07 16:34:56 -0600556 EXPECT(vkBindImageMemory(device(), handle(), mem.handle(), mem_offset) == VK_SUCCESS);
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800557}
558
Mark Lobodzinski722841d2016-09-07 16:34:56 -0600559VkSubresourceLayout Image::subresource_layout(const VkImageSubresource &subres) const {
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600560 VkSubresourceLayout data;
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800561 size_t size = sizeof(data);
Courtney Goeltzenleuchter06d89472015-10-20 16:40:38 -0600562 vkGetImageSubresourceLayout(device(), handle(), &subres, &data);
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700563 if (size != sizeof(data)) memset(&data, 0, sizeof(data));
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800564
565 return data;
566}
567
Mark Lobodzinski722841d2016-09-07 16:34:56 -0600568VkSubresourceLayout Image::subresource_layout(const VkImageSubresourceLayers &subrescopy) const {
Courtney Goeltzenleuchter01ee1ca2015-09-10 16:41:13 -0600569 VkSubresourceLayout data;
Mark Lobodzinski722841d2016-09-07 16:34:56 -0600570 VkImageSubresource subres = subresource(subrescopy.aspectMask, subrescopy.mipLevel, subrescopy.baseArrayLayer);
Courtney Goeltzenleuchter01ee1ca2015-09-10 16:41:13 -0600571 size_t size = sizeof(data);
Courtney Goeltzenleuchter06d89472015-10-20 16:40:38 -0600572 vkGetImageSubresourceLayout(device(), handle(), &subres, &data);
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700573 if (size != sizeof(data)) memset(&data, 0, sizeof(data));
Courtney Goeltzenleuchter01ee1ca2015-09-10 16:41:13 -0600574
575 return data;
576}
577
Karl Schultz6addd812016-02-02 17:17:23 -0700578bool Image::transparent() const {
Mark Lobodzinski722841d2016-09-07 16:34:56 -0600579 return (create_info_.tiling == VK_IMAGE_TILING_LINEAR && create_info_.samples == VK_SAMPLE_COUNT_1_BIT &&
580 !(create_info_.usage & (VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT | VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT)));
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800581}
582
Tony Barbour67e99152015-07-10 14:10:27 -0600583NON_DISPATCHABLE_HANDLE_DTOR(ImageView, vkDestroyImageView)
Chia-I Wu3158bf32015-07-03 11:49:42 +0800584
Karl Schultz6addd812016-02-02 17:17:23 -0700585void ImageView::init(const Device &dev, const VkImageViewCreateInfo &info) {
Chia-I Wu3158bf32015-07-03 11:49:42 +0800586 NON_DISPATCHABLE_HANDLE_INIT(vkCreateImageView, dev, &info);
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800587}
588
Tony Barbour67e99152015-07-10 14:10:27 -0600589NON_DISPATCHABLE_HANDLE_DTOR(ShaderModule, vkDestroyShaderModule)
Chia-I Wu4d0c7922015-07-03 11:49:42 +0800590
Mark Lobodzinski722841d2016-09-07 16:34:56 -0600591void ShaderModule::init(const Device &dev, const VkShaderModuleCreateInfo &info) {
Chia-I Wu4d0c7922015-07-03 11:49:42 +0800592 NON_DISPATCHABLE_HANDLE_INIT(vkCreateShaderModule, dev, &info);
Courtney Goeltzenleuchteree4027d2015-06-28 13:01:17 -0600593}
594
Mark Lobodzinski722841d2016-09-07 16:34:56 -0600595VkResult ShaderModule::init_try(const Device &dev, const VkShaderModuleCreateInfo &info) {
Chia-I Wu4d0c7922015-07-03 11:49:42 +0800596 VkShaderModule mod;
597
Chia-I Wuf7458c52015-10-26 21:10:41 +0800598 VkResult err = vkCreateShaderModule(dev.handle(), &info, NULL, &mod);
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700599 if (err == VK_SUCCESS) NonDispHandle::init(dev.handle(), mod);
Courtney Goeltzenleuchteree4027d2015-06-28 13:01:17 -0600600
601 return err;
602}
603
Tony Barbour67e99152015-07-10 14:10:27 -0600604NON_DISPATCHABLE_HANDLE_DTOR(Pipeline, vkDestroyPipeline)
Chia-I Wu2ff72fd2015-07-03 11:49:42 +0800605
Mark Lobodzinski722841d2016-09-07 16:34:56 -0600606void Pipeline::init(const Device &dev, const VkGraphicsPipelineCreateInfo &info) {
Jon Ashburnc669cc62015-07-09 15:02:25 -0600607 VkPipelineCache cache;
608 VkPipelineCacheCreateInfo ci;
Karl Schultz6addd812016-02-02 17:17:23 -0700609 memset((void *)&ci, 0, sizeof(VkPipelineCacheCreateInfo));
Jon Ashburnc669cc62015-07-09 15:02:25 -0600610 ci.sType = VK_STRUCTURE_TYPE_PIPELINE_CACHE_CREATE_INFO;
Chia-I Wuf7458c52015-10-26 21:10:41 +0800611 VkResult err = vkCreatePipelineCache(dev.handle(), &ci, NULL, &cache);
Jon Ashburnc669cc62015-07-09 15:02:25 -0600612 if (err == VK_SUCCESS) {
Mark Lobodzinski722841d2016-09-07 16:34:56 -0600613 NON_DISPATCHABLE_HANDLE_INIT(vkCreateGraphicsPipelines, dev, cache, 1, &info);
Chia-I Wuf7458c52015-10-26 21:10:41 +0800614 vkDestroyPipelineCache(dev.handle(), cache, NULL);
Jon Ashburnc669cc62015-07-09 15:02:25 -0600615 }
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800616}
617
Mark Lobodzinski722841d2016-09-07 16:34:56 -0600618VkResult Pipeline::init_try(const Device &dev, const VkGraphicsPipelineCreateInfo &info) {
Chris Forbes95292b12015-05-25 11:13:26 +1200619 VkPipeline pipe;
Jon Ashburnc669cc62015-07-09 15:02:25 -0600620 VkPipelineCache cache;
621 VkPipelineCacheCreateInfo ci;
Karl Schultz6addd812016-02-02 17:17:23 -0700622 memset((void *)&ci, 0, sizeof(VkPipelineCacheCreateInfo));
Jon Ashburnc669cc62015-07-09 15:02:25 -0600623 ci.sType = VK_STRUCTURE_TYPE_PIPELINE_CACHE_CREATE_INFO;
Chia-I Wuf7458c52015-10-26 21:10:41 +0800624 VkResult err = vkCreatePipelineCache(dev.handle(), &ci, NULL, &cache);
Chia-I Wuf368b602015-07-03 10:41:20 +0800625 EXPECT(err == VK_SUCCESS);
Chris Forbes95292b12015-05-25 11:13:26 +1200626 if (err == VK_SUCCESS) {
Mark Lobodzinski722841d2016-09-07 16:34:56 -0600627 err = vkCreateGraphicsPipelines(dev.handle(), cache, 1, &info, NULL, &pipe);
Jon Ashburnc669cc62015-07-09 15:02:25 -0600628 if (err == VK_SUCCESS) {
Chia-I Wu2ff72fd2015-07-03 11:49:42 +0800629 NonDispHandle::init(dev.handle(), pipe);
Jon Ashburnc669cc62015-07-09 15:02:25 -0600630 }
Chia-I Wuf7458c52015-10-26 21:10:41 +0800631 vkDestroyPipelineCache(dev.handle(), cache, NULL);
Chris Forbes95292b12015-05-25 11:13:26 +1200632 }
633
634 return err;
635}
636
Mark Lobodzinski722841d2016-09-07 16:34:56 -0600637void Pipeline::init(const Device &dev, const VkComputePipelineCreateInfo &info) {
Jon Ashburnc669cc62015-07-09 15:02:25 -0600638 VkPipelineCache cache;
639 VkPipelineCacheCreateInfo ci;
Karl Schultz6addd812016-02-02 17:17:23 -0700640 memset((void *)&ci, 0, sizeof(VkPipelineCacheCreateInfo));
Jon Ashburnc669cc62015-07-09 15:02:25 -0600641 ci.sType = VK_STRUCTURE_TYPE_PIPELINE_CACHE_CREATE_INFO;
Chia-I Wuf7458c52015-10-26 21:10:41 +0800642 VkResult err = vkCreatePipelineCache(dev.handle(), &ci, NULL, &cache);
Jon Ashburnc669cc62015-07-09 15:02:25 -0600643 if (err == VK_SUCCESS) {
Mark Lobodzinski722841d2016-09-07 16:34:56 -0600644 NON_DISPATCHABLE_HANDLE_INIT(vkCreateComputePipelines, dev, cache, 1, &info);
Chia-I Wuf7458c52015-10-26 21:10:41 +0800645 vkDestroyPipelineCache(dev.handle(), cache, NULL);
Jon Ashburnc669cc62015-07-09 15:02:25 -0600646 }
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800647}
648
Tony Barbour67e99152015-07-10 14:10:27 -0600649NON_DISPATCHABLE_HANDLE_DTOR(PipelineLayout, vkDestroyPipelineLayout)
Chia-I Wufd46e7d2015-07-03 11:49:42 +0800650
Mark Lobodzinski722841d2016-09-07 16:34:56 -0600651void PipelineLayout::init(const Device &dev, VkPipelineLayoutCreateInfo &info,
652 const std::vector<const DescriptorSetLayout *> &layouts) {
653 const std::vector<VkDescriptorSetLayout> layout_handles = make_handles<VkDescriptorSetLayout>(layouts);
Tony Barbour482c6092015-07-27 09:37:48 -0600654 info.pSetLayouts = layout_handles.data();
Chia-I Wufd46e7d2015-07-03 11:49:42 +0800655
656 NON_DISPATCHABLE_HANDLE_INIT(vkCreatePipelineLayout, dev, &info);
657}
658
Tony Barbour67e99152015-07-10 14:10:27 -0600659NON_DISPATCHABLE_HANDLE_DTOR(Sampler, vkDestroySampler)
Chia-I Wu8c721c62015-07-03 11:49:42 +0800660
Karl Schultz6addd812016-02-02 17:17:23 -0700661void Sampler::init(const Device &dev, const VkSamplerCreateInfo &info) {
Chia-I Wu8c721c62015-07-03 11:49:42 +0800662 NON_DISPATCHABLE_HANDLE_INIT(vkCreateSampler, dev, &info);
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800663}
664
Tony Barbour67e99152015-07-10 14:10:27 -0600665NON_DISPATCHABLE_HANDLE_DTOR(DescriptorSetLayout, vkDestroyDescriptorSetLayout)
Chia-I Wuafdfd7f2015-07-03 11:49:42 +0800666
Mark Lobodzinski722841d2016-09-07 16:34:56 -0600667void DescriptorSetLayout::init(const Device &dev, const VkDescriptorSetLayoutCreateInfo &info) {
Chia-I Wuafdfd7f2015-07-03 11:49:42 +0800668 NON_DISPATCHABLE_HANDLE_INIT(vkCreateDescriptorSetLayout, dev, &info);
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800669}
670
Tony Barbour67e99152015-07-10 14:10:27 -0600671NON_DISPATCHABLE_HANDLE_DTOR(DescriptorPool, vkDestroyDescriptorPool)
Chia-I Wuafdfd7f2015-07-03 11:49:42 +0800672
Mark Lobodzinski722841d2016-09-07 16:34:56 -0600673void DescriptorPool::init(const Device &dev, const VkDescriptorPoolCreateInfo &info) {
674 setDynamicUsage(info.flags & VK_DESCRIPTOR_POOL_CREATE_FREE_DESCRIPTOR_SET_BIT);
Courtney Goeltzenleuchterfe908d32015-09-16 16:12:45 -0600675 NON_DISPATCHABLE_HANDLE_INIT(vkCreateDescriptorPool, dev, &info);
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800676}
677
Mark Lobodzinski722841d2016-09-07 16:34:56 -0600678void DescriptorPool::reset() { EXPECT(vkResetDescriptorPool(device(), handle(), 0) == VK_SUCCESS); }
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800679
Mark Lobodzinski722841d2016-09-07 16:34:56 -0600680std::vector<DescriptorSet *> DescriptorPool::alloc_sets(const Device &dev,
681 const std::vector<const DescriptorSetLayout *> &layouts) {
682 const std::vector<VkDescriptorSetLayout> layout_handles = make_handles<VkDescriptorSetLayout>(layouts);
Chia-I Wu11078b02015-01-04 16:27:24 +0800683
Chia-I Wuafdfd7f2015-07-03 11:49:42 +0800684 std::vector<VkDescriptorSet> set_handles;
685 set_handles.resize(layout_handles.size());
Chia-I Wu11078b02015-01-04 16:27:24 +0800686
Chia-I Wu3432a0c2015-10-27 18:04:07 +0800687 VkDescriptorSetAllocateInfo alloc_info = {};
Chia-I Wu00ce5402015-11-10 16:21:09 +0800688 alloc_info.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO;
Jon Ashburnf19916e2016-01-11 13:12:43 -0700689 alloc_info.descriptorSetCount = layout_handles.size();
Courtney Goeltzenleuchterbee18a92015-10-23 14:21:05 -0600690 alloc_info.descriptorPool = handle();
691 alloc_info.pSetLayouts = layout_handles.data();
Mark Lobodzinski722841d2016-09-07 16:34:56 -0600692 VkResult err = vkAllocateDescriptorSets(device(), &alloc_info, set_handles.data());
Cody Northrop1e4f8022015-08-03 12:47:29 -0600693 EXPECT(err == VK_SUCCESS);
Chia-I Wu11078b02015-01-04 16:27:24 +0800694
695 std::vector<DescriptorSet *> sets;
Mark Lobodzinski722841d2016-09-07 16:34:56 -0600696 for (std::vector<VkDescriptorSet>::const_iterator it = set_handles.begin(); it != set_handles.end(); it++) {
Chia-I Wu11078b02015-01-04 16:27:24 +0800697 // do descriptor sets need memories bound?
Cody Northropcdc72a42015-10-08 11:39:25 -0600698 DescriptorSet *descriptorSet = new DescriptorSet(dev, this, *it);
Mark Lobodzinski40f7f402015-04-16 11:44:05 -0500699 sets.push_back(descriptorSet);
Chia-I Wu11078b02015-01-04 16:27:24 +0800700 }
Chia-I Wu11078b02015-01-04 16:27:24 +0800701 return sets;
702}
703
Mark Lobodzinski722841d2016-09-07 16:34:56 -0600704std::vector<DescriptorSet *> DescriptorPool::alloc_sets(const Device &dev, const DescriptorSetLayout &layout, uint32_t count) {
705 return alloc_sets(dev, std::vector<const DescriptorSetLayout *>(count, &layout));
Chia-I Wu11078b02015-01-04 16:27:24 +0800706}
707
Mark Lobodzinski722841d2016-09-07 16:34:56 -0600708DescriptorSet *DescriptorPool::alloc_sets(const Device &dev, const DescriptorSetLayout &layout) {
Courtney Goeltzenleuchterbee18a92015-10-23 14:21:05 -0600709 std::vector<DescriptorSet *> set = alloc_sets(dev, layout, 1);
Chia-I Wu11078b02015-01-04 16:27:24 +0800710 return (set.empty()) ? NULL : set[0];
711}
712
Karl Schultz6addd812016-02-02 17:17:23 -0700713DescriptorSet::~DescriptorSet() {
Tony Barbour67e99152015-07-10 14:10:27 -0600714 if (initialized()) {
Cody Northropcdc72a42015-10-08 11:39:25 -0600715 // Only call vkFree* on sets allocated from pool with usage *_DYNAMIC
716 if (containing_pool_->getDynamicUsage()) {
Karl Schultz6addd812016-02-02 17:17:23 -0700717 VkDescriptorSet sets[1] = {handle()};
Mark Lobodzinski722841d2016-09-07 16:34:56 -0600718 EXPECT(vkFreeDescriptorSets(device(), containing_pool_->GetObj(), 1, sets) == VK_SUCCESS);
Cody Northropcdc72a42015-10-08 11:39:25 -0600719 }
Tony Barbour67e99152015-07-10 14:10:27 -0600720 }
721}
Chia-I Wuafdfd7f2015-07-03 11:49:42 +0800722
Chia-I Wu3432a0c2015-10-27 18:04:07 +0800723NON_DISPATCHABLE_HANDLE_DTOR(CommandPool, vkDestroyCommandPool)
Courtney Goeltzenleuchteree5d80b2015-07-10 19:50:17 -0600724
Karl Schultz6addd812016-02-02 17:17:23 -0700725void CommandPool::init(const Device &dev, const VkCommandPoolCreateInfo &info) {
Courtney Goeltzenleuchteree5d80b2015-07-10 19:50:17 -0600726 NON_DISPATCHABLE_HANDLE_INIT(vkCreateCommandPool, dev, &info);
727}
728
Karl Schultz6addd812016-02-02 17:17:23 -0700729CommandBuffer::~CommandBuffer() {
Courtney Goeltzenleuchterbee18a92015-10-23 14:21:05 -0600730 if (initialized()) {
Karl Schultz6addd812016-02-02 17:17:23 -0700731 VkCommandBuffer cmds[] = {handle()};
Courtney Goeltzenleuchterbee18a92015-10-23 14:21:05 -0600732 vkFreeCommandBuffers(dev_handle_, cmd_pool_, 1, cmds);
733 }
Chia-I Wube2b9172015-07-03 11:49:42 +0800734}
735
Mark Lobodzinski722841d2016-09-07 16:34:56 -0600736void CommandBuffer::init(const Device &dev, const VkCommandBufferAllocateInfo &info) {
Chia-I Wu3432a0c2015-10-27 18:04:07 +0800737 VkCommandBuffer cmd;
Chia-I Wube2b9172015-07-03 11:49:42 +0800738
Chia-I Wu3432a0c2015-10-27 18:04:07 +0800739 // Make sure commandPool is set
740 assert(info.commandPool);
Courtney Goeltzenleuchterd8e68bb2015-07-13 12:53:32 -0600741
Mark Lobodzinski722841d2016-09-07 16:34:56 -0600742 if (EXPECT(vkAllocateCommandBuffers(dev.handle(), &info, &cmd) == VK_SUCCESS)) {
Chia-I Wube2b9172015-07-03 11:49:42 +0800743 Handle::init(cmd);
744 dev_handle_ = dev.handle();
Chia-I Wu3432a0c2015-10-27 18:04:07 +0800745 cmd_pool_ = info.commandPool;
Chia-I Wube2b9172015-07-03 11:49:42 +0800746 }
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800747}
748
Mark Lobodzinski722841d2016-09-07 16:34:56 -0600749void CommandBuffer::begin(const VkCommandBufferBeginInfo *info) { EXPECT(vkBeginCommandBuffer(handle(), info) == VK_SUCCESS); }
Jeremy Hayesd65ae082015-01-14 16:17:08 -0700750
Karl Schultz6addd812016-02-02 17:17:23 -0700751void CommandBuffer::begin() {
Chia-I Wu3432a0c2015-10-27 18:04:07 +0800752 VkCommandBufferBeginInfo info = {};
Jon Ashburnf19916e2016-01-11 13:12:43 -0700753 VkCommandBufferInheritanceInfo hinfo = {};
Chia-I Wu3432a0c2015-10-27 18:04:07 +0800754 info.flags = VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT;
755 info.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
Jon Ashburnf19916e2016-01-11 13:12:43 -0700756 info.pInheritanceInfo = &hinfo;
757 hinfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_INHERITANCE_INFO;
758 hinfo.pNext = NULL;
759 hinfo.renderPass = VK_NULL_HANDLE;
760 hinfo.subpass = 0;
761 hinfo.framebuffer = VK_NULL_HANDLE;
762 hinfo.occlusionQueryEnable = VK_FALSE;
763 hinfo.queryFlags = 0;
764 hinfo.pipelineStatistics = 0;
Jeremy Hayesd65ae082015-01-14 16:17:08 -0700765
766 begin(&info);
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800767}
768
Mark Lobodzinski722841d2016-09-07 16:34:56 -0600769void CommandBuffer::end() { EXPECT(vkEndCommandBuffer(handle()) == VK_SUCCESS); }
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800770
Mark Lobodzinski722841d2016-09-07 16:34:56 -0600771void CommandBuffer::reset(VkCommandBufferResetFlags flags) { EXPECT(vkResetCommandBuffer(handle(), flags) == VK_SUCCESS); }
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800772
Petr Kraus13c98a62017-12-09 00:22:39 +0100773} // namespace vk_testing