Plan §5.5.11 recorded what V4t deliberately left behind: it moved the table
ENTRY of every world texture to the device and kept CREATION with the caches,
because "creating world textures through IGpuTexture is real remaining work and
it belongs with the Vulkan world arm, which is the first thing that cannot use a
GL handle at all." §5.5.12 item 1 handed it forward and named the missing piece
exactly — "an ITextureArray implementation over IGpuTexture, not a codec",
because V6b's BlockCompressionCodec and BlockCompressionMipChain already supply
the BC chains. This is that work.
IWorldTextureArray is the seam, and the slot is what crosses it. Before this
commit ObjectMeshManager read BindlessWrapHandle/BindlessClampHandle off the
concrete GL array and interned them into the device table itself. A 64-bit
ARB_bindless_texture handle has no Vulkan spelling, so the array now answers the
question the caller was really asking — ResolveSlot(wrapping) — and each arm gets
there its own way: ManagedGLTextureArray makes the same idempotent interning call
one level down, and RhiWorldTextureArray returns a pair it registered at
construction. ReleaseTextureSlots replaces the snapshot dictionary the manager
kept for the same reason, and still runs only once physical retirement completes.
Which implementation exists is decided ONCE, by the IWorldTextureArrayFactory
composition builds — plan §3.1's no-runtime-fork rule. Everything above the seam
(capacity policy, slot allocation, ref counting, layer retirement, empty-atlas
eviction, and the whole of ObjectMeshManager's atlas policy) is written once and
branches on nothing.
Three things the RHI array does differently, each because the backends genuinely
differ rather than by choice: BC mip chains are CPU-built through
BlockCompressionMipChain, since Vulkan cannot blit into a compressed image, while
RGBA8 uses the device's blit; filtering lives in an immutable sampler rather than
a texture parameter, so both address modes are registered up front exactly as the
GL array holds two resident handles; and RGB8/A8/Rgba32f are refused at creation
with the reason named. A8 is the interesting refusal — the GL array serves it by
swizzling R into A, and a Vulkan swizzle lives in the image VIEW, which the pinned
GpuTextureDescription does not describe. A silent substitution would render wrong
and look like a shader bug.
TerrainAtlas gains the second construction path V6i drafted and reverted. The
decode is factored out and shared, so both arms read the same DATs, in the same
order, with the same resize-to-max policy; only the upload forks.
ICompositeTextureArrayBackend gains its RHI arm, which is four small methods
because that seam was already a seam.
The Vulkan arm is EXERCISED, not merely present. That is the whole reason the
V6i draft was reverted rather than landed — "built then reverted because nothing
exercised it" — and it is the same failure §5.5.12 measured twice in the
descriptor layouts. So the composition host now builds the real terrain atlas
through IGpuDevice.CreateTexture on the arm with no GL context, and creates and
releases one shared array of each format family plus one composite array at
startup. Creation only; nothing draws them. Releasing them in the same statement
covers one thing a retained bundle would not — that both slot pairs come back and
the images route through the retirement queue.
Gates: Release build; App tests 4,104 / 3 skips; strict GL offline pixel gate vs
0ca802cd 3.20e-05 (18 px of 563,200, inside the documented 9–31 px control band);
GL connected tools/run-repeat-connected-gate.ps1 -Runs 3 at 3/3 RENDERED on the
desktop witness AND 3/3 on the client capture; one Vulkan composition-host run
with VK_LAYER_KHRONOS_validation proven inserted by the loader at zero errors,
zero warnings, no [shutdown] diagnostic, and a captured frame. That run built
terrain-atlas 512x512x33 with 10 mip levels, terrain-alpha-atlas 512x512x8, RGBA8
64x64x32 (slots 3/4, 174,720 mip bytes blitted), BC1 64x64x32 (slots 5/6, 696 mip
bytes encoded) and composite 32x32x8 (slot 7).
One whole-suite run failed Issue181WallPressEquilibriumTests once; it passed
alone and did not recur in five further runs. Seven test classes mutate the same
process-global CameraDiagnostics switches with no xUnit collection isolation, and
this diff touches no camera, visibility or physics code. A separate run of the
UNCHANGED parent tree failed a different zero-allocation test, which is `#250`'s
documented class. Both are filed rather than attributed.
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
436 lines
16 KiB
C#
436 lines
16 KiB
C#
using System.Collections.Concurrent;
|
|
using System.Runtime.CompilerServices;
|
|
using AcDream.App.Composition;
|
|
using AcDream.App.Rendering;
|
|
using AcDream.App.Rendering.Gpu;
|
|
using AcDream.App.Rendering.Wb;
|
|
using AcDream.App.Rendering.Residency;
|
|
using AcDream.App.World;
|
|
using AcDream.App.Tests.Rendering.Gpu;
|
|
using AcDream.Content;
|
|
using AcDream.Core.Physics;
|
|
using AcDream.Core.Terrain;
|
|
using AcDream.UI.Abstractions.Settings;
|
|
using DatReaderWriter.DBObjs;
|
|
using Silk.NET.Input;
|
|
using Silk.NET.OpenGL;
|
|
using Shader = AcDream.App.Rendering.Shader;
|
|
|
|
namespace AcDream.App.Tests.Composition;
|
|
|
|
public sealed class WorldRenderCompositionTests
|
|
{
|
|
[Fact]
|
|
public void SuccessPublishesExactModernFoundationInFrozenOrder()
|
|
{
|
|
ResidencyBudgetOptions budgets =
|
|
ResidencyBudgetOptions.Default with
|
|
{
|
|
ObjectMeshGpuBytes = 321,
|
|
CompositePhysicalBytes = 654,
|
|
};
|
|
var fixture = new Fixture(budgets: budgets);
|
|
|
|
WorldRenderResult result = fixture.Compose();
|
|
|
|
Assert.Equal(Enum.GetValues<WorldRenderCompositionPoint>(), fixture.Points);
|
|
Assert.Same(fixture.Publication.Terrain, result.Foundation.Terrain);
|
|
Assert.Same(fixture.Publication.MeshAdapter, result.Foundation.MeshAdapter);
|
|
Assert.Same(fixture.Publication.TextureCache, result.Foundation.TextureCache);
|
|
Assert.Same(fixture.Lifetime.Atlas, result.Foundation.TerrainAtlas);
|
|
Assert.Equal(QualitySettings.From(QualityPreset.High).AnisotropicLevel,
|
|
fixture.Factory.AnisotropicLevel);
|
|
Assert.Same(budgets, result.Foundation.Residency.Budgets);
|
|
Assert.Same(budgets, fixture.Factory.MeshBudgets);
|
|
Assert.Same(budgets, fixture.Factory.TextureBudgets);
|
|
Assert.Same(result.Foundation.Residency, fixture.Factory.RegisteredResidency);
|
|
Assert.Equal(1, fixture.Lifetime.AcquireCalls);
|
|
Assert.Empty(fixture.Factory.Releases);
|
|
}
|
|
|
|
[Fact]
|
|
public void MissingDebugFontSkipsOnlyTheOptionalHudPrefix()
|
|
{
|
|
var fixture = new Fixture(hasFont: false);
|
|
|
|
WorldRenderResult result = fixture.Compose();
|
|
|
|
Assert.Null(result.Foundation.DebugFont);
|
|
Assert.Null(result.Foundation.TextRenderer);
|
|
Assert.DoesNotContain(WorldRenderCompositionPoint.DebugFontCreated, fixture.Points);
|
|
Assert.DoesNotContain(WorldRenderCompositionPoint.TextRendererCreated, fixture.Points);
|
|
Assert.DoesNotContain(WorldRenderCompositionPoint.HudResourcesPublished, fixture.Points);
|
|
Assert.Contains(WorldRenderCompositionPoint.HudResourcesCompleted, fixture.Points);
|
|
}
|
|
|
|
[Theory]
|
|
[MemberData(nameof(FailurePoints))]
|
|
public void FaultAfterEachBoundaryStopsTheExactSuffix(int pointValue)
|
|
{
|
|
var point = (WorldRenderCompositionPoint)pointValue;
|
|
var fixture = new Fixture(failurePoint: point);
|
|
|
|
Assert.Throws<InvalidOperationException>(fixture.Compose);
|
|
|
|
Assert.Equal(
|
|
Enum.GetValues<WorldRenderCompositionPoint>()
|
|
.TakeWhile(candidate => candidate <= point),
|
|
fixture.Points);
|
|
}
|
|
|
|
public static TheoryData<int> FailurePoints()
|
|
{
|
|
var data = new TheoryData<int>();
|
|
foreach (WorldRenderCompositionPoint point in
|
|
Enum.GetValues<WorldRenderCompositionPoint>())
|
|
{
|
|
data.Add((int)point);
|
|
}
|
|
return data;
|
|
}
|
|
|
|
[Theory]
|
|
[InlineData("terrain shader", "terrain shader")]
|
|
[InlineData("scene lighting", "scene lighting")]
|
|
[InlineData("debug lines", "debug lines")]
|
|
[InlineData("HUD", "text renderer|debug font")]
|
|
[InlineData("terrain", "terrain")]
|
|
[InlineData("mesh shader", "mesh shader")]
|
|
[InlineData("WB mesh adapter", "WB mesh adapter")]
|
|
[InlineData("texture cache", "texture cache")]
|
|
[InlineData("sampler cache", "sampler cache")]
|
|
public void FailedPublicationRollsBackOnlyItsUnpublishedResourcePrefix(
|
|
string publication,
|
|
string expectedReleaseOrder)
|
|
{
|
|
var fixture = new Fixture(publicationFailure: publication);
|
|
|
|
Assert.Throws<InvalidOperationException>(fixture.Compose);
|
|
|
|
Assert.Equal(
|
|
expectedReleaseOrder.Split('|'),
|
|
fixture.Factory.Releases);
|
|
}
|
|
|
|
[Fact]
|
|
public void PartialHudConstructionRollsBackFontWhenTextCreationFails()
|
|
{
|
|
var fixture = new Fixture(
|
|
failurePoint: WorldRenderCompositionPoint.DebugFontCreated);
|
|
|
|
Assert.Throws<InvalidOperationException>(fixture.Compose);
|
|
|
|
Assert.Equal(["debug font"], fixture.Factory.Releases);
|
|
}
|
|
|
|
[Fact]
|
|
public void GameWindowUsesPhaseAndNoLongerBuildsWorldFoundationInline()
|
|
{
|
|
string source = File.ReadAllText(Path.Combine(
|
|
FindRepoRoot(),
|
|
"src",
|
|
"AcDream.App",
|
|
"Rendering",
|
|
"GameWindow.cs"));
|
|
|
|
Assert.Contains("new WorldRenderCompositionPhase(", source,
|
|
StringComparison.Ordinal);
|
|
Assert.DoesNotContain("BindlessSupport.TryCreate(_gl", source,
|
|
StringComparison.Ordinal);
|
|
Assert.DoesNotContain("_terrainModernShader = new Shader", source,
|
|
StringComparison.Ordinal);
|
|
Assert.DoesNotContain("_wbMeshAdapter = new", source,
|
|
StringComparison.Ordinal);
|
|
Assert.DoesNotContain("_textureCache = new TextureCache", source,
|
|
StringComparison.Ordinal);
|
|
}
|
|
|
|
private sealed class Fixture
|
|
{
|
|
private readonly WorldRenderCompositionPoint? _failurePoint;
|
|
private readonly ResidencyBudgetOptions _budgets;
|
|
private readonly IGpuDevice _gpuDevice = new RecordingGpuDevice();
|
|
|
|
public Fixture(
|
|
bool hasFont = true,
|
|
WorldRenderCompositionPoint? failurePoint = null,
|
|
string? publicationFailure = null,
|
|
ResidencyBudgetOptions? budgets = null)
|
|
{
|
|
_failurePoint = failurePoint;
|
|
_budgets = budgets ?? ResidencyBudgetOptions.Default;
|
|
Factory = new Factory(hasFont);
|
|
Publication = new Publication(publicationFailure);
|
|
Lifetime = new RenderLifetime(Factory.Atlas);
|
|
var content = (ContentEffectsAudioResult)
|
|
RuntimeHelpers.GetUninitializedObject(typeof(ContentEffectsAudioResult));
|
|
Content = content;
|
|
}
|
|
|
|
public Factory Factory { get; }
|
|
public Publication Publication { get; }
|
|
public RenderLifetime Lifetime { get; }
|
|
public List<WorldRenderCompositionPoint> Points { get; } = [];
|
|
public ContentEffectsAudioResult Content { get; }
|
|
|
|
public WorldRenderResult Compose() =>
|
|
new WorldRenderCompositionPhase(
|
|
new WorldRenderDependencies(
|
|
new WorldEnvironmentController(),
|
|
Lifetime,
|
|
ImmediateGpuResourceRetirementQueue.Instance,
|
|
_budgets,
|
|
0xA9B4FFFFu,
|
|
Path.Combine(Path.GetTempPath(), "acdream-tests"),
|
|
_ => { },
|
|
_gpuDevice,
|
|
new GpuDeviceFrameLifetime(_gpuDevice)),
|
|
Publication,
|
|
Factory,
|
|
point =>
|
|
{
|
|
Points.Add(point);
|
|
if (point == _failurePoint)
|
|
throw new InvalidOperationException($"fault at {point}");
|
|
}).Compose(
|
|
new GameWindowPlatformResult<GameWindowGraphics, IInputContext>(TestGameWindowGraphics.OpenGl, null!),
|
|
Content,
|
|
new SettingsDevToolsResult(
|
|
QualitySettings.From(QualityPreset.High),
|
|
null));
|
|
}
|
|
|
|
private sealed class RenderLifetime(TerrainAtlas atlas)
|
|
: IGameRenderResourceLifetime
|
|
{
|
|
public TerrainAtlas Atlas { get; } = atlas;
|
|
public int AcquireCalls { get; private set; }
|
|
|
|
public TerrainAtlas AcquireTerrainAtlas(Func<TerrainAtlas> factory)
|
|
{
|
|
AcquireCalls++;
|
|
return Atlas;
|
|
}
|
|
}
|
|
|
|
private sealed class Factory(bool hasFont) : IWorldRenderCompositionFactory
|
|
{
|
|
private readonly Dictionary<IDisposable, string> _names =
|
|
new(ReferenceEqualityComparer.Instance);
|
|
|
|
public TerrainAtlas Atlas { get; } = Stub<TerrainAtlas>();
|
|
public int AnisotropicLevel { get; private set; }
|
|
public List<string> Releases { get; } = [];
|
|
public ResidencyBudgetOptions? MeshBudgets { get; private set; }
|
|
public ResidencyBudgetOptions? TextureBudgets { get; private set; }
|
|
public ResidencyManager? RegisteredResidency { get; private set; }
|
|
|
|
public void InitializeGlState(GL gl) { }
|
|
|
|
public WorldRegionData LoadRegion(IDatReaderWriter dats) =>
|
|
new(Stub<Region>(), new float[256]);
|
|
|
|
public void InitializeEnvironment(
|
|
WorldEnvironmentController environment,
|
|
Region region) { }
|
|
|
|
public BindlessSupport RequireBindless(GL gl, Action<string> log) =>
|
|
Stub<BindlessSupport>();
|
|
|
|
public TerrainAtlas AcquireTerrainAtlas(
|
|
IGameRenderResourceLifetime lifetime,
|
|
GL gl,
|
|
IDatReaderWriter dats,
|
|
BindlessSupport bindless) =>
|
|
lifetime.AcquireTerrainAtlas(() => Atlas);
|
|
|
|
/// <summary>
|
|
/// Campaign V slice V6i-2: the arm a backend with no GL context takes.
|
|
/// Returns the same stub atlas through the same lifetime owner, so the
|
|
/// composition assertions do not care which arm ran.
|
|
/// </summary>
|
|
public TerrainAtlas AcquireBackendNeutralTerrainAtlas(
|
|
IGameRenderResourceLifetime lifetime,
|
|
IGpuDevice device,
|
|
IDatReaderWriter dats) =>
|
|
lifetime.AcquireTerrainAtlas(() => Atlas);
|
|
|
|
/// <summary>
|
|
/// Recorded rather than run: the exercise needs a real
|
|
/// <see cref="IGpuDevice"/> to create images through. Its behaviour is
|
|
/// covered by <c>RhiWorldTextureArrayTests</c> and by the Vulkan
|
|
/// composition-host run — see plan §5.5.13.
|
|
/// </summary>
|
|
public void ExerciseBackendNeutralWorldTextures(
|
|
IGpuDevice device,
|
|
Action<string> log) =>
|
|
WorldTextureExerciseCount++;
|
|
|
|
public int WorldTextureExerciseCount { get; private set; }
|
|
|
|
public void SetTerrainAnisotropic(TerrainAtlas atlas, int level) =>
|
|
AnisotropicLevel = level;
|
|
|
|
public Shader CreateTerrainShader(GL gl, string shadersDirectory) =>
|
|
Resource<Shader>("terrain shader");
|
|
|
|
public SceneLightingUboBinding CreateSceneLighting(GL gl) =>
|
|
Resource<SceneLightingUboBinding>("scene lighting");
|
|
|
|
public DebugLineRenderer CreateDebugLines(
|
|
IGpuDevice device, ICurrentGpuFrameSource frameSource, string shadersDirectory) =>
|
|
Resource<DebugLineRenderer>("debug lines");
|
|
|
|
public byte[]? TryLoadDebugFont() => hasFont ? [1] : null;
|
|
|
|
public BitmapFont CreateDebugFont(IGpuDevice device, byte[] bytes) =>
|
|
Resource<BitmapFont>("debug font");
|
|
|
|
public TextRenderer CreateTextRenderer(
|
|
IGpuDevice device, ICurrentGpuFrameSource frameSource, string shadersDirectory) =>
|
|
Resource<TextRenderer>("text renderer");
|
|
|
|
public TerrainModernRenderer CreateTerrain(
|
|
GL gl,
|
|
BindlessSupport bindless,
|
|
Shader shader,
|
|
TerrainAtlas atlas,
|
|
IGpuDevice gpuDevice,
|
|
IGpuResourceRetirementQueue retirement) =>
|
|
Resource<TerrainModernRenderer>("terrain");
|
|
|
|
public WorldTerrainBuildContext CreateTerrainBuildContext(
|
|
uint initialCenterLandblockId,
|
|
float[] heightTable,
|
|
TerrainAtlas atlas) =>
|
|
new(
|
|
initialCenterLandblockId,
|
|
0xA9,
|
|
0xB4,
|
|
heightTable,
|
|
Stub<TerrainBlendingContext>(),
|
|
new ConcurrentDictionary<uint, SurfaceInfo>());
|
|
|
|
public Shader CreateMeshShader(GL gl, string shadersDirectory) =>
|
|
Resource<Shader>("mesh shader");
|
|
|
|
public WbMeshAdapter CreateMeshAdapter(
|
|
GL gl,
|
|
IGpuDevice device,
|
|
IDatReaderWriter dats,
|
|
IPreparedAssetSource preparedAssets,
|
|
IGpuResourceRetirementQueue retirement,
|
|
ResidencyBudgetOptions budgets)
|
|
{
|
|
MeshBudgets = budgets;
|
|
return Resource<WbMeshAdapter>("WB mesh adapter");
|
|
}
|
|
|
|
public TextureCache CreateTextureCache(
|
|
GL gl,
|
|
IGpuDevice device,
|
|
IDatReaderWriter dats,
|
|
BindlessSupport bindless,
|
|
IGpuResourceRetirementQueue retirement,
|
|
string diagnosticsDirectory,
|
|
ResidencyBudgetOptions budgets)
|
|
{
|
|
TextureBudgets = budgets;
|
|
return Resource<TextureCache>("texture cache");
|
|
}
|
|
|
|
public void RegisterResidencySources(
|
|
ResidencyManager manager,
|
|
WbMeshAdapter meshes,
|
|
TextureCache textures,
|
|
IPreparedAssetSource preparedAssets,
|
|
IAnimationLoader animations,
|
|
AcDream.Core.Audio.DatSoundCache? audio)
|
|
{
|
|
RegisteredResidency = manager;
|
|
}
|
|
|
|
public SamplerCache CreateSamplerCache(GL gl) =>
|
|
Resource<SamplerCache>("sampler cache");
|
|
|
|
public void Release(IDisposable resource)
|
|
{
|
|
Releases.Add(_names[resource]);
|
|
}
|
|
|
|
private T Resource<T>(string name)
|
|
where T : class, IDisposable
|
|
{
|
|
T value = Stub<T>();
|
|
_names.Add(value, name);
|
|
return value;
|
|
}
|
|
}
|
|
|
|
private sealed class Publication(string? failure) : IGameWindowWorldRenderPublication
|
|
{
|
|
public TerrainModernRenderer? Terrain { get; private set; }
|
|
public WbMeshAdapter? MeshAdapter { get; private set; }
|
|
public TextureCache? TextureCache { get; private set; }
|
|
|
|
public void PublishBindlessSupport(BindlessSupport value) =>
|
|
Fail("bindless");
|
|
public void PublishTerrainShader(Shader value) =>
|
|
Fail("terrain shader");
|
|
public void PublishSceneLighting(SceneLightingUboBinding value) =>
|
|
Fail("scene lighting");
|
|
public void PublishDebugLines(DebugLineRenderer value) =>
|
|
Fail("debug lines");
|
|
public void PublishHudResources(BitmapFont font, TextRenderer text) =>
|
|
Fail("HUD");
|
|
|
|
public void PublishTerrain(TerrainModernRenderer value)
|
|
{
|
|
Fail("terrain");
|
|
Terrain = value;
|
|
}
|
|
|
|
public void PublishTerrainBuildState(
|
|
float[] heightTable,
|
|
TerrainBlendingContext blending,
|
|
ConcurrentDictionary<uint, SurfaceInfo> surfaceCache) =>
|
|
Fail("terrain build state");
|
|
public void PublishMeshShader(Shader value) => Fail("mesh shader");
|
|
|
|
public void PublishWbMeshAdapter(WbMeshAdapter value)
|
|
{
|
|
Fail("WB mesh adapter");
|
|
MeshAdapter = value;
|
|
}
|
|
|
|
public void PublishTextureCache(TextureCache value)
|
|
{
|
|
Fail("texture cache");
|
|
TextureCache = value;
|
|
}
|
|
|
|
public void PublishSamplerCache(SamplerCache value) =>
|
|
Fail("sampler cache");
|
|
|
|
private void Fail(string point)
|
|
{
|
|
if (string.Equals(failure, point, StringComparison.Ordinal))
|
|
throw new InvalidOperationException($"publication failed at {point}");
|
|
}
|
|
}
|
|
|
|
private static T Stub<T>() where T : class =>
|
|
(T)RuntimeHelpers.GetUninitializedObject(typeof(T));
|
|
|
|
private static string FindRepoRoot()
|
|
{
|
|
DirectoryInfo? directory = new(AppContext.BaseDirectory);
|
|
while (directory is not null)
|
|
{
|
|
if (File.Exists(Path.Combine(directory.FullName, "AcDream.slnx")))
|
|
return directory.FullName;
|
|
directory = directory.Parent;
|
|
}
|
|
throw new DirectoryNotFoundException("Could not find AcDream.slnx.");
|
|
}
|
|
}
|