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

Commit 0991239

Browse files
committed
Cleanups for TAA
Couple more bits to do * Support RT arrays * Generalize code between OnPreCull and OnPreRender for TAA
1 parent 8a0f378 commit 0991239

4 files changed

Lines changed: 66 additions & 66 deletions

File tree

PostProcessing/Runtime/Effects/TemporalAntialiasing.cs

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -41,17 +41,16 @@ enum Pass
4141

4242
// Ping-pong between two history textures as we can't read & write the same target in the
4343
// same pass
44-
// TODO: We need to make left/right arrays
45-
//readonly RenderTexture[] m_HistoryTextures = new RenderTexture[2];
46-
readonly RenderTexture[][] m_HistoryTextures = new RenderTexture[2][];
44+
const int k_NumEyes = 2;
45+
const int k_NumHistoryTextures = 2;
46+
readonly RenderTexture[][] m_HistoryTextures = new RenderTexture[k_NumEyes][];
4747

48-
//int m_HistoryPingPong;
49-
int[] m_HistoryPingPong = new int [2];
48+
int[] m_HistoryPingPong = new int [k_NumEyes];
5049

5150
public TemporalAntialiasing()
5251
{
53-
m_HistoryTextures[0] = new RenderTexture[2];
54-
m_HistoryTextures[1] = new RenderTexture[2];
52+
m_HistoryTextures[(int)Camera.StereoscopicEye.Left] = new RenderTexture[k_NumHistoryTextures];
53+
m_HistoryTextures[(int)Camera.StereoscopicEye.Right] = new RenderTexture[k_NumHistoryTextures];
5554
}
5655

5756
public bool IsSupported()
@@ -132,24 +131,28 @@ public void ConfiguredStereoJitteredProjectionMatrices(PostProcessRenderContext
132131
context.camera.SetStereoProjectionMatrix(eye, jitteredMatrix);
133132
}
134133

134+
// jitter has to be scaled for the actual eye texture size, not just the intermediate texture size
135+
// which could be double-wide in certain stereo rendering scenarios
135136
jitter = new Vector2(jitter.x / context.singleEyeWidth, jitter.y / context.height);
136137
camera.useJitteredProjectionMatrixForTransparentRendering = false;
137138
}
138139

139140
RenderTexture CheckHistory(int id, PostProcessRenderContext context)
140141
{
141-
//var rt = m_HistoryTextures[id];
142-
var rt = m_HistoryTextures[context.activeEye][id];
142+
var rt = m_HistoryTextures[context.xrActiveEye][id];
143143

144144
if (m_ResetHistory || rt == null || !rt.IsCreated())
145145
{
146146
RenderTexture.ReleaseTemporary(rt);
147147

148148
rt = RenderTexture.GetTemporary(context.width, context.height, 0, context.sourceFormat);
149-
rt.name = "Temporal Anti-aliasing History id #" + id.ToString() + " for eye " + context.activeEye.ToString();
149+
if (XR.XRSettings.isDeviceActive)
150+
rt.name = "Temporal Anti-aliasing History id #" + id.ToString() + " for eye " + context.xrActiveEye.ToString();
151+
else
152+
rt.name = "Temporal Anti-aliasing History id #" + id.ToString();
153+
150154
rt.filterMode = FilterMode.Bilinear;
151-
//m_HistoryTextures[id] = rt;
152-
m_HistoryTextures[context.activeEye][id] = rt;
155+
m_HistoryTextures[context.xrActiveEye][id] = rt;
153156

154157
context.command.BlitFullscreenTriangle(context.source, rt);
155158
}
@@ -158,18 +161,19 @@ RenderTexture CheckHistory(int id, PostProcessRenderContext context)
158161
// On size change, simply copy the old history to the new one. This looks better
159162
// than completely discarding the history and seeing a few aliased frames.
160163
var rt2 = RenderTexture.GetTemporary(context.width, context.height, 0, context.sourceFormat);
161-
//rt2.name = "Temporal Anti-aliasing History";
162-
rt2.name = "Temporal Anti-aliasing History id #" + id.ToString() + " for eye " + context.activeEye.ToString();
164+
if (XR.XRSettings.isDeviceActive)
165+
rt2.name = "Temporal Anti-aliasing History id #" + id.ToString() + " for eye " + context.xrActiveEye.ToString();
166+
else
167+
rt2.name = "Temporal Anti-aliasing History id #" + id.ToString();
168+
163169
rt2.filterMode = FilterMode.Bilinear;
164-
//m_HistoryTextures[id] = rt2;
165-
m_HistoryTextures[context.activeEye][id] = rt2;
170+
m_HistoryTextures[context.xrActiveEye][id] = rt2;
166171

167172
context.command.BlitFullscreenTriangle(rt, rt2);
168173
RenderTexture.ReleaseTemporary(rt);
169174
}
170175

171-
//return m_HistoryTextures[id];
172-
return m_HistoryTextures[context.activeEye][id];
176+
return m_HistoryTextures[context.xrActiveEye][id];
173177
}
174178

175179
internal void Render(PostProcessRenderContext context)
@@ -179,12 +183,10 @@ internal void Render(PostProcessRenderContext context)
179183
var cmd = context.command;
180184
cmd.BeginSample("TemporalAntialiasing");
181185

182-
//int pp = m_HistoryPingPong;
183-
int pp = m_HistoryPingPong[context.activeEye];
186+
int pp = m_HistoryPingPong[context.xrActiveEye];
184187
var historyRead = CheckHistory(++pp % 2, context);
185188
var historyWrite = CheckHistory(++pp % 2, context);
186-
//m_HistoryPingPong = ++pp % 2;
187-
m_HistoryPingPong[context.activeEye] = ++pp % 2;
189+
m_HistoryPingPong[context.xrActiveEye] = ++pp % 2;
188190

189191
const float kMotionAmplification = 100f * 60f;
190192
sheet.properties.SetVector(ShaderIDs.Jitter, jitter);

PostProcessing/Runtime/PostProcessLayer.cs

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -256,14 +256,9 @@ void OnPreCull()
256256
// when ResetProjectionMatrix() is called and will break transparent rendering if TAA
257257
// is switched off and the FOV or any other camera property changes.
258258
m_Camera.ResetProjectionMatrix();
259-
//m_Camera.nonJitteredProjectionMatrix = m_Camera.projectionMatrix;
259+
m_Camera.nonJitteredProjectionMatrix = m_Camera.projectionMatrix;
260260
if (XR.XRSettings.isDeviceActive)
261-
{
262261
m_Camera.ResetStereoProjectionMatrices();
263-
//m_Camera.nonJitteredProjectionMatrix = m_Camera.GetStereoProjectionMatrix(Camera.StereoscopicEye.Left);
264-
}
265-
else
266-
m_Camera.nonJitteredProjectionMatrix = m_Camera.projectionMatrix;
267262

268263
context.Reset();
269264
context.camera = m_Camera;
@@ -372,28 +367,30 @@ void OnPreCull()
372367
void OnPreRender()
373368
{
374369
// Unused in scriptable render pipelines
375-
// we only execute this for right eye
370+
// Only needed for multi-pass stereo right eye
376371
if (RuntimeUtilities.scriptableRenderPipelineActive ||
377372
(m_Camera.stereoActiveEye != Camera.MonoOrStereoscopicEye.Right))
378373
return;
379374

375+
// Probably should re-factor everything below to share common code between OnPreCull
376+
// and OnPreRender.
377+
380378
var context = m_CurrentContext;
381379
var sourceFormat = m_Camera.allowHDR ? RenderTextureFormat.DefaultHDR : RenderTextureFormat.Default;
382380

383-
// Resets the projection matrix from previous frame in case TAA was enabled.
384-
// We also need to force reset the non-jittered projection matrix here as it's not done
385-
// when ResetProjectionMatrix() is called and will break transparent rendering if TAA
386-
// is switched off and the FOV or any other camera property changes.
387-
//m_Camera.ResetProjectionMatrix();
388-
//m_Camera.nonJitteredProjectionMatrix = m_Camera.projectionMatrix;
389-
//m_Camera.nonJitteredProjectionMatrix = m_Camera.GetStereoNonJitteredProjectionMatrix(Camera.StereoscopicEye.Right);
390-
//m_Camera.projectionMatrix = m_Camera.GetStereoProjectionMatrix(Camera.StereoscopicEye.Right);
391-
392381
context.Reset();
393382
context.camera = m_Camera;
394383
context.sourceFormat = sourceFormat;
395384

396-
// TODO: I assume these are all executed for left eye before coming here
385+
// I don't think I need the big hammer of clearing out the command buffers totally for the right eye.
386+
// Really, I just need to clear out any effect that relies on history frames (like TAA), since each
387+
// eye will have their own retained state. The command buffers might be re-usable, assuming
388+
// they start out being sourced from camera targets.
389+
// Also, clearing of these buffers is dependent on their use being completed on the left eye first.
390+
// Materials and render textures are what I am curious about, as far as re-use.
391+
// There is also the possibility of separating out the history-based effects into their own command
392+
// buffers, and adding those per eye. I don't think the bang-for-buck is there for that though,
393+
// especially since single-pass is the path moving forward.
397394
m_LegacyCmdBufferBeforeReflections.Clear();
398395
m_LegacyCmdBufferOpaque.Clear();
399396
m_LegacyCmdBuffer.Clear();
@@ -421,6 +418,7 @@ void OnPreRender()
421418
opaqueOnlyEffects += isFogActive ? 1 : 0;
422419
opaqueOnlyEffects += hasCustomOpaqueOnlyEffects ? 1 : 0;
423420

421+
// This works on right eye because it is resolved/populated at runtime
424422
var cameraTarget = new RenderTargetIdentifier(BuiltinRenderTextureType.CameraTarget);
425423

426424
if (opaqueOnlyEffects > 0)

PostProcessing/Runtime/PostProcessRenderContext.cs

Lines changed: 24 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -24,28 +24,22 @@ public Camera camera
2424
RenderTextureDescriptor xrDesc = XR.XRSettings.eyeTextureDesc;
2525
m_width = xrDesc.width;
2626
m_height = xrDesc.height;
27-
// we should create eye-specific params
28-
// in order to support knowing the size of each eye
27+
28+
m_xrSinglePass = (xrDesc.vrUsage == VRTextureUsage.TwoEyes);
2929

3030
if (camera.stereoActiveEye == Camera.MonoOrStereoscopicEye.Right)
31-
m_activeEye = (int)Camera.StereoscopicEye.Right;
31+
m_xrActiveEye = (int)Camera.StereoscopicEye.Right;
3232

33-
if ((xrDesc.vrUsage == VRTextureUsage.TwoEyes) &&
34-
(xrDesc.dimension != TextureDimension.Tex2DArray))
35-
{
36-
m_singleEyeWidth = m_width / 2;
37-
}
33+
if (m_xrSinglePass && (xrDesc.dimension != TextureDimension.Tex2DArray))
34+
m_xrSingleEyeWidth = m_width / 2;
3835
else
39-
m_singleEyeWidth = m_width;
40-
41-
// if the intermediate texture is being used for two eyes, that's single pass
42-
m_xrSinglePass = (xrDesc.vrUsage == VRTextureUsage.TwoEyes);
36+
m_xrSingleEyeWidth = m_width;
4337
}
4438
else
4539
{
4640
m_width = m_camera.pixelWidth;
4741
m_height = m_camera.pixelHeight;
48-
m_singleEyeWidth = m_width;
42+
m_xrSingleEyeWidth = m_width;
4943
}
5044
}
5145
}
@@ -96,18 +90,27 @@ public int height
9690
get { return m_height; }
9791
}
9892

99-
private int m_singleEyeWidth;
100-
public int singleEyeWidth
101-
{
102-
get { return m_singleEyeWidth; }
103-
}
104-
93+
// Is XR running in single-pass stereo mode?
10594
private bool m_xrSinglePass;
10695
public bool xrSinglePass
10796
{
10897
get { return m_xrSinglePass; }
10998
}
11099

100+
// Current active rendering eye (for XR)
101+
private int m_xrActiveEye;
102+
public int xrActiveEye
103+
{
104+
get { return m_xrActiveEye; }
105+
}
106+
107+
// Current single eye width in pixels (for XR)
108+
private int m_xrSingleEyeWidth;
109+
public int singleEyeWidth
110+
{
111+
get { return m_xrSingleEyeWidth; }
112+
}
113+
111114
// Are we currently rendering in the scene view?
112115
public bool isSceneView { get; internal set; }
113116

@@ -118,21 +121,15 @@ public bool xrSinglePass
118121
// to do temporal reprojection (see: Depth of Field)
119122
public TemporalAntialiasing temporalAntialiasing { get; internal set; }
120123

121-
private int m_activeEye;
122-
public int activeEye
123-
{
124-
get { return m_activeEye; }
125-
}
126-
127124
public void Reset()
128125
{
129126
m_camera = null;
130127
m_width = 0;
131128
m_height = 0;
132129

133-
m_singleEyeWidth = 0;
134-
m_activeEye = (int)Camera.StereoscopicEye.Left;
135130
m_xrSinglePass = false;
131+
m_xrActiveEye = (int)Camera.StereoscopicEye.Left;
132+
m_xrSingleEyeWidth = 0;
136133

137134
command = null;
138135
source = 0;

PostProcessing/Runtime/Utils/RuntimeUtilities.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,9 @@ public static void CopyTexture(CommandBuffer cmd, RenderTargetIdentifier source,
192192
cmd.BlitFullscreenTriangle(source, destination);
193193
}
194194

195+
// TODO: Generalize the GetTemporaryRT and Blit commands in order to support
196+
// RT Arrays for Stereo Instancing/MultiView
197+
195198
#endregion
196199

197200
#region Unity specifics

0 commit comments

Comments
 (0)