AccelerationStructure.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. // Copyright (C) 2009-present, 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. BufferView m_indexBuffer;
  17. U32 m_indexCount = 0;
  18. IndexType m_indexType = IndexType::kCount;
  19. BufferView m_positionBuffer;
  20. U32 m_positionStride = 0;
  21. Format m_positionsFormat = Format::kNone;
  22. U32 m_positionCount = 0;
  23. Bool isValid() const
  24. {
  25. Bool valid = true;
  26. valid = valid && (m_indexBuffer.isValid() && m_indexCount * getIndexSize(m_indexType) == m_indexBuffer.getRange());
  27. const U32 vertSize = getFormatInfo(m_positionsFormat).m_texelSize;
  28. valid = valid
  29. && (m_positionBuffer.isValid() && m_positionStride >= vertSize && m_positionStride * m_positionCount == m_positionBuffer.getRange());
  30. return valid;
  31. }
  32. };
  33. /// @memberof AccelerationStructureInitInfo
  34. class AccelerationStructureInstanceInfo
  35. {
  36. public:
  37. AccelerationStructurePtr m_bottomLevel;
  38. Mat3x4 m_transform = Mat3x4::getIdentity();
  39. U32 m_hitgroupSbtRecordIndex = 0; ///< Points to a hitgroup SBT record.
  40. U8 m_mask = 0xFF; ///< A mask that this instance belongs to. Will be tested against what's in traceRayEXT().
  41. };
  42. /// @memberof AccelerationStructureInitInfo
  43. class TopLevelAccelerationStructureInitInfo
  44. {
  45. public:
  46. class
  47. {
  48. public:
  49. ConstWeakArray<AccelerationStructureInstanceInfo> m_instances;
  50. } m_directArgs; ///< Pass some representation of the instances.
  51. class
  52. {
  53. public:
  54. U32 m_maxInstanceCount = 0;
  55. BufferView m_instancesBuffer; ///< Filled with AccelerationStructureInstance structs.
  56. } m_indirectArgs; ///< Pass the instances GPU buffer directly.
  57. Bool isValid() const
  58. {
  59. return m_directArgs.m_instances.getSize() > 0
  60. || (m_indirectArgs.m_maxInstanceCount > 0 && m_indirectArgs.m_instancesBuffer.isValid()
  61. && m_indirectArgs.m_instancesBuffer.getRange() == sizeof(AccelerationStructureInstance) * m_indirectArgs.m_maxInstanceCount);
  62. }
  63. };
  64. /// Acceleration struture init info.
  65. /// @memberof AccelerationStructure
  66. class AccelerationStructureInitInfo : public GrBaseInitInfo
  67. {
  68. public:
  69. AccelerationStructureType m_type = AccelerationStructureType::kCount;
  70. BottomLevelAccelerationStructureInitInfo m_bottomLevel;
  71. TopLevelAccelerationStructureInitInfo m_topLevel;
  72. AccelerationStructureInitInfo(CString name = {})
  73. : GrBaseInitInfo(name)
  74. {
  75. }
  76. Bool isValid() const
  77. {
  78. if(m_type == AccelerationStructureType::kCount)
  79. {
  80. return false;
  81. }
  82. return (m_type == AccelerationStructureType::kBottomLevel) ? m_bottomLevel.isValid() : m_topLevel.isValid();
  83. }
  84. };
  85. /// Acceleration structure GPU object.
  86. class AccelerationStructure : public GrObject
  87. {
  88. ANKI_GR_OBJECT
  89. public:
  90. static constexpr GrObjectType kClassType = GrObjectType::kAccelerationStructure;
  91. AccelerationStructureType getType() const
  92. {
  93. ANKI_ASSERT(m_type != AccelerationStructureType::kCount);
  94. return m_type;
  95. }
  96. /// Get the size of the scratch buffer used in building this AS.
  97. PtrSize getBuildScratchBufferSize() const
  98. {
  99. ANKI_ASSERT(m_scratchBufferSize != 0);
  100. return m_scratchBufferSize;
  101. }
  102. U64 getGpuAddress() const;
  103. protected:
  104. PtrSize m_scratchBufferSize = 0;
  105. AccelerationStructureType m_type = AccelerationStructureType::kCount;
  106. /// Construct.
  107. AccelerationStructure(CString name)
  108. : GrObject(kClassType, name)
  109. {
  110. }
  111. /// Destroy.
  112. ~AccelerationStructure()
  113. {
  114. }
  115. private:
  116. /// Allocate and initialize a new instance.
  117. [[nodiscard]] static AccelerationStructure* newInstance(const AccelerationStructureInitInfo& init);
  118. };
  119. /// @}
  120. } // end namespace anki