diff --git a/com.unity.render-pipelines.high-definition/CHANGELOG.md b/com.unity.render-pipelines.high-definition/CHANGELOG.md index 1f948672829..53a1a2b8b70 100644 --- a/com.unity.render-pipelines.high-definition/CHANGELOG.md +++ b/com.unity.render-pipelines.high-definition/CHANGELOG.md @@ -55,6 +55,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. - Added controls for the users to manually feed the ray tracing acceleration structure that should be used for a given camera (case 1370678). - Depth of Field is now disabled in orthographic cameras - it was using the hidden perspective settings (case 1372582). - Modified HDRP to use common FSR logic from SRP core. +- Optimized FSR by merging the RCAS logic into the FinalPass shader. ## [13.1.0] - 2021-09-24 diff --git a/com.unity.render-pipelines.high-definition/Runtime/PostProcessing/Shaders/FinalPass.shader b/com.unity.render-pipelines.high-definition/Runtime/PostProcessing/Shaders/FinalPass.shader index d7258edd9a1..7e62aaff3b8 100644 --- a/com.unity.render-pipelines.high-definition/Runtime/PostProcessing/Shaders/FinalPass.shader +++ b/com.unity.render-pipelines.high-definition/Runtime/PostProcessing/Shaders/FinalPass.shader @@ -13,7 +13,7 @@ Shader "Hidden/HDRP/FinalPass" #pragma multi_compile_local_fragment _ APPLY_AFTER_POST #pragma multi_compile_local _ HDR_OUTPUT_REC2020 HDR_OUTPUT_SCRGB - #pragma multi_compile_local_fragment _ CATMULL_ROM_4 BYPASS + #pragma multi_compile_local_fragment _ CATMULL_ROM_4 RCAS BYPASS #define DEBUG_UPSCALE_POINT 0 #include "Packages/com.unity.render-pipelines.core/ShaderLibrary/Common.hlsl" @@ -38,6 +38,15 @@ Shader "Hidden/HDRP/FinalPass" SAMPLER(sampler_LinearClamp); SAMPLER(sampler_LinearRepeat); + #define FSR_INPUT_TEXTURE _InputTexture + #define FSR_INPUT_SAMPLER s_linear_clamp_sampler + #if ENABLE_ALPHA + // When alpha is in use, activate the alpha-passthrough mode in the RCAS implementation. + // When this mode is active, ApplyRCAS returns a four component vector (rgba) instead of a three component vector (rgb). + #define FSR_ENABLE_ALPHA 1 + #endif + #include "Packages/com.unity.render-pipelines.core/Runtime/PostProcessing/Shaders/FSRCommon.hlsl" + float2 _GrainParams; // x: intensity, y: response float4 _GrainTextureParams; // xy: _ScreenSize.xy / GrainTextureSize.xy, zw: (random offset in UVs) * _GrainTextureParams.xy float3 _DitherParams; // xy: _ScreenSize.xy / DitherTextureSize.xy, z: texture_id @@ -95,6 +104,7 @@ Shader "Hidden/HDRP/FinalPass" float2 positionNDC = input.texcoord; uint2 positionSS = input.texcoord * _ScreenSize.xy; + uint2 scaledPositionSS = ((input.texcoord.xy * _UVTransform.xy) + _UVTransform.zw) * _ViewPortSize.xy; // Flip logic positionSS = positionSS * _UVTransform.xy + _UVTransform.zw * (_ScreenSize.xy - 1.0); @@ -102,8 +112,10 @@ Shader "Hidden/HDRP/FinalPass" #ifdef CATMULL_ROM_4 CTYPE outColor = UpscaledResult(positionNDC.xy); + #elif defined(RCAS) + CTYPE outColor = ApplyRCAS(scaledPositionSS); #elif defined(BYPASS) - CTYPE outColor = LOAD_TEXTURE2D_X(_InputTexture, ((input.texcoord.xy * _UVTransform.xy) + _UVTransform.zw) * _ViewPortSize.xy).CTYPE_SWIZZLE; + CTYPE outColor = LOAD_TEXTURE2D_X(_InputTexture, scaledPositionSS).CTYPE_SWIZZLE; #else CTYPE outColor = LOAD_TEXTURE2D_X(_InputTexture, positionSS).CTYPE_SWIZZLE; #endif diff --git a/com.unity.render-pipelines.high-definition/Runtime/PostProcessing/Shaders/RobustContrastAdaptiveSharpen.compute b/com.unity.render-pipelines.high-definition/Runtime/PostProcessing/Shaders/RobustContrastAdaptiveSharpen.compute deleted file mode 100644 index 3b3990c97df..00000000000 --- a/com.unity.render-pipelines.high-definition/Runtime/PostProcessing/Shaders/RobustContrastAdaptiveSharpen.compute +++ /dev/null @@ -1,32 +0,0 @@ -#pragma kernel KMain - -#pragma multi_compile _ ENABLE_ALPHA -#pragma only_renderers d3d11 playstation xboxone xboxseries vulkan metal switch - -#include "Packages/com.unity.render-pipelines.core/ShaderLibrary/Common.hlsl" -#include "Packages/com.unity.render-pipelines.high-definition/Runtime/ShaderLibrary/ShaderVariables.hlsl" - -TEXTURE2D_X(_InputTexture); -RW_TEXTURE2D_X(float4, _OutputTexture); - -#define FSR_INPUT_TEXTURE _InputTexture -#define FSR_INPUT_SAMPLER s_linear_clamp_sampler -#ifdef ENABLE_ALPHA - #define FSR_ENABLE_ALPHA 1 -#endif -#include "Packages/com.unity.render-pipelines.core/Runtime/PostProcessing/Shaders/FSRCommon.hlsl" - -[numthreads(64, 1, 1)] -void KMain(uint3 LocalThreadId : SV_GroupThreadID, uint3 WorkGroupId : SV_GroupID, uint3 dispatchThreadId : SV_DispatchThreadID) -{ - UNITY_XR_ASSIGN_VIEW_INDEX(dispatchThreadId.z); - - // Do remapping of local xy in workgroup for a more PS-like swizzle pattern. - uint2 gxy = ARmp8x8(LocalThreadId.x) + uint2(WorkGroupId.x << 3u, WorkGroupId.y << 3u); - -#ifdef ENABLE_ALPHA - _OutputTexture[COORD_TEXTURE2D_X(gxy)] = ApplyRCAS(gxy); -#else - _OutputTexture[COORD_TEXTURE2D_X(gxy)] = float4(ApplyRCAS(gxy), 1.0); -#endif -} diff --git a/com.unity.render-pipelines.high-definition/Runtime/PostProcessing/Shaders/RobustContrastAdaptiveSharpen.compute.meta b/com.unity.render-pipelines.high-definition/Runtime/PostProcessing/Shaders/RobustContrastAdaptiveSharpen.compute.meta deleted file mode 100644 index 5b5a5f32074..00000000000 --- a/com.unity.render-pipelines.high-definition/Runtime/PostProcessing/Shaders/RobustContrastAdaptiveSharpen.compute.meta +++ /dev/null @@ -1,8 +0,0 @@ -fileFormatVersion: 2 -guid: d907373e407ff65479c449a66c04443d -ComputeShaderImporter: - externalObjects: {} - preprocessorOverride: 0 - userData: - assetBundleName: - assetBundleVariant: diff --git a/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/HDProfileId.cs b/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/HDProfileId.cs index 9e9bb1bc037..d96dd9c519d 100644 --- a/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/HDProfileId.cs +++ b/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/HDProfileId.cs @@ -241,7 +241,6 @@ internal enum HDProfileId CustomPostProcessAfterPP, CustomPostProcessAfterOpaqueAndSky, ContrastAdaptiveSharpen, - RobustContrastAdaptiveSharpen, EdgeAdaptiveSpatialUpsampling, PrepareProbeVolumeList, ProbeVolumeDebug, diff --git a/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/HDRenderPipeline.PostProcess.cs b/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/HDRenderPipeline.PostProcess.cs index 7291a3f49e6..ad5a713c310 100644 --- a/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/HDRenderPipeline.PostProcess.cs +++ b/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/HDRenderPipeline.PostProcess.cs @@ -572,7 +572,6 @@ TextureHandle RenderPostProcess(RenderGraph renderGraph, // AMD Fidelity FX passes source = ContrastAdaptiveSharpeningPass(renderGraph, hdCamera, source); source = EdgeAdaptiveSpatialUpsampling(renderGraph, hdCamera, source); - source = RobustContrastAdaptiveSharpeningPass(renderGraph, hdCamera, source); } FinalPass(renderGraph, hdCamera, afterPostProcessBuffer, alphaTexture, dest, source, uiBuffer, m_BlueNoise, flipYInPostProcess); @@ -4755,58 +4754,6 @@ TextureHandle ContrastAdaptiveSharpeningPass(RenderGraph renderGraph, HDCamera h #endregion - #region RCAS - class RCASData - { - public ComputeShader rcasCS; - public int mainKernel; - public int viewCount; - public int outputWidth; - public int outputHeight; - - public TextureHandle source; - public TextureHandle destination; - } - - TextureHandle RobustContrastAdaptiveSharpeningPass(RenderGraph renderGraph, HDCamera hdCamera, TextureHandle source) - { - if (hdCamera.DynResRequest.enabled && hdCamera.DynResRequest.filter == DynamicResUpscaleFilter.EdgeAdaptiveScalingUpres) - { - using (var builder = renderGraph.AddRenderPass("Robust Contrast Adaptive Sharpen", out var passData, ProfilingSampler.Get(HDProfileId.RobustContrastAdaptiveSharpen))) - { - passData.rcasCS = defaultResources.shaders.robustContrastAdaptiveSharpenCS; - if (PostProcessEnableAlpha()) - passData.rcasCS.EnableKeyword("ENABLE_ALPHA"); - else - passData.rcasCS.DisableKeyword("ENABLE_ALPHA"); - passData.mainKernel = passData.rcasCS.FindKernel("KMain"); - passData.viewCount = hdCamera.viewCount; - passData.outputWidth = Mathf.RoundToInt(hdCamera.finalViewport.width); - passData.outputHeight = Mathf.RoundToInt(hdCamera.finalViewport.height); - passData.source = builder.ReadTexture(source); - passData.destination = builder.WriteTexture(GetPostprocessUpsampledOutputHandle(renderGraph, "Robust Contrast Adaptive Sharpen Destination")); - - builder.SetRenderFunc( - (RCASData data, RenderGraphContext ctx) => - { - FSRUtils.SetRcasConstants(ctx.cmd); - ctx.cmd.SetComputeTextureParam(data.rcasCS, data.mainKernel, HDShaderIDs._InputTexture, data.source); - ctx.cmd.SetComputeTextureParam(data.rcasCS, data.mainKernel, HDShaderIDs._OutputTexture, data.destination); - - int dispatchX = HDUtils.DivRoundUp(data.outputWidth, 8); - int dispatchY = HDUtils.DivRoundUp(data.outputHeight, 8); - - ctx.cmd.DispatchCompute(data.rcasCS, data.mainKernel, dispatchX, dispatchY, data.viewCount); - }); - - source = passData.destination; - } - } - return source; - } - - #endregion - #region EASU class EASUData { @@ -4976,9 +4923,15 @@ void FinalPass(RenderGraph renderGraph, HDCamera hdCamera, TextureHandle afterPo finalPassMaterial.EnableKeyword("CATMULL_ROM_4"); break; case DynamicResUpscaleFilter.ContrastAdaptiveSharpen: - case DynamicResUpscaleFilter.EdgeAdaptiveScalingUpres: finalPassMaterial.EnableKeyword("BYPASS"); break; + case DynamicResUpscaleFilter.EdgeAdaptiveScalingUpres: + // The RCAS half of the FSR technique (EASU + RCAS) is merged into FinalPass instead of + // running it inside a separate compute shader. This allows us to avoid an additional + // round-trip through memory which improves performance. + finalPassMaterial.EnableKeyword("RCAS"); + FSRUtils.SetRcasConstants(ctx.cmd); + break; } } else diff --git a/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/HDRenderPipelineRuntimeResources.cs b/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/HDRenderPipelineRuntimeResources.cs index 780c2acff0b..c211706af58 100644 --- a/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/HDRenderPipelineRuntimeResources.cs +++ b/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/HDRenderPipelineRuntimeResources.cs @@ -338,8 +338,6 @@ public sealed class ShaderResources [Reload("Runtime/PostProcessing/Shaders/ContrastAdaptiveSharpen.compute")] public ComputeShader contrastAdaptiveSharpenCS; - [Reload("Runtime/PostProcessing/Shaders/RobustContrastAdaptiveSharpen.compute")] - public ComputeShader robustContrastAdaptiveSharpenCS; [Reload("Runtime/PostProcessing/Shaders/EdgeAdaptiveSpatialUpsampling.compute")] public ComputeShader edgeAdaptiveSpatialUpsamplingCS; [Reload("Runtime/VirtualTexturing/Shaders/DownsampleVTFeedback.compute")]