GLES shader compiler: small negative constant times mediump value gives wrong result

In an OpenGL ES 3.2 fragment shader, multiplying a mediump value by a small
negative constant gives a wrong result: the constant behaves like -4.0.

#version 320 es
precision highp float;
precision mediump sampler2D;
uniform sampler2D feat;   // RGBA16F texture filled with -0.05
in vec2 uv;
out vec4 o;
void main() {
    float x = -1.0e-05 * texture(feat, uv).b;
    o = vec4(x, 0.0, 0.0, 1.0);
}

Expected x = 0.0000005, got x = 0.199951.

Only constants between about -1e-5 and -6e-5 are affected. -7e-5, -1e-6 and
positive constants give correct results. With precision highp sampler2D
the result is correct.

Full standalone repro attached

gles_const_test.py.txt (6.3 KB)

(gles_const_test.py, needs only python3 and
the driver, runs headless via EGL). Output:

GL_RENDERER: NVIDIA GeForce GTX 1660 Ti/PCIe/SSE2 | GL_VERSION: OpenGL ES 3.2 NVIDIA 615.71.09
feat.b = -0.05; x = const * feat.b; expected x = -0.05 * const

const       -1.0e-05 (expected x = +0.000001):  mediump x = +0.199951 WRONG   highp   x = +0.000000 ok
const -2.5856789e-05 (expected x = +0.000001):  mediump x = +0.199951 WRONG   highp   x = +0.000001 ok
const       -6.0e-05 (expected x = +0.000003):  mediump x = +0.199951 WRONG   highp   x = +0.000003 ok
const       -7.0e-05 (expected x = +0.000003):  mediump x = +0.000004 ok   highp   x = +0.000003 ok
const  2.5856789e-05 (expected x = -0.000001):  mediump x = +0.000000 ok   highp   x = -0.000001 ok

Reproduced on Linux:

  • GTX 1660 Ti, 615.71.09
  • RTX 2060 SUPER, 610.43.02
  • RTX 3060, 570.172.08 and 560.35.03
  • RTX 5060 Ti, 580.126.09

Mesa llvmpipe gives correct results for the same test.

Found via mpv: vo=gpu: GLES shader header uses mediump sampler2D, causing a strong green cast with CNN user shaders (Anime4K) on NVIDIA · Issue #18566 · mpv-player/mpv · GitHub
Possibly related: Nvidia OpenGL compiler bug (-k * log2(r)); bugged