Divide Framework 0.1
A free and open-source 3D Framework under heavy development
Loading...
Searching...
No Matches
vkDescriptors.cpp
Go to the documentation of this file.
1
2
4
5namespace Divide {
6
8 : _device(device)
9 {
10 }
11
13 {
14 //delete every descriptor layout held
15 for (const auto& pair : _layoutCache)
16 {
17 vkDestroyDescriptorSetLayout(_device, pair.second, nullptr);
18 }
19 }
20
21 VkDescriptorSetLayout DescriptorLayoutCache::createDescriptorLayout(VkDescriptorSetLayoutCreateInfo* info)
22 {
24
25 DescriptorLayoutInfo layoutinfo{};
26 layoutinfo.bindings.reserve(info->bindingCount);
27 bool isSorted = true;
28 I32 lastBinding = -1;
29
30 for (U32 i = 0u; i < info->bindingCount; i++)
31 {
32 layoutinfo.bindings.push_back(info->pBindings[i]);
33
34 //check that the bindings are in strict increasing order
35 if (to_I32(info->pBindings[i].binding) > lastBinding)
36 {
37 lastBinding = info->pBindings[i].binding;
38 }
39 else
40 {
41 isSorted = false;
42 }
43 }
44
45 if (!isSorted)
46 {
47 eastl::sort(eastl::begin(layoutinfo.bindings),
48 eastl::end(layoutinfo.bindings),
49 [](const VkDescriptorSetLayoutBinding& a, const VkDescriptorSetLayoutBinding& b ) {
50 return a.binding < b.binding;
51 });
52 }
53
54 const auto it = _layoutCache.find(layoutinfo);
55 if (it != _layoutCache.end())
56 {
57 return (*it).second;
58 }
59
60 VkDescriptorSetLayout layout;
61 vkCreateDescriptorSetLayout(_device, info, nullptr, &layout);
62 hashAlg::emplace(_layoutCache, layoutinfo, layout);
63 return layout;
64 }
65
67 {
68 if (other.bindings.size() != bindings.size())
69 {
70 return false;
71 }
72
73 //compare each of the bindings is the same. Bindings are sorted so they will match
74 for (size_t i = 0u; i < bindings.size(); i++)
75 {
76 if (other.bindings[i] != bindings[i])
77 {
78 return false;
79 }
80 }
81
82 return true;
83 }
84
86 {
88
89 size_t h = 1337;
90 Util::Hash_combine(h, bindings.size());
91
92 for (const VkDescriptorSetLayoutBinding& b : bindings)
93 {
94 Util::Hash_combine(h, b.binding,
95 b.descriptorType,
96 b.descriptorCount,
97 b.stageFlags);
98
99 if (b.pImmutableSamplers != nullptr)
100 {
101 Util::Hash_combine(h, *b.pImmutableSamplers);
102 }
103 }
104
105 return h;
106 }
107
109 {
111
112 DescriptorBuilder builder{};
113 builder.cache = layoutCache;
114 builder.alloc = allocator;
115 return builder;
116 }
117
118
119 DescriptorBuilder& DescriptorBuilder::bindBuffer(const U32 binding, VkDescriptorBufferInfo* bufferInfo, const VkDescriptorType type, const VkShaderStageFlags stageFlags)
120 {
121 VkDescriptorSetLayoutBinding& newBinding = bindings.emplace_back();
122
123 newBinding.descriptorCount = 1;
124 newBinding.descriptorType = type;
125 newBinding.stageFlags = stageFlags;
126 newBinding.binding = binding;
127 newBinding.pImmutableSamplers = nullptr;
128
129 writes.push_back(vk::writeDescriptorSet(type, binding, bufferInfo, 1u));
130 return *this;
131 }
132
133
134 DescriptorBuilder& DescriptorBuilder::bindImage(const U32 binding, VkDescriptorImageInfo* imageInfo, const VkDescriptorType type, const VkShaderStageFlags stageFlags)
135 {
136 VkDescriptorSetLayoutBinding& newBinding = bindings.emplace_back();
137
138 newBinding.descriptorCount = 1;
139 newBinding.descriptorType = type;
140 newBinding.stageFlags = stageFlags;
141 newBinding.binding = binding;
142 newBinding.pImmutableSamplers = nullptr;
143
144 writes.push_back(vk::writeDescriptorSet(type, binding, imageInfo, 1u));
145 return *this;
146 }
147
148 bool DescriptorBuilder::buildSetFromLayout(VkDescriptorSet& set, const VkDescriptorSetLayout& layoutIn, VkDevice device )
149 {
151
152 {
154 //allocate descriptor
155 if ( !alloc->Allocate( layoutIn, set ) )
156 {
157 return false;
158 }
159 }
160 {
162
163 //write descriptor
164 for ( VkWriteDescriptorSet& w : writes )
165 {
166 w.dstSet = set;
167 }
168
169 vkUpdateDescriptorSets( device, to_U32( writes.size() ), writes.data(), 0, nullptr );
170 }
171 return true;
172 }
173
174 bool DescriptorBuilder::buildSetAndLayout(VkDescriptorSet& set, VkDescriptorSetLayout& layoutOut, VkDevice device )
175 {
177
178 VkDescriptorSetLayoutCreateInfo layoutInfo{ .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO };
179 layoutInfo.pBindings = bindings.data();
180 layoutInfo.bindingCount = to_U32( bindings.size() );
181 layoutOut = cache->createDescriptorLayout(&layoutInfo);
182
183 return buildSetFromLayout(set, layoutOut, device);
184 }
185
186
187} //namespace Divide
VkFlags VkShaderStageFlags
Definition: GLSLToSPIRV.h:37
#define PROFILE_SCOPE_AUTO(CATEGORY)
Definition: Profiler.h:87
#define PROFILE_SCOPE(NAME, CATEGORY)
Definition: Profiler.h:86
DescriptorLayoutCache * cache
static DescriptorBuilder Begin(DescriptorLayoutCache *layoutCache, vke::DescriptorAllocatorHandle *allocator)
DescriptorBuilder & bindBuffer(U32 binding, VkDescriptorBufferInfo *bufferInfo, VkDescriptorType type, VkShaderStageFlags stageFlags)
bool buildSetFromLayout(VkDescriptorSet &set, const VkDescriptorSetLayout &layoutIn, VkDevice device)
bool buildSetAndLayout(VkDescriptorSet &set, VkDescriptorSetLayout &layoutOut, VkDevice device)
DescriptorBuilder & bindImage(U32 binding, VkDescriptorImageInfo *imageInfo, VkDescriptorType type, VkShaderStageFlags stageFlags)
DescriptorLayoutCache(VkDevice device)
VkDescriptorSetLayout createDescriptorLayout(VkDescriptorSetLayoutCreateInfo *info)
hashMap< DescriptorLayoutInfo, VkDescriptorSetLayout, DescriptorLayoutHash > _layoutCache
Definition: vkDescriptors.h:86
constexpr Optick::Category::Type Graphics
Definition: Profiler.h:60
void Hash_combine(size_t &seed, const T &v, const Rest &... rest) noexcept
a la Boost
Definition: MathHelper.inl:799
VkWriteDescriptorSet writeDescriptorSet(VkDescriptorType type, uint32_t binding, VkDescriptorBufferInfo *bufferInfo, uint32_t descriptorCount=1)
Handle console commands that start with a forward slash.
Definition: AIProcessor.cpp:7
constexpr U32 to_U32(const T value)
int32_t I32
constexpr I32 to_I32(const T value)
uint32_t U32
Divide::hashPairReturn< K, V, HashFun > emplace(Divide::hashMap< K, V, HashFun, Predicate > &map, K key, Args &&... args)
Definition: HashMap.h:121
vector< VkDescriptorSetLayoutBinding > bindings
Definition: vkDescriptors.h:73
bool operator==(const DescriptorLayoutInfo &other) const