acdream/src/AcDream.App/Platform/GraphicalWindowBackendSelection.cs
Erik 8a7a0837e1 feat(render): Vulkan campaign V11 step 2 — delete the OpenGL backend
Vulkan is the sole, user-signed-off backend (V10 landed) and step 1
already removed ImGui/Studio/DevTools. This step deletes the GL
rendering backend itself: every Gpu/Gl/** implementation, the Wb
ManagedGL*/GLHelpers/GLSLShader/GLStateScope/RenderStateCache/
BindlessSupport family, Shader/ShaderProgramConstruction/SamplerCache,
RenderBootstrap, and RenderFrameGlStateController.

GameWindow.cs's Run()/CreateGraphics()/CreateBackbufferReader()/
OnLoad() collapse to their Vulkan-only arm; GameWindowGraphics loses
its OpenGlGameWindowGraphics subclass. RuntimeOptions.RenderBackend and
RenderBackendKind (incl. the Gl member of GpuBackendKind) are gone —
there is nothing left to select between. The five world-draw dual-arm
renderers (WbDrawDispatcher, EnvCellRenderer, TerrainModernRenderer,
ParticleRenderer, SkyRenderer) and the composition roots
(WorldRenderComposition, HostInputCameraComposition,
LivePresentationComposition, FrameRootComposition) collapse to their
RHI-only arm. GL-only diagnostic properties with a live external reader
(DynamicBufferCount and friends) simplify to a documented `=> 0`/no-op
rather than disappearing, since the reader is out of this commit's
scope.

A few GL-flavored mechanisms turned out to be backend-neutral once
isolated: GlConstructionCleanupLedger is renamed
ResourceConstructionCleanupLedger (exception-chain walking has nothing
to do with GL), and GlfwNativePlatformProbe moved out of the otherwise
GL-only GraphicalCapabilityRecord.cs into
GraphicalWindowBackendSelection.cs before the rest of that file was
deleted.

Test files with no surviving subject are deleted outright
(GraphicalCapabilityRequirementsTests, ShaderProgramConstructionTests,
PortalDepthShaderParityTests, TextureCacheBindlessTests,
TextRendererFailureSafetyTests, ClipFrameUploadTests, every
Gpu/Gl/*Tests, GlTextureOwnershipTests, RenderFrameGlStateControllerTests);
others get their dead GL-only members trimmed while their live
assertions stay (ClipFrameLayoutTests' MeshClipSsboBinding check now
reads GpuBindingModel.StorageClipRegions, the same binding index under
its new backend-neutral name; GpuResourceRetirementTransactionTests
drops its OpenGLGraphicsDevice-subclassing test double and the two GL
queue tests it existed for). EnvCellRendererTests' construction helper
now builds a real ObjectMeshManager via VulkanMeshPipelineDevice
instead of passing null through a null-forgiving operator, since the
RHI constructor never tolerated a null mesh manager and the old GL
constructor (which did) is gone.

Deferred to the next two steps, deliberately not touched here: the
Silk.NET.OpenGL/.Extensions.ARB package references, IMeshPipelineDevice.Gl
(WbMeshAdapter's GL? threading stays in place), Chorizite.Core's stale
csproj comment (the package itself is still load-bearing —
TextureFormat and friends are used well beyond the deleted
ManagedGLUniformBuffer), and the CI/gate scripts.

Build: `dotnet build AcDream.slnx -c Release` — 0 warnings, 0 errors.
Tests: full-solution `dotnet test` green across every project
(App.Tests 3937/3940 + 3 skips, Core.Tests 3296/3298 + 2 skips, all
others 100%); the 2 App.Tests names that flake under full-suite
parallel execution (#250-family, documented pre-existing) pass in
isolation.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-07-29 02:19:53 +02:00

244 lines
8.5 KiB
C#

using Silk.NET.GLFW;
using Silk.NET.Core.Loader;
using Silk.NET.Windowing.Glfw;
namespace AcDream.App.Platform;
internal enum GraphicalDisplayProtocol
{
Unknown,
Windows,
X11,
Wayland,
Automatic,
}
/// <summary>
/// Immutable process-start decision for Silk's GLFW 3.4 backend. Linux users
/// may force <c>x11</c> or <c>wayland</c> with
/// <c>ACDREAM_DISPLAY_PROTOCOL</c>; otherwise GLFW selects from the available
/// native backends. The decision is applied once before any window exists.
/// </summary>
internal sealed record GraphicalWindowBackendSelection(
GraphicalDisplayProtocol RequestedProtocol,
string Reason)
{
internal const string EnvironmentVariable =
"ACDREAM_DISPLAY_PROTOCOL";
internal static GraphicalWindowBackendSelection Resolve(
GraphicalHostOperatingSystem operatingSystem,
Func<string, string?> environment)
{
ArgumentNullException.ThrowIfNull(environment);
if (operatingSystem == GraphicalHostOperatingSystem.Windows)
{
return new(
GraphicalDisplayProtocol.Windows,
"Windows graphical host");
}
string? configured = environment(EnvironmentVariable);
if (!string.IsNullOrWhiteSpace(configured))
{
return configured.Trim().ToLowerInvariant() switch
{
"auto" => new(
GraphicalDisplayProtocol.Automatic,
$"{EnvironmentVariable}=auto"),
"x11" => new(
GraphicalDisplayProtocol.X11,
$"{EnvironmentVariable}=x11"),
"wayland" => new(
GraphicalDisplayProtocol.Wayland,
$"{EnvironmentVariable}=wayland"),
_ => throw new InvalidOperationException(
$"{EnvironmentVariable} must be auto, x11, or wayland; " +
$"received '{configured}'."),
};
}
string? sessionType = environment("XDG_SESSION_TYPE");
string? waylandDisplay = environment("WAYLAND_DISPLAY");
if (string.Equals(
sessionType,
"wayland",
StringComparison.OrdinalIgnoreCase)
&& !string.IsNullOrWhiteSpace(waylandDisplay))
{
return new(
GraphicalDisplayProtocol.Wayland,
"XDG_SESSION_TYPE=wayland and WAYLAND_DISPLAY is set");
}
string? xDisplay = environment("DISPLAY");
if (!string.IsNullOrWhiteSpace(xDisplay))
{
return new(
GraphicalDisplayProtocol.X11,
"DISPLAY is set");
}
if (!string.IsNullOrWhiteSpace(waylandDisplay))
{
return new(
GraphicalDisplayProtocol.Wayland,
"WAYLAND_DISPLAY is set");
}
return new(
GraphicalDisplayProtocol.Automatic,
"no X11 or Wayland environment was detected");
}
}
/// <summary>
/// The only App-layer owner allowed to select a native GLFW platform. GLFW
/// requires the platform init hint before <c>glfwInit</c>; applying it after
/// <see cref="Silk.NET.Windowing.Window.Create"/> is too late.
/// </summary>
internal static class GraphicalWindowBackendConfigurator
{
private const int GlfwPlatformInitHint = 0x00050003;
private const int GlfwAnyPlatform = 0x00060000;
private const int GlfwWin32Platform = 0x00060001;
private const int GlfwWaylandPlatform = 0x00060003;
private const int GlfwX11Platform = 0x00060004;
private static readonly object Gate = new();
private static GraphicalDisplayProtocol? _configuredProtocol;
private static Glfw? _glfw;
internal static void Configure(
GraphicalHostPlatformServices platform)
{
ArgumentNullException.ThrowIfNull(platform);
GraphicalDisplayProtocol requested =
platform.WindowBackend.RequestedProtocol;
lock (Gate)
{
if (_configuredProtocol is { } existing)
{
if (existing != requested)
{
throw new InvalidOperationException(
"The GLFW platform is already configured as " +
$"{existing}; it cannot change to {requested} in " +
"the same process.");
}
return;
}
PreferPublishedNativeLibraries();
GlfwWindowing.Use();
// Silk's windowing backend initializes this exact singleton.
// A separate Glfw.GetApi() instance would receive the hint but
// would not own the window backend's process-global GLFW state.
Glfw glfw = GlfwProvider.UninitializedGLFW.Value;
glfw.InitHint(
(InitHint)GlfwPlatformInitHint,
requested switch
{
GraphicalDisplayProtocol.Windows =>
GlfwWin32Platform,
GraphicalDisplayProtocol.X11 =>
GlfwX11Platform,
GraphicalDisplayProtocol.Wayland =>
GlfwWaylandPlatform,
GraphicalDisplayProtocol.Automatic =>
GlfwAnyPlatform,
_ => throw new ArgumentOutOfRangeException(
nameof(requested)),
});
_glfw = glfw;
_configuredProtocol = requested;
}
}
internal static bool TryGetConfiguredApi(out Glfw? glfw)
{
lock (Gate)
{
glfw = _glfw;
return glfw is not null;
}
}
private static void PreferPublishedNativeLibraries()
{
if (PathResolver.Default is not DefaultPathResolver resolver)
{
throw new InvalidOperationException(
"Silk.NET's default native path resolver is unavailable.");
}
// Silk 2.23 names its Linux GLFW ABI libglfw.so.3.3, while the
// packaged multi-backend GLFW 3.4 asset is libglfw.so.3. Move the
// application directory ahead of the system passthrough so Silk's
// version resolver reaches the packaged .so.3 before a distribution
// GLFW 3.3. This also makes every packaged native closure deterministic
// instead of silently preferring a machine-global library.
List<Func<string, IEnumerable<string>>> resolvers =
resolver.Resolvers;
resolvers.Remove(DefaultPathResolver.BaseDirectoryResolver);
resolvers.Insert(
0,
DefaultPathResolver.BaseDirectoryResolver);
}
}
/// <summary>
/// Reads the GLFW platform actually selected at runtime (as opposed to the one
/// requested — GLFW's <c>Automatic</c> hint can resolve to either X11 or
/// Wayland). Moved here from the deleted (Campaign V slice V11) raw-GL
/// <c>GraphicalCapabilityRecord.cs</c>: <see cref="VulkanGraphicsContext"/>
/// depends on this for its own capability report, so it survived the GL arm
/// that used to sit alongside it.
/// </summary>
internal static unsafe class GlfwNativePlatformProbe
{
private const int GlfwWin32Platform = 0x00060001;
private const int GlfwWaylandPlatform = 0x00060003;
private const int GlfwX11Platform = 0x00060004;
internal static GraphicalDisplayProtocol GetActiveProtocol(
GraphicalHostOperatingSystem operatingSystem)
{
if (operatingSystem == GraphicalHostOperatingSystem.Windows)
return GraphicalDisplayProtocol.Windows;
if (!GraphicalWindowBackendConfigurator.TryGetConfiguredApi(
out Glfw? glfw)
|| glfw is null
|| !glfw.Context.TryGetProcAddress(
"glfwGetPlatform",
out nint export))
{
return GraphicalDisplayProtocol.Unknown;
}
int platform =
((delegate* unmanaged[Cdecl]<int>)export)();
return platform switch
{
GlfwX11Platform => GraphicalDisplayProtocol.X11,
GlfwWaylandPlatform => GraphicalDisplayProtocol.Wayland,
GlfwWin32Platform => GraphicalDisplayProtocol.Windows,
_ => GraphicalDisplayProtocol.Unknown,
};
}
internal static string GetVersion(
GraphicalHostOperatingSystem operatingSystem)
{
if (!GraphicalWindowBackendConfigurator.TryGetConfiguredApi(
out Glfw? glfw)
|| glfw is null)
{
return "unknown";
}
return glfw.GetVersionString() ?? "unknown";
}
}