MeshBinaryLoader.cpp 9.4 KB

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