acdream/tests/AcDream.Core.Tests/Textures/SurfaceDecoderTests.cs
Erik cc55c3f812 fix: heightmap transpose + solid-color + translucency + clipmap textures
Three root causes found via systematic debugging after the user reported
that the dc60405 texture fix and 4763b97 height table fix had no visible
effect on Holtburg.

## Heightmap transpose (LandblockMesh.Build)

Phase 1's LandblockMesh.Build indexed block.Height as y*9+x but AC packs
per-vertex heights in x-major order (x*9+y, matching ACViewer's
LandblockStruct: Height[x * VertexDim + y]). The bug was invisible on
flat landblocks (Phase 1 smoke test) but left buildings buried by 10-13
world-Z units on Holtburg, because building Frame.Origin positions
reference the un-transposed ground truth.

Diagnostic evidence (before fix, Holtburg 0xA9B4FFFF):
  entity 0x020000A5 at ( 84.6,126.0) entityZ= 66.03 terrainZ= 78.15 delta=-12.13
  entity 0x02000118 at ( 74.2,139.9) entityZ= 66.03 terrainZ= 78.92 delta=-12.89

After fix: deltas are 0.03 to 2.18 — buildings now sit on the ground
with small positive offsets for foundations.

Regression test added: Build_HeightmapPackedAsXMajor_NotYMajor asserts
asymmetric heights land at the correct world positions.

## Solid-color surfaces with Translucency=1.0 (SurfaceDecoder.DecodeSolidColor)

The "bright pink doors and windows" the user saw were 11 Holtburg
surfaces with OrigTextureId==0 — these carry a ColorValue instead of
a texture chain. Phase 2a's TextureCache dropped them into the magenta
fallback. All 11 turned out to be Base1Solid|Translucent with
Translucency=1.00, meaning "fully transparent placeholder surface"
(debug ColorValue is gray/green/red/blue/black, never displayed).

DecodeSolidColor now takes a translucency parameter and multiplies
alpha by (1 - translucency), so Translucency=1.0 → alpha=0, and the
mesh shader's existing alpha discard (< 0.5) makes the pixel invisible.

TextureCache honors Surface.Type.HasFlag(Base1Solid) and passes
surface.Translucency through.

Regression tests added: DecodeSolidColor_Opaque_PreservesAlpha and
DecodeSolidColor_FullyTranslucent_AlphaGoesToZero.

## Clipmap alpha-key (DecodeIndex16)

AC convention (per ACViewer TextureCache.IndexToColor): on surfaces
marked Base1ClipMap, palette indices 0..7 are treated as fully
transparent regardless of their actual palette color. Without this,
low-index pixels on clipmap surfaces (typically doorway cutouts and
foliage) render as opaque using whatever sentinel color is at those
palette slots.

DecodeRenderSurface now takes an isClipMap parameter. TextureCache
passes Surface.Type.HasFlag(Base1ClipMap). DecodeIndex16 forces
rgba=(0,0,0,0) when isClipMap && idx < 8.

Regression test added: DecodeIndex16_ClipMap_ZerosAlphaForLowIndices.

## Notes

- dc60405's PFID_INDEX16 palette decoder remains correct — no change.
- 4763b97's LandHeightTable wiring remains correct — real-table lookup
  still runs, it just happens to be linear at Holtburg's height range.
  The fix is forward-compatible with mountains elsewhere.
- All three bugs were invisible to the original unit tests. The new
  regression tests pin them down.

## State

- dotnet build: 0 warnings, 0 errors
- dotnet test: 42 passing (was 38 + 4 new)
- Runtime: 126 entities hydrated on Holtburg, no exceptions, no
  magenta fallback (counter was 11, now 0 via diagnostic confirmation)

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-10 19:37:06 +02:00

150 lines
4.8 KiB
C#

using AcDream.Core.Textures;
using DatReaderWriter.DBObjs;
using DatReaderWriter.Enums;
using DatReaderWriter.Types;
namespace AcDream.Core.Tests.Textures;
public class SurfaceDecoderTests
{
[Fact]
public void Decode_A8R8G8B8_ConvertsToRgba8()
{
// Source format is B, G, R, A in memory (little-endian ARGB).
// One 2x2 image: red, green, blue, white pixels.
var src = new byte[]
{
0x00, 0x00, 0xFF, 0xFF, // red (B=0, G=0, R=255, A=255)
0x00, 0xFF, 0x00, 0xFF, // green
0xFF, 0x00, 0x00, 0xFF, // blue
0xFF, 0xFF, 0xFF, 0xFF, // white
};
var rs = new RenderSurface
{
Width = 2,
Height = 2,
Format = PixelFormat.PFID_A8R8G8B8,
SourceData = src,
};
var decoded = SurfaceDecoder.DecodeRenderSurface(rs);
Assert.Equal(2, decoded.Width);
Assert.Equal(2, decoded.Height);
Assert.Equal(16, decoded.Rgba8.Length); // 2*2*4
// red pixel, in RGBA: 255, 0, 0, 255
Assert.Equal(0xFF, decoded.Rgba8[0]);
Assert.Equal(0x00, decoded.Rgba8[1]);
Assert.Equal(0x00, decoded.Rgba8[2]);
Assert.Equal(0xFF, decoded.Rgba8[3]);
}
[Fact]
public void Decode_UnsupportedFormat_ReturnsMagenta()
{
var rs = new RenderSurface
{
Width = 4,
Height = 4,
Format = PixelFormat.PFID_INDEX16, // not implemented path
SourceData = new byte[32],
};
var decoded = SurfaceDecoder.DecodeRenderSurface(rs);
Assert.Same(DecodedTexture.Magenta, decoded);
}
[Fact]
public void Decode_NullSourceData_ReturnsMagenta()
{
var rs = new RenderSurface
{
Width = 4,
Height = 4,
Format = PixelFormat.PFID_A8R8G8B8,
SourceData = null!,
};
var decoded = SurfaceDecoder.DecodeRenderSurface(rs);
Assert.Same(DecodedTexture.Magenta, decoded);
}
[Fact]
public void Decode_TruncatedA8R8G8B8_ReturnsMagenta()
{
// Buffer too small for width*height*4.
var rs = new RenderSurface
{
Width = 2,
Height = 2,
Format = PixelFormat.PFID_A8R8G8B8,
SourceData = new byte[8], // should be 16
};
var decoded = SurfaceDecoder.DecodeRenderSurface(rs);
Assert.Same(DecodedTexture.Magenta, decoded);
}
[Fact]
public void DecodeSolidColor_Opaque_PreservesAlpha()
{
var color = new ColorARGB { Alpha = 0xFF, Red = 0x11, Green = 0x22, Blue = 0x33 };
var decoded = SurfaceDecoder.DecodeSolidColor(color, translucency: 0f);
Assert.Equal(1, decoded.Width);
Assert.Equal(1, decoded.Height);
Assert.Equal(new byte[] { 0x11, 0x22, 0x33, 0xFF }, decoded.Rgba8);
}
[Fact]
public void DecodeSolidColor_FullyTranslucent_AlphaGoesToZero()
{
// Surfaces marked Base1Solid + Translucent with Translucency=1.0 are
// AC's convention for "invisible placeholder surfaces" — the engine renders
// them as nothing. Alpha must go to 0 so the mesh shader's discard rule
// makes them invisible.
var color = new ColorARGB { Alpha = 0xFF, Red = 0xC8, Green = 0xC8, Blue = 0xC8 };
var decoded = SurfaceDecoder.DecodeSolidColor(color, translucency: 1f);
Assert.Equal(0, decoded.Rgba8[3]); // alpha must be zero
}
[Fact]
public void DecodeIndex16_ClipMap_ZerosAlphaForLowIndices()
{
// Build a 4x1 INDEX16 surface with indices 0, 1, 7, 8.
// On a clipmap surface, indices 0..7 should be fully transparent and
// index 8 should render with its palette color.
var rs = new RenderSurface
{
Width = 4,
Height = 1,
Format = PixelFormat.PFID_INDEX16,
SourceData = new byte[]
{
0x00, 0x00, // index 0
0x01, 0x00, // index 1
0x07, 0x00, // index 7
0x08, 0x00, // index 8
},
};
var palette = new Palette();
for (int i = 0; i < 16; i++)
palette.Colors.Add(new ColorARGB { Alpha = 0xFF, Red = 0xAA, Green = 0xBB, Blue = 0xCC });
var decoded = SurfaceDecoder.DecodeRenderSurface(rs, palette, isClipMap: true);
// Pixels 0, 1, 2 (indices 0, 1, 7) should be fully transparent.
Assert.Equal(0, decoded.Rgba8[3]); // pixel 0 alpha
Assert.Equal(0, decoded.Rgba8[7]); // pixel 1 alpha
Assert.Equal(0, decoded.Rgba8[11]); // pixel 2 alpha
// Pixel 3 (index 8) should have the palette alpha.
Assert.Equal(0xFF, decoded.Rgba8[15]);
Assert.Equal(0xAA, decoded.Rgba8[12]);
}
}