MeshBinaryLoader.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  1. // Copyright (C) 2009-present, 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(ResourceFilesystem::getSingleton().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::kUv, false, false));
  144. ANKI_CHECK(checkFormat(VertexStreamId::kBoneIds, true, false));
  145. ANKI_CHECK(checkFormat(VertexStreamId::kBoneWeights, true, false));
  146. // Vertex buffers
  147. const Format boneIdxFormat = m_header.m_vertexAttributes[VertexStreamId::kBoneIds].m_format;
  148. const Format boneWeightsFormat = m_header.m_vertexAttributes[VertexStreamId::kBoneWeights].m_format;
  149. if((boneIdxFormat == Format::kNone && boneWeightsFormat != Format::kNone)
  150. || (boneWeightsFormat == Format::kNone && boneIdxFormat != Format::kNone))
  151. {
  152. ANKI_RESOURCE_LOGE("Bone buffers are partially present");
  153. return Error::kUserData;
  154. }
  155. // LOD
  156. if(h.m_lodCount == 0 || h.m_lodCount > kMaxLodCount)
  157. {
  158. ANKI_RESOURCE_LOGE("Wrong LOD count");
  159. return Error::kUserData;
  160. }
  161. // Indices format
  162. if(h.m_indexType != IndexType::kU16)
  163. {
  164. ANKI_RESOURCE_LOGE("Wrong format for indices");
  165. return Error::kUserData;
  166. }
  167. if(h.m_meshletPrimitiveFormat != kMeshletPrimitiveFormat)
  168. {
  169. ANKI_RESOURCE_LOGE("Wrong format for meshlet primitives");
  170. return Error::kUserData;
  171. }
  172. // m_totalIndexCount
  173. for(U32 lod = 0; lod < h.m_lodCount; ++lod)
  174. {
  175. const U indicesPerFace = 3;
  176. if(h.m_indexCounts[lod] == 0 || (h.m_indexCounts[lod] % indicesPerFace) != 0)
  177. {
  178. ANKI_RESOURCE_LOGE("Wrong index count");
  179. return Error::kUserData;
  180. }
  181. }
  182. // m_totalVertexCount
  183. for(U32 lod = 0; lod < h.m_lodCount; ++lod)
  184. {
  185. if(h.m_vertexCounts[lod] == 0)
  186. {
  187. ANKI_RESOURCE_LOGE("Wrong vertex count");
  188. return Error::kUserData;
  189. }
  190. }
  191. for(U32 lod = 0; lod < h.m_lodCount; ++lod)
  192. {
  193. if(h.m_meshletPrimitiveCounts[lod] == 0 || h.m_meshletCounts[lod] == 0)
  194. {
  195. ANKI_RESOURCE_LOGE("Wrong meshlet counts");
  196. return Error::kUserData;
  197. }
  198. }
  199. // m_subMeshCount
  200. if(h.m_subMeshCount == 0)
  201. {
  202. ANKI_RESOURCE_LOGE("Wrong submesh count");
  203. return Error::kUserData;
  204. }
  205. if(h.m_maxPrimitivesPerMeshlet != kMaxPrimitivesPerMeshlet || h.m_maxVerticesPerMeshlet != kMaxVerticesPerMeshlet)
  206. {
  207. ANKI_RESOURCE_LOGE("Wrong max primitives or max vertices per meshlet");
  208. return Error::kUserData;
  209. }
  210. // AABB
  211. ANKI_CHECK(checkBoundingVolume(h.m_boundingVolume));
  212. // Check the file size
  213. PtrSize totalSize = sizeof(m_header);
  214. totalSize += sizeof(MeshBinarySubMesh) * m_header.m_subMeshCount;
  215. for(U32 lod = 0; lod < h.m_lodCount; ++lod)
  216. {
  217. totalSize += getLodBuffersSize(lod);
  218. }
  219. if(totalSize != m_file->getSize())
  220. {
  221. ANKI_RESOURCE_LOGE("Unexpected file size");
  222. return Error::kUserData;
  223. }
  224. return Error::kNone;
  225. }
  226. Error MeshBinaryLoader::storeIndexBuffer(U32 lod, void* ptr, PtrSize size)
  227. {
  228. ANKI_ASSERT(ptr);
  229. ANKI_ASSERT(isLoaded());
  230. ANKI_ASSERT(lod < m_header.m_lodCount);
  231. ANKI_ASSERT(size == getIndexBufferSize(lod));
  232. PtrSize seek = sizeof(m_header) + m_subMeshes.getSizeInBytes();
  233. for(U32 l = lod + 1; l < m_header.m_lodCount; ++l)
  234. {
  235. seek += getLodBuffersSize(l);
  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::storeVertexBuffer(U32 lod, U32 bufferIdx, void* ptr, PtrSize size)
  242. {
  243. ANKI_ASSERT(ptr);
  244. ANKI_ASSERT(isLoaded());
  245. ANKI_ASSERT(size == getVertexBufferSize(lod, bufferIdx));
  246. ANKI_ASSERT(lod < m_header.m_lodCount);
  247. PtrSize seek = sizeof(m_header) + m_subMeshes.getSizeInBytes();
  248. for(U32 l = lod + 1; l < m_header.m_lodCount; ++l)
  249. {
  250. seek += getLodBuffersSize(l);
  251. }
  252. seek += getIndexBufferSize(lod);
  253. for(U32 i = 0; i < bufferIdx; ++i)
  254. {
  255. seek += getVertexBufferSize(lod, i);
  256. }
  257. ANKI_CHECK(m_file->seek(seek, FileSeekOrigin::kBeginning));
  258. ANKI_CHECK(m_file->read(ptr, size));
  259. return Error::kNone;
  260. }
  261. Error MeshBinaryLoader::storeMeshletIndicesBuffer(U32 lod, void* ptr, PtrSize size)
  262. {
  263. ANKI_ASSERT(ptr);
  264. ANKI_ASSERT(isLoaded());
  265. ANKI_ASSERT(size == getMeshletPrimitivesBufferSize(lod));
  266. ANKI_ASSERT(lod < m_header.m_lodCount);
  267. PtrSize seek = sizeof(m_header) + m_subMeshes.getSizeInBytes();
  268. for(U32 l = lod + 1; l < m_header.m_lodCount; ++l)
  269. {
  270. seek += getLodBuffersSize(l);
  271. }
  272. seek += getIndexBufferSize(lod);
  273. for(U32 i = 0; i < m_header.m_vertexBuffers.getSize(); ++i)
  274. {
  275. seek += getVertexBufferSize(lod, i);
  276. }
  277. seek += getMeshletsBufferSize(lod);
  278. ANKI_CHECK(m_file->seek(seek, FileSeekOrigin::kBeginning));
  279. ANKI_CHECK(m_file->read(ptr, size));
  280. return Error::kNone;
  281. }
  282. Error MeshBinaryLoader::storeMeshletBuffer(U32 lod, WeakArray<MeshBinaryMeshlet> out)
  283. {
  284. ANKI_ASSERT(isLoaded());
  285. ANKI_ASSERT(out.getSizeInBytes() == getMeshletsBufferSize(lod));
  286. ANKI_ASSERT(lod < m_header.m_lodCount);
  287. PtrSize seek = sizeof(m_header) + m_subMeshes.getSizeInBytes();
  288. for(U32 l = lod + 1; l < m_header.m_lodCount; ++l)
  289. {
  290. seek += getLodBuffersSize(l);
  291. }
  292. seek += getIndexBufferSize(lod);
  293. for(U32 i = 0; i < m_header.m_vertexBuffers.getSize(); ++i)
  294. {
  295. seek += getVertexBufferSize(lod, i);
  296. }
  297. ANKI_CHECK(m_file->seek(seek, FileSeekOrigin::kBeginning));
  298. ANKI_CHECK(m_file->read(&out[0], out.getSizeInBytes()));
  299. return Error::kNone;
  300. }
  301. Error MeshBinaryLoader::storeIndicesAndPosition(U32 lod, ResourceDynamicArray<U32>& indices, ResourceDynamicArray<Vec3>& positions)
  302. {
  303. ANKI_ASSERT(isLoaded());
  304. ANKI_ASSERT(lod < m_header.m_lodCount);
  305. // Store indices
  306. {
  307. indices.resize(m_header.m_indexCounts[lod]);
  308. // Store to staging buff
  309. DynamicArray<U8, MemoryPoolPtrWrapper<BaseMemoryPool>, PtrSize> staging(m_subMeshes.getMemoryPool());
  310. staging.resize(getIndexBufferSize(lod));
  311. ANKI_CHECK(storeIndexBuffer(lod, &staging[0], staging.getSizeInBytes()));
  312. // Copy from staging
  313. ANKI_ASSERT(m_header.m_indexType == IndexType::kU16);
  314. for(U32 i = 0; i < m_header.m_indexCounts[lod]; ++i)
  315. {
  316. indices[i] = *reinterpret_cast<U16*>(&staging[PtrSize(i) * 2]);
  317. }
  318. }
  319. // Store positions
  320. {
  321. const MeshBinaryVertexAttribute& attrib = m_header.m_vertexAttributes[VertexStreamId::kPosition];
  322. DynamicArray<U16Vec4, MemoryPoolPtrWrapper<BaseMemoryPool>> tempPositions(m_subMeshes.getMemoryPool());
  323. tempPositions.resize(m_header.m_vertexCounts[lod]);
  324. static_assert(kMeshRelatedVertexStreamFormats[VertexStreamId::kPosition] == Format::kR16G16B16A16_Unorm, "Incorrect format");
  325. ANKI_CHECK(storeVertexBuffer(lod, attrib.m_bufferIndex, &tempPositions[0], tempPositions.getSizeInBytes()));
  326. positions.resize(m_header.m_vertexCounts[lod]);
  327. for(U32 i = 0; i < tempPositions.getSize(); ++i)
  328. {
  329. positions[i] = Vec3(U16Vec3(tempPositions[i].xyz)) / F32(kMaxU16);
  330. positions[i] *= Vec3(&attrib.m_scale[0]);
  331. positions[i] += Vec3(&attrib.m_translation[0]);
  332. }
  333. }
  334. return Error::kNone;
  335. }
  336. PtrSize MeshBinaryLoader::getLodBuffersSize(U32 lod) const
  337. {
  338. ANKI_ASSERT(lod < m_header.m_lodCount);
  339. PtrSize size = getIndexBufferSize(lod);
  340. size += getMeshletsBufferSize(lod);
  341. size += getMeshletPrimitivesBufferSize(lod);
  342. for(U32 vertBufferIdx = 0; vertBufferIdx < m_header.m_vertexBuffers.getSize(); ++vertBufferIdx)
  343. {
  344. if(m_header.m_vertexBuffers[vertBufferIdx].m_vertexStride > 0)
  345. {
  346. size += getVertexBufferSize(lod, vertBufferIdx);
  347. }
  348. }
  349. return size;
  350. }
  351. } // end namespace anki