• entries
    3
  • comments
    3
  • views
    36056

Entries in this blog

Litheon
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.


gallery_63508_366_112019.png

[media]

[/media]



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

[subheading]Resources[/subheading]

  • 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!
    #ifdef FADETOVIEWER
    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;
    #endif

    // 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);
    #endif
    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);
    #ifdef FADETOVIEWER
    // Fade to viewer factor
    factors.x = (1.0 - faceviewerfactor);
    #endif
    #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;
    #endif

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

    }
    else
    {
    ++nCurrSample;
    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;
    }
    #endif
    }

    #ifdef FADETOVIEWER
    }
    else
    {
    }
    #endif


    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]

      gallery_63508_366_126881.png




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

      gallery_63508_366_95789.png



      [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.

      gallery_63508_366_46272.png



      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).
Litheon
I think we are all on this website because we are in a constant urge for knowledge.
And then it is possible that in our journey we encounter this:

pixhack_2.png



This happens if you use PIX on an application that uses the D3DPERF_SetOptions(1) function to disable profiling/analysis tools.

An easy way to circumvent this problem is to edit the binary of the application.
Then the only thing we need to change in the binary is the argument of D3DPERF_SetOptions from 1 to 0.

As an example I will demonstrate it with the game Portal 2.

The tools I used:
WinAPIOverride32: http://jacquelin.potier.free.fr/winapioverride32/
MHS6.1.rar: http://memoryhacking.com/download.php

[size="4"]Step 1: Locate where D3DPERF_SetOptions is called

First we need to figure out where D3DPERF_SetOptions is called in the application. For that we can use the API monitoring software WinAPIOverride32. The official website of WinAPIOverride32 contains very good tutorials.

First you need to create a monitoring file in order to let WinAPIOverride know what we want to monitor. Because D3DPERF_SetOptions is located in the Direct3D 9 DLL we want to create a description of the d3d9.dll. Thus, you can use DllExportFinder.exe on d3d9.dll in your Windows system directory or save the following in d3d9.txt at "winapioverride32_bin\monitoring files" in your WinApiOverride32 directory.


; Monitoring file generated for exports table of d3d9.dll v6.1.7601.17514 by MonitoringFileBuilder
!C:\Windows\SysWOW64\d3d9.dll|Direct3DShaderValidatorCreate9()
!C:\Windows\SysWOW64\d3d9.dll|PSGPError()
!C:\Windows\SysWOW64\d3d9.dll|PSGPSampleTexture()
!C:\Windows\SysWOW64\d3d9.dll|int D3DPERF_BeginEvent(D3DCOLOR col, LPCWSTR wszName)
!C:\Windows\SysWOW64\d3d9.dll|int D3DPERF_EndEvent()
!C:\Windows\SysWOW64\d3d9.dll|DWORD D3DPERF_GetStatus()
!C:\Windows\SysWOW64\d3d9.dll|BOOL D3DPERF_QueryRepeatFrame()
!C:\Windows\SysWOW64\d3d9.dll|D3DPERF_SetMarker(D3DCOLOR col, LPCWSTR wszName)
C:\Windows\SysWOW64\d3d9.dll|D3DPERF_SetOptions(DWORD)
!C:\Windows\SysWOW64\d3d9.dll|D3DPERF_SetRegion(D3DCOLOR col, LPCWSTR wszName)
!C:\Windows\SysWOW64\d3d9.dll|DebugSetLevel()
!C:\Windows\SysWOW64\d3d9.dll|DebugSetMute()
!C:\Windows\SysWOW64\d3d9.dll|IDirect3D9 * Direct3DCreate9(UINT SDKVersion)
!C:\Windows\SysWOW64\d3d9.dll|HRESULT Direct3DCreate9Ex(UINT SDKVersion, IDirect3D9Ex **ppD3D)


Next, attach WinAPIOverride at application startup of the game you want to modify.


pixhack_3.png




Select the API D3DPERF_SetOptions in the monitoring wizard and resume the execution of the attached application.

pixhack_4.png




And WinAPIOverride shows us in which DLL and where D3DPERF_SetOptions is called (0x5D496D6F) (shaderapidx9.dll + 0x00026D6F). (I'm keeping Portal 2 running in windowed mode for the next step.)

pixhack_5.png





[size="4"]Step 2: Modify the binary

Now you have located where D3DPERF_SetOptions is called in the application memory (0x5D496D6F). Lets browse the memory of the running application Portal2.exe and see with our own eyes where exactly the function is called in the memory. For this I like to use L. Spiro's Memory Hacking Software.

Open the running Process "Portal2.exe". Goto File => Properties and select the right chunk that contains the caller address. If you right click you can view it in a Disassembler or in a Hex Editor.

pixhack_6.png



This is what the disassembler shows:

pixhack_7.png



You can see where the first argument "1" is PUSHed onto the stack and where D3DPERF_SetOptions is CALLed. You can now choose to replace the "CALL" command by a "NOP"command (no operation) or change the argument that we pass to D3DPERF_SetOptions. I chose the latter option. So we just want to change the code "6A 01" to "6A 00" at 5D496D6B. This is how the application memory looks of the running portal2.exe in the hexeditor:

pixhack_8.png



But off course we want to change the binary on the hard disk. So open the fileshaderapidx9.dll with the hex editor and go to the same location. (I just searched on the same sequence of hex bytes (55 8B EC 81 4C 01 00 56 etc.) of the line 0x5D496D60 with the find function).

pixhack_9.png



Then modify 6A 01 to 6A 00.


Save the file and you are done!



pixhack_1.jpg
Litheon

Glass Window Container

[size="3"][font="Calibri"]When I was playing Portal 2 I was intrigued by the light window containers in the game.

lightbox.png[/font]


[font="Calibri"][size="3"][color="#0000ff"]
[/color][/font]
[font="Calibri"][size="3"](not my video)

So I made a shader in Rendermonkey to recreate the principle of this effect (See attachment light_glass_container.zip!).

lightboxxieie.png[/font]


[font="Calibri"][size="3"]Basically it is just simple Parallax mapping + texture coordinate distortion to simulate the refraction.
[size="3"]I still need to do some tinkering to have the same results but the basic idea [size="3"]is there[size="3"][font="Calibri"].[/font] (I haven't figured out how they blend the texture colors inside the shader yet, I quickly made up my refraction distortion up etc.)

Interesting resources:
[size="3"][color="#0000ff"]http://www.valvesoftware.com/publications/2004/GDC2004_Half-Life2_Shading.pdf[/color] [size="3"]page 91 about the refraction shader that Half-Life 2 uses.

[font="Times New Roman"][size="3"][font="Calibri"][size="3"][color="#0000ff"]http://snarf-life-2.googlecode.com/svn/trunk/src/materialsystem/stdshaders/SDK_Refract_ps20.fxc[/color][/font]

[size="3"]* The media in the attachment contains only programmer art and media from the examples of rendermonkey.
* [font="Calibri"][size="3"]Theresources of the real material in Portal 2 can be found in the main *.VPK:
[/font][font="Calibri"][size="3"]materials\glass\glasswindow_refract01.vmt
[/font][font="Calibri"][size="3"]materials\glass\refract_light_color.vtf
[size="3"]materials\glass\refract_light_normal.vtf [/font][/font]
[/font]