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

83 lines
3.5 KiB
C#

using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
namespace AcDream.App.Rendering.Wb {
/// <summary>
/// Resource types for GPU memory tracking.
/// </summary>
public enum GpuResourceType {
Texture,
Buffer,
Shader,
VAO,
FBO,
RBO,
Other
}
/// <summary>
/// Details about a GPU resource type.
/// </summary>
public record GpuResourceDetails(GpuResourceType Type, int Count, long Bytes);
/// <summary>
/// Details about a specific named buffer.
/// </summary>
public record NamedBufferDetails(string Name, long CapacityBytes, long UsedBytes);
/// <summary>
/// Tracks manual VRAM allocations for buffers and textures.
/// </summary>
public static class GpuMemoryTracker {
private static long _allocatedBytes;
private static readonly long[] _allocatedBytesByType = new long[Enum.GetValues<GpuResourceType>().Length];
private static readonly int[] _resourceCountsByType = new int[Enum.GetValues<GpuResourceType>().Length];
private static readonly ConcurrentDictionary<string, NamedBufferDetails> _namedBuffers = new();
public static long AllocatedBytes => Interlocked.Read(ref _allocatedBytes);
public static int VaoCount => _resourceCountsByType[(int)GpuResourceType.VAO];
public static int ShaderCount => _resourceCountsByType[(int)GpuResourceType.Shader];
public static int BufferCount => _resourceCountsByType[(int)GpuResourceType.Buffer];
public static int TextureCount => _resourceCountsByType[(int)GpuResourceType.Texture];
public static int FboCount => _resourceCountsByType[(int)GpuResourceType.FBO];
public static int RboCount => _resourceCountsByType[(int)GpuResourceType.RBO];
public static void TrackAllocation(long sizeInBytes, GpuResourceType type = GpuResourceType.Other) {
Interlocked.Add(ref _allocatedBytes, sizeInBytes);
Interlocked.Add(ref _allocatedBytesByType[(int)type], sizeInBytes);
}
public static void TrackDeallocation(long sizeInBytes, GpuResourceType type = GpuResourceType.Other) {
Interlocked.Add(ref _allocatedBytes, -sizeInBytes);
Interlocked.Add(ref _allocatedBytesByType[(int)type], -sizeInBytes);
}
public static void TrackResourceAllocation(GpuResourceType type) => Interlocked.Increment(ref _resourceCountsByType[(int)type]);
public static void TrackResourceDeallocation(GpuResourceType type) => Interlocked.Decrement(ref _resourceCountsByType[(int)type]);
public static void TrackNamedBuffer(string name, long capacityBytes, long usedBytes) {
_namedBuffers[name] = new NamedBufferDetails(name, capacityBytes, usedBytes);
}
public static void UntrackNamedBuffer(string name) {
_namedBuffers.TryRemove(name, out _);
}
public static IEnumerable<NamedBufferDetails> GetNamedBufferDetails() => _namedBuffers.Values.OrderBy(b => b.Name);
public static IEnumerable<GpuResourceDetails> GetDetails() {
var types = Enum.GetValues<GpuResourceType>();
foreach (var type in types) {
yield return new GpuResourceDetails(
type,
_resourceCountsByType[(int)type],
Interlocked.Read(ref _allocatedBytesByType[(int)type])
);
}
}
}
}