Enqueue order grouped on the GM

This commit is contained in:
doterop 2020-07-28 18:12:32 +02:00 committed by bernat
parent d7cb538b60
commit dc9a3fd1aa
4 changed files with 32 additions and 41 deletions

View File

@ -196,7 +196,7 @@ void ACarlaGameModeBase::BeginPlay()
// OnBeginFrame // OnBeginFrame
// OnEndFrame // OnEndFrame
CaptureAtlasDelegate = FCoreDelegates::OnEndFrame.AddUObject(this, &ACarlaGameModeBase::CaptureAtlas); 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; if(!SceneCaptureSensors.Num()) return;
// Be sure that the atlas texture is ready
if(!IsAtlasTextureValid) if(!IsAtlasTextureValid)
{ {
CreateAtlasTextures(); CreateAtlasTextures();
} }
// Enqueue the commands to copy the captures to the atlas
for(ASceneCaptureSensor* Sensor : SceneCaptureSensors)
{
Sensor->CopyTextureToAtlas();
}
//if (!AtlasCopyRequestQueue.IsEmpty()) //if (!AtlasCopyRequestQueue.IsEmpty())
//{ //{
// UE_LOG(LogCarla, Error, TEXT("ACarlaGameModeBase::CaptureAtlas: request full")); // UE_LOG(LogCarla, Error, TEXT("ACarlaGameModeBase::CaptureAtlas: request full"));
@ -448,12 +455,12 @@ void ACarlaGameModeBase::CaptureAtlas()
FAtlasCopyRequest* AtlasCopyRequest = GetAtlasCopyRequest(); FAtlasCopyRequest* AtlasCopyRequest = GetAtlasCopyRequest();
AtlasCopyRequest->ResizeBuffer(AtlasTextureWidth, AtlasTextureHeight); AtlasCopyRequest->ResizeBuffer(AtlasTextureWidth, AtlasTextureHeight);
// Download Atlas texture
ENQUEUE_RENDER_COMMAND(ACarlaGameModeBase_CaptureAtlas) ENQUEUE_RENDER_COMMAND(ACarlaGameModeBase_CaptureAtlas)
( (
[This, &CurrentAtlasPixels = AtlasCopyRequest->AtlasImage](FRHICommandListImmediate& RHICmdList) mutable [This, &CurrentAtlasPixels = AtlasCopyRequest->AtlasImage](FRHICommandListImmediate& RHICmdList) mutable
{ {
FTexture2DRHIRef AtlasTexture = This->CamerasAtlasTexture[This->PreviousAtlas]; FTexture2DRHIRef AtlasTexture = This->CamerasAtlasTexture[This->PreviousAtlas];
//TArray<FColor>& CurrentAtlasPixels = This->AtlasPixels[This->CurrentAtlas];
if (!AtlasTexture) if (!AtlasTexture)
{ {
@ -461,8 +468,6 @@ void ACarlaGameModeBase::CaptureAtlas()
return; return;
} }
// Download Atlas texture
FIntRect Rect = FIntRect(0, 0, This->AtlasTextureWidth, This->AtlasTextureHeight); FIntRect Rect = FIntRect(0, 0, This->AtlasTextureWidth, This->AtlasTextureHeight);
#if !UE_BUILD_SHIPPING #if !UE_BUILD_SHIPPING
@ -470,7 +475,6 @@ void ACarlaGameModeBase::CaptureAtlas()
Rect = FIntRect(0, 0, This->SurfaceW, This->SurfaceH); Rect = FIntRect(0, 0, This->SurfaceW, This->SurfaceH);
} }
#endif #endif
// GDynamicRHI->RHIReadSurfaceData(AtlasTexture, Rect, CurrentAtlasPixels, FReadSurfaceDataFlags(RCM_UNorm, CubeFace_MAX)); // GDynamicRHI->RHIReadSurfaceData(AtlasTexture, Rect, CurrentAtlasPixels, FReadSurfaceDataFlags(RCM_UNorm, CubeFace_MAX));
#if !UE_BUILD_SHIPPING #if !UE_BUILD_SHIPPING
@ -484,16 +488,17 @@ void ACarlaGameModeBase::CaptureAtlas()
CurrentAtlasPixels, CurrentAtlasPixels,
FReadSurfaceDataFlags(RCM_UNorm, CubeFace_MAX)); FReadSurfaceDataFlags(RCM_UNorm, CubeFace_MAX));
// This->PreviousAtlas = This->CurrentAtlas;
This->PreviousAtlas = This->CurrentAtlas; // This->CurrentAtlas = (This->CurrentAtlas + 1) & ~kMaxNumTextures;
This->CurrentAtlas = (This->CurrentAtlas + 1) & ~kMaxNumTextures;
} }
); );
// AtlasCopyRequest->Start(); AtlasCopyRequest->Start();
// AtlasCopyRequest->Wait(); // AtlasCopyRequest->Wait();
AtlasCopyRequestQueue.Enqueue(AtlasCopyRequest); AtlasCopyRequestQueue.Enqueue(AtlasCopyRequest);
SendAtlas();
} }
void ACarlaGameModeBase::SendAtlas() void ACarlaGameModeBase::SendAtlas()
@ -505,7 +510,7 @@ void ACarlaGameModeBase::SendAtlas()
// Be sure that the request has finished // Be sure that the request has finished
if(AtlasCopyRequest /* && AtlasCopyRequest->IsComplete() */ ) if( AtlasCopyRequest && AtlasCopyRequest->IsComplete() )
{ {
//UE_LOG(LogCarla, Warning, TEXT("ACarlaGameModeBase::SendAtlas: DONE")); //UE_LOG(LogCarla, Warning, TEXT("ACarlaGameModeBase::SendAtlas: DONE"));
#if !UE_BUILD_SHIPPING #if !UE_BUILD_SHIPPING

View File

@ -100,10 +100,6 @@ public:
return IsAtlasTextureValid; return IsAtlasTextureValid;
} }
const TArray<FColor>& GetCurrentAtlasPixels() const{
return AtlasPixels[CurrentAtlas];
}
FTexture2DRHIRef GetCurrentCamerasAtlasTexture() const{ FTexture2DRHIRef GetCurrentCamerasAtlasTexture() const{
return CamerasAtlasTexture[CurrentAtlas]; return CamerasAtlasTexture[CurrentAtlas];
} }
@ -229,7 +225,6 @@ private:
TQueue<FAtlasCopyRequest*> AtlasCopyRequestsQueuePool; TQueue<FAtlasCopyRequest*> AtlasCopyRequestsQueuePool;
static const uint32 kMaxNumTextures = 2u; // This has to be POT static const uint32 kMaxNumTextures = 2u; // This has to be POT
TArray<FColor> AtlasPixels[kMaxNumTextures]; // TODO: remove
TArray<ASceneCaptureSensor*> SceneCaptureSensors; TArray<ASceneCaptureSensor*> SceneCaptureSensors;
FTexture2DRHIRef CamerasAtlasTexture[kMaxNumTextures]; FTexture2DRHIRef CamerasAtlasTexture[kMaxNumTextures];
uint32 AtlasTextureWidth = 0u; uint32 AtlasTextureWidth = 0u;

View File

@ -484,7 +484,7 @@ void ASceneCaptureSensor::BeginPlay()
Super::BeginPlay(); Super::BeginPlay();
CopyTextureDelegate = FCoreDelegates::OnEndFrameRT.AddUObject(this, &ASceneCaptureCamera::CopyTextureToAtlas); //CopyTextureDelegate = FCoreDelegates::OnEndFrame.AddUObject(this, &ASceneCaptureCamera::CopyTextureToAtlas);
ACarlaGameModeBase* GameMode = Cast<ACarlaGameModeBase>(GetWorld()->GetAuthGameMode()); ACarlaGameModeBase* GameMode = Cast<ACarlaGameModeBase>(GetWorld()->GetAuthGameMode());
GameMode->AddSceneCaptureSensor(this); GameMode->AddSceneCaptureSensor(this);
@ -513,24 +513,11 @@ void ASceneCaptureSensor::EndPlay(const EEndPlayReason::Type EndPlayReason)
Super::EndPlay(EndPlayReason); Super::EndPlay(EndPlayReason);
SCENE_CAPTURE_COUNTER = 0u; SCENE_CAPTURE_COUNTER = 0u;
FCoreDelegates::OnEndFrameRT.Remove(CopyTextureDelegate); FCoreDelegates::OnEndFrame.Remove(CopyTextureDelegate);
ACarlaGameModeBase* GameMode = Cast<ACarlaGameModeBase>(GetWorld()->GetAuthGameMode()); ACarlaGameModeBase* GameMode = Cast<ACarlaGameModeBase>(GetWorld()->GetAuthGameMode());
GameMode->RemoveSceneCaptureSensor(this); GameMode->RemoveSceneCaptureSensor(this);
} }
void ASceneCaptureSensor::CopyTextureFromAtlas(const TArray<FColor>& AtlasImage)
{
if(AtlasImage.Num() > 0 && ImageToSend.Num() > 0)
{
SCOPE_CYCLE_COUNTER(STAT_CarlaSensorBufferCopy);
const FColor* Source = &AtlasImage[0] + PositionInAtlas.Y;
ImageToSend = TArray<FColor>(Source, ImageWidth * ImageHeight);
}
}
void ASceneCaptureSensor::CopyTextureToAtlas() void ASceneCaptureSensor::CopyTextureToAtlas()
{ {
@ -570,20 +557,24 @@ void ASceneCaptureSensor::CopyTextureToAtlas()
CopyInfo.DestPosition = This->PositionInAtlas; // Where to copy the texture CopyInfo.DestPosition = This->PositionInAtlas; // Where to copy the texture
RHICmdList.CopyTexture(Texture, AtlasTexture, CopyInfo); RHICmdList.CopyTexture(Texture, AtlasTexture, CopyInfo);
/*
ASceneCaptureSensor& CameraRef = *Camera;
FPixelReader::WritePixelsToArray(
*(CameraRef.CaptureRenderTarget),
CameraRef.Pixels[CameraRef.PreviousTexture],
RHICmdList);
*/
} }
} }
); );
} }
void ASceneCaptureSensor::CopyTextureFromAtlas(const TArray<FColor>& AtlasImage)
{
if(AtlasImage.Num() > 0 && ImageToSend.Num() > 0)
{
SCOPE_CYCLE_COUNTER(STAT_CarlaSensorBufferCopy);
const FColor* Source = &AtlasImage[0] + PositionInAtlas.Y;
ImageToSend = TArray<FColor>(Source, ImageWidth * ImageHeight);
}
}
// ============================================================================= // =============================================================================
// -- Local static functions implementations ----------------------------------- // -- Local static functions implementations -----------------------------------
// ============================================================================= // =============================================================================

View File

@ -270,6 +270,8 @@ public:
FPixelReader::SavePixelsToDisk(*CaptureRenderTarget, FilePath); FPixelReader::SavePixelsToDisk(*CaptureRenderTarget, FilePath);
} }
void CopyTextureToAtlas();
void CopyTextureFromAtlas(const TArray<FColor>& AtlasImage); void CopyTextureFromAtlas(const TArray<FColor>& AtlasImage);
template <typename TSensor> template <typename TSensor>
@ -297,8 +299,6 @@ protected:
virtual void SetUpSceneCaptureComponent(USceneCaptureComponent2D &SceneCapture) {} virtual void SetUpSceneCaptureComponent(USceneCaptureComponent2D &SceneCapture) {}
void CopyTextureToAtlas();
FDelegateHandle CopyTextureDelegate; FDelegateHandle CopyTextureDelegate;
TArray<FColor> ImageToSend; TArray<FColor> ImageToSend;