View Full Version : Problem when using uniform variable as a loop count in fragment shader

mr somboon
02-24-2007, 11:10 PM
I've some problem about my fragment shader.

i have a function to calculate lighting define as

void diffuseHemiSphere(int i,vec3 normal,vec3 lightVec,float attenuation){

vec4 SkyColor = gl_LightSource[i].diffuse;
vec4 GroundColor = vec4(0.0,0.0,0.0,0.0);

float costheta = dot(normal, lightVec);
float a = 0.5 + 0.5 * costheta;

diffuseTerm += (a*SkyColor + (1.0-a)*GroundColor)*gl_FrontMaterial.diffuse*attenuati on;

}when i do something like this it work

for(int i=0;i<1;i++){

}but when i use uniform variable as a loop count it does'nt work

for(int i=0;i<lightCount;i++){

}"lightCount" is a uniform variable define in fragment shader,and I have successfully use it as
a if/else condition.

I have tried it on my both Geforce 7600gt/6100.
using the latest nvidia's driver

Can someone explain it to me why it did'nt work.



02-25-2007, 02:53 AM
What is "didn't work"? It won't compile?

02-25-2007, 04:35 AM
Post the complete shader. Nobody can the if it's a CG or GLSL shader.
With GLSL it should work. For CG is a profile needet that supports dynamic branching (minimum fp40)

02-25-2007, 08:32 AM
That functionality is not supported in hardware. gl_LightSource[i] is a indexed lookup into constants, which is only supported in the vertex shader in this generation hardware. If you have a static loop it can be unrolled and made into static constant lookups. If you absolutely need to make it dynamic, you'll need to store indexed data into a 1D texture and sample that instead.

mr somboon
02-25-2007, 03:56 PM
Thank for your answer.

I will try to create a shader based on number of lights in the scene instead of making a dynamic one.