Files
rea/src/domain/formatMetadata/gpuAssetMetadata.ts
T
morluto 78d2593a45 feat(artifacts): report declared DDS, GLB, KTX and KTX2 headers and regions (#14248)
* feat(artifacts): report declared DDS, GLB, KTX and KTX2 headers and regions

Recognize DDS (including the DX10 extension), GLB, KTX 1.1 and KTX 2.0
by their header signatures and inventory them as containers. The root
format metadata carries the declared header facts. Each declared byte
region is a member that is hashed, classified and extractable: the DDS
payload; GLB JSON and BIN chunks and BIN-stored images; KTX key/value
data and mip levels; and KTX2 DFD, KVD, SGD and levels.

Nothing is decoded. A DDS payload is checked against the size derived
from its dimensions, mips, faces, layers and block size. Disagreeing
extents are reported in extent_check with unknown coverage rather than
refused, and regions past the end of the file are dropped, not clipped.
Structural header faults refuse as malformed input with the format and
offset.

Part of #14155

* fix(gpu-assets): bound GLB chunks and keep unexamined regions unknown

- Stop the GLB chunk walk at 1,024 chunks with unknown coverage.
- Refuse a GLB chunk that crosses the declared length, or a chunk table
  that stops short of a physically present declared end, as malformed.
- Report coverage unknown when GLB images were not all listed: past the
  image allowance, an unparsed JSON chunk, or an unresolved bufferView.
- Leave the DDS payload size unknown when rows are padded to a declared
  pitch or more than 32 mip levels are declared, instead of deriving it
  from different dimensions.

* fix(gpu-assets): report GLB images in URI-backed buffers as external

Only buffers[0] without a uri is the GLB BIN chunk. An image whose
bufferView sits in a buffer with a uri, or an image named by uri, is
stored outside the file; it was counted as an unresolved image and made
coverage unknown. Such images are now counted in external_images and
leave coverage complete; only views that resolve to no buffer stay
unknown.

* fix(gpu-assets): report KTX2 level-index failures at file offsets

The 64-bit level-index fields were read from a buffer that starts at file
offset 80, but an unsafe value reported its index-relative position.
The reader now takes the buffer's file base and reports base plus
position, so every failure offset is a file offset.

* fix(gpu-assets): count a GLB image as external only for a URI string

An image or buffer whose uri is present but not a string, and an image
with neither a uri nor a bufferView, were counted in external_images and
left coverage complete without an established source. Only a URI string
now marks a source external; other sources stay unresolved and keep
coverage unknown.
2026-11-24 04:28:08 +00:00

146 lines
4.4 KiB
TypeScript

import { z } from "zod";
import { offset, uint32 } from "./primitives.js";
/**
* Whether declared extents agree with the file: `consistent`, `truncated`
* (a declared region ends past the file) or `trailing-bytes` (bytes follow
* every declared region). A disagreement is a fact, not a refusal.
*/
const extentCheck = z.enum(["consistent", "truncated", "trailing-bytes"]);
/** DDS_HEADER and, when present, DDS_HEADER_DXT10 fields as declared. */
export const ddsMetadataSchema = z.strictObject({
kind: z.literal("dds"),
header_flags: uint32,
height: uint32,
width: uint32,
depth: uint32,
pitch_or_linear_size: uint32,
mip_map_count: uint32,
pixel_format_flags: uint32,
/** Four printable characters, or null when the FOURCC flag is clear. */
four_cc: z.string().nullable(),
rgb_bit_count: uint32,
caps: uint32,
caps2: uint32,
dx10: z
.strictObject({
dxgi_format: uint32,
/** Name from the public DXGI_FORMAT enum, when REA lists it. */
dxgi_format_name: z.string().nullable(),
resource_dimension: uint32,
misc_flag: uint32,
array_size: uint32,
misc_flags2: uint32,
})
.nullable(),
payload_offset: offset,
payload_bytes: offset,
/** Derived from dimensions, mips, faces and block size; null when the format's size is not derived. */
expected_payload_bytes: offset.nullable(),
extent_check: extentCheck,
});
export type DdsMetadata = z.infer<typeof ddsMetadataSchema>;
/** The GLB container header, chunk table and counts from its JSON chunk. */
export const glbMetadataSchema = z.strictObject({
kind: z.literal("glb"),
version: uint32,
declared_length: uint32,
chunks: z.array(
z.strictObject({
/** `JSON`, `BIN` or the raw type as eight hex digits. */
type: z.string(),
offset,
length: uint32,
}),
),
/** `asset.version` and `asset.generator`, as declared. */
asset_version: z.string().nullable(),
asset_generator: z.string().nullable(),
/** Array lengths of the top-level glTF collections that are present. */
counts: z.record(z.string(), offset),
extensions_used: z.array(z.string()),
/** Why the JSON chunk was not read for counts, when it was not. */
json_status: z.enum(["parsed", "invalid", "limit"]),
/**
* Images stored outside the GLB: named by a URI, or in a buffer that has a
* URI. They are not regions of this file.
*/
external_images: offset,
extent_check: extentCheck,
});
export type GlbMetadata = z.infer<typeof glbMetadataSchema>;
/** KTX 1.1 header fields as declared. */
export const ktxMetadataSchema = z.strictObject({
kind: z.literal("ktx"),
endianness: z.enum(["little", "big"]),
gl_type: uint32,
gl_type_size: uint32,
gl_format: uint32,
gl_internal_format: uint32,
gl_base_internal_format: uint32,
pixel_width: uint32,
pixel_height: uint32,
pixel_depth: uint32,
array_elements: uint32,
faces: uint32,
mip_levels: uint32,
key_value_bytes: uint32,
/** Mip levels whose image sizes were read before the file ended. */
levels_read: uint32,
extent_check: extentCheck,
});
export type KtxMetadata = z.infer<typeof ktxMetadataSchema>;
const ktx2Range = z.strictObject({ offset, length: offset });
/** KTX 2.0 header and index fields as declared. */
export const ktx2MetadataSchema = z.strictObject({
kind: z.literal("ktx2"),
vk_format: uint32,
type_size: uint32,
pixel_width: uint32,
pixel_height: uint32,
pixel_depth: uint32,
layer_count: uint32,
face_count: uint32,
level_count: uint32,
supercompression_scheme: uint32,
/** `none`, `BasisLZ`, `Zstandard` or `ZLIB`; null for a scheme REA does not name. */
supercompression_name: z.string().nullable(),
dfd: ktx2Range,
kvd: ktx2Range,
sgd: ktx2Range,
levels: z.array(
z.strictObject({
offset,
length: offset,
uncompressed_length: offset,
}),
),
extent_check: extentCheck,
});
export type Ktx2Metadata = z.infer<typeof ktx2MetadataSchema>;
/** One declared region of a GPU asset: its role and position in the declaration. */
export const gpuAssetRegionMetadataSchema = z.strictObject({
kind: z.literal("gpu-asset-region"),
role: z.enum([
"payload",
"json-chunk",
"bin-chunk",
"chunk",
"image",
"key-value-data",
"data-format-descriptor",
"supercompression-global-data",
"level",
]),
index: uint32,
});
export type GpuAssetRegionMetadata = z.infer<
typeof gpuAssetRegionMetadataSchema
>;