Skip to content

Commit 24cb0a0

Browse files
volkan-unityEvergreen
authored andcommitted
[Port] [6000.0] [HDRP] Fix DLSS & FSR2 black screen when CustomPass is used
1 parent 6a61d6f commit 24cb0a0

10 files changed

Lines changed: 1673 additions & 3 deletions

File tree

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
@@ -526,8 +526,11 @@ private void InternalNVIDIARender(in DLSSPass.Parameters parameters, UpscalerRes
526526
: parameters.drsSettings.DLSSSharpness;
527527

528528
dlssViewData.inputRes = new UpscalerResolution() { width = (uint)parameters.hdCamera.actualWidth, height = (uint)parameters.hdCamera.actualHeight };
529-
dlssViewData.outputRes = new UpscalerResolution() { width = (uint)DynamicResolutionHandler.instance.finalViewport.x, height = (uint)DynamicResolutionHandler.instance.finalViewport.y };
530-
529+
dlssViewData.outputRes = new UpscalerResolution() {
530+
width = (uint)parameters.hdCamera.finalViewport.width,
531+
height = (uint)parameters.hdCamera.finalViewport.height
532+
};
533+
531534
dlssViewData.jitterX = -parameters.hdCamera.taaJitter.x;
532535
dlssViewData.jitterY = -parameters.hdCamera.taaJitter.y;
533536
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: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
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+
[SerializeField] private float _snapToGrid;
33+
[SerializeField] private float _varianceBias;
34+
35+
protected override bool executeInSceneView => false;
36+
37+
protected override void Setup(ScriptableRenderContext renderContext, CommandBuffer cmd)
38+
{
39+
_depthBufferDescriptor = new RenderTextureDescriptor((int)_resolution, (int)_resolution, GraphicsFormat.None,
40+
GraphicsFormat.D32_SFloat)
41+
{
42+
autoGenerateMips = false,
43+
enableRandomWrite = false
44+
};
45+
46+
_maskBuffer = RTHandles.Alloc((int)_resolution, (int)_resolution, colorFormat: GraphicsFormat.D32_SFloat,
47+
autoGenerateMips: false, isShadowMap: true);
48+
49+
_camera = new GameObject { hideFlags = HideFlags.HideAndDontSave }.AddComponent<Camera>();
50+
_camera.cullingMask = _cullingMask;
51+
_camera.enabled = false;
52+
_camera.orthographic = true;
53+
_camera.targetTexture = _maskBuffer.rt;
54+
}
55+
56+
protected override void Cleanup()
57+
{
58+
CoreUtils.Destroy(_camera.gameObject);
59+
RTHandles.Release(_maskBuffer);
60+
}
61+
62+
protected override void Execute(CustomPassContext ctx)
63+
{
64+
if (!UpdateCamera(ctx.hdCamera.camera)) return;
65+
if (!_camera.TryGetCullingParameters(out var cullingParameters)) return;
66+
67+
cullingParameters.cullingOptions = CullingOptions.ShadowCasters;
68+
ctx.cullingResults = ctx.renderContext.Cull(ref cullingParameters);
69+
70+
ctx.cmd.GetTemporaryRT(ShaderIDs._TemporaryDepthBuffer, _depthBufferDescriptor);
71+
CoreUtils.SetRenderTarget(ctx.cmd, ShaderIDs._TemporaryDepthBuffer, ClearFlag.Depth);
72+
CustomPassUtils.RenderDepthFromCamera(ctx, _camera, _camera.cullingMask,
73+
overrideRenderState: new RenderStateBlock(RenderStateMask.Depth | RenderStateMask.Raster)
74+
{
75+
depthState = new DepthState(true, CompareFunction.LessEqual),
76+
rasterState = new RasterState(_cullMode, 0, 0, _depthClip)
77+
});
78+
79+
ctx.cmd.CopyTexture(ShaderIDs._TemporaryDepthBuffer, _maskBuffer);
80+
81+
ctx.cmd.ReleaseTemporaryRT(ShaderIDs._TemporaryDepthBuffer);
82+
}
83+
84+
private bool UpdateCamera(Camera camera)
85+
{
86+
if (camera.cameraType != CameraType.Game || !camera.CompareTag("MainCamera"))
87+
return false;
88+
89+
float3 position = camera.transform.position;
90+
if (_snapToGrid > 0)
91+
position = math.round(position * _snapToGrid) / _snapToGrid;
92+
93+
_camera.transform.position = position;
94+
_camera.orthographicSize = _range * camera.farClipPlane;
95+
_camera.nearClipPlane = -_range * camera.farClipPlane;
96+
_camera.farClipPlane = _range * camera.farClipPlane;
97+
_camera.transform.rotation =
98+
Quaternion.FromToRotation(Vector3.forward, Vector3.down) * Quaternion.Euler(_rotation);
99+
100+
return true;
101+
}
102+
103+
public static class ShaderIDs
104+
{
105+
public static readonly int _TemporaryDepthBuffer = Shader.PropertyToID("_TemporaryDepthBuffer");
106+
}
107+
108+
private enum TextureResolution
109+
{
110+
_128 = 128,
111+
_256 = 256,
112+
_512 = 512,
113+
_1024 = 1024,
114+
_2048 = 2048,
115+
_4096 = 4096
116+
}
117+
}

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)