PDA

View Full Version : Cg Shader : Curious problem



MiDeM
01-27-2005, 05:24 AM
Hi,

I am trying to develop with Cg. I have a basic problem.

Here's the my very simple code :

For Vp:

struct VertexIn
{
float4 mPosition : POSITION; // World space
float4 mTexCoord : TEXCOORD0; // Texture coord for unit 0.
};

struct VertexToFragment
{
float4 mOutPosition : POSITION; // Clip space
};


VertexToFragment main( VertexIn pInVertex,
uniform float4x4 pModelViewProj)
{
VertexToFragment lOutVertex;

lOutVertex.mOutPosition = mul(pModelViewProj, pInVertex.mPosition);

return lOutVertex;
} For Fp:

struct VertexToFragment
{
float4 mOutPosition : POSITION;
};

// Main shader.
half4 main( VertexToFragment pInFragment) : COLOR
{
half4 lColor;

lColor.x = (mOutPosition.x + 1.0) / 2.0; // From [-1..1] To [0..1]
lColor.x = 0.0;
lColor.x = 0.0;
lColor.x = 1.0;

return lColor;
}I use those programs with a simple test where i draw a quad on the entire viewport. And normally, if i understand well, the result should be a quad with a red gradient in the X direction. I tested a lot of things, the clip coordinates of my fragments are always (0,0,0,1). I don't understand why :confused: The VP works fine but the connection between the VP and the FP seems to be wrong.

Is there someone who can give me a tip, plz ? :p

Thx.

.fetox
01-27-2005, 05:58 AM
for the first look the vertexshader seems ok, fragmentshader should not be the problem, you could try with a simple {return 1;} in fragmain to get white output.. i think you should post your cg-api-calls along with that, maybe the problem is to find there..

so long
olli

Relic
01-27-2005, 06:05 AM
Looks like a simple typo. lColor.x?

MiDeM
01-27-2005, 06:32 AM
for the lColor.x it is a copy paste problem, sorry but it is not my Cg problem :)

my app is running on Quadro FX 4000.

For Cg calls are:

Cg initialization:


gVertexProfile = CG_PROFILE_VP30;
gFragmentProfile = CG_PROFILE_FP30;

gContext = cgCreateContext();
CheckCgError();

cgGLEnableProfile(gVertexProfile);
gVertexProgram = cgCreateProgramFromFile(gContext, CG_SOURCE, CWD "vertexInterlace_v2.cg", gVertexProfile, NULL, NULL);
CheckCgError();

cgGetProgramString(gVertexProgram, CG_COMPILED_PROGRAM);

if
(gVertexProgram != NULL)
{
cgGLLoadProgram(gVertexProgram);
CheckCgError();

gModelViewProjParam = cgGetNamedParameter(gVertexProgram, "pModelViewProj");
CheckCgError();
}
cgGLDisableProfile(gVertexProfile);

cgGLEnableProfile(gFragmentProfile);
gFragmentProgram = cgCreateProgramFromFile(gContext, CG_SOURCE, CWD "fragmentInterlace_v2.cg", gFragmentProfile, NULL, NULL);
CheckCgError();

cgGetProgramString(gFragmentProgram, CG_COMPILED_PROGRAM);
if
(gFragmentProgram != NULL)
{
cgGLLoadProgram(gFragmentProgram);
CheckCgError();
}
cgGLDisableProfile(gFragmentProfile);Render loop (the viewport values are [0,0,720,576] and the ortho values are [0,720,0,576,0.1,100])

cgGLBindProgram(gVertexProgram);
cgGLBindProgram(gFragmentProgram);
cgGLSetStateMatrixParameter(gModelViewProjParam, CG_GL_MODELVIEW_PROJECTION_MATRIX, CG_GL_MATRIX_IDENTITY);
cgGLEnableProfile(gVertexProfile);
cgGLEnableProfile(gFragmentProfile);

glBegin(GL_QUADS);
glTexCoord2f(0.0f, 0.0f);
glVertex2f(0.0f, 0.0f);
glTexCoord2f(1.0f, 0.0f);
glVertex2f(720.0f, 0.0f);
glTexCoord2f(1.0f, 1.0f);
glVertex2f(720.0f, 576.0f);
glTexCoord2f(0.0f, 1.0f);
glVertex2f(0.0f, 576.0f);
glEnd();

cgGLDisableProfile(gFragmentProfile);
cgGLDisableProfile(gVertexProfile);The fragment clip coords are always [0,0,0,1]. I don't understand why ???
Any idea ???

Thx

V-man
01-27-2005, 07:39 AM
I don't know what the values in your matrices are, but that is related.
Try converting to normalized device coordinates.

MiDeM
01-27-2005, 08:29 AM
I tried to convert the clip coords to device coords in the VS and pass it to FS via an additional field (a tex coord). It works fine that's why i don't understand why the var mOutPosition is modified between the VP and the FP calls !!!
I use Cg 1.2. Can it be the problem ?

Thx