acdream/tests/AcDream.Content.Tests/PreparedAssetSourceTests.cs
Erik 75664805f8 feat(rendering): port retail one-pass detail material
Replace the building and EnvCell detail replay with retail's exact single-pass stage result, including authored surface opacity, squared detail alpha, final-alpha clipping, and the original subset pipeline/order. Arm the ordered walk command in place to close #471, delete the replay pipelines/shaders, and advance prepared content to recipe 9.

Mutation witnesses (each restored before commit):
- X=a*qA: RetailDetailTextureContractTests.BothShaderFamiliesUseTheSharedOnePassSourceAndDebugPrecedesDetailSample line 174, missing materialAlpha * detail.a * detail.a.
- X*=base alpha: same test line 175, forbidden baseTexel.a found.
- CLIP against base alpha: EnvCellAlphaDrawSourceTests.ClipShaders_UseGreaterEqualForThePerRangeReference line 260, final-X conditional missing.
- second detail draw: EnvCellAlphaDrawSourceTests.DetailOn_EveryEnvCellFamilyDrawsOnceInPlaceWithAuthoredOpacity line 105, Assert.Single saw 2 MDI calls.
- straight-alpha substitution: WalkStaticStreamPopulatorTests.ImmediateBuildingDetail_RetainsOriginalFramebufferFamily line 1244, Additive first failed (only wb-mesh-alpha-1x recorded; InvAlpha also failed).
- omit ordered arm: OrderPreservingSubmitterTests.PrepareThenDraw_OrdinaryBuildingClipBuildingOrdinary_ArmsOnePassInPlace line 305, expected (77,3.5), got (0,0).
- omit atmospheric combine: RetailDetailTextureContractTests.BothShaderFamiliesUseTheSharedOnePassSourceAndDebugPrecedesDetailSample line 173, atmospheric shared include missing.
- drop serialized opacity: ObjectMeshDataSerializerTests.SurfaceOpacity_RoundTripsBitExactlyAndDeterministically line 288, first reported 0.5 bits 1056964608 vs 1065353216.
- stale detail arm: ordered adjacency test line 307, expected following ordinary (0,0), got (77,3.5).
- per-frame surface map: EnvCellAlphaDrawSourceTests.ProductionWholeLeaf_WarmedScanSubmitRhiAndFilteredReplayDoNotAllocate line 178, expected 0 B, got 147456 B.

Verification before commit: shader compiler 23/23; focused App 213/213; Content 75/75; Core Wb 10/10; launcher migration 6/6; Release solution build 0 warnings / 0 errors; git diff --check clean.
2026-09-05 02:03:13 +02:00

280 lines
9.5 KiB
C#

using System.Numerics;
using AcDream.Content.Pak;
namespace AcDream.Content.Tests;
public sealed class PreparedAssetSourceTests : IDisposable
{
private static readonly PreparedAssetCatalogIdentity Identity =
new(10, 20, 30, 40, PakFormat.CurrentBakeToolVersion);
private readonly List<string> _paths = [];
[Fact]
public void Probe_IsTypedAndDoesNotFaultPayloadBytes()
{
string path = WritePak(
(PakAssetType.GfxObjMesh, 0x0100_0001u, Mesh(0x0100_0001u)));
using var source = new PakPreparedAssetSource(path, Identity);
Assert.Equal(
PreparedAssetPresence.Available,
source.Probe(PakAssetType.GfxObjMesh, 0x0100_0001u));
Assert.Equal(
PreparedAssetPresence.Missing,
source.Probe(PakAssetType.SetupMesh, 0x0100_0001u));
Assert.Equal(2, source.Stats.Probes);
Assert.Equal(0, source.Stats.Reads);
}
[Fact]
public void Read_PreservesMissingLoadedAndCorruptStates()
{
const uint fileId = 0x0100_0001u;
string path = WritePak(
(PakAssetType.GfxObjMesh, fileId, Mesh(fileId)));
using (var source = new PakPreparedAssetSource(path, Identity))
{
PreparedAssetReadResult loaded =
source.Read(PreparedAssetRequest.GfxObj(fileId));
Assert.Equal(PreparedAssetReadStatus.Loaded, loaded.Status);
Assert.NotNull(loaded.Data);
PreparedAssetReadResult missing =
source.Read(PreparedAssetRequest.GfxObj(0x0100_FFFFu));
Assert.Equal(PreparedAssetReadStatus.Missing, missing.Status);
Assert.Null(missing.Data);
}
FlipFirstBlobByte(path);
using var corruptSource = new PakPreparedAssetSource(path, Identity);
Assert.Equal(
PreparedAssetPresence.Available,
corruptSource.Probe(PakAssetType.GfxObjMesh, fileId));
PreparedAssetReadResult corrupt =
corruptSource.Read(PreparedAssetRequest.GfxObj(fileId));
Assert.Equal(PreparedAssetReadStatus.Corrupt, corrupt.Status);
Assert.Null(corrupt.Data);
Assert.Equal(
PreparedAssetPresence.Corrupt,
corruptSource.Probe(PakAssetType.GfxObjMesh, fileId));
}
[Fact]
public void Read_EnvCellAliasUsesSourceKeyAndRuntimeGeometryIdentity()
{
const uint primaryCell = 0x1234_0101u;
const uint aliasCell = 0x1234_0102u;
const ulong geometryId = 0x2_0000_1234UL;
string path = NewPath();
using (var writer = new PakWriter(path, Header()))
{
writer.AddBlob(
PakKey.Compose(PakAssetType.EnvCellMesh, primaryCell),
Mesh(geometryId));
writer.AddAlias(
PakKey.Compose(PakAssetType.EnvCellMesh, aliasCell),
PakKey.Compose(PakAssetType.EnvCellMesh, primaryCell));
writer.Finish();
}
using var source = new PakPreparedAssetSource(path, Identity);
var request = PreparedAssetRequest.EnvCellGeometry(
aliasCell,
geometryId,
environmentId: 7,
cellStructure: 3,
surfaces: [1, 2, 3]);
PreparedAssetReadResult result = source.Read(request);
Assert.Equal(PreparedAssetReadStatus.Loaded, result.Status);
Assert.Equal(geometryId, result.Data!.ObjectId);
}
[Fact]
public void Read_RejectsPayloadIdentityOrTypeMismatchAsCorruption()
{
const uint setupId = 0x0200_0001u;
string path = WritePak(
(PakAssetType.SetupMesh, setupId, Mesh(setupId)));
var diagnostics = new List<string>();
using var source = new PakPreparedAssetSource(
path,
Identity,
diagnostics.Add);
PreparedAssetReadResult result =
source.Read(PreparedAssetRequest.Setup(setupId));
Assert.Equal(PreparedAssetReadStatus.Corrupt, result.Status);
Assert.Single(diagnostics);
Assert.Contains("identity mismatch", diagnostics[0]);
}
[Fact]
public void Constructor_RejectsEveryCatalogIdentityMismatch()
{
string path = WritePak(
(PakAssetType.GfxObjMesh, 1u, Mesh(1u)));
var mismatches = new[]
{
Identity with { PortalIteration = 11 },
Identity with { CellIteration = 21 },
Identity with { HighResIteration = 31 },
Identity with { LanguageIteration = 41 },
Identity with { BakeToolVersion = Identity.BakeToolVersion + 1 },
};
foreach (PreparedAssetCatalogIdentity expected in mismatches)
{
InvalidDataException error = Assert.Throws<InvalidDataException>(
() => new PakPreparedAssetSource(path, expected));
Assert.Contains("does not match the installed DAT set", error.Message);
Assert.Contains("Re-bake", error.Message);
}
}
/// <summary>
/// S5-c4: an older recipe-8 package must be rejected by a recipe-9
/// consumer through the catalog-identity check
/// alone, BEFORE any TextureBatchData payload field is ever
/// deserialized -- so an older pak can never be silently misread as
/// carrying authored surface opacity.
/// </summary>
[Fact]
public void Constructor_RejectsOlderRecipePackageBeforePayloadDecode()
{
// PakWriter stamps BakeToolVersion to PakFormat.CurrentBakeToolVersion
// unconditionally (the header-template default-0 footgun guard), so
// an "older recipe" file has to be produced by patching the on-disk
// header dword directly -- the same technique
// PakRoundTripTests.Reader_RejectsWrongFormatVersion uses for the
// sibling FormatVersion field. BakeToolVersion is offset 36 per
// PakHeader's normative layout.
string path = WritePak(
(PakAssetType.EnvCellMesh, 1u, Mesh(1u)));
using (var fs = new FileStream(path, FileMode.Open, FileAccess.ReadWrite))
{
fs.Position = 36;
Span<byte> buf = stackalloc byte[4];
System.Buffers.Binary.BinaryPrimitives.WriteUInt32LittleEndian(
buf, 8u);
fs.Write(buf);
}
InvalidDataException error = Assert.Throws<InvalidDataException>(
() => new PakPreparedAssetSource(path, Identity));
Assert.Contains("does not match the installed DAT set", error.Message);
Assert.Contains("Re-bake", error.Message);
Assert.Contains("bake tool 8 != 9", error.Message);
}
[Fact]
public void Read_PreCanceledTokenPropagatesWithoutStartingRead()
{
const uint fileId = 0x0100_0001u;
string path = WritePak(
(PakAssetType.GfxObjMesh, fileId, Mesh(fileId)));
using var source = new PakPreparedAssetSource(path, Identity);
using var cancellation = new CancellationTokenSource();
cancellation.Cancel();
Assert.Throws<OperationCanceledException>(
() => source.Read(
PreparedAssetRequest.GfxObj(fileId),
cancellation.Token));
Assert.Equal(0, source.Stats.Reads);
}
[Fact]
public void Dispose_ReleasesMappedPackage()
{
string path = WritePak(
(PakAssetType.GfxObjMesh, 1u, Mesh(1u)));
var source = new PakPreparedAssetSource(path, Identity);
source.Dispose();
using var exclusive = new FileStream(
path,
FileMode.Open,
FileAccess.ReadWrite,
FileShare.None);
Assert.True(exclusive.CanWrite);
}
private string WritePak(
params (PakAssetType Type, uint FileId, ObjectMeshData Data)[] entries)
{
string path = NewPath();
using var writer = new PakWriter(path, Header());
foreach ((PakAssetType type, uint fileId, ObjectMeshData data) in entries)
{
writer.AddBlob(PakKey.Compose(type, fileId), data);
}
writer.Finish();
return path;
}
private string NewPath()
{
string path = Path.Combine(
Path.GetTempPath(),
$"acdream-prepared-{Guid.NewGuid():N}.pak");
_paths.Add(path);
return path;
}
private static PakHeader Header() =>
new()
{
PortalIteration = Identity.PortalIteration,
CellIteration = Identity.CellIteration,
HighResIteration = Identity.HighResIteration,
LanguageIteration = Identity.LanguageIteration,
BakeToolVersion = Identity.BakeToolVersion,
};
private static ObjectMeshData Mesh(ulong objectId) =>
new()
{
ObjectId = objectId,
Vertices =
[
new(
new Vector3(1, 2, 3),
Vector3.UnitZ,
new Vector2(0.25f, 0.75f)),
],
};
private static void FlipFirstBlobByte(string path)
{
using var stream = new FileStream(
path,
FileMode.Open,
FileAccess.ReadWrite,
FileShare.None);
stream.Position = PakHeader.Size + 4;
int value = stream.ReadByte();
Assert.NotEqual(-1, value);
stream.Position = PakHeader.Size + 4;
stream.WriteByte((byte)(value ^ 0xFF));
}
public void Dispose()
{
foreach (string path in _paths)
{
try
{
File.Delete(path);
}
catch
{
// Best-effort test cleanup.
}
}
}
}