acdream/src/AcDream.App/Rendering/Wb/ManagedGLUniformBuffer.cs
Erik 4cc38805b5 feat(O-T3): extract GL infrastructure to AcDream.App
Phase O Task 3 — verbatim-copy GL infra from Chorizite.OpenGLSDLBackend
into src/AcDream.App/Rendering/Wb/ (namespace AcDream.App.Rendering.Wb).

18 files extracted (all namespace-changed; no algorithm changes):
  OpenGLGraphicsDevice, ManagedGLTexture, ManagedGLTextureArray,
  ManagedGLVertexBuffer, ManagedGLIndexBuffer, ManagedGLVertexArray,
  ManagedGLFrameBuffer, ManagedGLUniformBuffer, GLSLShader, GLHelpers,
  GLStateScope, GpuMemoryTracker, SceneData, DebugRenderSettings,
  TextureParameters, TextureFormatExtensions, BufferUsageExtensions,
  EmbeddedResourceReader.

3 internals promoted to public (O-D9):
  EmbeddedResourceReader, TextureFormatExtensions, BufferUsageExtensions.

SixLabors.ImageSharp not reachable: TextureHelpers was placed in
AcDream.Core (no GL/ImageSharp dep); only the GL types went to App.

TextureHelpers.GetCompressedLayerSize added to AcDream.Core.Rendering.Wb
(was in Chorizite.OpenGLSDLBackend.Lib.TextureHelpers; uses
Chorizite.Core.Render.Enums.TextureFormat which Core gets transitively
via the still-present WB project refs).

T3/T4 boundary interims:
  - WbMeshAdapter._graphicsDevice stays Chorizite.OpenGLSDLBackend.OpenGLGraphicsDevice
    (T4 will swap it when ObjectMeshManager is extracted).
  - OpenGLGraphicsDevice.ParticleBatcher deferred to null! (T4 extracts
    ParticleBatcher alongside ObjectMeshManager; can't pass `this` of our
    new type to the WB-original ctor before T4).
  - ManagedGLTextureArray uses our TextureHelpers via explicit alias.
  - IUniformBuffer is in Chorizite.Core.dll under Chorizite.OpenGLSDLBackend
    namespace (unusual packaging); resolved via type alias.
  - AcDream.App.csproj gets explicit Chorizite.Core 0.0.18 PackageReference
    (IUniformBuffer + other Chorizite.Core types now used directly in App).

Build green. Test baseline 1147+8 maintained (1902 passing, 8 pre-existing
MotionInterpreterTests failures unrelated to T3).

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-21 16:00:31 +02:00

143 lines
5.4 KiB
C#

using Chorizite.Core.Render;
using Chorizite.Core.Render.Enums;
using Silk.NET.OpenGL;
using System.Runtime.InteropServices;
using BufferUsage = Chorizite.Core.Render.Enums.BufferUsage;
// IUniformBuffer is in Chorizite.Core.dll but under the Chorizite.OpenGLSDLBackend namespace
using IUniformBuffer = Chorizite.OpenGLSDLBackend.IUniformBuffer;
namespace AcDream.App.Rendering.Wb {
/// <summary>
/// OpenGL uniform buffer
/// </summary>
public unsafe class ManagedGLUniformBuffer : IUniformBuffer {
private uint bufferId;
private readonly OpenGLGraphicsDevice _device;
private GL GL => _device.GL;
/// <inheritdoc />
public int Size { get; private set; }
/// <inheritdoc />
public BufferUsage Usage { get; private set; }
/// <summary>
/// Initializes a new instance of the <see cref="ManagedGLUniformBuffer"/> class.
/// </summary>
/// <param name="device">Graphics device</param>
/// <param name="usage">Buffer usage</param>
/// <param name="size">The size of the buffer, in bytes</param>
public unsafe ManagedGLUniformBuffer(OpenGLGraphicsDevice device, BufferUsage usage, int size) {
_device = device;
Size = size;
Usage = usage;
// Generate the buffer
bufferId = GL.GenBuffer();
if (bufferId == 0) {
throw new Exception("Failed to generate uniform buffer.");
}
GpuMemoryTracker.TrackResourceAllocation(GpuResourceType.Buffer);
GLHelpers.CheckErrors(GL);
// Allocate the buffer with the specified size
GL.BindBuffer(GLEnum.UniformBuffer, bufferId);
GLHelpers.CheckErrors(GL);
GL.BufferData(
GLEnum.UniformBuffer,
(uint)Size,
(void*)0, // No initial data
GLEnum.DynamicDraw);
GLHelpers.CheckErrors(GL);
GpuMemoryTracker.TrackAllocation(Size, GpuResourceType.Buffer);
}
/// <inheritdoc />
public unsafe void SetData<T>(T[] data) where T : unmanaged {
SetData(data.AsSpan());
}
/// <inheritdoc />
public unsafe void SetData<T>(Span<T> data) where T : unmanaged {
uint dataSize = (uint)data.Length * (uint)Marshal.SizeOf<T>();
// Ensure the buffer size is sufficient
if (dataSize > Size) {
throw new ArgumentException($"Data size ({dataSize} bytes) exceeds buffer size ({Size} bytes).");
}
GL.BindBuffer(GLEnum.UniformBuffer, bufferId);
fixed (T* ptr = data) {
GL.BufferSubData(GLEnum.UniformBuffer, 0, (nuint)dataSize, ptr);
}
}
/// <inheritdoc />
public unsafe void SetSubData<T>(T[] data, int destinationOffsetBytes, int sourceOffsetElements = 0, int lengthElements = 0) where T : unmanaged {
SetSubData(data.AsSpan(), destinationOffsetBytes, sourceOffsetElements, lengthElements);
}
/// <inheritdoc />
public unsafe void SetSubData<T>(Span<T> data, int destinationOffsetBytes, int sourceOffsetElements = 0, int lengthElements = 0) where T : unmanaged {
if (lengthElements <= 0) {
lengthElements = data.Length - sourceOffsetElements;
}
uint dataSizeBytes = (uint)lengthElements * (uint)Marshal.SizeOf<T>();
// Validate buffer bounds
if (destinationOffsetBytes + dataSizeBytes > Size) {
throw new ArgumentException($"Update would exceed buffer size. Buffer size: {Size}, Update range: {destinationOffsetBytes} to {destinationOffsetBytes + dataSizeBytes}");
}
GL.BindBuffer(GLEnum.UniformBuffer, bufferId);
fixed (T* ptr = data.Slice(sourceOffsetElements, lengthElements)) {
GL.BufferSubData(GLEnum.UniformBuffer, (nint)destinationOffsetBytes, (nuint)dataSizeBytes, ptr);
}
}
/// <summary>
/// Sets a single piece of data in the buffer.
/// </summary>
public unsafe void SetData<T>(ref T data) where T : unmanaged {
fixed (T* pData = &data) {
SetData(new Span<T>(pData, 1));
}
}
/// <summary>
/// Binds the buffer to the specified binding point.
/// </summary>
/// <param name="bindingPoint">The binding point to bind to</param>
public void Bind(uint bindingPoint) {
GL.BindBufferBase(GLEnum.UniformBuffer, bindingPoint, bufferId);
GLHelpers.CheckErrors(GL);
}
/// <inheritdoc />
public void Bind() {
GL.BindBuffer(GLEnum.UniformBuffer, bufferId);
GLHelpers.CheckErrors(GL);
}
/// <inheritdoc />
public void Unbind() {
GL.BindBuffer(GLEnum.UniformBuffer, 0);
GLHelpers.CheckErrors(GL);
}
public void Dispose() {
_device.QueueGLAction(GL => {
if (bufferId != 0) {
GL.DeleteBuffer(bufferId);
GpuMemoryTracker.TrackResourceDeallocation(GpuResourceType.Buffer);
GLHelpers.CheckErrors(GL);
GpuMemoryTracker.TrackDeallocation(Size, GpuResourceType.Buffer);
bufferId = 0;
}
});
}
}
}