• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
67rtyus

Sampler arrays in HLSL - Shader Model 3.0

6 posts in this topic

Hi; I am currently trying to implement Parallel Split Shadow Mapping. In this algorithm, for every pixel, I need to find to which frustum split the current pixel belongs and then I need to read the depth value of the pixel from this split's shadow texture. This is a situation, in which the texture to be sampled will be determined in runtime, when the pixel shader begins to execute. My question is whether is this possible in Pixel Shader Model 3.0. I have read in the DirectX SDK Documentation the following:
Quote:
Samplers may also be used in array, although no back end currently supports dynamic array access of samplers. Therefore, the following is valid because it can be resolved at compile time: tex2D(s[0],tex) However, this example is not valid. tex2D(s[a],tex)
It says that we can't use a variable as an index into a sampler array. On the other hand, on NVIDIA's site, in the GPU Gems 3 book, there is a chapter on PSSM, which gives an example how the pixel shader for the algorithm can be implemented in Direct3D 9. The link is: http://http.developer.nvidia.com/GPUGems3/gpugems3_ch10.html And the code is:
float4 PS_RenderShadows(   
  float4 texCoord[numSplits+1] : TEXCOORD): COLOR   
{   
  float light = 1.0f;   
  // Fetch view-space distance from first texture coordinate register.   
   float distance = texCoord[0].z;   
  for(int i = 0; i < numSplits; i++)   
  {   
    if(distance < splitPlane[i])   
    {   
      float depth = texCoord[i+1].z/ texCoord[i+1].w;   
      float depthSM = tex2Dproj(shadowMapSampler[i], texCoord[i+1]);   
      // Standard depth comparison   
      light = (depth < depthSM) ? 1.0f : 0.0f;   
      break;   
    }   
  }   
  return light;   
} 
numSplits is probably a shader constant here, indicating the number of different textures the shader can sample from. This code does exactly what I want to do and uses an variable as the sampler array index.What puzzles me is how this example can be valid, considering what the documentation says.How can this be possible? What is the difference between the documentation and nvidia example?
0

Share this post


Link to post
Share on other sites
It is simple the reasoning :

You CAN use variables as index for sampler array, but that variable MUST be calculable by the compiler at compile time. In this specific case :

i is the variable used that is a variable of the loop between 0 and numSplits. numSplits, I suppose is a costant defined in the shader.
So the compiler knows how many times the loop will be executed and can unroll the loop easily changing the variable with a static value as required.
0

Share this post


Link to post
Share on other sites
You you can't do proper dynamic access of textures by an array until SM40. With SM30 you can use dynamic branching, but of course that can have potential performance pitfalls.

Personally I prefer to keep all of my shadowmap cascades in a single texture, and then just calculate an offset based on the split index calculated in the shader and use that to look up the final shadowmap depth value.
0

Share this post


Link to post
Share on other sites
I recently read the same documentation page one more time and saw that just under the quote I gave in the last message:

Quote:

Samplers may also be used in array, although no back end currently supports dynamic array access of samplers. Therefore, the following is valid because it can be resolved at compile time:

tex2D(s[0],tex)

However, this example is not valid.

tex2D(s[a],tex)

Dynamic access of samplers is primarily useful for writing programs with literal loops. The following code illustrates sampler array accessing:

sampler sm[4];

float4 main(float4 tex[4] : TEXCOORD) : COLOR
{
float4 retColor = 1;

for(int i = 0; i < 4;i++)
{
retColor *= tex2D(sm[i],tex[i]);
}

return retColor;
}


I think it works like feal87 says; this code works because the compiler can unroll the loop. It knows that it will read sm[0],sm[1],sm[2] and sm[3] in that order.

But, still I have a few questions:

1-
Quote:
You you can't do proper dynamic access of textures by an array until SM40.

If the "numSplits" from the NVIDIA example were a constant, which is set by the application via SetPixelShaderConstantI function, would the code work? I think it wouldn't because the compiler couldn't unroll the for loop without knowing the value of the numSplits.

2-
Quote:
With SM30 you can use dynamic branching, but of course that can have potential performance pitfalls.
How is the dynamic branching implemented by the SM3.0? I am asking this because I have the impression, that somehow in the pixel shaders both of the branching ways are executed. For example if I have the following code:

if(value1 > variable)
{
DoActionA();
}
else
{
DoActionB();
}

In this case if both DoActionA() and DoActionB() hit the performance by 5 fps, then this piece of code results in nearly 9-10 fps of performance loss. I tested this branching issue in the pixel shaders on many occasions and the result was always the same. (By the way, my graphics card is a GeForce 8600 GS). The DirectX Documentation says the following about this:
Quote:

The most familiar branching support is dynamic branching. With dynamic branching, the comparison condition resides in a variable, which means that the comparison is done for each vertex or each pixel at run time (as opposed to the comparison occuring at compile time, or between two draw calls). The performance hit is the cost of the branch plus the cost of the instructions on the side of the branch taken. Dynamic branching is implemented in shader model 3 or higher. Optimizing shaders that work with these models is similar to optimizing code that runs on a CPU.

But I am not very convinced on that. Besides, if I remember it correctly, I have read somewhere that in shaders both of the branching paths are executed and the results of the correct path are taken thereafter. My observations seem to verify this, too. So I need to learn what is really going on in dynamic branches,especially for SM3.0.
0

Share this post


Link to post
Share on other sites
1) It would not work, because the compiler does not know at compile time what value will assume that variable. It has to be a constant value known by the compiler at compile time.
2) It depends, sometimes for the GPU is faster to execute both path as you have experienced. I don't know specifically when, but its true that sometimes it execute both path.
0

Share this post


Link to post
Share on other sites
With dynamic branching, the execution depends on what path a group of pixels have to take through the code. If every pixel in the group runs the same branch, then they can also all skip the unused branch. However, if even 1 pixel in a group needs to run the other side of a conditional branch, then all of the others will too, and just mask off the result. So here, you are relying on neighboring pixels to run the same code path in order to get speed improvements, and often this will be the case.
0

Share this post


Link to post
Share on other sites
Quote:

With dynamic branching, the execution depends on what path a group of pixels have to take through the code. If every pixel in the group runs the same branch, then they can also all skip the unused branch. However, if even 1 pixel in a group needs to run the other side of a conditional branch, then all of the others will too, and just mask off the result. So here, you are relying on neighboring pixels to run the same code path in order to get speed improvements, and often this will be the case.


The pixels are processed in groups called quads, which are consisting of 4 neighboring pixels, as far as i know. Are the groups, you have mentioned, the same with these quads?
0

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  
Followers 0