PDA

View Full Version : GLSL instruction limitation ?


NDark
04-22-2008, 02:01 AM
I write both vertex and fragment shader with GLSL , and I am trying to blend 4 or more textures in one pass which means sampling different textures and summation those colors with some sort of weighting .

I use 3 textures the result is fine . When I use 4 , the program using is fail . At started I think it might be memory , I decrease the variable using , still failed . When I comment the calculation of weighting , 4 texture is OK .



So , I wonder that is there instruction limitation in GLSL , or how small it is ( because I didn't write too large my codes ) ?



My graphic card is GeForce 7600GS , OpenGL2.0 , NVidia driver updated and NVidia OpenGL SDK 10 is installed .

My code is following :
vertex shader :

varying vec3 L0 ;// 4 light position for weighting
varying vec3 L1 ;
varying vec3 L2 ;
varying vec3 L3 ;
varying vec3 N ;// normal

void main(void)
{
vec3 P = vec3( gl_ModelViewMatrix * gl_Vertex) ;
L0 = normalize( gl_LightSource[ 0 ].position - P ) ;
L1 = normalize( gl_LightSource[ 1 ].position - P ) ;
L2 = normalize( gl_LightSource[ 2 ].position - P ) ;
L3 = normalize( gl_LightSource[ 3 ].position - P ) ;
N = normalize( gl_NormalMatrix * gl_Normal);

// texture matrix for projective texture
gl_TexCoord[ 0 ] = gl_TextureMatrix[ 0 ] * gl_Vertex ;
gl_TexCoord[ 1 ] = gl_TextureMatrix[ 1 ] * gl_Vertex ;
gl_TexCoord[ 2 ] = gl_TextureMatrix[ 2 ] * gl_Vertex ;
gl_TexCoord[ 3 ] = gl_TextureMatrix[ 3 ] * gl_Vertex ;

gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex ;
}



fragment shader :

varying vec3 L0 ;
varying vec3 L1 ;
varying vec3 L2 ;
varying vec3 L3 ;
varying vec3 N ;
uniform sampler2D Texture0 ;// 4 texture binded with GL_TEXTURE0~4
uniform sampler2D Texture1 ;
uniform sampler2D Texture2 ;
uniform sampler2D Texture3 ;

void main (void)
{
vec4 texColor = texture2DProj( Texture0 , gl_TexCoord[ 0 ] ) * max( dot( N , L0 ), 0.0 ) ;
texColor += texture2DProj( Texture1 , gl_TexCoord[ 1 ] ) * max( dot( N , L1 ), 0.0 ) ;
texColor += texture2DProj( Texture3 , gl_TexCoord[ 2 ] ) * max( dot( N , L2 ), 0.0 ) ;

texColor += texture2DProj( Texture3 , gl_TexCoord[ 3 ] ) * max( dot( N , L3 ), 0.0 ) ;// If i comment this line , the program works .
gl_FragColor = min( texColor , 1.0 ) ; // prevent overflow
}


thanks in advance .


NDark

Kenneth Gorking
04-22-2008, 05:12 AM
As far as I remember, GLSL 1.10 has an instruction limit of 1024, but not all implementations adhere to the specifications (just look at the dynamic branching issues)...

There are also some inconsistencies in your code, you could try and fix them and see what happens:


In the vertex program you have a 'varying vec3 P', but not in the fragment program
When weighting the 3rd texture, you are using L3 instead of L2
'min( texColor , 1.0 )' should probably be 'texColor = min( texColor , 1.0 )'


Btw. I'm pretty sure the hardware automatically clamps the output values of fragment program.

NDark
04-22-2008, 06:33 PM
thank you , Mr. Gorking , I fixed the writed-mistake , and make code more clear.

NDark
05-05-2008, 01:39 AM
It's the problem of using too much varying variable in Shader ,
solution link is here :
http://www.opengl.org/discussion_boards/ubbthreads.php?ubb=showflat&Number=237438#Post237438