View Full Version : A strange problem about Cg FS

10-29-2006, 08:10 PM
I happen to find a problem about Cg fs, it is as following:

float4 main(float2 texcoord : TEXCOORD0,
uniform samplerRECT originTex,
uniform float w,
uniform float h,
uniform float tx,
uniform float ty):COLOR
float ss = tx+(texcoord.x-tx)*w;
float tt = ty+(texcoord.y-ty)*h;
float s[4], t[4];
s[0] = ss-1;
s[0] = 0;
s[1] = s[0]+1;
s[2] = s[0]+2;
s[3] = s[0]+3;

t[0] = tt-1;
t[0] = 0;
t[1] = t[0]+1;
t[2] = t[0]+2;
t[3] = t[0]+3;

float s01 = s[0]-s[1];
float s02 = s[0]-s[2];
float s03 = s[0]-s[3];
float s12 = s[1]-s[2];
float s13 = s[1]-s[3];
float s23 = s[2]-s[3];

float t01 = t[0]-t[1];
float t02 = t[0]-t[2];
float t03 = t[0]-t[3];
float t12 = t[1]-t[2];
float t13 = t[1]-t[3];
float t23 = t[2]-t[3];

float sa, sb, sc, sd;
sa = (ss-s[1])*(ss-s[2])*(ss-s[3])/(s01*s02*s03);
sb = (ss-s[0])*(ss-s[2])*(ss-s[3])/(-s01*s12*s13);
sc = (ss-s[0])*(ss-s[1])*(ss-s[3])/(s02*s12*s23);
sd = (ss-s[0])*(ss-s[1])*(ss-s[2])/(-s03*s13*s23);

float3 col[4];
col[0] = texRECT(originTex,float2(s[0],t[0])).xyz;
col[1] = texRECT(originTex,float2(s[1],t[1])).xyz;
col[2] = texRECT(originTex,float2(s[2],t[2])).xyz;
col[3] = texRECT(originTex,float2(s[3],t[3])).xyz;

float3 someColor;
someColor = col[0]*sa+col[1]*sb+col[2]*sc+col[3]*sd;
return float4(someColor, 1);
}Just a FS in my program, a quad is drawn like this:

//Bind VS and FS ...
cgGLSetParameter1f(g_W, s);
cgGLSetParameter1f(g_H, s);
cgGLSetParameter1f(g_Tx, tl);
cgGLSetParameter1f(g_Ty, tb);

glTexCoord2i(tl, tb);
glVertex2f(sl, sb);
glTexCoord2i(tl+sx*(tr-tl), tb);
glVertex2f(sr, sb);
glTexCoord2i(tl+sx*(tr-tl), tb+sy*(tt-tb));
glVertex2f(sr, st);
glTexCoord2i(tl, tb+sy*(tt-tb));
glVertex2f(sl, st);
glEnd();g_W,g_H,g_Tx,g_Ty are the CGParameter values of the uniform variables in FS.
After running, when i move mouse to change the "tl" and "tb" value, i find at the first few frames, it is rendered in very low fps(less than 10), but soon rendered in high fps(normal). The rendering result is right but fps shows strange way, why?

10-30-2006, 03:50 PM
Later i test the Cg program and i find that the "tx" and "ty" affect considerably. After I add a VS and move the uniform variables to VS(then passed on to FS as "out" parameters). fps is normal. It is so strange!