MeshResource.h 3.2 KB

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