Optimize arena rendering and simulation loop to 60 FPS
This commit is contained in:
parent
805c169794
commit
0f36eed3a0
12 changed files with 1810 additions and 231 deletions
201
client/Main.cs
201
client/Main.cs
|
|
@ -1,4 +1,6 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using Godot;
|
||||
using RustToDust.Core;
|
||||
using RustToDust.Ymir;
|
||||
|
|
@ -26,6 +28,29 @@ public partial class Main : Node3D
|
|||
private bool _interactQueued;
|
||||
private int _seed = 42;
|
||||
|
||||
// Temporary Step 1/Step 3 perf probe (enabled only when RTD_PERF_FRAMES is set)
|
||||
private int _perfTargetFrames;
|
||||
private int _perfFrameCount;
|
||||
private int _perfPhysicsTicks;
|
||||
private long _perfLastFrameTick;
|
||||
private int _perfLastDirtyRebakes;
|
||||
private readonly List<double> _perfFrameMs = new();
|
||||
private readonly List<double> _perfPhysicsStepMs = new();
|
||||
private readonly List<double> _perfTerrainSyncMs = new();
|
||||
private readonly List<double> _perfArenaSyncMs = new();
|
||||
private readonly List<double> _perfHudMs = new();
|
||||
private readonly List<int> _perfDirtyRebakeDelta = new();
|
||||
private readonly List<double> _perfDrawCalls = new();
|
||||
private readonly List<double> _perfPrimitives = new();
|
||||
private readonly List<double> _perfObjects = new();
|
||||
private readonly List<double> _perfNodeCount = new();
|
||||
private readonly List<double> _perfTimeProcess = new();
|
||||
private readonly List<double> _perfTimePhysicsProcess = new();
|
||||
private double _perfPendingStepMs;
|
||||
private double _perfPendingTerrainMs;
|
||||
private double _perfPendingArenaMs;
|
||||
private double _perfCamMsSum;
|
||||
|
||||
public ArenaSession Session => _session ?? throw new InvalidOperationException($"No active ArenaSession ({Failure ?? "uninitialized"})");
|
||||
public TerrainRenderer Terrain => _terrain;
|
||||
public ArenaRenderer Arena => _arena;
|
||||
|
|
@ -56,7 +81,15 @@ public partial class Main : Node3D
|
|||
_hud = new Hud(() => _session, OnEquipCargo, () => _cameraYaw) { Name = "Hud" };
|
||||
AddChild(_hud);
|
||||
|
||||
if (int.TryParse(System.Environment.GetEnvironmentVariable("RTD_PERF_FRAMES"), out int perfFrames) && perfFrames > 0)
|
||||
{
|
||||
_perfTargetFrames = perfFrames;
|
||||
ArenaSession.PerfEnabled = true;
|
||||
}
|
||||
|
||||
Reset(_seed);
|
||||
_perfLastFrameTick = Stopwatch.GetTimestamp();
|
||||
_perfLastDirtyRebakes = _terrain.DirtyRebakes;
|
||||
}
|
||||
|
||||
public override void _ExitTree()
|
||||
|
|
@ -87,7 +120,7 @@ public partial class Main : Node3D
|
|||
UpdateCamera(1f);
|
||||
_hud.Sync(_session);
|
||||
|
||||
if (DisplayServer.GetName() != "headless" && !_hud.GarageExpanded)
|
||||
if (DisplayServer.GetName() != "headless" && !_hud.GarageExpanded && _perfTargetFrames == 0)
|
||||
{
|
||||
Input.MouseMode = Input.MouseModeEnum.Captured;
|
||||
}
|
||||
|
|
@ -185,9 +218,27 @@ public partial class Main : Node3D
|
|||
var input = _scriptedInput ?? GatherPlayerInput();
|
||||
_interactQueued = false;
|
||||
|
||||
_session.Step(input);
|
||||
_terrain.Sync(_session.State.Terrain);
|
||||
_arena.Sync(_session, _elapsedSeconds, (float)delta);
|
||||
if (_perfTargetFrames > 0)
|
||||
{
|
||||
long t0 = Stopwatch.GetTimestamp();
|
||||
_session.Step(input);
|
||||
long t1 = Stopwatch.GetTimestamp();
|
||||
_terrain.Sync(_session.State.Terrain);
|
||||
long t2 = Stopwatch.GetTimestamp();
|
||||
_arena.Sync(_session, _elapsedSeconds, (float)delta);
|
||||
long t3 = Stopwatch.GetTimestamp();
|
||||
double toMs = 1000.0 / Stopwatch.Frequency;
|
||||
_perfPendingStepMs += (t1 - t0) * toMs;
|
||||
_perfPendingTerrainMs += (t2 - t1) * toMs;
|
||||
_perfPendingArenaMs += (t3 - t2) * toMs;
|
||||
_perfPhysicsTicks++;
|
||||
}
|
||||
else
|
||||
{
|
||||
_session.Step(input);
|
||||
_terrain.Sync(_session.State.Terrain);
|
||||
_arena.Sync(_session, _elapsedSeconds, (float)delta);
|
||||
}
|
||||
}
|
||||
|
||||
public override void _Process(double delta)
|
||||
|
|
@ -197,8 +248,144 @@ public partial class Main : Node3D
|
|||
return;
|
||||
}
|
||||
|
||||
UpdateCamera((float)delta);
|
||||
_hud.Sync(_session);
|
||||
if (_perfTargetFrames > 0)
|
||||
{
|
||||
long tc0 = Stopwatch.GetTimestamp();
|
||||
UpdateCamera((float)delta);
|
||||
_terrain.UpdateTileLod(_camera.GlobalPosition);
|
||||
long th0 = Stopwatch.GetTimestamp();
|
||||
_hud.Sync(_session);
|
||||
long th1 = Stopwatch.GetTimestamp();
|
||||
_perfCamMsSum += (th0 - tc0) * 1000.0 / Stopwatch.Frequency;
|
||||
double hudMs = (th1 - th0) * 1000.0 / Stopwatch.Frequency;
|
||||
|
||||
long now = Stopwatch.GetTimestamp();
|
||||
double frameMs = (now - _perfLastFrameTick) * 1000.0 / Stopwatch.Frequency;
|
||||
_perfLastFrameTick = now;
|
||||
|
||||
_perfFrameMs.Add(frameMs);
|
||||
_perfPhysicsStepMs.Add(_perfPendingStepMs);
|
||||
_perfTerrainSyncMs.Add(_perfPendingTerrainMs);
|
||||
_perfArenaSyncMs.Add(_perfPendingArenaMs);
|
||||
_perfHudMs.Add(hudMs);
|
||||
_perfPendingStepMs = 0;
|
||||
_perfPendingTerrainMs = 0;
|
||||
_perfPendingArenaMs = 0;
|
||||
|
||||
int rebakesNow = _terrain.DirtyRebakes;
|
||||
_perfDirtyRebakeDelta.Add(rebakesNow - _perfLastDirtyRebakes);
|
||||
_perfLastDirtyRebakes = rebakesNow;
|
||||
|
||||
_perfDrawCalls.Add(Performance.GetMonitor(Performance.Monitor.RenderTotalDrawCallsInFrame));
|
||||
_perfPrimitives.Add(Performance.GetMonitor(Performance.Monitor.RenderTotalPrimitivesInFrame));
|
||||
_perfObjects.Add(Performance.GetMonitor(Performance.Monitor.RenderTotalObjectsInFrame));
|
||||
_perfNodeCount.Add(Performance.GetMonitor(Performance.Monitor.ObjectNodeCount));
|
||||
_perfTimeProcess.Add(Performance.GetMonitor(Performance.Monitor.TimeProcess) * 1000.0);
|
||||
_perfTimePhysicsProcess.Add(Performance.GetMonitor(Performance.Monitor.TimePhysicsProcess) * 1000.0);
|
||||
|
||||
_perfFrameCount++;
|
||||
if (_perfFrameCount >= _perfTargetFrames)
|
||||
{
|
||||
PrintPerfSummaryAndQuit();
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
UpdateCamera((float)delta);
|
||||
_terrain.UpdateTileLod(_camera.GlobalPosition);
|
||||
_hud.Sync(_session);
|
||||
}
|
||||
}
|
||||
|
||||
private void PrintPerfSummaryAndQuit()
|
||||
{
|
||||
static double Avg(List<double> v)
|
||||
{
|
||||
if (v.Count == 0) return 0;
|
||||
double s = 0;
|
||||
foreach (double x in v) s += x;
|
||||
return s / v.Count;
|
||||
}
|
||||
static double P95(List<double> v)
|
||||
{
|
||||
if (v.Count == 0) return 0;
|
||||
var sorted = new List<double>(v);
|
||||
sorted.Sort();
|
||||
int idx = Math.Clamp((int)Math.Ceiling(sorted.Count * 0.95) - 1, 0, sorted.Count - 1);
|
||||
return sorted[idx];
|
||||
}
|
||||
double totalMs = 0;
|
||||
foreach (double x in _perfFrameMs) totalMs += x;
|
||||
double fps = totalMs > 0 ? (_perfFrameMs.Count * 1000.0 / totalMs) : 0;
|
||||
double rebakeAvg = 0;
|
||||
int rebakeTotal = 0;
|
||||
foreach (int r in _perfDirtyRebakeDelta) rebakeTotal += r;
|
||||
rebakeAvg = _perfDirtyRebakeDelta.Count > 0 ? (double)rebakeTotal / _perfDirtyRebakeDelta.Count : 0;
|
||||
|
||||
double steadyMs = 0;
|
||||
int steadyCount = Math.Max(0, _perfFrameMs.Count - 10);
|
||||
for (int i = 10; i < _perfFrameMs.Count; i++) steadyMs += _perfFrameMs[i];
|
||||
double steadyAvgMs = steadyCount > 0 ? steadyMs / steadyCount : 0;
|
||||
double steadyFps = steadyAvgMs > 0 ? 1000.0 / steadyAvgMs : 0;
|
||||
|
||||
GD.Print("=== PERF BENCHMARK SUMMARY ===");
|
||||
GD.Print($"Frames={_perfFrameMs.Count} PhysicsTicks={_perfPhysicsTicks} AvgFPS={fps:F2} SteadyFPS(10..59)={steadyFps:F2} EngineFPS={Engine.GetFramesPerSecond():F1}");
|
||||
GD.Print($"frameMs: avg={Avg(_perfFrameMs):F2} steadyAvg={steadyAvgMs:F2} p95={P95(_perfFrameMs):F2}");
|
||||
GD.Print($"physicsStepMs/frame: avg={Avg(_perfPhysicsStepMs):F2} p95={P95(_perfPhysicsStepMs):F2}");
|
||||
GD.Print($"terrainSyncMs/frame: avg={Avg(_perfTerrainSyncMs):F2} p95={P95(_perfTerrainSyncMs):F2} dirtyRebakesTotal={rebakeTotal} dirtyRebakesAvg={rebakeAvg:F2}");
|
||||
GD.Print($"arenaSyncMs/frame: avg={Avg(_perfArenaSyncMs):F2} p95={P95(_perfArenaSyncMs):F2}");
|
||||
GD.Print($"hudMs/frame: avg={Avg(_perfHudMs):F2} p95={P95(_perfHudMs):F2}");
|
||||
GD.Print($"RenderTotalDrawCallsInFrame: avg={Avg(_perfDrawCalls):F0} max={P95(_perfDrawCalls):F0}");
|
||||
GD.Print($"RenderTotalPrimitivesInFrame: avg={Avg(_perfPrimitives):F0}");
|
||||
GD.Print($"RenderTotalObjectsInFrame: avg={Avg(_perfObjects):F0}");
|
||||
GD.Print($"ObjectNodeCount: avg={Avg(_perfNodeCount):F0}");
|
||||
GD.Print($"TimeProcessMs: avg={Avg(_perfTimeProcess):F2}");
|
||||
GD.Print($"TimePhysicsProcessMs: avg={Avg(_perfTimePhysicsProcess):F2}");
|
||||
GD.Print($"UpdateCameraMs/frame: avg={_perfCamMsSum / _perfFrameMs.Count:F2}");
|
||||
GD.Print($"StepBreakdownMs/tick: ring={ArenaSession.PerfRingMs / _perfPhysicsTicks:F2} ai={ArenaSession.PerfAiMs / _perfPhysicsTicks:F2} drive={ArenaSession.PerfDriveMs / _perfPhysicsTicks:F2} world={ArenaSession.PerfWorldMs / _perfPhysicsTicks:F2} sync={ArenaSession.PerfBodySyncMs / _perfPhysicsTicks:F2} detect={ArenaSession.PerfDetectMs / _perfPhysicsTicks:F2} weapons={ArenaSession.PerfWeaponsMs / _perfPhysicsTicks:F2}");
|
||||
foreach (var kv in _arena.AssetCache.Vehicles)
|
||||
{
|
||||
int tris = 0;
|
||||
var bm = kv.Value.BakedStaticMesh;
|
||||
for (int s = 0; s < bm.GetSurfaceCount(); s++)
|
||||
{
|
||||
var arr = bm.SurfaceGetArrays(s);
|
||||
if (arr.Count > (int)Mesh.ArrayType.Index && arr[(int)Mesh.ArrayType.Index].VariantType == Variant.Type.PackedInt32Array)
|
||||
tris += arr[(int)Mesh.ArrayType.Index].AsInt32Array().Length / 3;
|
||||
}
|
||||
int farTris = 0;
|
||||
var fm = kv.Value.BakedLodFarMesh;
|
||||
for (int s = 0; s < fm.GetSurfaceCount(); s++)
|
||||
{
|
||||
var arr = fm.SurfaceGetArrays(s);
|
||||
if (arr.Count > (int)Mesh.ArrayType.Index && arr[(int)Mesh.ArrayType.Index].VariantType == Variant.Type.PackedInt32Array)
|
||||
farTris += arr[(int)Mesh.ArrayType.Index].AsInt32Array().Length / 3;
|
||||
}
|
||||
int nodes = CountSubtreeNodes(kv.Value.SceneTemplate);
|
||||
GD.Print($" Vehicle '{kv.Key}': nodes={nodes} bakedTris={tris} farTris={farTris} surfs={bm.GetSurfaceCount()} farSurfs={fm.GetSurfaceCount()} detachChunks={kv.Value.DetachableParts.Count}");
|
||||
}
|
||||
if (_arena.AssetCache.Vehicles.TryGetValue("paladin", out var palTpl))
|
||||
{
|
||||
var names = new List<string>();
|
||||
CollectNodeNames(palTpl.SceneTemplate, names, 40);
|
||||
GD.Print($" Paladin sample nodes: {string.Join(", ", names)}");
|
||||
}
|
||||
GD.Print("=== END PERF BENCHMARK ===");
|
||||
GetTree().Quit(0);
|
||||
}
|
||||
|
||||
private static void CollectNodeNames(Node cur, List<string> outNames, int limit)
|
||||
{
|
||||
if (outNames.Count >= limit) return;
|
||||
outNames.Add($"{cur.Name}({cur.GetType().Name})");
|
||||
for (int i = 0; i < cur.GetChildCount(); i++) CollectNodeNames(cur.GetChild(i), outNames, limit);
|
||||
}
|
||||
|
||||
private static int CountSubtreeNodes(Node cur)
|
||||
{
|
||||
int n = 1;
|
||||
for (int i = 0; i < cur.GetChildCount(); i++) n += CountSubtreeNodes(cur.GetChild(i));
|
||||
return n;
|
||||
}
|
||||
|
||||
private PlayerInput GatherPlayerInput()
|
||||
|
|
@ -374,6 +561,8 @@ public partial class Main : Node3D
|
|||
LightColor = new Color(1.0f, 0.92f, 0.78f),
|
||||
LightEnergy = 1.45f,
|
||||
ShadowEnabled = true,
|
||||
DirectionalShadowMode = DirectionalLight3D.ShadowMode.Parallel2Splits,
|
||||
DirectionalShadowMaxDistance = 145f,
|
||||
RotationDegrees = new Vector3(-48f, 32f, 0f)
|
||||
};
|
||||
AddChild(sun);
|
||||
|
|
|
|||
Loading…
Reference in a new issue