MeshBinaryLoader.cpp 9.3 KB

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