phase(N.4) Task 9: real WB pipeline bring-up + InstancedMeshRenderer routing

WbMeshAdapter now actually constructs the WB pipeline:
- OpenGLGraphicsDevice(gl, logger, DebugRenderSettings)
- DefaultDatReaderWriter(datDir) — opens its own file handles for now
  (memory cost ~50-100MB of duplicate index caches, acceptable for
  foundation work per plan Adjustment 1)
- ObjectMeshManager(graphicsDevice, dats, NullLogger)

InstancedMeshRenderer.EnsureUploaded routes through the adapter when
ACDREAM_USE_WB_FOUNDATION=1 is set; uses a WbManagedSentinel entry
in the local cache to mark "this GfxObj lives in WB now". CollectGroups
skips sentinel entries; both Draw passes skip them; Dispose skips them
(no GL resources to free — ObjectMeshManager owns those). Task 22's
WbDrawDispatcher will eventually draw WB-managed objects. With flag
off, behavior is byte-identical to before.

WbMeshAdapter constructor signature changed from (GL, DatCollection,
Logger) to (GL, string datDir, Logger). Updated tests to use
CreateUninitialized() for behavior tests and single null-GL guard test
for constructor validation. GameWindow updated to pass _datDir and to
wire _wbMeshAdapter into InstancedMeshRenderer.

AcDream.App.csproj gets direct ProjectReferences to WorldBuilder.Shared
and Chorizite.OpenGLSDLBackend — project refs are not transitive in
.NET, so AcDream.App must list them explicitly even though AcDream.Core
already references them.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
Erik 2026-05-08 13:31:30 +02:00
parent 3d111e473e
commit 4ad7a985cf
5 changed files with 137 additions and 49 deletions

View file

@ -20,6 +20,7 @@
// needs to update the shader and uniform setup at the call sites.
using System.Numerics;
using System.Runtime.InteropServices;
using AcDream.App.Rendering.Wb;
using AcDream.Core.Meshing;
using AcDream.Core.Terrain;
using AcDream.Core.World;
@ -33,6 +34,20 @@ public sealed unsafe class InstancedMeshRenderer : IDisposable
private readonly Shader _shader;
private readonly TextureCache _textures;
/// <summary>
/// Optional WB adapter. When non-null and <see cref="WbFoundationFlag.IsEnabled"/>,
/// <see cref="EnsureUploaded"/> hands the GfxObj ref to the WB pipeline instead of
/// uploading into our own VAO pool. The draw loop skips sentinel entries — Task 22's
/// WbDrawDispatcher will eventually draw them.
/// </summary>
private readonly WbMeshAdapter? _wbMeshAdapter;
// Sentinel: a GfxObj that has been handed to the WB pipeline gets this list
// stored in _gpuByGfxObj. The Draw loop recognises it by reference identity
// (object.ReferenceEquals) and skips it — no legacy VAO draw for WB-managed
// objects until Task 22 wires up WbDrawDispatcher.
private static readonly List<SubMeshGpu> WbManagedSentinel = new(0);
// One GPU bundle per unique GfxObj id. Each GfxObj can have multiple sub-meshes.
private readonly Dictionary<uint, List<SubMeshGpu>> _gpuByGfxObj = new();
@ -67,11 +82,13 @@ public sealed unsafe class InstancedMeshRenderer : IDisposable
private readonly record struct GroupKey(uint GfxObjId, ulong TextureSignature);
public InstancedMeshRenderer(GL gl, Shader shader, TextureCache textures)
public InstancedMeshRenderer(GL gl, Shader shader, TextureCache textures,
WbMeshAdapter? wbMeshAdapter = null)
{
_gl = gl;
_shader = shader;
_textures = textures;
_wbMeshAdapter = wbMeshAdapter;
_instanceVbo = _gl.GenBuffer();
}
@ -83,6 +100,17 @@ public sealed unsafe class InstancedMeshRenderer : IDisposable
if (_gpuByGfxObj.ContainsKey(gfxObjId))
return;
// Phase N.4 Task 9: when the WB foundation flag is on and we have an
// adapter, hand this GfxObj to the WB pipeline instead of uploading our
// own VAO. The sentinel entry marks "this GfxObj lives in WB now" so the
// draw loop knows to skip it. Task 22's WbDrawDispatcher will draw them.
if (WbFoundationFlag.IsEnabled && _wbMeshAdapter is not null)
{
_wbMeshAdapter.IncrementRefCount(gfxObjId);
_gpuByGfxObj[gfxObjId] = WbManagedSentinel;
return;
}
var list = new List<SubMeshGpu>(subMeshes.Count);
foreach (var sm in subMeshes)
list.Add(UploadSubMesh(sm));
@ -217,6 +245,11 @@ public sealed unsafe class InstancedMeshRenderer : IDisposable
if (!_gpuByGfxObj.TryGetValue(key.GfxObjId, out var subMeshes))
continue;
// WB-managed GfxObjs have a sentinel entry; Task 22 (WbDrawDispatcher)
// will draw them. Skip here to avoid drawing with stale/null VAO data.
if (object.ReferenceEquals(subMeshes, WbManagedSentinel))
continue;
bool hasOpaqueSubMesh = false;
foreach (var sub in subMeshes)
{
@ -292,6 +325,10 @@ public sealed unsafe class InstancedMeshRenderer : IDisposable
if (!_gpuByGfxObj.TryGetValue(key.GfxObjId, out var subMeshes))
continue;
// WB-managed GfxObjs — skip; Task 22 will draw them.
if (object.ReferenceEquals(subMeshes, WbManagedSentinel))
continue;
bool hasTranslucentSubMesh = false;
foreach (var sub in subMeshes)
{
@ -419,7 +456,10 @@ public sealed unsafe class InstancedMeshRenderer : IDisposable
foreach (var meshRef in entity.MeshRefs)
{
if (!_gpuByGfxObj.ContainsKey(meshRef.GfxObjId))
if (!_gpuByGfxObj.TryGetValue(meshRef.GfxObjId, out var cachedMeshes))
continue;
// WB-managed GfxObjs don't go through our instance pipeline.
if (object.ReferenceEquals(cachedMeshes, WbManagedSentinel))
continue;
var model = meshRef.PartTransform * entityRoot;
@ -525,6 +565,11 @@ public sealed unsafe class InstancedMeshRenderer : IDisposable
{
foreach (var subs in _gpuByGfxObj.Values)
{
// WB-managed entries use the sentinel — no GL resources to free here;
// ObjectMeshManager owns those resources.
if (object.ReferenceEquals(subs, WbManagedSentinel))
continue;
foreach (var sub in subs)
{
_gl.DeleteBuffer(sub.Vbo);