blob: da1c0d87d18d2002e4fcdf72160607ef7d42f5f8 [file] [log] [blame]
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001// VK tests
Chia-I Wuf1e2e992014-12-27 14:12:52 +08002//
3// Copyright (C) 2014 LunarG, Inc.
4//
5// Permission is hereby granted, free of charge, to any person obtaining a
6// copy of this software and associated documentation files (the "Software"),
7// to deal in the Software without restriction, including without limitation
8// the rights to use, copy, modify, merge, publish, distribute, sublicense,
9// and/or sell copies of the Software, and to permit persons to whom the
10// Software is furnished to do so, subject to the following conditions:
11//
12// The above copyright notice and this permission notice shall be included
13// in all copies or substantial portions of the Software.
14//
15// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21// DEALINGS IN THE SOFTWARE.
22
23#include <iostream>
24#include <string.h> // memset(), memcmp()
Courtney Goeltzenleuchter992fb4f2015-04-19 19:07:33 -060025#include <assert.h>
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -060026#include "vktestbinding.h"
Chia-I Wuf1e2e992014-12-27 14:12:52 +080027
28namespace {
Chia-I Wuf1e2e992014-12-27 14:12:52 +080029
Courtney Goeltzenleuchter992fb4f2015-04-19 19:07:33 -060030#define DERIVED_OBJECT_TYPE_INIT(create_func, dev, vk_object_type, ...) \
31 do { \
32 obj_type obj; \
33 dev_ = &dev; \
34 if (EXPECT(create_func(dev.obj(), __VA_ARGS__, &obj) == VK_SUCCESS)) \
35 base_type::init(obj, vk_object_type); \
Mike Stroyanb050c682015-04-17 12:36:38 -060036 } while (0)
37
Chia-I Wuf1e2e992014-12-27 14:12:52 +080038#define STRINGIFY(x) #x
39#define EXPECT(expr) ((expr) ? true : expect_failure(STRINGIFY(expr), __FILE__, __LINE__, __FUNCTION__))
Mark Lobodzinski40f7f402015-04-16 11:44:05 -050040
Chia-I Wuf1e2e992014-12-27 14:12:52 +080041
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -060042vk_testing::ErrorCallback error_callback;
Chia-I Wuf1e2e992014-12-27 14:12:52 +080043
44bool expect_failure(const char *expr, const char *file, unsigned int line, const char *function)
45{
46 if (error_callback) {
47 error_callback(expr, file, line, function);
48 } else {
49 std::cerr << file << ":" << line << ": " << function <<
50 ": Expectation `" << expr << "' failed.\n";
51 }
52
53 return false;
54}
55
56template<class T, class S>
57std::vector<T> make_objects(const std::vector<S> &v)
58{
59 std::vector<T> objs;
60 objs.reserve(v.size());
61 for (typename std::vector<S>::const_iterator it = v.begin(); it != v.end(); it++)
62 objs.push_back((*it)->obj());
63 return objs;
64}
65
66template<typename T>
Tony Barbour59a47322015-06-24 16:06:58 -060067std::vector<T> get_memory_reqs(VkDevice device, VkObjectType obj_type, VkObject obj, size_t min_elems)
Chia-I Wuf1e2e992014-12-27 14:12:52 +080068{
69 std::vector<T> info;
Tony Barbour59a47322015-06-24 16:06:58 -060070
71 info.resize((min_elems > 0)?min_elems:1);
72 if (!EXPECT(vkGetObjectMemoryRequirements(device, obj_type, obj, &info[0]) == VK_SUCCESS))
73 info.clear();
Chia-I Wuf1e2e992014-12-27 14:12:52 +080074
75 if (info.size() < min_elems)
76 info.resize(min_elems);
77
78 return info;
79}
Chia-I Wuf1e2e992014-12-27 14:12:52 +080080} // namespace
81
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -060082namespace vk_testing {
Chia-I Wuf1e2e992014-12-27 14:12:52 +080083
84void set_error_callback(ErrorCallback callback)
85{
86 error_callback = callback;
87}
88
Tony Barbourd1c35722015-04-16 15:59:00 -060089VkPhysicalDeviceProperties PhysicalGpu::properties() const
Chia-I Wuf1e2e992014-12-27 14:12:52 +080090{
Tony Barbour59a47322015-06-24 16:06:58 -060091 VkPhysicalDeviceProperties info;
92
93 EXPECT(vkGetPhysicalDeviceProperties(gpu_, &info) == VK_SUCCESS);
94
95 return info;
Chia-I Wuf1e2e992014-12-27 14:12:52 +080096}
97
Tony Barbourd1c35722015-04-16 15:59:00 -060098VkPhysicalDevicePerformance PhysicalGpu::performance() const
Chia-I Wuf1e2e992014-12-27 14:12:52 +080099{
Tony Barbour59a47322015-06-24 16:06:58 -0600100 VkPhysicalDevicePerformance info;
101
102 EXPECT(vkGetPhysicalDevicePerformance(gpu_, &info) == VK_SUCCESS);
103
104 return info;
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800105}
106
Tony Barbourd1c35722015-04-16 15:59:00 -0600107std::vector<VkPhysicalDeviceQueueProperties> PhysicalGpu::queue_properties() const
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800108{
Tony Barbour59a47322015-06-24 16:06:58 -0600109 std::vector<VkPhysicalDeviceQueueProperties> info;
110 uint32_t count;
111
112 if (EXPECT(vkGetPhysicalDeviceQueueCount(gpu_, &count) == VK_SUCCESS)) {
113 info.resize(count);
114 if (!EXPECT(vkGetPhysicalDeviceQueueProperties(gpu_, count, &info[0]) == VK_SUCCESS))
115 info.clear();
116 }
117
118 return info;
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800119}
120
Tony Barbourd1c35722015-04-16 15:59:00 -0600121VkPhysicalDeviceMemoryProperties PhysicalGpu::memory_properties() const
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800122{
Tony Barbour59a47322015-06-24 16:06:58 -0600123 VkPhysicalDeviceMemoryProperties info;
124
125 EXPECT(vkGetPhysicalDeviceMemoryProperties(gpu_, &info) == VK_SUCCESS);
126
127 return info;
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800128}
129
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600130void PhysicalGpu::add_extension_dependencies(
131 uint32_t dependency_count,
132 VkExtensionProperties *depencency_props,
133 std::vector<VkExtensionProperties> &ext_list)
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800134{
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600135 for (uint32_t i = 0; i < dependency_count; i++) {
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800136
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600137 }
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800138}
139
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600140std::vector<VkExtensionProperties> PhysicalGpu::extensions() const
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800141{
Tobin Ehlis9430c782015-04-16 18:04:57 -0600142 // Extensions to enable
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800143 static const char *known_exts[] = {
Chia-I Wuf8693382015-04-16 22:02:10 +0800144 "VK_WSI_LunarG",
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800145 };
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600146 std::vector<VkExtensionProperties> exts;
Tobin Ehlis9430c782015-04-16 18:04:57 -0600147 uint32_t extCount = 0;
Tony Barbour59a47322015-06-24 16:06:58 -0600148 if (!EXPECT(vkGetGlobalExtensionCount(&extCount) == VK_SUCCESS))
Tobin Ehlis9430c782015-04-16 18:04:57 -0600149 return exts;
150
151 VkExtensionProperties extProp;
Tobin Ehlis9430c782015-04-16 18:04:57 -0600152 // TODO : Need to update this if/when we have more than 1 extension to enable
153 for (uint32_t i = 0; i < extCount; i++) {
Tony Barbour59a47322015-06-24 16:06:58 -0600154 if (!EXPECT(vkGetGlobalExtensionProperties(i, &extProp) == VK_SUCCESS))
Tobin Ehlis9430c782015-04-16 18:04:57 -0600155 return exts;
156
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600157 if (!strcmp(known_exts[0], extProp.name))
158 exts.push_back(extProp);
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800159 }
160
161 return exts;
162}
163
Courtney Goeltzenleuchter992fb4f2015-04-19 19:07:33 -0600164void BaseObject::init(VkObject obj, VkObjectType type, bool own)
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800165{
166 EXPECT(!initialized());
Courtney Goeltzenleuchter992fb4f2015-04-19 19:07:33 -0600167 reinit(obj, type, own);
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800168}
169
Courtney Goeltzenleuchter992fb4f2015-04-19 19:07:33 -0600170void BaseObject::reinit(VkObject obj, VkObjectType type, bool own)
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800171{
172 obj_ = obj;
Courtney Goeltzenleuchter992fb4f2015-04-19 19:07:33 -0600173 object_type_ = type;
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800174 own_obj_ = own;
175}
176
Courtney Goeltzenleuchter992fb4f2015-04-19 19:07:33 -0600177uint32_t Object::memory_allocation_count() const
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800178{
Mark Lobodzinski23065352015-05-29 09:32:35 -0500179 return 1;
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800180}
181
Courtney Goeltzenleuchter992fb4f2015-04-19 19:07:33 -0600182std::vector<VkMemoryRequirements> Object::memory_requirements() const
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800183{
Mark Lobodzinski23065352015-05-29 09:32:35 -0500184 uint32_t num_allocations = 1;
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600185 std::vector<VkMemoryRequirements> info =
Tony Barbour59a47322015-06-24 16:06:58 -0600186 get_memory_reqs<VkMemoryRequirements>(dev_->obj(), type(), obj(), 0);
Jon Ashburn7e781952015-01-16 09:37:43 -0700187 EXPECT(info.size() == num_allocations);
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800188 if (info.size() == 1 && !info[0].size)
189 info.clear();
190
191 return info;
192}
193
Courtney Goeltzenleuchter992fb4f2015-04-19 19:07:33 -0600194void Object::init(VkObject obj, VkObjectType object_type, bool own)
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800195{
Courtney Goeltzenleuchter992fb4f2015-04-19 19:07:33 -0600196 BaseObject::init(obj, object_type, own);
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800197 mem_alloc_count_ = memory_allocation_count();
198}
199
Courtney Goeltzenleuchter992fb4f2015-04-19 19:07:33 -0600200void Object::reinit(VkObject obj, VkObjectType object_type, bool own)
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800201{
202 cleanup();
Courtney Goeltzenleuchter992fb4f2015-04-19 19:07:33 -0600203 BaseObject::reinit(obj, object_type, own);
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800204 mem_alloc_count_ = memory_allocation_count();
205}
206
207void Object::cleanup()
208{
209 if (!initialized())
210 return;
211
Courtney Goeltzenleuchterde69c412015-06-14 21:31:09 -0600212 if (own())
213 EXPECT(vkDestroyObject(dev_->obj(), type(), obj()) == VK_SUCCESS);
214
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800215 if (internal_mems_) {
216 delete[] internal_mems_;
217 internal_mems_ = NULL;
218 primary_mem_ = NULL;
219 }
220
221 mem_alloc_count_ = 0;
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800222}
223
Mark Lobodzinski23065352015-05-29 09:32:35 -0500224void Object::bind_memory(const GpuMemory &mem, VkDeviceSize mem_offset)
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800225{
Tony Barbour44bb3ab2015-04-09 16:00:18 -0600226 bound = true;
Mark Lobodzinski23065352015-05-29 09:32:35 -0500227 EXPECT(vkBindObjectMemory(dev_->obj(), type(), obj(), mem.obj(), mem_offset) == VK_SUCCESS);
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800228}
229
Courtney Goeltzenleuchter30a39452015-04-22 10:02:41 -0600230void Object::alloc_memory()
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800231{
232 if (!EXPECT(!internal_mems_) || !mem_alloc_count_)
233 return;
234
235 internal_mems_ = new GpuMemory[mem_alloc_count_];
236
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600237 const std::vector<VkMemoryRequirements> mem_reqs = memory_requirements();
Courtney Goeltzenleuchter95487bc2015-04-14 18:48:46 -0600238 VkMemoryAllocInfo info, *next_info = NULL;
Jon Ashburn8a5da752015-01-19 15:00:26 -0700239
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800240 for (int i = 0; i < mem_reqs.size(); i++) {
Jon Ashburn25bbe462015-01-20 08:50:12 -0700241 info = GpuMemory::alloc_info(mem_reqs[i], next_info);
Mark Lobodzinskie39f6952015-04-16 08:52:00 -0500242 primary_mem_ = &internal_mems_[i];
Courtney Goeltzenleuchter30a39452015-04-22 10:02:41 -0600243 internal_mems_[i].init(*dev_, info);
Mark Lobodzinski23065352015-05-29 09:32:35 -0500244 bind_memory(internal_mems_[i], 0);
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800245 }
246}
247
Tony Barbour4c97d7a2015-04-22 15:10:33 -0600248void Object::alloc_memory(VkMemoryPropertyFlags &reqs)
249{
250 if (!EXPECT(!internal_mems_) || !mem_alloc_count_)
251 return;
252
253 internal_mems_ = new GpuMemory[mem_alloc_count_];
254
255 std::vector<VkMemoryRequirements> mem_reqs = memory_requirements();
256 VkMemoryAllocInfo info, *next_info = NULL;
257
258 for (int i = 0; i < mem_reqs.size(); i++) {
259 mem_reqs[i].memPropsRequired |= reqs;
260 info = GpuMemory::alloc_info(mem_reqs[i], next_info);
261 primary_mem_ = &internal_mems_[i];
262 internal_mems_[i].init(*dev_, info);
Mark Lobodzinski23065352015-05-29 09:32:35 -0500263 bind_memory(internal_mems_[i], 0);
Tony Barbour4c97d7a2015-04-22 15:10:33 -0600264 }
265}
266
Courtney Goeltzenleuchter30a39452015-04-22 10:02:41 -0600267void Object::alloc_memory(const std::vector<VkDeviceMemory> &mems)
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800268{
269 if (!EXPECT(!internal_mems_) || !mem_alloc_count_)
270 return;
271
272 internal_mems_ = new GpuMemory[mem_alloc_count_];
273
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600274 const std::vector<VkMemoryRequirements> mem_reqs = memory_requirements();
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800275 if (!EXPECT(mem_reqs.size() == mems.size()))
276 return;
277
278 for (int i = 0; i < mem_reqs.size(); i++) {
279 primary_mem_ = &internal_mems_[i];
280
Courtney Goeltzenleuchter30a39452015-04-22 10:02:41 -0600281 internal_mems_[i].init(*dev_, mems[i]);
Mark Lobodzinski23065352015-05-29 09:32:35 -0500282 bind_memory(internal_mems_[i], 0);
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800283 }
284}
285
Tony Barbourd1c35722015-04-16 15:59:00 -0600286std::vector<VkDeviceMemory> Object::memories() const
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800287{
Tony Barbourd1c35722015-04-16 15:59:00 -0600288 std::vector<VkDeviceMemory> mems;
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800289 if (internal_mems_) {
290 mems.reserve(mem_alloc_count_);
291 for (uint32_t i = 0; i < mem_alloc_count_; i++)
292 mems.push_back(internal_mems_[i].obj());
293 }
294
295 return mems;
296}
297
298Device::~Device()
299{
300 if (!initialized())
301 return;
302
303 for (int i = 0; i < QUEUE_COUNT; i++) {
304 for (std::vector<Queue *>::iterator it = queues_[i].begin(); it != queues_[i].end(); it++)
305 delete *it;
306 queues_[i].clear();
307 }
308
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600309 EXPECT(vkDestroyDevice(obj()) == VK_SUCCESS);
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800310}
311
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600312void Device::init(std::vector<VkExtensionProperties> extensions)
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800313{
314 // request all queues
Tony Barbourd1c35722015-04-16 15:59:00 -0600315 const std::vector<VkPhysicalDeviceQueueProperties> queue_props = gpu_.queue_properties();
Courtney Goeltzenleuchter95487bc2015-04-14 18:48:46 -0600316 std::vector<VkDeviceQueueCreateInfo> queue_info;
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800317 queue_info.reserve(queue_props.size());
318 for (int i = 0; i < queue_props.size(); i++) {
Courtney Goeltzenleuchter95487bc2015-04-14 18:48:46 -0600319 VkDeviceQueueCreateInfo qi = {};
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800320 qi.queueNodeIndex = i;
321 qi.queueCount = queue_props[i].queueCount;
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600322 if (queue_props[i].queueFlags & VK_QUEUE_GRAPHICS_BIT) {
Courtney Goeltzenleuchter18248e62015-03-05 18:09:39 -0700323 graphics_queue_node_index_ = i;
324 }
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800325 queue_info.push_back(qi);
326 }
327
Courtney Goeltzenleuchter95487bc2015-04-14 18:48:46 -0600328 VkDeviceCreateInfo dev_info = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600329 dev_info.sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO;
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600330 dev_info.pNext = NULL;
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800331 dev_info.queueRecordCount = queue_info.size();
332 dev_info.pRequestedQueues = &queue_info[0];
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600333 dev_info.extensionCount = extensions.size();
334 dev_info.pEnabledExtensions = &extensions[0];
Jon Ashburn9fc13ba2015-05-18 09:06:15 -0600335 dev_info.flags = 0;
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800336
337 init(dev_info);
338}
339
Courtney Goeltzenleuchter95487bc2015-04-14 18:48:46 -0600340void Device::init(const VkDeviceCreateInfo &info)
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800341{
Courtney Goeltzenleuchter992fb4f2015-04-19 19:07:33 -0600342 VkDevice obj;
343 if (EXPECT(vkCreateDevice(gpu_.obj(), &info, &obj) == VK_SUCCESS)) {
344 base_type::init(obj, VK_OBJECT_TYPE_DEVICE);
345 }
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800346
347 init_queues();
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800348 init_formats();
349}
350
351void Device::init_queues()
352{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600353 VkResult err;
Courtney Goeltzenleuchter18248e62015-03-05 18:09:39 -0700354 uint32_t queue_node_count;
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800355
Tony Barbour59a47322015-06-24 16:06:58 -0600356 err = vkGetPhysicalDeviceQueueCount(gpu_.obj(), &queue_node_count);
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600357 EXPECT(err == VK_SUCCESS);
Courtney Goeltzenleuchter18248e62015-03-05 18:09:39 -0700358 EXPECT(queue_node_count >= 1);
359
Tony Barbour3d69c9e2015-05-20 16:53:31 -0600360 VkPhysicalDeviceQueueProperties* queue_props = new VkPhysicalDeviceQueueProperties[queue_node_count];
Courtney Goeltzenleuchter18248e62015-03-05 18:09:39 -0700361
Tony Barbour59a47322015-06-24 16:06:58 -0600362 err = vkGetPhysicalDeviceQueueProperties(gpu_.obj(), queue_node_count, queue_props);
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600363 EXPECT(err == VK_SUCCESS);
Courtney Goeltzenleuchter18248e62015-03-05 18:09:39 -0700364
Tony Barbour7ea6aa22015-05-22 09:44:58 -0600365 for (uint32_t i = 0; i < queue_node_count; i++) {
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600366 VkQueue queue;
Courtney Goeltzenleuchter18248e62015-03-05 18:09:39 -0700367
Tony Barbour7ea6aa22015-05-22 09:44:58 -0600368 for (uint32_t j = 0; j < queue_props[i].queueCount; j++) {
Mark Lobodzinski40f7f402015-04-16 11:44:05 -0500369 // TODO: Need to add support for separate MEMMGR and work queues, including synchronization
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600370 err = vkGetDeviceQueue(obj(), i, j, &queue);
371 EXPECT(err == VK_SUCCESS);
Courtney Goeltzenleuchter18248e62015-03-05 18:09:39 -0700372
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600373 if (queue_props[i].queueFlags & VK_QUEUE_GRAPHICS_BIT) {
Courtney Goeltzenleuchter18248e62015-03-05 18:09:39 -0700374 queues_[GRAPHICS].push_back(new Queue(queue));
375 }
376
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600377 if (queue_props[i].queueFlags & VK_QUEUE_COMPUTE_BIT) {
Courtney Goeltzenleuchter18248e62015-03-05 18:09:39 -0700378 queues_[COMPUTE].push_back(new Queue(queue));
379 }
380
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600381 if (queue_props[i].queueFlags & VK_QUEUE_DMA_BIT) {
Courtney Goeltzenleuchter18248e62015-03-05 18:09:39 -0700382 queues_[DMA].push_back(new Queue(queue));
383 }
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800384 }
385 }
386
Chris Forbes02038792015-06-04 10:49:27 +1200387 delete[] queue_props;
Tony Barbour3d69c9e2015-05-20 16:53:31 -0600388
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800389 EXPECT(!queues_[GRAPHICS].empty() || !queues_[COMPUTE].empty());
390}
391
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800392void Device::init_formats()
393{
Tony Barbourd1c35722015-04-16 15:59:00 -0600394 for (int f = VK_FORMAT_BEGIN_RANGE; f <= VK_FORMAT_END_RANGE; f++) {
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600395 const VkFormat fmt = static_cast<VkFormat>(f);
396 const VkFormatProperties props = format_properties(fmt);
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800397
Jeremy Hayesa058eee2015-01-23 08:51:43 -0700398 if (props.linearTilingFeatures) {
Tony Barbourd1c35722015-04-16 15:59:00 -0600399 const Format tmp = { fmt, VK_IMAGE_TILING_LINEAR, props.linearTilingFeatures };
Jeremy Hayesa058eee2015-01-23 08:51:43 -0700400 formats_.push_back(tmp);
401 }
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800402
Jeremy Hayesa058eee2015-01-23 08:51:43 -0700403 if (props.optimalTilingFeatures) {
Tony Barbourd1c35722015-04-16 15:59:00 -0600404 const Format tmp = { fmt, VK_IMAGE_TILING_OPTIMAL, props.optimalTilingFeatures };
Jeremy Hayesa058eee2015-01-23 08:51:43 -0700405 formats_.push_back(tmp);
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800406 }
407 }
408
409 EXPECT(!formats_.empty());
410}
411
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600412VkFormatProperties Device::format_properties(VkFormat format)
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800413{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600414 VkFormatProperties data;
Chris Forbesbc0bb772015-06-21 22:55:02 +1200415 if (!EXPECT(vkGetPhysicalDeviceFormatInfo(gpu().obj(), format, &data) == VK_SUCCESS))
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800416 memset(&data, 0, sizeof(data));
417
418 return data;
419}
420
421void Device::wait()
422{
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600423 EXPECT(vkDeviceWaitIdle(obj()) == VK_SUCCESS);
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800424}
425
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600426VkResult Device::wait(const std::vector<const Fence *> &fences, bool wait_all, uint64_t timeout)
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800427{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600428 const std::vector<VkFence> fence_objs = make_objects<VkFence>(fences);
429 VkResult err = vkWaitForFences(obj(), fence_objs.size(), &fence_objs[0], wait_all, timeout);
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600430 EXPECT(err == VK_SUCCESS || err == VK_TIMEOUT);
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800431
432 return err;
433}
434
Chia-I Wu9d00ed72015-05-25 16:27:55 +0800435VkResult Device::update_descriptor_sets(const std::vector<VkWriteDescriptorSet> &writes, const std::vector<VkCopyDescriptorSet> &copies)
436{
437 return vkUpdateDescriptorSets(obj(), writes.size(), &writes[0], copies.size(), &copies[0]);
438}
439
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600440void Queue::submit(const std::vector<const CmdBuffer *> &cmds, Fence &fence)
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800441{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600442 const std::vector<VkCmdBuffer> cmd_objs = make_objects<VkCmdBuffer>(cmds);
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600443 EXPECT(vkQueueSubmit(obj(), cmd_objs.size(), &cmd_objs[0], fence.obj()) == VK_SUCCESS);
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800444}
445
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600446void Queue::submit(const CmdBuffer &cmd, Fence &fence)
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800447{
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600448 submit(std::vector<const CmdBuffer*>(1, &cmd), fence);
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800449}
450
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600451void Queue::submit(const CmdBuffer &cmd)
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800452{
453 Fence fence;
Courtney Goeltzenleuchter97b75232015-04-07 17:13:38 -0600454 submit(cmd, fence);
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800455}
456
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800457void Queue::wait()
458{
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600459 EXPECT(vkQueueWaitIdle(obj()) == VK_SUCCESS);
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800460}
461
Courtney Goeltzenleuchterebb95842015-03-25 17:14:29 -0600462void Queue::signal_semaphore(Semaphore &sem)
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800463{
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600464 EXPECT(vkQueueSignalSemaphore(obj(), sem.obj()) == VK_SUCCESS);
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800465}
466
Courtney Goeltzenleuchterebb95842015-03-25 17:14:29 -0600467void Queue::wait_semaphore(Semaphore &sem)
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800468{
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600469 EXPECT(vkQueueWaitSemaphore(obj(), sem.obj()) == VK_SUCCESS);
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800470}
471
472GpuMemory::~GpuMemory()
473{
474 if (initialized() && own())
Courtney Goeltzenleuchter992fb4f2015-04-19 19:07:33 -0600475 EXPECT(vkFreeMemory(dev_->obj(), obj()) == VK_SUCCESS);
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800476}
477
Courtney Goeltzenleuchter95487bc2015-04-14 18:48:46 -0600478void GpuMemory::init(const Device &dev, const VkMemoryAllocInfo &info)
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800479{
Courtney Goeltzenleuchter992fb4f2015-04-19 19:07:33 -0600480 DERIVED_OBJECT_TYPE_INIT(vkAllocMemory, dev, VK_OBJECT_TYPE_DEVICE_MEMORY, &info);
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800481}
482
Courtney Goeltzenleuchter992fb4f2015-04-19 19:07:33 -0600483void GpuMemory::init(const Device &dev, VkDeviceMemory mem)
484{
485 dev_ = &dev;
486 BaseObject::init(mem, VK_OBJECT_TYPE_DEVICE_MEMORY, false);
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800487}
488
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600489const void *GpuMemory::map(VkFlags flags) const
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800490{
491 void *data;
Courtney Goeltzenleuchter992fb4f2015-04-19 19:07:33 -0600492 if (!EXPECT(vkMapMemory(dev_->obj(), obj(), 0 ,0, flags, &data) == VK_SUCCESS))
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800493 data = NULL;
494
495 return data;
496}
497
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600498void *GpuMemory::map(VkFlags flags)
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800499{
500 void *data;
Courtney Goeltzenleuchter992fb4f2015-04-19 19:07:33 -0600501 if (!EXPECT(vkMapMemory(dev_->obj(), obj(), 0, 0, flags, &data) == VK_SUCCESS))
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800502 data = NULL;
503
504 return data;
505}
506
507void GpuMemory::unmap() const
508{
Courtney Goeltzenleuchter992fb4f2015-04-19 19:07:33 -0600509 EXPECT(vkUnmapMemory(dev_->obj(), obj()) == VK_SUCCESS);
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800510}
511
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600512void Fence::init(const Device &dev, const VkFenceCreateInfo &info)
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800513{
Courtney Goeltzenleuchter992fb4f2015-04-19 19:07:33 -0600514 DERIVED_OBJECT_TYPE_INIT(vkCreateFence, dev, VK_OBJECT_TYPE_FENCE, &info);
Courtney Goeltzenleuchter30a39452015-04-22 10:02:41 -0600515 alloc_memory();
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800516}
517
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600518void Semaphore::init(const Device &dev, const VkSemaphoreCreateInfo &info)
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800519{
Courtney Goeltzenleuchter992fb4f2015-04-19 19:07:33 -0600520 DERIVED_OBJECT_TYPE_INIT(vkCreateSemaphore, dev, VK_OBJECT_TYPE_SEMAPHORE, &info);
Courtney Goeltzenleuchter30a39452015-04-22 10:02:41 -0600521 alloc_memory();
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800522}
523
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600524void Event::init(const Device &dev, const VkEventCreateInfo &info)
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800525{
Courtney Goeltzenleuchter992fb4f2015-04-19 19:07:33 -0600526 DERIVED_OBJECT_TYPE_INIT(vkCreateEvent, dev, VK_OBJECT_TYPE_EVENT, &info);
Courtney Goeltzenleuchter30a39452015-04-22 10:02:41 -0600527 alloc_memory();
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800528}
529
530void Event::set()
531{
Courtney Goeltzenleuchter992fb4f2015-04-19 19:07:33 -0600532 EXPECT(vkSetEvent(dev_->obj(), obj()) == VK_SUCCESS);
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800533}
534
535void Event::reset()
536{
Courtney Goeltzenleuchter992fb4f2015-04-19 19:07:33 -0600537 EXPECT(vkResetEvent(dev_->obj(), obj()) == VK_SUCCESS);
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800538}
539
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600540void QueryPool::init(const Device &dev, const VkQueryPoolCreateInfo &info)
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800541{
Courtney Goeltzenleuchter992fb4f2015-04-19 19:07:33 -0600542 DERIVED_OBJECT_TYPE_INIT(vkCreateQueryPool, dev, VK_OBJECT_TYPE_QUERY_POOL, &info);
Courtney Goeltzenleuchter30a39452015-04-22 10:02:41 -0600543 alloc_memory();
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800544}
545
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600546VkResult QueryPool::results(uint32_t start, uint32_t count, size_t size, void *data)
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800547{
548 size_t tmp = size;
Courtney Goeltzenleuchter992fb4f2015-04-19 19:07:33 -0600549 VkResult err = vkGetQueryPoolResults(dev_->obj(), obj(), start, count, &tmp, data, 0);
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600550 if (err == VK_SUCCESS) {
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800551 if (!EXPECT(tmp == size))
552 memset(data, 0, size);
553 } else {
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600554 EXPECT(err == VK_NOT_READY);
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800555 }
556
557 return err;
558}
559
Courtney Goeltzenleuchter95487bc2015-04-14 18:48:46 -0600560void Buffer::init(const Device &dev, const VkBufferCreateInfo &info)
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800561{
562 init_no_mem(dev, info);
Courtney Goeltzenleuchter30a39452015-04-22 10:02:41 -0600563 alloc_memory();
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800564}
565
Tony Barbour4c97d7a2015-04-22 15:10:33 -0600566void Buffer::init(const Device &dev, const VkBufferCreateInfo &info, VkMemoryPropertyFlags &reqs)
567{
568 init_no_mem(dev, info);
569 alloc_memory(reqs);
570}
571
Courtney Goeltzenleuchter95487bc2015-04-14 18:48:46 -0600572void Buffer::init_no_mem(const Device &dev, const VkBufferCreateInfo &info)
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800573{
Courtney Goeltzenleuchter992fb4f2015-04-19 19:07:33 -0600574 DERIVED_OBJECT_TYPE_INIT(vkCreateBuffer, dev, VK_OBJECT_TYPE_BUFFER, &info);
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800575 create_info_ = info;
576}
577
Mark Lobodzinski23065352015-05-29 09:32:35 -0500578void Buffer::bind_memory(VkDeviceSize offset, VkDeviceSize size,
Mark Lobodzinski942b1722015-05-11 17:21:15 -0500579 const GpuMemory &mem, VkDeviceSize mem_offset)
580{
581 VkQueue queue = dev_->graphics_queues()[0]->obj();
Mark Lobodzinski23065352015-05-29 09:32:35 -0500582 EXPECT(vkQueueBindSparseBufferMemory(queue, obj(), offset, size, mem.obj(), mem_offset) == VK_SUCCESS);
Mark Lobodzinski942b1722015-05-11 17:21:15 -0500583}
584
Courtney Goeltzenleuchter95487bc2015-04-14 18:48:46 -0600585void BufferView::init(const Device &dev, const VkBufferViewCreateInfo &info)
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800586{
Courtney Goeltzenleuchter992fb4f2015-04-19 19:07:33 -0600587 DERIVED_OBJECT_TYPE_INIT(vkCreateBufferView, dev, VK_OBJECT_TYPE_BUFFER_VIEW, &info);
Courtney Goeltzenleuchter30a39452015-04-22 10:02:41 -0600588 alloc_memory();
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800589}
590
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600591void Image::init(const Device &dev, const VkImageCreateInfo &info)
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800592{
593 init_no_mem(dev, info);
Courtney Goeltzenleuchter30a39452015-04-22 10:02:41 -0600594 alloc_memory();
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800595}
596
Tony Barbour4c97d7a2015-04-22 15:10:33 -0600597void Image::init(const Device &dev, const VkImageCreateInfo &info, VkMemoryPropertyFlags &reqs)
598{
599 init_no_mem(dev, info);
600 alloc_memory(reqs);
601}
602
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600603void Image::init_no_mem(const Device &dev, const VkImageCreateInfo &info)
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800604{
Courtney Goeltzenleuchter992fb4f2015-04-19 19:07:33 -0600605 DERIVED_OBJECT_TYPE_INIT(vkCreateImage, dev, VK_OBJECT_TYPE_IMAGE, &info);
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800606 init_info(dev, info);
607}
608
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600609void Image::init_info(const Device &dev, const VkImageCreateInfo &info)
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800610{
611 create_info_ = info;
612
613 for (std::vector<Device::Format>::const_iterator it = dev.formats().begin(); it != dev.formats().end(); it++) {
614 if (memcmp(&it->format, &create_info_.format, sizeof(it->format)) == 0 && it->tiling == create_info_.tiling) {
615 format_features_ = it->features;
616 break;
617 }
618 }
619}
620
Mark Lobodzinski23065352015-05-29 09:32:35 -0500621void Image::bind_memory(const Device &dev, const VkImageMemoryBindInfo &info,
Tony Barbourd1c35722015-04-16 15:59:00 -0600622 const GpuMemory &mem, VkDeviceSize mem_offset)
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800623{
Mark Lobodzinski40f7f402015-04-16 11:44:05 -0500624 VkQueue queue = dev.graphics_queues()[0]->obj();
Mark Lobodzinski23065352015-05-29 09:32:35 -0500625 EXPECT(vkQueueBindSparseImageMemory(queue, obj(), &info, mem.obj(), mem_offset) == VK_SUCCESS);
Chia-I Wu1a28fe02015-01-01 07:55:04 +0800626}
627
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600628VkSubresourceLayout Image::subresource_layout(const VkImageSubresource &subres) const
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800629{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600630 VkSubresourceLayout data;
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800631 size_t size = sizeof(data);
Tony Barbour59a47322015-06-24 16:06:58 -0600632 if (!EXPECT(vkGetImageSubresourceLayout(dev_->obj(), obj(), &subres, &data) == VK_SUCCESS && size == sizeof(data)))
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800633 memset(&data, 0, sizeof(data));
634
635 return data;
636}
637
638bool Image::transparent() const
639{
Tony Barbourd1c35722015-04-16 15:59:00 -0600640 return (create_info_.tiling == VK_IMAGE_TILING_LINEAR &&
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800641 create_info_.samples == 1 &&
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600642 !(create_info_.usage & (VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT |
643 VK_IMAGE_USAGE_DEPTH_STENCIL_BIT)));
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800644}
645
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600646void ImageView::init(const Device &dev, const VkImageViewCreateInfo &info)
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800647{
Courtney Goeltzenleuchter992fb4f2015-04-19 19:07:33 -0600648 DERIVED_OBJECT_TYPE_INIT(vkCreateImageView, dev, VK_OBJECT_TYPE_IMAGE_VIEW, &info);
Courtney Goeltzenleuchter30a39452015-04-22 10:02:41 -0600649 alloc_memory();
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800650}
651
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600652void ColorAttachmentView::init(const Device &dev, const VkColorAttachmentViewCreateInfo &info)
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800653{
Courtney Goeltzenleuchter992fb4f2015-04-19 19:07:33 -0600654 DERIVED_OBJECT_TYPE_INIT(vkCreateColorAttachmentView, dev, VK_OBJECT_TYPE_COLOR_ATTACHMENT_VIEW, &info);
Courtney Goeltzenleuchter30a39452015-04-22 10:02:41 -0600655 alloc_memory();
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800656}
657
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600658void DepthStencilView::init(const Device &dev, const VkDepthStencilViewCreateInfo &info)
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800659{
Courtney Goeltzenleuchter992fb4f2015-04-19 19:07:33 -0600660 DERIVED_OBJECT_TYPE_INIT(vkCreateDepthStencilView, dev, VK_OBJECT_TYPE_DEPTH_STENCIL_VIEW, &info);
Courtney Goeltzenleuchter30a39452015-04-22 10:02:41 -0600661 alloc_memory();
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800662}
663
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600664void Shader::init(const Device &dev, const VkShaderCreateInfo &info)
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800665{
Courtney Goeltzenleuchter992fb4f2015-04-19 19:07:33 -0600666 DERIVED_OBJECT_TYPE_INIT(vkCreateShader, dev, VK_OBJECT_TYPE_SHADER, &info);
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800667}
668
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600669VkResult Shader::init_try(const Device &dev, const VkShaderCreateInfo &info)
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800670{
Courtney Goeltzenleuchter992fb4f2015-04-19 19:07:33 -0600671 /*
672 * Note: Cannot use DERIVED_OBJECT_TYPE_INIT as we need the
673 * return code.
674 */
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600675 VkShader sh;
Courtney Goeltzenleuchter992fb4f2015-04-19 19:07:33 -0600676 dev_ = &dev;
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600677 VkResult err = vkCreateShader(dev.obj(), &info, &sh);
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600678 if (err == VK_SUCCESS)
Courtney Goeltzenleuchter992fb4f2015-04-19 19:07:33 -0600679 Object::init(sh, VK_OBJECT_TYPE_SHADER);
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800680
681 return err;
682}
683
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600684void Pipeline::init(const Device &dev, const VkGraphicsPipelineCreateInfo &info)
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800685{
Courtney Goeltzenleuchter992fb4f2015-04-19 19:07:33 -0600686 DERIVED_OBJECT_TYPE_INIT(vkCreateGraphicsPipeline, dev, VK_OBJECT_TYPE_PIPELINE, &info);
Courtney Goeltzenleuchter30a39452015-04-22 10:02:41 -0600687 alloc_memory();
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800688}
689
Chris Forbes95292b12015-05-25 11:13:26 +1200690VkResult Pipeline::init_try(const Device &dev, const VkGraphicsPipelineCreateInfo &info)
691{
692 VkPipeline pipe;
693 dev_ = &dev;
694 VkResult err = vkCreateGraphicsPipeline(dev.obj(), &info, &pipe);
695 if (err == VK_SUCCESS) {
696 Object::init(pipe, VK_OBJECT_TYPE_PIPELINE);
697 alloc_memory();
698 }
699
700 return err;
701}
702
Courtney Goeltzenleuchter0d40f152015-03-25 15:37:49 -0600703void Pipeline::init(
704 const Device &dev,
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600705 const VkGraphicsPipelineCreateInfo &info,
706 const VkPipeline basePipeline)
Courtney Goeltzenleuchter0d40f152015-03-25 15:37:49 -0600707{
Courtney Goeltzenleuchter992fb4f2015-04-19 19:07:33 -0600708 DERIVED_OBJECT_TYPE_INIT(vkCreateGraphicsPipelineDerivative, dev, VK_OBJECT_TYPE_PIPELINE, &info, basePipeline);
Courtney Goeltzenleuchter30a39452015-04-22 10:02:41 -0600709 alloc_memory();
Courtney Goeltzenleuchter0d40f152015-03-25 15:37:49 -0600710}
711
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600712void Pipeline::init(const Device &dev, const VkComputePipelineCreateInfo &info)
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800713{
Courtney Goeltzenleuchter992fb4f2015-04-19 19:07:33 -0600714 DERIVED_OBJECT_TYPE_INIT(vkCreateComputePipeline, dev, VK_OBJECT_TYPE_PIPELINE, &info);
Courtney Goeltzenleuchter30a39452015-04-22 10:02:41 -0600715 alloc_memory();
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800716}
717
718void Pipeline::init(const Device&dev, size_t size, const void *data)
719{
Courtney Goeltzenleuchter992fb4f2015-04-19 19:07:33 -0600720 DERIVED_OBJECT_TYPE_INIT(vkLoadPipeline, dev, VK_OBJECT_TYPE_PIPELINE, size, data);
Courtney Goeltzenleuchter30a39452015-04-22 10:02:41 -0600721 alloc_memory();
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800722}
723
Courtney Goeltzenleuchter0d40f152015-03-25 15:37:49 -0600724void Pipeline::init(
725 const Device&dev,
726 size_t size,
727 const void *data,
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600728 const VkPipeline basePipeline)
Courtney Goeltzenleuchter0d40f152015-03-25 15:37:49 -0600729{
Courtney Goeltzenleuchter992fb4f2015-04-19 19:07:33 -0600730 DERIVED_OBJECT_TYPE_INIT(vkLoadPipelineDerivative, dev, VK_OBJECT_TYPE_PIPELINE, size, data, basePipeline);
Courtney Goeltzenleuchter30a39452015-04-22 10:02:41 -0600731 alloc_memory();
Courtney Goeltzenleuchter0d40f152015-03-25 15:37:49 -0600732}
733
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800734size_t Pipeline::store(size_t size, void *data)
735{
Courtney Goeltzenleuchter992fb4f2015-04-19 19:07:33 -0600736 if (!EXPECT(vkStorePipeline(dev_->obj(), obj(), &size, data) == VK_SUCCESS))
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800737 size = 0;
738
739 return size;
740}
741
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600742void Sampler::init(const Device &dev, const VkSamplerCreateInfo &info)
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800743{
Courtney Goeltzenleuchter992fb4f2015-04-19 19:07:33 -0600744 DERIVED_OBJECT_TYPE_INIT(vkCreateSampler, dev, VK_OBJECT_TYPE_SAMPLER, &info);
Courtney Goeltzenleuchter30a39452015-04-22 10:02:41 -0600745 alloc_memory();
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800746}
747
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600748void DescriptorSetLayout::init(const Device &dev, const VkDescriptorSetLayoutCreateInfo &info)
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800749{
Courtney Goeltzenleuchter992fb4f2015-04-19 19:07:33 -0600750 DERIVED_OBJECT_TYPE_INIT(vkCreateDescriptorSetLayout, dev, VK_OBJECT_TYPE_DESCRIPTOR_SET_LAYOUT, &info);
Courtney Goeltzenleuchter30a39452015-04-22 10:02:41 -0600751 alloc_memory();
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800752}
753
Mark Lobodzinski0fadf5f2015-04-17 14:11:39 -0500754void PipelineLayout::init(const Device &dev, VkPipelineLayoutCreateInfo &info, const std::vector<const DescriptorSetLayout *> &layouts)
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800755{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600756 const std::vector<VkDescriptorSetLayout> layout_objs = make_objects<VkDescriptorSetLayout>(layouts);
Mark Lobodzinski0fadf5f2015-04-17 14:11:39 -0500757 info.pSetLayouts = &layout_objs[0];
Chia-I Wu41126e52015-03-26 15:27:55 +0800758
Courtney Goeltzenleuchter992fb4f2015-04-19 19:07:33 -0600759 DERIVED_OBJECT_TYPE_INIT(vkCreatePipelineLayout, dev, VK_OBJECT_TYPE_PIPELINE_LAYOUT, &info);
Courtney Goeltzenleuchter30a39452015-04-22 10:02:41 -0600760 alloc_memory();
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800761}
762
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600763void DescriptorPool::init(const Device &dev, VkDescriptorPoolUsage usage,
764 uint32_t max_sets, const VkDescriptorPoolCreateInfo &info)
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800765{
Courtney Goeltzenleuchter992fb4f2015-04-19 19:07:33 -0600766 DERIVED_OBJECT_TYPE_INIT(vkCreateDescriptorPool, dev, VK_OBJECT_TYPE_DESCRIPTOR_POOL, usage, max_sets, &info);
Courtney Goeltzenleuchter30a39452015-04-22 10:02:41 -0600767 alloc_memory();
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800768}
769
Chia-I Wufae40bc2015-03-26 15:23:52 +0800770void DescriptorPool::reset()
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800771{
Courtney Goeltzenleuchter992fb4f2015-04-19 19:07:33 -0600772 EXPECT(vkResetDescriptorPool(dev_->obj(), obj()) == VK_SUCCESS);
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800773}
774
Mark Lobodzinski40f7f402015-04-16 11:44:05 -0500775std::vector<DescriptorSet *> DescriptorPool::alloc_sets(const Device &dev, VkDescriptorSetUsage usage, const std::vector<const DescriptorSetLayout *> &layouts)
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800776{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600777 const std::vector<VkDescriptorSetLayout> layout_objs = make_objects<VkDescriptorSetLayout>(layouts);
Chia-I Wu11078b02015-01-04 16:27:24 +0800778
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600779 std::vector<VkDescriptorSet> set_objs;
Chia-I Wu11078b02015-01-04 16:27:24 +0800780 set_objs.resize(layout_objs.size());
781
782 uint32_t set_count;
Courtney Goeltzenleuchter992fb4f2015-04-19 19:07:33 -0600783 VkResult err = vkAllocDescriptorSets(dev_->obj(), obj(), usage, layout_objs.size(), &layout_objs[0], &set_objs[0], &set_count);
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600784 if (err == VK_SUCCESS)
Chia-I Wu11078b02015-01-04 16:27:24 +0800785 EXPECT(set_count == set_objs.size());
786 set_objs.resize(set_count);
787
788 std::vector<DescriptorSet *> sets;
789 sets.reserve(set_count);
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600790 for (std::vector<VkDescriptorSet>::const_iterator it = set_objs.begin(); it != set_objs.end(); it++) {
Chia-I Wu11078b02015-01-04 16:27:24 +0800791 // do descriptor sets need memories bound?
Courtney Goeltzenleuchter992fb4f2015-04-19 19:07:33 -0600792 DescriptorSet *descriptorSet = new DescriptorSet(dev, *it);
Mark Lobodzinski40f7f402015-04-16 11:44:05 -0500793 sets.push_back(descriptorSet);
Chia-I Wu11078b02015-01-04 16:27:24 +0800794 }
Chia-I Wu11078b02015-01-04 16:27:24 +0800795 return sets;
796}
797
Mark Lobodzinski40f7f402015-04-16 11:44:05 -0500798std::vector<DescriptorSet *> DescriptorPool::alloc_sets(const Device &dev, VkDescriptorSetUsage usage, const DescriptorSetLayout &layout, uint32_t count)
Chia-I Wu11078b02015-01-04 16:27:24 +0800799{
Mark Lobodzinski40f7f402015-04-16 11:44:05 -0500800 return alloc_sets(dev, usage, std::vector<const DescriptorSetLayout *>(count, &layout));
Chia-I Wu11078b02015-01-04 16:27:24 +0800801}
802
Mark Lobodzinski40f7f402015-04-16 11:44:05 -0500803DescriptorSet *DescriptorPool::alloc_sets(const Device &dev, VkDescriptorSetUsage usage, const DescriptorSetLayout &layout)
Chia-I Wu11078b02015-01-04 16:27:24 +0800804{
Mark Lobodzinski40f7f402015-04-16 11:44:05 -0500805 std::vector<DescriptorSet *> set = alloc_sets(dev, usage, layout, 1);
Chia-I Wu11078b02015-01-04 16:27:24 +0800806 return (set.empty()) ? NULL : set[0];
807}
808
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600809void DynamicVpStateObject::init(const Device &dev, const VkDynamicVpStateCreateInfo &info)
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800810{
Courtney Goeltzenleuchter992fb4f2015-04-19 19:07:33 -0600811 DERIVED_OBJECT_TYPE_INIT(vkCreateDynamicViewportState, dev, VK_OBJECT_TYPE_DYNAMIC_VP_STATE, &info);
Courtney Goeltzenleuchter30a39452015-04-22 10:02:41 -0600812 alloc_memory();
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800813}
814
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600815void DynamicRsStateObject::init(const Device &dev, const VkDynamicRsStateCreateInfo &info)
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800816{
Courtney Goeltzenleuchter992fb4f2015-04-19 19:07:33 -0600817 DERIVED_OBJECT_TYPE_INIT(vkCreateDynamicRasterState, dev, VK_OBJECT_TYPE_DYNAMIC_RS_STATE, &info);
Courtney Goeltzenleuchter30a39452015-04-22 10:02:41 -0600818 alloc_memory();
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800819}
820
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600821void DynamicCbStateObject::init(const Device &dev, const VkDynamicCbStateCreateInfo &info)
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800822{
Courtney Goeltzenleuchter992fb4f2015-04-19 19:07:33 -0600823 DERIVED_OBJECT_TYPE_INIT(vkCreateDynamicColorBlendState, dev, VK_OBJECT_TYPE_DYNAMIC_CB_STATE, &info);
Courtney Goeltzenleuchter30a39452015-04-22 10:02:41 -0600824 alloc_memory();
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800825}
826
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600827void DynamicDsStateObject::init(const Device &dev, const VkDynamicDsStateCreateInfo &info)
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800828{
Courtney Goeltzenleuchter992fb4f2015-04-19 19:07:33 -0600829 DERIVED_OBJECT_TYPE_INIT(vkCreateDynamicDepthStencilState, dev, VK_OBJECT_TYPE_DYNAMIC_DS_STATE, &info);
Courtney Goeltzenleuchter30a39452015-04-22 10:02:41 -0600830 alloc_memory();
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800831}
832
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600833void CmdBuffer::init(const Device &dev, const VkCmdBufferCreateInfo &info)
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800834{
Courtney Goeltzenleuchter992fb4f2015-04-19 19:07:33 -0600835 DERIVED_OBJECT_TYPE_INIT(vkCreateCommandBuffer, dev, VK_OBJECT_TYPE_COMMAND_BUFFER, &info);
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800836}
837
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600838void CmdBuffer::begin(const VkCmdBufferBeginInfo *info)
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800839{
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600840 EXPECT(vkBeginCommandBuffer(obj(), info) == VK_SUCCESS);
Jeremy Hayesd65ae082015-01-14 16:17:08 -0700841}
842
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600843void CmdBuffer::begin(VkRenderPass renderpass_obj, VkFramebuffer framebuffer_obj)
Jeremy Hayesd65ae082015-01-14 16:17:08 -0700844{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600845 VkCmdBufferBeginInfo info = {};
846 VkCmdBufferGraphicsBeginInfo graphics_cmd_buf_info = {};
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600847 graphics_cmd_buf_info.sType = VK_STRUCTURE_TYPE_CMD_BUFFER_GRAPHICS_BEGIN_INFO;
Tony Barbourbdf0a312015-04-01 17:10:07 -0600848 graphics_cmd_buf_info.pNext = NULL;
Courtney Goeltzenleuchter69894b72015-04-03 15:25:24 -0600849 graphics_cmd_buf_info.renderPassContinue.renderPass = renderpass_obj;
850 graphics_cmd_buf_info.renderPassContinue.framebuffer = framebuffer_obj;
Tony Barbourbdf0a312015-04-01 17:10:07 -0600851
Tony Barbourd1c35722015-04-16 15:59:00 -0600852 info.flags = VK_CMD_BUFFER_OPTIMIZE_SMALL_BATCH_BIT |
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600853 VK_CMD_BUFFER_OPTIMIZE_ONE_TIME_SUBMIT_BIT;
854 info.sType = VK_STRUCTURE_TYPE_CMD_BUFFER_BEGIN_INFO;
Jeremy Hayesd65ae082015-01-14 16:17:08 -0700855 info.pNext = &graphics_cmd_buf_info;
856
857 begin(&info);
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800858}
859
860void CmdBuffer::begin()
861{
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600862 VkCmdBufferBeginInfo info = {};
Tony Barbourd1c35722015-04-16 15:59:00 -0600863 info.flags = VK_CMD_BUFFER_OPTIMIZE_SMALL_BATCH_BIT |
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600864 VK_CMD_BUFFER_OPTIMIZE_ONE_TIME_SUBMIT_BIT;
865 info.sType = VK_STRUCTURE_TYPE_CMD_BUFFER_BEGIN_INFO;
Jeremy Hayesd65ae082015-01-14 16:17:08 -0700866
867 begin(&info);
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800868}
869
870void CmdBuffer::end()
871{
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600872 EXPECT(vkEndCommandBuffer(obj()) == VK_SUCCESS);
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800873}
874
875void CmdBuffer::reset()
876{
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600877 EXPECT(vkResetCommandBuffer(obj()) == VK_SUCCESS);
Chia-I Wuf1e2e992014-12-27 14:12:52 +0800878}
879
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600880}; // namespace vk_testing