View more

View more

View more

### Image of the Day Submit

IOTD | Top Screenshots

### The latest, straight to your Inbox.

Subscribe to GameDev.net Direct to receive the latest updates and exclusive content.

# Randomized Shadow Lookup

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

7 replies to this topic

Posted 05 January 2014 - 08:18 PM

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++){
sum += (occluder < receiver) ? 0 : 1;
}


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

### #2L. Spiro  Members

Posted 05 January 2014 - 09:20 PM

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

### #3MJP  Moderators

Posted 06 January 2014 - 02:51 AM

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.

Posted 06 January 2014 - 03:56 PM

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
}
else if(sum == 4.0f){
// all out of shadow
}
else{
// on penumbra
// take lots of samples
}


Thanks again.

### #5L. Spiro  Members

Posted 06 January 2014 - 06:14 PM

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

Posted 06 January 2014 - 06:38 PM

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, 06 January 2014 - 09:15 PM.

### #7MJP  Moderators

Posted 06 January 2014 - 07:18 PM

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.

Posted 09 January 2014 - 12:11 AM

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.

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.