Hi I'm trying to implement SSR in my engine but I'm having some issues. I'm using this algorithm as it seems simple enough: http://www.christophergullbergbrady.com/SSR/ but this is the result i get:
You can see my GBuffer layout at the top (worldpos, normals, depth (r32_float), last frame) and the result of the SSR shader below it. I'm using 1.0, 0.0, 1.0 as the base color to easier visualize the results. As you can see the output is incorrect. It varies based on view direction but the result always roughly looks like that. What is going wrong? I have tried a number of algorithms and they all seem to give the wrong output. Is it a problem with my gbuffers?
Here is my SSR code
Texture2D colorTexture : register(t0);
Texture2D depthTexture : register(t1);
Texture2D positionTexture : register(t2);
Texture2D normalTexture : register(t3);
SamplerState SampleType;
cbuffer ParamBuffer {
matrix view;
matrix invProj;
matrix proj;
};
struct PixelInputType {
float4 position : SV_POSITION;
float2 tex : TEXCOORD0;
};
float4 TexturePixelShader(PixelInputType input) : SV_TARGET{
float3 outputColor;
float3 position = positionTexture.Sample(SampleType, input.tex).xyz;
float3 normal = normalTexture.Sample(SampleType, input.tex).xyz;
float3 viewPos = mul(view, float4(position, 1.0)).xyz;
float3 viewNormal = mul(view, float4(normal, 1.0)).xyz;
float3 reflection = reflect(position, normal);
float3 step = reflection;
float3 newPosition = position + step;
int loops = 150;
for (int i = 0; i < loops; ++i) {
float4 vPos = float4(viewPos, 1.0);
float4 samplePosition = mul(proj, vPos);
samplePosition.xy = (samplePosition.xy / samplePosition.w) * float2(0.5, -0.5) + 0.5;
float currentDepth = abs(vPos.z);
float sampleDepth = depthTexture.Sample(SampleType, samplePosition.xy).r;
if (abs(samplePosition.z - sampleDepth) < 0.001)
{
outputColor = colorTexture.Sample(SampleType, samplePosition.xy).rgb;
}
else {
step *= 1.0 - 0.5 * max(sign(currentDepth - sampleDepth), 0.0);
newPosition += step * (sign(sampleDepth - currentDepth) + 0.000001);
outputColor = float3(1.0, 0, 1.0);
}
}
return float4(outputColor, 1.0f);
}
The sampler is set to CLAMP and the filter mode is MIN_MAG_MIP_POINT. Any ideas how to fix it? Thanks.