Skip to content

gpu: the software device evaluates exp, exp2, log, log2, atan and other WGSL builtins as 0 #2334

Backlog

Description

@houko

Context

Line numbers are at a34e7e7 (main). Found while building a game layer on NSGo (nsgo-dev/game), part of #1047.

The software GPU device (GPUSoftwareDevice, internal/driver/gpu_software.go:123, behind gpu.NewSoftwareRenderer and headless nsgotest) runs shaders by interpreting shaderir. irVM.evalCall (internal/driver/gpu_software.go:3463) switches on callee.BuiltinFunc (:3564) and, for any builtin without a case, falls through to return irVal{} (:3723). No error is raised at pipeline creation or at draw time; the call simply evaluates to zero.

The WGSL front end maps many builtins the VM has no case for. internal/shader/wgsl/builtins.go:68-77 registers asin, acos, atan, exp, exp2, log, log2, trunc, round (as RoundEven), sinh, cosh, tanh, dpdx/dpdy/fwidth and their variants, and builtins.go:30-36 registers distance, sign, atan2, faceForward, transpose and determinant, refract (:293). None of these shaderir builtins (internal/shaderir/program.go:324-398) appears anywhere in gpu_software.go; neither do Mod, Mat2F, the non-square matrix constructors or FloatBitsToInt / FloatBitsToUint / IntBitsToFloat / UintBitsToFloat. Some WGSL functions that look supported are lowered onto them too: ldexp multiplies by Exp2 (builtins.go:352), and asinh, acosh and atanh are written with Log (builtins.go:357-381), so they also come out as zero.

The interpreter does implement sin, cos, tan, pow, sqrt, inverseSqrt, abs, floor, ceil, fract, min, max, clamp, mix, step, smoothstep, length, normalize, dot, cross and reflect (gpu_software.go:3564-3722), which makes the missing ones easy to miss.

Nothing under docs/ describes the software device as supporting only a subset of builtins; docs/architecture/gpu.md presents it as the device compute and the extended formats run on today, and docs/public/en/testing.md points headless GPU tests at it.

Why it matters

Headless GPU tests and offscreen rendering with the software renderer silently produce wrong pixels or wrong compute results for ordinary shaders, and the test author sees only a black or saturated output, far from the cause. A game layer built on NSGo hit this four separate times while writing its renderer: exponential fog (exp), an environment BRDF approximation (exp2), the AgX tone mapper (log2) and clustered lighting's depth slice (log2 / exp). Each had to be rewritten around the gap (pow(2.718281828, x) for exp, pow(2.0, x) for exp2, a polynomial log2, a binary search over a split-depth table) so that its pixel tests could pass headlessly, even though every native backend runs the original WGSL correctly.

Because the failure is silent, a test can also pass for the wrong reason: a term that should be small but nonzero (fog factor, attenuation) evaluates to exactly zero, and an assertion with a tolerance may not notice.

Proposal

  1. Add cases to irVM.evalCall for every scalar or component-wise float builtin the front ends can emit, using the existing mapVec1 / mapVec2 helpers: Asin, Acos, Atan, Atan2, Exp, Exp2, Log, Log2, Sign, Trunc, RoundEven, Sinh, Cosh, Tanh, Mod, plus Distance, Faceforward, Refract, Transpose, Determinant, Mat2F and the non-square matrix constructors, and the four bit-cast builtins (which need the int / uint lanes, not f). Derivatives (Dfdx, Dfdy, Fwidth) need quad evaluation; until the rasterizer provides that, they could return 0 deliberately and be documented as such.
  2. Make an unknown builtin fail loudly instead of returning irVal{}: either reject the pipeline at CreatePipeline / CreateComputePipeline on the software device by walking the IR for builtins the VM does not implement, or panic / record a structured error in the default branch. A test that uses an unsupported builtin should fail with the builtin's name.
  3. Add a table-driven test that compiles a small WGSL compute shader calling each builtin internal/shader/wgsl/builtins.go registers, dispatches it on the software device and compares the results with Go's math package, so a builtin added to the front end without a VM case fails CI.

Acceptance

  • exp, exp2, log, log2, atan, atan2, asin, acos, sign, trunc, round, sinh, cosh, tanh, distance, faceForward, refract, transpose, determinant, ldexp, asinh, acosh, atanh and bitcast between f32 and i32 / u32 give the same results on the software device as on the native backends, within float32 tolerance, for scalars and vectors.
  • No shaderir builtin the WGSL or Kage front ends can emit reaches the return irVal{} fallthrough silently: it is either implemented or reported as an error naming the builtin.
  • A test in internal/driver (or gpu) covers every builtin in the WGSL front end's table against 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