AccelerationStructure.h 3.4 KB

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