View Full Version : texture matrix + texture rect + fragment shader

02-06-2004, 06:44 AM
OK this I do not understand...
I tend to futs with my texture matrix. I use it to scale the texture coordinates differently depending on whether the texture is GL_TEXTURE_RECTANGLE_NV or just GL_TEXTURE_2D, and I also use it for flipping the image. Pretty regular stuff.

What's perplexing is that precisely these texture matrix operations screw up the texture coordinates in the fragment shader if and only if I'm using GL_TEXTURE_RECTANGLE_NV. I.e. if I simply apply a glScalef(1,-1,1) to the texture matrix the image goes black if I use a fragment shader, if I use the same code with the regular gl (no shader) the texture simply flips (i.e. the texture matrix modifications are correct). The shader works correctly as long as I don't use GL_TEXTURE_RECTANGLE_NV, and it also works correctly if I use GL_TEXTURE_RECTANGLE_NV and don't touch the texture matrix but modify the texture coordinates in software...

I should also point out that I'm not using a vertex shader, and that in the following fragment shader WPOS is also incorrectly modified by the texture matrix operations as well (x is correct and y stays 0)... very strange... makes no sense whatever to me

float4 main(in float4 tcrd : TEXCOORD0,
in float4 wpos : WPOS,

uniform samplerRECT back,
uniform samplerRECT front) : COLOR
float epsilon = 0.001f;
float4 bpix = texRECT(back, tcrd .xy);
float4 fpix = texRECT(front, tcrd .xy);

float balpha = bpix.a;
float falpha = fpix.a;
float balphaAdj = balpha * (1-falpha);
float finalAlpha = falpha + balphaAdj;

bpix = (fpix * falpha + bpix * balphaAdj) / finalAlpha;
bpix.a = finalAlpha;

return bpix;

can someone explain what I'm missing here?

02-09-2004, 02:34 AM
Have you looked at what assembly code the compiler generated?
Maybe there's a clamp to zero on texture coordinate y if you always get y == 0 from a glScale(1, -1, 1)?

02-09-2004, 06:29 AM
How silly of me. Of course I can't use glScalef(1,-1,1) to flip a fragment input texture with GL_TEXTURE_RECTANGLE_NV since you can't tile with GL_TEXTURE_RECTANGLE_NV. I need to do a translate as well.

But that still doesn't explain why touching the texture matrix changes the destination pixel (WPOS)

this is what the compiled code looks like:

#profile arbfp1
#program main
#semantic main.tex
#semantic main.back
#var float4 color : $vin.COLOR : COLOR : 0 : 1
#var float4 wpos : $vin.WPOS : WPOS : 1 : 1
#var samplerRECT tex : : texunit 0 : 2 : 1
#var samplerRECT back : : texunit 1 : 3 : 1
#var float4 main : $vout.COLOR : COLOR : -1 : 1
PARAM c0 = {1, 0, 0, 0};
TEX R0, fragment.position, texture[1], RECT;
ADD R1.x, c0.x, -fragment.color.primary.w;
MUL R1.y, R0.w, R1.x;
MUL R2, R0, R1.y;
MAD R1.x, R0.w, R1.x, fragment.color.primary.w;
MAD R2, fragment.color.primary, fragment.color.primary.w, R2;
RCP R0.x, R1.x;
MUL result.color.xyz, R2, R0.x;
MOV result.color.w, R1.x;