MeshResource.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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/Resource/ResourceObject.h>
  7. #include <AnKi/Math.h>
  8. #include <AnKi/Gr.h>
  9. #include <AnKi/Collision/Aabb.h>
  10. #include <AnKi/Shaders/Include/ModelTypes.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. /// Helper function for correct loading
  24. Bool isCompatible(const MeshResource& other) const;
  25. /// Load from a mesh file
  26. ANKI_USE_RESULT Error load(const ResourceFilename& filename, Bool async);
  27. /// Get the complete bounding box.
  28. const Aabb& getBoundingShape() const
  29. {
  30. return m_aabb;
  31. }
  32. /// Get submesh info.
  33. void getSubMeshInfo(U32 subMeshId, U32& firstIndex, U32& indexCount, Aabb& aabb) const
  34. {
  35. const SubMesh& sm = m_subMeshes[subMeshId];
  36. firstIndex = sm.m_firstIndex;
  37. indexCount = sm.m_indexCount;
  38. aabb = sm.m_aabb;
  39. }
  40. U32 getSubMeshCount() const
  41. {
  42. return m_subMeshes.getSize();
  43. }
  44. /// Get all info around vertex indices.
  45. void getIndexBufferInfo(BufferPtr& buff, PtrSize& buffOffset, U32& indexCount, IndexType& indexType) const
  46. {
  47. buff = m_indexBuffer;
  48. buffOffset = 0;
  49. indexCount = m_indexCount;
  50. indexType = m_indexType;
  51. }
  52. /// Get the number of logical vertex buffers.
  53. U32 getVertexBufferCount() const
  54. {
  55. return m_vertexBufferInfos.getSize();
  56. }
  57. /// Get vertex buffer info.
  58. void getVertexBufferInfo(const U32 buffIdx, BufferPtr& buff, PtrSize& offset, PtrSize& stride) const
  59. {
  60. buff = m_vertexBuffer;
  61. offset = m_vertexBufferInfos[buffIdx].m_offset;
  62. stride = m_vertexBufferInfos[buffIdx].m_stride;
  63. }
  64. /// Get attribute info. You need to check if the attribute is preset first (isVertexAttributePresent)
  65. void getVertexAttributeInfo(const VertexAttributeId attrib, U32& bufferIdx, Format& format,
  66. U32& relativeOffset) const
  67. {
  68. ANKI_ASSERT(isVertexAttributePresent(attrib));
  69. bufferIdx = m_attributes[attrib].m_buffIdx;
  70. format = m_attributes[attrib].m_format;
  71. relativeOffset = m_attributes[attrib].m_relativeOffset;
  72. }
  73. /// Check if a vertex attribute is present.
  74. Bool isVertexAttributePresent(const VertexAttributeId attrib) const
  75. {
  76. return !!m_attributes[attrib].m_format;
  77. }
  78. /// Return true if it has bone weights.
  79. Bool hasBoneWeights() const
  80. {
  81. return isVertexAttributePresent(VertexAttributeId::BONE_WEIGHTS);
  82. }
  83. AccelerationStructurePtr getBottomLevelAccelerationStructure() const
  84. {
  85. ANKI_ASSERT(m_blas.isCreated());
  86. return m_blas;
  87. }
  88. const MeshGpuDescriptor& getMeshGpuDescriptor() const
  89. {
  90. return m_meshGpuDescriptor;
  91. }
  92. /// Get the buffer that contains all the indices of all submesses.
  93. BufferPtr getIndexBuffer() const
  94. {
  95. return m_indexBuffer;
  96. }
  97. /// Get the buffer that contains all the vertices of all submesses.
  98. BufferPtr getVertexBuffer() const
  99. {
  100. return m_vertexBuffer;
  101. }
  102. private:
  103. class LoadTask;
  104. class LoadContext;
  105. class SubMesh
  106. {
  107. public:
  108. U32 m_firstIndex;
  109. U32 m_indexCount;
  110. Aabb m_aabb;
  111. };
  112. class VertBuffInfo
  113. {
  114. public:
  115. PtrSize m_offset; ///< Offset from the base of m_vertBuff.
  116. U32 m_stride;
  117. };
  118. class AttribInfo
  119. {
  120. public:
  121. Format m_format = Format::NONE;
  122. U32 m_relativeOffset = 0;
  123. U32 m_buffIdx = 0;
  124. };
  125. DynamicArray<SubMesh> m_subMeshes;
  126. DynamicArray<VertBuffInfo> m_vertexBufferInfos;
  127. Array<AttribInfo, U(VertexAttributeId::COUNT)> m_attributes;
  128. BufferPtr m_indexBuffer;
  129. BufferPtr m_vertexBuffer;
  130. U32 m_indexCount = 0;
  131. U32 m_vertexCount = 0;
  132. Aabb m_aabb;
  133. IndexType m_indexType;
  134. // RT
  135. AccelerationStructurePtr m_blas;
  136. MeshGpuDescriptor m_meshGpuDescriptor;
  137. ANKI_USE_RESULT Error loadAsync(MeshBinaryLoader& loader) const;
  138. };
  139. /// @}
  140. } // end namespace anki