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

Commit 933d87e

Browse files
committed
Avoid redundant color buffer restores using new Unity 2018.2 CommandBuffer.SetRenderTarget overloads
1 parent 3df814c commit 933d87e

File tree

2 files changed

+54
-6
lines changed

2 files changed

+54
-6
lines changed

PostProcessing/Runtime/PostProcessLayer.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -379,7 +379,7 @@ void BuildCommandBuffers()
379379
// on tiled GPU as it won't be able to resolve
380380
int tempTarget0 = m_TargetPool.Get();
381381
context.GetScreenSpaceTemporaryRT(cmd, tempTarget0, 0, sourceFormat);
382-
cmd.Blit(cameraTarget, tempTarget0);
382+
cmd.BuiltinBlit(cameraTarget, BuiltinRenderTextureType.CurrentActive);
383383
context.source = tempTarget0;
384384

385385
int tempTarget1 = -1;
@@ -424,7 +424,7 @@ void BuildCommandBuffers()
424424
// tiled GPUs
425425
int tempRt = m_TargetPool.Get();
426426
context.GetScreenSpaceTemporaryRT(m_LegacyCmdBuffer, tempRt, 0, sourceFormat, RenderTextureReadWrite.sRGB);
427-
m_LegacyCmdBuffer.Blit(cameraTarget, tempRt, RuntimeUtilities.copyStdMaterial, stopNaNPropagation ? 1 : 0);
427+
m_LegacyCmdBuffer.BuiltinBlit(cameraTarget, tempRt, RuntimeUtilities.copyStdMaterial, stopNaNPropagation ? 1 : 0);
428428
if (!m_NaNKilled)
429429
m_NaNKilled = stopNaNPropagation;
430430

PostProcessing/Runtime/Utils/RuntimeUtilities.cs

Lines changed: 52 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -146,12 +146,31 @@ public static PropertySheet copySheet
146146
}
147147
}
148148

149+
public static void SetRenderTargetWithLoadStoreAction(this CommandBuffer cmd, RenderTargetIdentifier rt, RenderBufferLoadAction loadAction, RenderBufferStoreAction storeAction)
150+
{
151+
#if UNITY_2018_2_OR_NEWER
152+
cmd.SetRenderTarget(rt, loadAction, storeAction);
153+
#else
154+
cmd.SetRenderTarget(rt);
155+
#endif
156+
}
157+
public static void SetRenderTargetWithLoadStoreAction(this CommandBuffer cmd,
158+
RenderTargetIdentifier color, RenderBufferLoadAction colorLoadAction, RenderBufferStoreAction colorStoreAction,
159+
RenderTargetIdentifier depth, RenderBufferLoadAction depthLoadAction, RenderBufferStoreAction depthStoreAction)
160+
{
161+
#if UNITY_2018_2_OR_NEWER
162+
cmd.SetRenderTarget(color, colorLoadAction, colorStoreAction, depth, depthLoadAction, depthStoreAction);
163+
#else
164+
cmd.SetRenderTarget(color, depth);
165+
#endif
166+
}
167+
149168
// Use a custom blit method to draw a fullscreen triangle instead of a fullscreen quad
150169
// https://michaldrobot.com/2014/04/01/gcn-execution-patterns-in-full-screen-passes/
151170
public static void BlitFullscreenTriangle(this CommandBuffer cmd, RenderTargetIdentifier source, RenderTargetIdentifier destination, bool clear = false)
152171
{
153172
cmd.SetGlobalTexture(ShaderIDs.MainTex, source);
154-
cmd.SetRenderTarget(destination);
173+
cmd.SetRenderTargetWithLoadStoreAction(destination, RenderBufferLoadAction.DontCare, RenderBufferStoreAction.Store);
155174

156175
if (clear)
157176
cmd.ClearRenderTarget(true, true, Color.clear);
@@ -162,7 +181,7 @@ public static void BlitFullscreenTriangle(this CommandBuffer cmd, RenderTargetId
162181
public static void BlitFullscreenTriangle(this CommandBuffer cmd, RenderTargetIdentifier source, RenderTargetIdentifier destination, PropertySheet propertySheet, int pass, bool clear = false)
163182
{
164183
cmd.SetGlobalTexture(ShaderIDs.MainTex, source);
165-
cmd.SetRenderTarget(destination);
184+
cmd.SetRenderTargetWithLoadStoreAction(destination, RenderBufferLoadAction.DontCare, RenderBufferStoreAction.Store);
166185

167186
if (clear)
168187
cmd.ClearRenderTarget(true, true, Color.clear);
@@ -173,10 +192,18 @@ public static void BlitFullscreenTriangle(this CommandBuffer cmd, RenderTargetId
173192
public static void BlitFullscreenTriangle(this CommandBuffer cmd, RenderTargetIdentifier source, RenderTargetIdentifier destination, RenderTargetIdentifier depth, PropertySheet propertySheet, int pass, bool clear = false)
174193
{
175194
cmd.SetGlobalTexture(ShaderIDs.MainTex, source);
176-
cmd.SetRenderTarget(destination, depth);
177-
195+
178196
if (clear)
197+
{
198+
cmd.SetRenderTargetWithLoadStoreAction(destination, RenderBufferLoadAction.DontCare, RenderBufferStoreAction.Store,
199+
depth, RenderBufferLoadAction.DontCare, RenderBufferStoreAction.Store);
179200
cmd.ClearRenderTarget(true, true, Color.clear);
201+
}
202+
else
203+
{
204+
cmd.SetRenderTargetWithLoadStoreAction(destination, RenderBufferLoadAction.DontCare, RenderBufferStoreAction.Store,
205+
depth, RenderBufferLoadAction.Load, RenderBufferStoreAction.Store);
206+
}
180207

181208
cmd.DrawMesh(fullscreenTriangle, Matrix4x4.identity, propertySheet.material, 0, pass, propertySheet.properties);
182209
}
@@ -200,11 +227,32 @@ public static void BlitFullscreenTriangle(Texture source, RenderTexture destinat
200227
if (source != null)
201228
material.SetTexture(ShaderIDs.MainTex, source);
202229

230+
if (destination != null)
231+
destination.DiscardContents(true, false);
232+
203233
Graphics.SetRenderTarget(destination);
204234
Graphics.DrawMeshNow(fullscreenTriangle, Matrix4x4.identity);
205235
RenderTexture.active = oldRt;
206236
}
207237

238+
public static void BuiltinBlit(this CommandBuffer cmd, Rendering.RenderTargetIdentifier source, Rendering.RenderTargetIdentifier dest)
239+
{
240+
#if UNITY_2018_2_OR_NEWER
241+
cmd.SetRenderTarget(dest, RenderBufferLoadAction.DontCare, RenderBufferStoreAction.Store);
242+
dest = BuiltinRenderTextureType.CurrentActive;
243+
#endif
244+
cmd.Blit(source, dest);
245+
}
246+
247+
public static void BuiltinBlit(this CommandBuffer cmd, Rendering.RenderTargetIdentifier source, Rendering.RenderTargetIdentifier dest, Material mat, int pass = 0)
248+
{
249+
#if UNITY_2018_2_OR_NEWER
250+
cmd.SetRenderTarget(dest, RenderBufferLoadAction.DontCare, RenderBufferStoreAction.Store);
251+
dest = BuiltinRenderTextureType.CurrentActive;
252+
#endif
253+
cmd.Blit(source, dest, mat, pass);
254+
}
255+
208256
// Fast basic copy texture if available, falls back to blit copy if not
209257
// Assumes that both textures have the exact same type and format
210258
public static void CopyTexture(CommandBuffer cmd, RenderTargetIdentifier source, RenderTargetIdentifier destination)

0 commit comments

Comments
 (0)