MeshBinaryLoader.cpp 12 KB

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