|
| 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 | +} |
0 commit comments