Incorrect loop optimization on a simple OpenGL fragment shader

ization on a simple OpenGL fragment shader

The NVIDIA driver has a miscompilation on a simple fragment shader program. The program is as follows:

uniform int t;
layout(location = 0) out vec4 fragColor;
void main() {
  uint c = 0u;
  for (int i = -1; i > -2; i -= t) {
    c = uint(1);
  }
  fragColor = vec4(float(c));
}

During runtime, the uniform input variable t is set to 1. So the loop will be executed once, and variable c will receive the value of 1, causing the fragColor to be set to vec4(1.0) (white color). However, the NVIDIA driver incorrectly optimizes the loop, causing the fragment shader to return vec4(0.0) (black color) instead.

Further investigation shows that any of the following modifications will make the bug disappear:

  1. Remove the explicit initialization of c (i.e., uint c;).
  2. Change the loop condition to i = 0; i < 1; i += t.
  3. Change the loop condition to i = -1; i >= -2; i -= 1, i.e., substitute t with a constant value.

Steps to reproduce:

I prepare a minimal OpenGL application that loads the fragment shader to draw a 256x256 white square. The application source code and the fragment shader that triggers the error are given here. The package also contains a detailed README file describing system information and steps to run the code. It should take no more than a minute to reproduce the issue.

Hello, I am wondering is there any update on this bug?

I believe that this bug most likely happens in the compiler of the NVIDIA graphics driver. I am using NVIDIA 3060, and the detailed information for my environment is provided in the post above.

This bug is creating headaches for me as my shader program constantly encounters variants of this bug example and got incorrect results.

I would greatly appreciate it if you could fix this bug. Thanks!