Skip to content

A LINEAR texture's WriteTexels writes it linear: Tsukihime's title movie backdrop was black - #1983

Closed
beeven wants to merge 2 commits into
seedds:mainfrom
beeven:fix/write-texels-linear
Closed

beeven wants to merge 2 commits into
seedds:mainfrom
beeven:fix/write-texels-linear

Conversation

@beeven

@beeven beeven commented Nov 9, 2026

Copy link
Copy Markdown
Contributor

The symptom

Tsukihime (01001DC01486A000) renders its title screen with a black backdrop: the moon-and-clouds background is a fullscreen movie, and the game samples the texture the host gave the video decoder as if it were empty. Games whose movies deliver into a CPU buffer (the NV12 copy path) are unaffected: the decoder's native-texture output is the broken path.

The cause

The video decoder's output textures are NVN_TEXTURE_FLAGS_LINEAR | NVN_TEXTURE_FLAGS_VIDEO_DECODE (flags 0x12, RGBA8 1920x1080). write_texels (nvnTextureWriteTexels / Swizzled, what the decoder uses to deliver a frame) feeds every region to write_region_bl, whose geometry refuses a LINEAR layout (nvn_deswizzle_geom returns -1 for the linear flags), so the write falls to the swizzle fallback - which reinterprets the linear storage as block-linear: a whole-surface 1920x1080 region memcpys only hb = height/8 = 135 block-rows of misplaced bytes, then nvn_swizzle "swizzles" the linear storage over itself. The row-based upload of a LINEAR texture then shows a texture that stayed empty: the movie draws black.

write_region_linear and the flag dispatch already exist - write_region_mirror uses them for the CopyTextureToTexture mirror path since 3870fcc - but WriteTexels never took it. The first commit routes WriteTexels' region loop through write_region_mirror like the copy mirror does: block-linear textures take exactly the same path as before (the dispatch selects write_region_bl when the flag is clear), LINEAR textures get row-stride writes.

Evidence

With the second commit's DM_MOVIE_DBG probe, on the unpatched host each title-movie frame shows the write going in and not sticking: fmt=0x25 flags=0x12, swizzle-fallback, src_nz=129600 dst_nz=0 - the source holds the frame, the storage does not. Patched, the same writes report mirror: src_nz=129600 dst_nz=129600 - every frame of the title movie lands - and the title screen renders its backdrop:

Tsukihime title after the fix

The probe stays off unless DM_MOVIE_DBG is set (registered in docs/env_switches.tsv; one line per CPU texture write: which path wrote it, and a nonzero-byte sample of source vs storage), so a future "movie draws black" report can be triaged from one log line.

Tested on the mac host (M4 Pro, macOS 27.2): title screen, opening movie, in-game event movies and gallery CGs all render; the buffer-copy movie path never went through WriteTexels and is unchanged.

Mino added 2 commits November 9, 2026 21:14
…ck-linear one: the video decoder's output pool (Tsukihime's movies) carries NVN_TEXTURE_FLAGS_LINEAR | NVN_TEXTURE_FLAGS_VIDEO_DECODE, and write_region_bl's geometry refuses a LINEAR layout, so the write fell to the swizzle fallback, which reinterprets the linear storage as block-linear (a whole 1920x1080 frame writes a few thousand misplaced bytes, hb being the GOB-row count) and the row-based upload shows the texture as empty: a black movie backdrop. Dispatching on the texture's flags is what the CopyTextureToTexture mirror already did
…yte sample, for the games whose movies draw black
@github-actions github-actions Bot added nvn NVN on Metal and its tests (dm/gfx/nvn, dm/gfx/tests) docs Documentation labels Nov 9, 2026
seedds added a commit that referenced this pull request Nov 11, 2026
…e-texture movie into a LINEAR texture is no longer black: write_texels wrote every texture by block-linear address, nvn_deswizzle_geom refuses a LINEAR or LINEAR_RENDER_TARGET layout, so write_region_bl failed, and the whole-level fallback's nvn_swizzle refused it too with its result ignored: the storage kept what it held, with no log line for a whole-texture write. The host's native-texture movie output (nvn_host_write_texture) goes through it, so TSUKIHIME -A piece of blue glass moon- (01001DC01486A000, no profile in the repository) drew its title backdrop, a 1920x1080 RGBA8 LINEAR | VIDEO_DECODE movie texture, black. Now a linear texture is one 2D level of rows of its stride (tex_linear_row, the stride GetStorageSize counts, now shared by ReadTexels, the upload, gsb_geom and linear_fit), written as ReadTexels reads it; a region outside those rows, another target or level, or a block-compressed format is refused with a [RED] line, as is a block-linear write nvn_swizzle refuses. mtl_tex_linear_cpu_written leaves a texture viewed on its pool's buffer (linear_fit) as that view, which reads the storage itself (made CPU-written it would be uploaded whole at each write); any other is taken from its storage as tex_mtl_ takes one at first use. The diagnosis is PR #1983's (the probe showing each title-movie frame in the source and not in the storage, and the author's Mac run of the title, the opening movie, event movies and gallery CGs with the fix); changed here: the PR's write_region_mirror no longer exists on main (ab41d6a), LINEAR_RENDER_TARGET is covered too, the buffer view is no longer made CPU-written, the DM_MOVIE_DBG probe is left out for the [RED] lines. New reference entry (WriteTexels into a linear texture dropped the write); movies.md no longer says the output is always block-linear. Checked: t_nvn 311 checks with the new 6c2b (a 37x5 LINEAR | VIDEO_DECODE texture of stride 256 and a 16x16 LINEAR_RENDER_TARGET of stride 128, four regions each, some with a padded source stride: storage byte for byte with the row padding and the bytes after, a region past the last row refused, ReadTexels back, no CPU-written texture added for the buffer views; without the linear branch 3 fail, with mtl_tex_cpu_written in place of the view rule the last fails), t_linear_readback 61 cases, t_cpu_storage 135, t_cpu_tex 23, t_direct_upload 26,384, t_partial_upload 3,585, test_docs; lint_c.py tidy on the changed sources: nothing above main's own count (nvn_mtl.m's nullability pair is 18 on main as here). Not run: any game (Tsukihime is not here), the full Mac host build, iOS, regress.py (only linear textures change, which were not written before). Open: a LINEAR_RENDER_TARGET or IMAGE linear texture the GPU drew into as a private texture and the CPU then partly writes is uploaded from its storage, as a block-linear one is (PR #1983)

Co-authored-by: Beeven <5119686+beeven@users.noreply.github.com>
@seedds

seedds commented Nov 11, 2026

Copy link
Copy Markdown
Owner

Thanks: the diagnosis was right, and it landed on main as 9d60776, with you credited as co-author.

It was rewritten rather than merged because write_region_mirror / write_region_linear were removed from main by ab41d6a the day after this PR, so the branch no longer built. What changed against the PR:

  • write_texels writes a linear texture by rows of its stride (tex_linear_row, now shared with ReadTexels, the upload and the buffer view), for LINEAR_RENDER_TARGET too, not only LINEAR.
  • A texture viewed on its pool's buffer is not marked CPU-written after the write: it reads its storage directly, and marking it would have made a 1080p movie a full 8 MB upload every frame. Other linear textures are marked as before.
  • One detail of the description: the swizzle fallback wrote nothing at all, since nvn_swizzle refuses a linear layout through the same check. The storage stayed as it was, which matches your probe's dst_nz=0.
  • DM_MOVIE_DBG is left out. Instead, a refused write now gets a [RED] line, both a linear region outside its rows and a block-linear write nvn_swizzle refuses. That refusal was ignored silently before, which is why this bug left no trace in the log.
  • t_nvn 6c2b checks the linear writes byte for byte. The reference entry is "WriteTexels into a linear texture dropped the write" in docs/reference/nvn-objects.md.

Not run here: Tsukihime itself, which isn't on this machine. A retest of the title screen, the opening movie and the gallery on current main would be welcome.

@seedds seedds closed this Nov 11, 2026
kingbumi31 pushed a commit to kingbumi31/no-lag-kart that referenced this pull request Nov 15, 2026
…e-texture movie into a LINEAR texture is no longer black: write_texels wrote every texture by block-linear address, nvn_deswizzle_geom refuses a LINEAR or LINEAR_RENDER_TARGET layout, so write_region_bl failed, and the whole-level fallback's nvn_swizzle refused it too with its result ignored: the storage kept what it held, with no log line for a whole-texture write. The host's native-texture movie output (nvn_host_write_texture) goes through it, so TSUKIHIME -A piece of blue glass moon- (01001DC01486A000, no profile in the repository) drew its title backdrop, a 1920x1080 RGBA8 LINEAR | VIDEO_DECODE movie texture, black. Now a linear texture is one 2D level of rows of its stride (tex_linear_row, the stride GetStorageSize counts, now shared by ReadTexels, the upload, gsb_geom and linear_fit), written as ReadTexels reads it; a region outside those rows, another target or level, or a block-compressed format is refused with a [RED] line, as is a block-linear write nvn_swizzle refuses. mtl_tex_linear_cpu_written leaves a texture viewed on its pool's buffer (linear_fit) as that view, which reads the storage itself (made CPU-written it would be uploaded whole at each write); any other is taken from its storage as tex_mtl_ takes one at first use. The diagnosis is PR seedds#1983's (the probe showing each title-movie frame in the source and not in the storage, and the author's Mac run of the title, the opening movie, event movies and gallery CGs with the fix); changed here: the PR's write_region_mirror no longer exists on main (ab41d6a), LINEAR_RENDER_TARGET is covered too, the buffer view is no longer made CPU-written, the DM_MOVIE_DBG probe is left out for the [RED] lines. New reference entry (WriteTexels into a linear texture dropped the write); movies.md no longer says the output is always block-linear. Checked: t_nvn 311 checks with the new 6c2b (a 37x5 LINEAR | VIDEO_DECODE texture of stride 256 and a 16x16 LINEAR_RENDER_TARGET of stride 128, four regions each, some with a padded source stride: storage byte for byte with the row padding and the bytes after, a region past the last row refused, ReadTexels back, no CPU-written texture added for the buffer views; without the linear branch 3 fail, with mtl_tex_cpu_written in place of the view rule the last fails), t_linear_readback 61 cases, t_cpu_storage 135, t_cpu_tex 23, t_direct_upload 26,384, t_partial_upload 3,585, test_docs; lint_c.py tidy on the changed sources: nothing above main's own count (nvn_mtl.m's nullability pair is 18 on main as here). Not run: any game (Tsukihime is not here), the full Mac host build, iOS, regress.py (only linear textures change, which were not written before). Open: a LINEAR_RENDER_TARGET or IMAGE linear texture the GPU drew into as a private texture and the CPU then partly writes is uploaded from its storage, as a block-linear one is (PR seedds#1983)

Co-authored-by: Beeven <5119686+beeven@users.noreply.github.com>
(cherry picked from commit 9d60776)
kingbumi31 pushed a commit to kingbumi31/no-lag-kart that referenced this pull request Nov 15, 2026
…e-texture movie into a LINEAR texture is no longer black: write_texels wrote every texture by block-linear address, nvn_deswizzle_geom refuses a LINEAR or LINEAR_RENDER_TARGET layout, so write_region_bl failed, and the whole-level fallback's nvn_swizzle refused it too with its result ignored: the storage kept what it held, with no log line for a whole-texture write. The host's native-texture movie output (nvn_host_write_texture) goes through it, so TSUKIHIME -A piece of blue glass moon- (01001DC01486A000, no profile in the repository) drew its title backdrop, a 1920x1080 RGBA8 LINEAR | VIDEO_DECODE movie texture, black. Now a linear texture is one 2D level of rows of its stride (tex_linear_row, the stride GetStorageSize counts, now shared by ReadTexels, the upload, gsb_geom and linear_fit), written as ReadTexels reads it; a region outside those rows, another target or level, or a block-compressed format is refused with a [RED] line, as is a block-linear write nvn_swizzle refuses. mtl_tex_linear_cpu_written leaves a texture viewed on its pool's buffer (linear_fit) as that view, which reads the storage itself (made CPU-written it would be uploaded whole at each write); any other is taken from its storage as tex_mtl_ takes one at first use. The diagnosis is PR seedds#1983's (the probe showing each title-movie frame in the source and not in the storage, and the author's Mac run of the title, the opening movie, event movies and gallery CGs with the fix); changed here: the PR's write_region_mirror no longer exists on main (ab41d6a), LINEAR_RENDER_TARGET is covered too, the buffer view is no longer made CPU-written, the DM_MOVIE_DBG probe is left out for the [RED] lines. New reference entry (WriteTexels into a linear texture dropped the write); movies.md no longer says the output is always block-linear. Checked: t_nvn 311 checks with the new 6c2b (a 37x5 LINEAR | VIDEO_DECODE texture of stride 256 and a 16x16 LINEAR_RENDER_TARGET of stride 128, four regions each, some with a padded source stride: storage byte for byte with the row padding and the bytes after, a region past the last row refused, ReadTexels back, no CPU-written texture added for the buffer views; without the linear branch 3 fail, with mtl_tex_cpu_written in place of the view rule the last fails), t_linear_readback 61 cases, t_cpu_storage 135, t_cpu_tex 23, t_direct_upload 26,384, t_partial_upload 3,585, test_docs; lint_c.py tidy on the changed sources: nothing above main's own count (nvn_mtl.m's nullability pair is 18 on main as here). Not run: any game (Tsukihime is not here), the full Mac host build, iOS, regress.py (only linear textures change, which were not written before). Open: a LINEAR_RENDER_TARGET or IMAGE linear texture the GPU drew into as a private texture and the CPU then partly writes is uploaded from its storage, as a block-linear one is (PR seedds#1983)

Co-authored-by: Beeven <5119686+beeven@users.noreply.github.com>
(cherry picked from commit 9d60776)
kingbumi31 pushed a commit to kingbumi31/no-lag-kart that referenced this pull request Nov 15, 2026
…e-texture movie into a LINEAR texture is no longer black: write_texels wrote every texture by block-linear address, nvn_deswizzle_geom refuses a LINEAR or LINEAR_RENDER_TARGET layout, so write_region_bl failed, and the whole-level fallback's nvn_swizzle refused it too with its result ignored: the storage kept what it held, with no log line for a whole-texture write. The host's native-texture movie output (nvn_host_write_texture) goes through it, so TSUKIHIME -A piece of blue glass moon- (01001DC01486A000, no profile in the repository) drew its title backdrop, a 1920x1080 RGBA8 LINEAR | VIDEO_DECODE movie texture, black. Now a linear texture is one 2D level of rows of its stride (tex_linear_row, the stride GetStorageSize counts, now shared by ReadTexels, the upload, gsb_geom and linear_fit), written as ReadTexels reads it; a region outside those rows, another target or level, or a block-compressed format is refused with a [RED] line, as is a block-linear write nvn_swizzle refuses. mtl_tex_linear_cpu_written leaves a texture viewed on its pool's buffer (linear_fit) as that view, which reads the storage itself (made CPU-written it would be uploaded whole at each write); any other is taken from its storage as tex_mtl_ takes one at first use. The diagnosis is PR seedds#1983's (the probe showing each title-movie frame in the source and not in the storage, and the author's Mac run of the title, the opening movie, event movies and gallery CGs with the fix); changed here: the PR's write_region_mirror no longer exists on main (ab41d6a), LINEAR_RENDER_TARGET is covered too, the buffer view is no longer made CPU-written, the DM_MOVIE_DBG probe is left out for the [RED] lines. New reference entry (WriteTexels into a linear texture dropped the write); movies.md no longer says the output is always block-linear. Checked: t_nvn 311 checks with the new 6c2b (a 37x5 LINEAR | VIDEO_DECODE texture of stride 256 and a 16x16 LINEAR_RENDER_TARGET of stride 128, four regions each, some with a padded source stride: storage byte for byte with the row padding and the bytes after, a region past the last row refused, ReadTexels back, no CPU-written texture added for the buffer views; without the linear branch 3 fail, with mtl_tex_cpu_written in place of the view rule the last fails), t_linear_readback 61 cases, t_cpu_storage 135, t_cpu_tex 23, t_direct_upload 26,384, t_partial_upload 3,585, test_docs; lint_c.py tidy on the changed sources: nothing above main's own count (nvn_mtl.m's nullability pair is 18 on main as here). Not run: any game (Tsukihime is not here), the full Mac host build, iOS, regress.py (only linear textures change, which were not written before). Open: a LINEAR_RENDER_TARGET or IMAGE linear texture the GPU drew into as a private texture and the CPU then partly writes is uploaded from its storage, as a block-linear one is (PR seedds#1983)

Co-authored-by: Beeven <5119686+beeven@users.noreply.github.com>
(cherry picked from commit 9d60776)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

docs Documentation nvn NVN on Metal and its tests (dm/gfx/nvn, dm/gfx/tests)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants