Sign in to follow this  

Using Global Variables in .fx files

This topic is 4862 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I have a real simple vertex shader to test out an idea. I'm looking to modify the color of every other vertex in a stream. Using the effect framework, is it legal to have a "global" variable that I just increment, then mod it by two (looking for odd & even results). When stepping through the shader with the debugger visual studio shows that the shader is always reset to this.. "fCounter * (no storage) float" Do I need to forc the "fCounter" variable to be stored in a specific register or something to make it persist throughout the processing of the entire vertex stream? Thanks in advance, ~Rob
float fCounter = 0;
float4x4 matWorldViewProj : WORLDVIEWPROJ;

struct VS_OUTPUT
{
	float4 Pos : POSITION;
	float4 Color : COLOR0;
};

VS_OUTPUT VS(float3 vPosition : POSITION)
{
    VS_OUTPUT Output = (VS_OUTPUT)0;    
    fCounter += 1;
    if((fCounter % 2) == 0)	
    {
	Output.Color.r = 1.0f;
	Output.Color.g = 1.0f;
	Output.Color.b = 1.0f;
	Output.Color.a = 1.0f;
    }
    else
    {
	Output.Color.r = 1.0f;
	Output.Color.g = 0.0f;
	Output.Color.b = 0.0f;
	Output.Color.a = 1.0f;
    }
    return Output;
}

Share this post


Link to post
Share on other sites
I believe that you have to declare the variable as 'static' and you may have to declare it inside the function beacuse "Global variables are treated as if they are declared uniform." (DX9 docs) and all your functions may see the same inital value for variables declared as uniform (the wording in the docs is a little strange on this).


Be aware that static variables are not accessable by the surrounding application.

hope that this helps.

Share this post


Link to post
Share on other sites
Thanks for the tip. Unfortunately I tried using both "global" and local static variables and they don't seem to hold state across each iteration of the shader.

Each time I walk through the debugger the variable always seems to be set back to its default state of "*".

Does anyone else have any way they've been using a temp variable that persists for the duration of a single stream getting passed through a shader?

Thanks again for any help,
~Rob

Share this post


Link to post
Share on other sites
Since graphics cards has more than one vertex/pixel pipeline and they are executing shaders parallel it is not possible to write any constant space register, so your idea is absolutely unfeasible. From a vertex shader you can write only the output registers. Part of them will be passed to the pixel shader, the others are used for special purposes (position, fog, etc)

Share this post


Link to post
Share on other sites
OK, I have some other ideas on how to accomplish what I'm doing, but I figured I'd check for the simplest solution first.

My real goal is to get a shader that can blur a starfield when the camera moves (like Freespace & Tachyon did). My previous approach was going to be to move every other vertex in a line-list, based on the camera velocity. Since I can't write to a temporary register, I'll put an even/odd flag into my vertex stream that the shader can access. Blending weights may also be a solution, but it seems overkill for something so simple. Let me go give that a whirl now....

~Rob

Share this post


Link to post
Share on other sites
Well, I ended up throwing in odd/even flags into the first texture coordinate register (since my starfield doesn't use that register). Seems to work well enough!

Thanks for the help,
~Rob

Share this post


Link to post
Share on other sites

This topic is 4862 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this