More checks, and removing the yield loop

This commit is contained in:
bernatx 2023-06-07 15:31:15 +02:00 committed by bernat
parent 27a06da659
commit 4eb8b614cd
10 changed files with 71 additions and 49 deletions

View File

@ -156,9 +156,12 @@ void ADVSCamera::PostPhysTick(UWorld *World, ELevelTick TickType, float DeltaTim
{
TRACE_CPUPROFILER_EVENT_SCOPE_STR("ADVSCamera Stream Send");
/** Send the events **/
auto Stream = GetDataStream(*this);
auto Buffer = Stream.PopBufferFromPool();
Stream.Send(*this, events, std::move(Buffer));
if (IsStreamReady())
{
auto Stream = GetDataStream(*this);
auto Buffer = Stream.PopBufferFromPool();
Stream.Send(*this, events, std::move(Buffer));
}
}
}

View File

@ -58,8 +58,11 @@ void AGnssSensor::PostPhysTick(UWorld *World, ELevelTick TickType, float DeltaSe
{
TRACE_CPUPROFILER_EVENT_SCOPE_STR("AGnssSensor Stream Send");
auto Stream = GetDataStream(*this);
Stream.Send(*this, carla::geom::GeoLocation{Latitude, Longitude, Altitude});
if (IsStreamReady())
{
auto Stream = GetDataStream(*this);
Stream.Send(*this, carla::geom::GeoLocation{Latitude, Longitude, Altitude});
}
}
}

View File

@ -184,12 +184,15 @@ float AInertialMeasurementUnit::ComputeCompass()
void AInertialMeasurementUnit::PostPhysTick(UWorld *World, ELevelTick TickType, float DeltaTime)
{
TRACE_CPUPROFILER_EVENT_SCOPE(AInertialMeasurementUnit::PostPhysTick);
auto Stream = GetDataStream(*this);
Stream.Send(
*this,
ComputeAccelerometer(DeltaTime),
ComputeGyroscope(),
ComputeCompass());
if (IsStreamReady())
{
auto Stream = GetDataStream(*this);
Stream.Send(
*this,
ComputeAccelerometer(DeltaTime),
ComputeGyroscope(),
ComputeCompass());
}
}
void AInertialMeasurementUnit::SetAccelerationStandardDeviation(const FVector &Vec)

View File

@ -132,7 +132,7 @@ void AObstacleDetectionSensor::OnObstacleDetectionEvent(
float HitDistance,
const FHitResult &Hit)
{
if ((Actor != nullptr) && (OtherActor != nullptr))
if ((Actor != nullptr) && (OtherActor != nullptr) && IsStreamReady())
{
const auto &Episode = GetEpisode();
GetDataStream(*this).Send(*this,

View File

@ -113,7 +113,7 @@ void FPixelReader::SendPixelsInRenderThread(TSensor &Sensor, bool use16BitFormat
TRACE_CPUPROFILER_EVENT_SCOPE_STR("FWritePixels_SendPixelsInRenderThread");
/// @todo Can we make sure the sensor is not going to be destroyed?
if (!Sensor.IsPendingKill())
if (!Sensor.IsPendingKill() && Sensor.IsStreamReady())
{
FPixelReader::Payload FuncForSending =
[&Sensor, Frame = FCarlaEngine::GetFrameCounter(), Conversor = std::move(Conversor)](void *LockedData, uint32 Size, uint32 Offset, uint32 ExpectedRowBytes)
@ -131,6 +131,8 @@ void FPixelReader::SendPixelsInRenderThread(TSensor &Sensor, bool use16BitFormat
Size = Converted.Num() * Converted.GetTypeSize();
}
if (!Sensor.IsStreamReady())
return;
auto Stream = Sensor.GetDataStream(Sensor);
Stream.SetFrameNumber(Frame);
auto Buffer = Stream.PopBufferFromPool();

View File

@ -76,8 +76,11 @@ void ARadar::PostPhysTick(UWorld *World, ELevelTick TickType, float DeltaTime)
{
TRACE_CPUPROFILER_EVENT_SCOPE_STR("Send Stream");
auto DataStream = GetDataStream(*this);
DataStream.Send(*this, RadarData, DataStream.PopBufferFromPool());
if (IsStreamReady())
{
auto DataStream = GetDataStream(*this);
DataStream.Send(*this, RadarData, DataStream.PopBufferFromPool());
}
}
}

View File

@ -61,8 +61,11 @@ void ARayCastLidar::PostPhysTick(UWorld *World, ELevelTick TickType, float Delta
{
TRACE_CPUPROFILER_EVENT_SCOPE_STR("Send Stream");
auto DataStream = GetDataStream(*this);
DataStream.Send(*this, LidarData, DataStream.PopBufferFromPool());
if (IsStreamReady())
{
auto DataStream = GetDataStream(*this);
DataStream.Send(*this, LidarData, DataStream.PopBufferFromPool());
}
}
}

View File

@ -71,8 +71,11 @@ void ARayCastSemanticLidar::PostPhysTick(UWorld *World, ELevelTick TickType, flo
{
TRACE_CPUPROFILER_EVENT_SCOPE_STR("Send Stream");
auto DataStream = GetDataStream(*this);
DataStream.Send(*this, SemanticLidarData, DataStream.PopBufferFromPool());
if (IsStreamReady())
{
auto DataStream = GetDataStream(*this);
DataStream.Send(*this, SemanticLidarData, DataStream.PopBufferFromPool());
}
}
}

View File

@ -53,6 +53,13 @@ struct FCameraGBufferUint8
{
return {};
}
/// Return if the stream is ready or not
bool IsStreamReady()
{
return Stream.IsStreamReady();
}
/// Return the FDataStream associated with this sensor.
///
/// You need to provide a reference to self, this is necessary for template
@ -60,11 +67,6 @@ struct FCameraGBufferUint8
template <typename SensorT>
FAsyncDataStream GetDataStream(const SensorT &Self)
{
while (!Stream.IsStreamReady())
{
std::this_thread::yield();
}
return Stream.MakeAsyncDataStream(Self, Self.GetEpisode().GetElapsedGameTime());
}
@ -100,6 +102,13 @@ struct FCameraGBufferFloat
{
return {};
}
/// Return if the stream is ready or not
bool IsStreamReady()
{
return Stream.IsStreamReady();
}
/// Return the FDataStream associated with this sensor.
///
/// You need to provide a reference to self, this is necessary for template
@ -107,11 +116,6 @@ struct FCameraGBufferFloat
template <typename SensorT>
FAsyncDataStream GetDataStream(const SensorT &Self)
{
while (!Stream.IsStreamReady())
{
std::this_thread::yield();
}
return Stream.MakeAsyncDataStream(Self, Self.GetEpisode().GetElapsedGameTime());
}
@ -521,22 +525,25 @@ private:
for (auto& Pixel : Pixels)
Pixel = PixelType::Black;
}
auto GBufferStream = CameraGBuffer.GetDataStream(Self);
auto Buffer = GBufferStream.PopBufferFromPool();
Buffer.copy_from(
carla::sensor::SensorRegistry::get<CameraGBufferT*>::type::header_offset,
Pixels);
if (Buffer.empty()) {
return;
if (CameraGBuffer.IsStreamReady())
{
auto GBufferStream = CameraGBuffer.GetDataStream(Self);
auto Buffer = GBufferStream.PopBufferFromPool();
Buffer.copy_from(
carla::sensor::SensorRegistry::get<CameraGBufferT*>::type::header_offset,
Pixels);
if (Buffer.empty()) {
return;
}
SCOPE_CYCLE_COUNTER(STAT_CarlaSensorStreamSend);
TRACE_CPUPROFILER_EVENT_SCOPE_STR("Stream Send");
GBufferStream.Send(
CameraGBuffer,
std::move(Buffer),
ViewSize.X,
ViewSize.Y,
Self.GetFOVAngle());
}
SCOPE_CYCLE_COUNTER(STAT_CarlaSensorStreamSend);
TRACE_CPUPROFILER_EVENT_SCOPE_STR("Stream Send");
GBufferStream.Send(
CameraGBuffer,
std::move(Buffer),
ViewSize.X,
ViewSize.Y,
Self.GetFOVAngle());
}
protected:

View File

@ -107,11 +107,6 @@ protected:
template <typename SensorT>
FAsyncDataStream GetDataStream(const SensorT &Self)
{
while (!Stream.IsStreamReady())
{
std::this_thread::yield();
}
return Stream.MakeAsyncDataStream(Self, GetEpisode().GetElapsedGameTime());
}