Go to the documentation of this file.
33 #include <unordered_map>
52 template <
typename EngineImplTraits>
53 class TopLevelASBase :
public DeviceObjectBase<typename EngineImplTraits::TopLevelASInterface, typename EngineImplTraits::RenderDeviceImplType, TopLevelASDesc>
57 using BaseInterface =
typename EngineImplTraits::TopLevelASInterface;
60 using RenderDeviceImplType =
typename EngineImplTraits::RenderDeviceImplType;
63 using BottomLevelASImplType =
typename EngineImplTraits::BottomLevelASImplType;
67 Uint32 ContributionToHitGroupIndex = 0;
70 #ifdef DILIGENT_DEVELOPMENT
84 RenderDeviceImplType* pDevice,
86 bool bIsDeviceInternal =
false) :
99 const
Uint32 InstanceCount,
100 const
Uint32 BaseContributionToHitGroupIndex,
101 const
Uint32 HitGroupStride,
108 size_t StringPoolSize = 0;
109 for (
Uint32 i = 0; i < InstanceCount; ++i)
111 VERIFY_EXPR(pInstances[i].InstanceName !=
nullptr);
117 Uint32 InstanceOffset = BaseContributionToHitGroupIndex;
119 for (
Uint32 i = 0; i < InstanceCount; ++i)
121 const auto& Inst = pInstances[i];
123 InstanceDesc Desc = {};
125 Desc.pBLAS = ValidatedCast<BottomLevelASImplType>(Inst.pBLAS);
126 Desc.ContributionToHitGroupIndex = Inst.ContributionToHitGroupIndex;
127 Desc.InstanceIndex = i;
128 CalculateHitGroupIndex(Desc, InstanceOffset, HitGroupStride, BindingMode);
130 #ifdef DILIGENT_DEVELOPMENT
131 Desc.Version = Desc.pBLAS->GetVersion();
133 bool IsUniqueName = this->
m_Instances.emplace(NameCopy, Desc).second;
148 #ifdef DILIGENT_DEVELOPMENT
149 this->m_DvpVersion.fetch_add(1);
155 #ifdef DILIGENT_DEVELOPMENT
156 this->m_DvpVersion.fetch_add(1);
164 const Uint32 InstanceCount,
165 const Uint32 BaseContributionToHitGroupIndex,
166 const Uint32 HitGroupStride,
170 #ifdef DILIGENT_DEVELOPMENT
171 bool Changed =
false;
173 Uint32 InstanceOffset = BaseContributionToHitGroupIndex;
175 for (
Uint32 i = 0; i < InstanceCount; ++i)
177 const auto& Inst = pInstances[i];
178 auto Iter = this->
m_Instances.find(Inst.InstanceName);
182 UNEXPECTED(
"Failed to find instance with name '", Inst.InstanceName,
"' in instances from the previous build");
186 auto& Desc = Iter->second;
187 const auto PrevIndex = Desc.ContributionToHitGroupIndex;
188 const auto pPrevBLAS = Desc.pBLAS;
190 Desc.pBLAS = ValidatedCast<BottomLevelASImplType>(Inst.pBLAS);
191 Desc.ContributionToHitGroupIndex = Inst.ContributionToHitGroupIndex;
193 CalculateHitGroupIndex(Desc, InstanceOffset, HitGroupStride, BindingMode);
195 #ifdef DILIGENT_DEVELOPMENT
196 Changed = Changed || (pPrevBLAS != Desc.pBLAS);
197 Changed = Changed || (PrevIndex != Desc.ContributionToHitGroupIndex);
198 Desc.Version = Desc.pBLAS->GetVersion();
204 #ifdef DILIGENT_DEVELOPMENT
210 this->m_DvpVersion.fetch_add(1);
227 for (
auto& SrcInst : Src.m_Instances)
230 this->
m_Instances.emplace(NameCopy, SrcInst.second);
235 #ifdef DILIGENT_DEVELOPMENT
236 this->m_DvpVersion.fetch_add(1);
250 const auto& Inst = Iter->second;
253 Result.
pBLAS = Inst.pBLAS.template RawPtr<IBottomLevelAS>();
275 "Unsupported state for top-level acceleration structure");
298 VERIFY((State & (State - 1)) == 0,
"Single state is expected");
300 return (this->
m_State & State) == State;
303 #ifdef DILIGENT_DEVELOPMENT
304 bool ValidateContent()
const
310 LOG_ERROR_MESSAGE(
"TLAS with name ('", this->
m_Desc.
Name,
"') doesn't have instances, use IDeviceContext::BuildTLAS() or IDeviceContext::CopyTLAS() to initialize TLAS content");
317 const InstanceDesc& Inst = NameAndInst.second;
319 if (Inst.Version != Inst.pBLAS->GetVersion())
321 LOG_ERROR_MESSAGE(
"Instance with name '", NameAndInst.first.GetStr(),
"' contains BLAS with name '", Inst.pBLAS->GetDesc().Name,
322 "' that was changed after TLAS build, you must rebuild TLAS");
328 LOG_ERROR_MESSAGE(
"Instance with name '", NameAndInst.first.GetStr(),
"' contains BLAS with name '", Inst.pBLAS->GetDesc().Name,
329 "' that must be in BUILD_AS_READ state, but current state is ",
339 return this->m_DvpVersion.load();
341 #endif // DILIGENT_DEVELOPMENT
344 void ClearInstanceData()
346 this->m_Instances.clear();
359 if (Desc.ContributionToHitGroupIndex == TLAS_INSTANCE_OFFSET_AUTO)
361 Desc.ContributionToHitGroupIndex = InstanceOffset;
369 default:
UNEXPECTED(
"Unknown ray tracing shader binding mode");
378 constexpr
Uint32 MaxIndex = (1u << 24);
379 VERIFY(Desc.ContributionToHitGroupIndex < MaxIndex,
"ContributionToHitGroupIndex must be less than ", MaxIndex);
387 std::unordered_map<HashMapStringKey, InstanceDesc, HashMapStringKey::Hasher>
m_Instances;
391 #ifdef DILIGENT_DEVELOPMENT
392 std::atomic<Uint32> m_DvpVersion{0};
bool CheckState(RESOURCE_STATE State) const
Definition: TopLevelASBase.hpp:296
Base interface for a reference counter object that stores the number of strong and weak references an...
Definition: ReferenceCounters.h:44
#define LOG_ERROR_MESSAGE(...)
Definition: Errors.hpp:122
virtual TLASBuildInfo GetBuildInfo() const override final
Implementation of ITopLevelAS::GetBuildInfo().
Definition: TopLevelASBase.hpp:266
void Clear()
Definition: StringPool.hpp:85
virtual void SetState(RESOURCE_STATE State) override final
Implementation of ITopLevelAS::SetState().
Definition: TopLevelASBase.hpp:272
virtual RESOURCE_STATE GetState() const override final
Implementation of ITopLevelAS::GetState().
Definition: TopLevelASBase.hpp:280
virtual TLASInstanceDesc GetInstanceDesc(const char *Name) const override final
Implementation of ITopLevelAS::GetInstanceDesc().
Definition: TopLevelASBase.hpp:241
ScratchBufferSizes m_ScratchSize
Definition: TopLevelASBase.hpp:385
#define LOG_ERROR_AND_THROW(...)
Definition: Errors.hpp:101
Uint32 InstanceCount
The number of instances, same as BuildTLASAttribs::InstanceCount.
Definition: TopLevelAS.h:104
Top-level AS description.
Definition: TopLevelAS.h:49
Defines TLAS state that was used in the last build.
Definition: TopLevelAS.h:101
@ HIT_GROUP_BINDING_MODE_PER_GEOMETRY
Each geometry in every instance may use a unique hit shader group. In this mode, the SBT reserves spa...
Definition: TopLevelAS.h:78
size_t GetRemainingSize() const
Definition: StringPool.hpp:154
@ HIT_GROUP_BINDING_MODE_USER_DEFINED
The user must specify TLASBuildInstanceData::ContributionToHitGroupIndex and only use IShaderBindingT...
Definition: TopLevelAS.h:94
RESOURCE_STATE m_State
Definition: TopLevelASBase.hpp:383
IBottomLevelAS * pBLAS
Bottom-level AS that is specified in TLASBuildInstanceData::pBLAS.
Definition: TopLevelAS.h:132
TLASBuildInfo m_BuildInfo
Definition: TopLevelASBase.hpp:384
#define UNEXPECTED(...)
Definition: DebugUtilities.hpp:77
struct TopLevelASDesc TopLevelASDesc
Definition: TopLevelAS.h:68
Defines the scratch buffer info for acceleration structure.
Definition: BottomLevelAS.h:177
TopLevelASDesc m_Desc
Object description.
Definition: DeviceObjectBase.hpp:182
Char * CopyString(const String &Str)
Definition: StringPool.hpp:124
@ HIT_GROUP_BINDING_MODE_LAST
Definition: TopLevelAS.h:96
virtual ScratchBufferSizes GetScratchBufferSizes() const override final
Implementation of ITopLevelAS::GetScratchBufferSizes().
Definition: TopLevelASBase.hpp:286
~TopLevelASBase()
Definition: TopLevelASBase.hpp:92
This structure is used by BuildTLASAttribs.
Definition: DeviceContext.h:1040
Top-level AS instance description.
Definition: TopLevelAS.h:122
IMemoryAllocator & GetRawAllocator()
Returns raw memory allocator.
Definition: EngineMemory.cpp:51
const Char * GetResourceStateFlagString(RESOURCE_STATE State)
Returns the string containing the buffer mode description.
Definition: GraphicsAccessories.cpp:1045
#define IMPLEMENT_QUERY_INTERFACE_IN_PLACE(InterfaceID, ParentClassName)
Definition: ObjectBase.hpp:59
@ RESOURCE_STATE_RAY_TRACING
The resource is used as a top-level AS shader resource in a trace rays operation.
Definition: GraphicsTypes.h:2878
#define DILIGENT_CALL_TYPE
Definition: CommonDefinitions.h:45
uint32_t Uint32
32-bit unsigned integer
Definition: BasicTypes.h:51
Uint32 FirstContributionToHitGroupIndex
First hit group location, same as BuildTLASAttribs::BaseContributionToHitGroupIndex.
Definition: TopLevelAS.h:113
Uint32 HitGroupStride
The number of hit shader groups, same as BuildTLASAttribs::HitGroupStride.
Definition: TopLevelAS.h:107
@ RESOURCE_STATE_UNKNOWN
The resource state is not known to the engine and is managed by the application.
Definition: GraphicsTypes.h:2817
std::unordered_map< HashMapStringKey, InstanceDesc, HashMapStringKey::Hasher > m_Instances
Definition: TopLevelASBase.hpp:387
@ RESOURCE_STATE_BUILD_AS_WRITE
The resource is used as the target for AS building or AS copy operations.
Definition: GraphicsTypes.h:2875
StringPool m_StringPool
Definition: TopLevelASBase.hpp:389
DeviceObjectBase< BaseInterface, RenderDeviceImplType, TopLevelASDesc > TDeviceObjectBase
Definition: TopLevelASBase.hpp:76
void ValidateTopLevelASDesc(const TopLevelASDesc &Desc) noexcept(false)
Validates top-level AS description and throws an exception in case of an error.
Definition: TopLevelASBase.cpp:33
bool SetInstanceData(const TLASBuildInstanceData *pInstances, const Uint32 InstanceCount, const Uint32 BaseContributionToHitGroupIndex, const Uint32 HitGroupStride, const HIT_GROUP_BINDING_MODE BindingMode) noexcept
Definition: TopLevelASBase.hpp:98
@ HIT_GROUP_BINDING_MODE_PER_TLAS
All instances in each TLAS will use the same hit group. In this mode, the SBT reserves a single slot ...
Definition: TopLevelAS.h:90
HIT_GROUP_BINDING_MODE
Defines hit group binding mode used by the top-level AS.
Definition: TopLevelAS.h:72
void Reserve(size_t Size, IMemoryAllocator &Allocator)
Definition: StringPool.hpp:71
TopLevelASBase(IReferenceCounters *pRefCounters, RenderDeviceImplType *pDevice, const TopLevelASDesc &Desc, bool bIsDeviceInternal=false)
Definition: TopLevelASBase.hpp:83
Uint32 InstanceIndex
The autogenerated index of the instance. Same as InstanceIndex() in HLSL and gl_InstanceID in GLSL.
Definition: TopLevelAS.h:129
#define VERIFY_EXPR(...)
Definition: DebugUtilities.hpp:79
@ RESOURCE_STATE_BUILD_AS_READ
The resource is used as vertex/index/instance buffer in an AS building operation or as an acceleratio...
Definition: GraphicsTypes.h:2872
#define VERIFY(...)
Definition: DebugUtilities.hpp:76
Implementation of a simple fixed-size string pool.
Definition: StringPool.hpp:42
void CopyInstancceData(const TopLevelASBase &Src) noexcept
Definition: TopLevelASBase.hpp:220
Uint32 LastContributionToHitGroupIndex
Last hit group location.
Definition: TopLevelAS.h:116
bool IsInKnownState() const
Definition: TopLevelASBase.hpp:291
RESOURCE_STATE
Resource usage state.
Definition: GraphicsTypes.h:2814
@ HIT_GROUP_BINDING_MODE_PER_INSTANCE
Each instance may use a unique hit shader group. In this mode, the SBT reserves one slot for each ins...
Definition: TopLevelAS.h:84
Template class implementing base functionality of the top-level acceleration structure object.
Definition: TopLevelASBase.hpp:53
Uint32 ContributionToHitGroupIndex
Index that corresponds to the one specified in TLASBuildInstanceData::ContributionToHitGroupIndex.
Definition: TopLevelAS.h:125
HIT_GROUP_BINDING_MODE BindingMode
Hit group binding mode, same as BuildTLASAttribs::BindingMode.
Definition: TopLevelAS.h:110
const Char * Name
Object name.
Definition: GraphicsTypes.h:1199
Template class implementing base functionality of the device object.
Definition: DeviceObjectBase.hpp:45
bool UpdateInstances(const TLASBuildInstanceData *pInstances, const Uint32 InstanceCount, const Uint32 BaseContributionToHitGroupIndex, const Uint32 HitGroupStride, const HIT_GROUP_BINDING_MODE BindingMode) noexcept
Definition: TopLevelASBase.hpp:163
The library uses Direct3D-style math:
Definition: AdvancedMath.hpp:37
static size_t GetRequiredReserveSize(const char *str)
Definition: StringPool.hpp:98