Frame time on a big farm: batched grass, sleeping bundles, no light probes

Measured in a real 2560x1440 window on the player's Mommy save (RTX 4090):
the GPU took ~5 ms, the main thread the rest.

- Grass: a fully mown sector draws only its floor (one draw, not up to six);
  untouched and fully mown sectors share a mow map look, so the fur pile
  draws them all in one instanced call per pile (GC_SECTOR_BUFFER, sector
  frames from a structured buffer). Grass draws 1112 -> 337 at the base,
  876 -> 439 in the meadow.
- Bundles: loose ones sleep when they come to rest (our gravity kept ~600
  bodies awake), ones on belts skip the ground checks, and all of them are
  stepped from one CargoTick instead of two Unity messages each.
- Belts leave a jammed line alone and only look every few steps whether it
  can move; splitters test distance before transforming every bundle.
- Light probes off on everything (the planet has none) and the GPU Resident
  Drawer off: both cost CPU per moving renderer for nothing.
- -demogpu N -onscreen measures frame time with one feature off at a time.

Base view 17-19 ms -> 14-15 ms, meadow 14-16 ms -> 11.5-12 ms.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
NiemandKG
2026-11-09 20:01:02 +06:00
parent 80271aea72
commit 57d0f4d31a
17 changed files with 360 additions and 20 deletions

View File

@ -0,0 +1,96 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using UnityEngine;
using UnityEngine.Rendering;
using UnityEngine.Rendering.Universal;
namespace GrassCutter
{
public partial class DemoBot
{
/// <summary>
/// -demogpu N -onscreen: save slot N in a real window, vsync off, and the frame time with one thing switched off at
/// a time (grass, MSAA, SSAO, shadows, post, lights, bundles...), at the base and out in the meadow. The CPU side
/// takes about 7 ms, so anything slower is the GPU. Writes gpu.txt.
/// </summary>
IEnumerator Gpu(int slot)
{
SaveSlots.Current = slot - 1;
yield return new WaitForSeconds(2f);
NetSession.Instance.HostLocal();
while (!Me || !Ts) yield return null;
yield return new WaitForSeconds(6f);
QualitySettings.vSyncCount = 0;
Application.targetFrameRate = -1;
var asset = GraphicsSettings.currentRenderPipeline as UniversalRenderPipelineAsset;
var datas = asset ? typeof(UniversalRenderPipelineAsset).GetField("m_RendererDataList", BindingFlags.NonPublic | BindingFlags.Instance)?.GetValue(asset) as ScriptableRendererData[] : null;
var features = datas != null && datas.Length > 0 && datas[0] ? datas[0].rendererFeatures : new List<ScriptableRendererFeature>();
var cam = CameraFx.Cam ? CameraFx.Cam : Camera.main;
var camData = cam ? cam.GetUniversalAdditionalCameraData() : null;
var sun = FindObjectsByType<Light>(FindObjectsSortMode.None).Where(l => l.type == LightType.Directional).OrderByDescending(l => l.intensity).FirstOrDefault();
var outp = new StringBuilder();
outp.AppendLine($"screen {Screen.width}x{Screen.height}, {SystemInfo.graphicsDeviceName}, msaa {asset?.msaaSampleCount}, hdr {asset?.supportsHDR}, shadows {asset?.shadowDistance} m x{asset?.shadowCascadeCount}, features {string.Join(",", features.Select(f => f ? f.name + (f.isActive ? "" : "(off)") : "null"))}");
outp.AppendLine($"{BuildPiece.All.Count} pieces, {Cargo.All.Count} cargo, {FindObjectsByType<Renderer>(FindObjectsSortMode.None).Length} renderers, {FindObjectsByType<Light>(FindObjectsSortMode.None).Length} lights");
void Toggle(IEnumerable<Renderer> rs, bool on) { foreach (var r in rs) if (r) r.enabled = on; }
List<Renderer> Of(IEnumerable<Component> cs) => cs.Where(c => c).SelectMany(c => c.GetComponentsInChildren<Renderer>()).ToList();
var tests = new List<(string name, Action off, Action on)>
{
("base", () => { }, () => { }),
("grass off", () => { if (GrassField.Instance) GrassField.Instance.enabled = false; }, () => { if (GrassField.Instance) GrassField.Instance.enabled = true; }),
("msaa 1x", () => asset.msaaSampleCount = 1, () => asset.msaaSampleCount = 4),
("hdr off", () => asset.supportsHDR = false, () => asset.supportsHDR = true),
("post off", () => { if (camData) camData.renderPostProcessing = false; }, () => { if (camData) camData.renderPostProcessing = true; }),
("shadows off", () => asset.shadowDistance = 0f, () => asset.shadowDistance = 90f),
("shadows 2 casc 60 m", () => { asset.shadowCascadeCount = 2; asset.shadowDistance = 60f; }, () => { asset.shadowCascadeCount = 4; asset.shadowDistance = 90f; }),
("hard shadows", () => { if (sun) sun.shadows = LightShadows.Hard; }, () => { if (sun) sun.shadows = LightShadows.Soft; }),
("other lights off", () => { foreach (var l in FindObjectsByType<Light>(FindObjectsSortMode.None)) if (l != sun) l.enabled = false; }, () => { foreach (var l in FindObjectsByType<Light>(FindObjectsInactive.Include, FindObjectsSortMode.None)) if (l != sun) l.enabled = true; }),
("bundles hidden", () => Toggle(Of(Cargo.All), false), () => Toggle(Of(Cargo.All), true)),
("pieces hidden", () => Toggle(Of(BuildPiece.All), false), () => Toggle(Of(BuildPiece.All), true)),
("render scale 0.75", () => asset.renderScale = 0.75f, () => asset.renderScale = 1f),
};
foreach (var f in features)
if (f && f.isActive) { var ff = f; tests.Add(($"feature {ff.name} off", () => ff.SetActive(false), () => ff.SetActive(true))); }
tests.Add(("base again", () => { }, () => { }));
var views = new (string name, Vector3 at, Vector3 look)[]
{
("base", Planet.FromBase(0f, -14f), Planet.FromBase(0f, 6f)),
("meadow", Planet.FromBase(140f, 120f), Planet.FromBase(260f, 220f)),
};
foreach (var v in views)
{
Me.Teleport(Planet.OnSurface(v.at) + v.at * 0.2f);
yield return new WaitForSeconds(2f);
Me.LookAt(Planet.OnSurface(v.look));
yield return new WaitForSeconds(1.5f);
double baseMs = 0;
foreach (var (name, off, on) in tests)
{
off();
yield return new WaitForSeconds(0.6f);
var dts = new List<float>();
float until = Time.realtimeSinceStartup + 4f;
while (Time.realtimeSinceStartup < until) { yield return null; dts.Add(Time.unscaledDeltaTime * 1000f); }
on();
dts.Sort();
double ms = dts.Count > 0 ? dts[dts.Count / 2] : 0;
if (name == "base") baseMs = ms;
if (name == "base") outp.AppendLine($"{v.name}: grass draws {(GrassField.Instance ? GrassField.Instance.Draws : -1)}");
outp.AppendLine($"{v.name,-7} {name,-34} {ms,6:0.0} ms {1000.0 / Math.Max(0.1, ms),5:0} fps {(name == "base" ? "" : $"{ms - baseMs:+0.0;-0.0} ms")}");
yield return new WaitForSeconds(0.3f);
}
yield return Shot($"gpu_{v.name}");
}
File.WriteAllText(Path.Combine(folder, "gpu.txt"), outp.ToString());
Log("gpu done");
Application.Quit();
}
}
}