-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvulkan-objects.cpp
executable file
·562 lines (451 loc) · 18.3 KB
/
vulkan-objects.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
// Copyright (c) 2016-2021 Andrew Helge Cox
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
// Internal includes
#include "krust/public-api/vulkan-objects.h"
#include "krust/public-api/vulkan_struct_init.h"
#include "krust/public-api/thread-base.h"
#include "krust/public-api/logging.h"
#include "krust/public-api/krust-assertions.h"
#include "krust/public-api/krust-errors.h"
#include "krust/internal/keep-alive-set.h"
#include "krust/internal/krust-internal.h"
#include "krust/internal/scoped-temp-array.h"
#include "krust/public-api/vulkan.h"
#include "krust-kernel/public-api/debug.h"
#define KRUST_CALL_CREATOR(NAME) \
const VkResult result = vkCreate##NAME(device, &info, Internal::sAllocator, &m##NAME);\
if (result != VK_SUCCESS)\
{\
m##NAME = VK_NULL_HANDLE;\
ThreadBase::Get().GetErrorPolicy().VulkanError("vkCreate"#NAME, result, nullptr, __FUNCTION__, __FILE__, __LINE__);\
}
#define KRUST_VKOBJ_CONSTRUCTOR(NAME) \
NAME::NAME(Device& device, const Vk##NAME##CreateInfo& info) : \
mDevice(&device)\
{\
KRUST_CALL_CREATOR(NAME);\
}
#define KRUST_VKOBJ_DESTRUCTOR(NAME) \
NAME::~NAME() \
{\
vkDestroy##NAME(*mDevice, m##NAME, Internal::sAllocator);\
}
#define KRUST_VKOBJ_LIFETIME(NAME) \
KRUST_VKOBJ_CONSTRUCTOR(NAME)\
KRUST_VKOBJ_DESTRUCTOR(NAME)
#define KRUST_CALL_CREATOR_EX(NAME, SUFFIX) \
const VkResult result = vkCreate##NAME##SUFFIX(device, &info, Internal::sAllocator, &m##NAME);\
if (result != VK_SUCCESS)\
{\
m##NAME = VK_NULL_HANDLE;\
ThreadBase::Get().GetErrorPolicy().VulkanError("vkCreate"#NAME#SUFFIX, result, nullptr, __FUNCTION__, __FILE__, __LINE__);\
}
#define KRUST_VKOBJ_CONSTRUCTOR_EX(NAME, SUFFIX) \
NAME::NAME(Device& device, const Vk##NAME##CreateInfo##SUFFIX& info) : \
mDevice(&device)\
{\
KRUST_CALL_CREATOR_EX(NAME, SUFFIX);\
}
#define KRUST_VKOBJ_DESTRUCTOR_EX(NAME, SUFFIX) \
NAME::~NAME() \
{\
vkDestroy##NAME##SUFFIX(GetDevice(), m##NAME, Internal::sAllocator);\
}
#define KRUST_VKOBJ_LIFETIME_EX(NAME, SUFFIX) \
KRUST_VKOBJ_CONSTRUCTOR_EX(NAME, SUFFIX) \
KRUST_VKOBJ_DESTRUCTOR_EX(NAME, SUFFIX)
namespace Krust
{
/// Max size for a temporary buffer on the stack (over this and we do a temp heap alloc).
constexpr unsigned MAX_STACK_BUFFER_BYTES = DEFAULT_MAX_LOCAL_BUFFER_BYTES;
// -----------------------------------------------------------------------------
CommandBuffer::CommandBuffer(CommandPool& pool, const VkCommandBufferLevel level) :
mPool(&pool)
{
auto info = CommandBufferAllocateInfo(pool, level, 1u);
const VkResult result = vkAllocateCommandBuffers(pool.GetDevice(), &info, &mCommandBuffer);
if (result != VK_SUCCESS)
{
mCommandBuffer = VK_NULL_HANDLE;
auto & threadBase = ThreadBase::Get();
threadBase.GetErrorPolicy().VulkanError("vkAllocateCommandBuffers", result, nullptr, __FUNCTION__, __FILE__, __LINE__);
}
}
CommandBuffer::CommandBuffer(CommandPool& pool, VkCommandBuffer vkBuf) : mPool(&pool), mCommandBuffer(vkBuf) {}
CommandBufferPtr CommandBuffer::New(CommandPool & pool, VkCommandBufferLevel level)
{
return CommandBufferPtr{ new CommandBuffer{pool, level} };
}
void CommandBuffer::Allocate(CommandPool & pool, VkCommandBufferLevel level, unsigned number, std::vector<CommandBufferPtr>& outCommandBuffers)
{
ScopedTempArray<VkCommandBuffer, MAX_STACK_BUFFER_BYTES> buffers(number);
outCommandBuffers.clear();
outCommandBuffers.reserve(number); // Do early to throw if going to throw.
auto info = CommandBufferAllocateInfo(pool, level, number);
const VkResult result = vkAllocateCommandBuffers(pool.GetDevice(), &info, buffers.Get());
if (result != VK_SUCCESS)
{
auto & threadBase = ThreadBase::Get();
threadBase.GetErrorPolicy().VulkanError("vkAllocateCommandBuffers", result, nullptr, __FUNCTION__, __FILE__, __LINE__);
}
try
{
std::for_each(buffers.Get(), buffers.Get() + number, [&pool, &outCommandBuffers](auto rawCommandBuffer) {
outCommandBuffers.push_back(CommandBufferPtr(new CommandBuffer(pool, rawCommandBuffer)));
});
}
catch (...)
{
vkFreeCommandBuffers(pool.GetDevice(), pool, number, buffers.Get());
throw;
}
}
CommandBuffer::~CommandBuffer()
{
vkFreeCommandBuffers(mPool->GetDevice(), *mPool, 1u, &mCommandBuffer);
delete reinterpret_cast<KeepAliveSet*>(mKeepAlives);
}
void CommandBuffer::KeepAlive(VulkanObject & needed)
{
if (!mKeepAlives)
{
mKeepAlives = new KeepAliveSet{};
}
reinterpret_cast<KeepAliveSet*>(mKeepAlives)->Add(needed);
}
void CommandBuffer::Reset(const VkCommandBufferResetFlags flags, bool deleteKeepAlives)
{
if (mKeepAlives)
{
if(deleteKeepAlives){
delete reinterpret_cast<KeepAliveSet*>(mKeepAlives);
mKeepAlives = nullptr;
} else {
/// @todo reinterpret_cast<KeepAliveSet*>(mKeepAlives)->Clear();
}
}
/// @todo Unfinished function. Never actually resets the underlying Vulkan object. [FixMe]
}
// -----------------------------------------------------------------------------
// AccelerationStructure
AccelerationStructure::AccelerationStructure(Buffer& buffer, const VkAccelerationStructureCreateInfoKHR& info) :
mBuffer(&buffer)
{
Device& device = buffer.GetDevice();
KRUST_CALL_CREATOR_EX(AccelerationStructure, KHR);
}
KRUST_VKOBJ_DESTRUCTOR_EX(AccelerationStructure, KHR)
AccelerationStructurePtr AccelerationStructure::New(
Buffer& buffer,
VkDeviceSize offset,
VkDeviceSize size,
VkAccelerationStructureTypeKHR type,
const void* pNext,
VkAccelerationStructureCreateFlagsKHR createFlags,
VkDeviceAddress deviceAddress
) {
const auto createInfo = AccelerationStructureCreateInfoKHR(createFlags, buffer, offset, size, type, deviceAddress);
AccelerationStructurePtr as(new AccelerationStructure(buffer, createInfo));
return as;
}
Device& AccelerationStructure::GetDevice() const { return mBuffer->GetDevice(); }
// -----------------------------------------------------------------------------
CommandPool::CommandPool(Device & device, VkCommandPoolCreateFlags flags, uint32_t queueFamilyIndex) :
mDevice(device)
{
auto poolInfo = CommandPoolCreateInfo(flags, queueFamilyIndex);
const VkResult result = vkCreateCommandPool(device, &poolInfo, Internal::sAllocator, &mCommandPool);
if (result != VK_SUCCESS)
{
mCommandPool = VK_NULL_HANDLE;
auto & threadBase = ThreadBase::Get();
threadBase.GetErrorPolicy().VulkanError("vkCreateCommandPool", result, nullptr, __FUNCTION__, __FILE__, __LINE__);
}
}
CommandPoolPtr CommandPool::New(Device & device, VkCommandPoolCreateFlags flags, uint32_t queueFamilyIndex)
{//DescriptorSet::DescriptorSet(Device& device, const VkDescriptorSetAllocateInfo& allocateInfo)
return new CommandPool { device, flags, queueFamilyIndex };
}
CommandPool::~CommandPool()
{
vkDestroyCommandPool(*mDevice, mCommandPool, Internal::sAllocator);
}
// -----------------------------------------------------------------------------
ComputePipeline::ComputePipeline(Device& device, const VkComputePipelineCreateInfo& createInfo) :
mDevice(device)
{
const VkResult result = vkCreateComputePipelines(device, VK_NULL_HANDLE, 1, &createInfo, Internal::sAllocator, &mPipeline);
if (result != VK_SUCCESS)
{
mPipeline = VK_NULL_HANDLE;
ThreadBase::Get().GetErrorPolicy().VulkanError("vkCreateComputePipelines", result, nullptr, __FUNCTION__, __FILE__, __LINE__);
}
}
ComputePipelinePtr ComputePipeline::New(Device& device, const VkComputePipelineCreateInfo& createInfo)
{
return new ComputePipeline { device, createInfo };
}
ComputePipeline::~ComputePipeline()
{
vkDestroyPipeline(*mDevice, mPipeline, Internal::sAllocator);
}
// -----------------------------------------------------------------------------
DescriptorPool::DescriptorPool(Device& device, const VkDescriptorPoolCreateInfo& createInfo) :
mDevice(device)
{
VkDescriptorPoolCreateInfo ci = createInfo;
ci.flags |= VK_DESCRIPTOR_POOL_CREATE_FREE_DESCRIPTOR_SET_BIT;
const VkResult result = vkCreateDescriptorPool(device, &ci, Internal::sAllocator, &mDescriptorPool);
if (result != VK_SUCCESS)
{
mDescriptorPool = VK_NULL_HANDLE;
auto & threadBase = ThreadBase::Get();
threadBase.GetErrorPolicy().VulkanError("vkCreateDescriptorPool", result, nullptr, __FUNCTION__, __FILE__, __LINE__);
}
}
DescriptorPoolPtr DescriptorPool::New(Device& device, VkDescriptorPoolCreateFlags flags, uint32_t maxSets, uint32_t poolSizeCount, const VkDescriptorPoolSize* pPoolSizes)
{
return new DescriptorPool { device, DescriptorPoolCreateInfo(flags, maxSets, poolSizeCount, pPoolSizes)};
}
DescriptorPoolPtr DescriptorPool::New(Device& device, VkDescriptorPoolCreateFlags flags, uint32_t maxSets, const VkDescriptorPoolSize& poolSize)
{
return new DescriptorPool { device, DescriptorPoolCreateInfo(flags, maxSets, 1, &poolSize)};
}
KRUST_VKOBJ_DESTRUCTOR(DescriptorPool)
// -----------------------------------------------------------------------------
DescriptorSet::DescriptorSet(DescriptorPool& pool, const VkDescriptorSetAllocateInfo& allocateInfo) :
mPool(pool)
{
Device& device = pool.GetDevice();
const VkResult result = vkAllocateDescriptorSets(device, &allocateInfo, &mDescriptorSet);
if (result != VK_SUCCESS)
{
mDescriptorSet = VK_NULL_HANDLE;
ThreadBase::Get().GetErrorPolicy().VulkanError("vkAllocateDescriptorSets", result, nullptr, __FUNCTION__, __FILE__, __LINE__);
}
}
DescriptorSetPtr DescriptorSet::Allocate(
DescriptorPool& pool,
DescriptorSetLayout& setLayout)
{
return new DescriptorSet {pool, DescriptorSetAllocateInfo(pool, 1, setLayout.GetDescriptorSetLayoutAddress())};
}
DescriptorSet::~DescriptorSet()
{
/// @todo Delay freeing until all command buffers referencing the set are free
/// and all work on GPUs using them is complete.
/// 1. Command buffers must hold a reference to them.
/// 2. Queues keep them alive transitively by keeping command buffers alive.
vkFreeDescriptorSets(mPool->GetDevice(), *mPool, 1, &mDescriptorSet);
}
// -----------------------------------------------------------------------------
KRUST_VKOBJ_LIFETIME(DescriptorSetLayout)
DescriptorSetLayoutPtr DescriptorSetLayout::New(
Device& device,
VkDescriptorSetLayoutCreateFlags flags,
uint32_t bindingCount,
const VkDescriptorSetLayoutBinding* pBindings)
{
return new DescriptorSetLayout {device, DescriptorSetLayoutCreateInfo(flags, bindingCount, pBindings)};
}
// -----------------------------------------------------------------------------
Device::Device(Instance & instance, VkPhysicalDevice physicalDevice, const VkDeviceCreateInfo & createInfo) :
mInstance(instance)
{
KRUST_ASSERT1(instance != VK_NULL_HANDLE, "Invalid instance.");
KRUST_ASSERT1(physicalDevice != VK_NULL_HANDLE, "Invalid physical device.");
mPhysicalDevice = physicalDevice;
auto & threadBase = ThreadBase::Get();
const VkResult result = vkCreateDevice(physicalDevice, &createInfo, Internal::sAllocator, &mDevice);
if (result != VK_SUCCESS)
{
mDevice = VK_NULL_HANDLE;
threadBase.GetErrorPolicy().VulkanError("vkCreateDevice", result, nullptr, __FUNCTION__, __FILE__, __LINE__);
}
}
DevicePtr Device::New(Instance & instance, VkPhysicalDevice physicalDevice, const VkDeviceCreateInfo & createInfo)
{
return new Device{ instance, physicalDevice, createInfo };
}
Device::~Device()
{
vkDestroyDevice(mDevice, Internal::sAllocator);
}
// -----------------------------------------------------------------------------
DeviceMemory::DeviceMemory(Device & device, const VkMemoryAllocateInfo & info) :
mDevice(&device)
{
const VkResult result = vkAllocateMemory(device, &info, Internal::sAllocator, &mDeviceMemory);
if (result != VK_SUCCESS)
{
mDeviceMemory = VK_NULL_HANDLE;
auto & threadBase = ThreadBase::Get();
threadBase.GetErrorPolicy().VulkanError("vkAllocateMemory", result, nullptr, __FUNCTION__, __FILE__, __LINE__);
}
}
DeviceMemoryPtr DeviceMemory::New(Device & device, const VkMemoryAllocateInfo & info)
{
return new DeviceMemory { device, info };
}
DeviceMemory::~DeviceMemory()
{
vkFreeMemory(*mDevice, mDeviceMemory, Internal::sAllocator);
}
// -----------------------------------------------------------------------------
// Buffer
KRUST_VKOBJ_LIFETIME(Buffer)
BufferPtr Buffer::New(Device& device, VkBufferCreateFlags flags, VkDeviceSize size, VkBufferUsageFlags usage, VkSharingMode sharingMode, uint32_t queueFamilyIndex)
{
return new Buffer(device, BufferCreateInfo(flags, size, usage, sharingMode, 1, &queueFamilyIndex));
}
VkResult Buffer::BindMemory(DeviceMemory& memory, VkDeviceSize memoryOffset)
{
const VkResult result = vkBindBufferMemory(*mDevice, mBuffer, memory, memoryOffset);
if(result == VK_SUCCESS){
mMemory = memory;
}
return result;
}
// -----------------------------------------------------------------------------
KRUST_VKOBJ_LIFETIME(Fence);
FencePtr Fence::New(Device& device, const VkFenceCreateFlags flags)
{
return new Fence {device, FenceCreateInfo(flags)};
}
// -----------------------------------------------------------------------------
KRUST_VKOBJ_LIFETIME(Image);
ImagePtr Image::New(Device & device, const VkImageCreateInfo & createInfo)
{
return new Image{ device, createInfo };
}
void Image::BindMemory(DeviceMemory& memory, const VkDeviceSize offset)
{
const VkResult result = vkBindImageMemory(*mDevice, mImage, memory, offset);
if (result != VK_SUCCESS)
{
ThreadBase::Get().GetErrorPolicy().VulkanError("vkBindImageMemory", result, nullptr, __FUNCTION__, __FILE__, __LINE__);
}
mMemory = DeviceMemoryPtr(&memory);
}
// -----------------------------------------------------------------------------
ImageView::ImageView(Image& image, const VkImageViewCreateInfo& createInfo) :
mImage(&image)
{
VkImageViewCreateInfo info { createInfo };
info.image = image;
mImage = image;
const VkResult result = vkCreateImageView(image.device(), &info, Internal::sAllocator, &mImageView);
if (result != VK_SUCCESS)
{
mImage.Reset(0);
mImageView = VK_NULL_HANDLE;
ThreadBase::Get().GetErrorPolicy().VulkanError("vkCreateImage", result, nullptr, __FUNCTION__, __FILE__, __LINE__);
}
}
ImageViewPtr ImageView::New(Image& image, const VkImageViewCreateInfo& createInfo)
{
return new ImageView { image, createInfo };
}
ImageView::~ImageView()
{
vkDestroyImageView(device(), mImageView, Internal::sAllocator);
}
// -----------------------------------------------------------------------------
Instance::Instance(const VkInstanceCreateInfo & createInfo)
{
auto & threadBase = ThreadBase::Get();
const VkResult result = vkCreateInstance(&createInfo, Internal::sAllocator, &mInstance);
if (result != VK_SUCCESS)
{
mInstance = VK_NULL_HANDLE;
threadBase.GetErrorPolicy().VulkanError("vkCreateInstance", result, nullptr, __FUNCTION__, __FILE__, __LINE__);
}
}
InstancePtr Instance::New(const VkInstanceCreateInfo & createInfo)
{
return new Instance { createInfo };
}
Instance::~Instance()
{
vkDestroyInstance(mInstance, Internal::sAllocator);
}
// -----------------------------------------------------------------------------
KRUST_VKOBJ_LIFETIME(PipelineLayout);
PipelineLayoutPtr PipelineLayout::New(
Device& device,
VkPipelineLayoutCreateFlags flags,
uint32_t setLayoutCount,
const VkDescriptorSetLayout* pSetLayouts,
uint32_t pushConstantRangeCount,
const VkPushConstantRange* pPushConstantRanges)
{
return new PipelineLayout(device, PipelineLayoutCreateInfo(flags, setLayoutCount, pSetLayouts, pushConstantRangeCount, pPushConstantRanges));
}
PipelineLayoutPtr PipelineLayout::New(
Device& device,
VkPipelineLayoutCreateFlags flags,
const VkDescriptorSetLayout& setLayout,
const VkPushConstantRange& pushConstantRange)
{
return new PipelineLayout(device, PipelineLayoutCreateInfo(flags, 1, &setLayout, 1, &pushConstantRange));
}
PipelineLayoutPtr PipelineLayout::New(
Device& device,
VkPipelineLayoutCreateFlags flags,
const VkDescriptorSetLayout& setLayout)
{
return new PipelineLayout(device, PipelineLayoutCreateInfo(flags, 1, &setLayout, 0, nullptr));
}
// -----------------------------------------------------------------------------
Queue::Queue(Device& device, const uint32_t queueFamilyIndex, const uint32_t queueIndex)
: mDevice(&device), mQueue(VK_NULL_HANDLE)
{
vkGetDeviceQueue(
device,
queueFamilyIndex,
queueIndex,
&mQueue);
if(VK_NULL_HANDLE == mQueue)
{
ThreadBase::Get().GetErrorPolicy().VulkanUnexpected("vkGetDeviceQueue", "Returned a null handle.", __FUNCTION__, __FILE__, __LINE__);\
}
}
Queue::~Queue()
{
// Keep the device from being destroyed while this queue is busy:
vkQueueWaitIdle(mQueue);
}
QueuePtr Queue::New(Device& device, const uint32_t queueFamilyIndex, const uint32_t queueIndex)
{
return new Queue(device, queueFamilyIndex, queueIndex);
}
// -----------------------------------------------------------------------------
KRUST_VKOBJ_LIFETIME(Semaphore)
SemaphorePtr Semaphore::New(Device& device)
{
return new Semaphore { device, SemaphoreCreateInfo(0) };
}
// -----------------------------------------------------------------------------
KRUST_VKOBJ_LIFETIME(ShaderModule)
ShaderModulePtr ShaderModule::New(Device& device, const VkShaderModuleCreateFlags flags, const ShaderBuffer& src)
{
return new ShaderModule { device, ShaderModuleCreateInfo(flags, byte_size(src), &src[0]) };
}
}