• entries
  • comments
  • views

Real-Time Local Reflections

Sign in to follow this  
Followers 0


I've been experimenting a little bit with Real-Time Local Reflections (RLR) (or Screen Space Reflections).
With this technique you ray trace in screen space to approximate local reflections.




As you can see my current implementation is far from perfect. And slow as hell on my laptop with a NVIDIA GT 435M sad.png (5 fps or 172 ms).
The video above was generated on a NVIDIA GTX 470 at 50-60 fps. smile.png


  • I think this technique was first shown on the Beyond3D forums by the user Graham: http://forum.beyond3...ead.php?t=56095 .
  • Crytek presentation for Siggraph 2011 "Secrets of CryENGINE 3 Graphics Technology" by Sousa, Kasyan and Schulz (slide 29 - 32).
  • This movie on YouTube claims to show the difference between Real-Time Local Reflections on and off in Crysis 2: http://www.youtube.c...?v=907vQsHofPM. (I haven't played the game myself yet, must do!)
  • Luminous Engine by Square Enix

    [subheading]My Implementation[/subheading]
    I hope the shader shown below is pretty self-explanatory smile.png
    First I calculate the reflection vector in view space. Then I transform it into screen space and I start to ray march according to the view space reflection vector until the depth in the sampled depth buffer is bigger than our current depth of our ray.

    Currently I render the scene twice (the 1[sup]st[/sup] time without the reflections, the 2[sup]nd[/sup] time with). In this way I can use the depth buffer of the 1[sup]st[/sup] pass for my reflections in the 2[sup]nd[/sup] one. But of course with a deferred renderer you can use the depth buffer from your G-buffer and sample the reflected pixel color from the previous frame.

    // Calculate View Space Reflection Vector!
    float3 vspReflect = reflect(normalize(Input.ViewPos), normalize(Input.ViewNormal));

    // Normalize, in this way we only need to check the .z component
    // to know how hard the reflection vector is facing the viewer
    vspReflect = normalize(vspReflect);

    // If the view space reflection vector is facing to hard to the viewer
    // then there is a high chance there is no available data!
    if (vspReflect.z > g_rlrOptions.y)

    // We want to smoothly fade out the reflection when facing the viewer.
    // Calculate this factor ...
    float rcpfadefact = rcp(1.0 - g_rlrOptions.y /* minimum .z value of reflection */ );
    float faceviewerfactor = (vspReflect.z - g_rlrOptions.y) * rcpfadefact;

    // Transform the View Space Reflection to Screen Space
    // This because we want to ray march in to the depth buffer in Screen Space (thus you can use the default hardware depthbuffer)
    // Depth is linear in Screen Space per Screen Pixel
    float3 vspPosReflect = Input.ViewPos + vspReflect;
    float3 sspPosReflect = mul(float4(vspPosReflect, 1.0), g_mProj).xyz / vspPosReflect.z;
    float3 sspReflect = sspPosReflect - Input.ScreenPos;

    // Resize Screen Space Reflection to an appropriate length.
    // We want to catch each pixel of the screen
    float scalefactor =
    g_rlrOptions2.y /* size of 1 pixel in screen space (I took this in the width (2/1280) because the width is almost always bigger than the height */
    / length(sspReflect.xy);
    scalefactor *= g_rlrOptions.x /* how many pixels at once (value = 1) */;
    sspReflect *= scalefactor;

    // Initial offsets
    // .xy for Screen Space is in the range of -1 to 1. But we want to sample from
    // a texture, thus we want to convert this to 0 to 1.
    float3 vCurrOffset = Input.ScreenPos + sspReflect ;
    vCurrOffset.xy = float2(vCurrOffset.x * 0.5 + 0.5,vCurrOffset.y * -0.5 + 0.5);
    float3 vLastOffset = Input.ScreenPos;
    vLastOffset.xy = float2(vLastOffset.x * 0.5 + 0.5,vLastOffset.y * -0.5 + 0.5);
    sspReflect = float3(sspReflect.x * 0.5 ,sspReflect.y * -0.5, sspReflect.z);

    // Number of samples
    int nNumSamples = (int)(g_rlrOptions2.x /* width of backbuffer (e.g. 1280) */ / g_rlrOptions.x) /* how many pixels at once (usualy 1) */;
    int nCurrSample = 0;
    // Calculate the number of samples to the edge! (min and maximum are 0 to 1)
    #ifndef DEBUGRLR
    float3 samplestoedge = ((sign(sspReflect.xyz) * 0.5 + 0.5) - vCurrOffset.xyz) / sspReflect.xyz;
    samplestoedge.x = min(samplestoedge.x, min(samplestoedge.y, samplestoedge.z));
    nNumSamples = min(nNumSamples, (int)samplestoedge.x);
    float3 vFinalResult;
    float vCurrSample;

    float2 dx, dy;
    dx = ddx( vCurrOffset.xy );
    dy = ddy( vCurrOffset.xy );
    while (nCurrSample < nNumSamples)

    // Sample from depth buffer
    vCurrSample = txPrevFrameDepth.SampleGrad(g_samParaboloid, vCurrOffset.xy, dx, dy).x;
    if (vCurrSample < vCurrOffset.z)

    // Calculate final offset
    vLastOffset.xy = vLastOffset.xy + (vCurrSample - vLastOffset.z) * sspReflect.xy;

    // Get Color
    vFinalResult = txPrevFrameDiffuse.SampleGrad(g_samParaboloid, vLastOffset.xy, dx, dy).xyz;

    const float blendfact = 0.6;
    float2 factors = float2(blendfact, blendfact);
    // Fade to viewer factor
    factors.x = (1.0 - faceviewerfactor);
    #ifdef FADETOEDGES
    // Fade out reflection samples at screen edges
    float screendedgefact = saturate(distance(vLastOffset.xy , float2(0.5, 0.5)) * 2.0);
    factors.y = screendedgefact;

    // Blend
    fvTotalDiffuse.xyz = lerp(vFinalResult, fvTotalDiffuse, max(max(factors.x /* linear curve */, factors.y * factors.y /* x^2 curve */), blendfact));
    nCurrSample = nNumSamples + 1;

    vLastOffset = vCurrOffset;
    vCurrOffset += sspReflect;

    #ifdef DEBUGRLR
    // Debugging....
    if ((vCurrOffset.z < 0.0) || (vCurrOffset.z > 1.0) )
    // Debug: Show blue color
    vFinalResult = float3(0.0, 0.0 ,1.0);
    fvTotalDiffuse = float3(0.0, 0.0, 1.0);
    nCurrSample = nNumSamples + 1;

    else if ( (vCurrOffset.x < 0.0) || (vCurrOffset.x > 1.0) || (vCurrOffset.y < 0.0) || (vCurrOffset.y > 1.0))
    // Debug: Show red color
    fvTotalDiffuse = float3(1.0, 0.0, 0.0);
    nCurrSample = nNumSamples + 1;


    As you can see my implementation contains 2 techniques, as mentioned in the Crytek presentation, in order to hide broken reflections.

    • Smoothly fade out if the reflection vector faces viewer as no data is available
    • Smoothly fade out reflection samples at screen edges

      They also mention that they add jitering tot hide noticeable step artifacts. I did not implement this.

      [subheading]Fade out when reflection vector faces viewer[/subheading]


      [subheading]Fade out when reflection samples reach screen border[/subheading]


      [subheading]Remaining problems[/subheading]
      Currently the biggest problem that I still have is for the areas where there is no information (see screen shot below).
      I'm thinking to experiment with comparing the depth of the neighboring pixels of the reflection intersection point in screen space.
      If the difference is too big, fade away or something like that ... not sure yet. But that will be for a next blog post.


      So tips, comments and ideas are very welcome!

      You can download the executable of the test project here:RealTimeLocalReflections_LitheonJan2012.rar. (z=forward, s=backward, q=left, d=right (i'll adapt this later for qwerty))
      But you will need a DirectX 11 video card (I've got feature level 11 enabled).

Sign in to follow this  
Followers 0


[color=#282828][font=helvetica, arial, verdana, tahoma, sans-serif][left]"Then I transform it into screen space and I start to ray march according to the view space reflection vector until the depth in the sampled depth buffer is bigger than our current depth of our ray"[/left][/font][/color]

[left][font="helvetica, arial, verdana, tahoma, sans-serif"][color="#282828"]Is stopping at (viewport.z > sample ray.z) or (when sample ray is no longer occluded) necessarily correct?[/color][/font][/left]

[color=#282828][font=helvetica, arial, verdana, tahoma, sans-serif][left]Imagine 3 spheres [/left][/font][/color]
1 2 3
[color=#282828][font=helvetica, arial, verdana, tahoma, sans-serif][left] o o o[/left][/font][/color]
[color=#282828][font=helvetica, arial, verdana, tahoma, sans-serif][left]z=4 z = 2 z = 3[/left][/font][/color]

[color=#282828][font=helvetica, arial, verdana, tahoma, sans-serif][left]And the reflection ray starts at 3 and goes towards 1. You will hit a pixel occupied by 2... and callit quits. [/left][/font][/color][color=#282828][font=helvetica, arial, verdana, tahoma, sans-serif][left]I currently[/left][/font][/color]

Share this comment

Link to comment

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