Diligent Engine  v.2.4.g
BottomLevelASBase.hpp
Go to the documentation of this file.
1 /*
2  * Copyright 2019-2021 Diligent Graphics LLC
3  * Copyright 2015-2019 Egor Yusov
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * In no event and under no legal theory, whether in tort (including negligence),
18  * contract, or otherwise, unless required by applicable law (such as deliberate
19  * and grossly negligent acts) or agreed to in writing, shall any Contributor be
20  * liable for any damages, including any direct, indirect, special, incidental,
21  * or consequential damages of any character arising as a result of this License or
22  * out of the use or inability to use the software (including but not limited to damages
23  * for loss of goodwill, work stoppage, computer failure or malfunction, or any and
24  * all other commercial damages or losses), even if such Contributor has been advised
25  * of the possibility of such damages.
26  */
27 
28 #pragma once
29 
32 
33 #include <unordered_map>
34 #include <atomic>
35 
36 #include "BottomLevelAS.h"
37 #include "DeviceObjectBase.hpp"
38 #include "RenderDeviceBase.hpp"
39 #include "FixedLinearAllocator.hpp"
40 #include "HashUtils.hpp"
41 
42 namespace Diligent
43 {
44 
46 {
47  Uint32 IndexInDesc = INVALID_INDEX; // Geometry index in BottomLevelASDesc
48  Uint32 ActualIndex = INVALID_INDEX; // Geometry index in build operation
49 
51  BLASGeomIndex(Uint32 _IndexInDesc,
52  Uint32 _ActualIndex) :
53  IndexInDesc{_IndexInDesc},
54  ActualIndex{_ActualIndex}
55  {}
56 };
57 using BLASNameToIndex = std::unordered_map<HashMapStringKey, BLASGeomIndex, HashMapStringKey::Hasher>;
58 
60 void ValidateBottomLevelASDesc(const BottomLevelASDesc& Desc) noexcept(false);
61 
63 void CopyBLASGeometryDesc(const BottomLevelASDesc& SrcDesc,
64  BottomLevelASDesc& DstDesc,
65  FixedLinearAllocator& MemPool,
66  const BLASNameToIndex* pSrcNameToIndex,
67  BLASNameToIndex& DstNameToIndex) noexcept(false);
68 
69 
71 
73 template <typename EngineImplTraits>
74 class BottomLevelASBase : public DeviceObjectBase<typename EngineImplTraits::BottomLevelASInterface, typename EngineImplTraits::RenderDeviceImplType, BottomLevelASDesc>
75 {
76 public:
77  // Base interface that this class inherits (IBottomLevelASD3D12 or IBottomLevelASVk).
78  using BaseInterface = typename EngineImplTraits::BottomLevelASInterface;
79 
80  // Render device implementation type (RenderDeviceD3D12Impl or RenderDeviceVkImpl).
81  using RenderDeviceImplType = typename EngineImplTraits::RenderDeviceImplType;
82 
84 
91  RenderDeviceImplType* pDevice,
92  const BottomLevelASDesc& Desc,
93  bool bIsDeviceInternal = false) :
94  TDeviceObjectBase{pRefCounters, pDevice, Desc, bIsDeviceInternal}
95  {
97 
98  if (Desc.CompactedSize > 0)
99  {
100  }
101  else
102  {
103  CopyGeometryDescriptionUnsafe(Desc, nullptr);
104  }
105  }
106 
108  {
109  ClearGeometry();
110  }
111 
113 
114  // Maps geometry that was used in a build operation to the geometry description.
115  // Returns the geometry index in geometry description.
116  Uint32 UpdateGeometryIndex(const char* Name, Uint32& ActualIndex, bool OnUpdate)
117  {
118  DEV_CHECK_ERR(Name != nullptr && Name[0] != '\0', "Geometry name must not be empty");
119 
120  auto iter = m_NameToIndex.find(Name);
121  if (iter != m_NameToIndex.end())
122  {
123  if (OnUpdate)
124  ActualIndex = iter->second.ActualIndex;
125  else
126  iter->second.ActualIndex = ActualIndex;
127  return iter->second.IndexInDesc;
128  }
129  LOG_ERROR_MESSAGE("Can't find geometry with name '", Name, '\'');
130  return INVALID_INDEX;
131  }
132 
134  virtual Uint32 DILIGENT_CALL_TYPE GetGeometryDescIndex(const char* Name) const override final
135  {
136  DEV_CHECK_ERR(Name != nullptr && Name[0] != '\0', "Geometry name must not be empty");
137 
138  auto iter = m_NameToIndex.find(Name);
139  if (iter != m_NameToIndex.end())
140  return iter->second.IndexInDesc;
141 
142  LOG_ERROR_MESSAGE("Can't find geometry with name '", Name, '\'');
143  return INVALID_INDEX;
144  }
145 
147  virtual Uint32 DILIGENT_CALL_TYPE GetGeometryIndex(const char* Name) const override final
148  {
149  DEV_CHECK_ERR(Name != nullptr && Name[0] != '\0', "Geometry name must not be emtpy");
150 
151  auto iter = m_NameToIndex.find(Name);
152  if (iter != m_NameToIndex.end())
153  {
154  VERIFY(iter->second.ActualIndex != INVALID_INDEX, "Geometry with name '", Name, "', exists, but was not enabled in the last build");
155  return iter->second.ActualIndex;
156  }
157  LOG_ERROR_MESSAGE("Can't find geometry with name '", Name, '\'');
158  return INVALID_INDEX;
159  }
160 
162  virtual void DILIGENT_CALL_TYPE SetState(RESOURCE_STATE State) override final
163  {
165  "Unsupported state for a bottom-level acceleration structure");
166  this->m_State = State;
167  }
168 
170  virtual RESOURCE_STATE DILIGENT_CALL_TYPE GetState() const override final
171  {
172  return this->m_State;
173  }
174 
177  {
178  return this->m_ScratchSize;
179  }
180 
181  bool IsInKnownState() const
182  {
183  return this->m_State != RESOURCE_STATE_UNKNOWN;
184  }
185 
186  bool CheckState(RESOURCE_STATE State) const
187  {
188  DEV_CHECK_ERR((State & (State - 1)) == 0, "Single state is expected");
189  DEV_CHECK_ERR(IsInKnownState(), "BLAS state is unknown");
190  return (this->m_State & State) == State;
191  }
192 
193 #ifdef DILIGENT_DEVELOPMENT
194  void UpdateVersion()
195  {
196  this->m_DvpVersion.fetch_add(1);
197  }
198 
199  Uint32 GetVersion() const
200  {
201  return this->m_DvpVersion.load();
202  }
203 #endif // DILIGENT_DEVELOPMENT
204 
205  void CopyGeometryDescription(const BottomLevelASBase& SrcBLAS) noexcept
206  {
207  ClearGeometry();
208 
209  try
210  {
211  CopyGeometryDescriptionUnsafe(SrcBLAS.GetDesc(), &SrcBLAS.m_NameToIndex);
212  }
213  catch (...)
214  {
215  ClearGeometry();
216  }
217  }
218 
220  {
222  }
223 
224  virtual Uint32 DILIGENT_CALL_TYPE GetActualGeometryCount() const override final
225  {
226  return m_GeometryCount;
227  }
228 
229 private:
230  void CopyGeometryDescriptionUnsafe(const BottomLevelASDesc& SrcDesc, const BLASNameToIndex* pSrcNameToIndex) noexcept(false)
231  {
233  CopyBLASGeometryDesc(SrcDesc, this->m_Desc, MemPool, pSrcNameToIndex, this->m_NameToIndex);
234  this->m_pRawPtr = MemPool.Release();
235  }
236 
237  void ClearGeometry() noexcept
238  {
239  if (this->m_pRawPtr != nullptr)
240  {
241  GetRawAllocator().Free(this->m_pRawPtr);
242  this->m_pRawPtr = nullptr;
243  }
244 
245  // Keep Name, Flags, CompactedSize, CommandQueueMask
246  this->m_Desc.pTriangles = nullptr;
247  this->m_Desc.TriangleCount = 0;
248  this->m_Desc.pBoxes = nullptr;
249  this->m_Desc.BoxCount = 0;
250 
251  m_NameToIndex = decltype(m_NameToIndex){};
252  }
253 
254 protected:
257  void* m_pRawPtr = nullptr;
260 
261 #ifdef DILIGENT_DEVELOPMENT
262  std::atomic<Uint32> m_DvpVersion{0};
263 #endif
264 };
265 
266 } // namespace Diligent
Diligent::FixedLinearAllocator
Implementation of a linear allocator on a fixed-size memory page.
Definition: FixedLinearAllocator.hpp:45
Diligent::BottomLevelASBase::m_State
RESOURCE_STATE m_State
Definition: BottomLevelASBase.hpp:255
Diligent::BottomLevelASBase::UpdateGeometryIndex
Uint32 UpdateGeometryIndex(const char *Name, Uint32 &ActualIndex, bool OnUpdate)
Definition: BottomLevelASBase.hpp:116
Diligent::IReferenceCounters
Base interface for a reference counter object that stores the number of strong and weak references an...
Definition: ReferenceCounters.h:44
LOG_ERROR_MESSAGE
#define LOG_ERROR_MESSAGE(...)
Definition: Errors.hpp:122
DeviceObjectBase.hpp
Diligent::BLASGeomIndex::BLASGeomIndex
BLASGeomIndex()
Definition: BottomLevelASBase.hpp:50
Diligent::BLASGeomIndex::ActualIndex
Uint32 ActualIndex
Definition: BottomLevelASBase.hpp:48
Diligent::BottomLevelASBase::m_GeometryCount
Uint32 m_GeometryCount
Definition: BottomLevelASBase.hpp:258
Diligent::BottomLevelASDesc::pBoxes
const BLASBoundingBoxDesc * pBoxes
Array of AABB geometry descriptions.
Definition: BottomLevelAS.h:154
Diligent::BottomLevelASBase::SetActualGeometryCount
void SetActualGeometryCount(Uint32 Count)
Definition: BottomLevelASBase.hpp:219
Diligent::ScratchBufferSizes
Defines the scratch buffer info for acceleration structure.
Definition: BottomLevelAS.h:177
Diligent::DeviceObjectBase< EngineImplTraits::BottomLevelASInterface, EngineImplTraits::RenderDeviceImplType, BottomLevelASDesc >::m_Desc
BottomLevelASDesc m_Desc
Object description.
Definition: DeviceObjectBase.hpp:182
Diligent::BottomLevelASBase::SetState
virtual void SetState(RESOURCE_STATE State) override final
Implementation of IBottomLevelAS::SetState()
Definition: BottomLevelASBase.hpp:162
DEV_CHECK_ERR
#define DEV_CHECK_ERR(...)
Definition: DebugUtilities.hpp:90
Diligent::BottomLevelASBase::BottomLevelASBase
BottomLevelASBase(IReferenceCounters *pRefCounters, RenderDeviceImplType *pDevice, const BottomLevelASDesc &Desc, bool bIsDeviceInternal=false)
Definition: BottomLevelASBase.hpp:90
Diligent::BottomLevelASBase::CopyGeometryDescription
void CopyGeometryDescription(const BottomLevelASBase &SrcBLAS) noexcept
Definition: BottomLevelASBase.hpp:205
RenderDeviceBase.hpp
Diligent::BottomLevelASBase::~BottomLevelASBase
~BottomLevelASBase()
Definition: BottomLevelASBase.hpp:107
Diligent::BottomLevelASBase::CheckState
bool CheckState(RESOURCE_STATE State) const
Definition: BottomLevelASBase.hpp:186
Diligent::DescriptorType::Count
@ Count
Diligent::BottomLevelASBase::GetGeometryDescIndex
virtual Uint32 GetGeometryDescIndex(const char *Name) const override final
Implementation of IBottomLevelAS::GetGeometryDescIndex()
Definition: BottomLevelASBase.hpp:134
Diligent::GetRawAllocator
IMemoryAllocator & GetRawAllocator()
Returns raw memory allocator.
Definition: EngineMemory.cpp:51
Diligent::BLASNameToIndex
std::unordered_map< HashMapStringKey, BLASGeomIndex, HashMapStringKey::Hasher > BLASNameToIndex
Definition: BottomLevelASBase.hpp:57
Diligent::ValidateBottomLevelASDesc
void ValidateBottomLevelASDesc(const BottomLevelASDesc &Desc) noexcept(false)
Validates bottom-level AS description and throws an exception in case of an error.
Definition: BottomLevelASBase.cpp:33
IMPLEMENT_QUERY_INTERFACE_IN_PLACE
#define IMPLEMENT_QUERY_INTERFACE_IN_PLACE(InterfaceID, ParentClassName)
Definition: ObjectBase.hpp:59
FixedLinearAllocator.hpp
Diligent::BottomLevelASBase< EngineD3D12ImplTraits >::BaseInterface
typename EngineD3D12ImplTraits ::BottomLevelASInterface BaseInterface
Definition: BottomLevelASBase.hpp:78
DILIGENT_CALL_TYPE
#define DILIGENT_CALL_TYPE
Definition: CommonDefinitions.h:45
Diligent::BottomLevelASDesc::pTriangles
const BLASTriangleDesc * pTriangles
Array of triangle geometry descriptions.
Definition: BottomLevelAS.h:148
Diligent::Uint32
uint32_t Uint32
32-bit unsigned integer
Definition: BasicTypes.h:51
Diligent::BottomLevelASBase::m_pRawPtr
void * m_pRawPtr
Definition: BottomLevelASBase.hpp:257
Diligent::RESOURCE_STATE_UNKNOWN
@ RESOURCE_STATE_UNKNOWN
The resource state is not known to the engine and is managed by the application.
Definition: GraphicsTypes.h:2817
Diligent::RESOURCE_STATE_BUILD_AS_WRITE
@ RESOURCE_STATE_BUILD_AS_WRITE
The resource is used as the target for AS building or AS copy operations.
Definition: GraphicsTypes.h:2875
Diligent::BottomLevelASBase::GetState
virtual RESOURCE_STATE GetState() const override final
Implementation of IBottomLevelAS::GetState()
Definition: BottomLevelASBase.hpp:170
Diligent::BottomLevelASBase
Template class implementing base functionality of the bottom-level acceleration structure object.
Definition: BottomLevelASBase.hpp:74
Diligent::BottomLevelASDesc::TriangleCount
Uint32 TriangleCount
The number of triangle geometries in pTriangles array.
Definition: BottomLevelAS.h:151
HashUtils.hpp
Diligent::BottomLevelASBase::TDeviceObjectBase
DeviceObjectBase< BaseInterface, RenderDeviceImplType, BottomLevelASDesc > TDeviceObjectBase
Definition: BottomLevelASBase.hpp:83
Diligent::BottomLevelASBase::GetGeometryIndex
virtual Uint32 GetGeometryIndex(const char *Name) const override final
Implementation of IBottomLevelAS::GetGeometryIndex()
Definition: BottomLevelASBase.hpp:147
Diligent::BottomLevelASBase::GetActualGeometryCount
virtual Uint32 GetActualGeometryCount() const override final
Definition: BottomLevelASBase.hpp:224
Diligent::RESOURCE_STATE_BUILD_AS_READ
@ 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
VERIFY
#define VERIFY(...)
Definition: DebugUtilities.hpp:76
Diligent::BottomLevelASBase::m_NameToIndex
BLASNameToIndex m_NameToIndex
Definition: BottomLevelASBase.hpp:256
Diligent::BottomLevelASBase::IsInKnownState
bool IsInKnownState() const
Definition: BottomLevelASBase.hpp:181
Diligent::RESOURCE_STATE
RESOURCE_STATE
Resource usage state.
Definition: GraphicsTypes.h:2814
Diligent::BottomLevelASDesc::BoxCount
Uint32 BoxCount
The number of AABB geometries in pBoxes array.
Definition: BottomLevelAS.h:157
Diligent::BottomLevelASBase< EngineD3D12ImplTraits >::RenderDeviceImplType
typename EngineD3D12ImplTraits ::RenderDeviceImplType RenderDeviceImplType
Definition: BottomLevelASBase.hpp:81
Diligent::BottomLevelASBase::GetScratchBufferSizes
virtual ScratchBufferSizes GetScratchBufferSizes() const override final
Implementation of IBottomLevelAS::GetScratchBufferSizes()
Definition: BottomLevelASBase.hpp:176
Diligent::BLASGeomIndex::BLASGeomIndex
BLASGeomIndex(Uint32 _IndexInDesc, Uint32 _ActualIndex)
Definition: BottomLevelASBase.hpp:51
Diligent::BottomLevelASDesc
Bottom-level AS description.
Definition: BottomLevelAS.h:145
Diligent::IMemoryAllocator::Free
virtual void Free(void *Ptr)=0
Releases memory.
Diligent::BLASGeomIndex::IndexInDesc
Uint32 IndexInDesc
Definition: BottomLevelASBase.hpp:47
Diligent::BLASGeomIndex
Definition: BottomLevelASBase.hpp:45
Diligent::CopyBLASGeometryDesc
void CopyBLASGeometryDesc(const BottomLevelASDesc &SrcDesc, BottomLevelASDesc &DstDesc, FixedLinearAllocator &MemPool, const BLASNameToIndex *pSrcNameToIndex, BLASNameToIndex &DstNameToIndex) noexcept(false)
Copies bottom-level AS geometry description using MemPool to allocate required space.
Definition: BottomLevelASBase.cpp:108
Diligent::DeviceObjectBase
Template class implementing base functionality of the device object.
Definition: DeviceObjectBase.hpp:45
Diligent
The library uses Direct3D-style math:
Definition: AdvancedMath.hpp:37
BottomLevelAS.h
Diligent::BottomLevelASBase::m_ScratchSize
ScratchBufferSizes m_ScratchSize
Definition: BottomLevelASBase.hpp:259