• Advertisement
Sign in to follow this  

Randomized Shadow Lookup

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

Hi guys,

 

Right now I offset my shadowmap lookups with an array of constant values. I'm not sure if that's such a good idea since every pixel has the same sampling pattern.

 

I think the way to go is to fill a texture with random offset values but I don't know how to access them that way, what should I use for the uv values ?

 

I'm doing this now:

 

static const float2 Offsets[8] =
{
  float2(0.560991f, 0.741455f),
  ...
};
 
for(int i=0;i<8;i++){
  occluder=tex2D(ShadowMap,input.proj.xy+Offsets[i]*0.001f).x;
  sum += (occluder < receiver) ? 0 : 1;
}
shadow=sum/8;

 

I'd like a better way that isn't terribly complicated. Thanks a lot.

 

 

Share this post


Link to post
Share on other sites
Advertisement

It is already the appropriate method to use static offsets.  Use a Poisson pattern for the offsets and if you want the pattern to look less obvious use more samples (more Poisson offsets).

 

 

L. Spiro

Share this post


Link to post
Share on other sites

If you have a 2D texture containing random values, then you'll want to index it based on the shadow UV coordinates you calculate for sampling the shadow map. This way the sample pattern remains stable as the camera moves. You'll want to scale the UV values by some multiplier and use WRAP sampling, this way the random pattern can tile across the entire area being affected by the shadow map. So for instance if you have a 64x64 random texture and a 512x512 shadow map, you could multiply the UV coordinates by 8.0 in order to tile it 8 times in the U and V directions.

 

Also you don't need to store the actual offsets in the texture. Instead you can store a random [0, 1] value and multiply it by 2?, and then use it as rotation value. This way you can share the same static offsets for all pixels, but have a random rotation to effectively change the sample pattern.

Share this post


Link to post
Share on other sites

Thanks a lot, I switched to a Poisson disk for now. I will try out MJP's method tonight and see what happens.

 

Is there a way I can do adaptive sampling ? For example, if the samples are along the shadow edge I can take many more samples. I tried taking four samples and testing them but it didn't really work:

 

sum=0.0f;
 
occluder=tex2D(Sampler0,input.proj.xy+float2(0,1)*0.002f).x;
sum+=(occluder < receiver) ? 0.0f : 1.0f;
 
occluder=tex2D(Sampler0,input.proj.xy+float2(1,0)*0.002f).x;
sum+=(occluder < receiver) ? 0.0f : 1.0f;
 
occluder=tex2D(Sampler0,input.proj.xy+float2(0,-1)*0.002f).x;
sum+=(occluder < receiver) ? 0.0f : 1.0f;
 
occluder=tex2D(Sampler0,input.proj.xy+float2(-1,0)*0.002f).x;
sum+=(occluder < receiver) ? 0.0f : 1.0f;
 
if(sum == 0.0f){
 // all in shadow
 shadow=0.0f;
}
else if(sum == 4.0f){
 // all out of shadow
 shadow=1.0f;
}
else{
 // on penumbra
 // take lots of samples
}

 

Thanks again.

Share this post


Link to post
Share on other sites

There typically is but you should give more details on what you are trying to do.

It looks like percentage closer soft shadowing, which includes a step to estimate the size of the penumbra.  At the risk of artifacts you can reduce the number of samples you take based on the size of the penumbra.

 

 

L. Spiro

Share this post


Link to post
Share on other sites

I'm trying to anti-alias the hard edges from my SSM setup, I don't really want soft shadows per se.

 

I average multiple samples offset by the Poisson values. I'm not sure if this is defined as PCF (I think it is),

 

I was thinking, if all the samples in the disk are in shadow there really isn't a need to samples so many, the average will be the same if I sample 2 or 200. So maybe I can do a test and take a large number of samples only where it's needed on the edges.

 

Thanks a lot for helping.

Edited by Endemoniada

Share this post


Link to post
Share on other sites

You can't really use the first few samples as an indicator of whether you need more samples. You don't know that you need more samples until you take more samples!

 

Instead you can try pre-computing information about the penumbra regions in the shadow map. One way to do this is build a low resolution texture that contains the min and max shadow map depth for an entire region of the shadow map (similar to a Hi-Z depth buffer). With this information you can know for sure that a pixel will be completely in shadow or completely out of shadow for an entire region by comparing the pixel depth to the min and mex depth values.

Share this post


Link to post
Share on other sites

MJP, that's interesting. I'm going to do some research on Hi-Z because I'm not really sure what you mean. Thanks for helping.

 

PS - I was just reading (I think your) Position From Depth 3, it's very useful.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement