Weird error in a Fragment Shader

This test code:

void main()
{
	int ar[] = {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1};

	float f_ind = gl_FragCoord.x * 0.01;

	int i_ind = int(f_ind);

	int val = ar[i_ind];

	if(val == 1)
		gl_FragColor = vec4(0.0, 1.0, 0.0, 1.0);
	else
		discard;
}

gives me this error (program info log):

(37) : error C6013: Only arrays of texcoords may be indexed in this profile, and only with a loop index variable

everything works fine if I assign a numeric value to f_ind (i.e. float f_ind = 5.0;).

Is this a bug or am I doing something wrong???

What nvidia gfx card do you have? From the error message it looks like it’s an older card which does not support what you are trying to achive and therefore you get the error message.

[ www.trenki.net | vector_math (3d math library) | software renderer ]

Some info I get with glGetString()

VENDOR: NVIDIA Corporation
RENDERER: GeForce 7600 GT/PCI/SSE2
VERSION: 2.1.1 NVIDIA 100.14.19
SHADING LANGUAGE VERSION: 1.20 NVIDIA via Cg compiler

I think my card is not so old… :slight_smile:

This calls for a texture lookup.
Put the data you want to index into a texture (1D in your case) and calculate the lookup s-coordinate instead of an array index.
There is also no need for expensive ifs if you use the HW alpha test and encode the discard condition into the texture.

I see, but why?

Yeah, this is the workaround I thought too, but I still think that this code is correct so this should be a compiler/language bug.

I don’t really need that if, I used it just to have a “visual debug”, however could you tell me more (or give me some reference) about encoding the discard condition into the texture?

Thank you.

Because your GPU isn’t able to index temporary or constant registers in the fragment shader.

The error message explained the problem pretty well. The only indexable data in a fragment shader on that generation hardware is the interpolators. At best you’d get software rendering.

Ok, I understand the problem now, thank you guys!

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