Skip to content
This repository was archived by the owner on Nov 30, 2020. It is now read-only.

Commit 241b952

Browse files
committed
Added initial VR support for SSR
1 parent 2a3c17e commit 241b952

2 files changed

Lines changed: 29 additions & 25 deletions

File tree

PostProcessing/Shaders/Builtins/ScreenSpaceReflections.hlsl

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,8 @@ Texture2D _CameraDepthTexture; SamplerState sampler_CameraDepthTexture;
5656
Texture2D _CameraMotionVectorsTexture; SamplerState sampler_CameraMotionVectorsTexture;
5757
Texture2D _CameraReflectionsTexture; SamplerState sampler_CameraReflectionsTexture;
5858

59-
Texture2D _CameraGBufferTexture0; SamplerState sampler_CameraGBufferTexture0; // albedo = g[0].rgb
60-
Texture2D _CameraGBufferTexture1; SamplerState sampler_CameraGBufferTexture1; // roughness = g[1].a
59+
Texture2D _CameraGBufferTexture0; // albedo = g[0].rgb
60+
Texture2D _CameraGBufferTexture1; // roughness = g[1].a
6161
Texture2D _CameraGBufferTexture2; SamplerState sampler_CameraGBufferTexture2; // normal.xyz 2. * g[2].rgb - 1.
6262

6363
Texture2D _Noise; SamplerState sampler_Noise;
@@ -105,7 +105,7 @@ float Vignette(float2 uv)
105105

106106
float3 GetViewSpacePosition(float2 uv)
107107
{
108-
float depth = _CameraDepthTexture.SampleLevel(sampler_CameraDepthTexture, uv, 0).r;
108+
float depth = _CameraDepthTexture.SampleLevel(sampler_CameraDepthTexture, UnityStereoTransformScreenSpaceTex(uv), 0).r;
109109
float4 result = mul(_InverseProjectionMatrix, float4(2.0 * uv - 1.0, depth, 1.0));
110110
return result.xyz / result.w;
111111
}
@@ -224,7 +224,7 @@ Result March(Ray ray, VaryingsDefault input)
224224

225225
uv *= _Test_TexelSize.xy;
226226

227-
float d = _CameraDepthTexture.SampleLevel(sampler_CameraDepthTexture, uv, 0);
227+
float d = _CameraDepthTexture.SampleLevel(sampler_CameraDepthTexture, UnityStereoTransformScreenSpaceTex(uv), 0);
228228
float depth = -LinearEyeDepth(d);
229229

230230
UNITY_FLATTEN
@@ -245,7 +245,7 @@ Result March(Ray ray, VaryingsDefault input)
245245
//
246246
float4 FragTest(VaryingsDefault i) : SV_Target
247247
{
248-
float4 gbuffer2 = _CameraGBufferTexture2.Sample(sampler_CameraGBufferTexture2, i.texcoord.xy);
248+
float4 gbuffer2 = _CameraGBufferTexture2.Sample(sampler_CameraGBufferTexture2, i.texcoordStereo);
249249

250250
if (dot(gbuffer2, 1.0) == 0.0)
251251
return 0.0;
@@ -273,9 +273,9 @@ float4 FragResolve(VaryingsDefault i) : SV_Target
273273
float4 test = _Test.Load(int3(i.vertex.xy, 0));
274274

275275
if (test.w == 0.0)
276-
return _MainTex.Sample(sampler_MainTex, i.texcoord);
276+
return _MainTex.Sample(sampler_MainTex, i.texcoordStereo);
277277

278-
float4 color = _MainTex.SampleLevel(sampler_MainTex, test.xy, 0);
278+
float4 color = _MainTex.SampleLevel(sampler_MainTex, UnityStereoTransformScreenSpaceTex(test.xy), 0);
279279

280280
float confidence = test.w * Attenuate(test.xy) * Vignette(test.xy);
281281

@@ -287,29 +287,29 @@ float4 FragResolve(VaryingsDefault i) : SV_Target
287287

288288
float4 FragReproject(VaryingsDefault i) : SV_Target
289289
{
290-
float2 motion = _CameraMotionVectorsTexture.SampleLevel(sampler_CameraMotionVectorsTexture, i.texcoord, 0).xy;
290+
float2 motion = _CameraMotionVectorsTexture.SampleLevel(sampler_CameraMotionVectorsTexture, i.texcoordStereo, 0).xy;
291291
float2 uv = i.texcoord - motion;
292292

293293
const float2 k = SSR_COLOR_NEIGHBORHOOD_SAMPLE_SPREAD * _MainTex_TexelSize.xy;
294294

295-
float4 color = _MainTex.SampleLevel(sampler_MainTex, i.texcoord, 0);
295+
float4 color = _MainTex.SampleLevel(sampler_MainTex, i.texcoordStereo, 0);
296296

297297
// 0 1 2
298298
// 3
299299
float4x4 top = float4x4(
300-
_MainTex.SampleLevel(sampler_MainTex, i.texcoord + float2(-k.x, -k.y), 0),
301-
_MainTex.SampleLevel(sampler_MainTex, i.texcoord + float2( 0.0, -k.y), 0),
302-
_MainTex.SampleLevel(sampler_MainTex, i.texcoord + float2( k.x, -k.y), 0),
303-
_MainTex.SampleLevel(sampler_MainTex, i.texcoord + float2(-k.x, 0.0), 0)
300+
_MainTex.SampleLevel(sampler_MainTex, UnityStereoTransformScreenSpaceTex(i.texcoord + float2(-k.x, -k.y)), 0),
301+
_MainTex.SampleLevel(sampler_MainTex, UnityStereoTransformScreenSpaceTex(i.texcoord + float2( 0.0, -k.y)), 0),
302+
_MainTex.SampleLevel(sampler_MainTex, UnityStereoTransformScreenSpaceTex(i.texcoord + float2( k.x, -k.y)), 0),
303+
_MainTex.SampleLevel(sampler_MainTex, UnityStereoTransformScreenSpaceTex(i.texcoord + float2(-k.x, 0.0)), 0)
304304
);
305305

306306
// 0
307307
// 1 2 3
308308
float4x4 bottom = float4x4(
309-
_MainTex.SampleLevel(sampler_MainTex, i.texcoord + float2( k.x, 0.0), 0),
310-
_MainTex.SampleLevel(sampler_MainTex, i.texcoord + float2(-k.x, k.y), 0),
311-
_MainTex.SampleLevel(sampler_MainTex, i.texcoord + float2( 0.0, k.y), 0),
312-
_MainTex.SampleLevel(sampler_MainTex, i.texcoord + float2( k.x, k.y), 0)
309+
_MainTex.SampleLevel(sampler_MainTex, UnityStereoTransformScreenSpaceTex(i.texcoord + float2( k.x, 0.0)), 0),
310+
_MainTex.SampleLevel(sampler_MainTex, UnityStereoTransformScreenSpaceTex(i.texcoord + float2(-k.x, k.y)), 0),
311+
_MainTex.SampleLevel(sampler_MainTex, UnityStereoTransformScreenSpaceTex(i.texcoord + float2( 0.0, k.y)), 0),
312+
_MainTex.SampleLevel(sampler_MainTex, UnityStereoTransformScreenSpaceTex(i.texcoord + float2( k.x, k.y)), 0)
313313
);
314314

315315
// PS4 INTRINSIC_MINMAX3
@@ -321,7 +321,7 @@ float4 FragReproject(VaryingsDefault i) : SV_Target
321321
float4 maximum = max(max(max(max(max(max(max(max(top[0], top[1]), top[2]), top[3]), bottom[0]), bottom[1]), bottom[2]), bottom[3]), color);
322322
#endif
323323

324-
float4 history = _History.SampleLevel(sampler_History, uv, 0);
324+
float4 history = _History.SampleLevel(sampler_History, UnityStereoTransformScreenSpaceTex(uv), 0);
325325
history = clamp(history, minimum, maximum);
326326

327327
color.a = saturate(smoothstep(0.002 * _MainTex_TexelSize.z, 0.0035 * _MainTex_TexelSize.z, length(motion)));
@@ -335,10 +335,10 @@ float4 FragReproject(VaryingsDefault i) : SV_Target
335335

336336
float4 FragComposite(VaryingsDefault i) : SV_Target
337337
{
338-
float z = _CameraDepthTexture.SampleLevel(sampler_CameraDepthTexture, i.texcoord, 0).r;
338+
float z = _CameraDepthTexture.SampleLevel(sampler_CameraDepthTexture, i.texcoordStereo, 0).r;
339339

340340
if (Linear01Depth(z) > 0.999)
341-
return _MainTex.Sample(sampler_MainTex, i.texcoord);
341+
return _MainTex.Sample(sampler_MainTex, i.texcoordStereo);
342342

343343
float4 gbuffer0 = _CameraGBufferTexture0.Load(int3(i.vertex.xy, 0));
344344
float4 gbuffer1 = _CameraGBufferTexture1.Load(int3(i.vertex.xy, 0));
@@ -354,10 +354,10 @@ float4 FragComposite(VaryingsDefault i) : SV_Target
354354
position = mul(_InverseViewMatrix, float4(position, 1.0)).xyz;
355355

356356
#if SSR_ENABLE_CONTACTS
357-
float4 test = _Test.SampleLevel(sampler_Test, i.texcoord, 0);
358-
float4 resolve = _Resolve.SampleLevel(sampler_Resolve, i.texcoord, SmoothnessToRoughness(gbuffer1.a) * (_BlurPyramidLODCount - 1.0) * test.z + 1.0);
357+
float4 test = _Test.SampleLevel(sampler_Test, i.texcoordStereo, 0);
358+
float4 resolve = _Resolve.SampleLevel(sampler_Resolve, i.texcoordStereo, SmoothnessToRoughness(gbuffer1.a) * (_BlurPyramidLODCount - 1.0) * test.z + 1.0);
359359
#else
360-
float4 resolve = _Resolve.SampleLevel(sampler_Resolve, i.texcoord, SmoothnessToRoughness(gbuffer1.a) * (_BlurPyramidLODCount - 1.0) + 1.0);
360+
float4 resolve = _Resolve.SampleLevel(sampler_Resolve, i.texcoordStereo, SmoothnessToRoughness(gbuffer1.a) * (_BlurPyramidLODCount - 1.0) + 1.0);
361361
#endif
362362

363363
float confidence = saturate(2.0 * dot(-eye, normalize(reflect(-eye, normal))));
@@ -373,9 +373,9 @@ float4 FragComposite(VaryingsDefault i) : SV_Target
373373

374374
resolve.rgb = UNITY_BRDF_PBS(gbuffer0.rgb, gbuffer1.rgb, oneMinusReflectivity, gbuffer1.a, normal, -eye, light, indirect).rgb;
375375

376-
float4 reflectionProbes = _CameraReflectionsTexture.Sample(sampler_CameraReflectionsTexture, i.texcoord);
376+
float4 reflectionProbes = _CameraReflectionsTexture.Sample(sampler_CameraReflectionsTexture, i.texcoordStereo);
377377

378-
float4 color = _MainTex.Sample(sampler_MainTex, i.texcoord);
378+
float4 color = _MainTex.Sample(sampler_MainTex, i.texcoordStereo);
379379
color.rgb = max(0.0, color.rgb - reflectionProbes.rgb);
380380

381381
float fade = 1.0 - resolve.a * _DistanceFade;

PostProcessing/Shaders/Builtins/ScreenSpaceReflections.shader

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ Shader "Hidden/PostProcessing/ScreenSpaceReflections"
55

66
CGINCLUDE
77

8+
#include "UnityCG.cginc"
89
#pragma target 5.0
910

1011
// Ported from StdLib, we can't include it as it'll conflict with internal Unity includes
@@ -17,6 +18,7 @@ Shader "Hidden/PostProcessing/ScreenSpaceReflections"
1718
{
1819
float4 vertex : SV_POSITION;
1920
float2 texcoord : TEXCOORD0;
21+
float2 texcoordStereo : TEXCOORD1;
2022
};
2123

2224
VaryingsDefault VertDefault(AttributesDefault v)
@@ -29,6 +31,8 @@ Shader "Hidden/PostProcessing/ScreenSpaceReflections"
2931
o.texcoord = o.texcoord * float2(1.0, -1.0) + float2(0.0, 1.0);
3032
#endif
3133

34+
o.texcoordStereo = TransformStereoScreenSpaceTex(o.texcoord, 1.0);
35+
3236
return o;
3337
}
3438

0 commit comments

Comments
 (0)