From 1afce0ee557c4efe85ee5e46ae994b699b855458 Mon Sep 17 00:00:00 2001 From: Axel Date: Fri, 5 Nov 2021 17:46:43 +0100 Subject: [PATCH] Added checks to prevent rendering before component initilization for instance semantic segmentation. --- .../Source/Carla/Game/TaggedComponent.cpp | 59 +++++++++++-------- .../Carla/Source/Carla/Game/TaggedComponent.h | 4 ++ .../Carla/Source/Carla/Game/Tagger.cpp | 16 ++--- .../Sensor/InstanceSegmentationCamera.cpp | 33 +---------- 4 files changed, 47 insertions(+), 65 deletions(-) diff --git a/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Game/TaggedComponent.cpp b/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Game/TaggedComponent.cpp index c6d2b0306..ded570038 100644 --- a/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Game/TaggedComponent.cpp +++ b/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Game/TaggedComponent.cpp @@ -2,6 +2,7 @@ #include "TaggedComponent.h" #include "Rendering/SkeletalMeshRenderData.h" +#include "SkeletalRenderPublic.h" // // UTaggedComponent @@ -125,30 +126,29 @@ FPrimitiveSceneProxy * UTaggedComponent::CreateSceneProxy(UStaticMeshComponent * FPrimitiveSceneProxy * UTaggedComponent::CreateSceneProxy(USkeletalMeshComponent * SkeletalMeshComponent) { - - FSkeletalMeshRenderData * SkeletalMeshRenderData = SkeletalMeshComponent->GetSkeletalMeshRenderData(); - - // Only create a scene proxy for rendering if properly initialized - if (SkeletalMeshRenderData == NULL) + if (bShouldWaitFrame) { - UE_LOG(LogCarla, Error, TEXT("Failed to create scene proxy for skeletal mesh component: %s"), *SkeletalMeshComponent->GetReadableName()); - return NULL; + return nullptr; } + ERHIFeatureLevel::Type SceneFeatureLevel = GetWorld()->FeatureLevel; + FSkeletalMeshRenderData* SkelMeshRenderData = SkeletalMeshComponent->GetSkeletalMeshRenderData(); - if (SkeletalMeshRenderData->LODRenderData.IsValidIndex(SkeletalMeshComponent->PredictedLODLevel) == false) - { - UE_LOG(LogCarla, Error, TEXT("Failed to create scene proxy for skeletal mesh component: %s"), *SkeletalMeshComponent->GetReadableName()); - return NULL; - } - - if (SkeletalMeshComponent->MeshObject == NULL) - { - UE_LOG(LogCarla, Error, TEXT("Failed to create scene proxy for skeletal mesh component: %s"), *SkeletalMeshComponent->GetReadableName()); - return NULL; - } - - // FIXME: How does this allocation get freed? - return new FTaggedSkeletalMeshSceneProxy(SkeletalMeshComponent, SkeletalMeshRenderData, TaggedMID); + // Only create a scene proxy for rendering if properly initialized + if (SkelMeshRenderData && + SkelMeshRenderData->LODRenderData.IsValidIndex(SkeletalMeshComponent->PredictedLODLevel) && + !SkeletalMeshComponent->bHideSkin && + SkeletalMeshComponent->MeshObject) + { + // Only create a scene proxy if the bone count being used is supported, or if we don't have a skeleton (this is the case with destructibles) + int32 MinLODIndex = SkeletalMeshComponent->ComputeMinLOD(); + int32 MaxBonesPerChunk = SkelMeshRenderData->GetMaxBonesPerSection(MinLODIndex); + int32 MaxSupportedNumBones = SkeletalMeshComponent->MeshObject->IsCPUSkinned() ? MAX_int32 : GetFeatureLevelMaxNumberOfBones(SceneFeatureLevel); + if (MaxBonesPerChunk <= MaxSupportedNumBones) + { + return new FTaggedSkeletalMeshSceneProxy(SkeletalMeshComponent, SkelMeshRenderData, TaggedMID); + } + } + return nullptr; } FPrimitiveSceneProxy * UTaggedComponent::CreateSceneProxy(UHierarchicalInstancedStaticMeshComponent * MeshComponent) @@ -199,7 +199,16 @@ void UTaggedComponent::TickComponent(float DeltaTime, ELevelTick TickType, FActo // // TODO: Try removing this if (bSkeletalMesh) { + // MarkRenderTransformDirty(); MarkRenderStateDirty(); + if(bShouldWaitFrame) + { + if(NumFramesToWait < 0) + { + bShouldWaitFrame = false; + } + NumFramesToWait--; + } } } @@ -225,7 +234,7 @@ FPrimitiveViewRelevance FTaggedStaticMeshSceneProxy::GetViewRelevance(const FSce { FPrimitiveViewRelevance ViewRelevance = FStaticMeshSceneProxy::GetViewRelevance(View); - ViewRelevance.bDrawRelevance = !View->Family->EngineShowFlags.NotDrawTaggedComponents; + ViewRelevance.bDrawRelevance = ViewRelevance.bDrawRelevance && !View->Family->EngineShowFlags.NotDrawTaggedComponents; return ViewRelevance; } @@ -252,7 +261,7 @@ FPrimitiveViewRelevance FTaggedSkeletalMeshSceneProxy::GetViewRelevance(const FS { FPrimitiveViewRelevance ViewRelevance = FSkeletalMeshSceneProxy::GetViewRelevance(View); - ViewRelevance.bDrawRelevance = !View->Family->EngineShowFlags.NotDrawTaggedComponents; + ViewRelevance.bDrawRelevance = ViewRelevance.bDrawRelevance && !View->Family->EngineShowFlags.NotDrawTaggedComponents; return ViewRelevance; } @@ -277,7 +286,7 @@ FPrimitiveViewRelevance FTaggedInstancedStaticMeshSceneProxy::GetViewRelevance(c { FPrimitiveViewRelevance ViewRelevance = FInstancedStaticMeshSceneProxy::GetViewRelevance(View); - ViewRelevance.bDrawRelevance = !View->Family->EngineShowFlags.NotDrawTaggedComponents; + ViewRelevance.bDrawRelevance = ViewRelevance.bDrawRelevance && !View->Family->EngineShowFlags.NotDrawTaggedComponents; return ViewRelevance; } @@ -303,7 +312,7 @@ FPrimitiveViewRelevance FTaggedHierarchicalStaticMeshSceneProxy::GetViewRelevanc { FPrimitiveViewRelevance ViewRelevance = FHierarchicalStaticMeshSceneProxy::GetViewRelevance(View); - ViewRelevance.bDrawRelevance = !View->Family->EngineShowFlags.NotDrawTaggedComponents; + ViewRelevance.bDrawRelevance = ViewRelevance.bDrawRelevance && !View->Family->EngineShowFlags.NotDrawTaggedComponents; return ViewRelevance; } diff --git a/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Game/TaggedComponent.h b/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Game/TaggedComponent.h index 92dbaada0..7aa43885a 100644 --- a/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Game/TaggedComponent.h +++ b/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Game/TaggedComponent.h @@ -39,6 +39,10 @@ private: FPrimitiveSceneProxy * CreateSceneProxy(USkeletalMeshComponent * SkeletalMeshComponent); FPrimitiveSceneProxy * CreateSceneProxy(UHierarchicalInstancedStaticMeshComponent * MeshComponent); FPrimitiveSceneProxy * CreateSceneProxy(UInstancedStaticMeshComponent * MeshComponent); + + // small hack to allow unreal to initialize the base component in skeletal meshes + bool bShouldWaitFrame = true; + int NumFramesToWait = 2; }; class FTaggedStaticMeshSceneProxy : public FStaticMeshSceneProxy diff --git a/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Game/Tagger.cpp b/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Game/Tagger.cpp index 838dc33af..f9645288b 100644 --- a/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Game/Tagger.cpp +++ b/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Game/Tagger.cpp @@ -104,10 +104,10 @@ void ATagger::TagActor(const AActor &Actor, bool bTagForSemanticSegmentation) UE_LOG(LogCarla, Log, TEXT(" - Label: \"%s\""), *GetTagAsString(Label)); #endif // CARLA_TAGGER_EXTRA_LOG - // Don't instance segment non-things (i.e., stuff) - // if (!IsThing(Label)) { - // continue; - // } + if(!Component->IsVisible() || !Component->GetStaticMesh()) + { + continue; + } // Find a tagged component that is attached to this component UTaggedComponent *TaggedComponent = NULL; @@ -154,10 +154,10 @@ void ATagger::TagActor(const AActor &Actor, bool bTagForSemanticSegmentation) UE_LOG(LogCarla, Log, TEXT(" - Label: \"%s\""), *GetTagAsString(Label)); #endif // CARLA_TAGGER_EXTRA_LOG - // Don't instance segment non-things (i.e., stuff) - // if (!IsThing(Label)) { - // continue; - // } + if(!Component->IsVisible() || !Component->GetSkeletalMeshRenderData()) + { + continue; + } // Find a tagged component that is attached to this component UTaggedComponent *TaggedComponent = NULL; diff --git a/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Sensor/InstanceSegmentationCamera.cpp b/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Sensor/InstanceSegmentationCamera.cpp index c9701ad78..814ec956b 100644 --- a/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Sensor/InstanceSegmentationCamera.cpp +++ b/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Sensor/InstanceSegmentationCamera.cpp @@ -33,44 +33,14 @@ void AInstanceSegmentationCamera::SetUpSceneCaptureComponent(USceneCaptureCompon ApplyViewMode(VMI_Unlit, true, SceneCapture.ShowFlags); SceneCapture.ShowFlags.SetNotDrawTaggedComponents(false); // TaggedComponent detects this and sets view relevance for proxy material - //SceneCapture.ShowFlags.SetDecals(false); - //SceneCapture.ShowFlags.SetPostProcessing(false); - //SceneCapture.ShowFlags.SetAmbientCubemap(false); - //SceneCapture.ShowFlags.SetVignette(false); - //SceneCapture.ShowFlags.SetRectLights(false); - //SceneCapture.ShowFlags.SetToneCurve(false); - //SceneCapture.ShowFlags.SetScreenPercentage(false); - //SceneCapture.ShowFlags.SetReflectionEnvironment(false); - //SceneCapture.ShowFlags.SetSpecular(false); - //SceneCapture.ShowFlags.SetContactShadows(false); - //SceneCapture.ShowFlags.SetRayTracedDistanceFieldShadows(false); - //SceneCapture.ShowFlags.SetCapsuleShadows(false); - //SceneCapture.ShowFlags.SetVolumetricLightmap(false); - //SceneCapture.ShowFlags.SetIndirectLightingCache(false); - //SceneCapture.ShowFlags.SetTexturedLightProfiles(false); - //SceneCapture.ShowFlags.SetDeferredLighting(false); - //SceneCapture.ShowFlags.SetTranslucency(false); - //SceneCapture.ShowFlags.SetBillboardSprites(false); - //SceneCapture.ShowFlags.SetBSPTriangles(false); // hmm - //SceneCapture.ShowFlags.SetLandscape(false); // hmm - //SceneCapture.ShowFlags.SetBSP(false); // hmm - //SceneCapture.ShowFlags.SetPostProcessMaterial(false); // hmm - SceneCapture.ShowFlags.SetAtmosphere(false); - //SceneCapture.ShowFlags.SetPrecomputedVisibility(false); - //SceneCapture.ShowFlags.SetPaper2DSprites(false); - //SceneCapture.ShowFlags.SetDistanceFieldAO(false); - //SceneCapture.ShowFlags.SetWidgetComponents(false); - //SceneCapture.ShowFlags.SetMediaPlanes(false); - //SceneCapture.PostProcessSettings.AutoExposureBias = 0; + SceneCapture.ShowFlags.SetAtmosphere(false); SceneCapture.PrimitiveRenderMode = ESceneCapturePrimitiveRenderMode::PRM_UseShowOnlyList; TArray TaggedComponents; GetObjectsOfClass(UTaggedComponent::StaticClass(), TaggedComponents, false, EObjectFlags::RF_ClassDefaultObject, EInternalObjectFlags::AllFlags); - UE_LOG(LogCarla, Warning, TEXT("AInstanceSegmentationCamera::SetUpSceneCaptureComponent with %d tagged components!"), TaggedComponents.Num()); - TArray ShowOnlyComponents; for (UObject *Object : TaggedComponents) { UPrimitiveComponent *Component = Cast(Object); @@ -86,7 +56,6 @@ void AInstanceSegmentationCamera::PostPhysTick(UWorld *World, ELevelTick TickTyp TArray TaggedComponents; GetObjectsOfClass(UTaggedComponent::StaticClass(), TaggedComponents, false, EObjectFlags::RF_ClassDefaultObject, EInternalObjectFlags::AllFlags); - // UE_LOG(LogCarla, Warning, TEXT("AInstanceSegmentationCamera::SetUpSceneCaptureComponent with %d tagged components!"), TaggedComponents.Num()); SceneCapture->ClearShowOnlyComponents(); for (UObject *Object : TaggedComponents) { UPrimitiveComponent *Component = Cast(Object);