Page 1 of 1

Trouble with uniform parameters in fragment programs

Posted: Wed Feb 02, 2005 11:04 pm
by Rhone
For some reason I cannot get any of my CG fragment programs to accept uniform parameters that are not of type "float4". For instance consider the following material script excerpt:

Code: Select all

fragment_program_ref main_fp
{
   param_named constColor float4 1 0 0 1
}
with accompanying CG code for the frament program:

Code: Select all

void main_fp(out float4 oColor : COLOR,
                    uniform float4 constColor)
{
   oColor = constColor;
}
This code operates correctly and displays the object as solid red. Now consider the following material excerpt and fragment program. These are intended to accomplish the same effect only using float3's instead of float4's:

Code: Select all

fragment_program_ref main_fp
{
   param_named constColor float3 1 0 0
}
and the fragment program...

Code: Select all

void main_fp(out float4 oColor : COLOR,
                    uniform float3 constColor)
{
   oColor.xyz = constColor;
   oColor.w = 1;
}
Unlike the first example, this second method does not work at all. The object is displayed as solid black indicating that the GPU did not recieve the correct values for "constColor".

Furthermore the same odd effect occurs when passing the fragment program a "float2"--the data is all zeroed out. Even worse, if you pass the fragment program a simple "float", the application will crash somewhere inside the CG parser. Does anyone know what is going on here?

Thanks!

Posted: Thu Feb 03, 2005 12:09 am
by sinbad
In previous versions you could only specify parameters in multiples of 4. In fact, that's what the Cg compiler does anyway - if you specify a float2 uniform parameter, you are actually getting a float4 parameter where you only use the first 2 entries.

This was documented in the manual in those versions.

It was changed in 0.15.1 or 0.15.2 (can't remember which) to support arbitrary numbers of floats, although like Cg the odd numbers are rounded so that you're still actually using float4. Therefore doing this:

Code: Select all

uniform float param1;
uniform float param2;
uniform float param3;
is actually no different to doing this:

Code: Select all

uniform float4 param1;
uniform float4 param2;
uniform float4 param3;
Check the assembler that the Cg compiler produces if you don't believe me ;) Instead you should try to pack your parameters into shared float4 params wherever possible.

Posted: Thu Feb 03, 2005 3:10 pm
by Rhone
Thanks, I'm still using an earlier version of Ogre, I guess that is the problem.