Implements GlGpuDevice and the rest of AcDream.App.Rendering.Gpu.Gl,
filling the V0-pinned IGpuDevice contract on OpenGL 4.3. This is the
first of the port slices described in
docs/plans/2026-07-27-vulkan-campaign.md: every later renderer port
(V2 onward) needs a real, driver-proven GL implementation of the RHI
to port onto, and the GL backend is deliberately built to be
behaviour-preserving rather than optimal, because that is what turns
each subsequent slice's pixel gate into a strict identity check
instead of a moving target. The Vulkan backend (V5+) is where the
actual efficiency gains land.
GlGpuDevice is a fresh root, not derived from Chorizite's
BaseGraphicsDevice/OpenGLGraphicsDevice - shedding that inheritance is
one of the things this campaign explicitly does. It owns its own
BindlessSupport instance rather than sharing the legacy WB render
path's, which is what lets it be constructed the moment a GL context
and a GpuFrameFlightController exist, with no dependency on when
WorldRenderCompositionPhase happens to detect bindless support later
in startup. The ring buffer keeps a managed staging array plus a real
GL buffer per flight slot and flushes with one BufferSubData
immediately before each Draw/DrawIndexed/MultiDrawIndexedIndirect
(never at bind time, since a renderer may still write after binding);
V1 throws on an over-capacity ring request rather than growing it,
since nothing consumes the device yet and a silent grow would hide a
future renderer's real working set. The texture table is a bump/free-
list allocator over a managed uvec2 handle array, gated through the
frame-flight retirement queue so a released slot cannot be reused
while a submitted frame might still read it. Push constants are
applied by uniform name on the currently-bound program, cached per
program, and explicitly re-applied whenever BindPipeline switches
programs - GL uniforms are per-program state, so the "survives
pipeline changes within a pass" guarantee the interface documents (a
freebie on Vulkan's shared pipeline layout) has to be emulated here.
BindlessSupport gained one additive method,
GetResidentHandle(texture, sampler), calling the same
ArbBindlessTexture.GetTextureSamplerHandle entry point
ManagedGLTextureArray already uses through a different path. The
existing GetResidentHandle(texture) cannot express
IGpuDevice.RegisterTexture's documented pair semantics ("the same
texture registered with two samplers occupies two slots"), so this
was the minimal change needed rather than a workaround.
The pure bookkeeping - ring watermark/alignment arithmetic, the
texture-slot allocator, render-state diffing, the push-constant field-
to-uniform-name table, and GL format mapping - lives in small GL-free
classes so it is unit-testable without a live context, following the
same seam pattern GpuFrameFlightController already uses for its fence
API. GlGpuTimerPool follows suit with an injectable timer-query API.
The device is constructed in HostInputCameraCompositionPhase
immediately after the frame-flight controller (the same phase that
already builds GpuFrameFlightController), rather than in
WorldRenderCompositionPhase as first considered: GlGpuDevice's self-
contained bindless detection means it has no ordering dependency on
the legacy WB path's BindlessSupport, so it can be proven against the
real driver as early as possible while keeping the composition change
to one phase. Composition, publication, and shutdown wiring follow
the existing acquire/publish/fault-injection pattern exactly, and GPU
device disposal is scheduled through the frame-flight retirement queue
before that queue itself is torn down. Nothing consumes the device
yet - that starts at V4a - so this slice's pixel gate is trivially a
tripwire.
App tests: 3834 passed / 3 skipped (V0 baseline 3785 + 49 new: ring,
texture-slot, render-state, push-constant, format-mapping, enum-
mapping, and timer-pool tests, plus one new fault-injection point in
the existing composition theory).
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
72 lines
3.2 KiB
C#
72 lines
3.2 KiB
C#
using System.Reflection;
|
|
|
|
namespace AcDream.App.Rendering.Gpu.Gl;
|
|
|
|
/// <summary>
|
|
/// The single source of truth mapping each <see cref="GpuPushConstants"/>
|
|
/// field to the GLSL uniform name the GL backend binds it to — the names
|
|
/// documented on the fields themselves. <see cref="GlGpuPushConstantBinder"/>
|
|
/// reads these constants (not string literals of its own) when it calls
|
|
/// <c>GL.GetUniformLocation</c>, and <see cref="AssertMapsEveryField"/> lets a
|
|
/// test prove the table cannot silently drop a field that a later slice adds
|
|
/// to the shared struct.
|
|
/// </summary>
|
|
internal static class GlPushConstantUniformNames
|
|
{
|
|
public const string ViewProjection = "uViewProjection";
|
|
public const string DrawIdOffset = "uDrawIDOffset";
|
|
public const string LightingMode = "uLightingMode";
|
|
public const string RenderPass = "uRenderPass";
|
|
public const string LightDebug = "uLightDebug";
|
|
public const string TextureIndexA = "uTextureIndexA";
|
|
public const string TextureIndexB = "uTextureIndexB";
|
|
public const string ParamA = "uParamA";
|
|
public const string ParamB = "uParamB";
|
|
|
|
/// <summary>
|
|
/// Field name (as declared on <see cref="GpuPushConstants"/>) to GLSL
|
|
/// uniform name, built from the same constants the binder applies with —
|
|
/// so the binder and this completeness table can never disagree.
|
|
/// </summary>
|
|
public static IReadOnlyDictionary<string, string> ByFieldName { get; } =
|
|
new Dictionary<string, string>
|
|
{
|
|
[nameof(GpuPushConstants.ViewProjection)] = ViewProjection,
|
|
[nameof(GpuPushConstants.DrawIdOffset)] = DrawIdOffset,
|
|
[nameof(GpuPushConstants.LightingMode)] = LightingMode,
|
|
[nameof(GpuPushConstants.RenderPass)] = RenderPass,
|
|
[nameof(GpuPushConstants.LightDebug)] = LightDebug,
|
|
[nameof(GpuPushConstants.TextureIndexA)] = TextureIndexA,
|
|
[nameof(GpuPushConstants.TextureIndexB)] = TextureIndexB,
|
|
[nameof(GpuPushConstants.ParamA)] = ParamA,
|
|
[nameof(GpuPushConstants.ParamB)] = ParamB,
|
|
};
|
|
|
|
/// <summary>
|
|
/// Throws if <see cref="GpuPushConstants"/> declares a public instance
|
|
/// field this table does not map — the drift guard the campaign spec asks
|
|
/// for. Called from a unit test, not from production code.
|
|
/// </summary>
|
|
internal static void AssertMapsEveryField()
|
|
{
|
|
FieldInfo[] fields = typeof(GpuPushConstants).GetFields(
|
|
BindingFlags.Public | BindingFlags.Instance);
|
|
foreach (FieldInfo field in fields)
|
|
{
|
|
if (!ByFieldName.ContainsKey(field.Name))
|
|
{
|
|
throw new InvalidOperationException(
|
|
$"GpuPushConstants.{field.Name} has no mapped GLSL uniform name in " +
|
|
$"{nameof(GlPushConstantUniformNames)}.");
|
|
}
|
|
}
|
|
|
|
if (ByFieldName.Count != fields.Length)
|
|
{
|
|
throw new InvalidOperationException(
|
|
$"{nameof(GlPushConstantUniformNames)} maps {ByFieldName.Count} names but " +
|
|
$"{nameof(GpuPushConstants)} declares {fields.Length} fields — a stale entry " +
|
|
"survives a field rename or removal.");
|
|
}
|
|
}
|
|
}
|