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

@ -37,9 +37,26 @@ Shader "GrassCutter/GrassFur"
#pragma multi_compile _ _MAIN_LIGHT_SHADOWS _MAIN_LIGHT_SHADOWS_CASCADE _MAIN_LIGHT_SHADOWS_SCREEN
#pragma multi_compile_fragment _ _SHADOWS_SOFT _SHADOWS_SOFT_LOW _SHADOWS_SOFT_MEDIUM _SHADOWS_SOFT_HIGH
#pragma multi_compile_fog
// many sectors in one draw: instance = sector * shells + shell, the sector's frame from a buffer. Only for
// sectors that share a mow map look (untouched, or mown to the last blade): on a small planet there are
// hundreds of them in view, and one draw each was most of a frame's CPU.
#pragma multi_compile_local _ GC_SECTOR_BUFFER
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Lighting.hlsl"
#if defined(GC_SECTOR_BUFFER)
struct GcSectorData { float4 c, e1, e2, salt, grid; float4 b[6]; };
StructuredBuffer<GcSectorData> _GcSectorBuf;
static GcSectorData gcS;
#define GC_SECTOR_C gcS.c
#define GC_SECTOR_E1 gcS.e1
#define GC_SECTOR_E2 gcS.e2
#define GC_SECTOR_SALT gcS.salt
#define GC_SECTOR_BORDER(k) gcS.b[k]
#define GC_GRID gcS.grid
#else
#define GC_GRID _GcGrid
#endif
#include "Planet.hlsl"
CBUFFER_START(UnityPerMaterial)
@ -77,6 +94,7 @@ Shader "GrassCutter/GrassFur"
float4 data : TEXCOORD1; // x = shell 0..1, y = pile height here, z = big patch noise, w = edge fade
float3 root : TEXCOORD2; // undisplaced world position (strand lookup)
float fog : TEXCOORD3;
nointerpolation uint sid : TEXCOORD6; // which sector of the draw
float2 plane : TEXCOORD4; // the sector's flat frame (mow map)
float2 air : TEXCOORD5; // x = sun through the clouds, y = gust passing
};
@ -84,18 +102,23 @@ Shader "GrassCutter/GrassFur"
Varyings vert(Attributes v)
{
float shells = _GcShells.x;
uint sid = v.iid / (uint)shells;
uint shell = v.iid - sid * (uint)shells;
#if defined(GC_SECTOR_BUFFER)
gcS = _GcSectorBuf[sid];
#endif
// instance 0 is the top shell; a single shell is the solid floor under the strands
float t = shells > 1 ? 1 - (float)v.iid / (shells - 1) : 0;
float2 plane = v.positionOS.xz * _GcGrid.x;
float3 up = normalize(_GcC.xyz * _GcPlanet.w + _GcE1.xyz * plane.x + _GcE2.xyz * plane.y);
float t = shells > 1 ? 1 - (float)shell / (shells - 1) : 0;
float2 plane = v.positionOS.xz * GC_GRID.x;
float3 up = normalize(GC_SECTOR_C.xyz * _GcPlanet.w + GC_SECTOR_E1.xyz * plane.x + GC_SECTOR_E2.xyz * plane.y);
float3 t1, t2;
GcTangents(up, t1, t2);
// a grid cuts corners over bumps: each corner stands on the highest ground within half a cell of it, and a
// little over, so no bump between corners comes up through the floor
float hs = _GcGrid.y * 0.5 / _GcPlanet.w;
float hs = GC_GRID.y * 0.5 / _GcPlanet.w;
float ground = max(GcHeight(up), max(max(GcHeight(normalize(up + t1 * hs)), GcHeight(normalize(up - t1 * hs))),
max(GcHeight(normalize(up + t2 * hs)), GcHeight(normalize(up - t2 * hs)))));
float3 root = _GcPlanet.xyz + up * (_GcPlanet.w + ground + 0.01 + _GcGrid.y * 0.012);
float3 root = _GcPlanet.xyz + up * (_GcPlanet.w + ground + 0.01 + GC_GRID.y * 0.012);
float dist = length(root - _GcCam.xyz);
float fade = 1 - smoothstep(_GcFade.x, _GcFade.y, dist);
@ -148,6 +171,7 @@ Shader "GrassCutter/GrassFur"
o.plane = plane;
o.fog = ComputeFogFactor(o.positionCS.z);
o.air = float2(GcCloudSun(root, tm), gust);
o.sid = sid;
return o;
}
@ -161,8 +185,11 @@ Shader "GrassCutter/GrassFur"
if ((_GcShells.y > 0 && dist < _GcShells.y + e) || dist >= _GcShells.z + e) discard;
// a sector's square reaches past its borders: each pixel belongs to one sector
#if defined(GC_SECTOR_BUFFER)
gcS = _GcSectorBuf[i.sid];
#endif
float3 d = normalize(i.root - _GcPlanet.xyz);
for (int b = 0; b < 6; b++) if (dot(d, _GcBorders[b].xyz) > 0) discard;
for (int b = 0; b < 6; b++) if (dot(d, GC_SECTOR_BORDER(b).xyz) > 0) discard;
float2 mask = GcMask(i.plane);
bool solid = t < 0.001; // the bottom of the pile: the floor everything stands on

View File

@ -47,6 +47,15 @@ float4 _GcC, _GcE1, _GcE2;
float4 _GcSalt; // xy = seed, z = flat frame metres to mow map uv
float4 _GcBorders[6];
// a shader that draws many sectors in one go defines these first (GrassFur: GC_SECTOR_BUFFER)
#ifndef GC_SECTOR_C
#define GC_SECTOR_C _GcC
#define GC_SECTOR_E1 _GcE1
#define GC_SECTOR_E2 _GcE2
#define GC_SECTOR_SALT _GcSalt
#define GC_SECTOR_BORDER(k) _GcBorders[k]
#endif
float GcHeight(float3 d)
{
float3 a = abs(d);
@ -82,22 +91,22 @@ void GcTangents(float3 up, out float3 t1, out float3 t2)
// this sector's mow map at a spot of its flat frame: r = cut (0..1), g = nothing grows here
float2 GcMask(float2 plane)
{
return SAMPLE_TEXTURE2D_LOD(_GcMaskTex, sampler_GcMaskTex, plane * _GcSalt.z + 0.5, 0).rg;
return SAMPLE_TEXTURE2D_LOD(_GcMaskTex, sampler_GcMaskTex, plane * GC_SECTOR_SALT.z + 0.5, 0).rg;
}
// how close a cut is: the map blurred over half a metre, so the grass beside a narrow strip can stand lower than the rest
float GcMaskNear(float2 plane)
{
return SAMPLE_TEXTURE2D_LOD(_GcMaskTex, sampler_GcMaskTex, plane * _GcSalt.z + 0.5, 1.5).r;
return SAMPLE_TEXTURE2D_LOD(_GcMaskTex, sampler_GcMaskTex, plane * GC_SECTOR_SALT.z + 0.5, 1.5).r;
}
// A spot in the flat frame of this draw's sector, on the ball. Each strand belongs to exactly one sector: the one whose
// borders all lie ahead of it. edge = metres to the nearest of them.
bool GcInSector(float2 plane, out float3 dir, out float edge)
{
dir = normalize(_GcC.xyz * _GcPlanet.w + _GcE1.xyz * plane.x + _GcE2.xyz * plane.y);
dir = normalize(GC_SECTOR_C.xyz * _GcPlanet.w + GC_SECTOR_E1.xyz * plane.x + GC_SECTOR_E2.xyz * plane.y);
edge = 1e5;
for (int k = 0; k < 6; k++) edge = min(edge, -dot(dir, _GcBorders[k].xyz));
for (int k = 0; k < 6; k++) edge = min(edge, -dot(dir, GC_SECTOR_BORDER(k).xyz));
return edge >= 0;
}