Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -142,28 +142,28 @@ void UFlowCounterSpawnerComponent::TickComponent(float DeltaTime, ELevelTick Tic

if (!bSpawning || PendingDoorMeshes.Num() == 0)
return;

// Check frame time throttle to avoid spawning when the game is struggling
float FrameTime = DeltaTime * 1000.0f; // ms
if (FrameTime > 25.0f)
{
// Skip this batch - system under load
LogToCustomLogger(FString::Printf(TEXT("TickComponent skipped spawning due to frame time %.2fms; pending doors: %d"),
FrameTime, PendingDoorMeshes.Num()));
return;
}

// Check TIME throttle (not frame throttle)
// Pace by wall-clock time only, never by frame time. This used to skip every frame slower than 25 ms,
// but ARuntimeMeshBuilder::DecideAndExecuteSpawnStrategy picks this batched path precisely when FPS is
// at or below 40, so on a slow (or thermally throttled) machine the queue could stop draining for good
// and leave the loading card up. A slow frame now only shortens the batch, via BatchTimeBudgetMs.
const double Now = FPlatformTime::Seconds();
if (Now - LastSpawnTime < SpawnInterval)
return;

LastSpawnTime = Now;

int32 SpawnedThisBatch = 0;
const double BatchDeadline = Now + BatchTimeBudgetMs / 1000.0;

while (PendingDoorMeshes.Num() > 0 && SpawnedThisBatch < MaxPerBatch)
{
// At least one per batch, however long it takes, so progress is guaranteed at any frame rate.
if (SpawnedThisBatch > 0 && FPlatformTime::Seconds() > BatchDeadline)
{
break;
}

const FPendingDoorEntry PendingDoor = PendingDoorMeshes[0];
PendingDoorMeshes.RemoveAtSwap(0);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,33 @@
#include "RHI.h"
#include "Subsystems/MobiusCustomLoggerSubsystem.h"
#include "Engine/Engine.h"
#include "Engine/GameViewportClient.h"
#include "Engine/World.h"
#include "HAL/IConsoleManager.h"
#include "UserConfig/UserProjectSettings.h"
#include "Widgets/SWindow.h"

static TAutoConsoleVariable<float> CVarMobiusMinimisedMaxFPS(
TEXT("Mobius.Perf.MinimisedMaxFPS"),
5.0f,
TEXT("Frame-rate cap while the packaged / -game window is minimised; t.MaxFPS is restored when it is restored.\n")
TEXT("0 disables the cap. The editor and PIE are never capped."),
ECVF_Default);

namespace
{
bool IsGameWindowMinimised()
{
if (GEngine && GEngine->GameViewport)
{
if (const TSharedPtr<SWindow> Window = GEngine->GameViewport->GetWindow())
{
return Window->IsWindowMinimized();
}
}
return false;
}
}

// Heavy render-feature cvar tables per render tier (task C1). Each entry documents what it gates and
// the per-tier value. Returned as "name=value" strings for ApplyConsoleCommands (-> ECVF_SetByCode).
Expand Down Expand Up @@ -135,6 +160,13 @@ void UPerformanceUtilSubsystem::Deinitialize()
CurrentRenderTier = ERpt_High;
}

// t.MaxFPS is process-wide, so never leave the minimised cap behind on a world teardown.
if (bMinimisedFrameCapActive && GEngine)
{
GEngine->SetMaxFPS(FPSBeforeMinimise);
bMinimisedFrameCapActive = false;
}

// Clean up any resources or delegates here if needed
if (ProjectSettings)
{
Expand All @@ -159,11 +191,48 @@ void UPerformanceUtilSubsystem::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);

UpdateMinimisedFrameCap();

// Calculate the current FPS based on DeltaTime
CalculateCurrentFPS(DeltaTime);
CheckCurrentFPS();
}

void UPerformanceUtilSubsystem::UpdateMinimisedFrameCap()
{
// Packaged and -game only. The editor deliberately keeps full rate when it is not in front
// (Config/DefaultEditorSettings.ini), and PIE shares the editor process.
if (GIsEditor || !GEngine)
{
return;
}

const float CapFPS = CVarMobiusMinimisedMaxFPS.GetValueOnGameThread();
const bool bWantCap = CapFPS > 0.0f && IsGameWindowMinimised();
if (bWantCap == bMinimisedFrameCapActive)
{
return;
}

if (bWantCap)
{
FPSBeforeMinimise = GEngine->GetMaxFPS();
GEngine->SetMaxFPS(CapFPS);
}
else
{
GEngine->SetMaxFPS(FPSBeforeMinimise);
}
bMinimisedFrameCapActive = bWantCap;

if (UMobiusCustomLoggerSubsystem* StartupLogger = GEngine->GetEngineSubsystem<UMobiusCustomLoggerSubsystem>())
{
StartupLogger->EnqueueLogMessage(bWantCap
? FString::Printf(TEXT("Window minimised: frame rate capped at %.0f FPS (was %.0f)"), CapFPS, FPSBeforeMinimise)
: FString::Printf(TEXT("Window restored: frame-rate limit back to %.0f FPS"), FPSBeforeMinimise));
}
}

static bool ParseCVarString(const FString& InString, FString& OutName, FString& OutValue)
{
// Split only on the first �=�
Expand Down Expand Up @@ -228,7 +297,13 @@ void UPerformanceUtilSubsystem::CalculateCurrentFPS(float DeltaTime)
if (DeltaTime > 0.0f)
{
CurrentFPS = 1.0f / DeltaTime;
SmoothedFPS = FMath::Lerp(SmoothedFPS, CurrentFPS, 0.1f);

// Minimised frames run under the minimised frame cap and say nothing about real performance; keep
// them out of the average so restoring the window does not read as a sudden low-FPS spell.
if (!IsGameWindowMinimised())
{
SmoothedFPS = FMath::Lerp(SmoothedFPS, CurrentFPS, 0.1f);
}
}
}

Expand Down Expand Up @@ -615,6 +690,10 @@ void UPerformanceUtilSubsystem::ApplyOptimizationsBasedOnFPS()
// set the avatar model type to low spec
CurrentAvatarModelType = EPss_Low;

// Latch until FPS recovers (see CheckCurrentFPS). The user can still switch the avatar model back
// by hand (UAdjustAvatarModel), and this will not immediately undo that.
bLowFPSOptimisationApplied = true;

// Notify listeners that auto scalability has changed
OnAutoScalabilityChanged.Broadcast();

Expand All @@ -628,9 +707,24 @@ void UPerformanceUtilSubsystem::ApplyOptimizationsBasedOnFPS()
//TODO: Our check for FPS is simplistic and only checks if the FPS is below a threshold. - we may want a range tolerance as well
void UPerformanceUtilSubsystem::CheckCurrentFPS()
{
if (CurrentFPS <= LowFPSThreshold)
// A minimised window renders nothing worth judging, and runs under the minimised frame cap, so it
// must neither arm the watchdog nor let an armed one fire.
if (IsGameWindowMinimised())
{
if (AutoScalabilityTimerHandle.IsValid())
{
GetWorld()->GetTimerManager().ClearTimer(AutoScalabilityTimerHandle);
AutoScalabilityTimerHandle.Invalidate();
}
return;
}

// Smoothed, not instantaneous: one long frame (a load hitch) is not a sustained drop.
if (SmoothedFPS <= LowFPSThreshold)
{
if (!AutoScalabilityTimerHandle.IsValid())
// Once per low-FPS episode. This used to re-arm straight after firing, so a machine that stayed
// slow re-applied the optimisation and logged two lines every 10 s indefinitely.
if (!bLowFPSOptimisationApplied && !AutoScalabilityTimerHandle.IsValid())
{
// Start the timer to trigger auto scalability adjustments
GetWorld()->GetTimerManager().SetTimer(AutoScalabilityTimerHandle, this,
Expand All @@ -642,10 +736,12 @@ void UPerformanceUtilSubsystem::CheckCurrentFPS()
}
}
}
else
else if (SmoothedFPS > LowFPSThreshold + 5.0f) // 5 FPS of hysteresis
{
// if we have hit the low FPS threshold and the timer is valid, we clear it, provided the FPS has improved by our tolerance
if (AutoScalabilityTimerHandle.IsValid() && CurrentFPS > LowFPSThreshold + 5.0f)// Adding a tolerance of 5 FPS
// Recovered: a later sustained drop may fire again.
bLowFPSOptimisationApplied = false;

if (AutoScalabilityTimerHandle.IsValid())
{
GetWorld()->GetTimerManager().ClearTimer(AutoScalabilityTimerHandle);
AutoScalabilityTimerHandle.Invalidate();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class MOBIUSCORE_API UFlowCounterSpawnerComponent : public UActorComponent
// called by owner when all doors discovered
void BeginSpawning();

// bypass frame-budget throttle — call after load spikes to finish remaining spawns immediately
// bypass the batch pacing — call after load spikes to finish remaining spawns immediately
void FlushRemainingSpawns();

// cancel in-progress spawning and discard all queued doors — call before a new load replaces the mesh
Expand All @@ -47,7 +47,14 @@ class MOBIUSCORE_API UFlowCounterSpawnerComponent : public UActorComponent

UPROPERTY(EditAnywhere)
float SpawnInterval = 0.25f;


/**
* Wall-clock budget for one batch, in milliseconds. A batch stops early once it has used this much,
* but always spawns at least one door, so the queue drains at any frame rate.
*/
UPROPERTY(EditAnywhere)
float BatchTimeBudgetMs = 4.0f;

UPROPERTY(EditAnywhere, BlueprintAssignable, Category = "MeshGenerator|Delegates")
FOnFlowCounterSpawned OnFlowCounterAutoSpawned;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,20 @@ class MOBIUSCORE_API UPerformanceUtilSubsystem : public UTickableWorldSubsystem
*/
void CheckCurrentFPS();

/**
* Packaged / -game only: cap the frame rate while the window is minimised (Mobius.Perf.MinimisedMaxFPS)
* and restore the previous t.MaxFPS when it is restored. Nothing on screen needs 120 FPS while
* minimised, and an hour of full-rate rendering behind a minimised window heats the machine for nothing.
*/
void UpdateMinimisedFrameCap();

/** Set once the low-FPS watchdog has fired; it stays quiet until FPS recovers instead of re-firing every 10 s. */
bool bLowFPSOptimisationApplied = false;

/** True while the minimised frame cap is in force; FPSBeforeMinimise is what t.MaxFPS returns to afterwards. */
bool bMinimisedFrameCapActive = false;
float FPSBeforeMinimise = 0.0f;

/**
* Called from the low-FPS watchdog (ApplyOptimizationsBasedOnFPS). Intentionally a no-op for the
* render tier: C1 is startup hardware-detect + the C2 override only, NOT FPS-driven runtime
Expand Down
Loading