MeshBinaryLoader.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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/MeshBinary.h>
  7. #include <AnKi/Resource/ResourceFilesystem.h>
  8. #include <AnKi/Resource/MeshBinary.h>
  9. #include <AnKi/Util/WeakArray.h>
  10. namespace anki
  11. {
  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. class MeshBinaryLoader
  16. {
  17. public:
  18. MeshBinaryLoader(ResourceManager* manager);
  19. MeshBinaryLoader(ResourceManager* manager, GenericMemoryPoolAllocator<U8> alloc)
  20. : m_manager(manager)
  21. , m_alloc(alloc)
  22. {
  23. }
  24. ~MeshBinaryLoader();
  25. ANKI_USE_RESULT Error load(const ResourceFilename& filename);
  26. ANKI_USE_RESULT Error storeIndexBuffer(void* ptr, PtrSize size);
  27. ANKI_USE_RESULT Error storeVertexBuffer(U32 bufferIdx, void* ptr, PtrSize size);
  28. /// Instead of calling storeIndexBuffer and storeVertexBuffer use this method to get those buffers into the CPU.
  29. ANKI_USE_RESULT Error storeIndicesAndPosition(DynamicArrayAuto<U32>& indices, DynamicArrayAuto<Vec3>& positions);
  30. const MeshBinaryHeader& getHeader() const
  31. {
  32. ANKI_ASSERT(isLoaded());
  33. return m_header;
  34. }
  35. Bool hasBoneInfo() const
  36. {
  37. ANKI_ASSERT(isLoaded());
  38. return m_header.m_vertexAttributes[VertexAttributeId::BONE_INDICES].m_format != Format::NONE;
  39. }
  40. ConstWeakArray<MeshBinarySubMesh> getSubMeshes() const
  41. {
  42. return ConstWeakArray<MeshBinarySubMesh>(m_subMeshes);
  43. }
  44. private:
  45. ResourceManager* m_manager;
  46. GenericMemoryPoolAllocator<U8> m_alloc;
  47. ResourceFilePtr m_file;
  48. MeshBinaryHeader m_header;
  49. DynamicArray<MeshBinarySubMesh> m_subMeshes;
  50. Bool isLoaded() const
  51. {
  52. return m_file.get() != nullptr;
  53. }
  54. PtrSize getIndexBufferSize() const
  55. {
  56. ANKI_ASSERT(isLoaded());
  57. return PtrSize(m_header.m_totalIndexCount) * ((m_header.m_indexType == IndexType::U16) ? 2 : 4);
  58. }
  59. PtrSize getAlignedIndexBufferSize() const
  60. {
  61. ANKI_ASSERT(isLoaded());
  62. return getAlignedRoundUp(MESH_BINARY_BUFFER_ALIGNMENT, getIndexBufferSize());
  63. }
  64. PtrSize getVertexBufferSize(U32 bufferIdx) const
  65. {
  66. ANKI_ASSERT(isLoaded());
  67. ANKI_ASSERT(bufferIdx < m_header.m_vertexBufferCount);
  68. return PtrSize(m_header.m_totalVertexCount) * PtrSize(m_header.m_vertexBuffers[bufferIdx].m_vertexStride);
  69. }
  70. PtrSize getAlignedVertexBufferSize(U32 bufferIdx) const
  71. {
  72. ANKI_ASSERT(isLoaded());
  73. ANKI_ASSERT(bufferIdx < m_header.m_vertexBufferCount);
  74. return getAlignedRoundUp(MESH_BINARY_BUFFER_ALIGNMENT, getVertexBufferSize(bufferIdx));
  75. }
  76. ANKI_USE_RESULT Error checkHeader() const;
  77. ANKI_USE_RESULT Error checkFormat(VertexAttributeId type, ConstWeakArray<Format> supportedFormats,
  78. U32 vertexBufferIdx, U32 relativeOffset) const;
  79. };
  80. /// @}
  81. } // end namespace anki