AccelerationStructure.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. // Copyright (C) 2009-2023, Panagiotis Christopoulos Charitos and contributors.
  2. // All rights reserved.
  3. // Code licensed under the BSD License.
  4. // http://www.anki3d.org/LICENSE
  5. #pragma once
  6. #include <AnKi/Gr/Buffer.h>
  7. #include <AnKi/Math.h>
  8. #include <AnKi/Util/WeakArray.h>
  9. namespace anki {
  10. /// @addtogroup graphics
  11. /// @{
  12. /// @memberof AccelerationStructureInitInfo
  13. class BottomLevelAccelerationStructureInitInfo
  14. {
  15. public:
  16. BufferPtr m_indexBuffer;
  17. PtrSize m_indexBufferOffset = 0;
  18. U32 m_indexCount = 0;
  19. IndexType m_indexType = IndexType::kCount;
  20. BufferPtr m_positionBuffer;
  21. PtrSize m_positionBufferOffset = 0;
  22. U32 m_positionStride = 0;
  23. Format m_positionsFormat = Format::kNone;
  24. U32 m_positionCount = 0;
  25. Bool isValid() const
  26. {
  27. if(m_indexBuffer.get() == nullptr || m_indexCount == 0 || m_indexType == IndexType::kCount || m_positionBuffer.get() == nullptr
  28. || m_positionStride == 0 || m_positionsFormat == Format::kNone || m_positionCount == 0)
  29. {
  30. return false;
  31. }
  32. const PtrSize posRange = m_positionBufferOffset + PtrSize(m_positionStride) * m_positionCount;
  33. const PtrSize formatSize = getFormatInfo(m_positionsFormat).m_texelSize;
  34. if(m_positionStride < formatSize)
  35. {
  36. return false;
  37. }
  38. if(posRange > m_positionBuffer->getSize())
  39. {
  40. return false;
  41. }
  42. const PtrSize idxStride = (m_indexType == IndexType::kU16) ? 2 : 4;
  43. if(m_indexBufferOffset + idxStride * m_indexCount > m_indexBuffer->getSize())
  44. {
  45. return false;
  46. }
  47. return true;
  48. }
  49. };
  50. /// @memberof AccelerationStructureInitInfo
  51. class AccelerationStructureInstance
  52. {
  53. public:
  54. AccelerationStructurePtr m_bottomLevel;
  55. Mat3x4 m_transform = Mat3x4::getIdentity();
  56. U32 m_hitgroupSbtRecordIndex = 0; ///< Points to a hitgroup SBT record.
  57. U8 m_mask = 0xFF; ///< A mask that this instance belongs to. Will be tested against what's in traceRayEXT().
  58. };
  59. /// @memberof AccelerationStructureInitInfo
  60. class TopLevelAccelerationStructureInitInfo
  61. {
  62. public:
  63. ConstWeakArray<AccelerationStructureInstance> m_instances;
  64. Bool isValid() const
  65. {
  66. return m_instances.getSize() > 0;
  67. }
  68. };
  69. /// Acceleration struture init info.
  70. /// @memberof AccelerationStructure
  71. class AccelerationStructureInitInfo : public GrBaseInitInfo
  72. {
  73. public:
  74. AccelerationStructureType m_type = AccelerationStructureType::kCount;
  75. BottomLevelAccelerationStructureInitInfo m_bottomLevel;
  76. TopLevelAccelerationStructureInitInfo m_topLevel;
  77. AccelerationStructureInitInfo(CString name = {})
  78. : GrBaseInitInfo(name)
  79. {
  80. }
  81. Bool isValid() const
  82. {
  83. if(m_type == AccelerationStructureType::kCount)
  84. {
  85. return false;
  86. }
  87. return (m_type == AccelerationStructureType::kBottomLevel) ? m_bottomLevel.isValid() : m_topLevel.isValid();
  88. }
  89. };
  90. /// Acceleration structure GPU object.
  91. class AccelerationStructure : public GrObject
  92. {
  93. ANKI_GR_OBJECT
  94. public:
  95. static constexpr GrObjectType kClassType = GrObjectType::kAccelerationStructure;
  96. AccelerationStructureType getType() const
  97. {
  98. ANKI_ASSERT(m_type != AccelerationStructureType::kCount);
  99. return m_type;
  100. }
  101. protected:
  102. AccelerationStructureType m_type = AccelerationStructureType::kCount;
  103. /// Construct.
  104. AccelerationStructure(CString name)
  105. : GrObject(kClassType, name)
  106. {
  107. }
  108. /// Destroy.
  109. ~AccelerationStructure()
  110. {
  111. }
  112. private:
  113. /// Allocate and initialize a new instance.
  114. [[nodiscard]] static AccelerationStructure* newInstance(const AccelerationStructureInitInfo& init);
  115. };
  116. /// @}
  117. } // end namespace anki