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

Commit fcdc3e2

Browse files
authored
Merge pull request #301 from Unity-Technologies/xr_taa_compatibility
XR Support for TAA (fixes)
2 parents c8ad0af + cf2dba8 commit fcdc3e2

5 files changed

Lines changed: 76 additions & 66 deletions

File tree

PostProcessing/Editor/PostProcessLayerEditor.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ void DoAntialiasing()
206206
if (m_AntialiasingMode.intValue == (int)PostProcessLayer.Antialiasing.TemporalAntialiasing)
207207
{
208208
if (RuntimeUtilities.isSinglePassStereoEnabled)
209-
EditorGUILayout.HelpBox("TAA doesn't work with Single-pass stereo rendering.", MessageType.Warning);
209+
EditorGUILayout.HelpBox("TAA requires Unity 2017.3+ for Single-pass stereo rendering support.", MessageType.Warning);
210210

211211
EditorGUILayout.PropertyField(m_TaaJitterSpread);
212212
EditorGUILayout.PropertyField(m_TaaStationaryBlending);

PostProcessing/Runtime/Effects/TemporalAntialiasing.cs

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -115,14 +115,15 @@ public void ConfigureJitteredProjectionMatrix(PostProcessRenderContext context)
115115
camera.useJitteredProjectionMatrixForTransparentRendering = false;
116116
}
117117

118+
// TODO: We'll probably need to isolate most of this for SRPs
118119
public void ConfigureStereoJitteredProjectionMatrices(PostProcessRenderContext context)
119120
{
120121
#if UNITY_2017_3_OR_NEWER
121122
var camera = context.camera;
122123
jitter = GenerateRandomOffset();
123124
jitter *= jitterSpread;
124125

125-
for (Camera.StereoscopicEye eye = Camera.StereoscopicEye.Left; eye <= Camera.StereoscopicEye.Right; eye++)
126+
for (var eye = Camera.StereoscopicEye.Left; eye <= Camera.StereoscopicEye.Right; eye++)
126127
{
127128
// This saves off the device generated projection matrices as non-jittered
128129
context.camera.CopyStereoDeviceProjectionMatrixToNonJittered(eye);
@@ -136,16 +137,25 @@ public void ConfigureStereoJitteredProjectionMatrices(PostProcessRenderContext c
136137

137138
// jitter has to be scaled for the actual eye texture size, not just the intermediate texture size
138139
// which could be double-wide in certain stereo rendering scenarios
139-
jitter = new Vector2(jitter.x / context.singleEyeWidth, jitter.y / context.height);
140+
jitter = new Vector2(jitter.x / context.xrSingleEyeWidth, jitter.y / context.height);
140141
camera.useJitteredProjectionMatrixForTransparentRendering = false;
141142
#endif
142143
}
143144

144-
private void GenerateHistoryName(RenderTexture rt, int id, PostProcessRenderContext context)
145+
void GenerateHistoryName(RenderTexture rt, int id, PostProcessRenderContext context)
145146
{
146-
rt.name = "Temporal Anti-aliasing History id #" + id.ToString();
147-
if (XR.XRSettings.isDeviceActive)
148-
rt.name += " for eye " + context.xrActiveEye.ToString();
147+
rt.name = "Temporal Anti-aliasing History id #" + id;
148+
149+
bool vrDeviceActive = false;
150+
151+
#if UNITY_2017_2_OR_NEWER
152+
vrDeviceActive = XR.XRSettings.isDeviceActive;
153+
#else
154+
vrDeviceActive = VR.VRSettings.isDeviceActive;
155+
#endif
156+
157+
if (vrDeviceActive)
158+
rt.name += " for eye " + context.xrActiveEye;
149159
}
150160

151161
RenderTexture CheckHistory(int id, PostProcessRenderContext context)

PostProcessing/Runtime/PostProcessLayer.cs

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@
55

66
namespace UnityEngine.Rendering.PostProcessing
77
{
8+
#if UNITY_2017_2_OR_NEWER
9+
using XRSettings = UnityEngine.XR.XRSettings;
10+
#elif UNITY_5_6_OR_NEWER
11+
using XRSettings = UnityEngine.VR.VRSettings;
12+
#endif
13+
814
// TODO: XMLDoc everything (?)
915
[DisallowMultipleComponent, ExecuteInEditMode, ImageEffectAllowedInSceneView]
1016
[AddComponentMenu("Rendering/Post-process Layer", -1)]
@@ -266,7 +272,8 @@ void OnPreCull()
266272
// is switched off and the FOV or any other camera property changes.
267273
m_Camera.ResetProjectionMatrix();
268274
m_Camera.nonJitteredProjectionMatrix = m_Camera.projectionMatrix;
269-
if (XR.XRSettings.isDeviceActive)
275+
276+
if (XRSettings.isDeviceActive)
270277
m_Camera.ResetStereoProjectionMatrices();
271278

272279
BuildCommandBuffers();
@@ -401,10 +408,9 @@ void OnPostRender()
401408
{
402409
m_Camera.ResetProjectionMatrix();
403410

404-
if (XR.XRSettings.isDeviceActive)
411+
if (XRSettings.isDeviceActive)
405412
{
406-
if (m_CurrentContext.xrSinglePass ||
407-
(m_Camera.stereoActiveEye == Camera.MonoOrStereoscopicEye.Right))
413+
if (RuntimeUtilities.isSinglePassStereoEnabled || m_Camera.stereoActiveEye == Camera.MonoOrStereoscopicEye.Right)
408414
m_Camera.ResetStereoProjectionMatrices();
409415
}
410416
}
@@ -589,14 +595,16 @@ public void Render(PostProcessRenderContext context)
589595
{
590596
if (!RuntimeUtilities.scriptableRenderPipelineActive)
591597
{
592-
if (XR.XRSettings.isDeviceActive)
598+
if (XRSettings.isDeviceActive)
593599
{
594600
// We only need to configure all of this once for stereo, during OnPreCull
595601
if (context.camera.stereoActiveEye != Camera.MonoOrStereoscopicEye.Right)
596602
temporalAntialiasing.ConfigureStereoJitteredProjectionMatrices(context);
597603
}
598604
else
605+
{
599606
temporalAntialiasing.ConfigureJitteredProjectionMatrix(context);
607+
}
600608
}
601609

602610
var taaTarget = m_TargetPool.Get();

PostProcessing/Runtime/PostProcessRenderContext.cs

Lines changed: 37 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,48 @@
11
namespace UnityEngine.Rendering.PostProcessing
22
{
3+
#if UNITY_2017_2_OR_NEWER
4+
using XRSettings = UnityEngine.XR.XRSettings;
5+
#elif UNITY_5_6_OR_NEWER
6+
using XRSettings = UnityEngine.VR.VRSettings;
7+
#endif
8+
39
// Context object passed around all post-fx in a frame
410
public sealed class PostProcessRenderContext
511
{
612
// -----------------------------------------------------------------------------------------
713
// The following should be filled by the render pipeline
814

915
// Camera currently rendering
10-
private Camera m_camera;
16+
Camera m_Camera;
1117
public Camera camera
1218
{
13-
get
14-
{
15-
return this.m_camera;
16-
}
17-
19+
get { return m_Camera; }
1820
set
1921
{
20-
this.m_camera = value;
22+
m_Camera = value;
2123

22-
if (XR.XRSettings.isDeviceActive)
24+
if (XRSettings.isDeviceActive)
2325
{
24-
RenderTextureDescriptor xrDesc = XR.XRSettings.eyeTextureDesc;
25-
m_width = xrDesc.width;
26-
m_height = xrDesc.height;
27-
28-
m_xrSinglePass = (xrDesc.vrUsage == VRTextureUsage.TwoEyes);
26+
#if UNITY_2017_2_OR_NEWER
27+
RenderTextureDescriptor xrDesc = XRSettings.eyeTextureDesc;
28+
width = xrDesc.width;
29+
height = xrDesc.height;
30+
#else
31+
width = m_Camera.pixelWidth * 2;
32+
height = m_Camera.pixelHeight;
33+
#endif
2934

3035
if (camera.stereoActiveEye == Camera.MonoOrStereoscopicEye.Right)
31-
m_xrActiveEye = (int)Camera.StereoscopicEye.Right;
36+
xrActiveEye = (int)Camera.StereoscopicEye.Right;
3237

33-
m_xrSingleEyeWidth = XR.XRSettings.eyeTextureWidth;
38+
xrSingleEyeWidth = XRSettings.eyeTextureWidth;
39+
xrSingleEyeHeight = XRSettings.eyeTextureHeight;
3440
}
3541
else
3642
{
37-
m_width = m_camera.pixelWidth;
38-
m_height = m_camera.pixelHeight;
39-
m_xrSingleEyeWidth = m_width;
43+
width = m_Camera.pixelWidth;
44+
height = m_Camera.pixelHeight;
45+
xrSingleEyeWidth = width;
4046
}
4147
}
4248
}
@@ -78,39 +84,19 @@ public Camera camera
7884
public PostProcessDebugLayer debugLayer { get; internal set; }
7985

8086
// Current camera width in pixels
81-
private int m_width;
82-
public int width
83-
{
84-
get { return m_width; }
85-
}
87+
public int width { get; private set; }
8688

8789
// Current camera height in pixels
88-
private int m_height;
89-
public int height
90-
{
91-
get { return m_height; }
92-
}
93-
94-
// Is XR running in single-pass stereo mode?
95-
private bool m_xrSinglePass;
96-
public bool xrSinglePass
97-
{
98-
get { return m_xrSinglePass; }
99-
}
90+
public int height { get; private set; }
10091

10192
// Current active rendering eye (for XR)
102-
private int m_xrActiveEye;
103-
public int xrActiveEye
104-
{
105-
get { return m_xrActiveEye; }
106-
}
93+
public int xrActiveEye { get; private set; }
10794

10895
// Current single eye width in pixels (for XR)
109-
private int m_xrSingleEyeWidth;
110-
public int singleEyeWidth
111-
{
112-
get { return m_xrSingleEyeWidth; }
113-
}
96+
public int xrSingleEyeWidth { get; private set; }
97+
98+
// Current single eye height in pixels (for XR)
99+
public int xrSingleEyeHeight { get; private set; }
114100

115101
// Are we currently rendering in the scene view?
116102
public bool isSceneView { get; internal set; }
@@ -124,13 +110,13 @@ public int singleEyeWidth
124110

125111
public void Reset()
126112
{
127-
m_camera = null;
128-
m_width = 0;
129-
m_height = 0;
113+
m_Camera = null;
114+
width = 0;
115+
height = 0;
130116

131-
m_xrSinglePass = false;
132-
m_xrActiveEye = (int)Camera.StereoscopicEye.Left;
133-
m_xrSingleEyeWidth = 0;
117+
xrActiveEye = (int)Camera.StereoscopicEye.Left;
118+
xrSingleEyeWidth = 0;
119+
xrSingleEyeHeight = 0;
134120

135121
command = null;
136122
source = 0;

PostProcessing/Runtime/Utils/RuntimeUtilities.cs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,8 @@ public static bool isSinglePassStereoEnabled
232232
return UnityEditor.PlayerSettings.virtualRealitySupported
233233
&& UnityEditor.PlayerSettings.stereoRenderingPath == UnityEditor.StereoRenderingPath.SinglePass
234234
&& Application.isPlaying;
235+
#elif UNITY_2017_2_OR_NEWER
236+
return UnityEngine.XRSettings.eyeTextureDesc.vrUsage == VRTextureUsage.TwoEyes;
235237
#else
236238
return false;
237239
#endif
@@ -408,13 +410,14 @@ public static Matrix4x4 GetJitteredOrthographicProjectionMatrix(Camera camera, V
408410

409411
public static Matrix4x4 GenerateJitteredProjectionMatrixFromOriginal(PostProcessRenderContext context, Matrix4x4 origProj, Vector2 jitter)
410412
{
411-
FrustumPlanes planes = origProj.decomposeProjection;
413+
#if UNITY_2017_2_OR_NEWER
414+
var planes = origProj.decomposeProjection;
412415

413416
float vertFov = Math.Abs(planes.top) + Math.Abs(planes.bottom);
414417
float horizFov = Math.Abs(planes.left) + Math.Abs(planes.right);
415418

416-
var planeJitter = new Vector2(jitter.x * horizFov / context.singleEyeWidth,
417-
jitter.y * vertFov / context.height);
419+
var planeJitter = new Vector2(jitter.x * horizFov / context.xrSingleEyeWidth,
420+
jitter.y * vertFov / context.height);
418421

419422
planes.left += planeJitter.x;
420423
planes.right += planeJitter.x;
@@ -424,6 +427,9 @@ public static Matrix4x4 GenerateJitteredProjectionMatrixFromOriginal(PostProcess
424427
var jitteredMatrix = Matrix4x4.Frustum(planes);
425428

426429
return jitteredMatrix;
430+
#endif
431+
432+
return origProj;
427433
}
428434

429435
#endregion

0 commit comments

Comments
 (0)