MeshBinaryLoader.h 2.8 KB

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