diff --git a/dm/gfx/nvn/hle_nvn.c b/dm/gfx/nvn/hle_nvn.c index 66b591af62..d494373863 100644 --- a/dm/gfx/nvn/hle_nvn.c +++ b/dm/gfx/nvn/hle_nvn.c @@ -352,7 +352,7 @@ NVN(nvnTextureBuilderSetStride, void, NVNtextureBuilder *b, ptrdiff_t s) { TB(b) NVN(nvnTextureBuilderSetStorage, void, NVNtextureBuilder *b, NVNmemoryPool *p, ptrdiff_t off) { TB(b)->pool = (pool_t *)p; TB(b)->off = off; } NVN(nvnTextureBuilderSetPackagedTextureLayout, void, NVNtextureBuilder *b, const NVNpackagedTextureLayout *l) { TB(b)->has_layout = l != NULL; if (l) memcpy(TB(b)->layout, l->layout, 8); - TB(b)->d.pkg_block_h = l ? l->layout[0] + 1 : 0; + TB(b)->d.pkg_block_h = l ? (l->layout[0] | (l->layout[1] & 0xf) << 8) + 1 : 0; } NVN(nvnTextureBuilderGetFlags, NVNtextureFlags, const NVNtextureBuilder *b) { return (NVNtextureFlags)((const tex_builder_t *)b)->d.flags; } static uint64_t g_layout_logged; diff --git a/dm/gfx/nvn/nvn_tex.c b/dm/gfx/nvn/nvn_tex.c index 4e41bd7bb6..2dcf102ef9 100644 --- a/dm/gfx/nvn/nvn_tex.c +++ b/dm/gfx/nvn/nvn_tex.c @@ -3,6 +3,7 @@ // one GOB = 64 bytes × 8 rows = 512 bytes; the block height (tileH, in GOBs) is picked by 'height×1.5'; levels are accumulated with block alignment. // The game's packaged textures (SetPackagedTextureLayout) carry 8 bytes of layout parameters: byte 0's low nibble = log2 of the block height (derived by comparing 16 cases // from the real game, 4 of which differ from the automatic algorithm), its high nibble = log2 of the block depth (3D); when present they take precedence. +// Byte 1 low nibble is log2 width alignment in GOBs (Bayonetta Origins SDK: SetPackagedTextureLayout stores H/D/W; its layout arithmetic pads full tiles and aligns array layers). // Linear storage counts pixel rows, also for block-compressed formats. The block depth nibble and the pixel rows were found by digitalhivehub against the original // NVN driver (nxnative PR #11) and taken from MacNativeGames 3c482a7, with the shapes of t_tex_layout. #include "nvn_tex.h" @@ -34,6 +35,10 @@ int nvn_fmt_bpb(int f, int *bw, int *bh) { fmt_t t = fmt_of(f); if (bw) *bw = t. static uint32_t mip(uint32_t s, unsigned l) { s >>= l; return s ? s : 1; } static uint32_t blk(uint32_t s, uint32_t b) { return (s + b - 1) / b; } static unsigned pick_tile(uint32_t gobs) { return gobs >= 16 ? 4 : gobs >= 8 ? 3 : gobs >= 4 ? 2 : gobs >= 2 ? 1 : 0; } +// Width-alignment log2 follows the H/D nibbles; pkg_block_h stores the layout bits plus one. +static unsigned packaged_width_log2(const nvn_texdesc *d) { + return d->pkg_block_h ? ((unsigned)(d->pkg_block_h - 1) >> 8) & 0xf : 0; +} // The automatic block height (no packaged layout): NVN truncates height in blocks × 1.5 / 8 before picking the power of two (found by MacNativeGames; Touchable checked // 16 such textures of Rhythm Heaven Groove). Rounding it up, as this file did until then, gave one step more at 6–10, 17–21, 38–42 and 81–85 block rows: Rhythm Heaven // Groove's 672×160 and 1232×160 BC7 textures (40 block rows) need 4 GOBs, not 8 (the calibration screen's beat numbers came out torn); its 112×112, 288×208 and 880×432 agree @@ -96,8 +101,11 @@ static uint64_t level_walk(const nvn_texdesc *d, unsigned level, unsigned dpl, u unsigned tileH, unsigned tileD, fmt_t t, uint32_t *gobs) { uint32_t sx, sy; samples_xy(d->samples, &sx, &sy); unsigned sh = tile_shape_h(d, dpl, t, tileH); - uint32_t tw = 128u >> (sh + tileD); if (!tw) tw = 1; // GOB columns of a 64 KB tile - int tiled = sparse_of(d); // still in a sparse texture's tiled levels + unsigned pw = packaged_width_log2(d); + uint32_t tw = pw ? 1u << pw : 128u >> (sh + tileD); if (!tw) tw = 1; + // Packaged width alignment is independent of SPARSE. The original driver stores + // layout nibbles H/D/W, and pads full tiles until the first smaller mip. + int tiled = sparse_of(d) || pw; // still in a sparse texture's tiled levels uint64_t off = 0; for (unsigned i = 0;; i++) { uint32_t lw = blk(mip(w * sx, i), t.bw); @@ -124,7 +132,8 @@ static uint64_t layer_bytes(const nvn_texdesc *d, unsigned dpl, int layered, uin uint32_t hh = blk(h, t.bh); unsigned th = tileH, td = tileD; while (th && hh <= (8u << (th - 1))) th--; while (td && z <= (1u << (td - 1))) td--; - unsigned sh = th + td + 9; uint64_t g = ls >> sh; + unsigned pw = packaged_width_log2(d); + unsigned sh = pw ? pw + tileH + tileD + 9 : th + td + 9; uint64_t g = ls >> sh; if (ls != g << sh) ls = (g + 1) << sh; } return ls; diff --git a/dm/gfx/nvn/nvn_tex.h b/dm/gfx/nvn/nvn_tex.h index 575caf1280..9b71af6fa2 100644 --- a/dm/gfx/nvn/nvn_tex.h +++ b/dm/gfx/nvn/nvn_tex.h @@ -5,7 +5,7 @@ typedef struct { int target, format, flags, samples; // NVNtextureTarget / NVNformat / NVNtextureFlags / sample count (0 or 1 = single-sampled) uint32_t width, height, depth, levels, stride; - int pkg_block_h; // Packaged byte 0 (low nibble log2 block height, high nibble log2 block depth) + 1; 0 = use the automatic algorithm + int pkg_block_h; // Packaged first 12 bits (log2 GOB height/depth/width nibbles) + 1; 0 = automatic } nvn_texdesc; typedef struct { diff --git a/dm/gfx/tests/t_nvn.c b/dm/gfx/tests/t_nvn.c index f3563dd4b2..9f6d9a2557 100644 --- a/dm/gfx/tests/t_nvn.c +++ b/dm/gfx/tests/t_nvn.c @@ -991,6 +991,24 @@ int main(void) { P(nvnTextureFinalize)(&mt[0]); P(nvnTextureFinalize)(&mt[1]); P(nvnMemoryPoolFinalize)(&mp); } + // Packaged H/D/W nibbles describe physical pitch independently of API SPARSE. + { + NVNtextureBuilder b = {0}; NVNpackagedTextureLayout layout={{3,4,0,0,0,0,0,0}}; + P(nvnTextureBuilderSetDevice)(&b,&dev); + P(nvnTextureBuilderSetDefaults)(&b); P(nvnTextureBuilderSetSize2D)(&b,2052,1026); + P(nvnTextureBuilderSetFormat)(&b,NVN_FORMAT_RGBA_ASTC_6x6); P(nvnTextureBuilderSetFlags)(&b,NVN_TEXTURE_FLAGS_IMAGE); + P(nvnTextureBuilderSetPackagedTextureLayout)(&b,&layout); + EXPECT("packaged width nibble reaches description",0x404,((tex_builder_t*)&b)->d.pkg_block_h); + EXPECT("packaged full tiles use padded pitch",0x120000,P(nvnTextureBuilderGetStorageSize)(&b)); + EXPECT("packaged pitch leaves public flags unchanged",NVN_TEXTURE_FLAGS_IMAGE,P(nvnTextureBuilderGetFlags)(&b)); + NVNtextureSparseTileLayout sparse; P(nvnTextureBuilderGetSparseTileLayout)(&b,&sparse); + EXPECT("packaged pitch does not advertise sparse tiles",0,sparse.numTiledLevels); + layout.layout[1]=0; P(nvnTextureBuilderSetPackagedTextureLayout)(&b,&layout); + EXPECT("ordinary packaged pitch remains unpadded",0x102000,P(nvnTextureBuilderGetStorageSize)(&b)); + P(nvnTextureBuilderSetPackagedTextureLayout)(&b,NULL); + EXPECT("clearing packaged layout clears width too",0,((tex_builder_t*)&b)->d.pkg_block_h); + } + // 6c. Write-protected dirty pages: after a packaged texture is uploaded its pool pages are read-only; game write → page fault → mark dirty → re-upload on next use; a direct host read() write needs prepare first { static uint8_t wb_[256 << 10] __attribute__((aligned(16384))); diff --git a/dm/gfx/tests/t_tex_layout.c b/dm/gfx/tests/t_tex_layout.c index 959d237b42..1531991aa0 100644 --- a/dm/gfx/tests/t_tex_layout.c +++ b/dm/gfx/tests/t_tex_layout.c @@ -1,6 +1,6 @@ // Texture storage against shapes the original NVN driver gives (digitalhivehub ran Tears of the Kingdom's driver under emulation, nxnative PR #11; MacNativeGames 3c482a7): // cube array depth counts layer-faces, packaged byte 0 = block height (low nibble) and block depth (high nibble), linear storage (LINEAR or LINEAR_RENDER_TARGET alone) -// counts pixel rows, also for BC. pkg is byte 0 + 1 (0 = automatic), as nvn_texdesc stores it. The MINIMAL_LAYOUT shapes (flag 0x200) are the driver's from PR #11's t_tex, +// counts pixel rows, also for BC. pkg is the first 12 layout bits + 1 (0 = automatic), as nvn_texdesc stores it. The MINIMAL_LAYOUT shapes (flag 0x200) are the driver's from PR #11's t_tex, // the array and packaged ones from TotK 1.2.1's driver (sdk b9046c31, GitHub issue #183): automatic blocks only when they add no padding to the layer, else 1 GOB. The sparse (flag 0x20) shapes are block heights read off a game's guest bytes instead: a sparse texture at least one 64 KB tile wide and high // takes its tile's block height (384x384 DXT5, 1350x270 BGRA8), a smaller one keeps the automatic one (192x160 DXT5 and 384x384 DXT1, Yoshi's Crafted World) #include @@ -8,6 +8,12 @@ typedef struct { const char *what; int target, format, flags; unsigned w, h, depth, levels, stride; int pkg; uint64_t size, layer, off1; unsigned align, th, td; } shape_t; static const shape_t driver[] = { + // Original Bayonetta Origins SDK arithmetic oracle: H3/D0/W4, no SPARSE flag. + {"packaged ASTC6x6 W4 paper", 1,0x7d,4,2052,1026,1,1,0,0x404,0x120000,0x120000,0,512,3,0}, + {"packaged ASTC6x6 W0 negative control", 1,0x7d,4,2052,1026,1,1,0,4,0x102000,0x102000,0,512,3,0}, + {"packaged ASTC6x6 W4 mip tail", 1,0x7d,4,2052,1026,1,8,0,0x404,0x19dc00,0x19dc00,0x120000,512,3,0}, + {"packaged ASTC6x6 W4 cube tail alignment", 8,0x7d,4,1024,1024,6,8,0,0x404,0x540000,0xe0000,0x90000,512,3,0}, + {"packaged ASTC6x6 W4 array tail alignment", 4,0x7d,4,2052,1026,3,8,0,0x404,0x4e0000,0x1a0000,0x120000,512,3,0}, {"cube array RGBA8 4x4 depth 6", 9, 0x25, 0, 4, 4, 6, 1, 0, 0, 3072, 512, 0, 512, 0, 0}, {"cube array RGBA8 64x64 depth 12, 7 levels", 9, 0x25, 0, 64, 64, 12, 7, 0, 0, 294912, 24576, 16384, 512, 3, 0}, {"packaged 3D BC4 64^3, 7 levels, byte 0x40", 2, 0x49, 0, 64, 64, 64, 7, 0, 0x41, 163328, 163328, 131072, 512, 0, 4}, diff --git a/docs/agents/symptoms.md b/docs/agents/symptoms.md index 8a99faa6db..9ea893d1ba 100644 --- a/docs/agents/symptoms.md +++ b/docs/agents/symptoms.md @@ -666,6 +666,7 @@ One line per symptom in the [subsystem references](../reference/): what you see - lighting that alternates between lit and unlit from one frame to the next on an iPhone, the same scene steady on a Mac; log lines `texture buffer (texture ID …, … at pool offset …) has no 2D-row form for a wrapped 1D fetch: the slot reads 0` and `texture type mismatch: … a buffer texture: the slot reads 0 (dummy)`. → [Buffer texture rows at a pool offset the device will not view](../reference/rendering.md#buffer-texture-rows-at-a-pool-offset-the-device-will-not-view) - torn digits and a background in blue steps (Rhythm Heaven Groove's latency calibration). → [Automatic block height truncates](../reference/rendering.md#automatic-block-height-truncates) - magenta (the ASTC error colour) on streamed textures in a sparse pool; `DM_NVN_ZBLOCK` finds such textures (all-zero blocks). → [Sparse textures laid out in 64 KB tiles](../reference/rendering.md#sparse-textures-laid-out-in-64-kb-tiles) +- a fixed magenta rectangle covers part of a packaged ASTC UI background while its card is open. → [Packaged textures carry width alignment as well as height and depth](../reference/rendering.md#packaged-textures-carry-width-alignment-as-well-as-height-and-depth) - a CPU-filled texture shuffled into a patchwork of rectangles (OCTOPATH TRAVELER's world map on the traveler selection). → [Sparse textures take the block height of their 64 KB tile](../reference/rendering.md#sparse-textures-take-the-block-height-of-their-64-kb-tile) - none visible on an M6 on the Frigate Orpheon's deck. Laid out 1 GOB high, the mapped tiles of a streamed 3D texture fall in scattered places, so lookups read zero in the holes; GitHub issue #783 reports a black gunship hull in the opening on an M2. → [Sparse 3D textures take the standard 3D tile](../reference/rendering.md#sparse-3d-textures-take-the-standard-3d-tile) - crashes that move between runs (30–70% of boots, in the first minute): in the allocator, on bogus pointers, in unrelated threads. Unreal's purgatory proxy (05-debugging) finds freed 64 KB-aligned blocks modified; with the parked blocks read-only, the faulting store is a `memcpy` of texture mip data from the texture streamer into the buffer `RHILockTexture2D` returned. → [Sparse textures of 8- and 16-byte blocks: blocks at most 8 GOBs high](../reference/rendering.md#sparse-textures-of-8--and-16-byte-blocks-blocks-at-most-8-gobs-high) diff --git a/docs/reference/rendering.md b/docs/reference/rendering.md index 18dccd33f7..44d8282771 100644 --- a/docs/reference/rendering.md +++ b/docs/reference/rendering.md @@ -225,6 +225,14 @@ The replay on Metal (`dm/gfx/nvn/nvn_mtl.m`) and the Vulkan and OpenGL layers. E - **Fix:** `nvn_tex.c` `level_walk`: a level at least one tile wide, high and deep is laid out with its width padded to whole tiles, the levels from the first smaller one on form the mip tail (packed as usual), and the storage is whole tiles, 64 KB aligned. - **Found on:** [Monster Hunter Rise](../../dm/games/0100B04011742000/README.md): laid out unpadded, the 1024×1024 ASTC 6×6 clothes of Hinoa and Minoto (43 GOBs wide instead of 48, level 1 at 0x81000 instead of 0x90000) read 2,183 all-zero blocks in level 0 and 3,043 in level 1, which ASTC decodes as its error colour: their trousers, sleeves and gloves were magenta, worst in Kamura Village. +### Packaged textures carry width alignment as well as height and depth + +- **Symptom:** a fixed magenta rectangle covers part of a packaged ASTC UI background while its card is open. +- **Cause:** `SetPackagedTextureLayout` kept only byte 0 in the texture description. The original driver consumes height, depth and width-alignment log2 nibbles from the first 12 bits. Width alignment pads full tiles until the mip tail independently of the API `SPARSE` flag. Dropping it changes row pitch, mip offsets and array-layer alignment; zero padding read as ASTC decodes magenta. +- **Fix:** preserve all three nibbles in the existing description integer, retaining its plus-one explicit-layout sentinel. Use the width alignment for packaged full tiles and array layers. Public flags, sparse-tile reporting, W0 layouts and the shader translation chain are unchanged. The texture-sharing key already includes the description. +- **Found on:** Bayonetta Origins: Cereza and the Lost Demon (`0100CF5010FEC000`), using an unregistered provisional profile. The introduction-card background is 2052x1026 ASTC 6x6, H3/D0/W4: native storage is `0x120000`, with 96 GOB columns; the old result is `0x102000`, with 86. At source y384 the wrong pitch reads ten zero padding columns, matching the observed 240-pixel-wide magenta area. Original-asset GPU replay reproduces the error and its correction. The user confirms the original purple block no longer appears on Duo; this is a same-scene user report, not full-game or cross-game validation. +- **Check:** `t_tex_layout` covers W0/W4, mip-tail and array/cube cases; `t_nvn` covers builder propagation and unchanged flags/sparse reporting. A separate local comparison against the original SDK arithmetic passed 37,296 shape/format/layout cases, and local original-asset GPU replay passed 76 checks. Those local checks require privately owned game material and are not distributed as repository test fixtures. + ### Sparse textures take the block height of their 64 KB tile - **Symptom:** a CPU-filled texture shuffled into a patchwork of rectangles (OCTOPATH TRAVELER's world map on the traveler selection).