Skip to content

Commit e76f7aa

Browse files
svc-reach-platform-supportEvergreen
authored andcommitted
[Port] [6000.4] [HDRP] Fix DLSS & FSR2 black screen when CustomPass is used
1 parent 4882743 commit e76f7aa

File tree

10 files changed

+1716
-7
lines changed

10 files changed

+1716
-7
lines changed

Packages/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/RenderPass/DLSSPass.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -555,8 +555,11 @@ private void InternalNVIDIARender(in DLSSPass.Parameters parameters, UpscalerRes
555555
dlssViewData.presetDLAA = parameters.drsSettings.DLSSRenderPresetForDLAA;
556556

557557
dlssViewData.inputRes = new UpscalerResolution() { width = (uint)parameters.hdCamera.actualWidth, height = (uint)parameters.hdCamera.actualHeight };
558-
dlssViewData.outputRes = new UpscalerResolution() { width = (uint)DynamicResolutionHandler.instance.finalViewport.x, height = (uint)DynamicResolutionHandler.instance.finalViewport.y };
559-
558+
dlssViewData.outputRes = new UpscalerResolution() {
559+
width = (uint)parameters.hdCamera.finalViewport.width,
560+
height = (uint)parameters.hdCamera.finalViewport.height
561+
};
562+
560563
dlssViewData.jitterX = -parameters.hdCamera.taaJitter.x;
561564
dlssViewData.jitterY = -parameters.hdCamera.taaJitter.y;
562565
dlssViewData.reset = parameters.resetHistory;

Packages/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/RenderPass/FSR2Pass.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -428,7 +428,10 @@ private void InternalAMDRender(
428428
bool useCameraCustomAttributes = parameters.hdCamera.fidelityFX2SuperResolutionUseCustomAttributes;
429429
var fsr2ViewData = new Fsr2ViewData();
430430
fsr2ViewData.inputRes = new UpscalerResolution() { width = (uint)parameters.hdCamera.actualWidth, height = (uint)parameters.hdCamera.actualHeight };
431-
fsr2ViewData.outputRes = new UpscalerResolution() { width = (uint)DynamicResolutionHandler.instance.finalViewport.x, height = (uint)DynamicResolutionHandler.instance.finalViewport.y };
431+
fsr2ViewData.outputRes = new UpscalerResolution() {
432+
width = (uint)parameters.hdCamera.finalViewport.width,
433+
height = (uint)parameters.hdCamera.finalViewport.height
434+
};
432435
fsr2ViewData.jitterX = parameters.hdCamera.taaJitter.x;
433436
fsr2ViewData.jitterY = parameters.hdCamera.taaJitter.y;
434437
fsr2ViewData.reset = parameters.hdCamera.isFirstFrame;

Tests/SRPTests/Projects/HDRP_Tests/Assets/GraphicTests/Common/CustomPass.meta

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
using Unity.Mathematics;
2+
using UnityEngine;
3+
using UnityEngine.Experimental.Rendering;
4+
using UnityEngine.Rendering;
5+
using UnityEngine.Rendering.HighDefinition;
6+
7+
// This is a customer-reported render pass breaking DLSS & FSR2 output.
8+
// The reason being the hdCamera used for the custom pass leading to
9+
// global state in the DynamicResolutionHandler to be set by the custom pass
10+
// and consumed by the upscaler passes right after, resulting in invalid
11+
// output resolution leading to a black screen.
12+
public class TestCustomRenderPassBreakingDLSSAndFSR2 : CustomPass
13+
{
14+
private Camera _camera;
15+
[Header("View")] [SerializeField] private LayerMask _cullingMask;
16+
[SerializeField] private readonly CullMode _cullMode = CullMode.Front;
17+
18+
private RenderTextureDescriptor _depthBufferDescriptor;
19+
[SerializeField] private bool _depthClip;
20+
21+
private RTHandle _maskBuffer;
22+
23+
[SerializeField] [Tooltip("Offset Geometry along normal")]
24+
private float _normalBias;
25+
26+
[SerializeField] [Range(0, 1)] [Tooltip("Distance % from camera far plane.")]
27+
private readonly float _range = 0.5f;
28+
29+
[Header("Rendering")] [SerializeField] private readonly TextureResolution _resolution = TextureResolution._256;
30+
[SerializeField] private Vector3 _rotation;
31+
32+
[Header("Shadow Map")] [SerializeField]
33+
private readonly float _slopeBias = 2f;
34+
35+
[SerializeField] private float _snapToGrid;
36+
[SerializeField] private float _varianceBias;
37+
38+
protected override bool executeInSceneView => false;
39+
40+
protected override void Setup(ScriptableRenderContext renderContext, CommandBuffer cmd)
41+
{
42+
_depthBufferDescriptor = new RenderTextureDescriptor((int)_resolution, (int)_resolution, GraphicsFormat.None,
43+
GraphicsFormat.D32_SFloat)
44+
{
45+
autoGenerateMips = false,
46+
enableRandomWrite = false
47+
};
48+
49+
_maskBuffer = RTHandles.Alloc((int)_resolution, (int)_resolution, colorFormat: GraphicsFormat.D32_SFloat,
50+
autoGenerateMips: false, isShadowMap: true);
51+
52+
_camera = new GameObject { hideFlags = HideFlags.HideAndDontSave }.AddComponent<Camera>();
53+
_camera.cullingMask = _cullingMask;
54+
_camera.enabled = false;
55+
_camera.orthographic = true;
56+
_camera.targetTexture = _maskBuffer.rt;
57+
}
58+
59+
protected override void Cleanup()
60+
{
61+
CoreUtils.Destroy(_camera.gameObject);
62+
RTHandles.Release(_maskBuffer);
63+
}
64+
65+
protected override void Execute(CustomPassContext ctx)
66+
{
67+
if (!UpdateCamera(ctx.hdCamera.camera)) return;
68+
if (!_camera.TryGetCullingParameters(out var cullingParameters)) return;
69+
70+
cullingParameters.cullingOptions = CullingOptions.ShadowCasters;
71+
ctx.cullingResults = ctx.renderContext.Cull(ref cullingParameters);
72+
73+
ctx.cmd.GetTemporaryRT(ShaderIDs._TemporaryDepthBuffer, _depthBufferDescriptor);
74+
CoreUtils.SetRenderTarget(ctx.cmd, ShaderIDs._TemporaryDepthBuffer, ClearFlag.Depth);
75+
ctx.cmd.SetGlobalDepthBias(1.0f, _slopeBias);
76+
CustomPassUtils.RenderDepthFromCamera(ctx, _camera, _camera.cullingMask,
77+
overrideRenderState: new RenderStateBlock(RenderStateMask.Depth | RenderStateMask.Raster)
78+
{
79+
depthState = new DepthState(true, CompareFunction.LessEqual),
80+
rasterState = new RasterState(_cullMode, 0, 0, _depthClip)
81+
});
82+
83+
ctx.cmd.CopyTexture(ShaderIDs._TemporaryDepthBuffer, _maskBuffer);
84+
85+
ctx.cmd.ReleaseTemporaryRT(ShaderIDs._TemporaryDepthBuffer);
86+
}
87+
88+
private bool UpdateCamera(Camera camera)
89+
{
90+
if (camera.cameraType != CameraType.Game || !camera.CompareTag("MainCamera"))
91+
return false;
92+
93+
float3 position = camera.transform.position;
94+
if (_snapToGrid > 0)
95+
position = math.round(position * _snapToGrid) / _snapToGrid;
96+
97+
_camera.transform.position = position;
98+
_camera.orthographicSize = _range * camera.farClipPlane;
99+
_camera.nearClipPlane = -_range * camera.farClipPlane;
100+
_camera.farClipPlane = _range * camera.farClipPlane;
101+
_camera.transform.rotation =
102+
Quaternion.FromToRotation(Vector3.forward, Vector3.down) * Quaternion.Euler(_rotation);
103+
104+
return true;
105+
}
106+
107+
public static class ShaderIDs
108+
{
109+
public static readonly int _TemporaryDepthBuffer = Shader.PropertyToID("_TemporaryDepthBuffer");
110+
}
111+
112+
private enum TextureResolution
113+
{
114+
_128 = 128,
115+
_256 = 256,
116+
_512 = 512,
117+
_1024 = 1024,
118+
_2048 = 2048,
119+
_4096 = 4096
120+
}
121+
}

Tests/SRPTests/Projects/HDRP_Tests/Assets/GraphicTests/Common/CustomPass/TestCustomRenderPassBreakingDLSSAndFSR2.cs.meta

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)