MeshResource.h 3.9 KB

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