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

Commit 3fbf810

Browse files
authored
Merge pull request #520 from Unity-Technologies/load-store-actions
Load store actions
2 parents 150f6de + 8ee72b7 commit 3fbf810

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
@@ -380,7 +380,7 @@ void BuildCommandBuffers()
380380
// on tiled GPU as it won't be able to resolve
381381
int tempTarget0 = m_TargetPool.Get();
382382
context.GetScreenSpaceTemporaryRT(cmd, tempTarget0, 0, sourceFormat);
383-
cmd.Blit(cameraTarget, tempTarget0);
383+
cmd.BuiltinBlit(cameraTarget, tempTarget0);
384384
context.source = tempTarget0;
385385

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

PostProcessing/Runtime/Utils/RuntimeUtilities.cs

Lines changed: 52 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -195,12 +195,31 @@ public static PropertySheet copySheet
195195
}
196196
}
197197

198+
public static void SetRenderTargetWithLoadStoreAction(this CommandBuffer cmd, RenderTargetIdentifier rt, RenderBufferLoadAction loadAction, RenderBufferStoreAction storeAction)
199+
{
200+
#if UNITY_2018_2_OR_NEWER
201+
cmd.SetRenderTarget(rt, loadAction, storeAction);
202+
#else
203+
cmd.SetRenderTarget(rt);
204+
#endif
205+
}
206+
public static void SetRenderTargetWithLoadStoreAction(this CommandBuffer cmd,
207+
RenderTargetIdentifier color, RenderBufferLoadAction colorLoadAction, RenderBufferStoreAction colorStoreAction,
208+
RenderTargetIdentifier depth, RenderBufferLoadAction depthLoadAction, RenderBufferStoreAction depthStoreAction)
209+
{
210+
#if UNITY_2018_2_OR_NEWER
211+
cmd.SetRenderTarget(color, colorLoadAction, colorStoreAction, depth, depthLoadAction, depthStoreAction);
212+
#else
213+
cmd.SetRenderTarget(color, depth);
214+
#endif
215+
}
216+
198217
// Use a custom blit method to draw a fullscreen triangle instead of a fullscreen quad
199218
// https://michaldrobot.com/2014/04/01/gcn-execution-patterns-in-full-screen-passes/
200219
public static void BlitFullscreenTriangle(this CommandBuffer cmd, RenderTargetIdentifier source, RenderTargetIdentifier destination, bool clear = false)
201220
{
202221
cmd.SetGlobalTexture(ShaderIDs.MainTex, source);
203-
cmd.SetRenderTarget(destination);
222+
cmd.SetRenderTargetWithLoadStoreAction(destination, RenderBufferLoadAction.DontCare, RenderBufferStoreAction.Store);
204223

205224
if (clear)
206225
cmd.ClearRenderTarget(true, true, Color.clear);
@@ -211,7 +230,7 @@ public static void BlitFullscreenTriangle(this CommandBuffer cmd, RenderTargetId
211230
public static void BlitFullscreenTriangle(this CommandBuffer cmd, RenderTargetIdentifier source, RenderTargetIdentifier destination, PropertySheet propertySheet, int pass, bool clear = false)
212231
{
213232
cmd.SetGlobalTexture(ShaderIDs.MainTex, source);
214-
cmd.SetRenderTarget(destination);
233+
cmd.SetRenderTargetWithLoadStoreAction(destination, RenderBufferLoadAction.DontCare, RenderBufferStoreAction.Store);
215234

216235
if (clear)
217236
cmd.ClearRenderTarget(true, true, Color.clear);
@@ -222,10 +241,18 @@ public static void BlitFullscreenTriangle(this CommandBuffer cmd, RenderTargetId
222241
public static void BlitFullscreenTriangle(this CommandBuffer cmd, RenderTargetIdentifier source, RenderTargetIdentifier destination, RenderTargetIdentifier depth, PropertySheet propertySheet, int pass, bool clear = false)
223242
{
224243
cmd.SetGlobalTexture(ShaderIDs.MainTex, source);
225-
cmd.SetRenderTarget(destination, depth);
226-
244+
227245
if (clear)
246+
{
247+
cmd.SetRenderTargetWithLoadStoreAction(destination, RenderBufferLoadAction.DontCare, RenderBufferStoreAction.Store,
248+
depth, RenderBufferLoadAction.DontCare, RenderBufferStoreAction.Store);
228249
cmd.ClearRenderTarget(true, true, Color.clear);
250+
}
251+
else
252+
{
253+
cmd.SetRenderTargetWithLoadStoreAction(destination, RenderBufferLoadAction.DontCare, RenderBufferStoreAction.Store,
254+
depth, RenderBufferLoadAction.Load, RenderBufferStoreAction.Store);
255+
}
229256

230257
cmd.DrawMesh(fullscreenTriangle, Matrix4x4.identity, propertySheet.material, 0, pass, propertySheet.properties);
231258
}
@@ -249,11 +276,32 @@ public static void BlitFullscreenTriangle(Texture source, RenderTexture destinat
249276
if (source != null)
250277
material.SetTexture(ShaderIDs.MainTex, source);
251278

279+
if (destination != null)
280+
destination.DiscardContents(true, false);
281+
252282
Graphics.SetRenderTarget(destination);
253283
Graphics.DrawMeshNow(fullscreenTriangle, Matrix4x4.identity);
254284
RenderTexture.active = oldRt;
255285
}
256286

287+
public static void BuiltinBlit(this CommandBuffer cmd, Rendering.RenderTargetIdentifier source, Rendering.RenderTargetIdentifier dest)
288+
{
289+
#if UNITY_2018_2_OR_NEWER
290+
cmd.SetRenderTarget(dest, RenderBufferLoadAction.DontCare, RenderBufferStoreAction.Store);
291+
dest = BuiltinRenderTextureType.CurrentActive;
292+
#endif
293+
cmd.Blit(source, dest);
294+
}
295+
296+
public static void BuiltinBlit(this CommandBuffer cmd, Rendering.RenderTargetIdentifier source, Rendering.RenderTargetIdentifier dest, Material mat, int pass = 0)
297+
{
298+
#if UNITY_2018_2_OR_NEWER
299+
cmd.SetRenderTarget(dest, RenderBufferLoadAction.DontCare, RenderBufferStoreAction.Store);
300+
dest = BuiltinRenderTextureType.CurrentActive;
301+
#endif
302+
cmd.Blit(source, dest, mat, pass);
303+
}
304+
257305
// Fast basic copy texture if available, falls back to blit copy if not
258306
// Assumes that both textures have the exact same type and format
259307
public static void CopyTexture(CommandBuffer cmd, RenderTargetIdentifier source, RenderTargetIdentifier destination)

0 commit comments

Comments
 (0)