Profile requires index expression to be compile-time constant

I am working on a fragment shader running on a Quadro FX 350M.

The shader compiler complains:

error 6504: profile requires index expression to be compile-time constant

when my code contains

glFragColor = col[iu];

where col[] is an array of vec4 colors.

So I do this:

       if(iu == 0)    gl_FragColor = col[0];
  else if(iu == 1)    gl_FragColor = col[1];
  else if(iu == 2)    gl_FragColor = col[2];
  else if(iu == 3)    gl_FragColor = col[3];
  else if(iu == 4)    gl_FragColor = col[4];
  else if(iu == 5)    gl_FragColor = col[5];
  else if(iu == 6)    gl_FragColor = col[6];
  else if(iu == 7)    gl_FragColor = col[7];
  else if(iu == 8)    gl_FragColor = col[8];
  else if(iu == 9)    gl_FragColor = col[9];

The compiler is happy and the shader works. Of course for larger arrays this is impractical. Also, I must go through a similar contortion to write to an array.

I can’t figure out what and where my profile is, let alone how to change it. Or is there another way around the kludge I’m using?

I’d appreciate any help.

sure there is, just put those colors in a 1D texture and transform the iu variable to the correct uv-coordinate.

The thing is that the FX cards can’t do branching, this means that all shaders have to be unraveled at compile time, the same goes with arrays.

Thank you for help.

The answer raises other questions. What is “branching” and “unraveled”?

I am used to having a pretty good feel for what my ordinary c code will compile to on an ordinary CPU and how it interacts with the hardware. Certainly vertex and fragment programs compile to a different computer hardware architecture which in some cases appears to restrict what can be programmed. Another example: I put a return in a conditional statement and the fragment program compiler complains:

error c5051: profile does not support conditional returns.

Now the OpenGL Shading Language specification indicates that it is ok to have such returns and doesn’t mention the possibility that in some cases it isn’t ok. The error message mentions a “profile”. Is this like the OpenGL ES common and common light profiles? Is it something I can get a hold of and read so I know what I can and can’t do?

I guess I’m after some understanding of what the compiler does with a shader program and some model of the hardware this compiled program interacts with. This is because it appears to be very different than the CPU model I am used to.

Sorry for the long post. Any references I can be pointed at would be much appreciated.

You need to understand that nvidia GLSL compiler is basically the Cg (nvidia shading language) compiler embedded into the driver. Cg can be compiled to various output formats (or profiles) for different hardware. Some profiles(hardware) support more functionality, some - less. In your case it means that your graphic card hardware is not able of executing conditional code (branching) or performing indexed array access in the fragment shader. It can, however, do conditional writes (data selection). Basically, code like

if (e) x = a else x = b

will be treated

x1 = a
x2 = b
x = iif(e, x1, x2)

It is true that the driver behavior is not really spec-conformant, but I think it is still better then forcing software mode… Consider buying a better video card

Thank you so much!

This topic was automatically closed 183 days after the last reply. New replies are no longer allowed.