@@ -41,15 +41,26 @@ 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- readonly RenderTexture [ ] m_HistoryTextures = new RenderTexture [ 2 ] ;
45- int m_HistoryPingPong ;
44+ const int k_NumEyes = 2 ;
45+ const int k_NumHistoryTextures = 2 ;
46+ readonly RenderTexture [ ] [ ] m_HistoryTextures = new RenderTexture [ k_NumEyes ] [ ] ;
47+
48+ int [ ] m_HistoryPingPong = new int [ k_NumEyes ] ;
49+
50+ public TemporalAntialiasing ( )
51+ {
52+ m_HistoryTextures [ ( int ) Camera . StereoscopicEye . Left ] = new RenderTexture [ k_NumHistoryTextures ] ;
53+ m_HistoryTextures [ ( int ) Camera . StereoscopicEye . Right ] = new RenderTexture [ k_NumHistoryTextures ] ;
54+ }
4655
4756 public bool IsSupported ( )
4857 {
4958 return SystemInfo . supportedRenderTargetCount >= 2
5059 && SystemInfo . supportsMotionVectors
51- && SystemInfo . graphicsDeviceType != GraphicsDeviceType . OpenGLES2
52- && ! RuntimeUtilities . isSinglePassStereoEnabled ;
60+ #if ! UNITY_2017_3_OR_NEWER
61+ & & ! RuntimeUtilities . isVREnabled
62+ #endif
63+ && SystemInfo . graphicsDeviceType != GraphicsDeviceType . OpenGLES2 ;
5364 }
5465
5566 internal DepthTextureMode GetCameraFlags ( )
@@ -96,18 +107,70 @@ public Matrix4x4 GetJitteredProjectionMatrix(Camera camera)
96107 return cameraProj ;
97108 }
98109
110+ public void ConfigureJitteredProjectionMatrix ( PostProcessRenderContext context )
111+ {
112+ var camera = context . camera ;
113+ camera . nonJitteredProjectionMatrix = camera . projectionMatrix ;
114+ camera . projectionMatrix = GetJitteredProjectionMatrix ( camera ) ;
115+ camera . useJitteredProjectionMatrixForTransparentRendering = false ;
116+ }
117+
118+ // TODO: We'll probably need to isolate most of this for SRPs
119+ public void ConfigureStereoJitteredProjectionMatrices ( PostProcessRenderContext context )
120+ {
121+ #if UNITY_2017_3_OR_NEWER
122+ var camera = context . camera ;
123+ jitter = GenerateRandomOffset ( ) ;
124+ jitter *= jitterSpread ;
125+
126+ for ( var eye = Camera . StereoscopicEye . Left ; eye <= Camera . StereoscopicEye . Right ; eye ++ )
127+ {
128+ // This saves off the device generated projection matrices as non-jittered
129+ context . camera . CopyStereoDeviceProjectionMatrixToNonJittered ( eye ) ;
130+ var originalProj = context . camera . GetStereoNonJitteredProjectionMatrix ( eye ) ;
131+
132+ // Currently no support for custom jitter func, as VR devices would need to provide
133+ // original projection matrix as input along with jitter
134+ var jitteredMatrix = RuntimeUtilities . GenerateJitteredProjectionMatrixFromOriginal ( context , originalProj , jitter ) ;
135+ context . camera . SetStereoProjectionMatrix ( eye , jitteredMatrix ) ;
136+ }
137+
138+ // jitter has to be scaled for the actual eye texture size, not just the intermediate texture size
139+ // which could be double-wide in certain stereo rendering scenarios
140+ jitter = new Vector2 ( jitter . x / context . xrSingleEyeWidth , jitter . y / context . height ) ;
141+ camera . useJitteredProjectionMatrixForTransparentRendering = false ;
142+ #endif
143+ }
144+
145+ void GenerateHistoryName ( RenderTexture rt , int id , PostProcessRenderContext context )
146+ {
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 ;
159+ }
160+
99161 RenderTexture CheckHistory ( int id , PostProcessRenderContext context )
100162 {
101- var rt = m_HistoryTextures [ id ] ;
163+ var rt = m_HistoryTextures [ context . xrActiveEye ] [ id ] ;
102164
103165 if ( m_ResetHistory || rt == null || ! rt . IsCreated ( ) )
104166 {
105167 RenderTexture . ReleaseTemporary ( rt ) ;
106168
107169 rt = RenderTexture . GetTemporary ( context . width , context . height , 0 , context . sourceFormat ) ;
108- rt . name = "Temporal Anti-aliasing History" ;
170+ GenerateHistoryName ( rt , id , context ) ;
171+
109172 rt . filterMode = FilterMode . Bilinear ;
110- m_HistoryTextures [ id ] = rt ;
173+ m_HistoryTextures [ context . xrActiveEye ] [ id ] = rt ;
111174
112175 context . command . BlitFullscreenTriangle ( context . source , rt ) ;
113176 }
@@ -116,15 +179,16 @@ RenderTexture CheckHistory(int id, PostProcessRenderContext context)
116179 // On size change, simply copy the old history to the new one. This looks better
117180 // than completely discarding the history and seeing a few aliased frames.
118181 var rt2 = RenderTexture . GetTemporary ( context . width , context . height , 0 , context . sourceFormat ) ;
119- rt2 . name = "Temporal Anti-aliasing History" ;
182+ GenerateHistoryName ( rt2 , id , context ) ;
183+
120184 rt2 . filterMode = FilterMode . Bilinear ;
121- m_HistoryTextures [ id ] = rt2 ;
185+ m_HistoryTextures [ context . xrActiveEye ] [ id ] = rt2 ;
122186
123187 context . command . BlitFullscreenTriangle ( rt , rt2 ) ;
124188 RenderTexture . ReleaseTemporary ( rt ) ;
125189 }
126190
127- return m_HistoryTextures [ id ] ;
191+ return m_HistoryTextures [ context . xrActiveEye ] [ id ] ;
128192 }
129193
130194 internal void Render ( PostProcessRenderContext context )
@@ -134,10 +198,10 @@ internal void Render(PostProcessRenderContext context)
134198 var cmd = context . command ;
135199 cmd . BeginSample ( "TemporalAntialiasing" ) ;
136200
137- int pp = m_HistoryPingPong ;
201+ int pp = m_HistoryPingPong [ context . xrActiveEye ] ;
138202 var historyRead = CheckHistory ( ++ pp % 2 , context ) ;
139203 var historyWrite = CheckHistory ( ++ pp % 2 , context ) ;
140- m_HistoryPingPong = ++ pp % 2 ;
204+ m_HistoryPingPong [ context . xrActiveEye ] = ++ pp % 2 ;
141205
142206 const float kMotionAmplification = 100f * 60f ;
143207 sheet . properties . SetVector ( ShaderIDs . Jitter , jitter ) ;
@@ -159,12 +223,17 @@ internal void Release()
159223 {
160224 for ( int i = 0 ; i < m_HistoryTextures . Length ; i ++ )
161225 {
162- RenderTexture . ReleaseTemporary ( m_HistoryTextures [ i ] ) ;
226+ for ( int j = 0 ; j < m_HistoryTextures [ i ] . Length ; j ++ )
227+ {
228+ RenderTexture . ReleaseTemporary ( m_HistoryTextures [ i ] [ j ] ) ;
229+ m_HistoryTextures [ i ] [ j ] = null ;
230+ }
163231 m_HistoryTextures [ i ] = null ;
164232 }
165233
166234 m_SampleIndex = 0 ;
167- m_HistoryPingPong = 0 ;
235+ m_HistoryPingPong [ 0 ] = 0 ;
236+ m_HistoryPingPong [ 1 ] = 0 ;
168237
169238 ResetHistory ( ) ;
170239 }
0 commit comments