diff --git a/trinity/Eve/EveInstancedMeshManager.cpp b/trinity/Eve/EveInstancedMeshManager.cpp index 70d670603..38c63c6eb 100644 --- a/trinity/Eve/EveInstancedMeshManager.cpp +++ b/trinity/Eve/EveInstancedMeshManager.cpp @@ -59,15 +59,51 @@ LodSlotDraw GetLodSlotDraw( TriGeometryRes& geometry, uint32_t meshIndex, uint32 { LodSlotDraw draw; draw.lodData = geometry.GetMeshLod( meshIndex, int( slot / SLOTS_PER_LOD ) ); - draw.primCount = GetPrimitiveCount( *draw.lodData, areaIndex, areaCount ); - bool reversed = areaReversed != ( ( slot % SLOTS_PER_LOD ) != 0 ); - draw.indexRange = GetAreaIndexRange( *draw.lodData, areaIndex, draw.primCount, reversed ); - draw.valid = draw.primCount != 0 && draw.indexRange.valid; + if( draw.lodData && draw.lodData->m_allocationsValid ) + { + draw.primCount = GetPrimitiveCount( *draw.lodData, areaIndex, areaCount ); + bool reversed = areaReversed != ( ( slot % SLOTS_PER_LOD ) != 0 ); + draw.indexRange = GetAreaIndexRange( *draw.lodData, areaIndex, draw.primCount, reversed ); + draw.valid = draw.primCount != 0 && draw.indexRange.valid; + } return draw; } } +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 ) { TRINITY_STATS_ZONE( __FUNCTION__ ); @@ -155,6 +191,7 @@ void EveInstancedMeshManager::RemovePerObjectData( PerObjectDataHandle& handle ) m_perObjectData.pop_back(); handle.index = PerObjectDataHandle::InvalidIndex; + handle.owner = nullptr; } void EveInstancedMeshManager::ReplaceHandle( PerObjectDataHandle* oldHandle, PerObjectDataHandle* newHandle ) @@ -379,6 +416,7 @@ void EveInstancedMeshManager::RemoveMeshGroup( MeshGroupHandle& handle ) } } handle.index = MeshGroupHandle::InvalidIndex; + handle.owner = nullptr; } } @@ -802,14 +840,16 @@ void EveInstancedMeshManager::GetPickingBatches( EvePendingPickingReadback& read { continue; } - InstanceBuffer::Allocation allocation = AllocateInstanceData( uint32_t( meshInfo.lodIndices[lod].size() ), mesh.isDynamic ); - UploadLodData( mesh, meshInfo, lod, allocation ); auto draw = GetLodSlotDraw( *mesh.geometry, mesh.meshIndex, mesh.areaIndex, mesh.areaCount, mesh.areaReversed, lod ); if( !draw.valid ) { 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; diff --git a/trinity/Eve/EveInstancedMeshManager.h b/trinity/Eve/EveInstancedMeshManager.h index ba7505691..e8ede912b 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 ); @@ -238,7 +240,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 d61de1506..3f3c9d643 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 ) @@ -748,7 +749,7 @@ void EveChildInstancedMeshes::AddMeshesToManager( EveInstancedMeshManager& manag { UnregisterFromMeshManager(); } - if( m_allRegistered ) + if( m_allRegistered && m_perObjectDataHandle ) { return; }