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
|
/*
* Copyright 2019-2021 Diligent Graphics LLC
* Copyright 2015-2019 Egor Yusov
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* In no event and under no legal theory, whether in tort (including negligence),
* contract, or otherwise, unless required by applicable law (such as deliberate
* and grossly negligent acts) or agreed to in writing, shall any Contributor be
* liable for any damages, including any direct, indirect, special, incidental,
* or consequential damages of any character arising as a result of this License or
* out of the use or inability to use the software (including but not limited to damages
* for loss of goodwill, work stoppage, computer failure or malfunction, or any and
* all other commercial damages or losses), even if such Contributor has been advised
* of the possibility of such damages.
*/
#pragma once
/// \file
/// Declaration of Diligent::ShaderResourceCacheVk class
// Shader resource cache stores Vk resources in a continuous chunk of memory:
//
// |Vulkan Descriptor Set|
// A ___________________________________________________________
// m_pMemory | | m_pResources, m_NumResources == m |
// | m_DescriptorSetAllocation| | |
// V | | V
// | DescriptorSet[0] | .... | DescriptorSet[Ns-1] | Res[0] | ... | Res[n-1] | .... | Res[0] | ... | Res[m-1] |
// | | A \
// | | | \
// | |________________________________________________| \RefCntAutoPtr
// | m_pResources, m_NumResources == n \_________
// | | Object |
// | m_DescriptorSetAllocation ---------
// V
// |Vulkan Descriptor Set|
//
// Ns = m_NumSets
//
//
// Descriptor set for static and mutable resources is assigned during cache initialization
// Descriptor set for dynamic resources is assigned at every draw call
#include <vector>
#include <memory>
#include "DescriptorPoolManager.hpp"
#include "SPIRVShaderResources.hpp"
#include "BufferVkImpl.hpp"
#include "ShaderResourceCacheCommon.hpp"
#include "PipelineResourceAttribsVk.hpp"
#include "VulkanUtilities/VulkanLogicalDevice.hpp"
namespace Diligent
{
class DeviceContextVkImpl;
// sizeof(ShaderResourceCacheVk) == 24 (x64, msvc, Release)
class ShaderResourceCacheVk
{
public:
explicit ShaderResourceCacheVk(ResourceCacheContentType ContentType) noexcept :
m_TotalResources{0},
m_ContentType{static_cast<Uint32>(ContentType)}
{
VERIFY_EXPR(GetContentType() == ContentType);
}
// clang-format off
ShaderResourceCacheVk (const ShaderResourceCacheVk&) = delete;
ShaderResourceCacheVk (ShaderResourceCacheVk&&) = delete;
ShaderResourceCacheVk& operator = (const ShaderResourceCacheVk&) = delete;
ShaderResourceCacheVk& operator = (ShaderResourceCacheVk&&) = delete;
// clang-format on
~ShaderResourceCacheVk();
static size_t GetRequiredMemorySize(Uint32 NumSets, const Uint32* SetSizes);
void InitializeSets(IMemoryAllocator& MemAllocator, Uint32 NumSets, const Uint32* SetSizes);
void InitializeResources(Uint32 Set, Uint32 Offset, Uint32 ArraySize, DescriptorType Type, bool HasImmutableSampler);
// sizeof(Resource) == 16 (x64, msvc, Release)
struct Resource
{
explicit Resource(DescriptorType _Type, bool _HasImmutableSampler) noexcept :
Type{_Type},
HasImmutableSampler{_HasImmutableSampler}
{
VERIFY(Type == DescriptorType::CombinedImageSampler || Type == DescriptorType::Sampler || !HasImmutableSampler,
"Immutable sampler can only be assigned to a combined image sampler or a separate sampler");
}
// clang-format off
Resource (const Resource&) = delete;
Resource (Resource&&) = delete;
Resource& operator = (const Resource&) = delete;
Resource& operator = (Resource&&) = delete;
/* 0 */ const DescriptorType Type;
/* 1 */ const bool HasImmutableSampler;
/*2-7*/ // Unused
/* 8 */ RefCntAutoPtr<IDeviceObject> pObject;
VkDescriptorBufferInfo GetUniformBufferDescriptorWriteInfo() const;
VkDescriptorBufferInfo GetStorageBufferDescriptorWriteInfo() const;
VkDescriptorImageInfo GetImageDescriptorWriteInfo () const;
VkBufferView GetBufferViewWriteInfo () const;
VkDescriptorImageInfo GetSamplerDescriptorWriteInfo() const;
VkDescriptorImageInfo GetInputAttachmentDescriptorWriteInfo() const;
VkWriteDescriptorSetAccelerationStructureKHR GetAccelerationStructureWriteInfo() const;
// clang-format on
bool IsNull() const { return pObject == nullptr; }
};
// sizeof(DescriptorSet) == 48 (x64, msvc, Release)
class DescriptorSet
{
public:
// clang-format off
DescriptorSet(Uint32 NumResources, Resource *pResources) :
m_NumResources {NumResources},
m_pResources {pResources }
{}
DescriptorSet (const DescriptorSet&) = delete;
DescriptorSet (DescriptorSet&&) = delete;
DescriptorSet& operator = (const DescriptorSet&) = delete;
DescriptorSet& operator = (DescriptorSet&&) = delete;
// clang-format on
const Resource& GetResource(Uint32 CacheOffset) const
{
VERIFY(CacheOffset < m_NumResources, "Offset ", CacheOffset, " is out of range");
return m_pResources[CacheOffset];
}
Uint32 GetSize() const { return m_NumResources; }
VkDescriptorSet GetVkDescriptorSet() const
{
return m_DescriptorSetAllocation.GetVkDescriptorSet();
}
// clang-format off
/* 0 */ const Uint32 m_NumResources = 0;
private:
friend ShaderResourceCacheVk;
Resource& GetResource(Uint32 CacheOffset)
{
VERIFY(CacheOffset < m_NumResources, "Offset ", CacheOffset, " is out of range");
return m_pResources[CacheOffset];
}
/* 8 */ Resource* const m_pResources = nullptr;
/*16 */ DescriptorSetAllocation m_DescriptorSetAllocation;
/*48 */ // End of structure
// clang-format on
};
const DescriptorSet& GetDescriptorSet(Uint32 Index) const
{
VERIFY_EXPR(Index < m_NumSets);
return reinterpret_cast<const DescriptorSet*>(m_pMemory.get())[Index];
}
void AssignDescriptorSetAllocation(Uint32 SetIndex, DescriptorSetAllocation&& Allocation)
{
auto& DescrSet = GetDescriptorSet(SetIndex);
VERIFY(DescrSet.GetSize() > 0, "Descriptor set is empty");
VERIFY(!DescrSet.m_DescriptorSetAllocation, "Descriptor set alloction has already been initialized");
DescrSet.m_DescriptorSetAllocation = std::move(Allocation);
}
// Sets the resource at the given desriptor set index and offset
const Resource& SetResource(const VulkanUtilities::VulkanLogicalDevice* pLogicalDevice,
Uint32 SetIndex,
Uint32 Offset,
Uint32 BindingIndex,
Uint32 ArrayIndex,
RefCntAutoPtr<IDeviceObject>&& pObject);
const Resource& ResetResource(Uint32 SetIndex,
Uint32 Offset)
{
return SetResource(nullptr, SetIndex, Offset, ~0u, ~0u, RefCntAutoPtr<IDeviceObject>{});
}
Uint32 GetNumDescriptorSets() const { return m_NumSets; }
Uint32 GetNumDynamicBuffers() const { return m_NumDynamicBuffers; }
ResourceCacheContentType GetContentType() const { return static_cast<ResourceCacheContentType>(m_ContentType); }
#ifdef DILIGENT_DEBUG
// Only for debug purposes: indicates what types of resources are stored in the cache
void DbgVerifyResourceInitialization() const;
void DbgVerifyDynamicBuffersCounter() const;
#endif
template <bool VerifyOnly>
void TransitionResources(DeviceContextVkImpl* pCtxVkImpl);
__forceinline Uint32 GetDynamicBufferOffsets(Uint32 CtxId, DeviceContextVkImpl* pCtxVkImpl, std::vector<uint32_t>& Offsets) const;
private:
Resource* GetFirstResourcePtr()
{
return reinterpret_cast<Resource*>(reinterpret_cast<DescriptorSet*>(m_pMemory.get()) + m_NumSets);
}
const Resource* GetFirstResourcePtr() const
{
return reinterpret_cast<const Resource*>(reinterpret_cast<const DescriptorSet*>(m_pMemory.get()) + m_NumSets);
}
DescriptorSet& GetDescriptorSet(Uint32 Index)
{
VERIFY_EXPR(Index < m_NumSets);
return reinterpret_cast<DescriptorSet*>(m_pMemory.get())[Index];
}
std::unique_ptr<void, STDDeleter<void, IMemoryAllocator>> m_pMemory;
Uint16 m_NumSets = 0;
// Total actual number of dynamic buffers (that were created with USAGE_DYNAMIC) bound in the resource cache
// regardless of the variable type. Note this variable is not equal to dynamic offsets count, which is constant.
Uint16 m_NumDynamicBuffers = 0;
Uint32 m_TotalResources : 31;
// Indicates what types of resources are stored in the cache
const Uint32 m_ContentType : 1;
#ifdef DILIGENT_DEBUG
// Debug array that stores flags indicating if resources in the cache have been initialized
std::vector<std::vector<bool>> m_DbgInitializedResources;
#endif
};
__forceinline Uint32 ShaderResourceCacheVk::GetDynamicBufferOffsets(Uint32 CtxId,
DeviceContextVkImpl* pCtxVkImpl,
std::vector<uint32_t>& Offsets) const
{
// If any of the sets being bound include dynamic uniform or storage buffers, then
// pDynamicOffsets includes one element for each array element in each dynamic descriptor
// type binding in each set. Values are taken from pDynamicOffsets in an order such that
// all entries for set N come before set N+1; within a set, entries are ordered by the binding
// numbers (unclear if this is SPIRV binding or VkDescriptorSetLayoutBinding number) in the
// descriptor set layouts; and within a binding array, elements are in order. (13.2.5)
// In each descriptor set, all uniform buffers with dynamic offsets (DescriptorType::UniformBufferDynamic)
// for every shader stage come first, followed by all storage buffers with dynamic offsets
// (DescriptorType::StorageBufferDynamic and DescriptorType::StorageBufferDynamic_ReadOnly) for every shader stage,
// followed by all other resources.
Uint32 OffsetInd = 0;
for (Uint32 set = 0; set < m_NumSets; ++set)
{
const auto& DescrSet = GetDescriptorSet(set);
const auto SetSize = DescrSet.GetSize();
Uint32 res = 0;
while (res < SetSize)
{
const auto& Res = DescrSet.GetResource(res);
if (Res.Type == DescriptorType::UniformBufferDynamic)
{
const auto* pBufferVk = Res.pObject.RawPtr<const BufferVkImpl>();
const auto Offset = pBufferVk != nullptr ? pBufferVk->GetDynamicOffset(CtxId, pCtxVkImpl) : 0;
Offsets[OffsetInd++] = Offset;
++res;
}
else
break;
}
while (res < SetSize)
{
const auto& Res = DescrSet.GetResource(res);
if (Res.Type == DescriptorType::StorageBufferDynamic ||
Res.Type == DescriptorType::StorageBufferDynamic_ReadOnly)
{
const auto* pBufferVkView = Res.pObject.RawPtr<const BufferViewVkImpl>();
const auto* pBufferVk = pBufferVkView != nullptr ? pBufferVkView->GetBuffer<const BufferVkImpl>() : nullptr;
const auto Offset = pBufferVk != nullptr ? pBufferVk->GetDynamicOffset(CtxId, pCtxVkImpl) : 0;
Offsets[OffsetInd++] = Offset;
++res;
}
else
break;
}
#ifdef DILIGENT_DEBUG
for (; res < SetSize; ++res)
{
const auto& Res = DescrSet.GetResource(res);
VERIFY((Res.Type != DescriptorType::UniformBufferDynamic &&
Res.Type != DescriptorType::StorageBufferDynamic &&
Res.Type != DescriptorType::StorageBufferDynamic_ReadOnly),
"All dynamic uniform and storage buffers are expected to go first in the beginning of each descriptor set");
}
#endif
}
return OffsetInd;
}
} // namespace Diligent
|