MeshBinaryLoader.cpp 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. // Copyright (C) 2009-2022, Panagiotis Christopoulos Charitos and contributors.
  2. // All rights reserved.
  3. // Code licensed under the BSD License.
  4. // http://www.anki3d.org/LICENSE
  5. #include <AnKi/Resource/MeshBinaryLoader.h>
  6. #include <AnKi/Resource/ResourceManager.h>
  7. namespace anki {
  8. MeshBinaryLoader::MeshBinaryLoader(ResourceManager* manager)
  9. : MeshBinaryLoader(manager, &manager->getTempMemoryPool())
  10. {
  11. }
  12. MeshBinaryLoader::~MeshBinaryLoader()
  13. {
  14. m_subMeshes.destroy(*m_pool);
  15. }
  16. Error MeshBinaryLoader::load(const ResourceFilename& filename)
  17. {
  18. // Load header + submeshes
  19. ANKI_CHECK(m_manager->getFilesystem().openFile(filename, m_file));
  20. ANKI_CHECK(m_file->read(&m_header, sizeof(m_header)));
  21. ANKI_CHECK(checkHeader());
  22. ANKI_CHECK(loadSubmeshes());
  23. return Error::kNone;
  24. }
  25. Error MeshBinaryLoader::loadSubmeshes()
  26. {
  27. m_subMeshes.create(*m_pool, m_header.m_subMeshCount);
  28. ANKI_CHECK(m_file->read(&m_subMeshes[0], m_subMeshes.getSizeInBytes()));
  29. // Checks
  30. const U32 indicesPerFace = !!(m_header.m_flags & MeshBinaryFlag::kQuad) ? 4 : 3;
  31. for(U32 lod = 0; lod < m_header.m_lodCount; ++lod)
  32. {
  33. U idxSum = 0;
  34. for(U32 i = 0; i < m_subMeshes.getSize(); i++)
  35. {
  36. const MeshBinarySubMesh& sm = m_subMeshes[i];
  37. if(sm.m_firstIndices[lod] != idxSum || (sm.m_indexCounts[lod] % indicesPerFace) != 0)
  38. {
  39. ANKI_RESOURCE_LOGE("Incorrect sub mesh info");
  40. return Error::kUserData;
  41. }
  42. for(U d = 0; d < 3; ++d)
  43. {
  44. if(sm.m_aabbMin[d] >= sm.m_aabbMax[d])
  45. {
  46. ANKI_RESOURCE_LOGE("Wrong submesh bounding box");
  47. return Error::kUserData;
  48. }
  49. }
  50. idxSum += sm.m_indexCounts[lod];
  51. }
  52. if(idxSum != m_header.m_totalIndexCounts[lod])
  53. {
  54. ANKI_RESOURCE_LOGE("Submesh index count doesn't add up to the total");
  55. return Error::kUserData;
  56. }
  57. }
  58. return Error::kNone;
  59. }
  60. Error MeshBinaryLoader::checkFormat(VertexStreamId stream, Bool isOptional, Bool canBeTransformed) const
  61. {
  62. const U32 vertexAttribIdx = U32(stream);
  63. const U32 vertexBufferIdx = U32(stream);
  64. const MeshBinaryVertexAttribute& attrib = m_header.m_vertexAttributes[vertexAttribIdx];
  65. // Check format
  66. if(isOptional && attrib.m_format == Format::kNone)
  67. {
  68. // Attrib is not in use, no more checks
  69. return Error::kNone;
  70. }
  71. if(attrib.m_format != kMeshRelatedVertexStreamFormats[stream])
  72. {
  73. ANKI_RESOURCE_LOGE("Vertex attribute %u has unsupported format %s", vertexAttribIdx,
  74. getFormatInfo(attrib.m_format).m_name);
  75. return Error::kUserData;
  76. }
  77. if(attrib.m_bufferIndex != vertexBufferIdx)
  78. {
  79. ANKI_RESOURCE_LOGE("Vertex attribute %u should belong to the %u vertex buffer", vertexAttribIdx,
  80. vertexBufferIdx);
  81. return Error::kUserData;
  82. }
  83. if(attrib.m_relativeOffset != 0)
  84. {
  85. ANKI_RESOURCE_LOGE("Vertex attribute %u should have relative vertex offset equal to 0", vertexAttribIdx);
  86. return Error::kUserData;
  87. }
  88. if(!canBeTransformed && Vec4(attrib.m_scale) != Vec4(1.0f))
  89. {
  90. ANKI_RESOURCE_LOGE("Vertex attribute %u should have 1.0 scale", vertexAttribIdx);
  91. return Error::kUserData;
  92. }
  93. if(canBeTransformed && Vec4(attrib.m_scale) <= kEpsilonf)
  94. {
  95. ANKI_RESOURCE_LOGE("Vertex attribute %u should have positive scale", vertexAttribIdx);
  96. return Error::kUserData;
  97. }
  98. if(canBeTransformed
  99. && (attrib.m_scale[0] != attrib.m_scale[1] || attrib.m_scale[0] != attrib.m_scale[2]
  100. || attrib.m_scale[0] != attrib.m_scale[3]))
  101. {
  102. ANKI_RESOURCE_LOGE("Vertex attribute %u should have uniform scale", vertexAttribIdx);
  103. return Error::kUserData;
  104. }
  105. if(!canBeTransformed && Vec4(attrib.m_translation) != Vec4(0.0f))
  106. {
  107. ANKI_RESOURCE_LOGE("Vertex attribute %u should have 0.0 translation", vertexAttribIdx);
  108. return Error::kUserData;
  109. }
  110. const U32 vertexBufferStride = getFormatInfo(attrib.m_format).m_texelSize;
  111. if(m_header.m_vertexBuffers[vertexBufferIdx].m_vertexStride != vertexBufferStride)
  112. {
  113. ANKI_RESOURCE_LOGE("Vertex buffer %u doesn't have the expected stride of %u", vertexBufferIdx,
  114. vertexBufferStride);
  115. return Error::kUserData;
  116. }
  117. return Error::kNone;
  118. }
  119. Error MeshBinaryLoader::checkHeader() const
  120. {
  121. const MeshBinaryHeader& h = m_header;
  122. // Header
  123. if(memcmp(&h.m_magic[0], kMeshMagic, 8) != 0)
  124. {
  125. ANKI_RESOURCE_LOGE("Wrong magic word");
  126. return Error::kUserData;
  127. }
  128. // Flags
  129. if((h.m_flags & ~MeshBinaryFlag::kAll) != MeshBinaryFlag::kNone)
  130. {
  131. ANKI_RESOURCE_LOGE("Wrong header flags");
  132. return Error::kUserData;
  133. }
  134. // Attributes
  135. ANKI_CHECK(checkFormat(VertexStreamId::kPosition, false, true));
  136. ANKI_CHECK(checkFormat(VertexStreamId::kNormal, false, false));
  137. ANKI_CHECK(checkFormat(VertexStreamId::kTangent, false, false));
  138. ANKI_CHECK(checkFormat(VertexStreamId::kUv, false, false));
  139. ANKI_CHECK(checkFormat(VertexStreamId::kBoneIds, true, false));
  140. ANKI_CHECK(checkFormat(VertexStreamId::kBoneWeights, true, false));
  141. // Vertex buffers
  142. const Format boneIdxFormat = m_header.m_vertexAttributes[VertexStreamId::kBoneIds].m_format;
  143. const Format boneWeightsFormat = m_header.m_vertexAttributes[VertexStreamId::kBoneWeights].m_format;
  144. if((boneIdxFormat == Format::kNone && boneWeightsFormat != Format::kNone)
  145. || (boneWeightsFormat == Format::kNone && boneIdxFormat != Format::kNone))
  146. {
  147. ANKI_RESOURCE_LOGE("Bone buffers are partially present");
  148. return Error::kUserData;
  149. }
  150. // LOD
  151. if(h.m_lodCount == 0 || h.m_lodCount >= kMaxLodCount)
  152. {
  153. ANKI_RESOURCE_LOGE("Wrong LOD count");
  154. return Error::kUserData;
  155. }
  156. // Indices format
  157. if(h.m_indexType != IndexType::kU16)
  158. {
  159. ANKI_RESOURCE_LOGE("Wrong format for indices");
  160. return Error::kUserData;
  161. }
  162. // m_totalIndexCount
  163. for(U32 lod = 0; lod < h.m_lodCount; ++lod)
  164. {
  165. const U indicesPerFace = !!(h.m_flags & MeshBinaryFlag::kQuad) ? 4 : 3;
  166. if(h.m_totalIndexCounts[lod] == 0 || (h.m_totalIndexCounts[lod] % indicesPerFace) != 0)
  167. {
  168. ANKI_RESOURCE_LOGE("Wrong index count");
  169. return Error::kUserData;
  170. }
  171. }
  172. // m_totalVertexCount
  173. for(U32 lod = 0; lod < h.m_lodCount; ++lod)
  174. {
  175. if(h.m_totalVertexCounts[lod] == 0)
  176. {
  177. ANKI_RESOURCE_LOGE("Wrong vertex count");
  178. return Error::kUserData;
  179. }
  180. }
  181. // m_subMeshCount
  182. if(h.m_subMeshCount == 0)
  183. {
  184. ANKI_RESOURCE_LOGE("Wrong submesh count");
  185. return Error::kUserData;
  186. }
  187. // AABB
  188. for(U d = 0; d < 3; ++d)
  189. {
  190. if(h.m_aabbMin[d] >= h.m_aabbMax[d])
  191. {
  192. ANKI_RESOURCE_LOGE("Wrong bounding box");
  193. return Error::kUserData;
  194. }
  195. }
  196. // Check the file size
  197. PtrSize totalSize = sizeof(m_header);
  198. totalSize += sizeof(MeshBinarySubMesh) * m_header.m_subMeshCount;
  199. for(U32 lod = 0; lod < h.m_lodCount; ++lod)
  200. {
  201. totalSize += getLodBuffersSize(lod);
  202. }
  203. if(totalSize != m_file->getSize())
  204. {
  205. ANKI_RESOURCE_LOGE("Unexpected file size");
  206. return Error::kUserData;
  207. }
  208. return Error::kNone;
  209. }
  210. Error MeshBinaryLoader::storeIndexBuffer(U32 lod, void* ptr, PtrSize size)
  211. {
  212. ANKI_ASSERT(ptr);
  213. ANKI_ASSERT(isLoaded());
  214. ANKI_ASSERT(lod < m_header.m_lodCount);
  215. ANKI_ASSERT(size == getIndexBufferSize(lod));
  216. PtrSize seek = sizeof(m_header) + m_subMeshes.getSizeInBytes();
  217. for(U32 l = lod + 1; l < m_header.m_lodCount; ++l)
  218. {
  219. seek += getLodBuffersSize(l);
  220. }
  221. ANKI_CHECK(m_file->seek(seek, FileSeekOrigin::kBeginning));
  222. ANKI_CHECK(m_file->read(ptr, size));
  223. return Error::kNone;
  224. }
  225. Error MeshBinaryLoader::storeVertexBuffer(U32 lod, U32 bufferIdx, void* ptr, PtrSize size)
  226. {
  227. ANKI_ASSERT(ptr);
  228. ANKI_ASSERT(isLoaded());
  229. ANKI_ASSERT(size == getVertexBufferSize(lod, bufferIdx));
  230. ANKI_ASSERT(lod < m_header.m_lodCount);
  231. PtrSize seek = sizeof(m_header) + m_subMeshes.getSizeInBytes();
  232. for(U32 l = lod + 1; l < m_header.m_lodCount; ++l)
  233. {
  234. seek += getLodBuffersSize(l);
  235. }
  236. seek += getIndexBufferSize(lod);
  237. for(U32 i = 0; i < bufferIdx; ++i)
  238. {
  239. seek += getVertexBufferSize(lod, i);
  240. }
  241. ANKI_CHECK(m_file->seek(seek, FileSeekOrigin::kBeginning));
  242. ANKI_CHECK(m_file->read(ptr, size));
  243. return Error::kNone;
  244. }
  245. Error MeshBinaryLoader::storeIndicesAndPosition(U32 lod, DynamicArrayRaii<U32>& indices,
  246. DynamicArrayRaii<Vec3>& positions)
  247. {
  248. ANKI_ASSERT(isLoaded());
  249. ANKI_ASSERT(lod < m_header.m_lodCount);
  250. // Store indices
  251. {
  252. indices.resize(m_header.m_totalIndexCounts[lod]);
  253. // Store to staging buff
  254. DynamicArrayRaii<U8, PtrSize> staging(m_pool);
  255. staging.create(getIndexBufferSize(lod));
  256. ANKI_CHECK(storeIndexBuffer(lod, &staging[0], staging.getSizeInBytes()));
  257. // Copy from staging
  258. ANKI_ASSERT(m_header.m_indexType == IndexType::kU16);
  259. for(U32 i = 0; i < m_header.m_totalIndexCounts[lod]; ++i)
  260. {
  261. indices[i] = *reinterpret_cast<U16*>(&staging[PtrSize(i) * 2]);
  262. }
  263. }
  264. // Store positions
  265. {
  266. positions.resize(m_header.m_totalVertexCounts[lod]);
  267. const MeshBinaryVertexAttribute& attrib = m_header.m_vertexAttributes[VertexStreamId::kPosition];
  268. ANKI_CHECK(storeVertexBuffer(lod, attrib.m_bufferIndex, &positions[0], positions.getSizeInBytes()));
  269. }
  270. return Error::kNone;
  271. }
  272. PtrSize MeshBinaryLoader::getLodBuffersSize(U32 lod) const
  273. {
  274. ANKI_ASSERT(lod < m_header.m_lodCount);
  275. PtrSize size = getIndexBufferSize(lod);
  276. for(U32 vertBufferIdx = 0; vertBufferIdx < m_header.m_vertexBuffers.getSize(); ++vertBufferIdx)
  277. {
  278. if(m_header.m_vertexBuffers[vertBufferIdx].m_vertexStride > 0)
  279. {
  280. size += getVertexBufferSize(lod, vertBufferIdx);
  281. }
  282. }
  283. return size;
  284. }
  285. } // end namespace anki