git.s-ol.nu ~forks/DiligentTools / d1120ac
GLTFLoader: added option to not load animation data and skin assiduous 11 months ago
3 changed file(s) with 72 addition(s) and 56 deletion(s). Raw diff Collapse all Expand all
283283
284284 struct Model
285285 {
286 struct VertexAttribs0
286 struct VertexBasicAttribs
287287 {
288288 float3 pos;
289289 float3 normal;
291291 float2 uv1;
292292 };
293293
294 struct VertexAttribs1
294 struct VertexSkinAttribs
295295 {
296296 float4 joint0;
297297 float4 weight0;
299299
300300 enum BUFFER_ID
301301 {
302 BUFFER_ID_VERTEX0 = 0,
303 BUFFER_ID_VERTEX1,
302 BUFFER_ID_VERTEX_BASIC_ATTRIBS = 0,
303 BUFFER_ID_VERTEX_SKIN_ATTRIBS,
304304 BUFFER_ID_INDEX,
305305 BUFFER_ID_NUM_BUFFERS
306306 };
347347
348348 /// Optional resource cache usage info.
349349 ResourceCacheUseInfo* pCacheInfo = nullptr;
350
351 /// Whether to load animation and initialize skin attributes
352 /// buffer.
353 bool LoadAnimationAndSkin = true;
350354 };
351355 Model(IRenderDevice* pDevice,
352356 IDeviceContext* pContext,
382386
383387 Uint32 GetBaseVertex() const
384388 {
385 auto& VertBuff = Buffers[BUFFER_ID_VERTEX0];
386 VERIFY(!VertBuff.pSuballocation || VertBuff.pSuballocation->GetOffset() % sizeof(VertexAttribs0) == 0,
389 auto& VertBuff = Buffers[BUFFER_ID_VERTEX_BASIC_ATTRIBS];
390 VERIFY(!VertBuff.pSuballocation || VertBuff.pSuballocation->GetOffset() % sizeof(VertexBasicAttribs) == 0,
387391 "Allocation offset is not multiple of sizeof(VertexAttribs0)");
388392 return VertBuff.pSuballocation ?
389 static_cast<Uint32>(VertBuff.pSuballocation->GetOffset() / sizeof(VertexAttribs0)) :
393 static_cast<Uint32>(VertBuff.pSuballocation->GetOffset() / sizeof(VertexBasicAttribs)) :
390394 0;
391395 }
392396
399403 Node* parent,
400404 const tinygltf::Node& gltf_node,
401405 uint32_t nodeIndex,
402 const tinygltf::Model& gltf_model);
406 const tinygltf::Model& gltf_model,
407 bool LoadSkin);
403408
404409 void LoadSkins(const tinygltf::Model& gltf_model);
405410
8181
8282 RefCntAutoPtr<ITextureAtlasSuballocation> FindAllocation(const char* CacheId);
8383
84 Uint32 GetResourceVersion()
84 Uint32 GetTextureVersion()
8585 {
8686 Uint32 Version = 0;
8787
9090 Version += atlas_it.second->GetVersion();
9191
9292 return Version;
93 }
94
95 Uint32 GetBufferVersion(Uint32 Index) const
96 {
97 return m_BufferSuballocators[Index]->GetVersion();
9398 }
9499
95100 IBuffer* GetBuffer(Uint32 Index, IRenderDevice* pDevice, IDeviceContext* pContext)
130135 using TexAllocationsHashMapType = std::unordered_map<std::string, RefCntWeakPtr<ITextureAtlasSuballocation>>;
131136 std::mutex m_TexAllocationsMtx;
132137 TexAllocationsHashMapType m_TexAllocations;
133
134 std::atomic_uint32_t m_ResourceVersion = {};
135138 };
136139
137140 } // namespace GLTF
7676 {
7777 std::vector<TextureInitData> Textures;
7878
79 std::vector<Uint32> IndexData;
80 std::vector<VertexAttribs0> VertexData0;
81 std::vector<VertexAttribs1> VertexData1;
79 std::vector<Uint32> IndexData;
80 std::vector<VertexBasicAttribs> VertexBasicData;
81 std::vector<VertexSkinAttribs> VertexSkinData;
8282 };
8383
8484
264264 Node* parent,
265265 const tinygltf::Node& gltf_node,
266266 uint32_t nodeIndex,
267 const tinygltf::Model& gltf_model)
267 const tinygltf::Model& gltf_model,
268 bool LoadSkin)
268269 {
269270 std::unique_ptr<Node> NewNode(new Node{});
270271 NewNode->Index = nodeIndex;
304305 {
305306 for (size_t i = 0; i < gltf_node.children.size(); i++)
306307 {
307 LoadNode(pDevice, NewNode.get(), gltf_model.nodes[gltf_node.children[i]], gltf_node.children[i], gltf_model);
308 LoadNode(pDevice, NewNode.get(), gltf_model.nodes[gltf_node.children[i]], gltf_node.children[i], gltf_model, LoadSkin);
308309 }
309310 }
310311
318319 const tinygltf::Primitive& primitive = gltf_mesh.primitives[j];
319320
320321 uint32_t indexStart = static_cast<uint32_t>(InitData->IndexData.size());
321 uint32_t vertexStart = static_cast<uint32_t>(InitData->VertexData0.size());
322 VERIFY_EXPR(InitData->VertexData1.empty() || InitData->VertexData0.size() == InitData->VertexData1.size());
322 uint32_t vertexStart = static_cast<uint32_t>(InitData->VertexBasicData.size());
323 VERIFY_EXPR(InitData->VertexSkinData.empty() || InitData->VertexBasicData.size() == InitData->VertexSkinData.size());
323324
324325 uint32_t indexCount = 0;
325326 uint32_t vertexCount = 0;
453454
454455 for (uint32_t v = 0; v < vertexCount; v++)
455456 {
456 VertexAttribs0 vert0{};
457 vert0.pos = float4(float3::MakeVector(bufferPos + v * posStride), 1.0f);
457 VertexBasicAttribs BasicAttribs{};
458 BasicAttribs.pos = float4(float3::MakeVector(bufferPos + v * posStride), 1.0f);
458459 // clang-format off
459 vert0.normal = bufferNormals != nullptr ? normalize(float3::MakeVector(bufferNormals + v * normalsStride)) : float3{};
460 vert0.uv0 = bufferTexCoordSet0 != nullptr ? float2::MakeVector(bufferTexCoordSet0 + v * texCoordSet0Stride) : float2{};
461 vert0.uv1 = bufferTexCoordSet1 != nullptr ? float2::MakeVector(bufferTexCoordSet1 + v * texCoordSet1Stride) : float2{};
460 BasicAttribs.normal = bufferNormals != nullptr ? normalize(float3::MakeVector(bufferNormals + v * normalsStride)) : float3{};
461 BasicAttribs.uv0 = bufferTexCoordSet0 != nullptr ? float2::MakeVector(bufferTexCoordSet0 + v * texCoordSet0Stride) : float2{};
462 BasicAttribs.uv1 = bufferTexCoordSet1 != nullptr ? float2::MakeVector(bufferTexCoordSet1 + v * texCoordSet1Stride) : float2{};
462463 // clang-format on
463 InitData->VertexData0.push_back(vert0);
464
465 VertexAttribs1 vert1{};
466 if (hasSkin)
464 InitData->VertexBasicData.push_back(BasicAttribs);
465
466 if (LoadSkin)
467467 {
468 vert1.joint0 = bufferJoints8 != nullptr ?
469 float4::MakeVector(bufferJoints8 + v * jointsStride) :
470 float4::MakeVector(bufferJoints16 + v * jointsStride);
471 vert1.weight0 = float4::MakeVector(bufferWeights + v * weightsStride);
468 VertexSkinAttribs SkinAttribs{};
469 if (hasSkin)
470 {
471 SkinAttribs.joint0 = bufferJoints8 != nullptr ?
472 float4::MakeVector(bufferJoints8 + v * jointsStride) :
473 float4::MakeVector(bufferJoints16 + v * jointsStride);
474 SkinAttribs.weight0 = float4::MakeVector(bufferWeights + v * weightsStride);
475 }
476 InitData->VertexSkinData.push_back(SkinAttribs);
472477 }
473 InitData->VertexData1.push_back(vert1);
474478 }
475479 }
476480
945949 }
946950 };
947951
948 if (!InitData->VertexData0.empty())
949 {
950 const auto& VertexData = InitData->VertexData0;
951 UpdateBuffer(BUFFER_ID_VERTEX0, VertexData.data(), VertexData.size() * sizeof(VertexData[0]));
952 }
953 if (!InitData->VertexData1.empty())
954 {
955 const auto& VertexData = InitData->VertexData1;
956 UpdateBuffer(BUFFER_ID_VERTEX1, VertexData.data(), VertexData.size() * sizeof(VertexData[0]));
952 if (!InitData->VertexBasicData.empty())
953 {
954 const auto& VertexData = InitData->VertexBasicData;
955 UpdateBuffer(BUFFER_ID_VERTEX_BASIC_ATTRIBS, VertexData.data(), VertexData.size() * sizeof(VertexData[0]));
956 }
957 if (!InitData->VertexSkinData.empty())
958 {
959 const auto& VertexData = InitData->VertexSkinData;
960 UpdateBuffer(BUFFER_ID_VERTEX_SKIN_ATTRIBS, VertexData.data(), VertexData.size() * sizeof(VertexData[0]));
957961 }
958962 if (!InitData->IndexData.empty())
959963 {
16671671 for (size_t i = 0; i < scene.nodes.size(); i++)
16681672 {
16691673 const tinygltf::Node node = gltf_model.nodes[scene.nodes[i]];
1670 LoadNode(pDevice, nullptr, node, scene.nodes[i], gltf_model);
1671 }
1672
1673 if (gltf_model.animations.size() > 0)
1674 {
1675 LoadAnimations(gltf_model);
1676 }
1677 LoadSkins(gltf_model);
1674 LoadNode(pDevice, nullptr, node, scene.nodes[i], gltf_model, CI.LoadAnimationAndSkin);
1675 }
1676
1677 if (CI.LoadAnimationAndSkin)
1678 {
1679 if (gltf_model.animations.size() > 0)
1680 {
1681 LoadAnimations(gltf_model);
1682 }
1683 LoadSkins(gltf_model);
1684 }
16781685
16791686 for (auto* node : LinearNodes)
16801687 {
16961703 Extensions = gltf_model.extensionsUsed;
16971704
16981705 {
1699 auto& VertexData0 = InitData->VertexData0;
1706 auto& VertexData0 = InitData->VertexBasicData;
17001707 auto BufferSize = static_cast<Uint32>(VertexData0.size() * sizeof(VertexData0[0]));
17011708 if (pResourceMgr != nullptr)
17021709 {
1703 Buffers[BUFFER_ID_VERTEX0].pSuballocation = pResourceMgr->AllocateBufferSpace(CI.pCacheInfo->VertexBuffer0Idx, BufferSize, 1);
1710 Buffers[BUFFER_ID_VERTEX_BASIC_ATTRIBS].pSuballocation = pResourceMgr->AllocateBufferSpace(CI.pCacheInfo->VertexBuffer0Idx, BufferSize, 1);
17041711 }
17051712 else
17061713 {
17131720 VBDesc.Usage = USAGE_IMMUTABLE;
17141721
17151722 BufferData BuffData(VertexData0.data(), VBDesc.uiSizeInBytes);
1716 pDevice->CreateBuffer(VBDesc, &BuffData, &Buffers[BUFFER_ID_VERTEX0].pBuffer);
1723 pDevice->CreateBuffer(VBDesc, &BuffData, &Buffers[BUFFER_ID_VERTEX_BASIC_ATTRIBS].pBuffer);
17171724
17181725 VertexData0.clear();
17191726 }
17201727 }
17211728
1722 {
1723 auto& VertexData1 = InitData->VertexData1;
1729 if (CI.LoadAnimationAndSkin)
1730 {
1731 auto& VertexData1 = InitData->VertexSkinData;
17241732 auto BufferSize = static_cast<Uint32>(VertexData1.size() * sizeof(VertexData1[0]));
17251733 if (pResourceMgr != nullptr)
17261734 {
1727 Buffers[BUFFER_ID_VERTEX1].pSuballocation = pResourceMgr->AllocateBufferSpace(CI.pCacheInfo->VertexBuffer1Idx, BufferSize, 1);
1735 Buffers[BUFFER_ID_VERTEX_SKIN_ATTRIBS].pSuballocation = pResourceMgr->AllocateBufferSpace(CI.pCacheInfo->VertexBuffer1Idx, BufferSize, 1);
17281736 }
17291737 else
17301738 {
17361744 VBDesc.Usage = USAGE_IMMUTABLE;
17371745
17381746 BufferData BuffData(VertexData1.data(), VBDesc.uiSizeInBytes);
1739 pDevice->CreateBuffer(VBDesc, &BuffData, &Buffers[BUFFER_ID_VERTEX1].pBuffer);
1747 pDevice->CreateBuffer(VBDesc, &BuffData, &Buffers[BUFFER_ID_VERTEX_SKIN_ATTRIBS].pBuffer);
17401748
17411749 VertexData1.clear();
17421750 }