@@ -41,15 +41,26 @@ enum Pass
41
41
42
42
// Ping-pong between two history textures as we can't read & write the same target in the
43
43
// 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
+ }
46
55
47
56
public bool IsSupported ( )
48
57
{
49
58
return SystemInfo . supportedRenderTargetCount >= 2
50
59
&& 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 ;
53
64
}
54
65
55
66
internal DepthTextureMode GetCameraFlags ( )
@@ -96,18 +107,70 @@ public Matrix4x4 GetJitteredProjectionMatrix(Camera camera)
96
107
return cameraProj ;
97
108
}
98
109
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
+
99
161
RenderTexture CheckHistory ( int id , PostProcessRenderContext context )
100
162
{
101
- var rt = m_HistoryTextures [ id ] ;
163
+ var rt = m_HistoryTextures [ context . xrActiveEye ] [ id ] ;
102
164
103
165
if ( m_ResetHistory || rt == null || ! rt . IsCreated ( ) )
104
166
{
105
167
RenderTexture . ReleaseTemporary ( rt ) ;
106
168
107
169
rt = RenderTexture . GetTemporary ( context . width , context . height , 0 , context . sourceFormat ) ;
108
- rt . name = "Temporal Anti-aliasing History" ;
170
+ GenerateHistoryName ( rt , id , context ) ;
171
+
109
172
rt . filterMode = FilterMode . Bilinear ;
110
- m_HistoryTextures [ id ] = rt ;
173
+ m_HistoryTextures [ context . xrActiveEye ] [ id ] = rt ;
111
174
112
175
context . command . BlitFullscreenTriangle ( context . source , rt ) ;
113
176
}
@@ -116,15 +179,16 @@ RenderTexture CheckHistory(int id, PostProcessRenderContext context)
116
179
// On size change, simply copy the old history to the new one. This looks better
117
180
// than completely discarding the history and seeing a few aliased frames.
118
181
var rt2 = RenderTexture . GetTemporary ( context . width , context . height , 0 , context . sourceFormat ) ;
119
- rt2 . name = "Temporal Anti-aliasing History" ;
182
+ GenerateHistoryName ( rt2 , id , context ) ;
183
+
120
184
rt2 . filterMode = FilterMode . Bilinear ;
121
- m_HistoryTextures [ id ] = rt2 ;
185
+ m_HistoryTextures [ context . xrActiveEye ] [ id ] = rt2 ;
122
186
123
187
context . command . BlitFullscreenTriangle ( rt , rt2 ) ;
124
188
RenderTexture . ReleaseTemporary ( rt ) ;
125
189
}
126
190
127
- return m_HistoryTextures [ id ] ;
191
+ return m_HistoryTextures [ context . xrActiveEye ] [ id ] ;
128
192
}
129
193
130
194
internal void Render ( PostProcessRenderContext context )
@@ -134,10 +198,10 @@ internal void Render(PostProcessRenderContext context)
134
198
var cmd = context . command ;
135
199
cmd . BeginSample ( "TemporalAntialiasing" ) ;
136
200
137
- int pp = m_HistoryPingPong ;
201
+ int pp = m_HistoryPingPong [ context . xrActiveEye ] ;
138
202
var historyRead = CheckHistory ( ++ pp % 2 , context ) ;
139
203
var historyWrite = CheckHistory ( ++ pp % 2 , context ) ;
140
- m_HistoryPingPong = ++ pp % 2 ;
204
+ m_HistoryPingPong [ context . xrActiveEye ] = ++ pp % 2 ;
141
205
142
206
const float kMotionAmplification = 100f * 60f ;
143
207
sheet . properties . SetVector ( ShaderIDs . Jitter , jitter ) ;
@@ -159,12 +223,17 @@ internal void Release()
159
223
{
160
224
for ( int i = 0 ; i < m_HistoryTextures . Length ; i ++ )
161
225
{
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
+ }
163
231
m_HistoryTextures [ i ] = null ;
164
232
}
165
233
166
234
m_SampleIndex = 0 ;
167
- m_HistoryPingPong = 0 ;
235
+ m_HistoryPingPong [ 0 ] = 0 ;
236
+ m_HistoryPingPong [ 1 ] = 0 ;
168
237
169
238
ResetHistory ( ) ;
170
239
}
0 commit comments