Repository navigation
Description
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:
- NaN compares equal to everything. The float branch returns 0 when neither
a.f[0] < b.f[0]nora.f[0] > b.f[0]holds, which is also the unordered case. So withxNaN,x == y,x <= yandx >= yare all true andx != yis false, for anyy. IEEE 754 and WGSL require all of these except!=to be false. This also breaks the usualx != xNaN test, which is always false on the software device. bool == boolandbool != boolignore the operands.cmpValswitches onInt,Uintand falls back to comparinga.f[0], butirValkeeps a bool in its.bfield (internal/driver/gpu_software.go:2487-2494) and leaves.fzero. Sotrue == falseis true andtrue != falseis 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) andselect(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 haveevalBinarymap it so that<,<=,>,>=and==give false and!=gives true. Simplest is to evaluate each operator directly on the floats inevalBinaryinstead of going through a three-way compare. - Add a
shaderir.Boolcase so==and!=comparea.bwithb.b. - While there, check that the comparisons the VM evaluates for vector operands (WGSL's component-wise
vecN<bool>results) are handled consistently, sincecmpValreads only element 0.
Acceptance
- A software-device test renders with
nNaN and checks thatn == n,n < 1.0,n <= 1.0,n > 1.0,n >= 1.0are false andn != nis true. - A software-device test checks
true == falseis false,true != falseis true, andtrue == trueis true. - Existing
internal/platform/gputestscenes still pass on the software device.
Activity