@@ -50,13 +50,24 @@ enum Pass
5050
5151 // Ping-pong between two history textures as we can't read & write the same target in the
5252 // same pass
53- readonly RenderTexture [ ] m_CoCHistoryTextures = new RenderTexture [ 2 ] ;
54- int m_HistoryPingPong ;
53+ const int k_NumEyes = 2 ;
54+ const int k_NumCoCHistoryTextures = 2 ;
55+ readonly RenderTexture [ ] [ ] m_CoCHistoryTextures = new RenderTexture [ k_NumEyes ] [ ] ;
56+ int [ ] m_HistoryPingPong = new int [ k_NumEyes ] ;
5557
5658 // Height of the 35mm full-frame format (36mm x 24mm)
5759 // TODO: Should be set by a physical camera
5860 const float k_FilmHeight = 0.024f ;
5961
62+ public DepthOfFieldRenderer ( )
63+ {
64+ for ( int eye = 0 ; eye < k_NumEyes ; eye ++ )
65+ {
66+ m_CoCHistoryTextures [ eye ] = new RenderTexture [ k_NumCoCHistoryTextures ] ;
67+ m_HistoryPingPong [ eye ] = 0 ;
68+ }
69+ }
70+
6071 public override DepthTextureMode GetCameraFlags ( )
6172 {
6273 return DepthTextureMode . Depth ;
@@ -84,19 +95,20 @@ float CalculateMaxCoCRadius(int screenHeight)
8495 return Mathf . Min ( 0.05f , radiusInPixels / screenHeight ) ;
8596 }
8697
87- RenderTexture CheckHistory ( int id , int width , int height , RenderTextureFormat format )
98+ RenderTexture CheckHistory ( int eye , int id , PostProcessRenderContext context , RenderTextureFormat format )
8899 {
89- var rt = m_CoCHistoryTextures [ id ] ;
100+ var rt = m_CoCHistoryTextures [ eye ] [ id ] ;
90101
91- if ( m_ResetHistory || rt == null || ! rt . IsCreated ( ) || rt . width != width || rt . height != height )
102+ if ( m_ResetHistory || rt == null || ! rt . IsCreated ( ) || rt . width != context . width || rt . height != context . height )
92103 {
93104 RenderTexture . ReleaseTemporary ( rt ) ;
94105
95- rt = RenderTexture . GetTemporary ( width , height , 0 , format ) ;
96- rt . name = "CoC History" ;
106+ // TODO: The CoCCalculation CoCTex uses RenderTextureReadWrite.Linear, why isn't this?
107+ rt = context . GetScreenSpaceTemporaryRT ( 0 , format ) ;
108+ rt . name = "CoC History, Eye: " + eye + ", ID: " + id ;
97109 rt . filterMode = FilterMode . Bilinear ;
98110 rt . Create ( ) ;
99- m_CoCHistoryTextures [ id ] = rt ;
111+ m_CoCHistoryTextures [ eye ] [ id ] = rt ;
100112 }
101113
102114 return rt ;
@@ -116,9 +128,9 @@ public override void Render(PostProcessRenderContext context)
116128 // Material setup
117129 var f = settings . focalLength . value / 1000f ;
118130 var s1 = Mathf . Max ( settings . focusDistance . value , f ) ;
119- var aspect = ( float ) context . width / ( float ) context . height ;
131+ var aspect = ( float ) context . screenWidth / ( float ) context . screenHeight ;
120132 var coeff = f * f / ( settings . aperture . value * ( s1 - f ) * k_FilmHeight * 2 ) ;
121- var maxCoC = CalculateMaxCoCRadius ( context . height ) ;
133+ var maxCoC = CalculateMaxCoCRadius ( context . screenHeight ) ;
122134
123135 var sheet = context . propertySheets . Get ( context . resources . shaders . depthOfField ) ;
124136 sheet . properties . Clear ( ) ;
@@ -132,7 +144,7 @@ public override void Render(PostProcessRenderContext context)
132144 cmd . BeginSample ( "DepthOfField" ) ;
133145
134146 // CoC calculation pass
135- cmd . GetTemporaryRT ( ShaderIDs . CoCTex , context . width , context . height , 0 , FilterMode . Bilinear , cocFormat , RenderTextureReadWrite . Linear ) ;
147+ context . GetScreenSpaceTemporaryRT ( cmd , ShaderIDs . CoCTex , 0 , cocFormat , RenderTextureReadWrite . Linear ) ;
136148 cmd . BlitFullscreenTriangle ( BuiltinRenderTextureType . None , ShaderIDs . CoCTex , sheet , ( int ) Pass . CoCCalculation ) ;
137149
138150 // CoC temporal filter pass when TAA is enabled
@@ -144,22 +156,22 @@ public override void Render(PostProcessRenderContext context)
144156
145157 sheet . properties . SetVector ( ShaderIDs . TaaParams , new Vector3 ( jitter . x , jitter . y , blend ) ) ;
146158
147- int pp = m_HistoryPingPong ;
148- var historyRead = CheckHistory ( ++ pp % 2 , context . width , context . height , cocFormat ) ;
149- var historyWrite = CheckHistory ( ++ pp % 2 , context . width , context . height , cocFormat ) ;
150- m_HistoryPingPong = ++ pp % 2 ;
159+ int pp = m_HistoryPingPong [ context . xrActiveEye ] ;
160+ var historyRead = CheckHistory ( context . xrActiveEye , ++ pp % 2 , context , cocFormat ) ;
161+ var historyWrite = CheckHistory ( context . xrActiveEye , ++ pp % 2 , context , cocFormat ) ;
162+ m_HistoryPingPong [ context . xrActiveEye ] = ++ pp % 2 ;
151163
152164 cmd . BlitFullscreenTriangle ( historyRead , historyWrite , sheet , ( int ) Pass . CoCTemporalFilter ) ;
153165 cmd . ReleaseTemporaryRT ( ShaderIDs . CoCTex ) ;
154166 cmd . SetGlobalTexture ( ShaderIDs . CoCTex , historyWrite ) ;
155167 }
156168
157169 // Downsampling and prefiltering pass
158- cmd . GetTemporaryRT ( ShaderIDs . DepthOfFieldTex , context . width / 2 , context . height / 2 , 0 , FilterMode . Bilinear , colorFormat ) ;
170+ context . GetScreenSpaceTemporaryRT ( cmd , ShaderIDs . DepthOfFieldTex , 0 , colorFormat , RenderTextureReadWrite . Default , FilterMode . Bilinear , context . width / 2 , context . height / 2 ) ;
159171 cmd . BlitFullscreenTriangle ( context . source , ShaderIDs . DepthOfFieldTex , sheet , ( int ) Pass . DownsampleAndPrefilter ) ;
160172
161173 // Bokeh simulation pass
162- cmd . GetTemporaryRT ( ShaderIDs . DepthOfFieldTemp , context . width / 2 , context . height / 2 , 0 , FilterMode . Bilinear , colorFormat ) ;
174+ context . GetScreenSpaceTemporaryRT ( cmd , ShaderIDs . DepthOfFieldTemp , 0 , colorFormat , RenderTextureReadWrite . Default , FilterMode . Bilinear , context . width / 2 , context . height / 2 ) ;
163175 cmd . BlitFullscreenTriangle ( ShaderIDs . DepthOfFieldTex , ShaderIDs . DepthOfFieldTemp , sheet , ( int ) Pass . BokehSmallKernel + ( int ) settings . kernelSize . value ) ;
164176
165177 // Postfilter pass
@@ -184,13 +196,16 @@ public override void Render(PostProcessRenderContext context)
184196
185197 public override void Release ( )
186198 {
187- for ( int i = 0 ; i < m_CoCHistoryTextures . Length ; i ++ )
199+ for ( int eye = 0 ; eye < k_NumEyes ; eye ++ )
188200 {
189- RenderTexture . ReleaseTemporary ( m_CoCHistoryTextures [ i ] ) ;
190- m_CoCHistoryTextures [ i ] = null ;
201+ for ( int i = 0 ; i < m_CoCHistoryTextures [ eye ] . Length ; i ++ )
202+ {
203+ RenderTexture . ReleaseTemporary ( m_CoCHistoryTextures [ eye ] [ i ] ) ;
204+ m_CoCHistoryTextures [ eye ] [ i ] = null ;
205+ }
206+ m_HistoryPingPong [ eye ] = 0 ;
191207 }
192208
193- m_HistoryPingPong = 0 ;
194209 ResetHistory ( ) ;
195210 }
196211 }
0 commit comments