fix(render): correct foliage-wind classification, receiver/caster desync, and frame binding (Campaign VM VM6 review)

Opus dual-lens review of the three VM6 commits (0930c35d, 39e8408c,
6cc5e183) found two blockers and two should-fix issues; all landed here
along with the review's nits and documentation corrections.

Blockers:
- A1: the procedural-scenery classifier tested bit 31 alone instead of
  the full top nibble (0xF000_0000 == 0x8000_0000), so it also matched
  LandblockStaticEntityIdAllocator's 0xC... namespace (fences/gates/
  building shells with a cutout subset), the 0xDA11_D0xx paperdoll id,
  and the 0xFFFF_FF01 portal-tunnel id as procedural scenery — all
  three would have swayed. ProceduralSceneryIdAllocator.IsInNamespace
  now does the exact top-nibble test; FoliageWindClassification
  delegates to it.
- A2: GroupKey (the receiver's instance-batching key) did not carry
  FoliageFlags while the caster's dedup key already did, so a scenery
  instance and a non-scenery instance sharing a mesh subset coalesced
  into one receiver InstanceGroup whose flags were last-writer-wins —
  disagreeing with the correctly-keyed caster. GroupKey now carries
  FoliageFlags, computed before key construction and set exactly once
  at group creation; the imperative re-stamp is gone, and CachedBatch's
  now-redundant FoliageFlags field is removed.

Should-fix:
- A3: the world receiver pass bound UniformAtmosphericFrame only by
  accident (leftover from the caster pass, which runs first each
  frame, since Vulkan binding state isn't reset between passes).
  DirectionalShadowFrameBinding now carries the caster's exact
  AtmosphericFrameBufferBinding and BindDirectionalShadowReceiver binds
  it explicitly.
- A4: a Setup-composed tree's opaque trunk part never got the trunk
  flag because HasCutoutSubset is cached per GfxObj part, not per
  entity. FoliageWindClassification.ComputeEntityHasCutoutSubset now
  ORs HasCutoutSubset across an entity's resolved sibling parts once
  per entity, threaded into ClassifyBatches/AddDirectionalShadowBatches
  via a new optional override parameter.

Nits: A5 hashes the per-vertex flutter seed relative to the instance
origin instead of absolute world XY (fp32 sin() precision loss at far
landblock corners), mirrored in both foliage_wind.glsl and
FoliageWindModel; A7 documents the max(maxHeight, 0.5) divide-guard as
a deliberate pseudocode divergence; A8 switches FoliageWindExclusions'
construction to ToFrozenSet() and softens the "never stale" doc
comment to "no slower than one frame behind."

Tests added: top-nibble classification (0xFFFFFFFFu now correctly
false), GroupKey inequality across entity-driven scenery/landblock-
static classification, a caster-batch test proving the same pairing
never coalesces, ComputeEntityHasCutoutSubset unit + end-to-end
two-part-Setup tests, the caster→receiver AtmosphericFrame binding
carry-through, flutter-hash translation invariance relative to
instance origin, and a Storm-wind mid-height displacement floor
guarding against a "no motion" regression.

Docs: plan VM6 body corrected to the five-row WeatherKind table, "bits
1 and 2", "all four" caster shaders, and top-nibble wording throughout;
the owner gate checklist's Rain/Storm step; the stale v1-only shader-
interface compatibility entry; semantic-bindings-v1.md's v2 members
folded into the main 192-byte block; the IA-25 register row's top-
nibble wording; AtmosphericFrameInputs.cs's ABI size reference.

foliage_wind.glsl's A5 change recompiled exactly the five shaders that
include it (mesh_atmospheric.vert, the four directional_shadow_world_*
casters) plus the manifest; no other .spv changed.

Verify: Release build 0 warnings/0 errors. App hermetic-lane filter
6,041/0 failed (no environment-specific failures this run).
RenderPackValidator 30/30. Full hermetic-filtered solution: 15,269/0
failed across 15 projects.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
Erik 2026-08-23 01:54:36 +02:00
parent 27a446f05c
commit 43e3abed4d
28 changed files with 724 additions and 123 deletions

View file

@ -490,9 +490,9 @@ public sealed partial class WbDrawDispatcher : IDisposable
/// material would otherwise classify them as foliage. Empty (no
/// exclusions) when no atmospheric pack is active. Assigned every frame
/// by <c>AtmosphericPostProcessGraph.RenderDirectionalShadows</c> from
/// the currently-selected pack's declaration — self-contained by
/// construction, so a pack switch or deactivation can never leave a
/// stale exclusion set applied.
/// the currently-selected pack's declaration, so a pack switch or
/// deactivation is reflected on the very next frame that runs the
/// assignment — not "never," just "no slower than one frame behind."
/// </summary>
public IReadOnlySet<uint> FoliageWindExclusions { get; set; } =
System.Collections.Frozen.FrozenSet<uint>.Empty;
@ -1841,6 +1841,24 @@ public sealed partial class WbDrawDispatcher : IDisposable
bool drewAny = false;
if (renderData.IsSetup && renderData.SetupParts.Count > 0)
{
// Campaign VM VM6 review fix round (A4): a Setup composite's
// parts are separate GfxObjs with their own independently
// cached ObjectRenderData/HasCutoutSubset — a tree's opaque
// trunk part carries no cutout batches of its own (the
// leaves are a DIFFERENT part), so classifying each part
// from only its own HasCutoutSubset would never give the
// trunk part the trunk flag. One bounded scan of THIS
// Setup's own (small, fixed-per-object) part list, once per
// entity per frame — not per batch, not per instance — ORs
// every currently-resolved part's HasCutoutSubset into one
// entity-scoped value classification uses below instead of
// each part's own. A part missing this frame contributes
// nothing; the entity is already marked incomplete by the
// loop below and reclassifies once every part has loaded.
bool entityHasCutoutSubset = FoliageWindClassification.ComputeEntityHasCutoutSubset(
renderData.SetupParts,
part => _meshAdapter.TryGetRenderData(part.GfxObjId) is { HasCutoutSubset: true });
// #188: setupPartIndex is the SAME index space
// TransparentPartHook.PartIndex addresses — retail's CPartArray
// numbers parts by their ordinal position in the Setup's own
@ -1905,7 +1923,7 @@ public sealed partial class WbDrawDispatcher : IDisposable
opacityMultiplier = 1f - translucencyValue; // CMaterial::SetTranslucencySimple 0x005396f0
}
if (!ClassifyBatches(partData, model, entity, meshRef, paletteIdentity, restPose, opacityMultiplier, collector))
if (!ClassifyBatches(partData, model, entity, meshRef, paletteIdentity, restPose, opacityMultiplier, collector, entityHasCutoutSubset))
currentEntityIncomplete = true;
_selectionSink?.AddVisiblePart(
entity.ServerGuid,
@ -2378,7 +2396,8 @@ public sealed partial class WbDrawDispatcher : IDisposable
g.TextureSlot,
g.TextureLayer,
g.Translucency,
g.CullMode);
g.CullMode,
g.FoliageFlags);
private void ObserveCurrentDispatcherSubmission(
int visibleInstanceCount,
@ -2996,15 +3015,19 @@ public sealed partial class WbDrawDispatcher : IDisposable
internal static void ApplyCacheHit(
EntityCacheEntry entry,
Matrix4x4 entityWorld,
Action<GroupKey, Matrix4x4, Vector3, uint> appendInstance)
Action<GroupKey, Matrix4x4, Vector3> appendInstance)
{
foreach (var cached in entry.Batches)
{
// Campaign VM VM6 review fix round: cached.Key.FoliageFlags is
// now the ONLY source of truth for a replayed group's foliage
// classification (GetOrCreateInstanceGroup derives it from the
// key at creation) — no separate FoliageFlags field to drift
// out of sync with it.
appendInstance(
cached.Key,
cached.RestPose * entityWorld,
cached.LocalSortCenter,
cached.FoliageFlags);
cached.LocalSortCenter);
}
}
@ -3033,7 +3056,7 @@ public sealed partial class WbDrawDispatcher : IDisposable
GroupRegistration = group.Registration,
};
}
AppendInstanceToGroup(group!, model, cached.LocalSortCenter, cached.FoliageFlags);
AppendInstanceToGroup(group!, model, cached.LocalSortCenter);
}
}
@ -3147,11 +3170,10 @@ public sealed partial class WbDrawDispatcher : IDisposable
private void AppendInstanceToGroup(
GroupKey key,
Matrix4x4 model,
Vector3 localSortCenter,
uint foliageFlags)
Vector3 localSortCenter)
{
InstanceGroup grp = GetOrCreateInstanceGroup(key);
AppendInstanceToGroup(grp, model, localSortCenter, foliageFlags);
AppendInstanceToGroup(grp, model, localSortCenter);
}
private InstanceGroup GetOrCreateInstanceGroup(GroupKey key)
@ -3177,6 +3199,13 @@ public sealed partial class WbDrawDispatcher : IDisposable
TextureLayer = key.TextureLayer,
Translucency = key.Translucency,
CullMode = key.CullMode,
// Campaign VM VM6 review fix round: FoliageFlags is now part of
// GroupKey (see GroupKey's doc comment), so it is set exactly
// once here, at group creation, from the SAME key that decides
// group identity — never re-stamped imperatively afterward,
// which is what let a shared group's classification flicker
// between whichever caller ran last.
FoliageFlags = key.FoliageFlags,
Registration = _nextGroupRegistration++,
LastUsedFrame = _groupFrame,
};
@ -3187,16 +3216,9 @@ public sealed partial class WbDrawDispatcher : IDisposable
private void AppendInstanceToGroup(
InstanceGroup grp,
Matrix4x4 model,
Vector3 localSortCenter,
uint foliageFlags)
Vector3 localSortCenter)
{
grp.LastUsedFrame = _groupFrame;
// Campaign VM VM6: re-stamp on every append (idempotent — the value
// is a deterministic function of the mesh subset the group's key
// already identifies) so a stale-registration replay that recreates
// an evicted InstanceGroup (see ApplyCacheHitDirect above) never
// leaves it at its zero default.
grp.FoliageFlags = foliageFlags;
grp.Matrices.Add(model);
grp.LocalSortCenters.Add(localSortCenter);
grp.SubmissionOrders.Add(_nextInstanceSubmissionOrder++);
@ -3337,8 +3359,18 @@ public sealed partial class WbDrawDispatcher : IDisposable
PaletteCompositeIdentity paletteIdentity,
Matrix4x4 restPose,
float opacityMultiplier = 1.0f,
List<CachedBatch>? collector = null)
List<CachedBatch>? collector = null,
// Campaign VM VM6 review fix round (A4): HasCutoutSubset is a
// per-PART (per-GfxObj) fact — a Setup composite's parts are
// separate GfxObjs, so a tree's opaque trunk part's OWN
// renderData.HasCutoutSubset is false even though the SAME Setup's
// leaves part has one. The caller passes the ENTITY/Setup-scoped OR
// across every resolved part here; null (the default) means "use
// renderData.HasCutoutSubset directly", which is already correct for
// a non-Setup single-mesh entity (see the flat MeshRef call site).
bool? entityHasCutoutSubsetOverride = null)
{
bool entityHasCutoutSubset = entityHasCutoutSubsetOverride ?? renderData.HasCutoutSubset;
bool allTexturesReady = true;
for (int batchIdx = 0; batchIdx < renderData.Batches.Count; batchIdx++)
{
@ -3369,9 +3401,25 @@ public sealed partial class WbDrawDispatcher : IDisposable
GpuTextureSlot texSlot = texture.Slot;
uint texLayer = texture.Layer;
// Campaign VM VM6 review fix round: classify BEFORE constructing
// the key and fold the result INTO the key (rather than
// stamping it onto whatever group the key already resolves to).
// Classification is from the RAW (pre-#188-promotion)
// batch.Translucency — a mid-fade trunk is still a trunk, it
// just landed in the alpha-blend group instead of opaque. This
// is what keeps a scenery instance and a non-scenery instance
// of the identical mesh subset in two SEPARATE groups instead of
// coalescing into one group whose classification depends on
// whichever entity classified it last.
uint foliageFlags = FoliageWindClassification.Classify(
entity.LocalEntityId,
FoliageWindExclusions.Contains(meshRef.GfxObjId),
batch.Translucency,
entityHasCutoutSubset);
var key = new GroupKey(
batch.FirstIndex, (int)batch.BaseVertex,
batch.IndexCount, texSlot, texLayer, translucency, batch.CullMode);
batch.IndexCount, texSlot, texLayer, translucency, batch.CullMode,
foliageFlags);
InstanceGroup grp = GetOrCreateInstanceGroup(key);
grp.Matrices.Add(model);
@ -3381,23 +3429,13 @@ public sealed partial class WbDrawDispatcher : IDisposable
AppendCurrentLightSet(grp); // Fix B — 8 ints per instance, parallel to Matrices
grp.Opacities.Add(opacityMultiplier); // #188 — parallel to Matrices
grp.SelectionLighting.Add(_currentEntitySelectionLighting);
// Campaign VM VM6: classify from the RAW (pre-#188-promotion)
// batch.Translucency — a mid-fade trunk is still a trunk, it
// just landed in the alpha-blend group instead of opaque.
uint foliageFlags = FoliageWindClassification.Classify(
entity.LocalEntityId,
FoliageWindExclusions.Contains(meshRef.GfxObjId),
batch.Translucency,
renderData.HasCutoutSubset);
grp.FoliageFlags = foliageFlags;
collector?.Add(new CachedBatch(
key,
texSlot,
restPose,
renderData.SortCenter,
grp,
grp.Registration,
foliageFlags));
grp.Registration));
}
return allTexturesReady;
}
@ -3851,9 +3889,14 @@ public sealed partial class WbDrawDispatcher : IDisposable
// into BatchData.flags alongside #226's bit 0 at BuildIndirectArrays.
// Group-level, not per-instance, because BatchData is read once per
// draw call (Batches[gl_DrawIDARB]) — every instance sharing one
// mesh-subset draw shares its classification. Set by ClassifyBatches
// on a fresh classification and re-stamped by AppendInstanceToGroup
// on every cache-hit replay (see CachedBatch.FoliageFlags).
// mesh-subset draw shares its classification. Review fix round: this
// is now set EXACTLY ONCE, in GetOrCreateInstanceGroup, from the
// owning GroupKey.FoliageFlags — never re-stamped imperatively after
// creation, so a shared group's classification can no longer
// flicker between whichever caller classified it last. A mesh
// subset reachable from both a scenery and a non-scenery entity now
// buckets into two distinct groups (distinct GroupKey.FoliageFlags)
// instead of coalescing into one.
public uint FoliageFlags;
public float SortDistance; // squared distance from camera to first instance, for opaque sort