Skip to content

Commit 94112a5

Browse files
author
Evergreen
committed
Add Y-Flip Support in SSAO Renderer Feature
1 parent f18770f commit 94112a5

24 files changed

+111
-30
lines changed

Packages/com.unity.render-pipelines.core/Runtime/Utilities/Blitter.cs

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1079,7 +1079,7 @@ public static void BlitCameraTexture(CommandBuffer cmd, RTHandle source, RTHandl
10791079
/// Adds in a <see cref="CommandBuffer"/> a command to copy a camera related texture identified by
10801080
/// its <see cref="RTHandle"/> into a destination render target, using a user material, specific shader pass and specific load / store actions.
10811081
/// </summary>
1082-
/// <remarks>
1082+
/// <remarks>
10831083
/// Camera related textures are created with the <see cref="RenderGraphModule.RenderGraph.CreateTexture"/>
10841084
/// method using <see cref="RenderGraphModule.TextureDesc.TextureDesc(Vector2,bool,bool)"/> or
10851085
/// <see cref="RenderGraphModule.TextureDesc.TextureDesc(ScaleFunc,bool,bool)"/> to
@@ -1093,6 +1093,7 @@ public static void BlitCameraTexture(CommandBuffer cmd, RTHandle source, RTHandl
10931093
/// <param name="cmd">Command Buffer used for recording the action.</param>
10941094
/// <param name="source">RTHandle of the source texture to copy from.</param>
10951095
/// <param name="destination">RTHandle of the destination render target to copy to.</param>
1096+
/// <param name="scaleBias">Scale and bias used to sample the input RTHandle.</param>
10961097
/// <param name="loadAction">Load action to perform on the destination render target prior to the copying.</param>
10971098
/// <param name="storeAction">Store action to perform on the destination render target after the copying.</param>
10981099
/// <param name="material">The material to use for writing to the destination target.</param>
@@ -1108,12 +1109,29 @@ public static void BlitCameraTexture(CommandBuffer cmd, RTHandle source, RTHandl
11081109
/// Blitter.BlitCameraTexture(cmd, source, dest, RenderBufferLoadAction.DontCare, RenderBufferStoreAction.Store, blitMaterial, 0);
11091110
/// ]]></code>
11101111
/// </example>
1111-
public static void BlitCameraTexture(CommandBuffer cmd, RTHandle source, RTHandle destination, RenderBufferLoadAction loadAction, RenderBufferStoreAction storeAction, Material material, int pass)
1112+
public static void BlitCameraTexture(CommandBuffer cmd, RTHandle source, RTHandle destination, Vector4 scaleBias, RenderBufferLoadAction loadAction, RenderBufferStoreAction storeAction, Material material, int pass)
11121113
{
1113-
Vector2 viewportScale = source.useScaling ? new Vector2(source.rtHandleProperties.rtHandleScale.x, source.rtHandleProperties.rtHandleScale.y) : Vector2.one;
11141114
// Will set the correct camera viewport as well.
11151115
CoreUtils.SetRenderTarget(cmd, destination, loadAction, storeAction, ClearFlag.None, Color.clear);
1116-
BlitTexture(cmd, source, viewportScale, material, pass);
1116+
BlitTexture(cmd, source, scaleBias, material, pass);
1117+
}
1118+
1119+
/// <summary>
1120+
/// Blit a RTHandle to another RTHandle.
1121+
/// This will properly account for partial usage (in term of resolution) of the texture for the current viewport.
1122+
/// This overloads allows the user to override the default blit shader
1123+
/// </summary>
1124+
/// <param name="cmd">Command Buffer used for rendering.</param>
1125+
/// <param name="source">Source RTHandle.</param>
1126+
/// <param name="destination">Destination RTHandle.</param>
1127+
/// <param name="loadAction">Load action to perform on the destination render target prior to the copying.</param>
1128+
/// <param name="storeAction">Store action to perform on the destination render target after the copying.</param>
1129+
/// <param name="material">The material to use for writing to the destination target.</param>
1130+
/// <param name="pass">The index of the pass to use in the material's shader.</param>
1131+
public static void BlitCameraTexture(CommandBuffer cmd, RTHandle source, RTHandle destination, RenderBufferLoadAction loadAction, RenderBufferStoreAction storeAction, Material material, int pass)
1132+
{
1133+
Vector2 viewportScale = source.useScaling ? new Vector2(source.rtHandleProperties.rtHandleScale.x, source.rtHandleProperties.rtHandleScale.y) : Vector2.one;
1134+
BlitCameraTexture(cmd, source, destination, viewportScale, loadAction, storeAction, material, pass);
11171135
}
11181136

11191137
/// <summary>

Packages/com.unity.render-pipelines.universal/Runtime/Passes/ScreenSpaceAmbientOcclusionPass.cs

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Runtime.CompilerServices;
23
using UnityEngine.Experimental.Rendering;
34
using UnityEngine.Rendering.RenderGraphModule;
45

@@ -312,6 +313,7 @@ private class SSAOPassData
312313
internal TextureHandle finalTexture;
313314
internal TextureHandle blurTexture;
314315
internal TextureHandle cameraNormalsTexture;
316+
internal UniversalCameraData cameraData;
315317
}
316318

317319
private void InitSSAOPassData(ref SSAOPassData data)
@@ -322,6 +324,26 @@ private void InitSSAOPassData(ref SSAOPassData data)
322324
data.directLightingStrength = m_CurrentSettings.DirectLightingStrength;
323325
}
324326

327+
private static Vector4 ComputeScaleBias(UniversalCameraData cameraData, RTHandle source, RTHandle destination)
328+
{
329+
Vector2 viewportScale;
330+
if (source.useScaling)
331+
{
332+
viewportScale.x = source.rtHandleProperties.rtHandleScale.x;
333+
viewportScale.y = source.rtHandleProperties.rtHandleScale.y;
334+
}
335+
else
336+
{
337+
viewportScale = Vector2.one;
338+
}
339+
340+
bool yFlip = cameraData.IsHandleYFlipped(source) != cameraData.IsHandleYFlipped(destination);
341+
if (yFlip)
342+
return new Vector4(viewportScale.x, -viewportScale.y, 0, viewportScale.y);
343+
else
344+
return new Vector4(viewportScale.x, viewportScale.y, 0, 0);
345+
}
346+
325347
public override void RecordRenderGraph(RenderGraph renderGraph, ContextContainer frameData)
326348
{
327349
UniversalCameraData cameraData = frameData.Get<UniversalCameraData>();
@@ -353,6 +375,7 @@ public override void RecordRenderGraph(RenderGraph renderGraph, ContextContainer
353375
passData.AOTexture = aoTexture;
354376
passData.finalTexture = finalTexture;
355377
passData.blurTexture = blurTexture;
378+
passData.cameraData = cameraData;
356379

357380
// Declare input textures
358381
builder.UseTexture(passData.AOTexture, AccessFlags.ReadWrite);
@@ -379,7 +402,7 @@ public override void RecordRenderGraph(RenderGraph renderGraph, ContextContainer
379402
// The global SSAO texture only needs to be set if After Opaque is disabled...
380403
if (!passData.afterOpaque && finalTexture.IsValid())
381404
{
382-
builder.UseTexture(passData.finalTexture, AccessFlags.ReadWrite);
405+
builder.UseTexture(passData.finalTexture, AccessFlags.Write);
383406
builder.SetGlobalTextureAfterPass(finalTexture, s_SSAOFinalTextureID);
384407
}
385408

@@ -399,24 +422,28 @@ public override void RecordRenderGraph(RenderGraph renderGraph, ContextContainer
399422
Blitter.BlitCameraTexture(cmd, data.AOTexture, data.AOTexture, RenderBufferLoadAction.DontCare, RenderBufferStoreAction.Store, data.material, (int) ShaderPasses.AmbientOcclusion);
400423

401424
// Blur passes
425+
Vector4 viewScaleBias;
402426
switch (data.BlurQuality)
403427
{
404428
// Bilateral
405429
case ScreenSpaceAmbientOcclusionSettings.BlurQualityOptions.High:
406430
Blitter.BlitCameraTexture(cmd, data.AOTexture, data.blurTexture, RenderBufferLoadAction.DontCare, RenderBufferStoreAction.Store, data.material, (int) ShaderPasses.BilateralBlurHorizontal);
407431
Blitter.BlitCameraTexture(cmd, data.blurTexture, data.AOTexture, RenderBufferLoadAction.DontCare, RenderBufferStoreAction.Store, data.material, (int) ShaderPasses.BilateralBlurVertical);
408-
Blitter.BlitCameraTexture(cmd, data.AOTexture, data.finalTexture, finalLoadAction, RenderBufferStoreAction.Store, data.material, (int) (data.afterOpaque ? ShaderPasses.BilateralAfterOpaque : ShaderPasses.BilateralBlurFinal));
432+
viewScaleBias = ComputeScaleBias(data.cameraData, data.AOTexture, data.finalTexture);
433+
Blitter.BlitCameraTexture(cmd, data.AOTexture, data.finalTexture, viewScaleBias, finalLoadAction, RenderBufferStoreAction.Store, data.material, (int) (data.afterOpaque ? ShaderPasses.BilateralAfterOpaque : ShaderPasses.BilateralBlurFinal));
409434
break;
410435

411436
// Gaussian
412437
case ScreenSpaceAmbientOcclusionSettings.BlurQualityOptions.Medium:
413438
Blitter.BlitCameraTexture(cmd, data.AOTexture, data.blurTexture, RenderBufferLoadAction.Load, RenderBufferStoreAction.Store, data.material, (int) ShaderPasses.GaussianBlurHorizontal);
414-
Blitter.BlitCameraTexture(cmd, data.blurTexture, data.finalTexture, finalLoadAction, RenderBufferStoreAction.Store, data.material, (int) (data.afterOpaque ? ShaderPasses.GaussianAfterOpaque : ShaderPasses.GaussianBlurVertical));
439+
viewScaleBias = ComputeScaleBias(data.cameraData, data.blurTexture, data.finalTexture);
440+
Blitter.BlitCameraTexture(cmd, data.blurTexture, data.finalTexture, viewScaleBias, finalLoadAction, RenderBufferStoreAction.Store, data.material, (int) (data.afterOpaque ? ShaderPasses.GaussianAfterOpaque : ShaderPasses.GaussianBlurVertical));
415441
break;
416442

417443
// Kawase
418444
case ScreenSpaceAmbientOcclusionSettings.BlurQualityOptions.Low:
419-
Blitter.BlitCameraTexture(cmd, data.AOTexture, data.finalTexture, finalLoadAction, RenderBufferStoreAction.Store, data.material, (int) (data.afterOpaque ? ShaderPasses.KawaseAfterOpaque : ShaderPasses.KawaseBlur));
445+
viewScaleBias = ComputeScaleBias(data.cameraData, data.AOTexture, data.finalTexture);
446+
Blitter.BlitCameraTexture(cmd, data.AOTexture, data.finalTexture, viewScaleBias, finalLoadAction, RenderBufferStoreAction.Store, data.material, (int) (data.afterOpaque ? ShaderPasses.KawaseAfterOpaque : ShaderPasses.KawaseBlur));
420447
break;
421448

422449
default:

Packages/com.unity.render-pipelines.universal/Runtime/UniversalRendererRenderGraph.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1055,7 +1055,21 @@ private void OnMainRendering(RenderGraph renderGraph, ScriptableRenderContext co
10551055
bool setGlobalTextures = isLastPass && hasFullPrepass;
10561056

10571057
if (isDepthNormalPrepass)
1058+
{
1059+
// We set camera properties once per execution of the URP render graph, y-flip status is determined based on whether we are rendering to the backbuffer or not.
1060+
// DepthNormal prepass always renders to an intermediate render target which is assumed to be y-flipped by all other logic in our codebase.
1061+
// Therefore we need to set the camera properties for the DepthNormal to be consistent with rendering to an intermediate render target.
1062+
if (resourceData.isActiveTargetBackBuffer)
1063+
{
1064+
SetupRenderGraphCameraProperties(renderGraph, false);
1065+
}
10581066
DepthNormalPrepassRender(renderGraph, renderPassInputs, depthTarget, batchLayerMask, setGlobalDepth, setGlobalTextures);
1067+
// Restore camera properties for the rest of the render graph execution.
1068+
if (resourceData.isActiveTargetBackBuffer)
1069+
{
1070+
SetupRenderGraphCameraProperties(renderGraph, true);
1071+
}
1072+
}
10591073
else
10601074
m_DepthPrepass.Render(renderGraph, frameData, ref depthTarget, batchLayerMask, setGlobalDepth);
10611075

Tests/SRPTests/Projects/UniversalGraphicsTest_Foundation/Assets/Scenes/200_Assets/Renderers/DepthNormalsRendererDeferred.asset

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ MonoBehaviour:
5959
m_DepthAttachmentFormat: 0
6060
m_DepthTextureFormat: 0
6161
m_AccurateGbufferNormals: 0
62-
m_IntermediateTextureMode: 1
62+
m_IntermediateTextureMode: 0
6363
--- !u!114 &7456660863052077093
6464
MonoBehaviour:
6565
m_ObjectHideFlags: 0

Tests/SRPTests/Projects/UniversalGraphicsTest_Foundation/Assets/Scenes/200_Assets/Renderers/DepthNormalsRendererDeferred_AccurateGBufferNormals.asset

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ MonoBehaviour:
5959
m_DepthAttachmentFormat: 0
6060
m_DepthTextureFormat: 0
6161
m_AccurateGbufferNormals: 1
62-
m_IntermediateTextureMode: 1
62+
m_IntermediateTextureMode: 0
6363
--- !u!114 &7456660863052077093
6464
MonoBehaviour:
6565
m_ObjectHideFlags: 0

Tests/SRPTests/Projects/UniversalGraphicsTest_Foundation/Assets/Scenes/200_Assets/Renderers/DepthNormalsRendererForward.asset

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ MonoBehaviour:
5959
m_DepthAttachmentFormat: 0
6060
m_DepthTextureFormat: 0
6161
m_AccurateGbufferNormals: 0
62-
m_IntermediateTextureMode: 1
62+
m_IntermediateTextureMode: 0
6363
--- !u!114 &7456660863052077093
6464
MonoBehaviour:
6565
m_ObjectHideFlags: 0

Tests/SRPTests/Projects/UniversalGraphicsTest_Foundation/Assets/Scenes/200_Assets/Renderers/DepthRendererDeferred.asset

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ MonoBehaviour:
5959
m_DepthAttachmentFormat: 0
6060
m_DepthTextureFormat: 0
6161
m_AccurateGbufferNormals: 0
62-
m_IntermediateTextureMode: 1
62+
m_IntermediateTextureMode: 0
6363
--- !u!114 &1912622647174990926
6464
MonoBehaviour:
6565
m_ObjectHideFlags: 0

Tests/SRPTests/Projects/UniversalGraphicsTest_Foundation/Assets/Scenes/200_Assets/Renderers/DepthRendererForward.asset

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ MonoBehaviour:
5959
m_DepthAttachmentFormat: 0
6060
m_DepthTextureFormat: 0
6161
m_AccurateGbufferNormals: 0
62-
m_IntermediateTextureMode: 1
62+
m_IntermediateTextureMode: 0
6363
--- !u!114 &1912622647174990926
6464
MonoBehaviour:
6565
m_ObjectHideFlags: 0

Tests/SRPTests/Projects/UniversalGraphicsTest_Foundation/Assets/Scenes/200_Assets/Renderers/SSAO_DeferredRenderer_AfterOpaque.asset

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ MonoBehaviour:
5959
m_DepthAttachmentFormat: 0
6060
m_DepthTextureFormat: 0
6161
m_AccurateGbufferNormals: 0
62-
m_IntermediateTextureMode: 1
62+
m_IntermediateTextureMode: 0
6363
--- !u!114 &3778245575090843755
6464
MonoBehaviour:
6565
m_ObjectHideFlags: 0

Tests/SRPTests/Projects/UniversalGraphicsTest_Foundation/Assets/Scenes/200_Assets/Renderers/SSAO_DeferredRenderer_Output_SSAO.asset

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ MonoBehaviour:
7878
m_DepthAttachmentFormat: 0
7979
m_DepthTextureFormat: 0
8080
m_AccurateGbufferNormals: 0
81-
m_IntermediateTextureMode: 1
81+
m_IntermediateTextureMode: 0
8282
--- !u!114 &3778245575090843755
8383
MonoBehaviour:
8484
m_ObjectHideFlags: 0

0 commit comments

Comments
 (0)