2
0

ModelComponent.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. // Copyright (C) 2009-2022, 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/Scene/Components/ModelComponent.h>
  6. #include <AnKi/Scene/SceneNode.h>
  7. #include <AnKi/Scene/SceneGraph.h>
  8. #include <AnKi/Resource/ModelResource.h>
  9. #include <AnKi/Resource/ResourceManager.h>
  10. namespace anki {
  11. ANKI_SCENE_COMPONENT_STATICS(ModelComponent)
  12. ModelComponent::ModelComponent(SceneNode* node)
  13. : SceneComponent(node, getStaticClassId())
  14. , m_node(node)
  15. {
  16. }
  17. ModelComponent::~ModelComponent()
  18. {
  19. m_modelPatchMergeKeys.destroy(m_node->getMemoryPool());
  20. GpuSceneMemoryPool& gpuScene = *getExternalSubsystems(*m_node).m_gpuSceneMemoryPool;
  21. gpuScene.free(m_gpuSceneMeshGpuViews);
  22. gpuScene.free(m_gpuSceneUniforms);
  23. m_gpuSceneUniformsOffsetPerPatch.destroy(m_node->getMemoryPool());
  24. }
  25. Error ModelComponent::loadModelResource(CString filename)
  26. {
  27. m_dirty = true;
  28. ModelResourcePtr rsrc;
  29. ANKI_CHECK(getExternalSubsystems(*m_node).m_resourceManager->loadResource(filename, rsrc));
  30. m_model = std::move(rsrc);
  31. const U32 modelPatchCount = m_model->getModelPatches().getSize();
  32. m_modelPatchMergeKeys.destroy(m_node->getMemoryPool());
  33. m_modelPatchMergeKeys.create(m_node->getMemoryPool(), modelPatchCount);
  34. for(U32 i = 0; i < modelPatchCount; ++i)
  35. {
  36. Array<U64, 2> toHash;
  37. toHash[0] = i;
  38. toHash[1] = m_model->getUuid();
  39. m_modelPatchMergeKeys[i] = computeHash(&toHash[0], sizeof(toHash));
  40. }
  41. // GPU scene allocations
  42. GpuSceneMemoryPool& gpuScene = *getExternalSubsystems(*m_node).m_gpuSceneMemoryPool;
  43. gpuScene.free(m_gpuSceneMeshGpuViews);
  44. gpuScene.allocate(sizeof(MeshGpuView) * m_modelPatchMergeKeys.getSize(), 4, m_gpuSceneMeshGpuViews);
  45. U32 uniformsSize = 0;
  46. m_gpuSceneUniformsOffsetPerPatch.resize(m_node->getMemoryPool(), modelPatchCount);
  47. for(U32 i = 0; i < modelPatchCount; ++i)
  48. {
  49. m_gpuSceneUniformsOffsetPerPatch[i] = uniformsSize / 4;
  50. const U32 size = U32(m_model->getModelPatches()[i].getMaterial()->getPrefilledLocalUniforms().getSizeInBytes());
  51. ANKI_ASSERT((size % 4) == 0);
  52. uniformsSize += size;
  53. }
  54. gpuScene.free(m_gpuSceneUniforms);
  55. gpuScene.allocate(uniformsSize, 4, m_gpuSceneUniforms);
  56. for(U32 i = 0; i < modelPatchCount; ++i)
  57. {
  58. m_gpuSceneUniformsOffsetPerPatch[i] += DwordOffset(m_gpuSceneUniforms.m_offset / 4);
  59. }
  60. return Error::kNone;
  61. }
  62. Error ModelComponent::update(SceneComponentUpdateInfo& info, Bool& updated)
  63. {
  64. if(ANKI_UNLIKELY(m_dirty && m_model.isCreated()))
  65. {
  66. GpuSceneMicroPatcher& gpuScenePatcher = *getExternalSubsystems(*info.m_node).m_gpuSceneMicroPatcher;
  67. // Upload the mesh views
  68. const U32 modelPatchCount = m_model->getModelPatches().getSize();
  69. DynamicArrayRaii<MeshGpuView> meshViews(info.m_framePool, modelPatchCount);
  70. for(U32 i = 0; i < modelPatchCount; ++i)
  71. {
  72. MeshGpuView& view = meshViews[i];
  73. const ModelPatch& patch = m_model->getModelPatches()[i];
  74. const MeshResource& mesh = *patch.getMesh();
  75. zeroMemory(view);
  76. view.m_positionScale = mesh.getPositionsScale();
  77. view.m_positionTranslation = mesh.getPositionsTranslation();
  78. for(U32 l = 0; l < mesh.getLodCount(); ++l)
  79. {
  80. for(VertexStreamId stream = VertexStreamId::kPosition; stream <= VertexStreamId::kBoneWeights; ++stream)
  81. {
  82. if(!mesh.isVertexStreamPresent(stream))
  83. {
  84. continue;
  85. }
  86. PtrSize offset;
  87. U32 vertCount;
  88. mesh.getVertexStreamInfo(l, stream, offset, vertCount);
  89. ANKI_ASSERT((offset % 4) == 0);
  90. view.m_vertexOffsets[l][U32(stream)] = U32(offset / 4);
  91. }
  92. PtrSize offset;
  93. U32 indexCount;
  94. IndexType indexType;
  95. mesh.getIndexBufferInfo(l, offset, indexCount, indexType);
  96. view.m_indexOffsets[l] = U32(offset);
  97. view.m_indexCounts[l] = indexCount;
  98. }
  99. }
  100. gpuScenePatcher.newCopy(*info.m_framePool, m_gpuSceneMeshGpuViews.m_offset, meshViews.getSizeInBytes(),
  101. &meshViews[0]);
  102. // Upload the uniforms
  103. DynamicArrayRaii<U32> allUniforms(info.m_framePool, U32(m_gpuSceneUniforms.m_size / 4));
  104. U32 count = 0;
  105. for(U32 i = 0; i < modelPatchCount; ++i)
  106. {
  107. const ModelPatch& patch = m_model->getModelPatches()[i];
  108. const MaterialResource& mtl = *patch.getMaterial();
  109. memcpy(&allUniforms[count], mtl.getPrefilledLocalUniforms().getBegin(),
  110. mtl.getPrefilledLocalUniforms().getSizeInBytes());
  111. count += U32(mtl.getPrefilledLocalUniforms().getSizeInBytes() / 4);
  112. }
  113. ANKI_ASSERT(count * 4 == m_gpuSceneUniforms.m_size);
  114. gpuScenePatcher.newCopy(*info.m_framePool, m_gpuSceneUniforms.m_offset, m_gpuSceneUniforms.m_size,
  115. &allUniforms[0]);
  116. }
  117. updated = m_dirty;
  118. m_dirty = false;
  119. return Error::kNone;
  120. }
  121. } // end namespace anki