#10495·wgpu

Vulkan: Derivatives after a discard are undefined due to OpKill

Author: nolife99Created Sep 28, 2026Updated Sep 29, 2026

Description WGSL defines discard as demoting the invocation to a helper invocation (§9.4.11). Helper invocations keep taking part in derivatives, so a fragment next to a discarded one should still get defined dpdx/dpdy results and implicit-LOD samples.

naga's SPIR-V backend emits OpKill for Statement::Kill (block.rs), which terminates the invocation. The rest of its quad then computes derivatives without it, which Vulkan leaves undefined, and control flow after the discard is no longer uniform. On my RX9070 the surviving fragments get wrong derivatives. in the repro below, every one whose expected result is visible comes out as 0.

d3d12's discard already demotes so there's no issue there

Repro steps Draw one triangle that covers a 16x16 rgba8unorm target (3 vertices, no buffers) with the below shader:

wgsl
struct DiscardVaryings {
    @builtin(position) Position: vec4<f32>,
    @location(0) Uv: vec2<f32>,
}

@vertex
fn Vertex(@builtin(vertex_index) index: u32) -> DiscardVaryings {
    let uv: vec2<f32> = vec2<f32>(f32((index << 1u) & 2u), f32(index & 2u));
    return DiscardVaryings(vec4<f32>(uv * 2.0f - vec2<f32>(1.0f), uv.x * 0.5f, 1.0f), uv);
}

@fragment
fn Fragment(v: DiscardVaryings, @builtin(front_facing) front: bool) -> @location(0) vec4<f32> {
    let cell: vec2<u32> = vec2<u32>(v.Position.xy);
    if ((cell.x ^ cell.y) & 1u) == 1u {
        discard;
    }
    let slope: f32 = dpdx(v.Uv.x * v.Uv.x) + dpdy(v.Uv.y);
    return vec4<f32>(v.Uv.x, v.Position.z, slope * 8.0f, select(0.5f, 1.0f, front));
}

Part of the SPIR-V output is below:

         %74 = OpLabel
               OpKill
         %73 = OpLabel
         ...
         %80 = OpDPdx %float %79
         ...
         %83 = OpDPdy %float %82

Expected vs observed behavior Image Claude helped generate this comparison.

In the expected image the right half picks up blue from the derivative term. On Vulkan the blue channel is 0 across the right half, so 64 of the 128 kept fragments differ, by up to 112/255. The left half matches only because the expected term is negative there and clamps to 0.

naga's HLSL thru FXC and DXC produce expected results. I also tested with a SPIR-V emitter I wrote on my own, which reproduces the exact same bug with OpKill, so that instruction is the only thing that causes this.

Extra materials Reference (expected) image: Image

Bugged (naga's vulkan SPIR-V) image: Image

naga's HLSL image (DXC), not bugged: Image

Platform

  • AMD Radeon RX 9070, Windows 11 (build 26200), AMD Software 26.8.1 (Vulkan 1.4.349, LLPC)
  • wgpu-native 29.0.3 (naga 29.0.3) on the Vulkan backend
  • As of current naga-cli 29.0.3 and trunk (fcd059f4) emit the same OpKill