diff --git a/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Game/CarlaGameModeBase.cpp b/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Game/CarlaGameModeBase.cpp index 977925553..51e315a99 100644 --- a/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Game/CarlaGameModeBase.cpp +++ b/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Game/CarlaGameModeBase.cpp @@ -196,7 +196,7 @@ void ACarlaGameModeBase::BeginPlay() // OnBeginFrame // OnEndFrame CaptureAtlasDelegate = FCoreDelegates::OnEndFrame.AddUObject(this, &ACarlaGameModeBase::CaptureAtlas); - SendAtlasDelegate = FCoreDelegates::OnEndFrame.AddUObject(this, &ACarlaGameModeBase::SendAtlas); + // SendAtlasDelegate = FCoreDelegates::OnEndFrame.AddUObject(this, &ACarlaGameModeBase::SendAtlas); } @@ -433,11 +433,18 @@ void ACarlaGameModeBase::CaptureAtlas() if(!SceneCaptureSensors.Num()) return; + // Be sure that the atlas texture is ready if(!IsAtlasTextureValid) { CreateAtlasTextures(); } + // Enqueue the commands to copy the captures to the atlas + for(ASceneCaptureSensor* Sensor : SceneCaptureSensors) + { + Sensor->CopyTextureToAtlas(); + } + //if (!AtlasCopyRequestQueue.IsEmpty()) //{ // UE_LOG(LogCarla, Error, TEXT("ACarlaGameModeBase::CaptureAtlas: request full")); @@ -448,12 +455,12 @@ void ACarlaGameModeBase::CaptureAtlas() FAtlasCopyRequest* AtlasCopyRequest = GetAtlasCopyRequest(); AtlasCopyRequest->ResizeBuffer(AtlasTextureWidth, AtlasTextureHeight); + // Download Atlas texture ENQUEUE_RENDER_COMMAND(ACarlaGameModeBase_CaptureAtlas) ( [This, &CurrentAtlasPixels = AtlasCopyRequest->AtlasImage](FRHICommandListImmediate& RHICmdList) mutable { FTexture2DRHIRef AtlasTexture = This->CamerasAtlasTexture[This->PreviousAtlas]; - //TArray& CurrentAtlasPixels = This->AtlasPixels[This->CurrentAtlas]; if (!AtlasTexture) { @@ -461,8 +468,6 @@ void ACarlaGameModeBase::CaptureAtlas() return; } - // Download Atlas texture - FIntRect Rect = FIntRect(0, 0, This->AtlasTextureWidth, This->AtlasTextureHeight); #if !UE_BUILD_SHIPPING @@ -470,7 +475,6 @@ void ACarlaGameModeBase::CaptureAtlas() Rect = FIntRect(0, 0, This->SurfaceW, This->SurfaceH); } #endif - // GDynamicRHI->RHIReadSurfaceData(AtlasTexture, Rect, CurrentAtlasPixels, FReadSurfaceDataFlags(RCM_UNorm, CubeFace_MAX)); #if !UE_BUILD_SHIPPING @@ -484,16 +488,17 @@ void ACarlaGameModeBase::CaptureAtlas() CurrentAtlasPixels, FReadSurfaceDataFlags(RCM_UNorm, CubeFace_MAX)); - - This->PreviousAtlas = This->CurrentAtlas; - This->CurrentAtlas = (This->CurrentAtlas + 1) & ~kMaxNumTextures; + // This->PreviousAtlas = This->CurrentAtlas; + // This->CurrentAtlas = (This->CurrentAtlas + 1) & ~kMaxNumTextures; } ); - // AtlasCopyRequest->Start(); + AtlasCopyRequest->Start(); // AtlasCopyRequest->Wait(); AtlasCopyRequestQueue.Enqueue(AtlasCopyRequest); + SendAtlas(); + } void ACarlaGameModeBase::SendAtlas() @@ -505,7 +510,7 @@ void ACarlaGameModeBase::SendAtlas() // Be sure that the request has finished - if(AtlasCopyRequest /* && AtlasCopyRequest->IsComplete() */ ) + if( AtlasCopyRequest && AtlasCopyRequest->IsComplete() ) { //UE_LOG(LogCarla, Warning, TEXT("ACarlaGameModeBase::SendAtlas: DONE")); #if !UE_BUILD_SHIPPING diff --git a/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Game/CarlaGameModeBase.h b/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Game/CarlaGameModeBase.h index b608a95db..6501d6481 100644 --- a/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Game/CarlaGameModeBase.h +++ b/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Game/CarlaGameModeBase.h @@ -100,10 +100,6 @@ public: return IsAtlasTextureValid; } - const TArray& GetCurrentAtlasPixels() const{ - return AtlasPixels[CurrentAtlas]; - } - FTexture2DRHIRef GetCurrentCamerasAtlasTexture() const{ return CamerasAtlasTexture[CurrentAtlas]; } @@ -229,7 +225,6 @@ private: TQueue AtlasCopyRequestsQueuePool; static const uint32 kMaxNumTextures = 2u; // This has to be POT - TArray AtlasPixels[kMaxNumTextures]; // TODO: remove TArray SceneCaptureSensors; FTexture2DRHIRef CamerasAtlasTexture[kMaxNumTextures]; uint32 AtlasTextureWidth = 0u; diff --git a/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Sensor/SceneCaptureSensor.cpp b/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Sensor/SceneCaptureSensor.cpp index c2b8ba928..c3008831f 100644 --- a/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Sensor/SceneCaptureSensor.cpp +++ b/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Sensor/SceneCaptureSensor.cpp @@ -484,7 +484,7 @@ void ASceneCaptureSensor::BeginPlay() Super::BeginPlay(); - CopyTextureDelegate = FCoreDelegates::OnEndFrameRT.AddUObject(this, &ASceneCaptureCamera::CopyTextureToAtlas); + //CopyTextureDelegate = FCoreDelegates::OnEndFrame.AddUObject(this, &ASceneCaptureCamera::CopyTextureToAtlas); ACarlaGameModeBase* GameMode = Cast(GetWorld()->GetAuthGameMode()); GameMode->AddSceneCaptureSensor(this); @@ -513,24 +513,11 @@ void ASceneCaptureSensor::EndPlay(const EEndPlayReason::Type EndPlayReason) Super::EndPlay(EndPlayReason); SCENE_CAPTURE_COUNTER = 0u; - FCoreDelegates::OnEndFrameRT.Remove(CopyTextureDelegate); + FCoreDelegates::OnEndFrame.Remove(CopyTextureDelegate); ACarlaGameModeBase* GameMode = Cast(GetWorld()->GetAuthGameMode()); GameMode->RemoveSceneCaptureSensor(this); } -void ASceneCaptureSensor::CopyTextureFromAtlas(const TArray& AtlasImage) -{ - - if(AtlasImage.Num() > 0 && ImageToSend.Num() > 0) - { - SCOPE_CYCLE_COUNTER(STAT_CarlaSensorBufferCopy); - - const FColor* Source = &AtlasImage[0] + PositionInAtlas.Y; - - ImageToSend = TArray(Source, ImageWidth * ImageHeight); - } -} - void ASceneCaptureSensor::CopyTextureToAtlas() { @@ -570,20 +557,24 @@ void ASceneCaptureSensor::CopyTextureToAtlas() CopyInfo.DestPosition = This->PositionInAtlas; // Where to copy the texture RHICmdList.CopyTexture(Texture, AtlasTexture, CopyInfo); - - /* - ASceneCaptureSensor& CameraRef = *Camera; - FPixelReader::WritePixelsToArray( - *(CameraRef.CaptureRenderTarget), - CameraRef.Pixels[CameraRef.PreviousTexture], - RHICmdList); - */ - } } ); } +void ASceneCaptureSensor::CopyTextureFromAtlas(const TArray& AtlasImage) +{ + + if(AtlasImage.Num() > 0 && ImageToSend.Num() > 0) + { + SCOPE_CYCLE_COUNTER(STAT_CarlaSensorBufferCopy); + + const FColor* Source = &AtlasImage[0] + PositionInAtlas.Y; + + ImageToSend = TArray(Source, ImageWidth * ImageHeight); + } +} + // ============================================================================= // -- Local static functions implementations ----------------------------------- // ============================================================================= diff --git a/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Sensor/SceneCaptureSensor.h b/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Sensor/SceneCaptureSensor.h index ebfed6a65..eb4721a4a 100644 --- a/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Sensor/SceneCaptureSensor.h +++ b/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Sensor/SceneCaptureSensor.h @@ -270,6 +270,8 @@ public: FPixelReader::SavePixelsToDisk(*CaptureRenderTarget, FilePath); } + void CopyTextureToAtlas(); + void CopyTextureFromAtlas(const TArray& AtlasImage); template @@ -297,8 +299,6 @@ protected: virtual void SetUpSceneCaptureComponent(USceneCaptureComponent2D &SceneCapture) {} - void CopyTextureToAtlas(); - FDelegateHandle CopyTextureDelegate; TArray ImageToSend;