renderer: fix DCC fast clears for images with more than one mip level (cp2077) #1946

Closed
github-bot wants to merge 1 commit from gh-pr/1946/head into gh-pr/1946/base
Owner

@⁠ArtyomZabroda on GitHub · 2026-10-27 22:26 UTC · original
ArtyomZabroda:pr/dcc-fast-clear → main · +393 −18 · 6 files · 1 commits · closed without merging

Addresses #182

Cyberpunk 2077 uses DCC fast clears on render targets that have more than one mip level. Before this patch, TileGetDccSize simply returned false for any surface with more than one mip level, and ResolveRenderColorTarget ignored that result:

if (has_dcc) {
	(void)TileGetDccSize(width, height, layers, bytes_per_element, levels,
	                     rt.attrib3.tile_mode, metadata_size, rt.attrib.num_fragments);

As a result, the image got a DCC metadata range of size 0, and MaterializeColorClear returned early. It also rejects every image with more than one mip level:

if (range.size == 0 || desc.info.resources.levels != 1) {
	return;
}

These missed clears cause graphics artifacts in the main menu: the UI render target is never cleared, so the previous frame's menu and cursor stay visible under the new one:

Снимок экрана 2026-10-27 204523

There is also another problem. In Cyberpunk 2077 a render target with more than one mip level
is sometimes sampled without any draw into it: its first use in the frame is a compute pass that
reads level 0 and writes the other levels, while level 0's DCC metadata holds the clear code 0x00.
The emulator creates a new image on that texture binding and fills it from guest memory, which
still holds the bytes of a previous resource, and the clear is never applied, so the generated
mip chain contains garbage. Example:
Снимок экрана 2026-10-27 221503
Снимок экрана 2026-10-27 221532
Снимок экрана 2026-10-27 221542

Changes

tile.cpp / tile.h

  • Moved the DCC block size calculation out of TileGetDccSize into TileGetDccBlockSize, so every DCC function computes the block raster the same way.
  • Added TileGetDccLevelRange, which returns the offset and size of one mip level inside the DCC metadata. Like guest color data, the metadata of a mip chain runs backwards: the smallest level comes first and level 0 comes last.
  • TileGetDccSize now supports single-layer images with more than one mip level by summing the sizes of all levels. Images with more than one mip level and more than one layer (arrays, cube maps) still return false, because the order of layers and levels in their metadata is not verified.

colorRenderTarget.cpp

  • ResolveRenderColorTarget now checks the result of TileGetDccSize. If the layout is unsupported, it logs RenderColorTarget: unsupported DCC layout, fast clears are ignored (at most 16 times per run) instead of silently dropping the clears.

textureCache.cpp / textureCache.h

  • MaterializeColorClear no longer rejects every image with more than one mip level. For a render target it checks only the metadata of the bound mip level (from TileGetDccLevelRange), clears that level and writes 0xff only into its range. The whole chain cannot be checked at once: the game fast clears only level 0 and fills the other levels with a compute shader, so the metadata of the whole chain never holds one uniform code. The GPU path added in b540579 checks the same per-level range. Images with one mip level behave exactly as before.
  • Added MaterializeFreshTextureClear for the second problem. When a DCC render-target image (tile mode kRenderTarget, single layer, single sample) with more than one mip level is bound as a texture and the GPU has not written to it yet, every bound level whose metadata holds one uniform clear code is cleared.
    • This function never writes to guest metadata. A texture descriptor's metadata address can point to memory the game has already reused, and writing 0xff there corrupted the game's heap and crashed it during testing.
    • The clear is not repeated on later bindings: ClearImage marks the image as GPU-modified, so later calls return early. The same happens after compute writes the generated mips through a storage binding.
**@⁠ArtyomZabroda** on GitHub · 2026-10-27 22:26 UTC · [original](https://github.com/KytyPS5/KytyPS5/pull/1946) `ArtyomZabroda:pr/dcc-fast-clear` → `main` · +393 −18 · 6 files · 1 commits · closed without merging Addresses #182 Cyberpunk 2077 uses DCC fast clears on render targets that have more than one mip level. Before this patch, TileGetDccSize simply returned false for any surface with more than one mip level, and ResolveRenderColorTarget ignored that result: ```cpp if (has_dcc) { (void)TileGetDccSize(width, height, layers, bytes_per_element, levels, rt.attrib3.tile_mode, metadata_size, rt.attrib.num_fragments); ``` As a result, the image got a DCC metadata range of size 0, and MaterializeColorClear returned early. It also rejects every image with more than one mip level: ```cpp if (range.size == 0 || desc.info.resources.levels != 1) { return; } ``` These missed clears cause graphics artifacts in the main menu: the UI render target is never cleared, so the previous frame's menu and cursor stay visible under the new one: <img width="1280" height="747" alt="Снимок экрана 2026-10-27 204523" src="https://github.com/user-attachments/assets/e8a1af22-ae73-4bf5-ae12-5dc971690e6b" /> There is also another problem. In Cyberpunk 2077 a render target with more than one mip level is sometimes sampled without any draw into it: its first use in the frame is a compute pass that reads level 0 and writes the other levels, while level 0's DCC metadata holds the clear code 0x00. The emulator creates a new image on that texture binding and fills it from guest memory, which still holds the bytes of a previous resource, and the clear is never applied, so the generated mip chain contains garbage. Example: <img width="1277" height="745" alt="Снимок экрана 2026-10-27 221503" src="https://github.com/user-attachments/assets/9f83661a-f109-4d64-8ab3-7f89759d3acf" /> <img width="1282" height="751" alt="Снимок экрана 2026-10-27 221532" src="https://github.com/user-attachments/assets/cbb74e18-8293-4798-8122-ab8c2baa55e7" /> <img width="1282" height="749" alt="Снимок экрана 2026-10-27 221542" src="https://github.com/user-attachments/assets/e7fe1f15-3221-42dc-a257-9e794aec6460" /> ## Changes **tile.cpp / tile.h** - Moved the DCC block size calculation out of `TileGetDccSize` into `TileGetDccBlockSize`, so every DCC function computes the block raster the same way. - Added `TileGetDccLevelRange`, which returns the offset and size of one mip level inside the DCC metadata. Like guest color data, the metadata of a mip chain runs backwards: the smallest level comes first and level 0 comes last. - `TileGetDccSize` now supports single-layer images with more than one mip level by summing the sizes of all levels. Images with more than one mip level and more than one layer (arrays, cube maps) still return false, because the order of layers and levels in their metadata is not verified. **colorRenderTarget.cpp** - `ResolveRenderColorTarget` now checks the result of `TileGetDccSize`. If the layout is unsupported, it logs `RenderColorTarget: unsupported DCC layout, fast clears are ignored` (at most 16 times per run) instead of silently dropping the clears. **textureCache.cpp / textureCache.h** - `MaterializeColorClear` no longer rejects every image with more than one mip level. For a render target it checks only the metadata of the bound mip level (from `TileGetDccLevelRange`), clears that level and writes `0xff` only into its range. The whole chain cannot be checked at once: the game fast clears only level 0 and fills the other levels with a compute shader, so the metadata of the whole chain never holds one uniform code. The GPU path added in b540579 checks the same per-level range. Images with one mip level behave exactly as before. - Added `MaterializeFreshTextureClear` for the second problem. When a DCC render-target image (tile mode `kRenderTarget`, single layer, single sample) with more than one mip level is bound as a texture and the GPU has not written to it yet, every bound level whose metadata holds one uniform clear code is cleared. - This function never writes to guest metadata. A texture descriptor's metadata address can point to memory the game has already reused, and writing `0xff` there corrupted the game's heap and crashed it during testing. - The clear is not repeated on later bindings: `ClearImage` marks the image as GPU-modified, so later calls return early. The same happens after compute writes the generated mips through a storage binding.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 10:09:58 +00:00
github-bot closed this pull request 2026-11-26 10:09:58 +00:00
Author
Owner

@⁠coderabbitai[bot] on GitHub · 2026-10-27 22:31 UTC · original · edited 2026-10-28 11:41 UTC

Review in Change Stack →

Walkthrough

The DCC layout API now calculates metadata ranges for mip levels and supports eligible mip chains. Texture clear materialization uses the selected mip’s metadata range for render-target and sampled-texture views. Tests cover layout calculations, clear behavior, metadata updates, and unsupported inputs. The render-target path also logs failed DCC size calculations.

Suggested reviewers: nmzik, claxten10

Priority: Normal

Change: Bug fix

Merge Risk: Moderate · up to 7c88e

Sampling a multi-mip render target through views of different mip levels can leave some levels with stale contents instead of the expected clear. This is a rendering-correctness problem in a narrow case and should be fixed before merge.

Pre-merge checks: Passed 4
Check name Status Explanation
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Title check Passed The title clearly and concisely describes the main change: fixing DCC fast clears for images with multiple mip levels in the renderer.
Description check Passed The description states the problem (DCC fast clears skipped for render targets with multiple mip levels), the changed functions (TileGetDccSize, ResolveRenderColorTarget, MaterializeColorClear, the ne…
Full details: Description check

Explanation

The description states the problem (DCC fast clears skipped for render targets with multiple mip levels), the changed functions (TileGetDccSize, ResolveRenderColorTarget, MaterializeColorClear, the new MaterializeFreshTextureClear), and the test scenario. It is specific and clearly related to the changeset, so it passes this lenient check.

  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

**@⁠coderabbitai[bot]** on GitHub · 2026-10-27 22:31 UTC · [original](https://github.com/KytyPS5/KytyPS5/pull/1946#issuecomment-6455207842) · edited 2026-10-28 11:41 UTC <!-- coderabbit-ai-generated:v1 --> <!-- This is an auto-generated comment: summarize by coderabbit.ai --> <!-- review_stack_entry_start --> <a href="https://app.coderabbit.ai/change-stack/KytyPS5/KytyPS5/pull/1946?scope=ghh_OusAJGA4HG8ljoOV0wJvVx8NmnZIjc2DdjmxOu0xZWA&amp;cs_source=review_comment"><picture><source media="(prefers-color-scheme: dark)" srcset="https://storage.googleapis.com/coderabbit_public_assets/review-stack-in-coderabbit-ui-dark.svg?v=3"><source media="(prefers-color-scheme: light)" srcset="https://storage.googleapis.com/coderabbit_public_assets/review-stack-in-coderabbit-ui.svg?v=3"><img src="https://storage.googleapis.com/coderabbit_public_assets/review-stack-in-coderabbit-ui.svg?v=3" alt="Review in Change Stack →" height="32"></picture></a> <!-- review_stack_entry_end --> <!-- walkthrough_start --> <details> <summary><strong>Walkthrough</strong></summary> <dl> <dd> The DCC layout API now calculates metadata ranges for mip levels and supports eligible mip chains. Texture clear materialization uses the selected mip’s metadata range for render-target and sampled-texture views. Tests cover layout calculations, clear behavior, metadata updates, and unsupported inputs. The render-target path also logs failed DCC size calculations. **Suggested reviewers:** `nmzik`, `claxten10` </dd> </dl> </details> <!-- walkthrough_end --> <!-- change_assessment_start --> <strong>Priority: <picture><img src="https://storage.googleapis.com/coderabbit_public_assets/review-comment-icons-v1/priority-normal-16.svg" width="16" height="16" align="absmiddle" alt=""></picture> Normal</strong> <!-- change_assessment_commit:"7c88e3eaee49ad671c15d3932f2415d1da95d93f" --> **Change:** Bug fix <!-- change_assessment_end --> <details> <summary><strong>Merge Risk:</strong> <strong><picture><img src="https://storage.googleapis.com/coderabbit_public_assets/review-comment-icons-v1/risk-normal-16.svg" width="16" height="16" align="absmiddle" alt=""></picture> Moderate</strong> · up to <code>7c88e</code></summary> <dl> <dd> <!-- final_review_risk_start --> <!-- final_review_risk_label **Merge Risk:** **🟡 Moderate** · up to `7c88e` --> <!-- final_review_risk_coverage:{"sourceCommitId":"7c88e3eaee49ad671c15d3932f2415d1da95d93f","coveredCommitId":"7c88e3eaee49ad671c15d3932f2415d1da95d93f","kind":"reviewed"} --> Sampling a multi-mip render target through views of different mip levels can leave some levels with stale contents instead of the expected clear. This is a rendering-correctness problem in a narrow case and should be fixed before merge. <!-- final_review_risk_end --> </dd> </dl> </details> <!-- pre_merge_checks_walkthrough_start --> <details> <summary><strong>Pre-merge checks: <sup><picture><img src="https://storage.googleapis.com/coderabbit_public_assets/pre-merge-checks-counter-passed-14-v2.svg" width="14" height="14" align="middle" alt="Passed" title="Passed"></picture></sup> 4</strong></summary> <dl> <dd> | Check name | Status | Explanation | | :------------------------ | :--------------------------------------------------------------------------------------------: | :-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | Linked Issues check | ![Passed](https://storage.googleapis.com/coderabbit_public_assets/pre-merge-checks-passed.svg) | Check skipped because no linked issues were found for this pull request. | | Out of Scope Changes check | ![Passed](https://storage.googleapis.com/coderabbit_public_assets/pre-merge-checks-passed.svg) | Check skipped because no linked issues were found for this pull request. | | Title check | ![Passed](https://storage.googleapis.com/coderabbit_public_assets/pre-merge-checks-passed.svg) | The title clearly and concisely describes the main change: fixing DCC fast clears for images with multiple mip levels in the renderer. | | Description check | ![Passed](https://storage.googleapis.com/coderabbit_public_assets/pre-merge-checks-passed.svg) | The description states the problem (DCC fast clears skipped for render targets with multiple mip levels), the changed functions (TileGetDccSize, ResolveRenderColorTarget, MaterializeColorClear, the ne… | <details> <summary><strong>Full details: Description check</strong></summary> <dl> <dd> **Explanation** The description states the problem (DCC fast clears skipped for render targets with multiple mip levels), the changed functions (TileGetDccSize, ResolveRenderColorTarget, MaterializeColorClear, the new MaterializeFreshTextureClear), and the test scenario. It is specific and clearly related to the changeset, so it passes this lenient check. </dd> </dl> </details> </dd> </dl> </details> <!-- pre_merge_checks_walkthrough_end --> <!-- autopilot:start --> - [ ] <!-- {"checkboxId":"2708ad07-9f24-4260-9c11-7dc76a49f2e3"} --> <strong title="Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts">Autofix</strong> · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts <!-- autopilot:end --> <!-- tips_start --> Thanks for using [CodeRabbit](https://coderabbit.ai?utm_source=oss&utm_medium=github&utm_campaign=KytyPS5/KytyPS5&utm_content=1946)! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. <details> <summary><strong>❤️ Share</strong></summary> <dl> <dd> - [X](https://twitter.com/intent/tweet?text=I%20just%20used%20%40coderabbitai%20for%20my%20code%20review%2C%20and%20it%27s%20fantastic%21%20It%27s%20free%20for%20OSS%20and%20offers%20a%20free%20trial%20for%20the%20proprietary%20code.%20Check%20it%20out%3A&url=https%3A//coderabbit.ai) - [Mastodon](https://mastodon.social/share?text=I%20just%20used%20%40coderabbitai%20for%20my%20code%20review%2C%20and%20it%27s%20fantastic%21%20It%27s%20free%20for%20OSS%20and%20offers%20a%20free%20trial%20for%20the%20proprietary%20code.%20Check%20it%20out%3A%20https%3A%2F%2Fcoderabbit.ai) - [Reddit](https://www.reddit.com/submit?title=Great%20tool%20for%20code%20review%20-%20CodeRabbit&text=I%20just%20used%20CodeRabbit%20for%20my%20code%20review%2C%20and%20it%27s%20fantastic%21%20It%27s%20free%20for%20OSS%20and%20offers%20a%20free%20trial%20for%20proprietary%20code.%20Check%20it%20out%3A%20https%3A//coderabbit.ai) - [LinkedIn](https://www.linkedin.com/sharing/share-offsite/?url=https%3A%2F%2Fcoderabbit.ai&mini=true&title=Great%20tool%20for%20code%20review%20-%20CodeRabbit&summary=I%20just%20used%20CodeRabbit%20for%20my%20code%20review%2C%20and%20it%27s%20fantastic%21%20It%27s%20free%20for%20OSS%20and%20offers%20a%20free%20trial%20for%20proprietary%20code) </dd> </dl> </details> <sub>Comment `@coderabbitai help` to get the list of available commands.</sub> <!-- tips_end -->
@ -1184,0 +1221,4 @@
std::scoped_lock lock {m_lock};
if (m_slot_images[id].IsGpuModified()) {
return;
}
Author
Owner

@⁠coderabbitai[bot] on GitHub · 2026-10-27 22:31 UTC · original

Major · Functional Correctness · Heavy lift

Preserve fresh clears for levels outside the first texture view.

If a fresh DCC mip chain is sampled through a view of level 0, the helper checks only level 0. A clear of that level marks the whole image GPU-modified. When a later view selects level 1, this guard skips its clear, so level 1 retains older resource bytes despite a valid clear key. Track materialization or GPU ownership per level, or apply all eligible level clears before setting the image-wide flag. (raw.githubusercontent.com)

**@⁠coderabbitai[bot]** on GitHub · 2026-10-27 22:31 UTC · [original](https://github.com/KytyPS5/KytyPS5/pull/1946#discussion_r4394915775) <!-- coderabbit-ai-generated:v1 --> **<picture><img src="https://storage.googleapis.com/coderabbit_public_assets/review-comment-icons-v1/major-16.svg" width="16" height="16" align="absmiddle" alt=""></picture> Major** · **Functional Correctness** · **Heavy lift** **Preserve fresh clears for levels outside the first texture view.** If a fresh DCC mip chain is sampled through a view of level 0, the helper checks only level 0. A clear of that level marks the *whole image* GPU-modified. When a later view selects level 1, this guard skips its clear, so level 1 retains older resource bytes despite a valid clear key. Track materialization or GPU ownership per level, or apply all eligible level clears before setting the image-wide flag. ([raw.githubusercontent.com](https://raw.githubusercontent.com/KytyPS5/KytyPS5/7c88e3eaee49ad671c15d3932f2415d1da95d93f/src/graphics/host_gpu/renderer/cache/textureCache.cpp)) <!-- fingerprinting:phantom:poseidon:wombat --> <!-- cr-indicator-types:potential_issue --> <!-- cr-comment:v1:0856771445dd12e004d439af --> <!-- This is an auto-generated comment by CodeRabbit -->
Author
Owner

@⁠coderabbitai[bot] on GitHub · 2026-10-27 22:31 UTC · original · reviewed

Actionable comments posted: 1


Review info
Run configuration
Commits

Reviewing files that changed from the base of the PR and between 1be1c6c4f9 and 7c88e3eaee.

Files selected for processing: 6
  • src/graphics/guest_gpu/tile.cpp
  • src/graphics/guest_gpu/tile.h
  • src/graphics/host_gpu/renderer/cache/textureCache.cpp
  • src/graphics/host_gpu/renderer/cache/textureCache.h
  • src/graphics/host_gpu/renderer/colorRenderTarget.cpp
  • tests/ShaderRecompilerComputeTests.cpp

Included review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 7 remain after this review.

**@⁠coderabbitai[bot]** on GitHub · 2026-10-27 22:31 UTC · [original](https://github.com/KytyPS5/KytyPS5/pull/1946#pullrequestreview-5675300786) · **reviewed** <!-- coderabbit-ai-generated:v1 --> **Actionable comments posted: 1** --- <details> <summary><picture><img src="https://storage.googleapis.com/coderabbit_public_assets/review-comment-icons-v1/info.svg" width="16" height="16" align="absmiddle" alt=""></picture> Review info</summary> <dl> <dd> <details> <summary><picture><source media="(prefers-color-scheme: dark)" srcset="https://storage.googleapis.com/coderabbit_public_assets/review-comment-icons-v1/cog-dark.svg"><img src="https://storage.googleapis.com/coderabbit_public_assets/review-comment-icons-v1/cog.svg" width="16" height="16" align="absmiddle" alt=""></picture> Run configuration</summary> <dl> <dd> - **Source mode**: Auto - **Repository**: [KytyPS5/KytyPS5](<https://github.com/KytyPS5/KytyPS5>) - **Branch**: [main](<https://github.com/KytyPS5/KytyPS5/tree/main>) - **File**: [.coderabbit.yaml](<https://github.com/KytyPS5/KytyPS5/blob/1be1c6c4f928e52f740ae7f76841c1da74220e54/.coderabbit.yaml>) - **Commit**: [1be1c6c](<https://github.com/KytyPS5/KytyPS5/commit/1be1c6c4f928e52f740ae7f76841c1da74220e54>) - **Layers**: Repository YAML - **Review profile**: CHILL - **Plan**: Advanced - **Run ID**: `e1dabfff-f246-4110-8409-bf9db795a978` </dd> </dl> </details> <details> <summary><picture><source media="(prefers-color-scheme: dark)" srcset="https://storage.googleapis.com/coderabbit_public_assets/review-comment-icons-v1/git-commit-horizontal-dark.svg"><img src="https://storage.googleapis.com/coderabbit_public_assets/review-comment-icons-v1/git-commit-horizontal.svg" width="16" height="16" align="absmiddle" alt=""></picture> Commits</summary> <dl> <dd> Reviewing files that changed from the base of the PR and between 1be1c6c4f928e52f740ae7f76841c1da74220e54 and 7c88e3eaee49ad671c15d3932f2415d1da95d93f. </dd> </dl> </details> <details> <summary><picture><source media="(prefers-color-scheme: dark)" srcset="https://storage.googleapis.com/coderabbit_public_assets/review-comment-icons-v1/file-plus-corner-dark.svg"><img src="https://storage.googleapis.com/coderabbit_public_assets/review-comment-icons-v1/file-plus-corner.svg" width="16" height="16" align="absmiddle" alt=""></picture> Files selected for processing: 6</summary> <dl> <dd> * `src/graphics/guest_gpu/tile.cpp` * `src/graphics/guest_gpu/tile.h` * `src/graphics/host_gpu/renderer/cache/textureCache.cpp` * `src/graphics/host_gpu/renderer/cache/textureCache.h` * `src/graphics/host_gpu/renderer/colorRenderTarget.cpp` * `tests/ShaderRecompilerComputeTests.cpp` </dd> </dl> </details> **Included review availability:** This review used your included allowance. Your plan provides up to 8 included reviews per hour; 7 remain after this review. </dd> </dl> </details> <!-- This is an auto-generated comment by CodeRabbit for review status --> <!-- coderabbit-review-publication v1 publication=927c96dd-bbe3-474e-b047-10db049a8437 attempt=34cd1918-d952-48fd-a9d7-d364df37d390 batch=1/1 -->
Author
Owner

@⁠Nmzik on GitHub · 2026-10-30 04:27 UTC · original

@⁠ArtyomZabroda Credited! Thanks. multi mipmap logic inside MaterializeColorClear

**@⁠Nmzik** on GitHub · 2026-10-30 04:27 UTC · [original](https://github.com/KytyPS5/KytyPS5/pull/1946#issuecomment-6514975722) @⁠ArtyomZabroda Credited! Thanks. multi mipmap logic inside MaterializeColorClear

Pull request closed

This discussion has been locked. Commenting is limited to contributors.
No description provided.