/ Coding

Screen Space Reflection Shader (C++)

The shader

In this project, I was tasked with writing a screen space reflection shader in our professor's C++ based game engine. This shader involved creating a few different textures using DirectX 11. These textures were generated during the first draw call that rendered all of the objects in the screen with their texture diffuse values only. The end result is a full-bright representation of the scene. This step produced 3 textures:

  • Diffuse map - a 1280x720 texture that stored the RGBA value of each pixel
  • Normal map - a 1280x720 texture that stored the xyz normal value of each pixel along with the sampled diffuse map value
  • Position map - a 4x32bit float 1280x720 texture that stored the absolute position of each pixel on the screen

Using these three textures, I was able to perform a 2 pass raytrace, bouncing the ray off the ground texture and figuring out if the ray connected with a point on the screen, then using a more precise raytrace to figure out exactly what pixel's value to sample for the reflection. This value was then added to the original color of the ground texture, with an intensity controlled by the diffuse map of the ground.

Improvements

I introduced a normal map clamping feature to improve the look of the reflections. Sampling the cobble's dramatic normal created many rays that bounced off the screen and ruined the reflection effect, so I clamped the normal of the texture close to the cobble plain's actual physical normal (up) to make the reflection rays tend towards the same direction.

Performance Impact

My screen space reflection shader kept the FPS of the game just above 120, down from 140 without the shader.

Jeffrey Hanford

Jeffrey Hanford

https://github.com/Tioga060/ Masters of Science in Computer Science, USC 2018 Bachelors of Science in Electrical and Computer Engineering, Olin College of Engineering 2016

Read More