Sign in to follow this  

Declaring arrays in my pixel shader

This topic is 2846 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

Hello, right now my pixel shader code is looping over an input texture and acquiring values using the texture sampler and "tex2d(...)". I have 3 loops where I loop over exactly the same coordinates, each time calling "tex2D()" to acquire the texture values, which seems like a lot of redundant work to me. Is it better to store the acquired texture values in an array instead during the first loop, and in the second and third loops get the values from the local array rather than calling "tex2D" all over again for each coordinate? If so, what are the rules governing array declarations in a pixel shader? My loops are a little large so I would need an array like: float[,] textureVals = new float[15, 15]; I'm using MDX and compiling for PS3.0. Thanks!

Share this post


Link to post
Share on other sites
In most cases the compiler will unroll loops, and I'm pretty sure that the compiler is smart enough not to insert duplicate texture fetches (at least, if it can determine that they're duplicates). However you should check your assembly to be sure.

As far as arrays, in ps_3_0 everything has to map back to fixed temporary registers. You can't access registers by index in ps_3_0, which means the compiler has to be able to map array indices to a register. This is easiest when your loops are fixed, which means the compiler can just directly map array indices to registers. If your array access is more dynamic, it may have to resort to some type of branching. Branching will also be necessary if you go over the number of temp registers (ps_3_0 guarantees 224). Again you should look at the assembly to see what's really going on. And if you're concerned about performance, then get familiar with NVShaderPerf and GPU Shader Analyzer.

Share this post


Link to post
Share on other sites

This topic is 2846 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