Sign in to follow this  

OpenGL SAVSM Numeric Instability Problems

Recommended Posts

Oni    122
[color="#333333"]Hi guys. Im having issue with loss of precision in my SAVSM setup.

when you see the light moving around the effect is very striking; there is a lot of noise with fragments going black and white all the time. This can be somewhat lessened by using the minvariance (thus ignoring anything below a certain threshold) but then we get even worse effects with incorrect falloff (see my other post).

Im using GLSL 1.2 because I'm on a mac so I dont have access to the modf function in order to split the precision across two channels as described in GPU Gems 3 Chapter 8.

Im using GL_RGBA32F_ARB textures with a Framebuffer object and ping ponging two textures to generate a summed area table which i use with the VSM algorithm.

Shadow Shader.

The Summed tables do seem to be working. I know this because I have a function that converts back from the summed table to the original depth map and the two images do look pretty much the same. Im also using the -0.5 + 0.5 trick in order to get some more precision but it doesnt seem to be helping [img][/img][/color]

uniform sampler2D ShadowMap;

uniform float minVariance;

uniform float ambientLevel;

uniform float lightAttenuation;

uniform int shadowMapSize;

uniform float filterSize;

varying vec4 ShadowCoord;

uniform vec4 lightPosition;

varying vec4 lightDir, eyeVec;

varying vec3 vertexNormal;

varying vec3 vertexNormalWorld;

vec3 ShadowCoordPostW;

float step = 1.0 / float(shadowMapSize);

float g_DistributeFactor = 1024.0;

// This is somewhat specific to the sub objects within our QC File so might need to be changed

// TODO - Minus light dir do we think?

float lightLevel() {

float d = ShadowCoordPostW.z - lightPosition.z;

float attenuation = 1.0 / (d * d * lightAttenuation);

return attenuation * max(dot(vertexNormalWorld,, 0.0);


float linstep(float min, float max, float v)


return clamp((v - min) / (max - min), 0.0, 1.0);


float ReduceLightBleeding(float p_max, float Amount)


// Remove the [0, Amount] tail and linearly rescale (Amount, 1].

return linstep(Amount, 1.0, p_max);


// Box Sample Blur - WITH SUMMED TABLES!

vec4 btex2D(vec2 uv) {

float ss = step * filterSize / 2.0;

float xmax = uv.x - ss;

float xmin = uv.x + ss;

float ymax = uv.y - ss;

float ymin = uv.y + ss;

vec4 total = texture2D(ShadowMap, vec2(xmax,ymax)) - texture2D(ShadowMap, vec2(xmax,ymin))

- texture2D(ShadowMap, vec2(xmin,ymax)) + texture2D(ShadowMap, vec2(xmin,ymin));

return total / (filterSize * filterSize);


// Upper Bound VSM Shadow code

float chebyshevUpperBound()


// We retrive the two moments previously stored (depth and depth*depth)

// these are split over r,g and b,a

vec4 moments = btex2D(ShadowCoordPostW.xy);

// float FactorInv = 1.0 / g_DistributeFactor;

// vec4 moments = vec4(splits.y * FactorInv + splits.x, splits.w * FactorInv + splits.z, 0.0,0.0);

//vec4 moments = vec4(splits.x * FactorInv + splits.y, splits.z * FactorInv + splits.w,0.0,0.0);

//moments.x += 0.5; // Since using SUMMED Tables we need to adjust

//moments.y += 0.5;

//vec2 moments = texture2D(ShadowMap,ShadowCoordPostW.xy).rg;

// Surface is fully lit. as the current fragment is before the light occluder

// Hardly ever occurs because the distances will always be greater or very close to equal

if (ShadowCoordPostW.z <= moments.x)

return 1.0 ;

// The fragment is either in shadow or penumbra. We now use chebyshev's upperBound to check

// How likely this pixel is to be lit (p_max)

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

variance = max(variance, minVariance);

float d = ShadowCoordPostW.z - moments.x ;

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

return p_max;

//return max (1.0 - p_max, 0.0);


void main()


ShadowCoordPostW = 0.5 * ( / ShadowCoord.w + 1.0);

float shadow = ReduceLightBleeding(chebyshevUpperBound(),0.15);

float litFactor = (1.0 - ambientLevel) * (shadow) * lightLevel();

//gl_FragColor = gl_Color * smoothstep(ambientLevel,1.0,max(lightLevel(),shadow));

gl_FragColor = gl_Color * (litFactor + ambientLevel);




varying vec4 v_position;

varying float tDepth;

float g_DistributeFactor = 1024.0;

void main()


// Is this linear depth? I would say yes but one can't be utterly sure.

// Could try a divide by the far plane?

float depth = v_position.z / v_position.w ;

depth = depth * 0.5 + 0.5; //Don't forget to move away from unit cube ([-1,1]) to [0,1] coordinate system

vec2 moments = vec2(depth, depth * depth);

// Adjusting moments (this is sort of bias per pixel) using derivative

float dx = dFdx(depth);

float dy = dFdy(depth);

moments.y += 0.25 * (dx*dx+dy*dy);

// Subtract 0.5 off now so we can get this into our summed area table calc

//moments -= 0.5;

// Split the moments into rg and ba for EVEN MORE PRECISION

// float FactorInv = 1.0 / g_DistributeFactor;

// gl_FragColor = vec4(floor(moments.x) * FactorInv, fract(moments.x ) * g_DistributeFactor,

// floor(moments.y) * FactorInv, fract(moments.y) * g_DistributeFactor);

gl_FragColor = vec4(moments,0.0,0.0);



[color="#333333"]So close and yet so far! Argh! >< [/color]

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this  

  • Similar Content

    • By pseudomarvin
      I assumed that if a shader is computationally expensive then the execution is just slower. But running the following GLSL FS instead just crashes
      void main() { float x = 0; float y = 0; int sum = 0; for (float x = 0; x < 10; x += 0.00005) { for (float y = 0; y < 10; y += 0.00005) { sum++; } } fragColor = vec4(1, 1, 1 , 1.0); } with unhandled exception in nvoglv32.dll. Are there any hard limits on the number of steps/time that a shader can take before it is shut down? I was thinking about implementing some time intensive computation in shaders where it would take on the order of seconds to compute a frame, is that possible? Thanks.
    • By Arulbabu Donbosco
      There are studios selling applications which is just copying any 3Dgraphic content and regenerating into another new window. especially for CAVE Virtual reality experience. so that the user opens REvite or CAD or any other 3D applications and opens a model. then when the user selects the rendered window the VR application copies the 3D model information from the OpenGL window. 
      I got the clue that the VR application replaces the windows opengl32.dll file. how this is possible ... how can we copy the 3d content from the current OpenGL window.
      anyone, please help me .. how to go further... to create an application like VR CAVE. 
    • By cebugdev
      hi all,

      i am trying to build an OpenGL 2D GUI system, (yeah yeah, i know i should not be re inventing the wheel, but this is for educational and some other purpose only),
      i have built GUI system before using 2D systems such as that of HTML/JS canvas, but in 2D system, i can directly match a mouse coordinates to the actual graphic coordinates with additional computation for screen size/ratio/scale ofcourse.
      now i want to port it to OpenGL, i know that to render a 2D object in OpenGL we specify coordiantes in Clip space or use the orthographic projection, now heres what i need help about.
      1. what is the right way of rendering the GUI? is it thru drawing in clip space or switching to ortho projection?
      2. from screen coordinates (top left is 0,0 nd bottom right is width height), how can i map the mouse coordinates to OpenGL 2D so that mouse events such as button click works? In consideration ofcourse to the current screen/size dimension.
      3. when let say if the screen size/dimension is different, how to handle this? in my previous javascript 2D engine using canvas, i just have my working coordinates and then just perform the bitblk or copying my working canvas to screen canvas and scale the mouse coordinates from there, in OpenGL how to work on a multiple screen sizes (more like an OpenGL ES question).
      lastly, if you guys know any books, resources, links or tutorials that handle or discuss this, i found one with marekknows opengl game engine website but its not free,
      Just let me know. Did not have any luck finding resource in google for writing our own OpenGL GUI framework.
      IF there are no any available online, just let me know, what things do i need to look into for OpenGL and i will study them one by one to make it work.
      thank you, and looking forward to positive replies.
    • By fllwr0491
      I have a few beginner questions about tesselation that I really have no clue.
      The opengl wiki doesn't seem to talk anything about the details.
      What is the relationship between TCS layout out and TES layout in?
      How does the tesselator know how control points are organized?
          e.g. If TES input requests triangles, but TCS can output N vertices.
             What happens in this case?
      In this article,
      the isoline example TCS out=4, but TES in=isoline.
      And gl_TessCoord is only a single one.
      So which ones are the control points?
      How are tesselator building primitives?
    • By Orella
      I've been developing a 2D Engine using SFML + ImGui.
      Here you can see an image
      The editor is rendered using ImGui and the scene window is a sf::RenderTexture where I draw the GameObjects and then is converted to ImGui::Image to render it in the editor.
      Now I need to create a 3D Engine during this year in my Bachelor Degree but using SDL2 + ImGui and I want to recreate what I did with the 2D Engine. 
      I've managed to render the editor like I did in the 2D Engine using this example that comes with ImGui. 
      3D Editor preview
      But I don't know how to create an equivalent of sf::RenderTexture in SDL2, so I can draw the 3D scene there and convert it to ImGui::Image to show it in the editor.
      If you can provide code will be better. And if you want me to provide any specific code tell me.
  • Popular Now