acdream/src/AcDream.App/Rendering/Wb/ManagedGLVertexArray.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

77 lines
2.8 KiB
C#

using Chorizite.Core.Render.Enums;
using Chorizite.Core.Render.Vertex;
using Silk.NET.OpenGL;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using VertexAttribType = Silk.NET.OpenGL.VertexAttribType;
namespace AcDream.App.Rendering.Wb {
public unsafe class ManagedGLVertexArray : IVertexArray {
private readonly OpenGLGraphicsDevice _device;
private GL GL => _device.GL;
private uint _vaoId = 0;
public ManagedGLVertexArray(OpenGLGraphicsDevice device, IVertexBuffer buffer, VertexFormat format) {
_device = device;
// Generate the vertex array
_vaoId = GL.GenVertexArray();
GLHelpers.CheckErrors(GL);
if (_vaoId == 0) {
throw new Exception("Failed to generate vertex array.");
}
GpuMemoryTracker.TrackResourceAllocation(GpuResourceType.VAO);
SetVertexBuffer(buffer, format);
}
public void SetVertexBuffer(IVertexBuffer buffer, VertexFormat format) {
GL.BindVertexArray(_vaoId);
GLHelpers.CheckErrors(GL);
buffer.Bind();
for (int i = 0; i < format.Attributes.Length; i++) {
var attr = format.Attributes[i];
GL.EnableVertexAttribArray((uint)i);
GLHelpers.CheckErrors(GL);
GL.VertexAttribPointer((uint)i, attr.Size, Convert(attr.Type), attr.Normalized, (uint)format.Stride, attr.Offset);
GLHelpers.CheckErrors(GL);
}
GL.BindVertexArray(0);
GLHelpers.CheckErrors(GL);
}
private GLEnum Convert(Chorizite.Core.Render.Enums.VertexAttribType type) => type switch {
Chorizite.Core.Render.Enums.VertexAttribType.Float => GLEnum.Float,
Chorizite.Core.Render.Enums.VertexAttribType.Int => GLEnum.Int,
Chorizite.Core.Render.Enums.VertexAttribType.UnsignedInt => GLEnum.UnsignedInt,
Chorizite.Core.Render.Enums.VertexAttribType.UnsignedByte => GLEnum.UnsignedByte,
Chorizite.Core.Render.Enums.VertexAttribType.Byte => GLEnum.Byte,
_ => throw new NotSupportedException()
};
public void Bind() {
GL.BindVertexArray(_vaoId);
GLHelpers.CheckErrors(GL);
}
public void Unbind() {
GL.BindVertexArray(0);
GLHelpers.CheckErrors(GL);
}
public void Dispose() {
_device.QueueGLAction(GL => {
if (_vaoId != 0) {
GL.DeleteVertexArray(_vaoId);
GpuMemoryTracker.TrackResourceDeallocation(GpuResourceType.VAO);
_vaoId = 0;
}
GLHelpers.CheckErrors(GL);
});
}
}
}