diff --git a/trinity/Eve/EveInstancedMeshManager.cpp b/trinity/Eve/EveInstancedMeshManager.cpp index 264357f2f..172b566bd 100644 --- a/trinity/Eve/EveInstancedMeshManager.cpp +++ b/trinity/Eve/EveInstancedMeshManager.cpp @@ -41,6 +41,38 @@ ITriRenderBatchAccumulator* FindBatchAccumulator( const T& batches, TriBatchType } } +EveInstancedMeshManager::~EveInstancedMeshManager() +{ + for( auto& [key, mesh] : m_meshInstances ) + { + for( auto& group : mesh.meshGroups ) + { + if( group.handle ) + { + group.handle->index = MeshGroupHandle::InvalidIndex; + group.handle->owner = nullptr; + } + } + } + + for( auto& [data, handle] : m_perObjectData ) + { + if( handle ) + { + handle->index = PerObjectDataHandle::InvalidIndex; + handle->owner = nullptr; + } + } + + for( auto& group : m_sphereGroups ) + { + if( group.handle ) + { + group.handle->index = BoundingSphereHandle::InvalidIndex; + group.handle->owner = nullptr; + } + } +} void EveInstancedMeshManager::CollectMeshes( EveComponentRegistry& registry ) { @@ -811,16 +843,22 @@ void EveInstancedMeshManager::GetPickingBatches( EvePendingPickingReadback& read { continue; } - InstanceBuffer::Allocation allocation = AllocateInstanceData( uint32_t( meshInfo.lodIndices[lod].size() ), mesh.isDynamic ); - UploadLodData( mesh, meshInfo, lod, allocation ); auto lodData = mesh.geometry->GetMeshLod( mesh.meshIndex, int( lod ) ); + if( !lodData || !lodData->m_allocationsValid ) + { + continue; + } + auto primCount = GetPrimitiveCount( *lodData, mesh.areaIndex, mesh.areaCount ); if( !primCount ) { continue; } + InstanceBuffer::Allocation allocation = AllocateInstanceData( uint32_t( meshInfo.lodIndices[lod].size() ), mesh.isDynamic ); + UploadLodData( mesh, meshInfo, lod, allocation ); + uint32_t stride = uint32_t( mesh.isDynamic ? sizeof( DynamicPerInstanceBufferElement ) : sizeof( StaticPerInstanceBufferElement ) ); Tr2RenderBatch batch; @@ -905,10 +943,16 @@ size_t EveInstancedMeshManager::GetBatches( const std::initializer_listGetMeshLod( mesh.meshIndex, int( lod ) ); + if( !lodData || !lodData->m_allocationsValid ) + { + instanceOffset += static_cast( meshInfo.lodIndices[lod].size() ); + continue; + } auto primCount = GetPrimitiveCount( *lodData, mesh.areaIndex, mesh.areaCount ); if( !primCount ) { + instanceOffset += static_cast( meshInfo.lodIndices[lod].size() ); continue; } diff --git a/trinity/Eve/EveInstancedMeshManager.h b/trinity/Eve/EveInstancedMeshManager.h index 5125ea925..e8161e052 100644 --- a/trinity/Eve/EveInstancedMeshManager.h +++ b/trinity/Eve/EveInstancedMeshManager.h @@ -10,6 +10,8 @@ BLUE_DECLARE( Tr2GpuStructuredBuffer ); class EveInstancedMeshManager { public: + ~EveInstancedMeshManager(); + struct InstanceFlags { void AddBatchType( TriBatchType type ); @@ -226,7 +228,7 @@ class EveInstancedMeshManager { std::vector meshGroups; - Tr2Effect* material = nullptr; + Tr2EffectPtr material = nullptr; float radius = 0.0f; float maxScreenSize = 0.0f; uint32_t totalVisibleInstances = 0; diff --git a/trinity/Eve/SpaceObject/Children/EveChildInstancedMeshes.cpp b/trinity/Eve/SpaceObject/Children/EveChildInstancedMeshes.cpp index 092483bdf..7798fdf56 100644 --- a/trinity/Eve/SpaceObject/Children/EveChildInstancedMeshes.cpp +++ b/trinity/Eve/SpaceObject/Children/EveChildInstancedMeshes.cpp @@ -29,6 +29,7 @@ EveChildInstancedMeshes::EveChildInstancedMeshes( IRoot* lockobj ) EveChildInstancedMeshes::~EveChildInstancedMeshes() { + UnregisterFromMeshManager(); for( Mesh& mesh : m_meshes ) { if( mesh.geometry ) @@ -735,7 +736,7 @@ void EveChildInstancedMeshes::AddMeshesToManager( EveInstancedMeshManager& manag { UnregisterFromMeshManager(); } - if( m_allRegistered ) + if( m_allRegistered && m_perObjectDataHandle ) { return; }