Search code examples
javaopenglglsllwjgl

Why is only the first shadow that I render at the correct position?


I am working on a Java + LWJGL Project. Currently I am trying to implement Variance Shadow Mapping, but only the first shadow map that I sample in my shader shows up at the correct position.

Fragment shader:

#version 330 core

in vec2 passTexCoords;
in vec4[4] shadowCoords;

//Fragment color
out vec4 out_Color;

uniform sampler2D modelTexture;
uniform sampler2D[4] shadowMaps;

#define SHADOW_BIAS 0.0005

float linstep(float low, float high, float v) {
    return clamp((v-low)/(high-low), 0.0, 1.0);
}

//compare ... The depth of the fragment in shadow map sapce
float sampleVarianceShadowMap(in sampler2D shadowMap, in vec2 coords, in float compare) {

    /* This is the Code that I want to use when I know what the problem was.
    vec2 moments = texture(shadowMap, coords.xy).rg;

    float p = step(compare, moments.x);
    float variance = max(moments.y - moments.x * moments.x, 0.00002);

    float d = compare - moments.x;
    float pMax = linstep(0.2, 1.0, variance / (variance + d*d));

    return min(max(p, pMax), 1.0);
    */

    //============================================================================HERE=========================================================================HERE====================
    //THE ERROR OCCURES HERE:

    //This doesn't work:
    float visibility = step(compare-SHADOW_BIAS, texture(shadowMap, coords.xy).r);  
    return visibility;

    //The shadows on the ground move in a weird way when the camera moves.

    //But this does:
    return step(compare-SHADOW_BIAS, texture(shadowMap, coords.xy).r);

    //With this code the shadows are at the correct place.

    //===========================================================================HERE==========================================================================HERE=====================
}

//To create a smooth darkness falloff at the edge of the shadow map
float calcShadowMapVisibilityFalloff(in vec2 coords, in float falloffStart, in float gradient) {
    float distFromTexCenter = length(coords * vec2(2.0) - vec2(1.0));
    float falloff = (clamp(pow(distFromTexCenter, gradient), falloffStart, 1.0) - falloffStart) * (1/(1-falloffStart));

    if(falloff > 1.0 || falloff < 0.0) {
        falloff = 0;
    }

    return 1-falloff;
}

void main(void){

    float shadowInvertedBrightness = 1.0;
    for(int i = 0; i < shadowMaps.length(); i++)
    {
        float visibility = 1 - sampleVarianceShadowMap(shadowMaps[i], shadowCoords[i].xy, shadowCoords[i].z);
        shadowInvertedBrightness -= (visibility / shadowMaps.length()) * calcShadowMapVisibilityFalloff(shadowCoords[i].xy, 0.85, 2.0);
    }

    shadowInvertedBrightness = clamp(shadowInvertedBrightness, 0.2, 1.0);

    //.bgra because I save textures with the BGRA format (I've read its faster)
    out_Color = texture(modelTexture, passTexCoords).bgra * vec4(shadowInvertedBrightness,shadowInvertedBrightness,shadowInvertedBrightness,1);
}

Vertex Shader:

#version 330 core

//Vertex coords
in vec3 position;
//Texture coords
in vec2 texCoords;

//The MVP matrix of the entity
uniform mat4 MVPMat;
//The "To Shadow Map Space" matrix
uniform mat4[4] shadowMVPBiasMats;
//The Transformation matrix of the entity
uniform mat4 transformMat;

out vec2 passTexCoords;
//Shadow map sample coords
out vec4[4] shadowCoords;

void main(void) {
    gl_Position = MVPMat * vec4(position, 1.0);

    vec4 worldPos = transformMat * vec4(position, 1.0);

    for(int i = 0; i < shadowMVPBiasMats.length(); i++) {
        shadowCoords[i] = shadowMVPBiasMats[i] * worldPos;
    }

    passTexCoords = texCoords;
}

Full Code (Example Project that you can import in Eclipse) and Screenshots:

System Information:

  • OS: Windows Home, Version: 10.0.15063
  • GPU: Intel HD Graphics 520
  • GPU Driver Version: 20.19.15.4642 (By Medion)

Solution

  • It seems to be an bug in the driver, because it doesn't happen when I run the program with my NVIDIA GPU.

    A Workaround for the problem:

    float textureShadowMap(in sampler2D shadowMap, in vec2 coords) {
        return texture(shadowMap, coords).r;
    }
    

    And in the sampleVarianceShadowMap Method:

    float visibility = step(compare-SHADOW_BIAS, textureShadowMap(shadowMap, coords));  
    return visibility;
    

    Since I am not 100% sure that it is a bug, it would be nice if someone can confirm this.