From 2309ffb18464a2a60123cce0958b59999ffac4e6 Mon Sep 17 00:00:00 2001 From: nsubiron Date: Wed, 10 May 2017 12:17:55 +0200 Subject: [PATCH] #34 Do not set custom depth pass if semantic segmentation is not needed --- Source/Carla/CityMapGenerator.cpp | 2 +- Source/Carla/CityMapGenerator.h | 8 +++++++ Source/Carla/Game/CarlaGameMode.cpp | 6 ++++-- Source/Carla/Game/CarlaSettings.cpp | 9 +++++++- Source/Carla/Game/CarlaSettings.h | 9 +++++++- Source/Carla/Tagger.cpp | 33 ++++++++++++----------------- Source/Carla/Tagger.h | 26 +++++++++++++++++++---- 7 files changed, 65 insertions(+), 28 deletions(-) diff --git a/Source/Carla/CityMapGenerator.cpp b/Source/Carla/CityMapGenerator.cpp index 49388fe24..a86099f0c 100644 --- a/Source/Carla/CityMapGenerator.cpp +++ b/Source/Carla/CityMapGenerator.cpp @@ -224,7 +224,7 @@ void ACityMapGenerator::GenerateRoadMap() check(GetWorld() != nullptr); check(RoadMap != nullptr); - ATagger::TagActorsInLevel(*GetWorld()); // We need the tags. + ATagger::TagActorsInLevel(*GetWorld(), bTagForSemanticSegmentation); // We need the tags. const float IntersectionSize = CityMapMeshTag::GetRoadIntersectionSize(); const uint32 Margin = IntersectionSize / 2u; diff --git a/Source/Carla/CityMapGenerator.h b/Source/Carla/CityMapGenerator.h index ca463d1ad..55fac69ff 100644 --- a/Source/Carla/CityMapGenerator.h +++ b/Source/Carla/CityMapGenerator.h @@ -152,6 +152,14 @@ private: UPROPERTY(Category = "Road Map", EditAnywhere, AdvancedDisplay) bool bGenerateRoadMapOnSave = true; + /** If true, activate the custom depth pass of each tagged actor in the level. + * This pass is necessary for rendering the semantic segmentation. However, + * it may add a performance penalty since occlusion doesn't seem to be + * applied to objects having this value active. + */ + UPROPERTY(Category = "Road Map", EditAnywhere, AdvancedDisplay) + bool bTagForSemanticSegmentation = false; + UPROPERTY() URoadMap *RoadMap; diff --git a/Source/Carla/Game/CarlaGameMode.cpp b/Source/Carla/Game/CarlaGameMode.cpp index 4fb66d4c3..cee954c05 100644 --- a/Source/Carla/Game/CarlaGameMode.cpp +++ b/Source/Carla/Game/CarlaGameMode.cpp @@ -70,7 +70,9 @@ void ACarlaGameMode::RestartPlayer(AController* NewPlayer) void ACarlaGameMode::BeginPlay() { Super::BeginPlay(); - TagActorsForSemanticSegmentation(); + if (GameInstance->GetCarlaSettings().bSemanticSegmentationEnabled) { + TagActorsForSemanticSegmentation(); + } GameController->BeginPlay(); } @@ -105,7 +107,7 @@ void ACarlaGameMode::AttachCaptureCamerasToPlayer(AController &Player) void ACarlaGameMode::TagActorsForSemanticSegmentation() { check(GetWorld() != nullptr); - ATagger::TagActorsInLevel(*GetWorld()); + ATagger::TagActorsInLevel(*GetWorld(), true); } APlayerStart *ACarlaGameMode::FindUnOccupiedStartPoints( diff --git a/Source/Carla/Game/CarlaSettings.cpp b/Source/Carla/Game/CarlaSettings.cpp index a757d43a2..03adea5f4 100644 --- a/Source/Carla/Game/CarlaSettings.cpp +++ b/Source/Carla/Game/CarlaSettings.cpp @@ -112,6 +112,11 @@ static void ValidateCameraDescription(FCameraDescription &Camera) Camera.ImageSizeY = (Camera.ImageSizeY == 0u ? 512u : Camera.ImageSizeY); } +static bool RequestedSemanticSegmentation(const FCameraDescription &Camera) +{ + return (Camera.PostProcessEffect == EPostProcessEffect::SemanticSegmentation); +} + static void LoadSettingsFromFile(const FString &FileName, UCarlaSettings &Settings) { UE_LOG(LogCarla, Log, TEXT("Loading settings from \"%s\""), *FileName); @@ -141,6 +146,7 @@ static void LoadSettingsFromFile(const FString &FileName, UCarlaSettings &Settin } ValidateCameraDescription(Camera); + Settings.bSemanticSegmentationEnabled |= RequestedSemanticSegmentation(Camera); } } @@ -183,7 +189,7 @@ void UCarlaSettings::LoadSettings() } } -void UCarlaSettings::LogSettings() +void UCarlaSettings::LogSettings() const { UE_LOG(LogCarla, Log, TEXT("== CARLA Settings ==============================================================")); UE_LOG(LogCarla, Log, TEXT("Settings file: %s"), *CurrentFileName); @@ -194,6 +200,7 @@ void UCarlaSettings::LogSettings() UE_LOG(LogCarla, Log, TEXT("Read Port = %d"), ReadPort); UE_LOG(LogCarla, Log, TEXT("[%s]"), S_CARLA_SCENECAPTURE); UE_LOG(LogCarla, Log, TEXT("Added %d cameras."), CameraDescriptions.Num()); + UE_LOG(LogCarla, Log, TEXT("Semantic Segmentation = %s"), (bSemanticSegmentationEnabled ? TEXT("enabled") : TEXT("disabled"))); for (auto &Item : CameraDescriptions) { UE_LOG(LogCarla, Log, TEXT("[%s/%s]"), S_CARLA_SCENECAPTURE, *Item.Key); UE_LOG(LogCarla, Log, TEXT("Image Size = %dx%d"), Item.Value.ImageSizeX, Item.Value.ImageSizeY); diff --git a/Source/Carla/Game/CarlaSettings.h b/Source/Carla/Game/CarlaSettings.h index 0b56688fc..ffa0cb3d9 100644 --- a/Source/Carla/Game/CarlaSettings.h +++ b/Source/Carla/Game/CarlaSettings.h @@ -18,7 +18,7 @@ public: void LoadSettings(); /** Log settings values. */ - void LogSettings(); + void LogSettings() const; private: @@ -59,5 +59,12 @@ public: UPROPERTY(Category = "Scene Capture", EditDefaultsOnly) TMap CameraDescriptions; + /** Whether semantic segmentation should be activated. The mechanisms for + * semantic segmentation impose some performance penalties even if it is not + * used, we only enable it if necessary. + */ + UPROPERTY(Category = "Scene Capture", EditDefaultsOnly) + bool bSemanticSegmentationEnabled = false; + /// @} }; diff --git a/Source/Carla/Tagger.cpp b/Source/Carla/Tagger.cpp index 3f1734e70..6dba77700 100644 --- a/Source/Carla/Tagger.cpp +++ b/Source/Carla/Tagger.cpp @@ -62,18 +62,21 @@ static ECityObjectLabel GetLabelByPath(const T *Object) return (StringArray.Num() > 3 ? GetLabelByFolderName(StringArray[3]) : ECityObjectLabel::None); } -static void SetStencilValue(UPrimitiveComponent *comp, const ECityObjectLabel &Label) { - if (Label != ECityObjectLabel::None) { - comp->SetRenderCustomDepth(true); - comp->SetCustomDepthStencilValue(CastEnum(Label)); - } +static void SetStencilValue( + UPrimitiveComponent &Component, + const ECityObjectLabel &Label, + const bool bSetRenderCustomDepth) { + Component.SetRenderCustomDepth( + bSetRenderCustomDepth && + !ATagger::MatchComponent(Component, ECityObjectLabel::None)); + Component.SetCustomDepthStencilValue(CastEnum(Label)); } // ============================================================================= // -- static ATagger functions ------------------------------------------------- // ============================================================================= -void ATagger::TagActor(const AActor &Actor) +void ATagger::TagActor(const AActor &Actor, bool bTagForSemanticSegmentation) { #ifdef CARLA_TAGGER_EXTRA_LOG UE_LOG(LogCarla, Log, TEXT("Actor: %s"), *Actor.GetName()); @@ -84,7 +87,7 @@ void ATagger::TagActor(const AActor &Actor) Actor.GetComponents(StaticMeshComponents); for (UStaticMeshComponent *Component : StaticMeshComponents) { const auto Label = GetLabelByPath(Component->GetStaticMesh()); - SetStencilValue(Component, Label); + SetStencilValue(*Component, Label, bTagForSemanticSegmentation); #ifdef CARLA_TAGGER_EXTRA_LOG UE_LOG(LogCarla, Log, TEXT(" + StaticMeshComponent: %s"), *Component->GetName()); UE_LOG(LogCarla, Log, TEXT(" - Label: \"%s\""), *GetLabelAsString(Label)); @@ -96,7 +99,7 @@ void ATagger::TagActor(const AActor &Actor) Actor.GetComponents(SkeletalMeshComponents); for (USkeletalMeshComponent *Component : SkeletalMeshComponents) { const auto Label = GetLabelByPath(Component->GetPhysicsAsset()); - SetStencilValue(Component, Label); + SetStencilValue(*Component, Label, bTagForSemanticSegmentation); #ifdef CARLA_TAGGER_EXTRA_LOG UE_LOG(LogCarla, Log, TEXT(" + SkeletalMeshComponent: %s"), *Component->GetName()); UE_LOG(LogCarla, Log, TEXT(" - Label: \"%s\""), *GetLabelAsString(Label)); @@ -104,21 +107,13 @@ void ATagger::TagActor(const AActor &Actor) } } -void ATagger::TagActorsInLevel(UWorld &World) +void ATagger::TagActorsInLevel(UWorld &World, bool bTagForSemanticSegmentation) { for (TActorIterator it(&World); it; ++it) { - TagActor(**it); + TagActor(**it, bTagForSemanticSegmentation); } } -ECityObjectLabel ATagger::GetTagOfTaggedComponent(const UPrimitiveComponent &Component) -{ - return - (Component.bRenderCustomDepth ? - static_cast(Component.CustomDepthStencilValue) : - ECityObjectLabel::None); -} - void ATagger::GetTagsOfTaggedActor(const AActor &Actor, TArray &Tags) { TArray Components; @@ -148,7 +143,7 @@ void ATagger::PostEditChangeProperty(FPropertyChangedEvent& PropertyChangedEvent Super::PostEditChangeProperty(PropertyChangedEvent); if (PropertyChangedEvent.Property) { if (bTriggerTagObjects && (GetWorld() != nullptr)) { - TagActorsInLevel(*GetWorld()); + TagActorsInLevel(*GetWorld(), bTagForSemanticSegmentation); } } bTriggerTagObjects = false; diff --git a/Source/Carla/Tagger.h b/Source/Carla/Tagger.h index e912728fc..5824ed6f6 100644 --- a/Source/Carla/Tagger.h +++ b/Source/Carla/Tagger.h @@ -3,6 +3,7 @@ #pragma once #include "GameFramework/Actor.h" +#include "Components/PrimitiveComponent.h" #include "Tagger.generated.h" enum class ECityObjectLabel : uint8 @@ -34,15 +35,29 @@ class CARLA_API ATagger : public AActor public: /// Set the tag of an actor. - static void TagActor(const AActor &Actor); + /// + /// If bTagForSemanticSegmentation true, activate the custom depth pass. This + /// pass is necessary for rendering the semantic segmentation. However, it may + /// add a performance penalty since occlusion doesn't seem to be applied to + /// objects having this value active. + static void TagActor(const AActor &Actor, bool bTagForSemanticSegmentation); /// Set the tag of every actor in level. - static void TagActorsInLevel(UWorld &World); + /// + /// If bTagForSemanticSegmentation true, activate the custom depth pass. This + /// pass is necessary for rendering the semantic segmentation. However, it may + /// add a performance penalty since occlusion doesn't seem to be applied to + /// objects having this value active. + static void TagActorsInLevel(UWorld &World, bool bTagForSemanticSegmentation); /// Retrieve the tag of an already tagged component. - static ECityObjectLabel GetTagOfTaggedComponent(const UPrimitiveComponent &Component); + static ECityObjectLabel GetTagOfTaggedComponent(const UPrimitiveComponent &Component) + { + return static_cast(Component.CustomDepthStencilValue); + } - /// Retrieve the tags of an already tagged actor. + /// Retrieve the tags of an already tagged actor. ECityObjectLabel::None is + /// not added to the array. static void GetTagsOfTaggedActor(const AActor &Actor, TArray &Tags); /// Return true if @a Component has been tagged with the given @a Tag. @@ -63,4 +78,7 @@ private: UPROPERTY(Category = "Tagger", EditAnywhere) bool bTriggerTagObjects = false; + + UPROPERTY(Category = "Tagger", EditAnywhere) + bool bTagForSemanticSegmentation = false; };