Skip to content

Commit

Permalink
initial work on screenspace reflections #114
Browse files Browse the repository at this point in the history
  • Loading branch information
johang88 committed Sep 13, 2014
1 parent 5a617d8 commit f959ba5
Show file tree
Hide file tree
Showing 5 changed files with 175 additions and 1 deletion.
90 changes: 90 additions & 0 deletions Data/core_data/shaders/post/screenspacereflections.glsl
@@ -0,0 +1,90 @@
import(/shaders/core);
import(/shaders/post/postcommon);
#ifdef VERTEX_SHADER

layout(location = ATTRIB_POSITION) in vec3 iPosition;
layout(location = ATTRIB_TEXCOORD_0) in vec2 iTexCoord;

out vec2 texCoord;

void main()
{
texCoord = iTexCoord;
gl_Position = vec4(iPosition, 1);
}

#else

in vec2 texCoord;

layout(location = 0) out vec4 oColor;

uniform float cameraNearClipDistance;
uniform mat4x4 itView;
uniform mat4x4 viewProjectionMatrix;
// uniform vec3 cameraPosition;
uniform vec2 cameraClipPlane;
uniform sampler2D samplerScene;
uniform sampler2D samplerNormal;
uniform sampler2D samplerDepth;

float getDepthAt(vec2 uv) {
float depth = texture2D(samplerDepth, uv).x;

// depth = depth * 2.0 - 1.0;

float n = cameraClipPlane.x;
float f = cameraClipPlane.y;

return (2 * n) / (f + n - depth * (f - n));
// return depth;
}

vec3 raytrace(in vec3 reflectionVector, in float startDepth) {
vec3 color = vec3(0, 0, 0);

float stepSize = 0.01; // Uniform this

float size = length(reflectionVector);
reflectionVector = normalize(reflectionVector / size);
reflectionVector *= stepSize;

vec2 uv = texCoord;
float currentDepth = startDepth;

int samples = 0;
while (uv.x >= 0.0 && uv.x <= 1.0 && uv.y >= 0.0 && uv.y <= 1.0) {
samples++;
uv += reflectionVector.xy;

currentDepth += reflectionVector.z * startDepth;
float sampledDepth = getDepthAt(uv);

if (currentDepth > sampledDepth) {
float delta = currentDepth - sampledDepth;
if (delta < 0.003) {
color = texture2D(samplerScene, texCoord).xyz;
break;
}
}

if (samples >= 64)
break;
}

return color;
}

void main() {
vec3 color = texture2D(samplerScene, texCoord).xyz;
vec3 normal = (itView * vec4(decodeNormals(texture2D(samplerNormal, texCoord).xyz), 0)).xyz;

float depth = getDepthAt(texCoord);

vec3 cameraPosition = normalize(vec3(0, 0, cameraClipPlane.x));
vec3 reflectionVector = (viewProjectionMatrix * vec4(reflect(-cameraPosition, normal), 0)).xyz;

oColor.xyz = color + raytrace(reflectionVector, depth); // todo ;)
oColor.w = 1;
}
#endif
56 changes: 55 additions & 1 deletion Triton.Graphics/Post/Effects/ScreenSpaceReflections.cs
Expand Up @@ -6,7 +6,61 @@

namespace Triton.Graphics.Post.Effects
{
class ScreenSpaceReflections
public class ScreenSpaceReflections : BaseEffect
{
private Resources.ShaderProgram Shader;
private ScreenSpaceReflectionsShaderParams ShaderParams;

public ScreenSpaceReflections(Backend backend, Common.ResourceManager resourceManager, BatchBuffer quadMesh)
: base(backend, resourceManager, quadMesh)
{
Shader = ResourceManager.Load<Resources.ShaderProgram>("/shaders/post/screenspacereflections");
}

public void Render(Camera camera, RenderTarget gbuffer, RenderTarget input, RenderTarget output)
{
if (ShaderParams == null)
{
ShaderParams = new ScreenSpaceReflectionsShaderParams();
Shader.GetUniformLocations(ShaderParams);
}

Matrix4 viewMatrix;
camera.GetViewMatrix(out viewMatrix);

Matrix4 projectionMatrix;
camera.GetProjectionMatrix(out projectionMatrix);

var viewProjectionMatrix = viewMatrix * projectionMatrix;

var cameraClipPlane = new Vector2(camera.NearClipDistance, camera.FarClipDistance);

var itView = Matrix4.Transpose(Matrix4.Invert(viewMatrix));

Backend.BeginPass(output, new Vector4(0.0f, 0.0f, 0.0f, 1.0f));
Backend.BeginInstance(Shader.Handle, new int[] { input.Textures[0].Handle, gbuffer.Textures[1].Handle, gbuffer.Textures[3].Handle },
samplers: new int[] { Backend.DefaultSamplerNoFiltering, Backend.DefaultSamplerNoFiltering, Backend.DefaultSamplerNoFiltering });
Backend.BindShaderVariable(ShaderParams.SamplerScene, 0);
Backend.BindShaderVariable(ShaderParams.SamplerNormal, 1);
Backend.BindShaderVariable(ShaderParams.SamplerDepth, 2);
Backend.BindShaderVariable(ShaderParams.CameraPosition, ref camera.Position);
Backend.BindShaderVariable(ShaderParams.ViewProjectionMatrix, ref projectionMatrix);
Backend.BindShaderVariable(ShaderParams.CameraClipPlane, ref cameraClipPlane);
Backend.BindShaderVariable(ShaderParams.ItView, ref itView);

Backend.DrawMesh(QuadMesh.MeshHandle);
Backend.EndPass();
}

class ScreenSpaceReflectionsShaderParams
{
public int SamplerScene = 0;
public int SamplerNormal = 0;
public int SamplerDepth = 0;
public int CameraPosition = 0;
public int ViewProjectionMatrix = 0;
public int CameraClipPlane = 0;
public int ItView = 0;
}
}
}
16 changes: 16 additions & 0 deletions Triton.Graphics/Post/PostEffectManager.cs
Expand Up @@ -20,8 +20,10 @@ public class PostEffectManager
// Settings
public AntiAliasing AntiAliasing = AntiAliasing.FXAA;
public HDRSettings HDRSettings = new HDRSettings();
public ScreenSpaceReflectionsSettings ScreenSpaceReflectionsSettings = new ScreenSpaceReflectionsSettings();

// Effects
private readonly Effects.ScreenSpaceReflections ScreenSpaceReflections;
private readonly Effects.AdaptLuminance AdaptLuminance;
private readonly Effects.Bloom Bloom;
private readonly Effects.Tonemap Tonemap;
Expand Down Expand Up @@ -58,6 +60,7 @@ public PostEffectManager(Common.ResourceManager resourceManager, Backend backend
Sprite = Backend.CreateSpriteBatch();

// Setup effects
ScreenSpaceReflections = new Effects.ScreenSpaceReflections(Backend, ResourceManager, QuadMesh);
AdaptLuminance = new Effects.AdaptLuminance(Backend, ResourceManager, QuadMesh);
Bloom = new Effects.Bloom(Backend, ResourceManager, QuadMesh);
Tonemap = new Effects.Tonemap(Backend, ResourceManager, QuadMesh);
Expand All @@ -69,6 +72,8 @@ public PostEffectManager(Common.ResourceManager resourceManager, Backend backend
HDRSettings.AdaptationRate = 0.5f;
HDRSettings.BlurSigma = 3.0f;
HDRSettings.BloomThreshold = 9.0f;

ScreenSpaceReflectionsSettings.Enable = false;
}

void SwapRenderTargets()
Expand Down Expand Up @@ -102,6 +107,15 @@ private void ApplyLumianceBloomAndTonemap(float deltaTime)
SwapRenderTargets();
}

private void ApplyScreenSpaceReflections(Camera camera, RenderTarget gbuffer)
{
if (!ScreenSpaceReflectionsSettings.Enable)
return;

ScreenSpaceReflections.Render(camera, gbuffer, TemporaryRenderTargets[0], TemporaryRenderTargets[1]);
SwapRenderTargets();
}

public RenderTarget Render(Camera camera, RenderTarget gbuffer, RenderTarget input, float deltaTime)
{
// We always start by rendering the input texture to a temporary render target
Expand All @@ -112,6 +126,8 @@ public RenderTarget Render(Camera camera, RenderTarget gbuffer, RenderTarget inp

SwapRenderTargets();


ApplyScreenSpaceReflections(camera, gbuffer);
ApplyLumianceBloomAndTonemap(deltaTime);

ApplyAA();
Expand Down
13 changes: 13 additions & 0 deletions Triton.Graphics/Post/ScreenSpaceReflectionsSettings.cs
@@ -0,0 +1,13 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Triton.Graphics.Post
{
public struct ScreenSpaceReflectionsSettings
{
public bool Enable;
}
}
1 change: 1 addition & 0 deletions Triton.Graphics/Triton.Graphics.csproj
Expand Up @@ -93,6 +93,7 @@
<Compile Include="Post\HDRSettings.cs" />
<Compile Include="Post\PostEffectManager.cs" />
<Compile Include="Post\RenderTargetManager.cs" />
<Compile Include="Post\ScreenSpaceReflectionsSettings.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="RenderOperation.cs" />
<Compile Include="RenderOperations.cs" />
Expand Down

0 comments on commit f959ba5

Please sign in to comment.