blob: 1f30fab77d186c52e93d87b2cc8b747af69f7806 [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
Dave Houlton0cf0d132017-12-22 13:55:53 -070022#include "test_common.h" // NOEXCEPT macro (must precede vktestbinding.h)
23#include "vktestbinding.h" // Left for clarity, no harm, already included via test_common.h
Petr Krausb9659a02017-12-11 01:17:46 +010024#include <algorithm>
Mark Lobodzinski722841d2016-09-07 16:34:56 -060025#include <assert.h>
26#include <iostream>
27#include <stdarg.h>
Mark Lobodzinski64318ba2017-01-26 13:34:13 -070028#include <string.h> // memset(), memcmp()
Chia-I Wuf1e2e992014-12-27 14:12:52 +080029
30namespace {
Chia-I Wuf1e2e992014-12-27 14:12:52 +080031
Mark Lobodzinski64318ba2017-01-26 13:34:13 -070032#define NON_DISPATCHABLE_HANDLE_INIT(create_func, dev, ...) \
33 do { \
34 handle_type handle; \
35 if (EXPECT(create_func(dev.handle(), __VA_ARGS__, NULL, &handle) == VK_SUCCESS)) \
36 NonDispHandle::init(dev.handle(), handle); \
Chia-I Wuf8f074f2015-07-03 10:58:57 +080037 } while (0)
38
Mark Lobodzinski64318ba2017-01-26 13:34:13 -070039#define NON_DISPATCHABLE_HANDLE_DTOR(cls, destroy_func) \
40 cls::~cls() { \
41 if (initialized()) destroy_func(device(), handle(), NULL); \
Chia-I Wud9e8e822015-07-03 11:45:55 +080042 }
43
Chia-I Wuf1e2e992014-12-27 14:12:52 +080044#define STRINGIFY(x) #x
Mark Lobodzinski722841d2016-09-07 16:34:56 -060045#define EXPECT(expr) ((expr) ? true : expect_failure(STRINGIFY(expr), __FILE__, __LINE__, __FUNCTION__))
Chia-I Wuf1e2e992014-12-27 14:12:52 +080046
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -060047vk_testing::ErrorCallback error_callback;
Chia-I Wuf1e2e992014-12-27 14:12:52 +080048
Mark Lobodzinski722841d2016-09-07 16:34:56 -060049bool expect_failure(const char *expr, const char *file, unsigned int line, const char *function) {
Chia-I Wuf1e2e992014-12-27 14:12:52 +080050 if (error_callback) {
51 error_callback(expr, file, line, function);
52 } else {
Mark Lobodzinski722841d2016-09-07 16:34:56 -060053 std::cerr << file << ":" << line << ": " << function << ": Expectation `" << expr << "' failed.\n";
Chia-I Wuf1e2e992014-12-27 14:12:52 +080054 }
55
56 return false;
57}
58
Mark Lobodzinski64318ba2017-01-26 13:34:13 -070059} // namespace
Chia-I Wuf1e2e992014-12-27 14:12:52 +080060
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -060061namespace vk_testing {
Chia-I Wuf1e2e992014-12-27 14:12:52 +080062
Karl Schultz6addd812016-02-02 17:17:23 -070063void set_error_callback(ErrorCallback callback) { error_callback = callback; }
Chia-I Wuf1e2e992014-12-27 14:12:52 +080064
Karl Schultz6addd812016-02-02 17:17:23 -070065VkPhysicalDeviceProperties PhysicalDevice::properties() const {
Tony Barbour59a47322015-06-24 16:06:58 -060066 VkPhysicalDeviceProperties info;
67
Courtney Goeltzenleuchter06d89472015-10-20 16:40:38 -060068 vkGetPhysicalDeviceProperties(handle(), &info);
Tony Barbour59a47322015-06-24 16:06:58 -060069
70 return info;
Chia-I Wuf1e2e992014-12-27 14:12:52 +080071}
72
Karl Schultz6addd812016-02-02 17:17:23 -070073std::vector<VkQueueFamilyProperties> PhysicalDevice::queue_properties() const {
Cody Northropd0802882015-08-03 17:04:53 -060074 std::vector<VkQueueFamilyProperties> info;
Tony Barbour59a47322015-06-24 16:06:58 -060075 uint32_t count;
76
Cody Northropd0802882015-08-03 17:04:53 -060077 // Call once with NULL data to receive count
Courtney Goeltzenleuchter06d89472015-10-20 16:40:38 -060078 vkGetPhysicalDeviceQueueFamilyProperties(handle(), &count, NULL);
79 info.resize(count);
80 vkGetPhysicalDeviceQueueFamilyProperties(handle(), &count, info.data());
Tony Barbour59a47322015-06-24 16:06:58 -060081
82 return info;
Chia-I Wuf1e2e992014-12-27 14:12:52 +080083}
84
Karl Schultz6addd812016-02-02 17:17:23 -070085VkPhysicalDeviceMemoryProperties PhysicalDevice::memory_properties() const {
Tony Barbour59a47322015-06-24 16:06:58 -060086 VkPhysicalDeviceMemoryProperties info;
87
Courtney Goeltzenleuchter06d89472015-10-20 16:40:38 -060088 vkGetPhysicalDeviceMemoryProperties(handle(), &info);
Mark Lobodzinskib3fbcd92015-07-02 16:49:40 -060089
Tony Barbour59a47322015-06-24 16:06:58 -060090 return info;
Chia-I Wuf1e2e992014-12-27 14:12:52 +080091}
92
Chris Forbesf9cfe182016-04-04 17:22:42 +120093VkPhysicalDeviceFeatures PhysicalDevice::features() const {
94 VkPhysicalDeviceFeatures features;
95 vkGetPhysicalDeviceFeatures(handle(), &features);
96 return features;
97}
98
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -060099/*
100 * Return list of Global layers available
101 */
Karl Schultz6addd812016-02-02 17:17:23 -0700102std::vector<VkLayerProperties> GetGlobalLayers() {
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -0600103 VkResult err;
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -0600104 std::vector<VkLayerProperties> layers;
Courtney Goeltzenleuchtercd69eee2015-07-06 09:10:47 -0600105 uint32_t layer_count;
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -0600106
Courtney Goeltzenleuchtercd69eee2015-07-06 09:10:47 -0600107 do {
108 layer_count = 0;
Courtney Goeltzenleuchter35985f62015-09-14 17:22:16 -0600109 err = vkEnumerateInstanceLayerProperties(&layer_count, NULL);
Courtney Goeltzenleuchtercd69eee2015-07-06 09:10:47 -0600110
111 if (err == VK_SUCCESS) {
112 layers.reserve(layer_count);
Mark Lobodzinski722841d2016-09-07 16:34:56 -0600113 err = vkEnumerateInstanceLayerProperties(&layer_count, layers.data());
Courtney Goeltzenleuchtercd69eee2015-07-06 09:10:47 -0600114 }
115 } while (err == VK_INCOMPLETE);
116
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -0600117 assert(err == VK_SUCCESS);
118
119 return layers;
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800120}
121
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -0600122/*
123 * Return list of Global extensions provided by the ICD / Loader
124 */
Mark Lobodzinski722841d2016-09-07 16:34:56 -0600125std::vector<VkExtensionProperties> GetGlobalExtensions() { return GetGlobalExtensions(NULL); }
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -0600126
127/*
128 * Return list of Global extensions provided by the specified layer
Karl Schultz6addd812016-02-02 17:17:23 -0700129 * If pLayerName is NULL, will return extensions implemented by the loader /
130 * ICDs
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -0600131 */
Karl Schultz6addd812016-02-02 17:17:23 -0700132std::vector<VkExtensionProperties> GetGlobalExtensions(const char *pLayerName) {
Courtney Goeltzenleuchtercd69eee2015-07-06 09:10:47 -0600133 std::vector<VkExtensionProperties> exts;
134 uint32_t ext_count;
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -0600135 VkResult err;
136
Courtney Goeltzenleuchtercd69eee2015-07-06 09:10:47 -0600137 do {
138 ext_count = 0;
Mark Lobodzinski722841d2016-09-07 16:34:56 -0600139 err = vkEnumerateInstanceExtensionProperties(pLayerName, &ext_count, NULL);
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -0600140
Courtney Goeltzenleuchtercd69eee2015-07-06 09:10:47 -0600141 if (err == VK_SUCCESS) {
Courtney Goeltzenleuchter381f3a22015-07-06 15:45:58 -0600142 exts.resize(ext_count);
Mark Lobodzinski722841d2016-09-07 16:34:56 -0600143 err = vkEnumerateInstanceExtensionProperties(pLayerName, &ext_count, exts.data());
Courtney Goeltzenleuchtercd69eee2015-07-06 09:10:47 -0600144 }
145 } while (err == VK_INCOMPLETE);
146
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -0600147 assert(err == VK_SUCCESS);
148
149 return exts;
150}
151
152/*
153 * Return list of PhysicalDevice extensions provided by the ICD / Loader
154 */
Mark Lobodzinski722841d2016-09-07 16:34:56 -0600155std::vector<VkExtensionProperties> PhysicalDevice::extensions() const { return extensions(NULL); }
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -0600156
157/*
158 * Return list of PhysicalDevice extensions provided by the specified layer
159 * If pLayerName is NULL, will return extensions for ICD / loader.
160 */
Mark Lobodzinski722841d2016-09-07 16:34:56 -0600161std::vector<VkExtensionProperties> PhysicalDevice::extensions(const char *pLayerName) const {
Courtney Goeltzenleuchtercd69eee2015-07-06 09:10:47 -0600162 std::vector<VkExtensionProperties> exts;
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -0600163 VkResult err;
164
Courtney Goeltzenleuchtercd69eee2015-07-06 09:10:47 -0600165 do {
166 uint32_t extCount = 0;
Mark Lobodzinski722841d2016-09-07 16:34:56 -0600167 err = vkEnumerateDeviceExtensionProperties(handle(), pLayerName, &extCount, NULL);
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -0600168
Courtney Goeltzenleuchtercd69eee2015-07-06 09:10:47 -0600169 if (err == VK_SUCCESS) {
Ian Elliott1a3845b2015-07-06 14:33:04 -0600170 exts.resize(extCount);
Mark Lobodzinski722841d2016-09-07 16:34:56 -0600171 err = vkEnumerateDeviceExtensionProperties(handle(), pLayerName, &extCount, exts.data());
Courtney Goeltzenleuchtercd69eee2015-07-06 09:10:47 -0600172 }
173 } while (err == VK_INCOMPLETE);
174
Courtney Goeltzenleuchter110fdf92015-06-29 15:39:26 -0600175 assert(err == VK_SUCCESS);
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800176
177 return exts;
178}
179
Mark Lobodzinski722841d2016-09-07 16:34:56 -0600180bool PhysicalDevice::set_memory_type(const uint32_t type_bits, VkMemoryAllocateInfo *info, const VkFlags properties,
Karl Schultz6addd812016-02-02 17:17:23 -0700181 const VkFlags forbid) const {
182 uint32_t type_mask = type_bits;
183 // Search memtypes to find first index with those properties
184 for (uint32_t i = 0; i < memory_properties_.memoryTypeCount; i++) {
185 if ((type_mask & 1) == 1) {
186 // Type is available, does it match user properties?
Mark Lobodzinski722841d2016-09-07 16:34:56 -0600187 if ((memory_properties_.memoryTypes[i].propertyFlags & properties) == properties &&
188 (memory_properties_.memoryTypes[i].propertyFlags & forbid) == 0) {
Karl Schultz6addd812016-02-02 17:17:23 -0700189 info->memoryTypeIndex = i;
190 return true;
191 }
192 }
193 type_mask >>= 1;
194 }
195 // No memory types matched, return failure
196 return false;
Mark Lobodzinskib3fbcd92015-07-02 16:49:40 -0600197}
198
Courtney Goeltzenleuchtercd69eee2015-07-06 09:10:47 -0600199/*
200 * Return list of PhysicalDevice layers
201 */
Karl Schultz6addd812016-02-02 17:17:23 -0700202std::vector<VkLayerProperties> PhysicalDevice::layers() const {
Courtney Goeltzenleuchtercd69eee2015-07-06 09:10:47 -0600203 std::vector<VkLayerProperties> layer_props;
204 VkResult err;
205
206 do {
207 uint32_t layer_count = 0;
Courtney Goeltzenleuchter35985f62015-09-14 17:22:16 -0600208 err = vkEnumerateDeviceLayerProperties(handle(), &layer_count, NULL);
Courtney Goeltzenleuchtercd69eee2015-07-06 09:10:47 -0600209
210 if (err == VK_SUCCESS) {
211 layer_props.reserve(layer_count);
Mark Lobodzinski722841d2016-09-07 16:34:56 -0600212 err = vkEnumerateDeviceLayerProperties(handle(), &layer_count, layer_props.data());
Courtney Goeltzenleuchtercd69eee2015-07-06 09:10:47 -0600213 }
214 } while (err == VK_INCOMPLETE);
215
216 assert(err == VK_SUCCESS);
217
218 return layer_props;
219}
220
Dave Houlton6c72f352018-02-06 17:49:16 -0700221QueueCreateInfoArray::QueueCreateInfoArray(const std::vector<VkQueueFamilyProperties> &queue_props)
222 : queue_info_(), queue_priorities_() {
John Zulauf01da3ee2017-10-18 18:13:37 -0600223 queue_info_.reserve(queue_props.size());
224
Petr Kraus540a67d2017-12-11 00:35:33 +0100225 for (uint32_t i = 0; i < (uint32_t)queue_props.size(); ++i) {
226 if (queue_props[i].queueCount > 0) {
227 VkDeviceQueueCreateInfo qi = {};
228 qi.sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO;
229 qi.pNext = NULL;
230 qi.queueFamilyIndex = i;
231 qi.queueCount = queue_props[i].queueCount;
232 queue_priorities_.emplace_back(qi.queueCount, 0.0f);
233 qi.pQueuePriorities = queue_priorities_[i].data();
234 queue_info_.push_back(qi);
235 }
John Zulauf01da3ee2017-10-18 18:13:37 -0600236 }
237}
238
Karl Schultz6addd812016-02-02 17:17:23 -0700239Device::~Device() {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700240 if (!initialized()) return;
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800241
242 for (int i = 0; i < QUEUE_COUNT; i++) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700243 for (std::vector<Queue *>::iterator it = queues_[i].begin(); it != queues_[i].end(); it++) delete *it;
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800244 queues_[i].clear();
245 }
246
Chia-I Wuf7458c52015-10-26 21:10:41 +0800247 vkDestroyDevice(handle(), NULL);
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800248}
249
Tony Barbour53f7e892016-08-09 13:44:00 -0600250void Device::init(std::vector<const char *> &extensions, VkPhysicalDeviceFeatures *features) {
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800251 // request all queues
Mark Lobodzinski722841d2016-09-07 16:34:56 -0600252 const std::vector<VkQueueFamilyProperties> queue_props = phy_.queue_properties();
John Zulauf01da3ee2017-10-18 18:13:37 -0600253 QueueCreateInfoArray queue_info(phy_.queue_properties());
Mark Young93ecb1d2016-01-13 13:47:16 -0700254 for (uint32_t i = 0; i < (uint32_t)queue_props.size(); i++) {
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600255 if (queue_props[i].queueFlags & VK_QUEUE_GRAPHICS_BIT) {
Courtney Goeltzenleuchter18248e62015-03-05 18:09:39 -0700256 graphics_queue_node_index_ = i;
257 }
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800258 }
Tobin Ehlis04dea912017-11-14 12:10:11 -0700259 // Only request creation with queuefamilies that have at least one queue
260 std::vector<VkDeviceQueueCreateInfo> create_queue_infos;
261 auto qci = queue_info.data();
262 for (uint32_t j = 0; j < queue_info.size(); ++j) {
263 if (qci[j].queueCount) {
264 create_queue_infos.push_back(qci[j]);
265 }
266 }
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800267
Petr Krausb9659a02017-12-11 01:17:46 +0100268 enabled_extensions_ = extensions;
269
Courtney Goeltzenleuchter95487bc2015-04-14 18:48:46 -0600270 VkDeviceCreateInfo dev_info = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600271 dev_info.sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO;
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600272 dev_info.pNext = NULL;
Tobin Ehlis04dea912017-11-14 12:10:11 -0700273 dev_info.queueCreateInfoCount = create_queue_infos.size();
274 dev_info.pQueueCreateInfos = create_queue_infos.data();
Tony Barbour4c70d102016-08-08 16:06:56 -0600275 dev_info.enabledLayerCount = 0;
276 dev_info.ppEnabledLayerNames = NULL;
Jon Ashburnf19916e2016-01-11 13:12:43 -0700277 dev_info.enabledExtensionCount = extensions.size();
Tony Barbour482c6092015-07-27 09:37:48 -0600278 dev_info.ppEnabledExtensionNames = extensions.data();
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800279
Tony Barbour53f7e892016-08-09 13:44:00 -0600280 VkPhysicalDeviceFeatures all_features;
281 if (features) {
282 dev_info.pEnabledFeatures = features;
283 } else {
284 // request all supportable features enabled
285 all_features = phy().features();
286 dev_info.pEnabledFeatures = &all_features;
287 }
Chris Forbesf9cfe182016-04-04 17:22:42 +1200288
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800289 init(dev_info);
290}
291
Karl Schultz6addd812016-02-02 17:17:23 -0700292void Device::init(const VkDeviceCreateInfo &info) {
Chia-I Wuf368b602015-07-03 10:41:20 +0800293 VkDevice dev;
294
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700295 if (EXPECT(vkCreateDevice(phy_.handle(), &info, NULL, &dev) == VK_SUCCESS)) Handle::init(dev);
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800296
297 init_queues();
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800298 init_formats();
299}
300
Karl Schultz6addd812016-02-02 17:17:23 -0700301void Device::init_queues() {
Courtney Goeltzenleuchter18248e62015-03-05 18:09:39 -0700302 uint32_t queue_node_count;
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800303
Cody Northropd0802882015-08-03 17:04:53 -0600304 // Call with NULL data to get count
Mark Lobodzinski722841d2016-09-07 16:34:56 -0600305 vkGetPhysicalDeviceQueueFamilyProperties(phy_.handle(), &queue_node_count, NULL);
Courtney Goeltzenleuchter18248e62015-03-05 18:09:39 -0700306 EXPECT(queue_node_count >= 1);
307
Mark Lobodzinski722841d2016-09-07 16:34:56 -0600308 VkQueueFamilyProperties *queue_props = new VkQueueFamilyProperties[queue_node_count];
Courtney Goeltzenleuchter18248e62015-03-05 18:09:39 -0700309
Mark Lobodzinski722841d2016-09-07 16:34:56 -0600310 vkGetPhysicalDeviceQueueFamilyProperties(phy_.handle(), &queue_node_count, queue_props);
Courtney Goeltzenleuchter18248e62015-03-05 18:09:39 -0700311
Tony Barbour7ea6aa22015-05-22 09:44:58 -0600312 for (uint32_t i = 0; i < queue_node_count; i++) {
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600313 VkQueue queue;
Courtney Goeltzenleuchter18248e62015-03-05 18:09:39 -0700314
Tony Barbour7ea6aa22015-05-22 09:44:58 -0600315 for (uint32_t j = 0; j < queue_props[i].queueCount; j++) {
Karl Schultz6addd812016-02-02 17:17:23 -0700316 // TODO: Need to add support for separate MEMMGR and work queues,
317 // including synchronization
Courtney Goeltzenleuchter06d89472015-10-20 16:40:38 -0600318 vkGetDeviceQueue(handle(), i, j, &queue);
Courtney Goeltzenleuchter18248e62015-03-05 18:09:39 -0700319
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600320 if (queue_props[i].queueFlags & VK_QUEUE_GRAPHICS_BIT) {
Tony Barbourfb21ea32015-07-23 10:35:30 -0600321 queues_[GRAPHICS].push_back(new Queue(queue, i));
Courtney Goeltzenleuchter18248e62015-03-05 18:09:39 -0700322 }
323
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600324 if (queue_props[i].queueFlags & VK_QUEUE_COMPUTE_BIT) {
Tony Barbourfb21ea32015-07-23 10:35:30 -0600325 queues_[COMPUTE].push_back(new Queue(queue, i));
Courtney Goeltzenleuchter18248e62015-03-05 18:09:39 -0700326 }
327
Chia-I Wu1b99bb22015-10-27 19:25:11 +0800328 if (queue_props[i].queueFlags & VK_QUEUE_TRANSFER_BIT) {
Tony Barbourfb21ea32015-07-23 10:35:30 -0600329 queues_[DMA].push_back(new Queue(queue, i));
Courtney Goeltzenleuchter18248e62015-03-05 18:09:39 -0700330 }
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800331 }
332 }
333
Chris Forbes02038792015-06-04 10:49:27 +1200334 delete[] queue_props;
Tony Barbour3d69c9e2015-05-20 16:53:31 -0600335
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800336 EXPECT(!queues_[GRAPHICS].empty() || !queues_[COMPUTE].empty());
337}
338
Karl Schultz6addd812016-02-02 17:17:23 -0700339void Device::init_formats() {
Tony Barbourd1c35722015-04-16 15:59:00 -0600340 for (int f = VK_FORMAT_BEGIN_RANGE; f <= VK_FORMAT_END_RANGE; f++) {
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600341 const VkFormat fmt = static_cast<VkFormat>(f);
342 const VkFormatProperties props = format_properties(fmt);
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800343
Jeremy Hayesa058eee2015-01-23 08:51:43 -0700344 if (props.linearTilingFeatures) {
Mark Lobodzinski722841d2016-09-07 16:34:56 -0600345 const Format tmp = {fmt, VK_IMAGE_TILING_LINEAR, props.linearTilingFeatures};
Jeremy Hayesa058eee2015-01-23 08:51:43 -0700346 formats_.push_back(tmp);
347 }
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800348
Jeremy Hayesa058eee2015-01-23 08:51:43 -0700349 if (props.optimalTilingFeatures) {
Mark Lobodzinski722841d2016-09-07 16:34:56 -0600350 const Format tmp = {fmt, VK_IMAGE_TILING_OPTIMAL, props.optimalTilingFeatures};
Jeremy Hayesa058eee2015-01-23 08:51:43 -0700351 formats_.push_back(tmp);
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800352 }
353 }
354
355 EXPECT(!formats_.empty());
356}
357
Petr Krausb9659a02017-12-11 01:17:46 +0100358bool Device::IsEnbledExtension(const char *extension) {
359 const auto is_x = [&extension](const char *enabled_extension) { return strcmp(extension, enabled_extension) == 0; };
360 return std::any_of(enabled_extensions_.begin(), enabled_extensions_.end(), is_x);
361}
362
Karl Schultz6addd812016-02-02 17:17:23 -0700363VkFormatProperties Device::format_properties(VkFormat format) {
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600364 VkFormatProperties data;
Courtney Goeltzenleuchter06d89472015-10-20 16:40:38 -0600365 vkGetPhysicalDeviceFormatProperties(phy().handle(), format, &data);
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800366
367 return data;
368}
369
Karl Schultz6addd812016-02-02 17:17:23 -0700370void Device::wait() { EXPECT(vkDeviceWaitIdle(handle()) == VK_SUCCESS); }
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800371
Mark Lobodzinski722841d2016-09-07 16:34:56 -0600372VkResult Device::wait(const std::vector<const Fence *> &fences, bool wait_all, uint64_t timeout) {
Petr Kraus858bacd2017-12-01 23:10:08 +0100373 const std::vector<VkFence> fence_handles = MakeVkHandles<VkFence>(fences);
Mark Lobodzinski722841d2016-09-07 16:34:56 -0600374 VkResult err = vkWaitForFences(handle(), fence_handles.size(), fence_handles.data(), wait_all, timeout);
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600375 EXPECT(err == VK_SUCCESS || err == VK_TIMEOUT);
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800376
377 return err;
378}
379
Mark Lobodzinski722841d2016-09-07 16:34:56 -0600380void Device::update_descriptor_sets(const std::vector<VkWriteDescriptorSet> &writes,
381 const std::vector<VkCopyDescriptorSet> &copies) {
382 vkUpdateDescriptorSets(handle(), writes.size(), writes.data(), copies.size(), copies.data());
Chia-I Wu9d00ed72015-05-25 16:27:55 +0800383}
384
John Zulauf71095472018-03-26 14:45:12 -0600385VkResult Queue::submit(const std::vector<const CommandBuffer *> &cmds, const Fence &fence, bool expect_success) {
Petr Kraus858bacd2017-12-01 23:10:08 +0100386 const std::vector<VkCommandBuffer> cmd_handles = MakeVkHandles<VkCommandBuffer>(cmds);
Courtney Goeltzenleuchter806c7002015-10-27 11:22:14 -0600387 VkSubmitInfo submit_info;
Chia-I Wuf9be13c2015-10-26 20:37:06 +0800388 submit_info.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
389 submit_info.pNext = NULL;
Chia-I Wud50a7d72015-10-26 20:48:51 +0800390 submit_info.waitSemaphoreCount = 0;
Courtney Goeltzenleuchter806c7002015-10-27 11:22:14 -0600391 submit_info.pWaitSemaphores = NULL;
Jon Ashburn7f9716c2015-12-30 16:42:50 -0700392 submit_info.pWaitDstStageMask = NULL;
Chia-I Wud50a7d72015-10-26 20:48:51 +0800393 submit_info.commandBufferCount = (uint32_t)cmd_handles.size();
Courtney Goeltzenleuchter806c7002015-10-27 11:22:14 -0600394 submit_info.pCommandBuffers = cmd_handles.data();
Chia-I Wud50a7d72015-10-26 20:48:51 +0800395 submit_info.signalSemaphoreCount = 0;
Courtney Goeltzenleuchter806c7002015-10-27 11:22:14 -0600396 submit_info.pSignalSemaphores = NULL;
Courtney Goeltzenleuchter646b9072015-10-20 18:04:07 -0600397
John Zulauf71095472018-03-26 14:45:12 -0600398 VkResult result = vkQueueSubmit(handle(), 1, &submit_info, fence.handle());
399 if (expect_success) EXPECT(result == VK_SUCCESS);
400 return result;
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800401}
402
John Zulauf71095472018-03-26 14:45:12 -0600403VkResult Queue::submit(const CommandBuffer &cmd, const Fence &fence, bool expect_success) {
404 return submit(std::vector<const CommandBuffer *>(1, &cmd), fence, expect_success);
405}
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800406
John Zulauf71095472018-03-26 14:45:12 -0600407VkResult Queue::submit(const CommandBuffer &cmd, bool expect_success) {
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800408 Fence fence;
John Zulauf71095472018-03-26 14:45:12 -0600409 return submit(cmd, fence);
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800410}
411
John Zulauf71095472018-03-26 14:45:12 -0600412VkResult Queue::wait() {
413 VkResult result = vkQueueWaitIdle(handle());
414 EXPECT(result == VK_SUCCESS);
415 return result;
416}
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800417
Karl Schultz6addd812016-02-02 17:17:23 -0700418DeviceMemory::~DeviceMemory() {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700419 if (initialized()) vkFreeMemory(device(), handle(), NULL);
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800420}
421
Karl Schultz6addd812016-02-02 17:17:23 -0700422void DeviceMemory::init(const Device &dev, const VkMemoryAllocateInfo &info) {
Chia-I Wu3432a0c2015-10-27 18:04:07 +0800423 NON_DISPATCHABLE_HANDLE_INIT(vkAllocateMemory, dev, &info);
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800424}
425
Karl Schultz6addd812016-02-02 17:17:23 -0700426const void *DeviceMemory::map(VkFlags flags) const {
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800427 void *data;
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700428 if (!EXPECT(vkMapMemory(device(), handle(), 0, VK_WHOLE_SIZE, flags, &data) == VK_SUCCESS)) data = NULL;
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800429
430 return data;
431}
432
Karl Schultz6addd812016-02-02 17:17:23 -0700433void *DeviceMemory::map(VkFlags flags) {
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800434 void *data;
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700435 if (!EXPECT(vkMapMemory(device(), handle(), 0, VK_WHOLE_SIZE, flags, &data) == VK_SUCCESS)) data = NULL;
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800436
437 return data;
438}
439
Karl Schultz6addd812016-02-02 17:17:23 -0700440void DeviceMemory::unmap() const { vkUnmapMemory(device(), handle()); }
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800441
Mike Schuchardt8cacbb02017-10-26 14:06:38 -0600442VkMemoryAllocateInfo DeviceMemory::get_resource_alloc_info(const Device &dev, const VkMemoryRequirements &reqs,
443 VkMemoryPropertyFlags mem_props) {
Tobin Ehlisa3d60642017-11-14 10:02:46 -0700444 // Find appropriate memory type for given reqs
445 VkPhysicalDeviceMemoryProperties dev_mem_props = dev.phy().memory_properties();
446 uint32_t mem_type_index = 0;
447 for (mem_type_index = 0; mem_type_index < dev_mem_props.memoryTypeCount; ++mem_type_index) {
448 if (mem_props == (mem_props & dev_mem_props.memoryTypes[mem_type_index].propertyFlags)) break;
449 }
450 // If we exceeded types, then this device doesn't have the memory we need
451 assert(mem_type_index < dev_mem_props.memoryTypeCount);
452 VkMemoryAllocateInfo info = alloc_info(reqs.size, mem_type_index);
Mike Schuchardt7e567b32017-11-16 13:15:20 -0700453 EXPECT(dev.phy().set_memory_type(reqs.memoryTypeBits, &info, mem_props));
Mike Schuchardt8cacbb02017-10-26 14:06:38 -0600454 return info;
455}
456
Tony Barbour67e99152015-07-10 14:10:27 -0600457NON_DISPATCHABLE_HANDLE_DTOR(Fence, vkDestroyFence)
Chia-I Wud9e8e822015-07-03 11:45:55 +0800458
Mark Lobodzinski722841d2016-09-07 16:34:56 -0600459void Fence::init(const Device &dev, const VkFenceCreateInfo &info) { NON_DISPATCHABLE_HANDLE_INIT(vkCreateFence, dev, &info); }
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800460
John Zulauf71095472018-03-26 14:45:12 -0600461VkResult Fence::wait(VkBool32 wait_all, uint64_t timeout) const {
462 VkFence fence = handle();
463 return vkWaitForFences(device(), 1, &fence, wait_all, timeout);
464}
465
Tony Barbour67e99152015-07-10 14:10:27 -0600466NON_DISPATCHABLE_HANDLE_DTOR(Semaphore, vkDestroySemaphore)
Chia-I Wu6b1c2482015-07-03 11:49:42 +0800467
Karl Schultz6addd812016-02-02 17:17:23 -0700468void Semaphore::init(const Device &dev, const VkSemaphoreCreateInfo &info) {
Chia-I Wu6b1c2482015-07-03 11:49:42 +0800469 NON_DISPATCHABLE_HANDLE_INIT(vkCreateSemaphore, dev, &info);
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800470}
471
Tony Barbour67e99152015-07-10 14:10:27 -0600472NON_DISPATCHABLE_HANDLE_DTOR(Event, vkDestroyEvent)
Chia-I Wuc5c97992015-07-03 11:49:42 +0800473
Mark Lobodzinski722841d2016-09-07 16:34:56 -0600474void Event::init(const Device &dev, const VkEventCreateInfo &info) { NON_DISPATCHABLE_HANDLE_INIT(vkCreateEvent, dev, &info); }
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800475
Karl Schultz6addd812016-02-02 17:17:23 -0700476void Event::set() { EXPECT(vkSetEvent(device(), handle()) == VK_SUCCESS); }
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800477
Karl Schultz6addd812016-02-02 17:17:23 -0700478void Event::reset() { EXPECT(vkResetEvent(device(), handle()) == VK_SUCCESS); }
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800479
Tony Barbour67e99152015-07-10 14:10:27 -0600480NON_DISPATCHABLE_HANDLE_DTOR(QueryPool, vkDestroyQueryPool)
Chia-I Wu1b7d4762015-07-03 11:49:42 +0800481
Karl Schultz6addd812016-02-02 17:17:23 -0700482void QueryPool::init(const Device &dev, const VkQueryPoolCreateInfo &info) {
Chia-I Wu1b7d4762015-07-03 11:49:42 +0800483 NON_DISPATCHABLE_HANDLE_INIT(vkCreateQueryPool, dev, &info);
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800484}
485
Mark Lobodzinski722841d2016-09-07 16:34:56 -0600486VkResult QueryPool::results(uint32_t first, uint32_t count, size_t size, void *data, size_t stride) {
487 VkResult err = vkGetQueryPoolResults(device(), handle(), first, count, size, data, stride, 0);
Chia-I Wuccc93a72015-10-26 18:36:20 +0800488 EXPECT(err == VK_SUCCESS || err == VK_NOT_READY);
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800489
490 return err;
491}
492
Tony Barbour67e99152015-07-10 14:10:27 -0600493NON_DISPATCHABLE_HANDLE_DTOR(Buffer, vkDestroyBuffer)
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800494
Mark Lobodzinski722841d2016-09-07 16:34:56 -0600495void Buffer::init(const Device &dev, const VkBufferCreateInfo &info, VkMemoryPropertyFlags mem_props) {
Tony Barbour4c97d7a2015-04-22 15:10:33 -0600496 init_no_mem(dev, info);
Chia-I Wu681d7a02015-07-03 13:44:34 +0800497
Mike Schuchardt8cacbb02017-10-26 14:06:38 -0600498 internal_mem_.init(dev, DeviceMemory::get_resource_alloc_info(dev, memory_requirements(), mem_props));
Chia-I Wu681d7a02015-07-03 13:44:34 +0800499 bind_memory(internal_mem_, 0);
Tony Barbour4c97d7a2015-04-22 15:10:33 -0600500}
501
Karl Schultz6addd812016-02-02 17:17:23 -0700502void Buffer::init_no_mem(const Device &dev, const VkBufferCreateInfo &info) {
Chia-I Wu681d7a02015-07-03 13:44:34 +0800503 NON_DISPATCHABLE_HANDLE_INIT(vkCreateBuffer, dev, &info);
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800504 create_info_ = info;
505}
506
Karl Schultz6addd812016-02-02 17:17:23 -0700507VkMemoryRequirements Buffer::memory_requirements() const {
Chia-I Wu681d7a02015-07-03 13:44:34 +0800508 VkMemoryRequirements reqs;
509
Courtney Goeltzenleuchter06d89472015-10-20 16:40:38 -0600510 vkGetBufferMemoryRequirements(device(), handle(), &reqs);
Chia-I Wu681d7a02015-07-03 13:44:34 +0800511
512 return reqs;
513}
514
Karl Schultz6addd812016-02-02 17:17:23 -0700515void Buffer::bind_memory(const DeviceMemory &mem, VkDeviceSize mem_offset) {
Mark Lobodzinski722841d2016-09-07 16:34:56 -0600516 EXPECT(vkBindBufferMemory(device(), handle(), mem.handle(), mem_offset) == VK_SUCCESS);
Mark Lobodzinski942b1722015-05-11 17:21:15 -0500517}
518
Tony Barbour67e99152015-07-10 14:10:27 -0600519NON_DISPATCHABLE_HANDLE_DTOR(BufferView, vkDestroyBufferView)
Chia-I Wu3158bf32015-07-03 11:49:42 +0800520
Karl Schultz6addd812016-02-02 17:17:23 -0700521void BufferView::init(const Device &dev, const VkBufferViewCreateInfo &info) {
Chia-I Wu3158bf32015-07-03 11:49:42 +0800522 NON_DISPATCHABLE_HANDLE_INIT(vkCreateBufferView, dev, &info);
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800523}
524
Tony Barbour67e99152015-07-10 14:10:27 -0600525NON_DISPATCHABLE_HANDLE_DTOR(Image, vkDestroyImage)
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800526
Mark Lobodzinski722841d2016-09-07 16:34:56 -0600527void Image::init(const Device &dev, const VkImageCreateInfo &info, VkMemoryPropertyFlags mem_props) {
Tony Barbour4c97d7a2015-04-22 15:10:33 -0600528 init_no_mem(dev, info);
Chia-I Wu681d7a02015-07-03 13:44:34 +0800529
Karl Schultzb5bc11e2016-05-04 08:36:08 -0600530 if (initialized()) {
Mike Schuchardt8cacbb02017-10-26 14:06:38 -0600531 internal_mem_.init(dev, DeviceMemory::get_resource_alloc_info(dev, memory_requirements(), mem_props));
Karl Schultzb5bc11e2016-05-04 08:36:08 -0600532 bind_memory(internal_mem_, 0);
533 }
Tony Barbour4c97d7a2015-04-22 15:10:33 -0600534}
535
Karl Schultz6addd812016-02-02 17:17:23 -0700536void Image::init_no_mem(const Device &dev, const VkImageCreateInfo &info) {
Chia-I Wu681d7a02015-07-03 13:44:34 +0800537 NON_DISPATCHABLE_HANDLE_INIT(vkCreateImage, dev, &info);
Karl Schultzb5bc11e2016-05-04 08:36:08 -0600538 if (initialized()) {
539 init_info(dev, info);
540 }
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800541}
542
Karl Schultz6addd812016-02-02 17:17:23 -0700543void Image::init_info(const Device &dev, const VkImageCreateInfo &info) {
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800544 create_info_ = info;
545
Mark Lobodzinski722841d2016-09-07 16:34:56 -0600546 for (std::vector<Device::Format>::const_iterator it = dev.formats().begin(); it != dev.formats().end(); it++) {
547 if (memcmp(&it->format, &create_info_.format, sizeof(it->format)) == 0 && it->tiling == create_info_.tiling) {
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800548 format_features_ = it->features;
549 break;
550 }
551 }
552}
553
Karl Schultz6addd812016-02-02 17:17:23 -0700554VkMemoryRequirements Image::memory_requirements() const {
Chia-I Wu681d7a02015-07-03 13:44:34 +0800555 VkMemoryRequirements reqs;
556
Courtney Goeltzenleuchter06d89472015-10-20 16:40:38 -0600557 vkGetImageMemoryRequirements(device(), handle(), &reqs);
Chia-I Wu681d7a02015-07-03 13:44:34 +0800558
559 return reqs;
560}
561
Karl Schultz6addd812016-02-02 17:17:23 -0700562void Image::bind_memory(const DeviceMemory &mem, VkDeviceSize mem_offset) {
Mark Lobodzinski722841d2016-09-07 16:34:56 -0600563 EXPECT(vkBindImageMemory(device(), handle(), mem.handle(), mem_offset) == VK_SUCCESS);
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800564}
565
Mark Lobodzinski722841d2016-09-07 16:34:56 -0600566VkSubresourceLayout Image::subresource_layout(const VkImageSubresource &subres) const {
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600567 VkSubresourceLayout data;
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800568 size_t size = sizeof(data);
Courtney Goeltzenleuchter06d89472015-10-20 16:40:38 -0600569 vkGetImageSubresourceLayout(device(), handle(), &subres, &data);
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700570 if (size != sizeof(data)) memset(&data, 0, sizeof(data));
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800571
572 return data;
573}
574
Mark Lobodzinski722841d2016-09-07 16:34:56 -0600575VkSubresourceLayout Image::subresource_layout(const VkImageSubresourceLayers &subrescopy) const {
Courtney Goeltzenleuchter01ee1ca2015-09-10 16:41:13 -0600576 VkSubresourceLayout data;
Mark Lobodzinski722841d2016-09-07 16:34:56 -0600577 VkImageSubresource subres = subresource(subrescopy.aspectMask, subrescopy.mipLevel, subrescopy.baseArrayLayer);
Courtney Goeltzenleuchter01ee1ca2015-09-10 16:41:13 -0600578 size_t size = sizeof(data);
Courtney Goeltzenleuchter06d89472015-10-20 16:40:38 -0600579 vkGetImageSubresourceLayout(device(), handle(), &subres, &data);
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700580 if (size != sizeof(data)) memset(&data, 0, sizeof(data));
Courtney Goeltzenleuchter01ee1ca2015-09-10 16:41:13 -0600581
582 return data;
583}
584
Karl Schultz6addd812016-02-02 17:17:23 -0700585bool Image::transparent() const {
Mark Lobodzinski722841d2016-09-07 16:34:56 -0600586 return (create_info_.tiling == VK_IMAGE_TILING_LINEAR && create_info_.samples == VK_SAMPLE_COUNT_1_BIT &&
587 !(create_info_.usage & (VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT | VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT)));
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800588}
589
Tony Barbour67e99152015-07-10 14:10:27 -0600590NON_DISPATCHABLE_HANDLE_DTOR(ImageView, vkDestroyImageView)
Chia-I Wu3158bf32015-07-03 11:49:42 +0800591
Karl Schultz6addd812016-02-02 17:17:23 -0700592void ImageView::init(const Device &dev, const VkImageViewCreateInfo &info) {
Chia-I Wu3158bf32015-07-03 11:49:42 +0800593 NON_DISPATCHABLE_HANDLE_INIT(vkCreateImageView, dev, &info);
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800594}
595
Tony Barbour67e99152015-07-10 14:10:27 -0600596NON_DISPATCHABLE_HANDLE_DTOR(ShaderModule, vkDestroyShaderModule)
Chia-I Wu4d0c7922015-07-03 11:49:42 +0800597
Mark Lobodzinski722841d2016-09-07 16:34:56 -0600598void ShaderModule::init(const Device &dev, const VkShaderModuleCreateInfo &info) {
Chia-I Wu4d0c7922015-07-03 11:49:42 +0800599 NON_DISPATCHABLE_HANDLE_INIT(vkCreateShaderModule, dev, &info);
Courtney Goeltzenleuchteree4027d2015-06-28 13:01:17 -0600600}
601
Mark Lobodzinski722841d2016-09-07 16:34:56 -0600602VkResult ShaderModule::init_try(const Device &dev, const VkShaderModuleCreateInfo &info) {
Chia-I Wu4d0c7922015-07-03 11:49:42 +0800603 VkShaderModule mod;
604
Chia-I Wuf7458c52015-10-26 21:10:41 +0800605 VkResult err = vkCreateShaderModule(dev.handle(), &info, NULL, &mod);
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700606 if (err == VK_SUCCESS) NonDispHandle::init(dev.handle(), mod);
Courtney Goeltzenleuchteree4027d2015-06-28 13:01:17 -0600607
608 return err;
609}
610
Tony Barbour67e99152015-07-10 14:10:27 -0600611NON_DISPATCHABLE_HANDLE_DTOR(Pipeline, vkDestroyPipeline)
Chia-I Wu2ff72fd2015-07-03 11:49:42 +0800612
Mark Lobodzinski722841d2016-09-07 16:34:56 -0600613void Pipeline::init(const Device &dev, const VkGraphicsPipelineCreateInfo &info) {
Jon Ashburnc669cc62015-07-09 15:02:25 -0600614 VkPipelineCache cache;
615 VkPipelineCacheCreateInfo ci;
Karl Schultz6addd812016-02-02 17:17:23 -0700616 memset((void *)&ci, 0, sizeof(VkPipelineCacheCreateInfo));
Jon Ashburnc669cc62015-07-09 15:02:25 -0600617 ci.sType = VK_STRUCTURE_TYPE_PIPELINE_CACHE_CREATE_INFO;
Chia-I Wuf7458c52015-10-26 21:10:41 +0800618 VkResult err = vkCreatePipelineCache(dev.handle(), &ci, NULL, &cache);
Jon Ashburnc669cc62015-07-09 15:02:25 -0600619 if (err == VK_SUCCESS) {
Mark Lobodzinski722841d2016-09-07 16:34:56 -0600620 NON_DISPATCHABLE_HANDLE_INIT(vkCreateGraphicsPipelines, dev, cache, 1, &info);
Chia-I Wuf7458c52015-10-26 21:10:41 +0800621 vkDestroyPipelineCache(dev.handle(), cache, NULL);
Jon Ashburnc669cc62015-07-09 15:02:25 -0600622 }
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800623}
624
Mark Lobodzinski722841d2016-09-07 16:34:56 -0600625VkResult Pipeline::init_try(const Device &dev, const VkGraphicsPipelineCreateInfo &info) {
Chris Forbes95292b12015-05-25 11:13:26 +1200626 VkPipeline pipe;
Jon Ashburnc669cc62015-07-09 15:02:25 -0600627 VkPipelineCache cache;
628 VkPipelineCacheCreateInfo ci;
Karl Schultz6addd812016-02-02 17:17:23 -0700629 memset((void *)&ci, 0, sizeof(VkPipelineCacheCreateInfo));
Jon Ashburnc669cc62015-07-09 15:02:25 -0600630 ci.sType = VK_STRUCTURE_TYPE_PIPELINE_CACHE_CREATE_INFO;
Chia-I Wuf7458c52015-10-26 21:10:41 +0800631 VkResult err = vkCreatePipelineCache(dev.handle(), &ci, NULL, &cache);
Chia-I Wuf368b602015-07-03 10:41:20 +0800632 EXPECT(err == VK_SUCCESS);
Chris Forbes95292b12015-05-25 11:13:26 +1200633 if (err == VK_SUCCESS) {
Mark Lobodzinski722841d2016-09-07 16:34:56 -0600634 err = vkCreateGraphicsPipelines(dev.handle(), cache, 1, &info, NULL, &pipe);
Jon Ashburnc669cc62015-07-09 15:02:25 -0600635 if (err == VK_SUCCESS) {
Chia-I Wu2ff72fd2015-07-03 11:49:42 +0800636 NonDispHandle::init(dev.handle(), pipe);
Jon Ashburnc669cc62015-07-09 15:02:25 -0600637 }
Chia-I Wuf7458c52015-10-26 21:10:41 +0800638 vkDestroyPipelineCache(dev.handle(), cache, NULL);
Chris Forbes95292b12015-05-25 11:13:26 +1200639 }
640
641 return err;
642}
643
Mark Lobodzinski722841d2016-09-07 16:34:56 -0600644void Pipeline::init(const Device &dev, const VkComputePipelineCreateInfo &info) {
Jon Ashburnc669cc62015-07-09 15:02:25 -0600645 VkPipelineCache cache;
646 VkPipelineCacheCreateInfo ci;
Karl Schultz6addd812016-02-02 17:17:23 -0700647 memset((void *)&ci, 0, sizeof(VkPipelineCacheCreateInfo));
Jon Ashburnc669cc62015-07-09 15:02:25 -0600648 ci.sType = VK_STRUCTURE_TYPE_PIPELINE_CACHE_CREATE_INFO;
Chia-I Wuf7458c52015-10-26 21:10:41 +0800649 VkResult err = vkCreatePipelineCache(dev.handle(), &ci, NULL, &cache);
Jon Ashburnc669cc62015-07-09 15:02:25 -0600650 if (err == VK_SUCCESS) {
Mark Lobodzinski722841d2016-09-07 16:34:56 -0600651 NON_DISPATCHABLE_HANDLE_INIT(vkCreateComputePipelines, dev, cache, 1, &info);
Chia-I Wuf7458c52015-10-26 21:10:41 +0800652 vkDestroyPipelineCache(dev.handle(), cache, NULL);
Jon Ashburnc669cc62015-07-09 15:02:25 -0600653 }
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800654}
655
Tony Barbour67e99152015-07-10 14:10:27 -0600656NON_DISPATCHABLE_HANDLE_DTOR(PipelineLayout, vkDestroyPipelineLayout)
Chia-I Wufd46e7d2015-07-03 11:49:42 +0800657
Mark Lobodzinski722841d2016-09-07 16:34:56 -0600658void PipelineLayout::init(const Device &dev, VkPipelineLayoutCreateInfo &info,
659 const std::vector<const DescriptorSetLayout *> &layouts) {
Petr Kraus858bacd2017-12-01 23:10:08 +0100660 const std::vector<VkDescriptorSetLayout> layout_handles = MakeVkHandles<VkDescriptorSetLayout>(layouts);
Petr Kraus65ccc882017-12-03 15:36:03 +0100661 info.setLayoutCount = layout_handles.size();
Tony Barbour482c6092015-07-27 09:37:48 -0600662 info.pSetLayouts = layout_handles.data();
Chia-I Wufd46e7d2015-07-03 11:49:42 +0800663
664 NON_DISPATCHABLE_HANDLE_INIT(vkCreatePipelineLayout, dev, &info);
665}
666
Tony Barbour67e99152015-07-10 14:10:27 -0600667NON_DISPATCHABLE_HANDLE_DTOR(Sampler, vkDestroySampler)
Chia-I Wu8c721c62015-07-03 11:49:42 +0800668
Karl Schultz6addd812016-02-02 17:17:23 -0700669void Sampler::init(const Device &dev, const VkSamplerCreateInfo &info) {
Chia-I Wu8c721c62015-07-03 11:49:42 +0800670 NON_DISPATCHABLE_HANDLE_INIT(vkCreateSampler, dev, &info);
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800671}
672
Tony Barbour67e99152015-07-10 14:10:27 -0600673NON_DISPATCHABLE_HANDLE_DTOR(DescriptorSetLayout, vkDestroyDescriptorSetLayout)
Chia-I Wuafdfd7f2015-07-03 11:49:42 +0800674
Mark Lobodzinski722841d2016-09-07 16:34:56 -0600675void DescriptorSetLayout::init(const Device &dev, const VkDescriptorSetLayoutCreateInfo &info) {
Chia-I Wuafdfd7f2015-07-03 11:49:42 +0800676 NON_DISPATCHABLE_HANDLE_INIT(vkCreateDescriptorSetLayout, dev, &info);
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800677}
678
Tony Barbour67e99152015-07-10 14:10:27 -0600679NON_DISPATCHABLE_HANDLE_DTOR(DescriptorPool, vkDestroyDescriptorPool)
Chia-I Wuafdfd7f2015-07-03 11:49:42 +0800680
Mark Lobodzinski722841d2016-09-07 16:34:56 -0600681void DescriptorPool::init(const Device &dev, const VkDescriptorPoolCreateInfo &info) {
682 setDynamicUsage(info.flags & VK_DESCRIPTOR_POOL_CREATE_FREE_DESCRIPTOR_SET_BIT);
Courtney Goeltzenleuchterfe908d32015-09-16 16:12:45 -0600683 NON_DISPATCHABLE_HANDLE_INIT(vkCreateDescriptorPool, dev, &info);
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800684}
685
Mark Lobodzinski722841d2016-09-07 16:34:56 -0600686void DescriptorPool::reset() { EXPECT(vkResetDescriptorPool(device(), handle(), 0) == VK_SUCCESS); }
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800687
Mark Lobodzinski722841d2016-09-07 16:34:56 -0600688std::vector<DescriptorSet *> DescriptorPool::alloc_sets(const Device &dev,
689 const std::vector<const DescriptorSetLayout *> &layouts) {
Petr Kraus858bacd2017-12-01 23:10:08 +0100690 const std::vector<VkDescriptorSetLayout> layout_handles = MakeVkHandles<VkDescriptorSetLayout>(layouts);
Chia-I Wu11078b02015-01-04 16:27:24 +0800691
Chia-I Wuafdfd7f2015-07-03 11:49:42 +0800692 std::vector<VkDescriptorSet> set_handles;
693 set_handles.resize(layout_handles.size());
Chia-I Wu11078b02015-01-04 16:27:24 +0800694
Chia-I Wu3432a0c2015-10-27 18:04:07 +0800695 VkDescriptorSetAllocateInfo alloc_info = {};
Chia-I Wu00ce5402015-11-10 16:21:09 +0800696 alloc_info.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO;
Jon Ashburnf19916e2016-01-11 13:12:43 -0700697 alloc_info.descriptorSetCount = layout_handles.size();
Courtney Goeltzenleuchterbee18a92015-10-23 14:21:05 -0600698 alloc_info.descriptorPool = handle();
699 alloc_info.pSetLayouts = layout_handles.data();
Mark Lobodzinski722841d2016-09-07 16:34:56 -0600700 VkResult err = vkAllocateDescriptorSets(device(), &alloc_info, set_handles.data());
Cody Northrop1e4f8022015-08-03 12:47:29 -0600701 EXPECT(err == VK_SUCCESS);
Chia-I Wu11078b02015-01-04 16:27:24 +0800702
703 std::vector<DescriptorSet *> sets;
Mark Lobodzinski722841d2016-09-07 16:34:56 -0600704 for (std::vector<VkDescriptorSet>::const_iterator it = set_handles.begin(); it != set_handles.end(); it++) {
Chia-I Wu11078b02015-01-04 16:27:24 +0800705 // do descriptor sets need memories bound?
Cody Northropcdc72a42015-10-08 11:39:25 -0600706 DescriptorSet *descriptorSet = new DescriptorSet(dev, this, *it);
Mark Lobodzinski40f7f402015-04-16 11:44:05 -0500707 sets.push_back(descriptorSet);
Chia-I Wu11078b02015-01-04 16:27:24 +0800708 }
Chia-I Wu11078b02015-01-04 16:27:24 +0800709 return sets;
710}
711
Mark Lobodzinski722841d2016-09-07 16:34:56 -0600712std::vector<DescriptorSet *> DescriptorPool::alloc_sets(const Device &dev, const DescriptorSetLayout &layout, uint32_t count) {
713 return alloc_sets(dev, std::vector<const DescriptorSetLayout *>(count, &layout));
Chia-I Wu11078b02015-01-04 16:27:24 +0800714}
715
Mark Lobodzinski722841d2016-09-07 16:34:56 -0600716DescriptorSet *DescriptorPool::alloc_sets(const Device &dev, const DescriptorSetLayout &layout) {
Courtney Goeltzenleuchterbee18a92015-10-23 14:21:05 -0600717 std::vector<DescriptorSet *> set = alloc_sets(dev, layout, 1);
Chia-I Wu11078b02015-01-04 16:27:24 +0800718 return (set.empty()) ? NULL : set[0];
719}
720
Karl Schultz6addd812016-02-02 17:17:23 -0700721DescriptorSet::~DescriptorSet() {
Tony Barbour67e99152015-07-10 14:10:27 -0600722 if (initialized()) {
Cody Northropcdc72a42015-10-08 11:39:25 -0600723 // Only call vkFree* on sets allocated from pool with usage *_DYNAMIC
724 if (containing_pool_->getDynamicUsage()) {
Karl Schultz6addd812016-02-02 17:17:23 -0700725 VkDescriptorSet sets[1] = {handle()};
Mark Lobodzinski722841d2016-09-07 16:34:56 -0600726 EXPECT(vkFreeDescriptorSets(device(), containing_pool_->GetObj(), 1, sets) == VK_SUCCESS);
Cody Northropcdc72a42015-10-08 11:39:25 -0600727 }
Tony Barbour67e99152015-07-10 14:10:27 -0600728 }
729}
Chia-I Wuafdfd7f2015-07-03 11:49:42 +0800730
Chia-I Wu3432a0c2015-10-27 18:04:07 +0800731NON_DISPATCHABLE_HANDLE_DTOR(CommandPool, vkDestroyCommandPool)
Courtney Goeltzenleuchteree5d80b2015-07-10 19:50:17 -0600732
Karl Schultz6addd812016-02-02 17:17:23 -0700733void CommandPool::init(const Device &dev, const VkCommandPoolCreateInfo &info) {
Courtney Goeltzenleuchteree5d80b2015-07-10 19:50:17 -0600734 NON_DISPATCHABLE_HANDLE_INIT(vkCreateCommandPool, dev, &info);
735}
736
Karl Schultz6addd812016-02-02 17:17:23 -0700737CommandBuffer::~CommandBuffer() {
Courtney Goeltzenleuchterbee18a92015-10-23 14:21:05 -0600738 if (initialized()) {
Karl Schultz6addd812016-02-02 17:17:23 -0700739 VkCommandBuffer cmds[] = {handle()};
Courtney Goeltzenleuchterbee18a92015-10-23 14:21:05 -0600740 vkFreeCommandBuffers(dev_handle_, cmd_pool_, 1, cmds);
741 }
Chia-I Wube2b9172015-07-03 11:49:42 +0800742}
743
Mark Lobodzinski722841d2016-09-07 16:34:56 -0600744void CommandBuffer::init(const Device &dev, const VkCommandBufferAllocateInfo &info) {
Chia-I Wu3432a0c2015-10-27 18:04:07 +0800745 VkCommandBuffer cmd;
Chia-I Wube2b9172015-07-03 11:49:42 +0800746
Chia-I Wu3432a0c2015-10-27 18:04:07 +0800747 // Make sure commandPool is set
748 assert(info.commandPool);
Courtney Goeltzenleuchterd8e68bb2015-07-13 12:53:32 -0600749
Mark Lobodzinski722841d2016-09-07 16:34:56 -0600750 if (EXPECT(vkAllocateCommandBuffers(dev.handle(), &info, &cmd) == VK_SUCCESS)) {
Chia-I Wube2b9172015-07-03 11:49:42 +0800751 Handle::init(cmd);
752 dev_handle_ = dev.handle();
Chia-I Wu3432a0c2015-10-27 18:04:07 +0800753 cmd_pool_ = info.commandPool;
Chia-I Wube2b9172015-07-03 11:49:42 +0800754 }
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800755}
756
Mark Lobodzinski722841d2016-09-07 16:34:56 -0600757void CommandBuffer::begin(const VkCommandBufferBeginInfo *info) { EXPECT(vkBeginCommandBuffer(handle(), info) == VK_SUCCESS); }
Jeremy Hayesd65ae082015-01-14 16:17:08 -0700758
Karl Schultz6addd812016-02-02 17:17:23 -0700759void CommandBuffer::begin() {
Chia-I Wu3432a0c2015-10-27 18:04:07 +0800760 VkCommandBufferBeginInfo info = {};
Jon Ashburnf19916e2016-01-11 13:12:43 -0700761 VkCommandBufferInheritanceInfo hinfo = {};
Chia-I Wu3432a0c2015-10-27 18:04:07 +0800762 info.flags = VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT;
763 info.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
Jon Ashburnf19916e2016-01-11 13:12:43 -0700764 info.pInheritanceInfo = &hinfo;
765 hinfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_INHERITANCE_INFO;
766 hinfo.pNext = NULL;
767 hinfo.renderPass = VK_NULL_HANDLE;
768 hinfo.subpass = 0;
769 hinfo.framebuffer = VK_NULL_HANDLE;
770 hinfo.occlusionQueryEnable = VK_FALSE;
771 hinfo.queryFlags = 0;
772 hinfo.pipelineStatistics = 0;
Jeremy Hayesd65ae082015-01-14 16:17:08 -0700773
774 begin(&info);
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800775}
776
Mark Lobodzinski722841d2016-09-07 16:34:56 -0600777void CommandBuffer::end() { EXPECT(vkEndCommandBuffer(handle()) == VK_SUCCESS); }
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800778
Mark Lobodzinski722841d2016-09-07 16:34:56 -0600779void CommandBuffer::reset(VkCommandBufferResetFlags flags) { EXPECT(vkResetCommandBuffer(handle(), flags) == VK_SUCCESS); }
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800780
Petr Kraus13c98a62017-12-09 00:22:39 +0100781} // namespace vk_testing