ModelComponent.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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(GpuSceneMesh) * 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;
  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] += U32(m_gpuSceneUniforms.m_offset);
  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<GpuSceneMesh> meshViews(info.m_framePool, modelPatchCount);
  70. for(U32 i = 0; i < modelPatchCount; ++i)
  71. {
  72. GpuSceneMesh& 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. const PtrSize elementSize = getFormatInfo(kMeshRelatedVertexStreamFormats[stream]).m_texelSize;
  90. ANKI_ASSERT((offset % elementSize) == 0);
  91. view.m_lods[l].m_vertexOffsets[U32(stream)] = U32(offset / elementSize);
  92. }
  93. PtrSize offset;
  94. U32 indexCount;
  95. IndexType indexType;
  96. mesh.getIndexBufferInfo(l, offset, indexCount, indexType);
  97. view.m_lods[l].m_indexOffset = U32(offset);
  98. view.m_lods[l].m_indexCount = indexCount;
  99. }
  100. // Copy the last LOD to the rest just in case
  101. for(U32 l = mesh.getLodCount(); l < kMaxLodCount; ++l)
  102. {
  103. view.m_lods[l] = view.m_lods[l - 1];
  104. }
  105. }
  106. gpuScenePatcher.newCopy(*info.m_framePool, m_gpuSceneMeshGpuViews.m_offset, meshViews.getSizeInBytes(),
  107. &meshViews[0]);
  108. // Upload the uniforms
  109. DynamicArrayRaii<U32> allUniforms(info.m_framePool, U32(m_gpuSceneUniforms.m_size / 4));
  110. U32 count = 0;
  111. for(U32 i = 0; i < modelPatchCount; ++i)
  112. {
  113. const ModelPatch& patch = m_model->getModelPatches()[i];
  114. const MaterialResource& mtl = *patch.getMaterial();
  115. memcpy(&allUniforms[count], mtl.getPrefilledLocalUniforms().getBegin(),
  116. mtl.getPrefilledLocalUniforms().getSizeInBytes());
  117. count += U32(mtl.getPrefilledLocalUniforms().getSizeInBytes() / 4);
  118. }
  119. ANKI_ASSERT(count * 4 == m_gpuSceneUniforms.m_size);
  120. gpuScenePatcher.newCopy(*info.m_framePool, m_gpuSceneUniforms.m_offset, m_gpuSceneUniforms.m_size,
  121. &allUniforms[0]);
  122. }
  123. updated = m_dirty;
  124. m_dirty = false;
  125. return Error::kNone;
  126. }
  127. } // end namespace anki