From ea8b79b3629558d8d23f2ec6be35c80c94020f85 Mon Sep 17 00:00:00 2001 From: gensokyo-dev Date: Wed, 2 Dec 2026 00:46:49 +0000 Subject: [PATCH] fix(artifacts): derive the DDS payload size for every fixed-size DXGI format The DXGI table listed only RGBA32, RGBA16 and RGBA8/BGRA8 formats, so a DX10 texture in R8_UINT, R16_FLOAT, R32_FLOAT, RG8 and other single- or two-channel formats had no expected payload size and passed the extent check even with no pixel data. List every format with a fixed number of bytes per texel. Co-Authored-By: gensokyo-dev <342580529+gensokyo-dev@users.noreply.github.com> --- docs/gpu-asset-headers.md | 10 ++-- .../gpuAsset/GpuAssetArtifactReader.test.ts | 56 +++++++++++++++++++ src/artifacts/gpuAsset/GpuAssetLayouts.ts | 15 ++++- src/artifacts/gpuAsset/gpuAsset.fixture.ts | 13 ++++- tests/acceptance/gpuAssetHeadersCli.test.ts | 48 ++++++++++++++++ 5 files changed, 132 insertions(+), 10 deletions(-) diff --git a/docs/gpu-asset-headers.md b/docs/gpu-asset-headers.md index c43b22a4596..0f3f8a6fd4e 100644 --- a/docs/gpu-asset-headers.md +++ b/docs/gpu-asset-headers.md @@ -22,10 +22,12 @@ Detection reads the bytes, so the file name does not matter: FourCC, RGB bit count and caps. When the FourCC is `DX10`, the root also reports the DXGI format number and name, the resource dimension, misc flags and the array size. The payload after the header is one region, - `payload.bin`. For block-compressed formats (BC1–BC7, DXT1–DXT5, ATI1/ATI2) - and common uncompressed formats, `expected_payload_bytes` is derived from - the dimensions, mips, cube faces, array layers and volume depth. Otherwise - it is null. Legacy signed FourCCs `BC4S` and `BC5S` use 8 and 16 + `payload.bin`. For block-compressed formats (BC1–BC7, DXT1–DXT5, ATI1/ATI2), + DXGI formats with a fixed number of bytes per texel and legacy uncompressed + formats, `expected_payload_bytes` is derived from the dimensions, mips, cube + faces, array layers and volume depth. Otherwise it is null; R1_UNORM, the + packed R8G8_B8G8 and G8R8_G8B8 formats and planar video formats are not + derived. Legacy signed FourCCs `BC4S` and `BC5S` use 8 and 16 bytes per 4×4 block, respectively. - **GLB**: version, declared length and the chunk table. The `asset.version` and `asset.generator` fields come from the JSON chunk, along with the diff --git a/src/artifacts/gpuAsset/GpuAssetArtifactReader.test.ts b/src/artifacts/gpuAsset/GpuAssetArtifactReader.test.ts index c8a169e4f95..20a59ee6d29 100644 --- a/src/artifacts/gpuAsset/GpuAssetArtifactReader.test.ts +++ b/src/artifacts/gpuAsset/GpuAssetArtifactReader.test.ts @@ -12,6 +12,7 @@ import { } from "./GpuAssetArtifactReader.js"; import { ddsBc7Fixture, + ddsDx10Fixture, GLB_IMAGE, glbFixture, ktx2Fixture, @@ -93,6 +94,61 @@ describe("DDS headers", () => { }, ); + it.each([ + [1, 16], + [4, 16], + [5, 12], + [8, 12], + [9, 8], + [22, 8], + [23, 4], + [47, 4], + [67, 4], + [87, 4], + [93, 4], + [48, 2], + [59, 2], + [85, 2], + [86, 2], + [115, 2], + [60, 1], + [65, 1], + ] as const)( + "checks the payload extent of DXGI format %i at %i bytes per texel", + async (dxgiFormat, texelBytes) => { + const header = ddsDx10Fixture(dxgiFormat, 0); + header.writeUInt32LE(4, 12); + header.writeUInt32LE(4, 16); + const expected = 16 * texelBytes; + for (const [difference, extent] of [ + [0, "consistent"], + [-1, "truncated"], + [1, "trailing-bytes"], + ] as const) { + const payload = Buffer.alloc(expected + difference, 0x55); + const result = await read("dds", Buffer.concat([header, payload])); + expect(result.metadata).toMatchObject({ + dx10: { dxgi_format: dxgiFormat }, + expected_payload_bytes: expected, + payload_bytes: payload.length, + extent_check: extent, + }); + } + }, + ); + + it.each([66, 68, 69, 100, 114])( + "does not derive a payload size for DXGI format %i", + async (dxgiFormat) => { + const result = await read("dds", ddsDx10Fixture(dxgiFormat, 17)); + expect(result.metadata).toMatchObject({ + expected_payload_bytes: null, + payload_bytes: 17, + extent_check: "consistent", + }); + }, + ); + it("reports DX10 facts and a payload matching the derived size", async () => { const result = await read("dds", ddsBc7Fixture()); expect(result.metadata).toMatchObject({ diff --git a/src/artifacts/gpuAsset/GpuAssetLayouts.ts b/src/artifacts/gpuAsset/GpuAssetLayouts.ts index 780b966865e..9040ddbbe47 100644 --- a/src/artifacts/gpuAsset/GpuAssetLayouts.ts +++ b/src/artifacts/gpuAsset/GpuAssetLayouts.ts @@ -40,10 +40,19 @@ const DXGI_BLOCK_BYTES = new Map([ (id) => [id, 16] as const, ), ]); +const dxgiRange = (first: number, last: number): number[] => + Array.from({ length: last - first + 1 }, (_, index) => first + index); +// Formats with a fixed number of bytes per texel. R1_UNORM, the packed 2x1 +// R8G8_B8G8/G8R8_G8B8 formats and planar video formats are not listed. const DXGI_PIXEL_BYTES = new Map([ - ...[1, 2, 3, 4].map((id) => [id, 16] as const), - ...[9, 10, 11, 12, 13, 14].map((id) => [id, 8] as const), - ...[27, 28, 29, 30, 31, 32, 87, 91].map((id) => [id, 4] as const), + ...dxgiRange(1, 4).map((id) => [id, 16] as const), + ...dxgiRange(5, 8).map((id) => [id, 12] as const), + ...dxgiRange(9, 22).map((id) => [id, 8] as const), + ...[...dxgiRange(23, 47), 67, ...dxgiRange(87, 93)].map( + (id) => [id, 4] as const, + ), + ...[...dxgiRange(48, 59), 85, 86, 115].map((id) => [id, 2] as const), + ...dxgiRange(60, 65).map((id) => [id, 1] as const), ]); const DXGI_NAMES = new Map([ [2, "R32G32B32A32_FLOAT"], diff --git a/src/artifacts/gpuAsset/gpuAsset.fixture.ts b/src/artifacts/gpuAsset/gpuAsset.fixture.ts index b205a3a4e5e..ad5165416b1 100644 --- a/src/artifacts/gpuAsset/gpuAsset.fixture.ts +++ b/src/artifacts/gpuAsset/gpuAsset.fixture.ts @@ -12,8 +12,11 @@ const u32s = (...values: number[]): Buffer => { return bytes; }; -/** DDS with a DX10 header: DXGI_FORMAT_BC7_UNORM (98), 256x256, one mip. */ -export const ddsBc7Fixture = (payloadBytes = 65_536): Buffer => { +/** DDS with a DX10 header for a 256x256 texture with one mip. */ +export const ddsDx10Fixture = ( + dxgiFormat: number, + payloadBytes = 65_536, +): Buffer => { const pixelFormat = Buffer.concat([ u32s(32, 0x4), Buffer.from("DX10", "latin1"), @@ -28,11 +31,15 @@ export const ddsBc7Fixture = (payloadBytes = 65_536): Buffer => { return Buffer.concat([ Buffer.from("DDS ", "latin1"), header, - u32s(98, 3, 0, 1, 0), + u32s(dxgiFormat, 3, 0, 1, 0), fill(payloadBytes), ]); }; +/** DDS with a DX10 header: DXGI_FORMAT_BC7_UNORM (98), 256x256, one mip. */ +export const ddsBc7Fixture = (payloadBytes = 65_536): Buffer => + ddsDx10Fixture(98, payloadBytes); + /** The 8-byte PNG signature and a fake IHDR, stored as a GLB image. */ export const GLB_IMAGE = Buffer.concat([ Buffer.from([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a]), diff --git a/tests/acceptance/gpuAssetHeadersCli.test.ts b/tests/acceptance/gpuAssetHeadersCli.test.ts index 977181daec3..738f156e492 100644 --- a/tests/acceptance/gpuAssetHeadersCli.test.ts +++ b/tests/acceptance/gpuAssetHeadersCli.test.ts @@ -6,6 +6,7 @@ import { cliTest } from "../support/cli/cliFixture.js"; import { createTestTempDirectory } from "../fixtures/temporaryDirectory.js"; import { ddsBc7Fixture, + ddsDx10Fixture, glbFixture, ktx2Fixture, ktxFixture, @@ -118,6 +119,53 @@ cliTest( }, ); +cliTest.for([ + { + label: "a complete R8_UINT payload", + dxgiFormat: 62, + payloadBytes: 65_536, + expected: 65_536, + extent: "consistent", + }, + { + label: "an R8_UINT header with no payload", + dxgiFormat: 62, + payloadBytes: 0, + expected: 65_536, + extent: "truncated", + }, +] as const)( + "checks the DDS extent of $label through the CLI", + async ({ dxgiFormat, payloadBytes, expected, extent }, { cli }) => { + const directory = await createTestTempDirectory("rea-dds-extent-cli-"); + const source = join(directory, "texture.dds"); + await writeFile(source, ddsDx10Fixture(dxgiFormat, payloadBytes)); + const inspected = await cli.run({ + arguments: ["inspect-artifact", source, "--json"], + }); + expect(inspected.exitCode, inspected.stdout + inspected.stderr).toBe(0); + const inspection = artifactInspectionResultSchema.parse( + parseEvidence(inspected.json).normalized_result, + ); + const graph = artifactInventoryResultSchema.parse( + inspection.substeps[0]?.evidence.normalized_result, + ); + expect(graph.coverage.status).toBe( + extent === "consistent" ? "complete" : "unknown", + ); + expect( + graph.occurrences.find((row) => row.logical_path === ".") + ?.format_metadata, + ).toMatchObject({ + kind: "dds", + dx10: { dxgi_format: dxgiFormat }, + payload_bytes: payloadBytes, + expected_payload_bytes: expected, + extent_check: extent, + }); + }, +); + cliTest("refuses a GLB whose first chunk is not JSON", async ({ cli }) => { const directory = await createTestTempDirectory("rea-glb-malformed-cli-"); const source = join(directory, "bad.glb");