MeshComponent.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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/Scene/Components/MeshComponent.h>
  6. #include <AnKi/Resource/ResourceManager.h>
  7. #include <AnKi/Resource/MeshResource.h>
  8. #include <AnKi/Core/App.h>
  9. namespace anki {
  10. MeshComponent::MeshComponent(SceneNode* node)
  11. : SceneComponent(node, kClassType)
  12. {
  13. }
  14. MeshComponent::~MeshComponent()
  15. {
  16. }
  17. MeshComponent& MeshComponent::setMeshFilename(CString fname)
  18. {
  19. MeshResourcePtr newRsrc;
  20. const Error err = ResourceManager::getSingleton().loadResource(fname, newRsrc);
  21. if(err)
  22. {
  23. ANKI_SCENE_LOGE("Failed to load resource: %s", fname.cstr());
  24. }
  25. else
  26. {
  27. m_resource = newRsrc;
  28. m_resourceDirty = true;
  29. }
  30. return *this;
  31. }
  32. CString MeshComponent::getMeshFilename() const
  33. {
  34. return (m_resource) ? m_resource->getFilename() : "*Error*";
  35. }
  36. Bool MeshComponent::isValid() const
  37. {
  38. return !!m_resource && m_resource->isLoaded();
  39. }
  40. void MeshComponent::update([[maybe_unused]] SceneComponentUpdateInfo& info, Bool& updated)
  41. {
  42. if(!isValid()) [[unlikely]]
  43. {
  44. for(GpuSceneArrays::MeshLod::Allocation& alloc : m_gpuSceneMeshLods)
  45. {
  46. alloc.free();
  47. }
  48. return;
  49. }
  50. m_gpuSceneMeshLodsReallocatedThisFrame = false;
  51. if(!m_resourceDirty) [[likely]]
  52. {
  53. return;
  54. }
  55. updated = true;
  56. m_resourceDirty = false;
  57. const MeshResource& mesh = *m_resource;
  58. const U32 submeshCount = mesh.getSubMeshCount();
  59. if(m_gpuSceneMeshLods.getSize() != submeshCount)
  60. {
  61. m_gpuSceneMeshLods.destroy();
  62. m_gpuSceneMeshLods.resize(submeshCount);
  63. for(GpuSceneArrays::MeshLod::Allocation& a : m_gpuSceneMeshLods)
  64. {
  65. a.allocate();
  66. m_gpuSceneMeshLodsReallocatedThisFrame = true;
  67. }
  68. }
  69. for(U32 submeshIdx = 0; submeshIdx < submeshCount; ++submeshIdx)
  70. {
  71. Array<GpuSceneMeshLod, kMaxLodCount> meshLods;
  72. for(U32 l = 0; l < mesh.getLodCount(); ++l)
  73. {
  74. GpuSceneMeshLod& meshLod = meshLods[l];
  75. meshLod = {};
  76. meshLod.m_positionScale = mesh.getPositionsScale();
  77. meshLod.m_positionTranslation = mesh.getPositionsTranslation();
  78. U32 firstIndex, indexCount, firstMeshlet, meshletCount;
  79. Aabb aabb;
  80. mesh.getSubMeshInfo(l, submeshIdx, firstIndex, indexCount, firstMeshlet, meshletCount, aabb);
  81. U32 totalIndexCount;
  82. IndexType indexType;
  83. PtrSize indexUgbOffset;
  84. mesh.getIndexBufferInfo(l, indexUgbOffset, totalIndexCount, indexType);
  85. for(VertexStreamId stream = VertexStreamId::kMeshRelatedFirst; stream < VertexStreamId::kMeshRelatedCount; ++stream)
  86. {
  87. if(mesh.isVertexStreamPresent(stream))
  88. {
  89. U32 vertCount;
  90. PtrSize ugbOffset;
  91. mesh.getVertexBufferInfo(l, stream, ugbOffset, vertCount);
  92. const PtrSize elementSize = getFormatInfo(kMeshRelatedVertexStreamFormats[stream]).m_texelSize;
  93. ANKI_ASSERT(ugbOffset % elementSize == 0);
  94. meshLod.m_vertexOffsets[U32(stream)] = U32(ugbOffset / elementSize);
  95. }
  96. else
  97. {
  98. meshLod.m_vertexOffsets[U32(stream)] = kMaxU32;
  99. }
  100. }
  101. meshLod.m_indexCount = indexCount;
  102. ANKI_ASSERT(indexUgbOffset % getIndexSize(indexType) == 0);
  103. meshLod.m_firstIndex = U32(indexUgbOffset / getIndexSize(indexType)) + firstIndex;
  104. if(GrManager::getSingleton().getDeviceCapabilities().m_meshShaders || g_cvarCoreMeshletRendering)
  105. {
  106. U32 dummy;
  107. PtrSize meshletBoundingVolumesUgbOffset, meshletGometryDescriptorsUgbOffset;
  108. mesh.getMeshletBufferInfo(l, meshletBoundingVolumesUgbOffset, meshletGometryDescriptorsUgbOffset, dummy);
  109. meshLod.m_firstMeshletBoundingVolume = firstMeshlet + U32(meshletBoundingVolumesUgbOffset / sizeof(MeshletBoundingVolume));
  110. meshLod.m_firstMeshletGeometryDescriptor = firstMeshlet + U32(meshletGometryDescriptorsUgbOffset / sizeof(MeshletGeometryDescriptor));
  111. meshLod.m_meshletCount = meshletCount;
  112. }
  113. meshLod.m_lod = l;
  114. if(GrManager::getSingleton().getDeviceCapabilities().m_rayTracingEnabled)
  115. {
  116. const U64 address = mesh.getBottomLevelAccelerationStructure(l, submeshIdx)->getGpuAddress();
  117. memcpy(&meshLod.m_blasAddress[0], &address, sizeof(meshLod.m_blasAddress));
  118. meshLod.m_tlasInstanceMask = 0xFFFFFFFF;
  119. }
  120. }
  121. // Copy the last LOD to the rest just in case
  122. for(U32 l = mesh.getLodCount(); l < kMaxLodCount; ++l)
  123. {
  124. meshLods[l] = meshLods[l - 1];
  125. }
  126. m_gpuSceneMeshLods[submeshIdx].uploadToGpuScene(meshLods);
  127. }
  128. }
  129. Error MeshComponent::serialize(SceneSerializer& serializer)
  130. {
  131. ANKI_SERIALIZE(m_resource, 1);
  132. return Error::kNone;
  133. }
  134. } // end namespace anki