MeshBinaryLoader.cpp 9.4 KB

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