AccelerationStructure.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. // Copyright (C) 2009-2021, 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. {
  11. /// @addtogroup graphics
  12. /// @{
  13. /// @memberof AccelerationStructureInitInfo
  14. class BottomLevelAccelerationStructureInitInfo
  15. {
  16. public:
  17. BufferPtr m_indexBuffer;
  18. PtrSize m_indexBufferOffset = 0;
  19. U32 m_indexCount = 0;
  20. IndexType m_indexType = IndexType::COUNT;
  21. BufferPtr m_positionBuffer;
  22. PtrSize m_positionBufferOffset = 0;
  23. U32 m_positionStride = 0;
  24. Format m_positionsFormat = Format::NONE;
  25. U32 m_positionCount = 0;
  26. Bool isValid() const
  27. {
  28. if(m_indexBuffer.get() == nullptr || m_indexCount == 0 || m_indexType == IndexType::COUNT
  29. || m_positionBuffer.get() == nullptr || m_positionStride == 0 || m_positionsFormat == Format::NONE
  30. || m_positionCount == 0)
  31. {
  32. return false;
  33. }
  34. const PtrSize posRange = m_positionBufferOffset + PtrSize(m_positionStride) * m_positionCount;
  35. const PtrSize formatSize = getFormatInfo(m_positionsFormat).m_texelSize;
  36. if(m_positionStride < formatSize)
  37. {
  38. return false;
  39. }
  40. if(posRange > m_positionBuffer->getSize())
  41. {
  42. return false;
  43. }
  44. const PtrSize idxStride = (m_indexType == IndexType::U16) ? 2 : 4;
  45. if(m_indexBufferOffset + idxStride * m_indexCount > m_indexBuffer->getSize())
  46. {
  47. return false;
  48. }
  49. return true;
  50. }
  51. };
  52. /// @memberof AccelerationStructureInitInfo
  53. class AccelerationStructureInstance
  54. {
  55. public:
  56. AccelerationStructurePtr m_bottomLevel;
  57. Mat3x4 m_transform = Mat3x4::getIdentity();
  58. U32 m_hitgroupSbtRecordIndex = 0; ///< Points to a hitgroup SBT record.
  59. U8 m_mask = 0xFF; ///< A mask that this instance belongs to. Will be tested against what's in traceRayEXT().
  60. };
  61. /// @memberof AccelerationStructureInitInfo
  62. class TopLevelAccelerationStructureInitInfo
  63. {
  64. public:
  65. ConstWeakArray<AccelerationStructureInstance> m_instances;
  66. Bool isValid() const
  67. {
  68. return m_instances.getSize() > 0;
  69. }
  70. };
  71. /// Acceleration struture init info.
  72. /// @memberof AccelerationStructure
  73. class AccelerationStructureInitInfo : public GrBaseInitInfo
  74. {
  75. public:
  76. AccelerationStructureType m_type = AccelerationStructureType::COUNT;
  77. BottomLevelAccelerationStructureInitInfo m_bottomLevel;
  78. TopLevelAccelerationStructureInitInfo m_topLevel;
  79. AccelerationStructureInitInfo(CString name = {})
  80. : GrBaseInitInfo(name)
  81. {
  82. }
  83. Bool isValid() const
  84. {
  85. if(m_type == AccelerationStructureType::COUNT)
  86. {
  87. return false;
  88. }
  89. return (m_type == AccelerationStructureType::BOTTOM_LEVEL) ? m_bottomLevel.isValid() : m_topLevel.isValid();
  90. }
  91. };
  92. /// Acceleration structure GPU object.
  93. class AccelerationStructure : public GrObject
  94. {
  95. ANKI_GR_OBJECT
  96. public:
  97. static const GrObjectType CLASS_TYPE = GrObjectType::ACCELERATION_STRUCTURE;
  98. AccelerationStructureType getType() const
  99. {
  100. ANKI_ASSERT(m_type != AccelerationStructureType::COUNT);
  101. return m_type;
  102. }
  103. protected:
  104. AccelerationStructureType m_type = AccelerationStructureType::COUNT;
  105. /// Construct.
  106. AccelerationStructure(GrManager* manager, CString name)
  107. : GrObject(manager, CLASS_TYPE, name)
  108. {
  109. }
  110. /// Destroy.
  111. ~AccelerationStructure()
  112. {
  113. }
  114. private:
  115. /// Allocate and initialize a new instance.
  116. static ANKI_USE_RESULT AccelerationStructure* newInstance(GrManager* manager,
  117. const AccelerationStructureInitInfo& init);
  118. };
  119. /// @}
  120. } // end namespace anki