MeshBinaryLoader.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. // Copyright (C) 2009-present, 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/MeshBinary.h>
  7. #include <AnKi/Resource/ResourceFilesystem.h>
  8. #include <AnKi/Resource/MeshBinary.h>
  9. #include <AnKi/Util/WeakArray.h>
  10. #include <AnKi/Shaders/Include/MeshTypes.h>
  11. namespace anki {
  12. /// @addtogroup resource
  13. /// @{
  14. /// This class loads the mesh binary file. It only supports a subset of combinations of vertex formats and buffers.
  15. /// The file is layed out in memory:
  16. /// * Header
  17. /// * Submeshes
  18. /// * LOD of max LOD
  19. /// ** Index buffer of all sub meshes
  20. /// ** Vertex buffer #0 of all sub meshes
  21. /// ** ...
  22. /// ** Meshlets of all sub meshes
  23. /// ** Local index buffer all sub meshes
  24. /// * LOD of max-1 LOD
  25. /// ...
  26. class MeshBinaryLoader
  27. {
  28. public:
  29. MeshBinaryLoader(BaseMemoryPool* pool)
  30. : m_subMeshes(pool)
  31. {
  32. ANKI_ASSERT(pool);
  33. }
  34. ~MeshBinaryLoader() = default;
  35. Error load(const ResourceFilename& filename);
  36. Error storeIndexBuffer(U32 lod, void* ptr, PtrSize size);
  37. Error storeVertexBuffer(U32 lod, U32 bufferIdx, void* ptr, PtrSize size);
  38. Error storeMeshletIndicesBuffer(U32 lod, void* ptr, PtrSize size);
  39. Error storeMeshletBuffer(U32 lod, WeakArray<MeshBinaryMeshlet> out);
  40. /// Instead of calling storeIndexBuffer and storeVertexBuffer use this method to get those buffers into the CPU.
  41. Error storeIndicesAndPosition(U32 lod, ResourceDynamicArray<U32>& indices, ResourceDynamicArray<Vec3>& positions);
  42. const MeshBinaryHeader& getHeader() const
  43. {
  44. ANKI_ASSERT(isLoaded());
  45. return m_header;
  46. }
  47. ConstWeakArray<MeshBinarySubMesh> getSubMeshes() const
  48. {
  49. return ConstWeakArray<MeshBinarySubMesh>(m_subMeshes);
  50. }
  51. private:
  52. ResourceFilePtr m_file;
  53. MeshBinaryHeader m_header;
  54. DynamicArray<MeshBinarySubMesh, MemoryPoolPtrWrapper<BaseMemoryPool>> m_subMeshes;
  55. Bool isLoaded() const
  56. {
  57. return m_file.get() != nullptr;
  58. }
  59. PtrSize getIndexBufferSize(U32 lod) const
  60. {
  61. ANKI_ASSERT(isLoaded());
  62. ANKI_ASSERT(lod < m_header.m_lodCount);
  63. return PtrSize(m_header.m_indexCounts[lod]) * getIndexSize(m_header.m_indexType);
  64. }
  65. PtrSize getMeshletsBufferSize(U32 lod) const
  66. {
  67. ANKI_ASSERT(isLoaded());
  68. ANKI_ASSERT(lod < m_header.m_lodCount);
  69. return PtrSize(m_header.m_meshletCounts[lod]) * sizeof(MeshBinaryMeshlet);
  70. }
  71. PtrSize getVertexBufferSize(U32 lod, U32 bufferIdx) const
  72. {
  73. ANKI_ASSERT(isLoaded());
  74. ANKI_ASSERT(lod < m_header.m_lodCount);
  75. return PtrSize(m_header.m_vertexCounts[lod]) * PtrSize(m_header.m_vertexBuffers[bufferIdx].m_vertexStride);
  76. }
  77. PtrSize getMeshletPrimitivesBufferSize(U32 lod) const
  78. {
  79. ANKI_ASSERT(isLoaded());
  80. ANKI_ASSERT(lod < m_header.m_lodCount);
  81. return PtrSize(m_header.m_meshletPrimitiveCounts[lod]) * getFormatInfo(kMeshletPrimitiveFormat).m_texelSize;
  82. }
  83. PtrSize getLodBuffersSize(U32 lod) const;
  84. Error checkHeader() const;
  85. Error checkFormat(VertexStreamId stream, Bool isOptional, Bool canBeTransformed) const;
  86. Error loadSubmeshes();
  87. };
  88. /// @}
  89. } // end namespace anki