diff options
| author | OreoFR <29048345+OreoFR@users.noreply.github.com> | 2026-11-25 09:37:42 +0100 |
|---|---|---|
| committer | OreoFR <29048345+OreoFR@users.noreply.github.com> | 2026-11-25 09:37:42 +0100 |
| commit | d763845349ec7f36669f4b2bfa67e40576b88c8a (patch) | |
| tree | f861e6e2442636ac474aba96e995da6aa403f298 /runtime/src | |
| parent | 81f33de1cd307ac7cd7365724c55d27999888629 (diff) | |
d3d12: dual-source blending reads SV_Target1 at Location 0, Index 1 (RT-DUAL-SOURCE-BLEND)
A PSO whose blend state uses a SRC1 factor gets its fragment module's Location 1
colour output moved to Location 0 with Index 1, as Vulkan takes the second
source. Slot 57 native PSOs now map the SRC1 factors of state blob A.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Diffstat (limited to 'runtime/src')
| -rw-r--r-- | runtime/src/dual_source_fragment.h | 59 | ||||
| -rw-r--r-- | runtime/src/native_pso.h | 9 |
2 files changed, 65 insertions, 3 deletions
diff --git a/runtime/src/dual_source_fragment.h b/runtime/src/dual_source_fragment.h new file mode 100644 index 000000000..86bc66236 --- /dev/null +++ b/runtime/src/dual_source_fragment.h | |||
| @@ -0,0 +1,59 @@ | |||
| 1 | #pragma once | ||
| 2 | |||
| 3 | // Dual-source blending (RT-DUAL-SOURCE-BLEND). D3D12 takes the second blend source from SV_Target1, which the | ||
| 4 | // translated pixel shader declares as the output at Location 1. Vulkan takes it from Location 0, Index 1 | ||
| 5 | // (VK_BLEND_FACTOR_SRC1_*, dualSrcBlend). For a pipeline whose blend state uses a SRC1 factor, the fragment module's | ||
| 6 | // Location 1 output is moved to Location 0 and given Index 1. Standard library only. | ||
| 7 | |||
| 8 | #include <cstdint> | ||
| 9 | #include <map> | ||
| 10 | #include <set> | ||
| 11 | #include <vector> | ||
| 12 | |||
| 13 | namespace leonida::dual_source { | ||
| 14 | |||
| 15 | // Rewrites `words` in place and returns true, or returns false and leaves them unchanged when the module is not one | ||
| 16 | // this rewrite understands: not SPIR-V, no Output variable at Location 1, an output at Location 2 or above (Vulkan has | ||
| 17 | // one dual-source attachment), or an Index decoration already present. | ||
| 18 | inline bool remap(std::vector<std::uint32_t>& words) { | ||
| 19 | constexpr std::uint32_t kOpDecorate = 71, kOpVariable = 59, kOpTypePointer = 32; | ||
| 20 | constexpr std::uint32_t kLocation = 30, kIndex = 32, kBuiltIn = 11, kStorageOutput = 3; | ||
| 21 | if (words.size() < 5 || words[0] != 0x07230203) return false; | ||
| 22 | std::map<std::uint32_t, std::size_t> location_at; // variable id -> word index of its Location literal | ||
| 23 | std::map<std::uint32_t, std::uint32_t> location_of; | ||
| 24 | std::set<std::uint32_t> output_pointers, outputs, builtins; | ||
| 25 | for (std::size_t at = 5; at < words.size();) { | ||
| 26 | const std::uint32_t n = words[at] >> 16, op = words[at] & 0xffffu; | ||
| 27 | if (!n || n > words.size() - at) return false; | ||
| 28 | if (op == kOpDecorate && n == 4 && words[at + 2] == kLocation) { | ||
| 29 | location_at[words[at + 1]] = at + 3; | ||
| 30 | location_of[words[at + 1]] = words[at + 3]; | ||
| 31 | } else if (op == kOpDecorate && n >= 3 && words[at + 2] == kIndex) { | ||
| 32 | return false; | ||
| 33 | } else if (op == kOpDecorate && n >= 3 && words[at + 2] == kBuiltIn) { | ||
| 34 | builtins.insert(words[at + 1]); | ||
| 35 | } else if (op == kOpTypePointer && n == 4 && words[at + 2] == kStorageOutput) { | ||
| 36 | output_pointers.insert(words[at + 1]); | ||
| 37 | } else if (op == kOpVariable && n >= 4 && words[at + 3] == kStorageOutput) { | ||
| 38 | outputs.insert(words[at + 2]); | ||
| 39 | } | ||
| 40 | at += n; | ||
| 41 | } | ||
| 42 | std::uint32_t second = 0; | ||
| 43 | for (const std::uint32_t id : outputs) { | ||
| 44 | if (builtins.count(id)) continue; | ||
| 45 | const auto loc = location_of.find(id); | ||
| 46 | if (loc == location_of.end()) continue; | ||
| 47 | if (loc->second >= 2) return false; | ||
| 48 | if (loc->second == 1) second = id; | ||
| 49 | } | ||
| 50 | if (!second) return false; | ||
| 51 | const std::size_t literal = location_at[second]; | ||
| 52 | words[literal] = 0; | ||
| 53 | // OpDecorate %second Index 1, right after its Location decoration (annotations stay together). | ||
| 54 | const std::vector<std::uint32_t> index{(4u << 16) | kOpDecorate, second, kIndex, 1}; | ||
| 55 | words.insert(words.begin() + static_cast<std::ptrdiff_t>(literal + 1), index.begin(), index.end()); | ||
| 56 | return true; | ||
| 57 | } | ||
| 58 | |||
| 59 | } // namespace leonida::dual_source | ||
diff --git a/runtime/src/native_pso.h b/runtime/src/native_pso.h index abe2c52e1..5d117dba3 100644 --- a/runtime/src/native_pso.h +++ b/runtime/src/native_pso.h | |||
| @@ -196,10 +196,11 @@ struct TargetBlend { | |||
| 196 | }; | 196 | }; |
| 197 | 197 | ||
| 198 | // An RDNA2 blend factor (V_028780_BLEND_*, the gfx10 register header) as a D3D12_BLEND, or 0 for one this path does | 198 | // An RDNA2 blend factor (V_028780_BLEND_*, the gfx10 register header) as a D3D12_BLEND, or 0 for one this path does |
| 199 | // not take: BOTH_(INV_)SRC_ALPHA (11, 12; not on gfx10) and the dual-source SRC1 factors (15 to 18), whose second | 199 | // not take: BOTH_(INV_)SRC_ALPHA (11, 12; not on gfx10). The dual-source SRC1 factors (15 to 18) read the pixel |
| 200 | // pixel-shader output the translator does not declare. For an alpha factor, a colour factor names its alpha. | 200 | // shader's SV_Target1, which the Vulkan layer moves to Location 0, Index 1 (RT-DUAL-SOURCE-BLEND). For an alpha |
| 201 | // factor, a colour factor names its alpha. | ||
| 201 | inline std::uint8_t d3d12_blend_factor(std::uint32_t f, bool alpha) { | 202 | inline std::uint8_t d3d12_blend_factor(std::uint32_t f, bool alpha) { |
| 202 | static constexpr std::uint8_t colour[21] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 0, 0, 14, 15, 0, 0, 0, 0, 20, 21}; | 203 | static constexpr std::uint8_t colour[21] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 0, 0, 14, 15, 16, 17, 18, 19, 20, 21}; |
| 203 | if (f >= std::size(colour)) return 0; | 204 | if (f >= std::size(colour)) return 0; |
| 204 | if (alpha) { | 205 | if (alpha) { |
| 205 | switch (f) { | 206 | switch (f) { |
| @@ -207,6 +208,8 @@ inline std::uint8_t d3d12_blend_factor(std::uint32_t f, bool alpha) { | |||
| 207 | case 3: return 6; // ONE_MINUS_SRC_COLOR -> INV_SRC_ALPHA | 208 | case 3: return 6; // ONE_MINUS_SRC_COLOR -> INV_SRC_ALPHA |
| 208 | case 8: return 7; // DST_COLOR -> DEST_ALPHA | 209 | case 8: return 7; // DST_COLOR -> DEST_ALPHA |
| 209 | case 9: return 8; // ONE_MINUS_DST_COLOR -> INV_DEST_ALPHA | 210 | case 9: return 8; // ONE_MINUS_DST_COLOR -> INV_DEST_ALPHA |
| 211 | case 15: return 18; // SRC1_COLOR -> SRC1_ALPHA | ||
| 212 | case 16: return 19; // INV_SRC1_COLOR -> INV_SRC1_ALPHA | ||
| 210 | default: break; | 213 | default: break; |
| 211 | } | 214 | } |
| 212 | } | 215 | } |