acdream/src/AcDream.App/Rendering/PortalDepthMaskRenderer.cs

105 lines
4.9 KiB
C#

using System;
using System.Numerics;
namespace AcDream.App.Rendering;
/// <summary>
/// BR-2 (holistic building-render port): retail's invisible portal depth
/// writes — the port of <c>D3DPolyRender::DrawPortalPolyInternal</c>
/// (Ghidra 0x0059bc90, pc:424490).
///
/// <para>Campaign FW's ordered frame walk restores the positional discipline
/// on which this primitive depends: farther landscape/static content is
/// submitted before a building punch, the punch precedes that building's
/// look-in cells, and nearer content repaints afterward. Consequently both
/// sides now use retail's one-pass depth operation directly; the former
/// stencil mark/bias approximation from #117 is intentionally gone.</para>
///
/// <para>Retail projects a portal polygon, software-clips it against the
/// installed portal view (<c>polyClipFinish</c>), and draws the survivor as a
/// COLOR-INVISIBLE triangle fan with depth-test ALWAYS + depth-write ON:</para>
/// <list type="bullet">
/// <item><b>Seal</b> (retail <c>maxZ2=6</c>, bit0 clear, data 0x00820e14):
/// z = the polygon's true projected depth. Drawn on portals leading OUTSIDE
/// (<c>other_cell_id==0xFFFF</c>) after the landscape pass — terrain seen
/// through a doorway keeps its pixels because farther interior geometry
/// z-fails inside the aperture (PView::DrawCells loop 1, Ghidra 0x005a4840,
/// pc:432783-432786).</item>
/// <item><b>Punch</b> (retail <c>maxZ1=7</c>, bit0 set, data 0x00820e18):
/// z forced to the far plane (0.99999988) — erases depth inside a building
/// aperture so the interior cells drawn next land cleanly
/// (ConstructView(CBldPortal) mode-1, pc:433827). BR-2 commit 2 wires this
/// side.</item>
/// </list>
///
/// <para>Where retail clips the polygon on the CPU against the view, we apply
/// the SAME view region via <c>gl_ClipDistance</c> from the slice's clip-space
/// half-planes (≤8, the validated <see cref="ClipPlaneSet"/> output) — the
/// depth write lands only inside the slice region, matching retail's clipped
/// fan.</para>
///
/// <para>The GL arm's inline program (rehomed clip planes, raw uniform
/// locations) and this class's own dynamic VAO/VBO ring were deleted at
/// Campaign V slice V11; the RHI arm compiles
/// <c>Rendering/Shaders/portal_depth.{vert,frag}</c> from committed SPIR-V,
/// and its per-frame fan vertices come from the frame ring instead.</para>
/// </summary>
public sealed partial class PortalDepthMaskRenderer : IDisposable
{
private readonly ResourceCleanupGroup _resources;
/// <summary>Shared by both arms: the largest fan <see cref="DrawDepthFan"/> accepts.</summary>
private const int MaxFanVerts = 32;
/// <summary>
/// The GL arm's per-flight VAO/VBO ring this used to report on was deleted
/// at Campaign V slice V11: the RHI arm draws every fan from a ring
/// allocation that lives until its frame retires, so there is no
/// persistent dynamic-buffer pool left to size.
/// </summary>
internal long DynamicBufferCapacityBytes => 0;
/// <summary>
/// Selects the GPU-fenced frame slot and resets its append cursor. Portal
/// fans written during one frame occupy distinct ranges, so later portal
/// slices never overwrite vertices still referenced by earlier draws.
///
/// <para>The GL arm's per-flight VAO/VBO ring this used to activate was
/// deleted at Campaign V slice V11 — the RHI arm owns no per-flight ring
/// at all, because every frame ring allocation is already distinct memory
/// that lives until the frame retires — so all this edge carries now is
/// the started latch.</para>
/// </summary>
public void BeginFrame(int frameSlot)
{
ArgumentOutOfRangeException.ThrowIfNegative(frameSlot);
_rhiFrameStarted = true;
}
/// <summary>
/// <summary>
/// Draw one portal polygon as an invisible depth write, clipped to the
/// slice's clip-space half-planes. <paramref name="forceFarZ"/> selects
/// punch (true, retail maxZ1) vs seal (false, retail maxZ2 true depth).
///
/// <para><b>Seal</b> (interior root): one pass, retail-verbatim —
/// depth ALWAYS + true projected depth. It runs immediately after the
/// gated full depth clear, so there is no nearer content to stomp.</para>
///
/// <para><b>Punch</b> (outdoor root / look-in): one pass, retail-verbatim
/// — depth ALWAYS + far-Z. The ordered frame walk, not a visibility test
/// at the aperture plane, supplies retail's painter-order safety.</para>
/// </summary>
public void DrawDepthFan(
ReadOnlySpan<Vector3> worldVerts,
in Matrix4x4 viewProjection,
ReadOnlySpan<Vector4> planes,
bool forceFarZ)
{
if (worldVerts.Length < 3)
return;
DrawDepthFanRhi(worldVerts, in viewProjection, planes, forceFarZ);
}
public void Dispose() => DisposeRhiResources();
}