gl_ClipVertex

Hi,

I am trying to replace the fixed functionality renderer with a glsl shader then improve it with various features.

Up to the point of clipping everything works very well. However, if I try to write to gl_ClipVertex (gl_ModelViewMatrix*gl_Vertex) in the vertex shader to use the enabled clip planes, I get a “vertex attribute count exceeds hardware limits” error message in Shader Designer and the shader does not work in my application either.

I don’t understand what can cause this, I didn’t even define any additional vertex attributes at all. I have a few dozens of uniforms and several varying variables, but that is all.

My OS is WinXP SP2 and my GPU is a 8800GT with the latest nVidia driver.

Thanks for your help.

Looks like clipvetex is deprecated in GL3…

If you insist on using it you might want to check out pages 58-59 of the (GL3) GLSL spec if you haven’t already, just to make sure you’ve battened your hatches.

It could be deprecated, but since there are no stable drivers yet and no design tools that would help me create a glsl 1.3 shader, I am sticking to the old stuff for now.

The problem is very strange, because writing to gl_ClipVertex in the vertex shader creates the strange problem not in the vertex shader but in the fragment shader. I get the error message during the linking of the shaders. I tried to pinpoint what is not working any more in the fragment shader, and if I comment out this line everything is ‘normal’ again:

VertColor = gl_FrontLightModelProduct.sceneColor + FAmbient * gl_FrontMaterial.ambient + FDiffuse * gl_FrontMaterial.diffuse;

Except I need this in the fragment shader if I want to use per fragment lights (and I do). I absolutely do not understand why this causes the “… exceeds hardware limits” message. If I only omit the ambient term like this:

VertColor = gl_FrontLightModelProduct.sceneColor + FDiffuse * gl_FrontMaterial.diffuse;

…then it works again, except there is no ambient lighting of course.

It still works if I ‘cheat’ and use a fixed ambient value:

VertColor = vec4(0.05,0.05,0.05,0.0);
VertColor += gl_FrontLightModelProduct.sceneColor + FDiffuse * gl_FrontMaterial.diffuse;

This looks like a total mystery to me. If anyone has a logical explanation, I would really appreciate it, because I have a strong suspicion the reason is something I should know about.

Maybe it is a driver bug.
It seems that “FDiffuse” in you codes is a varying variable, so i think there are too many varyings in your shader, but in fact, driver should give link error like this:
fragment attribute count exceeds hardware limits”

I hope it is not a driver bug, it is supposed to be a mature driver and a very successful and widely used card.

FDiffuse is not a varying, it is defined in the fragment shader, only its initial value is being read from a varying passed by the vertex shader.

I use 7 varying variables, maybe 50 uniforms and no attributes in my shader. I don’t think I should be close to any limit.

Now I really don’t know what is going on. It seems that the problem has nothing to do with ClipVertex. Here is the whole shader:

I cut away as much as I could to make it as short as possible. The vertex shader does not make too much sense this way but I deleted most of it on purpose. This shader does not work. When I compile it in Shader Designer, I get this error message:

Vertex attribute output count exceeds hardware limits

If I comment out just one line (line 74) in the Directional function:
FSpecular+=clamp(Ispec,0.0,1.0);

Then the shader works and I get a per fragment point light. But this makes no sense at all, at least not to me. Plese help.

Vertex shader:

varying vec3 Normal;
varying vec4 Ambient;
varying vec4 Diffuse;
varying vec4 Specular;

varying vec4 Campos;
varying vec3 Campos3;
varying vec3 Eye;

void main()
{
int i;

gl_TexCoord[0] = gl_TextureMatrix[0]*gl_MultiTexCoord0;
gl_TexCoord[1]=gl_TexCoord[2]=gl_TexCoord[3]=gl_TexCoord[4]=gl_TexCoord[0];
    
gl_Position = ftransform();
Normal = gl_NormalMatrix * gl_Normal;
Normal=normalize(Normal);

Ambient=vec4(0.0,0.0,0.0,0.0);
Diffuse=Ambient;
Specular=Ambient;

Campos=gl_ModelViewMatrix * gl_Vertex;
Campos3=Campos.xyz/Campos.w;

Eye=vec3(0.0, 0.0, 1.0);

gl_ClipVertex = Campos;

}

Fragment Shader:

uniform int texblending;
uniform int tenable[8];
uniform int lighttype[8];
uniform int atten[8];
uniform int numlights;
uniform int objperpixel;
uniform int lightperpixel[4];
uniform sampler2D tex0;
uniform sampler2D tex1;
uniform sampler2D tex2;
uniform sampler2D tex3;
uniform sampler2D tex4;

varying vec3 Normal;
varying vec4 Campos;
varying vec3 Campos3;
varying vec3 Eye;
varying vec4 Ambient;
varying vec4 Diffuse;
varying vec4 Specular;
vec4 FAmbient;
vec4 FDiffuse;
vec4 FSpecular;
vec4 VertColor;
float FShiny;
vec2 texcoor;

void Spot (in int ix)
{
}

void Point (in int ix)
{
//
float d;
float attenu=1.0;

vec3 L=normalize(gl_LightSource[ix].position.xyz - Campos3);

if (atten[ix]>0)
{
	d=length(L);
	attenu = 1.0 / (gl_LightSource[ix].constantAttenuation +
		gl_LightSource[ix].linearAttenuation * d +
		gl_LightSource[ix].quadraticAttenuation * d * d);
}

vec3 E=normalize(-Campos.xyz);
vec3 R=normalize(-reflect(L,Normal));

FAmbient+=gl_LightSource[ix].ambient;
FDiffuse+=clamp(( gl_LightSource[ix].diffuse * max(dot(Normal,L),0.0)),0.0,1.0)*attenu;
vec4 Ispec= gl_LightSource[ix].specular * pow(max(dot(R,E),0.0),FShiny)*attenu;

FSpecular+=clamp(Ispec,0.0,1.0);

//
}

void Directional (in int ix)
{

float d;

vec3 L=normalize(gl_LightSource[ix].position.xyz);
vec3 E=normalize(Eye);
vec3 R=normalize(-reflect(L,Normal));

FAmbient+=gl_LightSource[ix].ambient;
FDiffuse+=clamp(( gl_LightSource[ix].diffuse * max(dot(Normal,L),0.0)),0.0,1.0);
vec4 Ispec= gl_LightSource[ix].specular * pow(max(dot(R,E),0.0),FShiny);

//////
FSpecular+=clamp(Ispec,0.0,1.0);
//////
}

void PixelLight(in int i)
{

if (lighttype[i]==0)
	Directional (i);
else if (lighttype[i]==1)
{
	Point(i);
}
else if(lighttype[i]==2)
	Spot(i);

}
void Textura(out vec4 color)
{
color=vec4(0.0,0.0,0.0,0.0);
texcoor=vec2(gl_TexCoord[0].xy);
vec4 texture;
vec3 tempColor;

if (tenable[0]>0)
{
	color = texture2D(tex0, gl_TexCoord[0].xy);
}
if (tenable[1]>0)
{
	texture = texture2D(tex1, gl_TexCoord[0].xy);
	tempColor = mix(color.rgb, texture.rgb, texture.g);
	color = vec4 (tempColor, color.a);
}
if (tenable[2]>0)
{
	texture = texture2D(tex2, gl_TexCoord[0].xy);
	tempColor = mix(color.rgb, texture.rgb, texture.g);
	color = vec4 (tempColor, color.a);
}

if (tenable[3]>0)
{
	texture = texture2D(tex3, gl_TexCoord[0].xy);
	tempColor = mix(color.rgb, texture.rgb, texture.g);
	color = vec4 (tempColor, color.a);
}      

}

void main()
{
int i;

VertColor=gl_Color;

FAmbient=Ambient;
FDiffuse=Diffuse;
FSpecular=Specular;

FShiny=(gl_FrontMaterial.shininess+3.0)*0.3;
	
if (objperpixel>0)
{
	for (i=0; i<numlights;i++)
	{
		if (lightperpixel[i]>0)
		{
			PixelLight(i);
		}
	}
}

VertColor = vec4(FAmbient.rgb,gl_Color.a);
VertColor += gl_FrontLightModelProduct.sceneColor +  FDiffuse * gl_FrontMaterial.diffuse;	// + FSpecular * gl_FrontMaterial.specular;

if (tenable[4]>0)
{
	FSpecular = texture2D(tex4, gl_TexCoord[0].xy) * gl_FrontMaterial.specular;
}
vec4 Vcolor=vec4(VertColor);

vec4 color=vec4(0.0,0.0,0.0,0.0);

if (texblending>-1)
{
	Textura(color);
   	if (color.a>0.0)
   	{
	    if (texblending==0)
			Vcolor=color;
	    //else if (texblending==1)
			//VertColor=color=VertColor;
	    else if (texblending==2)
	    {
	    	vec3 temp=mix(Vcolor.rgb, color.rgb,color.a);
	    	Vcolor=vec4(temp,color.a);
	    }
	    else if (texblending==3)
	    {
	    	Vcolor.rgb+=color.rgb;
	    	Vcolor=clamp(Vcolor,0.0,1.0);
	    }
    }
}  
	
Vcolor += FSpecular * gl_FrontMaterial.specular;

gl_FragColor =Vcolor;

}

OK, I found out myself.

I totally misunderstood the limits of varying variables, and although the error message itself was incorrect, I ran out of varying slots.

I don’t yet know how I can squeeze varying variables needed for up to 8 lights - per fragment - into the 60 varying floats, but I guess I am going to find out.

That’s why you use deferred lighting.

Only in version 3.0

I am working on 2.0 at the moment :slight_smile:

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