Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 2 of 2

Thread: How to implement cascade shadowmapping

  1. #1
    Junior Member Newbie
    Join Date
    May 2018
    Posts
    4

    How to implement cascade shadowmapping

    Hello,
    I work on s simple cascade shadow mapping. single shadow mapping works and for glsl version of 4.00 or higher it also works, my problem is, that my design requires that I pass the generated depth maps (one for each cascade) as an array of uniform sampler2D to the phong.fragment shader.
    the pseudo code of that shader:
    Code :
    #version 400
     
    const int NUM_CASCADES = 4;
     
    layout (location = 0) out vec3 gShadow;
     
    in VsOut
    {
      vec4 fragPosition_LightSpace[NUM_CASCADES];
      vec4 fragPosition_WorldSpace;
    } vsOut;
     
    uniform sampler2D depthMap[NUM_CASCADES];
    uniform float cascades[NUM_CASCADES]; // has the distances at which the different cascades happen
     
    float shadowCalculation(
      vec4 fragPosition_LightSpace,
      sampler2D depthMap
    )
    {
      vec3 projCoords = fragPosition_LightSpace.xyz / fragPosition_LightSpace.w;
      projCoords = projCoords * 0.5 + 0.5;
      float currentDepth = projCoords.z;
      float bias = 0.0;
      float closestDepth = texture(depthMap, projCoords.xy).r;
      float shadow = currentDepth - bias > closestDepth ? 1.0 : 0.0;
      return 1.0 - clamp(shadow, 0.0, 1.0);
    }
     
    void main()
    {
      int cascade = 0;
      for(;cascade<NUM_CASCADES; cascade++)
      {
        if(length(vsOut.fragPosition_WorldSpace) <= cascades[cascade])
        {
          break;
        }
      }
      gShadow = vec3(1.0, 1.0, 1.0)*directionalShadowCalculation(
        vsOut.fragPosition_LightSpace[cascade],
        depthMap[cascade]
      );
    }
    If i change the #version to 330 i get this error message:
    error: sampler arrays indexed with non-constant expressions are forbidden in GLSL 1.30 and later
    for this line
    Code :
    ...
    gShadow = vec3(1.0, 1.0, 1.0)*directionalShadowCalculation(
        vsOut.fragPosition_LightSpace[cascade],
        depthMap[cascade] <-!!!!!
      );
    ...

    I would prefer to be able to run my application on older hardware. How could I design cascade shadows without dynamically indexed arrays of samplers?

  2. #2
    Senior Member OpenGL Guru Dark Photon's Avatar
    Join Date
    Oct 2004
    Location
    Druidia
    Posts
    4,511
    Quote Originally Posted by tsojtsoj View Post
    How could I design cascade shadows without dynamically indexed arrays of samplers?
    Use a 2D texture array instead.

    Related reference:

    (I was going to say 2D bindless textures might be another option. However, this doesn't help you because it is written against the OpenGL/GLSL 4.00 specs and requires a GL 4.x+ card.)
    Last edited by Dark Photon; 09-30-2018 at 05:26 PM.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •