Questions on communication w/ GPU programs

monopolar

18-05-2007 20:56:47

I'm presently using the newly bound Compositor support in Python-Ogre (1.0 RC1) to post process rendering for a specialized display. So far I have been successful with regard to writing my own Cg programs and invoking them within my code. I've even been able to add static textures via the material script.

My question is, how do I send a simple integer argument to a Cg fragment shader program? My present approach has been to use the GpuProgramParameters.setNamedConstant( ) method in the same fashion that it is applied to materials. While I am aware that the functions that accept lists have not been bound yet, I'm just passing an integer value (a frame count) at this point.

I've setup the cg fragment shader program (the value passed is frameNum and is presently just generating some vertical black lines for debug):


sampler RT : register(s0);
sampler Rand: register(s1);
sampler Lines: register(s2);

float4 GrayScale_ps(float2 pos: TEXCOORD1,
float2 iTexCoord : TEXCOORD0,
uniform int frameNum: register(c2)) : COLOR
{
float3 truncval=tex2D(RT, iTexCoord).rgb;
float3 thresh=tex2D(Rand, iTexCoord).rgb;
int posx=int((pos.x+1)*512);
int posy=int((pos.y+1)*512);
truncval=((frameNum+posx)%32!=0)?truncval:float3(0,0,0);
truncval=truncval+thresh;
truncval=truncval*(4,8,8);
truncval=floor(truncval);
truncval=truncval/float3(4,8,8);
float3 grayscale=dot(truncval,float3(16,8,1))/16;
return float4(truncval, 1.0);
}



The corresponding .material file:
fragment_program Ogre/Compositor/ArrDither cg
{
source ArrDither.cg
entry_point GrayScale_ps
profiles ps_2_0 fp30 arbfp1
}

material Ogre/Compositor/ArrDither
{
technique
{

pass
{
depth_check off

vertex_program_ref Ogre/Compositor/StdQuad_Tex2a_vp
{
}

fragment_program_ref Ogre/Compositor/ArrDither
{
param_named_auto frameNum frameNum 0
}

texture_unit 0
{
tex_coord_set 0
tex_address_mode clamp
filtering linear linear linear
}
texture_unit 1
{
texture threshtex2.tga 2d
tex_coord_set 1
tex_address_mode wrap
filtering linear linear linear
}
texture_unit 2
{
texture linetex.tga 2d
tex_coord_set 1
tex_address_mode wrap
filtering linear linear linear
}

}
}
}


And the corresponding code snippet in Examples.compositor

compositor ArrDither
{
technique
{
texture rt0 target_width target_height PF_R8G8B8
// render scene to a texture
target rt0 { input previous }

target_output
{
// Start with clear output
input none

pass render_quad
{
material Ogre/Compositor/ArrDither
input 0 rt0
}
}
}
}


Works like a charm with the constant value of frameNum

Then I try to increment the value of frameNum within my frame listener (self.cmsOn is a flag which makes sure that the compositor has been invoked and is on):

if self.cmsOn:
mat=(ogre.MaterialManager.getSingleton().getByName('Ogre/Compositor/ArrDither'))
_pass=mat.getTechnique(0).getPass(0)
params =_pass.getFragmentProgramParameters()
params.setNamedConstant("frameNum", self.frameCount)

It's presently bombing on the final line with a runtime error of "unidentifiable C++ exception"

bharling

19-05-2007 09:04:17

does changing it to a string work? as in:

params.setNamedConstant("frameNum", str(self.frameCount))

monopolar

20-05-2007 01:29:20

Sadly, no.
It generates an Improper Argument Type
Explicitly casting the value to int or uint doesn't work (it results in a Unspecified C++ Exception error).

Now here is where it gets weird...
If I explicitly cast it to float in the python program it doesn't bomb out but it doesn't work either (i.e. the vertical lines don't move). But when I then change the argument in the CG code to float it bombs out with another Unspecified C++ exception!

andy

20-05-2007 08:37:35

Can you post a complete demo/test program so I can have a look at the problem -- you can create a ticket on python-ogre and attach the necessary files to the ticket..

Cheers

Andy

andy

20-05-2007 08:50:01

I've looked at the code and setNamedConstant is overridden multiple times (it can take vectors, martix, int, or real as an argument).. There may be a bug where the override is picking the wrong function..

Perhaps you can try..
params.setNamedConstant(name="frameNum", val=self.frameCount)
or
params.setNamedConstant(name="frameNum", val=int(self.frameCount))

Andy

monopolar

20-05-2007 23:48:52

Both approaches result in the same error Unnamed CNC exception
Will post ticket
Hit the forums first since I feared that the problem was my limited knowledge of the Ins and outs of Ogre.

monopolar

21-05-2007 17:36:04

Posted a formal ticket.

andy

25-05-2007 08:01:21

I've put a potential fix in the SVN (will be part of RC2) but it needs a test program (I don't have one that uses this functionality -- yet )

Basically you can do:
xxx.setNamedConstant(<name>, real=<value>)
xxx.setNamedConstant(<name>, int=<value>)

If this doesn't seem reasonable I can/may rename the functions to setNamedConstantReal/setNamedConstantInt -- also once I have a test program I'll look to solve the problem in a more "transparent" fashion.

Cheers
Andy

andy

27-05-2007 14:11:56

The SVN has been updated with a new demo (Demo_Compositor) that shows how to program the GPU...

It does require a rebuild from the SVN (and will be in the next snapshot)

Cheers

Andy

monopolar

28-05-2007 00:55:32

I'll probably go for the next snapshot. Will test ASAP.
Many thanks!

monopolar

19-06-2007 02:24:30

I've installed the RC2 release and having a little trouble understanding the use of the ctypes methods in setNamedConstantFloat and setNamedConstantInt. In essence if I could just see how the time_0_X value is being passed to OldTV.cg in Demo_Compositor.py (or more likely CompositorDemo_FrameListener.py) I'd achieve enlightenment!

monopolar

19-06-2007 22:35:25

Just to clarify my code so far (in case I'm making a stupid error) w/ RC2
Here is the cg compositor code:

sampler RT : register(s0);
sampler Rand: register(s1);
sampler Lines: register(s2);

float4 GrayScale_ps(float2 pos: TEXCOORD1,
float2 iTexCoord : TEXCOORD0,
uniform float FrameNum:register(c2)) : COLOR
{
float3 truncval=tex2D(RT, iTexCoord).rgb;
float3 thresh=tex2D(Rand, iTexCoord).rgb;
int posx=int((pos.x+1)*512);
int posy=int((pos.y+1)*512);
truncval=((int(FrameNum)+posx)%32!=0)?truncval:float3(0,0,0);
truncval=truncval+thresh;
truncval=truncval*float3(7,7,3);
truncval=floor(truncval);
float3 grayscale=dot(truncval,float3(32,4,1))/255;
truncval=truncval/float3(7,7,3);
return float4(truncval, 1.0);
}


Here is the .material script

fragment_program Ogre/Compositor/ArrDither cg
{
source ArrDither.cg
entry_point GrayScale_ps
profiles ps_2_0 fp30 arbfp1
}

material Ogre/Compositor/ArrDither
{
technique
{

pass
{
depth_check off

vertex_program_ref Ogre/Compositor/StdQuad_Tex2a_vp
{
}

fragment_program_ref Ogre/Compositor/ArrDither
{
param_named_auto FrameNum float 0
}

texture_unit 0
{
tex_coord_set 0
tex_address_mode clamp
filtering linear linear linear
}
texture_unit 1
{
texture threshtex2.tga 2d
tex_coord_set 1
tex_address_mode wrap
filtering linear linear linear
}
texture_unit 2
{
texture linetex.tga 2d
tex_coord_set 1
tex_address_mode wrap
filtering linear linear linear
}

}
}
}

Here is the portion of the examples.compositor script:

compositor ArrDither
{
technique
{
texture rt0 target_width target_height PF_R8G8B8

// render scene to a texture
target rt0 { input previous }

target_output
{
// Start with clear output
input none

pass render_quad
{
// convert the previous render target to a black and white image, add some noise, distort it,
// then render to scene aligned quad
material Ogre/Compositor/ArrDither
input 0 rt0
}
}
}
}

and here is the portion of the frame listener

def frameStarted(self, evt):
if self.cmsOn:
mat=(ogre.MaterialManager.getSingleton().getByName('Ogre/Compositor/ArrDither'))
_pass=mat.getTechnique(0).getPass(0)
params =_pass.getFragmentProgramParameters()
params.setNamedConstant(name="FrameNum",real=float(self.frameCount))
if (self.frameCount%97)==0:
print "***-Frame Count=",self.frameCount
self.frameCount+=1
...

The compositor "works" fine except that the vertical black bars remain stationary rather than move as would be the case with FrameNum incrementing properly. The print statement works fine and indicates that self.frameCount is incrementing and that setNamedConstant is being called.

bharling

20-06-2007 15:10:15

I think this is exactly the same problem I'm having with the sky manager in the tech demo, therefore I'll watch this thread with interest.. :shock:

andy

21-06-2007 05:22:48

It's next on the list to look at..

Currently fixing some property "setters" such as 'vertexData' where we were not exposing the 'setter' capability.

Cheers
Andy

monopolar

21-06-2007 19:01:16

Your efforts are most appreciated!

bharling

22-06-2007 10:34:07

I posted this question on the skymanager thread in the showcase forum, this from sinbad:

And check the ogre.log for the compile error.

I accidentally updated the Dx SDK to April 2007 in 1.4.2 and the HLSL compiler has dropped support for SM1 shaders. Change them to Cg to resolve this which still supports SM1. It's just a case of changing the language code to cg and 'target' to 'profiles' (and you can add a GL profile in there if you like)


I wish I knew how to do this :)

andy

23-06-2007 04:04:35

I'm soooo glad to hear this as I couldn't see anything wrong with the Python-Ogre code.. And I also used the latest DX SDK when I built the binary release...

It looks like you need to play with sky.material to resolve the issue..

Cheers
Andy

bharling

23-06-2007 11:54:36

Yes indeedy!

Am going to have a crack at this today. Can anyone point me to a nice simple Cg introductory tutorial (geared towards idiots :wink: )

monopolar

25-06-2007 18:31:58

I'm already using Cg w/ OpenGL. So that isn't the problem.

check my post of Tue Jun 19 on this thread for details.

That said, I strongly suspect it is some form of stupidity on my part. I'm just having a hard time understanding the use of ctypes in the passing of parameters in the SetNamedConstant call. Any tips would be most appreciated.

dermont

26-06-2007 17:52:48

Try checking the log for errors or maybe (?) updating the profiles in "fragment_program Ogre/Compositor/ArrDither cg":

// profiles ps_2_0 fp30 arbfp1
profiles ps_2_0 arbfp1 fp30