MeshResource.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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/Resource/ResourceObject.h>
  7. #include <AnKi/Math.h>
  8. #include <AnKi/Gr.h>
  9. #include <AnKi/Collision/Aabb.h>
  10. #include <AnKi/Shaders/Include/MeshTypes.h>
  11. #include <AnKi/Core/GpuMemory/UnifiedGeometryBuffer.h>
  12. namespace anki {
  13. // Forward
  14. class MeshBinaryLoader;
  15. /// @addtogroup resource
  16. /// @{
  17. /// Mesh Resource. It contains the geometry packed in GPU buffers.
  18. class MeshResource : public ResourceObject
  19. {
  20. public:
  21. /// Default constructor
  22. MeshResource();
  23. ~MeshResource();
  24. /// Load from a mesh file
  25. Error load(const ResourceFilename& filename, Bool async);
  26. /// Get the complete bounding box.
  27. const Aabb& getBoundingShape() const
  28. {
  29. return m_aabb;
  30. }
  31. U32 getSubMeshCount() const
  32. {
  33. return m_subMeshes.getSize();
  34. }
  35. /// Get submesh info.
  36. void getSubMeshInfo(U32 lod, U32 subMeshId, U32& firstIndex, U32& indexCount, Aabb& aabb) const
  37. {
  38. const SubMesh& sm = m_subMeshes[subMeshId];
  39. firstIndex = sm.m_firstIndices[lod];
  40. indexCount = sm.m_indexCounts[lod];
  41. aabb = sm.m_aabb;
  42. }
  43. /// Get all info around vertex indices.
  44. void getIndexBufferInfo(U32 lod, PtrSize& buffOffset, U32& indexCount, IndexType& indexType) const
  45. {
  46. buffOffset = m_lods[lod].m_indexBufferAllocationToken.getOffset();
  47. ANKI_ASSERT(isAligned(getIndexSize(m_indexType), buffOffset));
  48. indexCount = m_lods[lod].m_indexCount;
  49. indexType = m_indexType;
  50. }
  51. /// Get vertex buffer info.
  52. void getVertexStreamInfo(U32 lod, VertexStreamId stream, PtrSize& bufferOffset, U32& vertexCount) const
  53. {
  54. bufferOffset = m_lods[lod].m_vertexBuffersAllocationToken[stream].getOffset();
  55. vertexCount = m_lods[lod].m_vertexCount;
  56. }
  57. const AccelerationStructurePtr& getBottomLevelAccelerationStructure(U32 lod) const
  58. {
  59. ANKI_ASSERT(m_lods[lod].m_blas);
  60. return m_lods[lod].m_blas;
  61. }
  62. /// Check if a vertex stream is present.
  63. Bool isVertexStreamPresent(const VertexStreamId stream) const
  64. {
  65. return !!(m_presentVertStreams & VertexStreamMask(1 << stream));
  66. }
  67. U32 getLodCount() const
  68. {
  69. return m_lods.getSize();
  70. }
  71. F32 getPositionsScale() const
  72. {
  73. return m_positionsScale;
  74. }
  75. Vec3 getPositionsTranslation() const
  76. {
  77. return m_positionsTranslation;
  78. }
  79. private:
  80. class LoadTask;
  81. class LoadContext;
  82. class Lod
  83. {
  84. public:
  85. UnifiedGeometryBufferAllocation m_indexBufferAllocationToken;
  86. Array<UnifiedGeometryBufferAllocation, U32(VertexStreamId::kMeshRelatedCount)> m_vertexBuffersAllocationToken;
  87. U32 m_indexCount = 0;
  88. U32 m_vertexCount = 0;
  89. AccelerationStructurePtr m_blas;
  90. };
  91. class SubMesh
  92. {
  93. public:
  94. Array<U32, kMaxLodCount> m_firstIndices;
  95. Array<U32, kMaxLodCount> m_indexCounts;
  96. Aabb m_aabb;
  97. };
  98. ResourceDynamicArray<SubMesh> m_subMeshes;
  99. ResourceDynamicArray<Lod> m_lods;
  100. Aabb m_aabb;
  101. IndexType m_indexType;
  102. VertexStreamMask m_presentVertStreams = VertexStreamMask::kNone;
  103. F32 m_positionsScale = 0.0f;
  104. Vec3 m_positionsTranslation = Vec3(0.0f);
  105. Error loadAsync(MeshBinaryLoader& loader) const;
  106. };
  107. /// @}
  108. } // end namespace anki