Now ScreenCaptureCamera read pixels in render queue.
This commit is contained in:
parent
ae26d280b4
commit
dd7969e7d7
|
@ -23,6 +23,8 @@ public class Carla : ModuleRules
|
||||||
new string[]
|
new string[]
|
||||||
{
|
{
|
||||||
"Core",
|
"Core",
|
||||||
|
"RenderCore",
|
||||||
|
"RHI"
|
||||||
// ... add other public dependencies that you statically link with here ...
|
// ... add other public dependencies that you statically link with here ...
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
|
@ -291,21 +291,16 @@ void ACityMapGenerator::GenerateRoadMap()
|
||||||
// Do the ray tracing.
|
// Do the ray tracing.
|
||||||
FHitResult Hit;
|
FHitResult Hit;
|
||||||
if (LineTrace(World, Start, End, Hit)) {
|
if (LineTrace(World, Start, End, Hit)) {
|
||||||
auto InstancedStaticMeshComponent = Cast<UInstancedStaticMeshComponent>(Hit.Component.Get());
|
auto StaticMeshComponent = Cast<UStaticMeshComponent>(Hit.Component.Get());
|
||||||
if (InstancedStaticMeshComponent == nullptr) {
|
if (StaticMeshComponent == nullptr) {
|
||||||
UE_LOG(LogCarla, Error, TEXT("Road component is not UInstancedStaticMeshComponent"));
|
UE_LOG(LogCarla, Error, TEXT("Road component is not UInstancedStaticMeshComponent"));
|
||||||
} else {
|
} else {
|
||||||
FTransform InstanceTransform;
|
RoadMap->SetPixelAt(
|
||||||
if (!InstancedStaticMeshComponent->GetInstanceTransform(Hit.Item, InstanceTransform, true)) {
|
PixelX,
|
||||||
UE_LOG(LogCarla, Error, TEXT("Failed to get instance's transform"));
|
PixelY,
|
||||||
} else {
|
GetTag(*StaticMeshComponent->GetStaticMesh()),
|
||||||
RoadMap->SetPixelAt(
|
StaticMeshComponent->GetOwner()->GetTransform(),
|
||||||
PixelX,
|
bLeftHandTraffic);
|
||||||
PixelY,
|
|
||||||
GetTag(*InstancedStaticMeshComponent->GetStaticMesh()),
|
|
||||||
InstanceTransform,
|
|
||||||
bLeftHandTraffic);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -11,6 +11,8 @@
|
||||||
|
|
||||||
#include "Settings/CarlaSettings.h"
|
#include "Settings/CarlaSettings.h"
|
||||||
#include "CarlaServer.h"
|
#include "CarlaServer.h"
|
||||||
|
#include "RenderingThread.h"
|
||||||
|
|
||||||
|
|
||||||
using Errc = CarlaServer::ErrorCode;
|
using Errc = CarlaServer::ErrorCode;
|
||||||
|
|
||||||
|
@ -115,16 +117,27 @@ void CarlaGameController::Tick(float DeltaSeconds)
|
||||||
// Send measurements.
|
// Send measurements.
|
||||||
{
|
{
|
||||||
check(GameState != nullptr);
|
check(GameState != nullptr);
|
||||||
if (Errc::Error == Server->SendMeasurements(
|
ENQUEUE_UNIQUE_RENDER_COMMAND_FOURPARAMETER( /** @TODO: move to the screencapturecamera */
|
||||||
|
FSendMeasurements,
|
||||||
|
CarlaServer*,Server,Server.Get(), /** @TODO: move unique pointer lambda reference */
|
||||||
|
const ACarlaGameState*, GameState, GameState,
|
||||||
|
const ACarlaPlayerState&, PlayerState,Player->GetPlayerState(),
|
||||||
|
//,ParamType4,ParamName4,ParamValue4,Code
|
||||||
|
bool,bSendNonPlayerAgentsInfo,CarlaSettings->bSendNonPlayerAgentsInfo,
|
||||||
|
{
|
||||||
|
if (Errc::Error == Server->SendMeasurements(
|
||||||
*GameState,
|
*GameState,
|
||||||
Player->GetPlayerState(),
|
PlayerState,
|
||||||
CarlaSettings->bSendNonPlayerAgentsInfo)) {
|
bSendNonPlayerAgentsInfo))
|
||||||
Server = nullptr;
|
{
|
||||||
return;
|
Server = nullptr;
|
||||||
}
|
}
|
||||||
|
});
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Read control, block if the settings say so.
|
// Read control, block if the settings say so.
|
||||||
|
if(Server!=nullptr)
|
||||||
{
|
{
|
||||||
const bool bShouldBlock = CarlaSettings->bSynchronousMode;
|
const bool bShouldBlock = CarlaSettings->bSynchronousMode;
|
||||||
if (Errc::Error == Server->ReadControl(*Player, bShouldBlock)) {
|
if (Errc::Error == Server->ReadControl(*Player, bShouldBlock)) {
|
||||||
|
|
|
@ -14,6 +14,11 @@
|
||||||
#include "GameFramework/Pawn.h"
|
#include "GameFramework/Pawn.h"
|
||||||
#include "WheeledVehicle.h"
|
#include "WheeledVehicle.h"
|
||||||
#include "WheeledVehicleMovementComponent.h"
|
#include "WheeledVehicleMovementComponent.h"
|
||||||
|
#include "CarlaPlayerState.h"
|
||||||
|
#include "CarlaWheeledVehicle.h"
|
||||||
|
#include "CarlaHUD.h"
|
||||||
|
#include "MapGen/RoadMap.h"
|
||||||
|
#include "Settings/CameraPostProcessParameters.h"
|
||||||
|
|
||||||
// =============================================================================
|
// =============================================================================
|
||||||
// -- Constructor and destructor -----------------------------------------------
|
// -- Constructor and destructor -----------------------------------------------
|
||||||
|
@ -99,11 +104,18 @@ void ACarlaVehicleController::Tick(float DeltaTime)
|
||||||
IntersectPlayerWithRoadMap();
|
IntersectPlayerWithRoadMap();
|
||||||
const auto NumberOfCameras = SceneCaptureCameras.Num();
|
const auto NumberOfCameras = SceneCaptureCameras.Num();
|
||||||
check(NumberOfCameras == CarlaPlayerState->Images.Num());
|
check(NumberOfCameras == CarlaPlayerState->Images.Num());
|
||||||
for (auto i = 0; i < NumberOfCameras; ++i) {
|
|
||||||
auto &Image = CarlaPlayerState->Images[i];
|
for (auto i = 0; i < NumberOfCameras; ++i)
|
||||||
if (!SceneCaptureCameras[i]->ReadPixels(Image.BitMap)) {
|
{
|
||||||
Image.BitMap.Empty();
|
FCapturedImage *Image = &CarlaPlayerState->Images[i];
|
||||||
}
|
ASceneCaptureCamera *Camera = SceneCaptureCameras[i];
|
||||||
|
ENQUEUE_UNIQUE_RENDER_COMMAND_TWOPARAMETER(
|
||||||
|
FReadFromTexture,
|
||||||
|
FCapturedImage*, Image, Image,
|
||||||
|
ASceneCaptureCamera *, Camera, Camera,
|
||||||
|
{
|
||||||
|
Camera->WritePixels(Image->BitMap);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -9,6 +9,7 @@
|
||||||
#include "WheeledVehicleController.h"
|
#include "WheeledVehicleController.h"
|
||||||
#include "CarlaVehicleController.generated.h"
|
#include "CarlaVehicleController.generated.h"
|
||||||
|
|
||||||
|
struct FCameraPostProcessParameters;
|
||||||
class ACarlaHUD;
|
class ACarlaHUD;
|
||||||
class ACarlaPlayerState;
|
class ACarlaPlayerState;
|
||||||
class ASceneCaptureCamera;
|
class ASceneCaptureCamera;
|
||||||
|
|
|
@ -15,8 +15,7 @@
|
||||||
#include "HighResScreenshot.h"
|
#include "HighResScreenshot.h"
|
||||||
#include "Materials/Material.h"
|
#include "Materials/Material.h"
|
||||||
#include "Paths.h"
|
#include "Paths.h"
|
||||||
#include "StaticMeshResources.h"
|
|
||||||
#include "TextureResource.h"
|
|
||||||
|
|
||||||
|
|
||||||
static constexpr auto DEPTH_MAT_PATH =
|
static constexpr auto DEPTH_MAT_PATH =
|
||||||
|
@ -198,7 +197,7 @@ bool ASceneCaptureCamera::ReadPixels(TArray<FColor> &BitMap) const
|
||||||
UE_LOG(LogCarla, Error, TEXT("SceneCaptureCamera: Missing render target"));
|
UE_LOG(LogCarla, Error, TEXT("SceneCaptureCamera: Missing render target"));
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
FTextureRenderTargetResource* RTResource = CaptureRenderTarget->GameThread_GetRenderTargetResource();
|
FTextureRenderTargetResource* RTResource = CaptureRenderTarget->GetRenderTargetResource(); // CaptureRenderTarget->GameThread_GetRenderTargetResource();
|
||||||
if (RTResource == nullptr) {
|
if (RTResource == nullptr) {
|
||||||
UE_LOG(LogCarla, Error, TEXT("SceneCaptureCamera: Missing render target"));
|
UE_LOG(LogCarla, Error, TEXT("SceneCaptureCamera: Missing render target"));
|
||||||
return false;
|
return false;
|
||||||
|
@ -208,6 +207,74 @@ bool ASceneCaptureCamera::ReadPixels(TArray<FColor> &BitMap) const
|
||||||
return RTResource->ReadPixels(BitMap, ReadPixelFlags);
|
return RTResource->ReadPixels(BitMap, ReadPixelFlags);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void ASceneCaptureCamera::WritePixels(TArray<FColor>& RawData)
|
||||||
|
{
|
||||||
|
check(IsInRenderingThread());
|
||||||
|
if(!CaptureRenderTarget)
|
||||||
|
{
|
||||||
|
UE_LOG(LogCarla, Error, TEXT("SceneCaptureCamera: Missing render target"));
|
||||||
|
return ;
|
||||||
|
}
|
||||||
|
FRHITexture2D *texture = CaptureRenderTarget->GetRenderTargetResource()->GetRenderTargetTexture();
|
||||||
|
|
||||||
|
uint32 width = texture->GetSizeX();
|
||||||
|
uint32 height = texture->GetSizeY();
|
||||||
|
uint32 stride;
|
||||||
|
uint8 *src = reinterpret_cast<uint8*>(RHILockTexture2D(texture, 0, RLM_ReadOnly, stride, false));
|
||||||
|
const uint32 bufferSize = stride * height;
|
||||||
|
//Alternative Method 0: deeplearning
|
||||||
|
/*void *buffer = FMemory::Malloc(bufferSize, 4);
|
||||||
|
if(!buffer)
|
||||||
|
{
|
||||||
|
UE_LOG(LogCarla, Display, TEXT("Error allocating memory"));
|
||||||
|
RHIUnlockTexture2D(texture, 0, false);
|
||||||
|
return ;
|
||||||
|
}
|
||||||
|
//FMemory::BigBlockMemcpy(buffer, src, bufferSize);*/
|
||||||
|
RawData.Empty(width*height);
|
||||||
|
for(unsigned y = 0; y < height; ++y)
|
||||||
|
{
|
||||||
|
for(unsigned x = 0; x < width; ++x)
|
||||||
|
{
|
||||||
|
//RGBA ->
|
||||||
|
RawData.Add(FColor(src[2],src[1],src[0],src[3]));
|
||||||
|
src += 4;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
RHIUnlockTexture2D(texture, 0, false);
|
||||||
|
//Alternative Method 1
|
||||||
|
/*FColor* RHIData = reinterpret_cast<FColor*>(RHILockTexture2D(TextureRHI, 0, RLM_ReadOnly, stride, false, false));
|
||||||
|
if(!RHIData) {
|
||||||
|
UE_LOG(LogCarla,Display,TEXT("ASceneCaptureCamera::WritePixels : RHIData is null"));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
RawData.Append(RHIData,Width*Height);*/
|
||||||
|
//Alternative Method 2
|
||||||
|
/*uint8* OriginBuffer = reinterpret_cast<uint8*>(RHILockTexture2D(TextureRHI, 0, RLM_ReadOnly, stride, false,false));
|
||||||
|
if(OriginBuffer)
|
||||||
|
{
|
||||||
|
for (int32 y = 0; y < Height; y++)
|
||||||
|
{
|
||||||
|
uint8* OriginPtr = &OriginBuffer[(Height - 1 - y) * stride];
|
||||||
|
for (int32 x = 0; x < Width; x++)
|
||||||
|
{
|
||||||
|
RawData[(Height - 1 - y) * Width] = FColor(*OriginPtr++,*OriginPtr++,*OriginPtr++,*OriginPtr++);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
//Alternative Method 3
|
||||||
|
/*
|
||||||
|
FRHITexture2D* Texture2D = TextureRHI->GetTexture2D();
|
||||||
|
uint8* TextureBuffer = (uint8*)RHILockTexture2D(Texture2D, 0, RLM_ReadOnly, stride, false, false);
|
||||||
|
FMemory::Memcpy( RawData.GetData(), TextureBuffer, stride + (Width*Height*4) );
|
||||||
|
*/
|
||||||
|
//End of Alternative methods lock
|
||||||
|
//RHIUnlockTexture2D(TextureRHI, 0, false, false);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
void ASceneCaptureCamera::UpdateDrawFrustum()
|
void ASceneCaptureCamera::UpdateDrawFrustum()
|
||||||
{
|
{
|
||||||
if(DrawFrustum && CaptureComponent2D)
|
if(DrawFrustum && CaptureComponent2D)
|
||||||
|
|
|
@ -65,6 +65,7 @@ public:
|
||||||
const FCameraPostProcessParameters &OverridePostProcessParameters);
|
const FCameraPostProcessParameters &OverridePostProcessParameters);
|
||||||
|
|
||||||
bool ReadPixels(TArray<FColor> &BitMap) const;
|
bool ReadPixels(TArray<FColor> &BitMap) const;
|
||||||
|
void WritePixels(TArray<FColor>& RawData);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue