phase(N.5) Task 9: BuildIndirectArrays — CPU layout for indirect dispatch
Pure CPU helper that lays out a group list into a contiguous indirect buffer (DrawElementsIndirectCommand[]) and parallel BatchData[] — opaque section first, transparent section second. Returns counts + byte offset for the transparent section. Tests cover: spec §5 walk-through layout; empty group list edge case; ClipMap classification (treated as opaque, not transparent). Static + public so tests can exercise without a GL context. Task 10 wires it into the rewritten Draw() method. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
parent
424d7b9015
commit
9a7a250b62
2 changed files with 196 additions and 0 deletions
|
|
@ -529,6 +529,108 @@ public sealed unsafe class WbDrawDispatcher : IDisposable
|
|||
_gl.DeleteBuffer(_indirectBuffer);
|
||||
}
|
||||
|
||||
// ── Public types + helpers for BuildIndirectArrays (Task 9) ─────────────
|
||||
//
|
||||
// These are public so the pure-CPU unit tests in AcDream.Core.Tests can
|
||||
// exercise BuildIndirectArrays without needing a GL context.
|
||||
|
||||
/// <summary>
|
||||
/// Public view of the per-group inputs to <see cref="BuildIndirectArrays"/> — used in tests.
|
||||
/// </summary>
|
||||
public readonly record struct IndirectGroupInput(
|
||||
int IndexCount,
|
||||
uint FirstIndex,
|
||||
int BaseVertex,
|
||||
int InstanceCount,
|
||||
int FirstInstance,
|
||||
ulong TextureHandle,
|
||||
uint TextureLayer,
|
||||
TranslucencyKind Translucency);
|
||||
|
||||
/// <summary>
|
||||
/// Public mirror of the per-group <see cref="BatchData"/> uploaded to the SSBO.
|
||||
/// Tests verify the layout. Same field shape as the private BatchData.
|
||||
/// </summary>
|
||||
[StructLayout(LayoutKind.Sequential, Pack = 8)]
|
||||
public struct BatchDataPublic
|
||||
{
|
||||
public ulong TextureHandle;
|
||||
public uint TextureLayer;
|
||||
public uint Flags;
|
||||
}
|
||||
|
||||
/// <summary>Result of <see cref="BuildIndirectArrays"/>.</summary>
|
||||
public readonly record struct IndirectLayoutResult(
|
||||
int OpaqueCount,
|
||||
int TransparentCount,
|
||||
int TransparentByteOffset);
|
||||
|
||||
/// <summary>
|
||||
/// Lays out the indirect commands + parallel BatchData array contiguously:
|
||||
/// opaque section first (caller sorts before calling), transparent section second.
|
||||
/// Pure CPU, no GL state. Caller passes pre-sized scratch arrays.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// Classification: Opaque + ClipMap → opaque pass (ClipMap uses discard, not
|
||||
/// blending). Everything else (AlphaBlend, Additive, InvAlpha) → transparent pass.
|
||||
/// </remarks>
|
||||
public static IndirectLayoutResult BuildIndirectArrays(
|
||||
IReadOnlyList<IndirectGroupInput> groups,
|
||||
DrawElementsIndirectCommand[] indirectScratch,
|
||||
BatchDataPublic[] batchScratch)
|
||||
{
|
||||
int opaqueCount = 0;
|
||||
int transparentCount = 0;
|
||||
|
||||
foreach (var g in groups)
|
||||
{
|
||||
if (IsOpaque(g.Translucency)) opaqueCount++;
|
||||
else transparentCount++;
|
||||
}
|
||||
|
||||
int oi = 0; // opaque write cursor (fills [0..opaqueCount))
|
||||
int ti = opaqueCount; // transparent write cursor (fills [opaqueCount..end))
|
||||
|
||||
foreach (var g in groups)
|
||||
{
|
||||
var dec = new DrawElementsIndirectCommand
|
||||
{
|
||||
Count = (uint)g.IndexCount,
|
||||
InstanceCount = (uint)g.InstanceCount,
|
||||
FirstIndex = g.FirstIndex,
|
||||
BaseVertex = g.BaseVertex,
|
||||
BaseInstance = (uint)g.FirstInstance,
|
||||
};
|
||||
var bd = new BatchDataPublic
|
||||
{
|
||||
TextureHandle = g.TextureHandle,
|
||||
TextureLayer = g.TextureLayer,
|
||||
Flags = 0,
|
||||
};
|
||||
|
||||
if (IsOpaque(g.Translucency))
|
||||
{
|
||||
indirectScratch[oi] = dec;
|
||||
batchScratch[oi] = bd;
|
||||
oi++;
|
||||
}
|
||||
else
|
||||
{
|
||||
indirectScratch[ti] = dec;
|
||||
batchScratch[ti] = bd;
|
||||
ti++;
|
||||
}
|
||||
}
|
||||
|
||||
const int SizeofDEIC = 20; // sizeof(DrawElementsIndirectCommand) — 5 × uint
|
||||
return new IndirectLayoutResult(opaqueCount, transparentCount, opaqueCount * SizeofDEIC);
|
||||
}
|
||||
|
||||
private static bool IsOpaque(TranslucencyKind t)
|
||||
=> t == TranslucencyKind.Opaque || t == TranslucencyKind.ClipMap;
|
||||
|
||||
// ────────────────────────────────────────────────────────────────────────
|
||||
|
||||
private readonly record struct GroupKey(
|
||||
uint Ibo,
|
||||
uint FirstIndex,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue