View Full Version : Texture lookup into vertex and fragment shader with Cg

03-03-2006, 03:46 AM
hi, I've got a problem when runnging a Cg (v1.4) vertex shader doing a texture lookup (32f, GL_NEAREST - runs HW-accelerated) and a fragment shader which is doing a lookup into another texture. It seems that the fragment shader does a lookup into the texture which was bound to the vertex shader.

So my question:
is it possible to do texture lookups into different textures in a vertex and fragment shader in one render pass?

In my Cg code I didn't set the TEXUNITS for both textures (Cg doesn't require this) - I just bound the one textureid to the vertex, the other to the fragment shader. Does anyone know if it might work if I set the TEXUNITS? Or maybe with GLSL


03-03-2006, 04:27 AM
Hi AnselmG,

Can you show us the shaders (vertex and fragment)?

Thanks -

03-03-2006, 06:32 AM
ok, I can give you more details:
(my HW is a GF 6800 Ultra AGP, driver: 81.98)

I need to do a 2D displacement for every pixel. therefore I render a GL_POINT at every pixel location with approprialte texture coordinates. then the vertex shader does a lookup into a texture GL_TEXTURE_2D NPOT with display res. this texture stores a new location for that point...the shader looks like this:

vertout main (appin IN
,uniform sampler2D lookupTexture
vertout OUT;
float2 tc = IN.texcoord.xy;
float2 offset = tex2D(lookupTexture, tc).xy;
float4 newPos = float4(offset.xy, IN.position.zw);
OUT.position = mul(glstate.matrix.mvp, newPos);
OUT.texcoords = tc.xy;
return OUT;

that works fine...in the fragment shader I now tried to apply an image texture onto that fragments:

pixout main(vertout IN,
uniform sampler2D image
) {

pixout OUT;

OUT.color = tex2D(image, IN.texcoords.xy);
return OUT;

but that didn't work - the result was an image textured with the looup texture from the vertex shader. I did a workaround and used the fragment shader to encode the displaced texcoords into the RG channels and rendered this into and 32f FBO - doing the image lookup in another render pass...but well...this doesn't seem to be the most efficent way to me....


03-03-2006, 11:39 AM
my pick would be to do: "uniform sampler2D image : TEXUNIT1" and the other TEXUNIT0 for the vertexshader..

you should add those : TEXUNITx things,else Cg doesnt know which texture you want to use... unless you use the cgruntime to the binding, which I never used so far, always bind with gl...

maybe that does the trick, however I havent used any vertex texture fetches yet, so pure guessing here

03-03-2006, 06:18 PM
If you don't specify which texture unit a sampler is bound to (either in the shader or through the runtime), then the compiler will choose resources for you. Since you have a vertex and fragment shader (note they are separate shaders) the compiler is auto assigning TEXUNIT0 to the sampler in each shader.