Jump to content
  • Advertisement
Sign in to follow this  
BlueSpud

Variance Shadow Mapping Shadow Brightness Issue

This topic is 1484 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,

After attempting to implement variance shadow mapping I've run into a problem and absolutely no idea what is causing it. Here is a picture of what is going on:

uCubW.png

 

The shadow seems to get lighter the closer it is to the occluder. Its very strange and the implementations / tutorials that I've looked at don't seem to have the same problem and haven't touched on how to fix it. Of course this image isn't enough, so here is the relevant code:

 

How I create the frame buffer for the shadow map:

frameBuffer bufferToReturn;
        bufferToReturn.x = x;
        bufferToReturn.y = y;

        glGenFramebuffers(1, &bufferToReturn.bufferID);
        glBindFramebuffer(GL_FRAMEBUFFER, bufferToReturn.bufferID);

        glGenTextures(1, &bufferToReturn.texture);
        glBindTexture(GL_TEXTURE_2D, bufferToReturn.texture);
        if (shadow == false)
            glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA_FLOAT32_APPLE, x, y, 0, GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, 0);
        else glTexImage2D(GL_TEXTURE_2D, 0, GL_R32F, x, y, 0, GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, 0);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);

        glBindTexture(GL_TEXTURE_2D, 0);


        glGenTextures(1, &bufferToReturn.depth);
        glBindTexture(GL_TEXTURE_2D, bufferToReturn.depth);

        glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT32, x, y, 0, GL_DEPTH_COMPONENT, GL_FLOAT, NULL);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);

        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
        if (shadow == true)
            glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE, GL_COMPARE_R_TO_TEXTURE);

        glBindTexture(GL_TEXTURE_2D, 0);

        glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, bufferToReturn.texture, 0);
        glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, bufferToReturn.depth, 0);
        
        glBindFramebuffer(GL_FRAMEBUFFER, 0);

        return bufferToReturn;

And here is the shader code for rendering the depth. I've only rendered out the depth to attempt to be memory conservative and just compute the moments in the lighting shader.

varying vec4 position;

void main()
{

    //get the depth
    float depth = position.z/position.w;

    gl_FragColor = vec4(depth, 0.0, 0.0, 1.0);
}

Lastly the lighting code, at least the parts of it that deal with shadow.

uniform sampler2D positionMap;
uniform sampler2D normalMap;
uniform sampler2D albedoMap;
uniform sampler2D shadowMap;

varying vec2 texcoord;
uniform mat4 shadowMatrix;

float chebyshevUpperBound(vec3 coord)
{
    float depth = texture2D(shadowMap,coord.xy).r;
    vec2 moments = vec2(depth,depth*depth);

    //if we are closer than the depth map, we are lit
    if (coord.z <= moments.x)
        return 1.0;

    float variance = moments.y - (moments.x*moments.x);
    variance = max(variance,0.0005);

    float momentdistance = coord.z - moments.x;
    float p_max = variance / (variance + momentdistance*momentdistance);
	
    return p_max;
}

void main()
{
    //get all the G-buffer information
    vec3 normal = normalize(texture2D(normalMap,texcoord)).rgb;
    vec4 color = (texture2D(albedoMap,texcoord));
    vec3 position = (texture2D(positionMap,texcoord)).rgb;
    vec4 lightPosition = shadowMatrix*vec4(position,1.0);

    lightPosition /= lightPosition.w;
    float shadow = chebyshevUpperBound(lightPosition.xyz);

    gl_FragColor = vec4(((diffuselight+specular)*shadow)+ambient,color.w);

  
}

Any input is greatly appreciated and hopefully may be able to help anybody else in the future with the same problem.

Share this post


Link to post
Share on other sites
Advertisement

Whilst I have little immediate experience on the subject, we had VSM shadows implemented recently, and a similar effect was observed.
The developer who implemented speculated (in passing) that the intensity of the light source was influencing the shadow

http://blenderartists.org/forum/showthread.php?268228-quot-Variance-quot-Shadow-whats-the-point&p=2217175&viewfull=1#post2217175

Share this post


Link to post
Share on other sites

Whilst I have little immediate experience on the subject, we had VSM shadows implemented recently, and a similar effect was observed.
The developer who implemented speculated (in passing) that the intensity of the light source was influencing the shadow

http://blenderartists.org/forum/showthread.php?268228-quot-Variance-quot-Shadow-whats-the-point&p=2217175&viewfull=1#post2217175

Thanks for the link. While it was useful, I've modified the shader so the diffuse is not used and the same thing occurs, so that can't be it.

Share this post


Link to post
Share on other sites

The problem is the way variance shadow mapping determines shadow/light per pixel. It takes the difference between the (linear depth)^2 and the depth^2, and the greater the distance, the greater the shadowing term, but when they get close together (which is what happens when the shadow approaches the occluder) the shadowing term gets lower. 

 

I also noticed you are getting the shadow map depth data incorrectly. You need to store both linear and squared depth in the depth map in separate channels. Then you can sample them both instead of 'vec2(depth,depth*depth)'. This means that the depth and depth2 values will be linearly sampled and that depth * depth != depth2 for most cases. This is a key point for variance shadow mapping because it allows you to apply a blur to the shadow map and get nice smooth shadows as a result instead of the square edges you have shown. 

 

As it stands in your code, if we simplify it you get 

float variance = moments.y - (moments.x * moments.x);

which is the same as

float variance = moments.y - moments.y; // ( = 0) this is why you sample both moments.x and moments.y from a depthmap
variance = max(variance, 0.0005); // ( = 0.0005)

and finally

float p_max = variance / (variance + momentdistance*momentdistance);

Share this post


Link to post
Share on other sites

 

The problem is the way variance shadow mapping determines shadow/light per pixel. It takes the difference between the (linear depth)^2 and the depth^2, and the greater the distance, the greater the shadowing term, but when they get close together (which is what happens when the shadow approaches the occluder) the shadowing term gets lower. 

 

I also noticed you are getting the shadow map depth data incorrectly. You need to store both linear and squared depth in the depth map in separate channels. Then you can sample them both instead of 'vec2(depth,depth*depth)'. This means that the depth and depth2 values will be linearly sampled and that depth * depth != depth2 for most cases. This is a key point for variance shadow mapping because it allows you to apply a blur to the shadow map and get nice smooth shadows as a result instead of the square edges you have shown. 

 

As it stands in your code, if we simplify it you get 

float variance = moments.y - (moments.x * moments.x);

which is the same as

float variance = moments.y - moments.y; // ( = 0) this is why you sample both moments.x and moments.y from a depthmap
variance = max(variance, 0.0005); // ( = 0.0005)

and finally

float p_max = variance / (variance + momentdistance*momentdistance);

Alright, So I've changed the code and something happened, but the problem I am trying to solve is the shadowing term issue. It doesn't seem to happen on the demos that I've seen like the original paper's implementation. So the question is, how am I supported to compensate for that so all the shadows are nice and uniform.

Share this post


Link to post
Share on other sites

Okay, for the getting lighter problem, your issue is these lines:

float momentdistance = coord.z - moments.x;
float p_max = variance / (variance + momentdistance*momentdistance);

You can see that as momentdistance approaches 0, p_max will approach 1 / 1, giving you fully light pixels. One simple solution for this would be to scale momentdistance up by some set value, but, to put it simply, there will ALWAYS be this 'getting lighter' problem when using variance shadow mapping, thats part of how it determines shadowing in the first place. In most cases this wont be an issue as the object being shadowed will be thick enough that you wont notice the light section since it will be hidden by the object it self, ie. a crate. 

Share this post


Link to post
Share on other sites

there will ALWAYS be this 'getting lighter' problem when using variance shadow mapping, thats part of how it determines shadowing in the first place. In most cases this wont be an issue as the object being shadowed will be thick enough that you wont notice the light section since it will be hidden by the object it self, ie. a crate. 

 

Yeah, I think that's just the nature of the algorithm. I used to use it, but I had many thin shadow-casters, and the artifacts ended up being too noticeable to justify its use. It looks good in demos/screenshots, because the objects and such are carefully chosen to hide the artifacts :P.

Edited by phil_t

Share this post


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

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!