Jump to content

  • Log In with Google      Sign In   
  • Create Account

Litheon's Journal

Real-Time Local Reflections

Posted by , 29 January 2012 - * * * * * · 23,454 views
RLR Screen Space Reflections and 1 more...
Real-Time Local Reflections
  • Fade at screen edges
  • Fade when reflection vector is facing the viewer
  • Errors where there is no data available
  • Real-Time Local Reflections

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.

Posted Image


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


My Implementation

I hope the shader shown below is pretty self-explanatory Posted Image
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 1st time without the reflections, the 2nd time with). In this way I can use the depth buffer of the 1st pass for my reflections in the 2nd 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);
	 // 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;	

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

Fade out when reflection vector faces viewer

Posted Image

Fade out when reflection samples reach screen border


Remaining problems

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:Attached File  RealTimeLocalReflections_LitheonJan2012.rar (2.36MB)
downloads: 2149. (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).

PIX: How to circumvent D3DPERF_SetOptions

Posted by , 09 November 2011 - - - - - - · 3,225 views

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:

Posted Image

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

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|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_SetRegion(D3DCOLOR col, LPCWSTR wszName)
!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.

Posted Image

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

Posted Image

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

Posted Image

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.

Posted Image

This is what the disassembler shows:


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:


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


Then modify 6A 01 to 6A 00.

Save the file and you are done!


Glass Window Container

Posted by , 08 November 2011 - - - - - - · 1,012 views
Portal, Portal 2 and 1 more...
When I was playing Portal 2 I was intrigued by the light window containers in the game.

Posted Image

(not my video)

So I made a shader in Rendermonkey to recreate the principle of this effect (See attachment Attached File  light_glass_container.zip (1000.16KB)
downloads: 78!).

Posted Image

Basically it is just simple Parallax mapping + texture coordinate distortion to simulate the refraction.
I still need to do some tinkering to have the same results but the basic idea is there. (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:
http://www.valvesoftware.com/publications/2004/GDC2004_Half-Life2_Shading.pdf page 91 about the refraction shader that Half-Life 2 uses.


* The media in the attachment contains only programmer art and media from the examples of rendermonkey.
* Theresources of the real material in Portal 2 can be found in the main *.VPK:

October 2016 »

1617181920 21 22

Recent Entries

Recent Comments