From 42f8f863bd479071732c6610f46a4657d3966ef2 Mon Sep 17 00:00:00 2001 From: Space V <40030799+spacevx@users.noreply.github.com> Date: Thu, 12 Nov 2026 08:28:18 +0100 Subject: [PATCH] Persistent ring for dynamic index buffers --- Source.ShaderAPI.Gl46/IndexBufferGl46.cs | 57 +++++++++++++++++++++--- 1 file changed, 51 insertions(+), 6 deletions(-) diff --git a/Source.ShaderAPI.Gl46/IndexBufferGl46.cs b/Source.ShaderAPI.Gl46/IndexBufferGl46.cs index 13d93e76..9caa82d9 100644 --- a/Source.ShaderAPI.Gl46/IndexBufferGl46.cs +++ b/Source.ShaderAPI.Gl46/IndexBufferGl46.cs @@ -22,7 +22,20 @@ public unsafe class IndexBufferGl46 : IDisposable int ibo = -1; int lastBufferSize = -1; int LockedPosition; + PersistentBufferRing? ring; + bool Written; + public unsafe void RecomputeIBO() { + if (Dynamic) { + if (ring == null || BufferSize > lastBufferSize) { + lastBufferSize = BufferSize; + ring?.Dispose(); + ring = new PersistentBufferRing(BufferSize); + Written = false; + } + return; + } + // Create the IBO if it doesn't exist if (ibo == -1) ibo = (int)glCreateBuffer(); @@ -72,6 +85,20 @@ public unsafe void RecomputeIBO() { position = firstIndex; startIndex = position; + if (Dynamic) { + if (ring == null) + RecomputeIBO(); + else if (discard && Written) + ring.Advance(); + + if (discard) + Written = false; + + Locked = true; + LockedPosition = position; + return (short*)ring!.Memory + position; + } + if (SysmemBuffer == null) { RecomputeIBO(); } @@ -87,8 +114,12 @@ public void Unlock(int indexCount) { if (!Locked) return; - if (indexCount > 0) - glNamedBufferSubData((uint)ibo, LockedPosition * 2, indexCount * 2, (short*)SysmemBuffer + LockedPosition); + if (indexCount > 0) { + if (Dynamic) + Written = true; + else + glNamedBufferSubData((uint)ibo, LockedPosition * 2, indexCount * 2, (short*)SysmemBuffer + LockedPosition); + } Position += indexCount; Locked = false; } @@ -96,10 +127,18 @@ public void Unlock(int indexCount) { public short* ModifyLock(int firstIndex, int indexCount, out int startIndex) { Assert(!Locked); + startIndex = firstIndex; + if (Dynamic) { + if (ring == null) + RecomputeIBO(); + + Locked = true; + return (short*)ring!.Memory + firstIndex; + } + if (SysmemBuffer == null) RecomputeIBO(); - startIndex = firstIndex; Locked = true; return (short*)SysmemBuffer + firstIndex; } @@ -108,8 +147,12 @@ public void ModifyUnlock(int firstIndex, int indexCount) { if (!Locked) return; - if (indexCount > 0) - glNamedBufferSubData((uint)ibo, firstIndex * 2, indexCount * 2, (short*)SysmemBuffer + firstIndex); + if (indexCount > 0) { + if (Dynamic) + Written = true; + else + glNamedBufferSubData((uint)ibo, firstIndex * 2, indexCount * 2, (short*)SysmemBuffer + firstIndex); + } Locked = false; } @@ -118,6 +161,8 @@ internal bool HasEnoughRoom(int indices) { } public void Dispose() { + ring?.Dispose(); + ring = null; if (ibo != -1) { Assert(SysmemBuffer != null); fixed (int* ugh = &ibo) @@ -147,5 +192,5 @@ internal void HandleLateCreation() { } - internal uint IBO() => ibo > 0 ? (uint)ibo : throw new NullReferenceException("Index Buffer Object was null"); + internal uint IBO() => ring != null ? ring.Buffer : ibo > 0 ? (uint)ibo : throw new NullReferenceException("Index Buffer Object was null"); }