Skip to content

gpu: software device compares NaN as equal to everything and ignores bool operands in == / != #2335

Backlog

Description

@houko

Context

The software GPU device (gpu.NewSoftwareRenderer, also what nsgotest renders with) runs shaders through the IR VM in internal/driver/gpu_software.go. Every relational and equality operator in evalBinary goes through cmpVal (main, internal/driver/gpu_software.go:3266-3276):

case shaderir.EqualOp:
    return irVal{typ: shaderir.Type{Main: shaderir.Bool}, b: cmpVal(a, b) == 0}
case shaderir.NotEqualOp:
    return irVal{typ: shaderir.Type{Main: shaderir.Bool}, b: cmpVal(a, b) != 0}

cmpVal (internal/driver/gpu_software.go:3377) returns -1, 1 or 0 and has two bugs:

  1. NaN compares equal to everything. The float branch returns 0 when neither a.f[0] < b.f[0] nor a.f[0] > b.f[0] holds, which is also the unordered case. So with x NaN, x == y, x <= y and x >= y are all true and x != y is false, for any y. IEEE 754 and WGSL require all of these except != to be false. This also breaks the usual x != x NaN test, which is always false on the software device.
  2. bool == bool and bool != bool ignore the operands. cmpVal switches on Int, Uint and falls back to comparing a.f[0], but irVal keeps a bool in its .b field (internal/driver/gpu_software.go:2487-2494) and leaves .f zero. So true == false is true and true != false is false, whatever the values.

Both reproduce with a fragment shader on gpu.NewSoftwareRenderer:

  • let n = 0.0 / 0.0; (or a NaN read from a uniform or texture) and select(vec4(0.0), vec4(1.0), n >= 1.0) returns 1.0; the GPU backends return 0.0.
  • With two bool values a = true, b = false (for example from comparisons on uniforms), select(vec4(0.0), vec4(1.0), a != b) returns 0.0; the GPU backends return 1.0.

Upstream main and v0.3.0 have the same code.

Why it matters

The software device is the reference renderer for headless tests and CI, so a shader that behaves differently there either fails tests that pass on hardware, or passes tests that should fail. A game layer built on NSGo (nsgo-dev/game) hit both: an HDR tone-mapping pass that filters NaN and negative values with x >= 0.0 let NaN through on the software device (worked around by using a strict x > 0.0), and a material shader using select(a, b, x != y) on bools picked the wrong branch.

Proposal

  • In cmpVal's float branch, return a distinct "unordered" result when either operand is NaN, and have evalBinary map it so that <, <=, >, >= and == give false and != gives true. Simplest is to evaluate each operator directly on the floats in evalBinary instead of going through a three-way compare.
  • Add a shaderir.Bool case so == and != compare a.b with b.b.
  • While there, check that the comparisons the VM evaluates for vector operands (WGSL's component-wise vecN<bool> results) are handled consistently, since cmpVal reads only element 0.

Acceptance

  • A software-device test renders with n NaN and checks that n == n, n < 1.0, n <= 1.0, n > 1.0, n >= 1.0 are false and n != n is true.
  • A software-device test checks true == false is false, true != false is true, and true == true is true.
  • Existing internal/platform/gputest scenes still pass on the software device.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions