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:
@ -97,6 +97,8 @@ namespace GrassCutter
|
||||
readonly List<HexGrid.Sector> _worked = new List<HexGrid.Sector>();
|
||||
readonly Plane[] _frustum = new Plane[6];
|
||||
int _mpbUsed, _here = -1;
|
||||
/// <summary>Grass draws issued last frame (for -demogpu).</summary>
|
||||
public int Draws { get; private set; }
|
||||
int _deckHash;
|
||||
|
||||
/// <summary>The grass's blocks were cleared (the ball changed size): the deck goes back in next frame.</summary>
|
||||
@ -119,6 +121,86 @@ namespace GrassCutter
|
||||
void OnDestroy()
|
||||
{
|
||||
if (_furTex) Destroy(_furTex);
|
||||
foreach (var b in _batches) b.buffer?.Release();
|
||||
if (_furBatchMat) Destroy(_furBatchMat);
|
||||
}
|
||||
|
||||
// ───────────────────────── batches ─────────────────────────
|
||||
|
||||
/// <summary>One sector's frame as the fur shader reads it from <c>_GcSectorBuf</c> (GC_SECTOR_BUFFER).</summary>
|
||||
[System.Runtime.InteropServices.StructLayout(System.Runtime.InteropServices.LayoutKind.Sequential)]
|
||||
struct SectorData { public Vector4 c, e1, e2, salt, grid, b0, b1, b2, b3, b4, b5; }
|
||||
|
||||
/// <summary>The sectors of one pile and one shared mow map look, drawn together at the end of the frame.</summary>
|
||||
sealed class Batch
|
||||
{
|
||||
public Pile pile;
|
||||
public bool onlyWorked;
|
||||
public Texture2D mask;
|
||||
public readonly List<SectorData> items = new List<SectorData>();
|
||||
public Bounds bounds;
|
||||
public GraphicsBuffer buffer;
|
||||
public readonly MaterialPropertyBlock mpb = new MaterialPropertyBlock();
|
||||
}
|
||||
|
||||
static readonly int SectorBufId = Shader.PropertyToID("_GcSectorBuf");
|
||||
readonly List<Batch> _batches = new List<Batch>();
|
||||
Material _furBatchMat;
|
||||
|
||||
bool Batched(Pile p, HexGrid.Sector s, Texture2D mask, bool onlyWorked)
|
||||
{
|
||||
if (mask != MowMap.Blank && mask != MowMap.Full) return false;
|
||||
if (!_furBatchMat && furMat)
|
||||
{
|
||||
_furBatchMat = new Material(furMat) { name = furMat.name + " (batched)" };
|
||||
_furBatchMat.EnableKeyword("GC_SECTOR_BUFFER");
|
||||
}
|
||||
if (!_furBatchMat) return false;
|
||||
Batch b = null;
|
||||
foreach (var x in _batches)
|
||||
if (x.mask == mask && x.onlyWorked == onlyWorked && x.pile.shells == p.shells && x.pile.inner == p.inner && x.pile.outer == p.outer && x.pile.fadeA == p.fadeA && x.pile.fadeB == p.fadeB) { b = x; break; }
|
||||
if (b == null) { b = new Batch { pile = p, onlyWorked = onlyWorked, mask = mask }; _batches.Add(b); }
|
||||
if (b.items.Count == 0) b.bounds = s.bounds; else b.bounds.Encapsulate(s.bounds);
|
||||
bool floor = p.shells == 1;
|
||||
var bs = s.borders;
|
||||
b.items.Add(new SectorData
|
||||
{
|
||||
c = s.c, e1 = s.e1, e2 = s.e2, salt = s.salt,
|
||||
grid = new Vector4(s.half, s.half * 2f / ((floor ? FloorSide : GridSide) - 1), 0f, 0f),
|
||||
b0 = bs[0], b1 = bs[1], b2 = bs[2], b3 = bs[3], b4 = bs[4], b5 = bs[5],
|
||||
});
|
||||
return true;
|
||||
}
|
||||
|
||||
void DrawBatches()
|
||||
{
|
||||
foreach (var b in _batches)
|
||||
{
|
||||
int n = b.items.Count;
|
||||
if (n == 0) continue;
|
||||
if (b.buffer == null || b.buffer.count < n)
|
||||
{
|
||||
b.buffer?.Release();
|
||||
b.buffer = new GraphicsBuffer(GraphicsBuffer.Target.Structured, Mathf.NextPowerOfTwo(Mathf.Max(64, n)), System.Runtime.InteropServices.Marshal.SizeOf<SectorData>());
|
||||
}
|
||||
b.buffer.SetData(b.items);
|
||||
var p = b.pile;
|
||||
b.mpb.SetBuffer(SectorBufId, b.buffer);
|
||||
b.mpb.SetTexture(MaskTexId, b.mask);
|
||||
b.mpb.SetVector(ShellsId, new Vector4(p.shells, b.onlyWorked ? 0f : p.inner, b.onlyWorked ? 1e6f : p.outer, SeamBand));
|
||||
b.mpb.SetVector(FadeId, new Vector4(p.fadeA, b.onlyWorked ? 1e6f : p.fadeB, b.onlyWorked ? 1f : 0f, 0f));
|
||||
var rp = new RenderParams(_furBatchMat)
|
||||
{
|
||||
worldBounds = b.bounds,
|
||||
matProps = b.mpb,
|
||||
shadowCastingMode = ShadowCastingMode.Off,
|
||||
receiveShadows = true,
|
||||
layer = 0,
|
||||
};
|
||||
Graphics.RenderMeshPrimitives(rp, p.shells == 1 ? _floorGrid : _grid, 0, p.shells * n);
|
||||
_drawn++;
|
||||
b.items.Clear();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>Meadow height in metres.</summary>
|
||||
@ -177,6 +259,7 @@ namespace GrassCutter
|
||||
int here = HexGrid.SectorAt(dir);
|
||||
if (here != _here || _gatheredReach != FurReach) { _here = here; _gatheredReach = FurReach; GatherNear(here); }
|
||||
_mpbUsed = 0;
|
||||
_drawn = 0;
|
||||
PublishFurAxes(dir);
|
||||
GeometryUtility.CalculateFrustumPlanes(cam, _frustum);
|
||||
|
||||
@ -187,6 +270,13 @@ namespace GrassCutter
|
||||
float arc = (s.c - dir).magnitude * Planet.Radius;
|
||||
float reachable = Mathf.Max(arc - s.radius, alt);
|
||||
var mask = MowMap.TextureFor(s);
|
||||
// mown to the last blade: only the floor shows (the pile's shells and every plant are cut away in the
|
||||
// shader anyway), one draw instead of up to six. Near the base that is most of what is in view.
|
||||
if (mask == MowMap.Full && mask)
|
||||
{
|
||||
if (furMat) DrawPile(FurFloor, s, mask, true);
|
||||
continue;
|
||||
}
|
||||
if (furMat)
|
||||
{
|
||||
var far = FurFar;
|
||||
@ -214,8 +304,12 @@ namespace GrassCutter
|
||||
if (furMat)
|
||||
foreach (var s in _worked)
|
||||
if (GeometryUtility.TestPlanesAABB(_frustum, s.bounds)) DrawPile(FurFloor, s, MowMap.TextureFor(s), true);
|
||||
DrawBatches();
|
||||
Draws = _mpbUsed + _drawn;
|
||||
}
|
||||
|
||||
int _drawn;
|
||||
|
||||
/// <summary>Every sector the rings can reach from here, the one underfoot first, and the worked ones further out.</summary>
|
||||
void GatherNear(int here)
|
||||
{
|
||||
@ -276,6 +370,7 @@ namespace GrassCutter
|
||||
/// <summary>The pile over a whole sector. onlyWorked: past the grass, just the floor where something was cut.</summary>
|
||||
void DrawPile(Pile p, HexGrid.Sector s, Texture2D mask, bool onlyWorked)
|
||||
{
|
||||
if (Batched(p, s, mask, onlyWorked)) return;
|
||||
var mpb = NextMpb();
|
||||
SetSector(mpb, s, mask);
|
||||
bool floor = p.shells == 1;
|
||||
|
||||
Reference in New Issue
Block a user