Sign in to follow this  

GLSL Optimizing out Attribute

This topic is 3312 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm attempting to implement basic bump mapping in tangent space, and for this I need to pass the tangent vector per vertex. Using samples on from the web, I've got the following vertex shader: attribute vec3 tangent; varying vec3 lightVec; varying vec3 eyeVec; void main(void) { vec3 n = normalize(gl_NormalMatrix * gl_Normal); vec3 t = normalize(gl_NormalMatrix * tangent); vec3 b = cross(n, t); vec3 vVertex = vec3(gl_ModelViewMatrix * gl_Vertex); vec3 tmpVec = gl_LightSource[0].position.xyz - vVertex; lightVec.x = dot(tmpVec, t); lightVec.y = dot(tmpVec, b); lightVec.z = dot(tmpVec, n); tmpVec = -vVertex; eyeVec.x = dot(tmpVec, t); eyeVec.y = dot(tmpVec, b); eyeVec.z = dot(tmpVec, n); //complete our standard fixed functionality gl_Position = ftransform(); gl_TexCoord[0] = gl_MultiTexCoord0; } However, my tangent attribute is always optimized out. Now, I understand that unused attributes will be optimized out, but I am using it to calculate my varying variables. Why is it being optimized out? How can I stop it? I've exhausted every book I have, and every internet resource. All of them just say they are optimized out if they are not used in the final calculation of a gl component (e.g. gl_Position), but surely this is not the case if it is used to calculate varying variables?

Share this post


Link to post
Share on other sites
Instead of passing the tangent in as an attribute, pass it in as a multiTexCoord#.

In your c++ code, you would set it up like this:

[source lang = "cpp"]

glBegin(GL_TRIANGLES)
{
for(i = 0; i < numVerts; ++i)
{
glMultiTexCoord2f(GL_TEXTURE0, vert[i].u, vert[i].v);
glMultiTexCoord3f(GL_TEXTURE1, vert[i].tangent.x, vert[i].tangent.y, vert[i].tangent.z);
glNormal3f(...);
glVertex3f(...);
}
}





In your shader, you would do this:


[source lang = "cpp"]
void main(void)
{
gl_TexCoord[0] = glMultiTexCoord0;

vec3 tangent = vec3(glMultiTexCoord1).xyz;
}





Hope this helps

Share this post


Link to post
Share on other sites
My bet the reason your tangent vector is getting optimized out is because, the varying calculated that uses it are not being used by your fragment shader, so then the driver can optimize it out, post your fragment shader too.

Share this post


Link to post
Share on other sites
Well, sending it as a multi-tex coord works. However, I still can't figure out why it's being optimized out.

The fragment shader, however, is making use of the varying variables, so that theory is defunct also.

Share this post


Link to post
Share on other sites
Intead of writing

vec3 tangent = vec3(glMultiTexCoord1).xyz;

you can do

vec3 tangent = glMultiTexCoord1.xyz;

or

vec3 tangent = vec3(glMultiTexCoord1);

Share this post


Link to post
Share on other sites
jwrayth, I guess you're using cgc.exe to compile to ARB asm. If that's the case, then make use of Cg semantics:

varying vec3 lightVec : TEX0;

You haven't posted the frag-shader code, it really may be ignoring that varying for some computational reason. (i.e a calculation with it always returning {0,0,0})

Share this post


Link to post
Share on other sites

This topic is 3312 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this