Sampler arrays in HLSL - Shader Model 3.0

Started by
5 comments, last by 67rtyus 14 years, 10 months ago
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)   
    {   
      float depth = texCoord[i+1].z/ texCoord[i+1].w;   
      float depthSM = tex2Dproj(shadowMapSampler, 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?
Advertisement
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.
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.
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,tex);
}

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.
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.
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.
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?

This topic is closed to new replies.

Advertisement