Sign in to follow this  
67rtyus

static keyword in HLSL again

Recommended Posts

Well, I have asked this kind of a question a while ago; but it was too long and I was not completely satisfied with the answers. Now again; but short and to the point this time: I have this following code in a vertex shader:
static float d=sqrt(Square_d);
static float t=(d-Di)/(Di_plus_one-Di);
static float d2=sqrt(NSquare_d);
static float t2=(d2-NDi)/(NDi_plus_one-NDi);
These values don't change per vertex, they do change per DrawIndexed call of every single mesh. I want those sqrt operations to be run on the GPU just once for every mesh. All the mesh's vertices will use the same values in the shader then. Does making them static and writing out of the Vertex Shader function do the trick? Thanks in advance

Share this post


Link to post
Share on other sites
No, not from my understanding. The static keyword will work across multiple invocations of the same function within a given shader. That is, if you call foo() twice in the same pixel shader and foo() has a static variable it will persist between the two calls. But this isn't really too exciting as, in most cases, everything is inlined anyway.

That said, it may well provide the compiler with some hints regarding preshaders which are what you really want.

If you compile your effect (note: I don't believe this works for stand-alone HLSL, only effects) using fxc.exe on the command line and output the ASM you may see a 'preshader' section at the header. This is where the compiler has pulled out common expressions that result in a constant per draw call - that is, the compiler could not flatten the expression down to an actual numerical constant but the expression is only dependent on an app-provided constant. Thus the FX framework will evaluate this shader code on the Draw**() call (when it knows what the actual value is) and flatten it down to a numerical constant and, iirc, upload that as a single constant to be used in the actual runtime code.

Double check it, but you'll probably find that your 4 statements, or parts thereof, will be made preshaders. BUT, if as your text suggests, they're dependent on a per-instance property it may not be able to do this. The above compile-and-view-ASM trick should verify it quite quickly [smile]

hth
Jack

Share this post


Link to post
Share on other sites
I am actually using standalone vertex and pixel shaders; not the effect files. I somehow dislike the way they are used, all those numOfPasses things etc. But are there such optimizations for the stand alone shaders too?

Share this post


Link to post
Share on other sites

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