forked from vsg-dev/VulkanSceneGraph
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDescriptorSet.cpp
163 lines (128 loc) · 5.86 KB
/
DescriptorSet.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
/* <editor-fold desc="MIT License">
Copyright(c) 2018 Robert Osfield
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.
</editor-fold> */
#include <vsg/core/Exception.h>
#include <vsg/core/compare.h>
#include <vsg/io/Options.h>
#include <vsg/state/DescriptorSet.h>
#include <vsg/traversals/CompileTraversal.h>
#include <vsg/viewer/View.h>
#include <vsg/vk/CommandBuffer.h>
#include <vsg/vk/DescriptorPool.h>
using namespace vsg;
DescriptorSet::DescriptorSet()
{
}
DescriptorSet::DescriptorSet(ref_ptr<DescriptorSetLayout> in_descriptorSetLayout, const Descriptors& in_descriptors) :
setLayout(in_descriptorSetLayout),
descriptors(in_descriptors)
{
}
DescriptorSet::~DescriptorSet()
{
release();
}
int DescriptorSet::compare(const Object& rhs_object) const
{
int result = Object::compare(rhs_object);
if (result != 0) return result;
auto& rhs = static_cast<decltype(*this)>(rhs_object);
if ((result = compare_pointer(setLayout, rhs.setLayout))) return result;
return compare_pointer_container(descriptors, rhs.descriptors);
}
void DescriptorSet::read(Input& input)
{
Object::read(input);
input.readObject("setLayout", setLayout);
input.readObjects("descriptors", descriptors);
}
void DescriptorSet::write(Output& output) const
{
Object::write(output);
output.writeObject("setLayout", setLayout);
output.writeObjects("descriptors", descriptors);
}
void DescriptorSet::compile(Context& context)
{
if (!_implementation[context.deviceID])
{
// make sure all the contributing objects are compiled
if (setLayout) setLayout->compile(context);
for (auto& descriptor : descriptors) descriptor->compile(context);
_implementation[context.deviceID] = context.allocateDescriptorSet(setLayout);
_implementation[context.deviceID]->assign(context, descriptors);
}
}
void DescriptorSet::release(uint32_t deviceID)
{
Implementation::recycle(_implementation[deviceID]);
}
void DescriptorSet::release()
{
for (auto& dsi : _implementation) Implementation::recycle(dsi);
_implementation.clear();
}
VkDescriptorSet DescriptorSet::vk(uint32_t deviceID) const
{
return _implementation[deviceID]->_descriptorSet;
}
////////////////////////////////////////////////////////////////////////////////////////////////////
//
// DescriptorSet::Implementation
//
DescriptorSet::Implementation::Implementation(DescriptorPool* descriptorPool, DescriptorSetLayout* descriptorSetLayout) :
_descriptorPool(descriptorPool),
_descriptorSetLayout(descriptorSetLayout)
{
auto device = descriptorPool->getDevice();
_descriptorPoolSizes.clear();
descriptorSetLayout->getDescriptorPoolSizes(_descriptorPoolSizes);
VkDescriptorSetLayout vkdescriptorSetLayout = descriptorSetLayout->vk(device->deviceID);
VkDescriptorSetAllocateInfo descriptSetAllocateInfo = {};
descriptSetAllocateInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO;
descriptSetAllocateInfo.descriptorPool = *descriptorPool;
descriptSetAllocateInfo.descriptorSetCount = 1;
descriptSetAllocateInfo.pSetLayouts = &vkdescriptorSetLayout;
// no need to locally lock DescriptorPool as the DescriptorSet::Implementation constructor should only be called by
// DescriptorPool::allocateDescriptorSet that will already have locked the DescriptorPool::mutex before calling this constructor.
// otherwise we'd need a : std::scoped_lock<std::mutex> lock(_descriptorPool->mutex);
if (VkResult result = vkAllocateDescriptorSets(*device, &descriptSetAllocateInfo, &_descriptorSet); result != VK_SUCCESS)
{
throw Exception{"Error: Failed to create DescriptorSet.", result};
}
}
DescriptorSet::Implementation::~Implementation()
{
if (_descriptorPool && _descriptorSet)
{
std::scoped_lock<std::mutex> lock(_descriptorPool->mutex);
vkFreeDescriptorSets(*(_descriptorPool->getDevice()), *_descriptorPool, 1, &_descriptorSet);
}
}
void DescriptorSet::Implementation::assign(Context& context, const Descriptors& in_descriptors)
{
// should we doing anything about previous _descriptor that may have been assigned?
_descriptors = in_descriptors;
if (_descriptors.empty()) return;
VkWriteDescriptorSet* descriptorWrites = context.scratchMemory->allocate<VkWriteDescriptorSet>(_descriptors.size());
for (size_t i = 0; i < _descriptors.size(); ++i)
{
_descriptors[i]->assignTo(context, descriptorWrites[i]);
descriptorWrites[i].dstSet = _descriptorSet;
}
auto device = _descriptorPool->getDevice();
vkUpdateDescriptorSets(*device, static_cast<uint32_t>(_descriptors.size()), descriptorWrites, 0, nullptr);
// clean up scratch memory so it can be reused.
context.scratchMemory->release();
}
void DescriptorSet::Implementation::recycle(ref_ptr<DescriptorSet::Implementation>& dsi)
{
if (dsi)
{
if (dsi->_descriptorPool) dsi->_descriptorPool->freeDescriptorSet(dsi);
dsi = {};
}
}