[OpenGL + CG] Ignored cgSetParameter

Started by
15 comments, last by BionicBytes 15 years, 1 month ago
tell you want - I'll write a simple test shader that takes a parameter and change it several times a begin/end block and see what it does on my environment.
I guess as long as the variable was not marked as a uniform then it should be valid to change at any time. What version of Cg are you running ? Just a thought - but there is a new Cg Version 2.1 out now.

Advertisement
Quote:Original post by BionicBytes
tell you want - I'll write a simple test shader that takes a parameter and change it several times a begin/end block and see what it does on my environment.
I guess as long as the variable was not marked as a uniform then it should be valid to change at any time. What version of Cg are you running ? Just a thought - but there is a new Cg Version 2.1 out now.


That would be great! I'm running 2.1.0016 (the November build, which I think is the latest). Thanks for the effort!
Sorry for the lateness in my reply. I've had a bad cold and couldn't face starring at a PC screen for any length of time. Anyway better now.

So - I've set up a simple shader test case.

The test case is: Can I draw two different rectangles in two different colors by changing the value of one shader parameter (t) twice whilst a single shader is bound.
If the test is successful two rect's are draw in different shades of red.

// the vp shaderstruct vp_in{	float3 Position	: POSITION;};struct vp2fp{	float4 HPos 		: POSITION;};vp2fp main(vp_in IN){	vp2fp OUT;	float4 pos=float4 (IN.Position.xyz,1.0);	OUT.HPos=mul(glstate.matrix.mvp,pos);	return OUT;} // now the fragment shaderstruct vp2fp{	float4 HPos 		: POSITION;};// Two interesting variations of the same thing. Here we have declared// a uniform float param, and in the second case param t is a function param.#if 1uniform float t;void main( vp2fp IN,out float4 color : COLOR){    	color = float4 (t,0,0,1);}#elsevoid main( vp2fp IN,in float t,out float4 color : COLOR){    	color = float4 (t,0,0,1);}#endif


Here is the main render loop where the rects are drawn.
// my lib code to get a shader referenceShader aShader = aMaterial.GetBaseShader();// Get a named Cg parameterSystem.IntPtr param = Cg.cgGetNamedParameter(aShader.GetFragmentProgram(),"t");		aMaterial.Bind(); // my lib code to grab and activate the above shader		Cg.cgSetParameter1f(param,0.8f);Gl.glRectf (10,10,100,100);Cg.cgSetParameter1f(param,0.4f);Gl.glRectf (10,110,100,210);				aMaterial.Unbind();Gl.glColor3f (1,1,1);


And the result is TWO different colored RED rectangles on the screen.

Now, if I change the shader code and make the #if 1 to a #if 0, and re-run the test the result is 2 BLACK rectangles.
So... I think in this second case the parameter t is being ignored.
But ... in the debugger the named Cg param is still valid - meaning (from what I know about Cg etc) is that the param was found in the list of enumerated Cg parameters. If the param was invalid the param value would have been 0.

I have no explanation for the t param when its specified as part of the main function.

B.T.W if i go back to the Uniform param t version, if i remove the uniform keyword, the test still works as expected and 2 red rects are displayed.

Does this help?
One other thing -- I compile my code using:

// vertex
cgc.exe -profile vp40

//frag
cgc.exe -profile fp40

I am running cg 2.0.0015 on a windows XP environment using C# and Tao framework.
I have however, also, run similar type of code in a Windows with c++ environment.
Quote:Original post by BionicBytes
Sorry for the lateness in my reply. I've had a bad cold and couldn't face starring at a PC screen for any length of time. Anyway better now.

So - I've set up a simple shader test case.

The test case is: Can I draw two different rectangles in two different colors by changing the value of one shader parameter (t) twice whilst a single shader is bound.
If the test is successful two rect's are draw in different shades of red.

*** Source Snippet Removed ***

Here is the main render loop where the rects are drawn.
*** Source Snippet Removed ***

And the result is TWO different colored RED rectangles on the screen.

Now, if I change the shader code and make the #if 1 to a #if 0, and re-run the test the result is 2 BLACK rectangles.
So... I think in this second case the parameter t is being ignored.
But ... in the debugger the named Cg param is still valid - meaning (from what I know about Cg etc) is that the param was found in the list of enumerated Cg parameters. If the param was invalid the param value would have been 0.

I have no explanation for the t param when its specified as part of the main function.

B.T.W if i go back to the Uniform param t version, if i remove the uniform keyword, the test still works as expected and 2 red rects are displayed.

Does this help?


Thanks for making the effort!

As for your first test (using 1), I get the same behaviour. However, this is because the parameter set calls are made after each rectangle is rendered. That is, I assume the glRectf methods use something similar to glBegin() and glEnd() calls. What I'm interested in is setting the parameters inside this glBegin() - glEnd() block, instead of after each block. So far, I have not been able to get this working; a I explained in my previous posts, only my last parameter set inside the glBegin() - glEnd() block is used.

Secondly, you seem to have the same problems as I do when you change your 1 to 0. I pass my parameters in the same way (using main), and get the same result as you: although the cg parameter is still valid, it appears to be set to 0, as evidenced by the two black rectangles.

So, still two questions remain:
- Is it possible to pass different values to the same cg parameter in a glBegin() - glEnd() block, and if so, how?
- How is it possible to pass values to cg parameters in a shader main function?
okay - i need to be quick because the international rugby is on.

So ... within a BEGIN/END block any call to Cg.cgSetParameter1f(param,0.8f); is invalid. Resulting in two black quads.
Placing the Cg.cgSetParameter1f(param,0.8f); outside of a BEGIN/END block is valid and results in RED rects. (of course both the same color).

So ... rule is must set Cg params outside of BEGIN/END blocks.
And for the second point - never set additional cg params as a parameter to main.
did my last post solve the issue?

This topic is closed to new replies.

Advertisement