blob: 4f0f277d2efd4d9d821a477a650d38c8362f282c [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 Lobodzinski64318ba2017-01-26 13:34:13 -070065} // namespace
Chia-I Wuf1e2e992014-12-27 14:12:52 +080066
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -060067namespace vk_testing {
Chia-I Wuf1e2e992014-12-27 14:12:52 +080068
Karl Schultz6addd812016-02-02 17:17:23 -070069void set_error_callback(ErrorCallback callback) { error_callback = callback; }
Chia-I Wuf1e2e992014-12-27 14:12:52 +080070
Karl Schultz6addd812016-02-02 17:17:23 -070071VkPhysicalDeviceProperties PhysicalDevice::properties() const {
Tony Barbour59a47322015-06-24 16:06:58 -060072 VkPhysicalDeviceProperties info;
73
Courtney Goeltzenleuchter06d89472015-10-20 16:40:38 -060074 vkGetPhysicalDeviceProperties(handle(), &info);
Tony Barbour59a47322015-06-24 16:06:58 -060075
76 return info;
Chia-I Wuf1e2e992014-12-27 14:12:52 +080077}
78
Karl Schultz6addd812016-02-02 17:17:23 -070079std::vector<VkQueueFamilyProperties> PhysicalDevice::queue_properties() const {
Cody Northropd0802882015-08-03 17:04:53 -060080 std::vector<VkQueueFamilyProperties> info;
Tony Barbour59a47322015-06-24 16:06:58 -060081 uint32_t count;
82
Cody Northropd0802882015-08-03 17:04:53 -060083 // Call once with NULL data to receive count
Courtney Goeltzenleuchter06d89472015-10-20 16:40:38 -060084 vkGetPhysicalDeviceQueueFamilyProperties(handle(), &count, NULL);
85 info.resize(count);
86 vkGetPhysicalDeviceQueueFamilyProperties(handle(), &count, info.data());
Tony Barbour59a47322015-06-24 16:06:58 -060087
88 return info;
Chia-I Wuf1e2e992014-12-27 14:12:52 +080089}
90
Karl Schultz6addd812016-02-02 17:17:23 -070091VkPhysicalDeviceMemoryProperties PhysicalDevice::memory_properties() const {
Tony Barbour59a47322015-06-24 16:06:58 -060092 VkPhysicalDeviceMemoryProperties info;
93
Courtney Goeltzenleuchter06d89472015-10-20 16:40:38 -060094 vkGetPhysicalDeviceMemoryProperties(handle(), &info);
Mark Lobodzinskib3fbcd92015-07-02 16:49:40 -060095
Tony Barbour59a47322015-06-24 16:06:58 -060096 return info;
Chia-I Wuf1e2e992014-12-27 14:12:52 +080097}
98
Chris Forbesf9cfe182016-04-04 17:22:42 +120099VkPhysicalDeviceFeatures PhysicalDevice::features() const {
100 VkPhysicalDeviceFeatures features;
101 vkGetPhysicalDeviceFeatures(handle(), &features);
102 return features;
103}
104
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -0600105/*
106 * Return list of Global layers available
107 */
Karl Schultz6addd812016-02-02 17:17:23 -0700108std::vector<VkLayerProperties> GetGlobalLayers() {
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -0600109 VkResult err;
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -0600110 std::vector<VkLayerProperties> layers;
Courtney Goeltzenleuchtercd69eee2015-07-06 09:10:47 -0600111 uint32_t layer_count;
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -0600112
Courtney Goeltzenleuchtercd69eee2015-07-06 09:10:47 -0600113 do {
114 layer_count = 0;
Courtney Goeltzenleuchter35985f62015-09-14 17:22:16 -0600115 err = vkEnumerateInstanceLayerProperties(&layer_count, NULL);
Courtney Goeltzenleuchtercd69eee2015-07-06 09:10:47 -0600116
117 if (err == VK_SUCCESS) {
118 layers.reserve(layer_count);
Mark Lobodzinski722841d2016-09-07 16:34:56 -0600119 err = vkEnumerateInstanceLayerProperties(&layer_count, layers.data());
Courtney Goeltzenleuchtercd69eee2015-07-06 09:10:47 -0600120 }
121 } while (err == VK_INCOMPLETE);
122
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -0600123 assert(err == VK_SUCCESS);
124
125 return layers;
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800126}
127
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -0600128/*
129 * Return list of Global extensions provided by the ICD / Loader
130 */
Mark Lobodzinski722841d2016-09-07 16:34:56 -0600131std::vector<VkExtensionProperties> GetGlobalExtensions() { return GetGlobalExtensions(NULL); }
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -0600132
133/*
134 * Return list of Global extensions provided by the specified layer
Karl Schultz6addd812016-02-02 17:17:23 -0700135 * If pLayerName is NULL, will return extensions implemented by the loader /
136 * ICDs
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -0600137 */
Karl Schultz6addd812016-02-02 17:17:23 -0700138std::vector<VkExtensionProperties> GetGlobalExtensions(const char *pLayerName) {
Courtney Goeltzenleuchtercd69eee2015-07-06 09:10:47 -0600139 std::vector<VkExtensionProperties> exts;
140 uint32_t ext_count;
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -0600141 VkResult err;
142
Courtney Goeltzenleuchtercd69eee2015-07-06 09:10:47 -0600143 do {
144 ext_count = 0;
Mark Lobodzinski722841d2016-09-07 16:34:56 -0600145 err = vkEnumerateInstanceExtensionProperties(pLayerName, &ext_count, NULL);
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -0600146
Courtney Goeltzenleuchtercd69eee2015-07-06 09:10:47 -0600147 if (err == VK_SUCCESS) {
Courtney Goeltzenleuchter381f3a22015-07-06 15:45:58 -0600148 exts.resize(ext_count);
Mark Lobodzinski722841d2016-09-07 16:34:56 -0600149 err = vkEnumerateInstanceExtensionProperties(pLayerName, &ext_count, exts.data());
Courtney Goeltzenleuchtercd69eee2015-07-06 09:10:47 -0600150 }
151 } while (err == VK_INCOMPLETE);
152
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -0600153 assert(err == VK_SUCCESS);
154
155 return exts;
156}
157
158/*
159 * Return list of PhysicalDevice extensions provided by the ICD / Loader
160 */
Mark Lobodzinski722841d2016-09-07 16:34:56 -0600161std::vector<VkExtensionProperties> PhysicalDevice::extensions() const { return extensions(NULL); }
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -0600162
163/*
164 * Return list of PhysicalDevice extensions provided by the specified layer
165 * If pLayerName is NULL, will return extensions for ICD / loader.
166 */
Mark Lobodzinski722841d2016-09-07 16:34:56 -0600167std::vector<VkExtensionProperties> PhysicalDevice::extensions(const char *pLayerName) const {
Courtney Goeltzenleuchtercd69eee2015-07-06 09:10:47 -0600168 std::vector<VkExtensionProperties> exts;
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -0600169 VkResult err;
170
Courtney Goeltzenleuchtercd69eee2015-07-06 09:10:47 -0600171 do {
172 uint32_t extCount = 0;
Mark Lobodzinski722841d2016-09-07 16:34:56 -0600173 err = vkEnumerateDeviceExtensionProperties(handle(), pLayerName, &extCount, NULL);
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -0600174
Courtney Goeltzenleuchtercd69eee2015-07-06 09:10:47 -0600175 if (err == VK_SUCCESS) {
Ian Elliott1a3845b2015-07-06 14:33:04 -0600176 exts.resize(extCount);
Mark Lobodzinski722841d2016-09-07 16:34:56 -0600177 err = vkEnumerateDeviceExtensionProperties(handle(), pLayerName, &extCount, exts.data());
Courtney Goeltzenleuchtercd69eee2015-07-06 09:10:47 -0600178 }
179 } while (err == VK_INCOMPLETE);
180
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -0600181 assert(err == VK_SUCCESS);
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800182
183 return exts;
184}
185
Mark Lobodzinski722841d2016-09-07 16:34:56 -0600186bool PhysicalDevice::set_memory_type(const uint32_t type_bits, VkMemoryAllocateInfo *info, const VkFlags properties,
Karl Schultz6addd812016-02-02 17:17:23 -0700187 const VkFlags forbid) const {
188 uint32_t type_mask = type_bits;
189 // Search memtypes to find first index with those properties
190 for (uint32_t i = 0; i < memory_properties_.memoryTypeCount; i++) {
191 if ((type_mask & 1) == 1) {
192 // Type is available, does it match user properties?
Mark Lobodzinski722841d2016-09-07 16:34:56 -0600193 if ((memory_properties_.memoryTypes[i].propertyFlags & properties) == properties &&
194 (memory_properties_.memoryTypes[i].propertyFlags & forbid) == 0) {
Karl Schultz6addd812016-02-02 17:17:23 -0700195 info->memoryTypeIndex = i;
196 return true;
197 }
198 }
199 type_mask >>= 1;
200 }
201 // No memory types matched, return failure
202 return false;
Mark Lobodzinskib3fbcd92015-07-02 16:49:40 -0600203}
204
Courtney Goeltzenleuchtercd69eee2015-07-06 09:10:47 -0600205/*
206 * Return list of PhysicalDevice layers
207 */
Karl Schultz6addd812016-02-02 17:17:23 -0700208std::vector<VkLayerProperties> PhysicalDevice::layers() const {
Courtney Goeltzenleuchtercd69eee2015-07-06 09:10:47 -0600209 std::vector<VkLayerProperties> layer_props;
210 VkResult err;
211
212 do {
213 uint32_t layer_count = 0;
Courtney Goeltzenleuchter35985f62015-09-14 17:22:16 -0600214 err = vkEnumerateDeviceLayerProperties(handle(), &layer_count, NULL);
Courtney Goeltzenleuchtercd69eee2015-07-06 09:10:47 -0600215
216 if (err == VK_SUCCESS) {
217 layer_props.reserve(layer_count);
Mark Lobodzinski722841d2016-09-07 16:34:56 -0600218 err = vkEnumerateDeviceLayerProperties(handle(), &layer_count, layer_props.data());
Courtney Goeltzenleuchtercd69eee2015-07-06 09:10:47 -0600219 }
220 } while (err == VK_INCOMPLETE);
221
222 assert(err == VK_SUCCESS);
223
224 return layer_props;
225}
226
Karl Schultz6addd812016-02-02 17:17:23 -0700227Device::~Device() {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700228 if (!initialized()) return;
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800229
230 for (int i = 0; i < QUEUE_COUNT; i++) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700231 for (std::vector<Queue *>::iterator it = queues_[i].begin(); it != queues_[i].end(); it++) delete *it;
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800232 queues_[i].clear();
233 }
234
Chia-I Wuf7458c52015-10-26 21:10:41 +0800235 vkDestroyDevice(handle(), NULL);
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800236}
237
Tony Barbour53f7e892016-08-09 13:44:00 -0600238void Device::init(std::vector<const char *> &extensions, VkPhysicalDeviceFeatures *features) {
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800239 // request all queues
Mark Lobodzinski722841d2016-09-07 16:34:56 -0600240 const std::vector<VkQueueFamilyProperties> queue_props = phy_.queue_properties();
Courtney Goeltzenleuchter95487bc2015-04-14 18:48:46 -0600241 std::vector<VkDeviceQueueCreateInfo> queue_info;
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800242 queue_info.reserve(queue_props.size());
Mark Lobodzinski53d023a2016-02-01 09:06:25 -0700243
244 std::vector<std::vector<float>> queue_priorities;
245
Mark Young93ecb1d2016-01-13 13:47:16 -0700246 for (uint32_t i = 0; i < (uint32_t)queue_props.size(); i++) {
Courtney Goeltzenleuchter95487bc2015-04-14 18:48:46 -0600247 VkDeviceQueueCreateInfo qi = {};
Karl Schultz6addd812016-02-02 17:17:23 -0700248 qi.sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO;
249 qi.pNext = NULL;
250 qi.queueFamilyIndex = i;
251 qi.queueCount = queue_props[i].queueCount;
Mark Lobodzinski53d023a2016-02-01 09:06:25 -0700252
Dustin Graves3c0bd582016-04-06 10:16:05 -0600253 queue_priorities.emplace_back(qi.queueCount, 0.0f);
Mark Lobodzinski53d023a2016-02-01 09:06:25 -0700254
255 qi.pQueuePriorities = queue_priorities[i].data();
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600256 if (queue_props[i].queueFlags & VK_QUEUE_GRAPHICS_BIT) {
Courtney Goeltzenleuchter18248e62015-03-05 18:09:39 -0700257 graphics_queue_node_index_ = i;
258 }
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800259 queue_info.push_back(qi);
260 }
261
Courtney Goeltzenleuchter95487bc2015-04-14 18:48:46 -0600262 VkDeviceCreateInfo dev_info = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600263 dev_info.sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO;
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600264 dev_info.pNext = NULL;
Chia-I Wu02124482015-11-06 06:42:02 +0800265 dev_info.queueCreateInfoCount = queue_info.size();
266 dev_info.pQueueCreateInfos = queue_info.data();
Tony Barbour4c70d102016-08-08 16:06:56 -0600267 dev_info.enabledLayerCount = 0;
268 dev_info.ppEnabledLayerNames = NULL;
Jon Ashburnf19916e2016-01-11 13:12:43 -0700269 dev_info.enabledExtensionCount = extensions.size();
Tony Barbour482c6092015-07-27 09:37:48 -0600270 dev_info.ppEnabledExtensionNames = extensions.data();
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800271
Tony Barbour53f7e892016-08-09 13:44:00 -0600272 VkPhysicalDeviceFeatures all_features;
273 if (features) {
274 dev_info.pEnabledFeatures = features;
275 } else {
276 // request all supportable features enabled
277 all_features = phy().features();
278 dev_info.pEnabledFeatures = &all_features;
279 }
Chris Forbesf9cfe182016-04-04 17:22:42 +1200280
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800281 init(dev_info);
282}
283
Karl Schultz6addd812016-02-02 17:17:23 -0700284void Device::init(const VkDeviceCreateInfo &info) {
Chia-I Wuf368b602015-07-03 10:41:20 +0800285 VkDevice dev;
286
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700287 if (EXPECT(vkCreateDevice(phy_.handle(), &info, NULL, &dev) == VK_SUCCESS)) Handle::init(dev);
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800288
289 init_queues();
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800290 init_formats();
291}
292
Karl Schultz6addd812016-02-02 17:17:23 -0700293void Device::init_queues() {
Courtney Goeltzenleuchter18248e62015-03-05 18:09:39 -0700294 uint32_t queue_node_count;
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800295
Cody Northropd0802882015-08-03 17:04:53 -0600296 // Call with NULL data to get count
Mark Lobodzinski722841d2016-09-07 16:34:56 -0600297 vkGetPhysicalDeviceQueueFamilyProperties(phy_.handle(), &queue_node_count, NULL);
Courtney Goeltzenleuchter18248e62015-03-05 18:09:39 -0700298 EXPECT(queue_node_count >= 1);
299
Mark Lobodzinski722841d2016-09-07 16:34:56 -0600300 VkQueueFamilyProperties *queue_props = new VkQueueFamilyProperties[queue_node_count];
Courtney Goeltzenleuchter18248e62015-03-05 18:09:39 -0700301
Mark Lobodzinski722841d2016-09-07 16:34:56 -0600302 vkGetPhysicalDeviceQueueFamilyProperties(phy_.handle(), &queue_node_count, queue_props);
Courtney Goeltzenleuchter18248e62015-03-05 18:09:39 -0700303
Tony Barbour7ea6aa22015-05-22 09:44:58 -0600304 for (uint32_t i = 0; i < queue_node_count; i++) {
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600305 VkQueue queue;
Courtney Goeltzenleuchter18248e62015-03-05 18:09:39 -0700306
Tony Barbour7ea6aa22015-05-22 09:44:58 -0600307 for (uint32_t j = 0; j < queue_props[i].queueCount; j++) {
Karl Schultz6addd812016-02-02 17:17:23 -0700308 // TODO: Need to add support for separate MEMMGR and work queues,
309 // including synchronization
Courtney Goeltzenleuchter06d89472015-10-20 16:40:38 -0600310 vkGetDeviceQueue(handle(), i, j, &queue);
Courtney Goeltzenleuchter18248e62015-03-05 18:09:39 -0700311
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600312 if (queue_props[i].queueFlags & VK_QUEUE_GRAPHICS_BIT) {
Tony Barbourfb21ea32015-07-23 10:35:30 -0600313 queues_[GRAPHICS].push_back(new Queue(queue, i));
Courtney Goeltzenleuchter18248e62015-03-05 18:09:39 -0700314 }
315
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600316 if (queue_props[i].queueFlags & VK_QUEUE_COMPUTE_BIT) {
Tony Barbourfb21ea32015-07-23 10:35:30 -0600317 queues_[COMPUTE].push_back(new Queue(queue, i));
Courtney Goeltzenleuchter18248e62015-03-05 18:09:39 -0700318 }
319
Chia-I Wu1b99bb22015-10-27 19:25:11 +0800320 if (queue_props[i].queueFlags & VK_QUEUE_TRANSFER_BIT) {
Tony Barbourfb21ea32015-07-23 10:35:30 -0600321 queues_[DMA].push_back(new Queue(queue, i));
Courtney Goeltzenleuchter18248e62015-03-05 18:09:39 -0700322 }
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800323 }
324 }
325
Chris Forbes02038792015-06-04 10:49:27 +1200326 delete[] queue_props;
Tony Barbour3d69c9e2015-05-20 16:53:31 -0600327
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800328 EXPECT(!queues_[GRAPHICS].empty() || !queues_[COMPUTE].empty());
329}
330
Karl Schultz6addd812016-02-02 17:17:23 -0700331void Device::init_formats() {
Tony Barbourd1c35722015-04-16 15:59:00 -0600332 for (int f = VK_FORMAT_BEGIN_RANGE; f <= VK_FORMAT_END_RANGE; f++) {
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600333 const VkFormat fmt = static_cast<VkFormat>(f);
334 const VkFormatProperties props = format_properties(fmt);
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800335
Jeremy Hayesa058eee2015-01-23 08:51:43 -0700336 if (props.linearTilingFeatures) {
Mark Lobodzinski722841d2016-09-07 16:34:56 -0600337 const Format tmp = {fmt, VK_IMAGE_TILING_LINEAR, props.linearTilingFeatures};
Jeremy Hayesa058eee2015-01-23 08:51:43 -0700338 formats_.push_back(tmp);
339 }
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800340
Jeremy Hayesa058eee2015-01-23 08:51:43 -0700341 if (props.optimalTilingFeatures) {
Mark Lobodzinski722841d2016-09-07 16:34:56 -0600342 const Format tmp = {fmt, VK_IMAGE_TILING_OPTIMAL, props.optimalTilingFeatures};
Jeremy Hayesa058eee2015-01-23 08:51:43 -0700343 formats_.push_back(tmp);
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800344 }
345 }
346
347 EXPECT(!formats_.empty());
348}
349
Karl Schultz6addd812016-02-02 17:17:23 -0700350VkFormatProperties Device::format_properties(VkFormat format) {
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600351 VkFormatProperties data;
Courtney Goeltzenleuchter06d89472015-10-20 16:40:38 -0600352 vkGetPhysicalDeviceFormatProperties(phy().handle(), format, &data);
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800353
354 return data;
355}
356
Karl Schultz6addd812016-02-02 17:17:23 -0700357void Device::wait() { EXPECT(vkDeviceWaitIdle(handle()) == VK_SUCCESS); }
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800358
Mark Lobodzinski722841d2016-09-07 16:34:56 -0600359VkResult Device::wait(const std::vector<const Fence *> &fences, bool wait_all, uint64_t timeout) {
Chia-I Wud9e8e822015-07-03 11:45:55 +0800360 const std::vector<VkFence> fence_handles = make_handles<VkFence>(fences);
Mark Lobodzinski722841d2016-09-07 16:34:56 -0600361 VkResult err = vkWaitForFences(handle(), fence_handles.size(), fence_handles.data(), wait_all, timeout);
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600362 EXPECT(err == VK_SUCCESS || err == VK_TIMEOUT);
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800363
364 return err;
365}
366
Mark Lobodzinski722841d2016-09-07 16:34:56 -0600367void Device::update_descriptor_sets(const std::vector<VkWriteDescriptorSet> &writes,
368 const std::vector<VkCopyDescriptorSet> &copies) {
369 vkUpdateDescriptorSets(handle(), writes.size(), writes.data(), copies.size(), copies.data());
Chia-I Wu9d00ed72015-05-25 16:27:55 +0800370}
371
Mark Lobodzinski722841d2016-09-07 16:34:56 -0600372void Queue::submit(const std::vector<const CommandBuffer *> &cmds, Fence &fence) {
373 const std::vector<VkCommandBuffer> cmd_handles = make_handles<VkCommandBuffer>(cmds);
Courtney Goeltzenleuchter806c7002015-10-27 11:22:14 -0600374 VkSubmitInfo submit_info;
Chia-I Wuf9be13c2015-10-26 20:37:06 +0800375 submit_info.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
376 submit_info.pNext = NULL;
Chia-I Wud50a7d72015-10-26 20:48:51 +0800377 submit_info.waitSemaphoreCount = 0;
Courtney Goeltzenleuchter806c7002015-10-27 11:22:14 -0600378 submit_info.pWaitSemaphores = NULL;
Jon Ashburn7f9716c2015-12-30 16:42:50 -0700379 submit_info.pWaitDstStageMask = NULL;
Chia-I Wud50a7d72015-10-26 20:48:51 +0800380 submit_info.commandBufferCount = (uint32_t)cmd_handles.size();
Courtney Goeltzenleuchter806c7002015-10-27 11:22:14 -0600381 submit_info.pCommandBuffers = cmd_handles.data();
Chia-I Wud50a7d72015-10-26 20:48:51 +0800382 submit_info.signalSemaphoreCount = 0;
Courtney Goeltzenleuchter806c7002015-10-27 11:22:14 -0600383 submit_info.pSignalSemaphores = NULL;
Courtney Goeltzenleuchter646b9072015-10-20 18:04:07 -0600384
Mark Lobodzinski722841d2016-09-07 16:34:56 -0600385 EXPECT(vkQueueSubmit(handle(), 1, &submit_info, fence.handle()) == VK_SUCCESS);
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800386}
387
Mark Lobodzinski722841d2016-09-07 16:34:56 -0600388void Queue::submit(const CommandBuffer &cmd, Fence &fence) { submit(std::vector<const CommandBuffer *>(1, &cmd), fence); }
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800389
Karl Schultz6addd812016-02-02 17:17:23 -0700390void Queue::submit(const CommandBuffer &cmd) {
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800391 Fence fence;
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600392 submit(cmd, fence);
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800393}
394
Karl Schultz6addd812016-02-02 17:17:23 -0700395void Queue::wait() { EXPECT(vkQueueWaitIdle(handle()) == VK_SUCCESS); }
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800396
Karl Schultz6addd812016-02-02 17:17:23 -0700397DeviceMemory::~DeviceMemory() {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700398 if (initialized()) vkFreeMemory(device(), handle(), NULL);
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800399}
400
Karl Schultz6addd812016-02-02 17:17:23 -0700401void DeviceMemory::init(const Device &dev, const VkMemoryAllocateInfo &info) {
Chia-I Wu3432a0c2015-10-27 18:04:07 +0800402 NON_DISPATCHABLE_HANDLE_INIT(vkAllocateMemory, dev, &info);
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800403}
404
Karl Schultz6addd812016-02-02 17:17:23 -0700405const void *DeviceMemory::map(VkFlags flags) const {
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800406 void *data;
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700407 if (!EXPECT(vkMapMemory(device(), handle(), 0, VK_WHOLE_SIZE, flags, &data) == VK_SUCCESS)) data = NULL;
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800408
409 return data;
410}
411
Karl Schultz6addd812016-02-02 17:17:23 -0700412void *DeviceMemory::map(VkFlags flags) {
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800413 void *data;
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700414 if (!EXPECT(vkMapMemory(device(), handle(), 0, VK_WHOLE_SIZE, flags, &data) == VK_SUCCESS)) data = NULL;
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800415
416 return data;
417}
418
Karl Schultz6addd812016-02-02 17:17:23 -0700419void DeviceMemory::unmap() const { vkUnmapMemory(device(), handle()); }
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800420
Mike Schuchardt8cacbb02017-10-26 14:06:38 -0600421VkMemoryAllocateInfo DeviceMemory::get_resource_alloc_info(const Device &dev, const VkMemoryRequirements &reqs,
422 VkMemoryPropertyFlags mem_props) {
423 VkMemoryAllocateInfo info = alloc_info(reqs.size, 0);
424 dev.phy().set_memory_type(reqs.memoryTypeBits, &info, mem_props);
425 return info;
426}
427
Tony Barbour67e99152015-07-10 14:10:27 -0600428NON_DISPATCHABLE_HANDLE_DTOR(Fence, vkDestroyFence)
Chia-I Wud9e8e822015-07-03 11:45:55 +0800429
Mark Lobodzinski722841d2016-09-07 16:34:56 -0600430void Fence::init(const Device &dev, const VkFenceCreateInfo &info) { NON_DISPATCHABLE_HANDLE_INIT(vkCreateFence, dev, &info); }
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800431
Tony Barbour67e99152015-07-10 14:10:27 -0600432NON_DISPATCHABLE_HANDLE_DTOR(Semaphore, vkDestroySemaphore)
Chia-I Wu6b1c2482015-07-03 11:49:42 +0800433
Karl Schultz6addd812016-02-02 17:17:23 -0700434void Semaphore::init(const Device &dev, const VkSemaphoreCreateInfo &info) {
Chia-I Wu6b1c2482015-07-03 11:49:42 +0800435 NON_DISPATCHABLE_HANDLE_INIT(vkCreateSemaphore, dev, &info);
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800436}
437
Tony Barbour67e99152015-07-10 14:10:27 -0600438NON_DISPATCHABLE_HANDLE_DTOR(Event, vkDestroyEvent)
Chia-I Wuc5c97992015-07-03 11:49:42 +0800439
Mark Lobodzinski722841d2016-09-07 16:34:56 -0600440void Event::init(const Device &dev, const VkEventCreateInfo &info) { NON_DISPATCHABLE_HANDLE_INIT(vkCreateEvent, dev, &info); }
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800441
Karl Schultz6addd812016-02-02 17:17:23 -0700442void Event::set() { EXPECT(vkSetEvent(device(), handle()) == VK_SUCCESS); }
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800443
Karl Schultz6addd812016-02-02 17:17:23 -0700444void Event::reset() { EXPECT(vkResetEvent(device(), handle()) == VK_SUCCESS); }
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800445
Tony Barbour67e99152015-07-10 14:10:27 -0600446NON_DISPATCHABLE_HANDLE_DTOR(QueryPool, vkDestroyQueryPool)
Chia-I Wu1b7d4762015-07-03 11:49:42 +0800447
Karl Schultz6addd812016-02-02 17:17:23 -0700448void QueryPool::init(const Device &dev, const VkQueryPoolCreateInfo &info) {
Chia-I Wu1b7d4762015-07-03 11:49:42 +0800449 NON_DISPATCHABLE_HANDLE_INIT(vkCreateQueryPool, dev, &info);
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800450}
451
Mark Lobodzinski722841d2016-09-07 16:34:56 -0600452VkResult QueryPool::results(uint32_t first, uint32_t count, size_t size, void *data, size_t stride) {
453 VkResult err = vkGetQueryPoolResults(device(), handle(), first, count, size, data, stride, 0);
Chia-I Wuccc93a72015-10-26 18:36:20 +0800454 EXPECT(err == VK_SUCCESS || err == VK_NOT_READY);
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800455
456 return err;
457}
458
Tony Barbour67e99152015-07-10 14:10:27 -0600459NON_DISPATCHABLE_HANDLE_DTOR(Buffer, vkDestroyBuffer)
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800460
Mark Lobodzinski722841d2016-09-07 16:34:56 -0600461void Buffer::init(const Device &dev, const VkBufferCreateInfo &info, VkMemoryPropertyFlags mem_props) {
Tony Barbour4c97d7a2015-04-22 15:10:33 -0600462 init_no_mem(dev, info);
Chia-I Wu681d7a02015-07-03 13:44:34 +0800463
Mike Schuchardt8cacbb02017-10-26 14:06:38 -0600464 internal_mem_.init(dev, DeviceMemory::get_resource_alloc_info(dev, memory_requirements(), mem_props));
Chia-I Wu681d7a02015-07-03 13:44:34 +0800465 bind_memory(internal_mem_, 0);
Tony Barbour4c97d7a2015-04-22 15:10:33 -0600466}
467
Karl Schultz6addd812016-02-02 17:17:23 -0700468void Buffer::init_no_mem(const Device &dev, const VkBufferCreateInfo &info) {
Chia-I Wu681d7a02015-07-03 13:44:34 +0800469 NON_DISPATCHABLE_HANDLE_INIT(vkCreateBuffer, dev, &info);
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800470 create_info_ = info;
471}
472
Karl Schultz6addd812016-02-02 17:17:23 -0700473VkMemoryRequirements Buffer::memory_requirements() const {
Chia-I Wu681d7a02015-07-03 13:44:34 +0800474 VkMemoryRequirements reqs;
475
Courtney Goeltzenleuchter06d89472015-10-20 16:40:38 -0600476 vkGetBufferMemoryRequirements(device(), handle(), &reqs);
Chia-I Wu681d7a02015-07-03 13:44:34 +0800477
478 return reqs;
479}
480
Karl Schultz6addd812016-02-02 17:17:23 -0700481void Buffer::bind_memory(const DeviceMemory &mem, VkDeviceSize mem_offset) {
Mark Lobodzinski722841d2016-09-07 16:34:56 -0600482 EXPECT(vkBindBufferMemory(device(), handle(), mem.handle(), mem_offset) == VK_SUCCESS);
Mark Lobodzinski942b1722015-05-11 17:21:15 -0500483}
484
Tony Barbour67e99152015-07-10 14:10:27 -0600485NON_DISPATCHABLE_HANDLE_DTOR(BufferView, vkDestroyBufferView)
Chia-I Wu3158bf32015-07-03 11:49:42 +0800486
Karl Schultz6addd812016-02-02 17:17:23 -0700487void BufferView::init(const Device &dev, const VkBufferViewCreateInfo &info) {
Chia-I Wu3158bf32015-07-03 11:49:42 +0800488 NON_DISPATCHABLE_HANDLE_INIT(vkCreateBufferView, dev, &info);
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800489}
490
Tony Barbour67e99152015-07-10 14:10:27 -0600491NON_DISPATCHABLE_HANDLE_DTOR(Image, vkDestroyImage)
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800492
Mark Lobodzinski722841d2016-09-07 16:34:56 -0600493void Image::init(const Device &dev, const VkImageCreateInfo &info, VkMemoryPropertyFlags mem_props) {
Tony Barbour4c97d7a2015-04-22 15:10:33 -0600494 init_no_mem(dev, info);
Chia-I Wu681d7a02015-07-03 13:44:34 +0800495
Karl Schultzb5bc11e2016-05-04 08:36:08 -0600496 if (initialized()) {
Mike Schuchardt8cacbb02017-10-26 14:06:38 -0600497 internal_mem_.init(dev, DeviceMemory::get_resource_alloc_info(dev, memory_requirements(), mem_props));
Karl Schultzb5bc11e2016-05-04 08:36:08 -0600498 bind_memory(internal_mem_, 0);
499 }
Tony Barbour4c97d7a2015-04-22 15:10:33 -0600500}
501
Karl Schultz6addd812016-02-02 17:17:23 -0700502void Image::init_no_mem(const Device &dev, const VkImageCreateInfo &info) {
Chia-I Wu681d7a02015-07-03 13:44:34 +0800503 NON_DISPATCHABLE_HANDLE_INIT(vkCreateImage, dev, &info);
Karl Schultzb5bc11e2016-05-04 08:36:08 -0600504 if (initialized()) {
505 init_info(dev, info);
506 }
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800507}
508
Karl Schultz6addd812016-02-02 17:17:23 -0700509void Image::init_info(const Device &dev, const VkImageCreateInfo &info) {
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800510 create_info_ = info;
511
Mark Lobodzinski722841d2016-09-07 16:34:56 -0600512 for (std::vector<Device::Format>::const_iterator it = dev.formats().begin(); it != dev.formats().end(); it++) {
513 if (memcmp(&it->format, &create_info_.format, sizeof(it->format)) == 0 && it->tiling == create_info_.tiling) {
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800514 format_features_ = it->features;
515 break;
516 }
517 }
518}
519
Karl Schultz6addd812016-02-02 17:17:23 -0700520VkMemoryRequirements Image::memory_requirements() const {
Chia-I Wu681d7a02015-07-03 13:44:34 +0800521 VkMemoryRequirements reqs;
522
Courtney Goeltzenleuchter06d89472015-10-20 16:40:38 -0600523 vkGetImageMemoryRequirements(device(), handle(), &reqs);
Chia-I Wu681d7a02015-07-03 13:44:34 +0800524
525 return reqs;
526}
527
Karl Schultz6addd812016-02-02 17:17:23 -0700528void Image::bind_memory(const DeviceMemory &mem, VkDeviceSize mem_offset) {
Mark Lobodzinski722841d2016-09-07 16:34:56 -0600529 EXPECT(vkBindImageMemory(device(), handle(), mem.handle(), mem_offset) == VK_SUCCESS);
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800530}
531
Mark Lobodzinski722841d2016-09-07 16:34:56 -0600532VkSubresourceLayout Image::subresource_layout(const VkImageSubresource &subres) const {
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600533 VkSubresourceLayout data;
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800534 size_t size = sizeof(data);
Courtney Goeltzenleuchter06d89472015-10-20 16:40:38 -0600535 vkGetImageSubresourceLayout(device(), handle(), &subres, &data);
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700536 if (size != sizeof(data)) memset(&data, 0, sizeof(data));
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800537
538 return data;
539}
540
Mark Lobodzinski722841d2016-09-07 16:34:56 -0600541VkSubresourceLayout Image::subresource_layout(const VkImageSubresourceLayers &subrescopy) const {
Courtney Goeltzenleuchter01ee1ca2015-09-10 16:41:13 -0600542 VkSubresourceLayout data;
Mark Lobodzinski722841d2016-09-07 16:34:56 -0600543 VkImageSubresource subres = subresource(subrescopy.aspectMask, subrescopy.mipLevel, subrescopy.baseArrayLayer);
Courtney Goeltzenleuchter01ee1ca2015-09-10 16:41:13 -0600544 size_t size = sizeof(data);
Courtney Goeltzenleuchter06d89472015-10-20 16:40:38 -0600545 vkGetImageSubresourceLayout(device(), handle(), &subres, &data);
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700546 if (size != sizeof(data)) memset(&data, 0, sizeof(data));
Courtney Goeltzenleuchter01ee1ca2015-09-10 16:41:13 -0600547
548 return data;
549}
550
Karl Schultz6addd812016-02-02 17:17:23 -0700551bool Image::transparent() const {
Mark Lobodzinski722841d2016-09-07 16:34:56 -0600552 return (create_info_.tiling == VK_IMAGE_TILING_LINEAR && create_info_.samples == VK_SAMPLE_COUNT_1_BIT &&
553 !(create_info_.usage & (VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT | VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT)));
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800554}
555
Tony Barbour67e99152015-07-10 14:10:27 -0600556NON_DISPATCHABLE_HANDLE_DTOR(ImageView, vkDestroyImageView)
Chia-I Wu3158bf32015-07-03 11:49:42 +0800557
Karl Schultz6addd812016-02-02 17:17:23 -0700558void ImageView::init(const Device &dev, const VkImageViewCreateInfo &info) {
Chia-I Wu3158bf32015-07-03 11:49:42 +0800559 NON_DISPATCHABLE_HANDLE_INIT(vkCreateImageView, dev, &info);
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800560}
561
Tony Barbour67e99152015-07-10 14:10:27 -0600562NON_DISPATCHABLE_HANDLE_DTOR(ShaderModule, vkDestroyShaderModule)
Chia-I Wu4d0c7922015-07-03 11:49:42 +0800563
Mark Lobodzinski722841d2016-09-07 16:34:56 -0600564void ShaderModule::init(const Device &dev, const VkShaderModuleCreateInfo &info) {
Chia-I Wu4d0c7922015-07-03 11:49:42 +0800565 NON_DISPATCHABLE_HANDLE_INIT(vkCreateShaderModule, dev, &info);
Courtney Goeltzenleuchteree4027d2015-06-28 13:01:17 -0600566}
567
Mark Lobodzinski722841d2016-09-07 16:34:56 -0600568VkResult ShaderModule::init_try(const Device &dev, const VkShaderModuleCreateInfo &info) {
Chia-I Wu4d0c7922015-07-03 11:49:42 +0800569 VkShaderModule mod;
570
Chia-I Wuf7458c52015-10-26 21:10:41 +0800571 VkResult err = vkCreateShaderModule(dev.handle(), &info, NULL, &mod);
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700572 if (err == VK_SUCCESS) NonDispHandle::init(dev.handle(), mod);
Courtney Goeltzenleuchteree4027d2015-06-28 13:01:17 -0600573
574 return err;
575}
576
Tony Barbour67e99152015-07-10 14:10:27 -0600577NON_DISPATCHABLE_HANDLE_DTOR(Pipeline, vkDestroyPipeline)
Chia-I Wu2ff72fd2015-07-03 11:49:42 +0800578
Mark Lobodzinski722841d2016-09-07 16:34:56 -0600579void Pipeline::init(const Device &dev, const VkGraphicsPipelineCreateInfo &info) {
Jon Ashburnc669cc62015-07-09 15:02:25 -0600580 VkPipelineCache cache;
581 VkPipelineCacheCreateInfo ci;
Karl Schultz6addd812016-02-02 17:17:23 -0700582 memset((void *)&ci, 0, sizeof(VkPipelineCacheCreateInfo));
Jon Ashburnc669cc62015-07-09 15:02:25 -0600583 ci.sType = VK_STRUCTURE_TYPE_PIPELINE_CACHE_CREATE_INFO;
Chia-I Wuf7458c52015-10-26 21:10:41 +0800584 VkResult err = vkCreatePipelineCache(dev.handle(), &ci, NULL, &cache);
Jon Ashburnc669cc62015-07-09 15:02:25 -0600585 if (err == VK_SUCCESS) {
Mark Lobodzinski722841d2016-09-07 16:34:56 -0600586 NON_DISPATCHABLE_HANDLE_INIT(vkCreateGraphicsPipelines, dev, cache, 1, &info);
Chia-I Wuf7458c52015-10-26 21:10:41 +0800587 vkDestroyPipelineCache(dev.handle(), cache, NULL);
Jon Ashburnc669cc62015-07-09 15:02:25 -0600588 }
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800589}
590
Mark Lobodzinski722841d2016-09-07 16:34:56 -0600591VkResult Pipeline::init_try(const Device &dev, const VkGraphicsPipelineCreateInfo &info) {
Chris Forbes95292b12015-05-25 11:13:26 +1200592 VkPipeline pipe;
Jon Ashburnc669cc62015-07-09 15:02:25 -0600593 VkPipelineCache cache;
594 VkPipelineCacheCreateInfo ci;
Karl Schultz6addd812016-02-02 17:17:23 -0700595 memset((void *)&ci, 0, sizeof(VkPipelineCacheCreateInfo));
Jon Ashburnc669cc62015-07-09 15:02:25 -0600596 ci.sType = VK_STRUCTURE_TYPE_PIPELINE_CACHE_CREATE_INFO;
Chia-I Wuf7458c52015-10-26 21:10:41 +0800597 VkResult err = vkCreatePipelineCache(dev.handle(), &ci, NULL, &cache);
Chia-I Wuf368b602015-07-03 10:41:20 +0800598 EXPECT(err == VK_SUCCESS);
Chris Forbes95292b12015-05-25 11:13:26 +1200599 if (err == VK_SUCCESS) {
Mark Lobodzinski722841d2016-09-07 16:34:56 -0600600 err = vkCreateGraphicsPipelines(dev.handle(), cache, 1, &info, NULL, &pipe);
Jon Ashburnc669cc62015-07-09 15:02:25 -0600601 if (err == VK_SUCCESS) {
Chia-I Wu2ff72fd2015-07-03 11:49:42 +0800602 NonDispHandle::init(dev.handle(), pipe);
Jon Ashburnc669cc62015-07-09 15:02:25 -0600603 }
Chia-I Wuf7458c52015-10-26 21:10:41 +0800604 vkDestroyPipelineCache(dev.handle(), cache, NULL);
Chris Forbes95292b12015-05-25 11:13:26 +1200605 }
606
607 return err;
608}
609
Mark Lobodzinski722841d2016-09-07 16:34:56 -0600610void Pipeline::init(const Device &dev, const VkComputePipelineCreateInfo &info) {
Jon Ashburnc669cc62015-07-09 15:02:25 -0600611 VkPipelineCache cache;
612 VkPipelineCacheCreateInfo ci;
Karl Schultz6addd812016-02-02 17:17:23 -0700613 memset((void *)&ci, 0, sizeof(VkPipelineCacheCreateInfo));
Jon Ashburnc669cc62015-07-09 15:02:25 -0600614 ci.sType = VK_STRUCTURE_TYPE_PIPELINE_CACHE_CREATE_INFO;
Chia-I Wuf7458c52015-10-26 21:10:41 +0800615 VkResult err = vkCreatePipelineCache(dev.handle(), &ci, NULL, &cache);
Jon Ashburnc669cc62015-07-09 15:02:25 -0600616 if (err == VK_SUCCESS) {
Mark Lobodzinski722841d2016-09-07 16:34:56 -0600617 NON_DISPATCHABLE_HANDLE_INIT(vkCreateComputePipelines, dev, cache, 1, &info);
Chia-I Wuf7458c52015-10-26 21:10:41 +0800618 vkDestroyPipelineCache(dev.handle(), cache, NULL);
Jon Ashburnc669cc62015-07-09 15:02:25 -0600619 }
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800620}
621
Tony Barbour67e99152015-07-10 14:10:27 -0600622NON_DISPATCHABLE_HANDLE_DTOR(PipelineLayout, vkDestroyPipelineLayout)
Chia-I Wufd46e7d2015-07-03 11:49:42 +0800623
Mark Lobodzinski722841d2016-09-07 16:34:56 -0600624void PipelineLayout::init(const Device &dev, VkPipelineLayoutCreateInfo &info,
625 const std::vector<const DescriptorSetLayout *> &layouts) {
626 const std::vector<VkDescriptorSetLayout> layout_handles = make_handles<VkDescriptorSetLayout>(layouts);
Tony Barbour482c6092015-07-27 09:37:48 -0600627 info.pSetLayouts = layout_handles.data();
Chia-I Wufd46e7d2015-07-03 11:49:42 +0800628
629 NON_DISPATCHABLE_HANDLE_INIT(vkCreatePipelineLayout, dev, &info);
630}
631
Tony Barbour67e99152015-07-10 14:10:27 -0600632NON_DISPATCHABLE_HANDLE_DTOR(Sampler, vkDestroySampler)
Chia-I Wu8c721c62015-07-03 11:49:42 +0800633
Karl Schultz6addd812016-02-02 17:17:23 -0700634void Sampler::init(const Device &dev, const VkSamplerCreateInfo &info) {
Chia-I Wu8c721c62015-07-03 11:49:42 +0800635 NON_DISPATCHABLE_HANDLE_INIT(vkCreateSampler, dev, &info);
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800636}
637
Tony Barbour67e99152015-07-10 14:10:27 -0600638NON_DISPATCHABLE_HANDLE_DTOR(DescriptorSetLayout, vkDestroyDescriptorSetLayout)
Chia-I Wuafdfd7f2015-07-03 11:49:42 +0800639
Mark Lobodzinski722841d2016-09-07 16:34:56 -0600640void DescriptorSetLayout::init(const Device &dev, const VkDescriptorSetLayoutCreateInfo &info) {
Chia-I Wuafdfd7f2015-07-03 11:49:42 +0800641 NON_DISPATCHABLE_HANDLE_INIT(vkCreateDescriptorSetLayout, dev, &info);
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800642}
643
Tony Barbour67e99152015-07-10 14:10:27 -0600644NON_DISPATCHABLE_HANDLE_DTOR(DescriptorPool, vkDestroyDescriptorPool)
Chia-I Wuafdfd7f2015-07-03 11:49:42 +0800645
Mark Lobodzinski722841d2016-09-07 16:34:56 -0600646void DescriptorPool::init(const Device &dev, const VkDescriptorPoolCreateInfo &info) {
647 setDynamicUsage(info.flags & VK_DESCRIPTOR_POOL_CREATE_FREE_DESCRIPTOR_SET_BIT);
Courtney Goeltzenleuchterfe908d32015-09-16 16:12:45 -0600648 NON_DISPATCHABLE_HANDLE_INIT(vkCreateDescriptorPool, dev, &info);
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800649}
650
Mark Lobodzinski722841d2016-09-07 16:34:56 -0600651void DescriptorPool::reset() { EXPECT(vkResetDescriptorPool(device(), handle(), 0) == VK_SUCCESS); }
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800652
Mark Lobodzinski722841d2016-09-07 16:34:56 -0600653std::vector<DescriptorSet *> DescriptorPool::alloc_sets(const Device &dev,
654 const std::vector<const DescriptorSetLayout *> &layouts) {
655 const std::vector<VkDescriptorSetLayout> layout_handles = make_handles<VkDescriptorSetLayout>(layouts);
Chia-I Wu11078b02015-01-04 16:27:24 +0800656
Chia-I Wuafdfd7f2015-07-03 11:49:42 +0800657 std::vector<VkDescriptorSet> set_handles;
658 set_handles.resize(layout_handles.size());
Chia-I Wu11078b02015-01-04 16:27:24 +0800659
Chia-I Wu3432a0c2015-10-27 18:04:07 +0800660 VkDescriptorSetAllocateInfo alloc_info = {};
Chia-I Wu00ce5402015-11-10 16:21:09 +0800661 alloc_info.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO;
Jon Ashburnf19916e2016-01-11 13:12:43 -0700662 alloc_info.descriptorSetCount = layout_handles.size();
Courtney Goeltzenleuchterbee18a92015-10-23 14:21:05 -0600663 alloc_info.descriptorPool = handle();
664 alloc_info.pSetLayouts = layout_handles.data();
Mark Lobodzinski722841d2016-09-07 16:34:56 -0600665 VkResult err = vkAllocateDescriptorSets(device(), &alloc_info, set_handles.data());
Cody Northrop1e4f8022015-08-03 12:47:29 -0600666 EXPECT(err == VK_SUCCESS);
Chia-I Wu11078b02015-01-04 16:27:24 +0800667
668 std::vector<DescriptorSet *> sets;
Mark Lobodzinski722841d2016-09-07 16:34:56 -0600669 for (std::vector<VkDescriptorSet>::const_iterator it = set_handles.begin(); it != set_handles.end(); it++) {
Chia-I Wu11078b02015-01-04 16:27:24 +0800670 // do descriptor sets need memories bound?
Cody Northropcdc72a42015-10-08 11:39:25 -0600671 DescriptorSet *descriptorSet = new DescriptorSet(dev, this, *it);
Mark Lobodzinski40f7f402015-04-16 11:44:05 -0500672 sets.push_back(descriptorSet);
Chia-I Wu11078b02015-01-04 16:27:24 +0800673 }
Chia-I Wu11078b02015-01-04 16:27:24 +0800674 return sets;
675}
676
Mark Lobodzinski722841d2016-09-07 16:34:56 -0600677std::vector<DescriptorSet *> DescriptorPool::alloc_sets(const Device &dev, const DescriptorSetLayout &layout, uint32_t count) {
678 return alloc_sets(dev, std::vector<const DescriptorSetLayout *>(count, &layout));
Chia-I Wu11078b02015-01-04 16:27:24 +0800679}
680
Mark Lobodzinski722841d2016-09-07 16:34:56 -0600681DescriptorSet *DescriptorPool::alloc_sets(const Device &dev, const DescriptorSetLayout &layout) {
Courtney Goeltzenleuchterbee18a92015-10-23 14:21:05 -0600682 std::vector<DescriptorSet *> set = alloc_sets(dev, layout, 1);
Chia-I Wu11078b02015-01-04 16:27:24 +0800683 return (set.empty()) ? NULL : set[0];
684}
685
Karl Schultz6addd812016-02-02 17:17:23 -0700686DescriptorSet::~DescriptorSet() {
Tony Barbour67e99152015-07-10 14:10:27 -0600687 if (initialized()) {
Cody Northropcdc72a42015-10-08 11:39:25 -0600688 // Only call vkFree* on sets allocated from pool with usage *_DYNAMIC
689 if (containing_pool_->getDynamicUsage()) {
Karl Schultz6addd812016-02-02 17:17:23 -0700690 VkDescriptorSet sets[1] = {handle()};
Mark Lobodzinski722841d2016-09-07 16:34:56 -0600691 EXPECT(vkFreeDescriptorSets(device(), containing_pool_->GetObj(), 1, sets) == VK_SUCCESS);
Cody Northropcdc72a42015-10-08 11:39:25 -0600692 }
Tony Barbour67e99152015-07-10 14:10:27 -0600693 }
694}
Chia-I Wuafdfd7f2015-07-03 11:49:42 +0800695
Chia-I Wu3432a0c2015-10-27 18:04:07 +0800696NON_DISPATCHABLE_HANDLE_DTOR(CommandPool, vkDestroyCommandPool)
Courtney Goeltzenleuchteree5d80b2015-07-10 19:50:17 -0600697
Karl Schultz6addd812016-02-02 17:17:23 -0700698void CommandPool::init(const Device &dev, const VkCommandPoolCreateInfo &info) {
Courtney Goeltzenleuchteree5d80b2015-07-10 19:50:17 -0600699 NON_DISPATCHABLE_HANDLE_INIT(vkCreateCommandPool, dev, &info);
700}
701
Karl Schultz6addd812016-02-02 17:17:23 -0700702CommandBuffer::~CommandBuffer() {
Courtney Goeltzenleuchterbee18a92015-10-23 14:21:05 -0600703 if (initialized()) {
Karl Schultz6addd812016-02-02 17:17:23 -0700704 VkCommandBuffer cmds[] = {handle()};
Courtney Goeltzenleuchterbee18a92015-10-23 14:21:05 -0600705 vkFreeCommandBuffers(dev_handle_, cmd_pool_, 1, cmds);
706 }
Chia-I Wube2b9172015-07-03 11:49:42 +0800707}
708
Mark Lobodzinski722841d2016-09-07 16:34:56 -0600709void CommandBuffer::init(const Device &dev, const VkCommandBufferAllocateInfo &info) {
Chia-I Wu3432a0c2015-10-27 18:04:07 +0800710 VkCommandBuffer cmd;
Chia-I Wube2b9172015-07-03 11:49:42 +0800711
Chia-I Wu3432a0c2015-10-27 18:04:07 +0800712 // Make sure commandPool is set
713 assert(info.commandPool);
Courtney Goeltzenleuchterd8e68bb2015-07-13 12:53:32 -0600714
Mark Lobodzinski722841d2016-09-07 16:34:56 -0600715 if (EXPECT(vkAllocateCommandBuffers(dev.handle(), &info, &cmd) == VK_SUCCESS)) {
Chia-I Wube2b9172015-07-03 11:49:42 +0800716 Handle::init(cmd);
717 dev_handle_ = dev.handle();
Chia-I Wu3432a0c2015-10-27 18:04:07 +0800718 cmd_pool_ = info.commandPool;
Chia-I Wube2b9172015-07-03 11:49:42 +0800719 }
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800720}
721
Mark Lobodzinski722841d2016-09-07 16:34:56 -0600722void CommandBuffer::begin(const VkCommandBufferBeginInfo *info) { EXPECT(vkBeginCommandBuffer(handle(), info) == VK_SUCCESS); }
Jeremy Hayesd65ae082015-01-14 16:17:08 -0700723
Karl Schultz6addd812016-02-02 17:17:23 -0700724void CommandBuffer::begin() {
Chia-I Wu3432a0c2015-10-27 18:04:07 +0800725 VkCommandBufferBeginInfo info = {};
Jon Ashburnf19916e2016-01-11 13:12:43 -0700726 VkCommandBufferInheritanceInfo hinfo = {};
Chia-I Wu3432a0c2015-10-27 18:04:07 +0800727 info.flags = VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT;
728 info.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
Jon Ashburnf19916e2016-01-11 13:12:43 -0700729 info.pInheritanceInfo = &hinfo;
730 hinfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_INHERITANCE_INFO;
731 hinfo.pNext = NULL;
732 hinfo.renderPass = VK_NULL_HANDLE;
733 hinfo.subpass = 0;
734 hinfo.framebuffer = VK_NULL_HANDLE;
735 hinfo.occlusionQueryEnable = VK_FALSE;
736 hinfo.queryFlags = 0;
737 hinfo.pipelineStatistics = 0;
Jeremy Hayesd65ae082015-01-14 16:17:08 -0700738
739 begin(&info);
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800740}
741
Mark Lobodzinski722841d2016-09-07 16:34:56 -0600742void CommandBuffer::end() { EXPECT(vkEndCommandBuffer(handle()) == VK_SUCCESS); }
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800743
Mark Lobodzinski722841d2016-09-07 16:34:56 -0600744void CommandBuffer::reset(VkCommandBufferResetFlags flags) { EXPECT(vkResetCommandBuffer(handle(), flags) == VK_SUCCESS); }
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800745
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700746}; // namespace vk_testing