fix(rendering): bound portal resource lifetime

Separate logical ownership, render publication, and GPU retirement across live entities, landblocks, particles, textures, mesh arenas, portal/UI teardown, and per-frame scratch storage. Add bounded DAT/texture caches, upload budgets, three-frame fence retirement, exact-incarnation appearance reconciliation, frame pacing, and extensive lifetime conformance coverage.\n\nThe seven-destination connected route now cuts peak working/private memory roughly in half, returns Caul to 125-153 FPS locally, and produces no WER or AMD reset.\n\nCo-authored-by: OpenAI Codex <codex@openai.com>
This commit is contained in:
Erik 2026-07-18 21:35:16 +02:00
parent 3971997689
commit 749e8ceeb1
225 changed files with 29107 additions and 3914 deletions

View file

@ -1,5 +1,6 @@
using System;
using System.Runtime.InteropServices;
using AcDream.App.Rendering.Wb;
using AcDream.Core.Lighting;
using Silk.NET.OpenGL;
@ -23,27 +24,70 @@ namespace AcDream.App.Rendering;
public sealed unsafe class SceneLightingUboBinding : IDisposable
{
private readonly GL _gl;
private readonly uint _ubo;
private uint _ubo;
private readonly List<uint>[] _buffersByFrame = [[], [], []];
private int _frameSlot;
private int _bufferCursor;
private bool _frameStarted;
private bool _disposed;
internal int DynamicBufferCount => _buffersByFrame.Sum(buffers => buffers.Count);
public SceneLightingUboBinding(GL gl)
{
_gl = gl ?? throw new ArgumentNullException(nameof(gl));
_ubo = _gl.GenBuffer();
_gl.BindBuffer(BufferTargetARB.UniformBuffer, _ubo);
// Pre-allocate with the final size; BufferSubData each frame.
_gl.BufferData(
BufferTargetARB.UniformBuffer,
(nuint)SceneLightingUbo.SizeInBytes,
(void*)0,
BufferUsageARB.DynamicDraw);
_gl.BindBuffer(BufferTargetARB.UniformBuffer, 0);
}
// Bind the buffer to the chosen binding point exactly once — shaders
// that declare this binding in their layout block will read from it
// on every draw without further intervention.
_gl.BindBufferBase(BufferTargetARB.UniformBuffer,
SceneLightingUbo.BindingPoint, _ubo);
/// <summary>
/// Resets the lighting-submission cursor for a GPU-fenced frame slot.
/// World, portal-space, and paperdoll draws can each upload different
/// lighting in one frame, so every upload receives distinct storage.
/// </summary>
public void BeginFrame(int frameSlot)
{
if ((uint)frameSlot >= (uint)_buffersByFrame.Length)
throw new ArgumentOutOfRangeException(nameof(frameSlot));
_frameSlot = frameSlot;
_bufferCursor = 0;
_frameStarted = true;
}
private void ActivateNextBuffer()
{
if (!_frameStarted)
throw new InvalidOperationException("BeginFrame must be called before uploading scene lighting.");
List<uint> buffers = _buffersByFrame[_frameSlot];
if (_bufferCursor == buffers.Count)
{
uint buffer = TrackedGlResource.CreateBuffer(
_gl,
"SceneLighting frame UBO creation");
try
{
TrackedGlResource.AllocateBufferStorage(
_gl,
GLEnum.UniformBuffer,
buffer,
0,
SceneLightingUbo.SizeInBytes,
GLEnum.DynamicDraw,
"SceneLighting frame UBO allocation");
buffers.Add(buffer);
}
catch
{
TrackedGlResource.DeleteBuffer(
_gl,
buffer,
0,
"SceneLighting frame UBO rollback");
throw;
}
}
_ubo = buffers[_bufferCursor++];
}
/// <summary>
@ -52,16 +96,30 @@ public sealed unsafe class SceneLightingUboBinding : IDisposable
/// </summary>
public void Upload(SceneLightingUbo data)
{
ActivateNextBuffer();
_gl.BindBuffer(BufferTargetARB.UniformBuffer, _ubo);
_gl.BufferSubData(BufferTargetARB.UniformBuffer,
(nint)0, (nuint)SceneLightingUbo.SizeInBytes, &data);
_gl.BindBufferBase(BufferTargetARB.UniformBuffer,
SceneLightingUbo.BindingPoint, _ubo);
_gl.BindBuffer(BufferTargetARB.UniformBuffer, 0);
}
public void Dispose()
{
if (_disposed) return;
_gl.DeleteBuffer(_ubo);
foreach (List<uint> buffers in _buffersByFrame)
{
foreach (uint buffer in buffers)
{
TrackedGlResource.DeleteBuffer(
_gl,
buffer,
SceneLightingUbo.SizeInBytes,
"SceneLighting frame UBO disposal");
}
buffers.Clear();
}
_disposed = true;
}
}