149 lines
5.9 KiB
C#
149 lines
5.9 KiB
C#
using System.Collections.Generic;
|
|
using AcDream.Core.World;
|
|
|
|
namespace AcDream.App.Rendering.Wb;
|
|
|
|
/// <summary>
|
|
/// Bridges landblock streaming events to <see cref="IWbMeshAdapter"/>'s
|
|
/// reference-count lifecycle. <b>Tier-aware by design</b>: only atlas-tier
|
|
/// entities (procedural / dat-hydrated, identified by
|
|
/// <c>ServerGuid == 0</c>) drive ref counts. Server-spawned entities
|
|
/// (per-instance tier) are skipped — those go through
|
|
/// <c>EntitySpawnAdapter</c> + <c>TextureCache.GetOrUploadWithPaletteOverride</c>
|
|
/// (see Phase N.4 spec, Architecture → Two-tier rendering split).
|
|
///
|
|
/// <para>
|
|
/// On load: walks the landblock's atlas-tier entities, collects unique
|
|
/// GfxObj ids from their <c>MeshRefs</c>, calls
|
|
/// <c>IncrementRefCount</c> per id, and pins each specialized EnvCell geometry
|
|
/// id without starting generic GfxObj decode. Snapshots both id-sets per
|
|
/// landblock so unload can match the load 1:1.
|
|
/// </para>
|
|
///
|
|
/// <para>
|
|
/// On unload: looks up both snapshots, calls <c>DecrementRefCount</c> per id,
|
|
/// drops the snapshots. Unknown / never-loaded landblocks no-op.
|
|
/// </para>
|
|
///
|
|
/// <para>
|
|
/// Idempotency: repeated notifications for the same landblock only register
|
|
/// newly-seen ids. This matters for two-tier streaming: a far-tier terrain
|
|
/// load first snapshots an empty entity set, then a later Far-to-Near promotion
|
|
/// supplies the actual stabs/buildings. Treating the second notification as a
|
|
/// blanket no-op leaves the world-state entity list populated while the WB
|
|
/// mesh cache never pins the promoted GfxObj ids.
|
|
/// </para>
|
|
///
|
|
/// <para>
|
|
/// Thread safety: the internal snapshots are intentionally not synchronized.
|
|
/// <see cref="AcDream.App.Streaming.GpuWorldState"/> invokes every load, unload, and readiness query
|
|
/// on the owning render/update thread.
|
|
/// </para>
|
|
/// </summary>
|
|
public sealed class LandblockSpawnAdapter
|
|
{
|
|
private readonly IWbMeshAdapter _adapter;
|
|
|
|
// Maps landblock id → unique GfxObj ids registered for that landblock.
|
|
// Written on load, read+cleared on unload. Single-threaded (streaming worker).
|
|
private readonly Dictionary<uint, HashSet<ulong>> _idsByLandblock = new();
|
|
// EnvCell shells are prepared through PrepareEnvCellGeomMeshDataAsync rather
|
|
// than generic GfxObj loading, but still require explicit lifetime pins.
|
|
// Keep their synthetic ids separate so registration uses the no-decode pin.
|
|
private readonly Dictionary<uint, HashSet<ulong>> _additionalReadinessIdsByLandblock = new();
|
|
|
|
public LandblockSpawnAdapter(IWbMeshAdapter adapter)
|
|
{
|
|
System.ArgumentNullException.ThrowIfNull(adapter);
|
|
_adapter = adapter;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Called when a landblock finishes streaming in or receives promoted
|
|
/// atlas-tier entities. Registers a ref-count increment with WB for each
|
|
/// unique atlas-tier GfxObj id that has not already been registered for
|
|
/// this landblock.
|
|
/// </summary>
|
|
public void OnLandblockLoaded(
|
|
LoadedLandblock landblock,
|
|
IEnumerable<ulong>? additionalReadinessIds = null)
|
|
{
|
|
System.ArgumentNullException.ThrowIfNull(landblock);
|
|
|
|
var unique = new HashSet<ulong>();
|
|
foreach (var entity in landblock.Entities)
|
|
{
|
|
// Atlas-tier filter: server-spawned entities (ServerGuid != 0)
|
|
// belong to the per-instance path and are NOT registered with WB.
|
|
if (entity.ServerGuid != 0) continue;
|
|
|
|
foreach (var meshRef in entity.MeshRefs)
|
|
unique.Add((ulong)meshRef.GfxObjId);
|
|
}
|
|
|
|
if (!_idsByLandblock.TryGetValue(landblock.LandblockId, out var registered))
|
|
{
|
|
_idsByLandblock[landblock.LandblockId] = unique;
|
|
foreach (var id in unique) _adapter.IncrementRefCount(id);
|
|
}
|
|
else
|
|
{
|
|
foreach (var id in unique)
|
|
{
|
|
if (registered.Add(id))
|
|
_adapter.IncrementRefCount(id);
|
|
}
|
|
}
|
|
|
|
if (!_additionalReadinessIdsByLandblock.TryGetValue(
|
|
landblock.LandblockId,
|
|
out var additional))
|
|
{
|
|
additional = new HashSet<ulong>();
|
|
_additionalReadinessIdsByLandblock[landblock.LandblockId] = additional;
|
|
}
|
|
if (additionalReadinessIds is not null)
|
|
{
|
|
foreach (var id in additionalReadinessIds)
|
|
if (additional.Add(id))
|
|
_adapter.PinPreparedRenderData(id);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// True only after every render mesh required by this published landblock
|
|
/// is drawable. This includes both ref-counted static GfxObjs and EnvCell
|
|
/// shell geometry prepared by the independent indoor pipeline.
|
|
/// </summary>
|
|
public bool IsLandblockRenderReady(uint landblockId)
|
|
{
|
|
if (!_idsByLandblock.TryGetValue(landblockId, out var registered)
|
|
|| !_additionalReadinessIdsByLandblock.TryGetValue(landblockId, out var additional))
|
|
{
|
|
return false;
|
|
}
|
|
|
|
foreach (var id in registered)
|
|
if (!_adapter.IsRenderDataReady(id))
|
|
return false;
|
|
foreach (var id in additional)
|
|
if (!_adapter.IsRenderDataReady(id))
|
|
return false;
|
|
return true;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Called when a landblock is unloaded from the streaming window.
|
|
/// Releases the ref-count for every GfxObj id that was registered on load.
|
|
/// Unknown landblock ids (never loaded, or already unloaded) are no-ops.
|
|
/// </summary>
|
|
public void OnLandblockUnloaded(uint landblockId)
|
|
{
|
|
if (!_idsByLandblock.TryGetValue(landblockId, out var unique)) return;
|
|
foreach (var id in unique) _adapter.DecrementRefCount(id);
|
|
if (_additionalReadinessIdsByLandblock.TryGetValue(landblockId, out var additional))
|
|
foreach (var id in additional) _adapter.DecrementRefCount(id);
|
|
_idsByLandblock.Remove(landblockId);
|
|
_additionalReadinessIdsByLandblock.Remove(landblockId);
|
|
}
|
|
}
|