[PR #1290] [MERGED] shader_recompiler: Move sampling parameter resolution to tracking pass and support more derivative types. #2037

Closed
opened 2026-02-27 21:14:56 +03:00 by kerem · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/shadps4-emu/shadPS4/pull/1290
Author: @squidbus
Created: 10/8/2024
Status: Merged
Merged: 10/10/2024
Merged by: @raphaelthegreat

Base: mainHead: sampling


📝 Commits (3)

  • 418ceb2 shader_recompiler: Move sampling parameter resolution to tracking pass and support more derivative types.
  • 7b04637 shader_recompiler: Only track sampler sharp on sample instructions.
  • 106305b shader_recompiler: Fix Inst args size.

📊 Changes

10 files changed (+333 additions, -267 deletions)

View changed files

📝 src/shader_recompiler/backend/spirv/emit_spirv_image.cpp (+17 -10)
📝 src/shader_recompiler/backend/spirv/emit_spirv_instructions.h (+4 -2)
📝 src/shader_recompiler/frontend/translate/vector_memory.cpp (+71 -113)
📝 src/shader_recompiler/ir/ir_emitter.cpp (+26 -17)
📝 src/shader_recompiler/ir/ir_emitter.h (+16 -10)
📝 src/shader_recompiler/ir/opcodes.h (+1 -1)
📝 src/shader_recompiler/ir/opcodes.inc (+6 -5)
📝 src/shader_recompiler/ir/passes/resource_tracking_pass.cpp (+189 -107)
📝 src/shader_recompiler/ir/reg.h (+2 -1)
📝 src/shader_recompiler/ir/value.h (+1 -1)

📄 Description

The current code for the IMAGE_SAMPLE instruction does not work with derivatives for non-2D images as the type is not known when the IR is emitted, and thus it cannot determine how many derivative parameters there are.

Instead, we can emit a placeholder IR instruction with all of the addressing data, and then once the resource is known we can resolve all of the parameters using the type and replace it with the correct instruction.

With the emission of the exact sampling IR instruction moved to after the resource has been determined, non-2D image derivatives are now supported, and the parameter handling is cleaner since it no longer uses individual placeholder arguments that need to be resolved.

As part of this change I ended up needing to increase the max IR argument count to 6, in order to pass the full set of derivatives for the ImageGradient instruction. Technically you could probably pack one of the vectors in with the lod_clamp, but it would make things a lot messier having to place the lod_clamp at the right index depending on the derivative size, as well as unpack and pass the right data later on in SPIR-V. It is much cleaner if we can just have one more parameter slot to pass it in separately.

I also changed the sample dref instructions to have a F32x4 return type, with the right component extracted in the IR for the result. Otherwise the typing would be inconsistent between the placeholder and replacement instructions, and in the initial IR the type would be wrong compared to what the code expects. Some of the other argument types have also been fixed up in the sampling instructions to match what they must be.

Tested in a few games. Fixes missing cube derivative support in some shaders in CUSA16429.


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/shadps4-emu/shadPS4/pull/1290 **Author:** [@squidbus](https://github.com/squidbus) **Created:** 10/8/2024 **Status:** ✅ Merged **Merged:** 10/10/2024 **Merged by:** [@raphaelthegreat](https://github.com/raphaelthegreat) **Base:** `main` ← **Head:** `sampling` --- ### 📝 Commits (3) - [`418ceb2`](https://github.com/shadps4-emu/shadPS4/commit/418ceb238229a3056315ea11aee2b800a2c32ab0) shader_recompiler: Move sampling parameter resolution to tracking pass and support more derivative types. - [`7b04637`](https://github.com/shadps4-emu/shadPS4/commit/7b0463762c17684db8ea70540e934c36729c47e7) shader_recompiler: Only track sampler sharp on sample instructions. - [`106305b`](https://github.com/shadps4-emu/shadPS4/commit/106305bad110c474c131cd1e0271425d7719206c) shader_recompiler: Fix Inst args size. ### 📊 Changes **10 files changed** (+333 additions, -267 deletions) <details> <summary>View changed files</summary> 📝 `src/shader_recompiler/backend/spirv/emit_spirv_image.cpp` (+17 -10) 📝 `src/shader_recompiler/backend/spirv/emit_spirv_instructions.h` (+4 -2) 📝 `src/shader_recompiler/frontend/translate/vector_memory.cpp` (+71 -113) 📝 `src/shader_recompiler/ir/ir_emitter.cpp` (+26 -17) 📝 `src/shader_recompiler/ir/ir_emitter.h` (+16 -10) 📝 `src/shader_recompiler/ir/opcodes.h` (+1 -1) 📝 `src/shader_recompiler/ir/opcodes.inc` (+6 -5) 📝 `src/shader_recompiler/ir/passes/resource_tracking_pass.cpp` (+189 -107) 📝 `src/shader_recompiler/ir/reg.h` (+2 -1) 📝 `src/shader_recompiler/ir/value.h` (+1 -1) </details> ### 📄 Description The current code for the `IMAGE_SAMPLE` instruction does not work with derivatives for non-2D images as the type is not known when the IR is emitted, and thus it cannot determine how many derivative parameters there are. Instead, we can emit a placeholder IR instruction with all of the addressing data, and then once the resource is known we can resolve all of the parameters using the type and replace it with the correct instruction. With the emission of the exact sampling IR instruction moved to after the resource has been determined, non-2D image derivatives are now supported, and the parameter handling is cleaner since it no longer uses individual placeholder arguments that need to be resolved. As part of this change I ended up needing to increase the max IR argument count to 6, in order to pass the full set of derivatives for the ImageGradient instruction. Technically you could probably pack one of the vectors in with the `lod_clamp`, but it would make things a lot messier having to place the `lod_clamp` at the right index depending on the derivative size, as well as unpack and pass the right data later on in SPIR-V. It is much cleaner if we can just have one more parameter slot to pass it in separately. I also changed the sample dref instructions to have a F32x4 return type, with the right component extracted in the IR for the result. Otherwise the typing would be inconsistent between the placeholder and replacement instructions, and in the initial IR the type would be wrong compared to what the code expects. Some of the other argument types have also been fixed up in the sampling instructions to match what they must be. Tested in a few games. Fixes missing cube derivative support in some shaders in CUSA16429. --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
kerem 2026-02-27 21:14:56 +03:00
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
starred/shadPS4#2037
No description provided.