Merge pull request #1203: on AMD's Windows drivers objects against clouds and fog lose the light blocky fringe round their silhouettes; a discard demotes the fragment as on the Switch (codexx-dev, #164)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Valiantsin DzerakhandClaude Opus 5.5 committed 2026-11-15 10:38:24 +03:00
commit c26c3a04bc
4 files changed
+80 -1

No files matched your search

+1 -1
View File
@@ -81,7 +81,7 @@ Source shorthands:
| 3.2 | **FMZ multiply mode** (`FMUL/FFMA/...` field `Fmz`, `.FMZ`): 0 × anything = 0, including Inf/NaN (DX9 rule). | IEEE: 0 × Inf = NaN. | Ryujinx decodes `Fmz` (`Decoders/InstDecoders.cs:306-309, 1901`) but no emitter reads it. Ours: same. Census needed (check 13). | RYU-SH |
| 3.3 | **Denormals**: per-instruction `.FTZ`. | AMD flushes fp32 denormals by default; `VK_KHR_shader_float_controls` sets it per shader. | Ryujinx ignores `.FTZ`. Low risk. | RYU-SH (no `Ftz` use in `Instructions/`) |
| 3.4 | **Warp size 32**: `SHFL`, `VOTE`, `S2R LANEID`, ballots are 32-bit. | AMD RDNA runs wave32 or wave64 per shader (the driver chooses; RADV fragment default wave64); `VK_EXT_subgroup_size_control` `requiredSubgroupSize = 32` fixes it. | Ryujinx adapts lane masks to `QueryHostSubgroupSize()` (default 32, `IGpuAccessor.cs:218`; `Instructions/InstEmitWarp.cs:72, 106`, `InstEmitMove.cs:175-185`). Ours: not overridden, no required size (check 6). | <https://docs.mesa3d.org/envvars.html> (`RADV_PERFTEST=pswave32`) |
| 3.5 | **Discard (`KIL`)** keeps the quad's derivatives valid (demote semantics). | `OpKill` terminates; later derivatives in the quad undefined. | Ryujinx emits `OpKill` (`CodeGen/Spirv/Instructions.cs:508`). | — |
| 3.5 | **Discard (`KIL`)** keeps the quad's derivatives valid (demote semantics). | `OpKill` terminates; later derivatives in the quad undefined. | Ryujinx emits `OpKill` (`CodeGen/Spirv/Instructions.cs:508`). Ours: on AMD's own drivers (`vk_caps.amd` without `mesa`) `vk_pipeline.c` `spirv_demote` turns each `OpKill` that a selection header branches to into `OpDemoteToHelperInvocation` plus a branch to the selection's merge block; `TOTK_DEMOTE` overrides. TotK's cloud shader discards and then takes `OpImageQueryLod`: with `OpKill` AMD's Windows driver lost fog in the fragments sharing a quad with a discarded one (#164). | <https://github.com/KhronosGroup/SPIRV-Registry/blob/main/extensions/EXT/SPV_EXT_demote_to_helper_invocation.asciidoc> |
| 3.6 | **Implicit-LOD sampling in divergent control flow** works on Maxwell (TEX unit uses quad neighbours). | Undefined in non-uniform control flow; AMD whole-quad mode usually works. | — | VKSPEC (derivatives) |
| 3.7 | **Integer division** does not exist (compiled to `XMAD`/`IMAD` sequences); MUFU.RCP(0) = +Inf, LG2(0) = -Inf. | Same IEEE edge cases through `1/x`, `Log2`. | Low risk. | — |
| 3.8 | **FP64** in some TotK cutscene shaders. | No FP64 on some hosts. | yuzu FP64 → FP32 (YZ-2306, PR 10693); ours: `softfloat64` job flag (`tools/xlate/Program.cs` `QueryHostSupportsShaderFloat64`). | — |
+1
View File
@@ -61,6 +61,7 @@ typedef struct {
int amd_sc; // AMD's Windows driver branch for RDNA 1 and 2 ("AMD proprietary shader compiler")
int mesa; // a Mesa driver the game runs on: RADV, ANV or NVK
int intel_igpu_win; // Intel's Windows driver on an integrated GPU (UHD, Iris Xe, Core Ultra graphics)
int demote; // shaderDemoteToHelperInvocation enabled: modules may use OpDemoteToHelperInvocation
} VkCaps;
extern VkCaps vk_caps;
void vk_caps_driver(VkPhysicalDevice p); // first, before vkCreateDevice: vk_caps.amd, amd_sc, mesa, intel_igpu_win
+3
View File
@@ -401,6 +401,9 @@ static int create_device(void) {
else if (!h->textureCompressionBC) miss = "textureCompressionBC";
if (miss) { rt_log("vk: required feature missing on %s: %s", g_vk.props.deviceName, miss); free(c); return 0; }
if (!c->fault.deviceFault) g_vk.has_device_fault = 0; // vkGetDeviceFaultInfoEXT needs the feature, not just the extension
// Vulkan 1.3 requires the feature, and the chain goes to vkCreateDevice as the driver filled it, so it is enabled
// wherever reported; vk_pipeline.c spirv_demote emits OpDemoteToHelperInvocation only when it is.
vk_caps.demote = c->f13.shaderDemoteToHelperInvocation;
rt_log("vk: GPU address map for device faults: %s",
has_abr && c->abr.reportAddressBinding ? "on (VK_EXT_device_address_binding_report; TOTK_ADDR_REPORT=0 turns it off)"
: !ab_want ? "off (TOTK_ADDR_REPORT=0 or no VK_EXT_debug_utils)"
+75
View File
@@ -873,6 +873,78 @@ out:
return o;
}
// Discard with demote semantics (docs/maxwell.md 3.5). Maxwell's KIL keeps the fragment running as a helper, so the
// derivatives its 2x2 quad takes afterwards stay defined; the translator emits OpKill, which ends the invocation, and
// what the quad's other fragments then get from an implicit derivative (OpImageQueryLod, implicit-LOD samples, dFdx) is
// undefined. Mesa keeps computing after a discard; AMD's Windows compiler does not, and TotK's cloud shader, which
// discards where the scene is in front and then takes OpImageQueryLod for its noise textures, lost fog in the pixels
// sharing a quad with a discarded one: a light fringe round every silhouette against clouds (issue #164).
// Each OpKill whose block is a direct target of a selection header becomes OpDemoteToHelperInvocation plus a branch to
// that selection's merge block, so the fragment goes on computing and writes nothing. An OpKill anywhere else (the end
// of a function, a switch case) stays. NULL: nothing to change.
// On by default on AMD's own drivers, where the fringe shows; other drivers keep the translator's OpKill, their output
// unchanged. TOTK_DEMOTE=1 or 0 overrides on any driver that enables shaderDemoteToHelperInvocation (vk_caps.demote,
// set in vk_device.c create_device); without the feature the module keeps OpKill, since the capability would be invalid.
static uint32_t *spirv_demote(const uint32_t *w, uint64_t n, uint64_t *out_n) {
enum { OP_EXTENSION = 10, OP_CAPABILITY = 17, OP_SELECTION_MERGE = 247, OP_LABEL = 248, OP_BRANCH = 249,
OP_BRANCH_CONDITIONAL = 250, OP_KILL = 252, OP_DEMOTE = 5380, CAP_DEMOTE = 5379 };
static const char ext_name[36] = "SPV_EXT_demote_to_helper_invocation"; // 35 characters and the terminator: 9 words
static int on = -1;
if (on < 0) {
on = vk_caps.demote && rt_knob("DEMOTE", vk_caps.amd && !vk_caps.mesa ? "1" : "0");
rt_log("vk_pipeline: discard as demote: %s (shaderDemoteToHelperInvocation %d, TOTK_DEMOTE)", on ? "on" : "off",
vk_caps.demote);
}
if (!on || n < 5) return NULL;
uint64_t kills = 0;
for (uint64_t i = 5; i < n;) {
uint32_t op = w[i] & 0xffff, l = w[i] >> 16;
if (!l || i + l > n) return NULL;
if (op == OP_KILL) kills++;
if (op == OP_CAPABILITY && l >= 2 && w[i + 1] == CAP_DEMOTE) return NULL; // already demotes
i += l;
}
if (!kills) return NULL;
uint32_t bound = w[3] < (1u << 22) ? w[3] : (1u << 22);
uint32_t *merge_of = calloc(bound ? bound : 1, sizeof *merge_of); // label -> merge block of the selection that branches to it
uint32_t *o = malloc((n + 12 + 2 * kills) * 4);
if (!merge_of || !o) { free(merge_of), free(o); return NULL; }
uint64_t on_w = 5, changed = 0;
uint32_t cur = 0, pending = 0;
int declared = 0;
memcpy(o, w, 5 * 4);
for (uint64_t i = 5; i < n;) {
uint32_t op = w[i] & 0xffff, l = w[i] >> 16;
const uint32_t *a = w + i + 1;
if (!declared && op != OP_CAPABILITY) { // capabilities come first, extensions right after them
o[on_w++] = 2u << 16 | OP_CAPABILITY, o[on_w++] = CAP_DEMOTE;
o[on_w++] = 10u << 16 | OP_EXTENSION;
memcpy(o + on_w, ext_name, sizeof ext_name), on_w += 9;
declared = 1;
}
if (op == OP_LABEL && l >= 2) cur = a[0];
if (op == OP_BRANCH_CONDITIONAL && l >= 4 && pending) {
// The true and the false block lie directly in the selection; a target that is the merge block itself does not.
for (int k = 1; k <= 2; k++)
if (a[k] < bound && a[k] != pending) merge_of[a[k]] = merge_of[a[k]] ? ~0u : pending; // two headers: leave it
}
pending = op == OP_SELECTION_MERGE && l >= 3 ? a[0] : 0;
uint32_t m = op == OP_KILL && cur < bound ? merge_of[cur] : 0;
if (m && m != ~0u && m != cur) {
o[on_w++] = 1u << 16 | OP_DEMOTE;
o[on_w++] = 2u << 16 | OP_BRANCH, o[on_w++] = m;
changed++;
} else {
memcpy(o + on_w, w + i, l * 4), on_w += l;
}
i += l;
}
free(merge_of);
if (!changed) { free(o); return NULL; }
*out_n = on_w;
return o;
}
// True when the module declares a GroupNonUniform* or SubgroupBallot-style capability (lane masks assume 32 lanes).
static int uses_subgroup_ops(const uint32_t *w, uint64_t words) {
for (uint64_t i = 5; i < words;) {
@@ -899,6 +971,9 @@ static VkShaderModule make_module(const uint32_t *w, uint64_t words) {
uint64_t fixed_n = 0;
uint32_t *fixed = spirv_fixup(w, words, &fixed_n);
if (fixed) w = fixed, words = fixed_n;
uint64_t demoted_n = 0;
uint32_t *demoted = spirv_demote(w, words, &demoted_n);
if (demoted) free(fixed), fixed = demoted, w = demoted, words = demoted_n;
VkShaderModuleCreateInfo ci = {.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO, .codeSize = (size_t)words * 4, .pCode = w};
VkShaderModule m = VK_NULL_HANDLE;
VkResult r = vkCreateShaderModule(P.device, &ci, NULL, &m);