diff --git a/TestProjects/HDRP_Tests/Assets/GraphicTests/Scenes/4x_PostProcessing/4060_CustomPostProcess/GreenPP.cs b/TestProjects/HDRP_Tests/Assets/GraphicTests/Scenes/4x_PostProcessing/4060_CustomPostProcess/GreenPP.cs index c19fd5dac8c..3a71b00f550 100644 --- a/TestProjects/HDRP_Tests/Assets/GraphicTests/Scenes/4x_PostProcessing/4060_CustomPostProcess/GreenPP.cs +++ b/TestProjects/HDRP_Tests/Assets/GraphicTests/Scenes/4x_PostProcessing/4060_CustomPostProcess/GreenPP.cs @@ -27,8 +27,7 @@ public override void Render(CommandBuffer cmd, HDCamera camera, RTHandle source, return; m_Material.SetFloat("_Intensity", intensity.value); - m_Material.SetTexture("_InputTexture", source); - HDUtils.DrawFullScreen(cmd, m_Material, destination); + cmd.Blit(source, destination, m_Material); } public override void Cleanup() diff --git a/TestProjects/HDRP_Tests/Assets/GraphicTests/Scenes/4x_PostProcessing/4060_CustomPostProcess/GreenPP.shader b/TestProjects/HDRP_Tests/Assets/GraphicTests/Scenes/4x_PostProcessing/4060_CustomPostProcess/GreenPP.shader index 47cd8910405..51a71512301 100644 --- a/TestProjects/HDRP_Tests/Assets/GraphicTests/Scenes/4x_PostProcessing/4060_CustomPostProcess/GreenPP.shader +++ b/TestProjects/HDRP_Tests/Assets/GraphicTests/Scenes/4x_PostProcessing/4060_CustomPostProcess/GreenPP.shader @@ -1,5 +1,11 @@ Shader "Hidden/Shader/GreenPP" { + Properties + { + // This property is necessary to make the CommandBuffer.Blit bind the source texture to _MainTex + _MainTex("Main Texture", 2DArray) = "grey" {} + } + HLSLINCLUDE #pragma target 4.5 @@ -36,14 +42,13 @@ Shader "Hidden/Shader/GreenPP" // List of properties to control your post process effect float _Intensity; - TEXTURE2D_X(_InputTexture); + TEXTURE2D_X(_MainTex); float4 CustomPostProcess(Varyings input) : SV_Target { UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(input); - uint2 positionSS = input.texcoord * _ScreenSize.xy; - float l = Luminance(LOAD_TEXTURE2D_X(_InputTexture, positionSS).xyz); + float l = Luminance(SAMPLE_TEXTURE2D_X(_MainTex, s_point_clamp_sampler, input.texcoord).xyz); return float4(0, l, 0, 1); } diff --git a/com.unity.render-pipelines.high-definition/CHANGELOG.md b/com.unity.render-pipelines.high-definition/CHANGELOG.md index d949e56b330..200680447f6 100644 --- a/com.unity.render-pipelines.high-definition/CHANGELOG.md +++ b/com.unity.render-pipelines.high-definition/CHANGELOG.md @@ -328,6 +328,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. - Fixed white flashes when history is reset due to changes on type of upsampler. - Fixed misc TAA issue: Slightly improved TAA flickering, Reduced ringing of TAA sharpening, tweak TAA High quality central color filtering. - Fixed TAA upsampling algorithm, now work properly +- Fixed custom post process template not working with Blit method. ### Changed - Changed Window/Render Pipeline/HD Render Pipeline Wizard to Window/Rendering/HDRP Wizard diff --git a/com.unity.render-pipelines.high-definition/Documentation~/Custom-Post-Process.md b/com.unity.render-pipelines.high-definition/Documentation~/Custom-Post-Process.md index 3c7f65e16fe..d746d8f7b7e 100644 --- a/com.unity.render-pipelines.high-definition/Documentation~/Custom-Post-Process.md +++ b/com.unity.render-pipelines.high-definition/Documentation~/Custom-Post-Process.md @@ -61,15 +61,14 @@ public sealed class GrayScale : CustomPostProcessVolumeComponent, IPostProcessCo return; m_Material.SetFloat("_Intensity", intensity.value); - m_Material.SetTexture("_InputTexture", source); - HDUtils.DrawFullScreen(cmd, m_Material, destination); + cmd.Blit(source, destination, m_Material, 0); } public override void Cleanup() => CoreUtils.Destroy(m_Material); } ``` -Firstly, the example code uses a ClampedFloatParameter which is a type that you may not be familiar with. This type is a float which you can clamp to a range. In the constructor: +This example code uses a `ClampedFloatParameter` that you can clamp to a range. In the constructor: * The first parameter is the default value of the property. @@ -77,24 +76,30 @@ Firstly, the example code uses a ClampedFloatParameter which is a type that you * The third parameter represents the maximum value to clamp the property to. -Next, there is the **IsActive()** function. HDRP calls this function before the **Render** function make sure it is possible to process the effect. If this function returns `false`, HDRP does not process the effect. It is good practice to check every property configuration where the effect either breaks or does nothing. In this example, **IsActive()** makes sure that it can find the **GrayScale.shader** and that the intensity is greater than 0. +HDRP calls the `IsActive()`function before the `Render` function to process the effect. If this function returns `false`, HDRP does not process the effect. It is good practice to check every property configuration where the effect either breaks or does nothing. In this example, `IsActive()` makes sure that HDRP can find the `GrayScale.shader` and that the intensity is greater than 0. -The **injectionPoint** override allows you to specify where in the pipeline HDRP executes the effect. There are currently three injection points: +The `injectionPoint` override allows you to specify where in the pipeline HDRP executes the effect. You can choose from the following injection points: -* **AfterOpaqueAndSky.** +* **AfterOpaqueAndSky** -* **BeforeTAA.** +* **BeforeTAA** -* **BeforePostProcess.** +* **BeforePostProcess** -* **AfterPostProcess**. +* **AfterPostProcess** -For more detailed information on where HDRP injects the custom post-process passes, see the following diagram: +The following diagram gives more information on where HDRP injects custom post-process passes: ![](Images/HDRP-frame-graph-diagram.png) + + **Note**: When you enable [Temporal anti-aliasing (TAA)](Anti-Aliasing.md#TAA), HDRP applies TAA between the injection points **BeforeTAA** and **beforePostProcess**. When you use [Depth Of Field](Post-Processing-Depth-of-Field.md) and enable its **Physically Based** property, HDRP performs a second TAA pass to perform temporal accumulation for this effect. -Now there are the **Setup**, **Render**, and **Cleanup** functions. These are here to respectively allocate, use, and release the resources that the effect needs. The only resource that the example uses is a single Material. The example creates the Material in **Setup** and, in **Cleanup**, uses CoreUtils.Destroy() to release the Material. In the **Render** function, you have access to a [CommandBuffer](https://docs.unity3d.com/2019.3/Documentation/ScriptReference/Rendering.CommandBuffer.html) which you can use to enqueue tasks for HDRP to execute. Here you can use HDUtils.DrawFullScreen to render a fullscreen quad. It uses the CommandBuffer and Material that you pass in then blits the result to the destination RTHandle. +The `Setup`, `Render`, and `Cleanup` functions allocate, use, and release the resources that the effect needs. The only resource that the above script example uses is a single Material. This example creates the Material in `Setup` and, in `Cleanup`, uses `CoreUtils.Destroy()` to release the Material. + +In the `Render` function, you have access to a [CommandBuffer](https://docs.unity3d.com/2019.3/Documentation/ScriptReference/Rendering.CommandBuffer.html) which you can use to enqueue tasks for HDRP to execute. You can use [CommandBuffer.Blit](https://docs.unity3d.com/ScriptReference/Rendering.CommandBuffer.Blit.html) here to render a fullscreen quad. When you use the `Blit` function, Unity binds the source buffer in parameter to the `_MainTex` property in the shader. For this to happen, you need to declare the `_MainTex` property in the [Properties](https://docs.unity3d.com/Manual/SL-Properties.html) section of the shader. + +**Note**: You can also use the [HDUtils.DrawFullscreen](https://docs.unity3d.com/Packages/com.unity.render-pipelines.high-definition@latest?subfolder=/api/UnityEngine.Rendering.HighDefinition.HDUtils.html#UnityEngine_Rendering_HighDefinition_HDUtils_DrawFullScreen_UnityEngine_Rendering_CommandBuffer_UnityEngine_Material_UnityEngine_Rendering_RTHandle_UnityEngine_MaterialPropertyBlock_System_Int32_) method. To do this, you need to multiply the `input.texcoord` by the `_RTHandleScale.xy` property to account for dynamic scaling. @@ -104,6 +109,12 @@ HDRP gives you total control over the vertex and fragment Shader so you can edit ```glsl Shader "Hidden/Shader/GrayScale" { + Properties + { + // This property is necessary to make the CommandBuffer.Blit bind the source texture to _MainTex + _MainTex("Main Texture", 2DArray) = "grey" {} + } + HLSLINCLUDE #pragma target 4.5 @@ -144,18 +155,18 @@ Shader "Hidden/Shader/GrayScale" // List of properties to control your post process effect float _Intensity; - TEXTURE2D_X(_InputTexture); + TEXTURE2D_X(_MainTex); float4 CustomPostProcess(Varyings input) : SV_Target { UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(input); - uint2 positionSS = input.texcoord * _ScreenSize.xy; - float3 outColor = LOAD_TEXTURE2D_X(_InputTexture, positionSS).xyz; + float3 sourceColor = SAMPLE_TEXTURE2D_X(_MainTex, s_linear_clamp_sampler, input.texcoord).xyz; + + // Apply greyscale effect + float3 color = lerp(sourceColor, Luminance(sourceColor), _Intensity); - // We do the blending manually instead of relying on the hardware blend - // It's necessary because the color buffer contains garbage from the previous post process effect. - return float4(lerp(outColor, Luminance(outColor).xxx, _Intensity), 1); + return float4(color, 1); } ENDHLSL diff --git a/com.unity.render-pipelines.high-definition/Editor/PostProcessing/Templates/CustomPostProcessingShader.template b/com.unity.render-pipelines.high-definition/Editor/PostProcessing/Templates/CustomPostProcessingShader.template index fac5e0b30f5..c373041f92e 100644 --- a/com.unity.render-pipelines.high-definition/Editor/PostProcessing/Templates/CustomPostProcessingShader.template +++ b/com.unity.render-pipelines.high-definition/Editor/PostProcessing/Templates/CustomPostProcessingShader.template @@ -1,5 +1,11 @@ Shader "Hidden/Shader/#SCRIPTNAME#" { + Properties + { + // This property is necessary to make the CommandBuffer.Blit bind the source texture to _MainTex + _MainTex("Main Texture", 2DArray) = "grey" {} + } + HLSLINCLUDE #pragma target 4.5 @@ -36,16 +42,20 @@ Shader "Hidden/Shader/#SCRIPTNAME#" // List of properties to control your post process effect float _Intensity; - TEXTURE2D_X(_InputTexture); + TEXTURE2D_X(_MainTex); float4 CustomPostProcess(Varyings input) : SV_Target { UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(input); - uint2 positionSS = input.texcoord * _ScreenSize.xy; - float3 outColor = LOAD_TEXTURE2D_X(_InputTexture, positionSS).xyz; + // Note that if HDUtils.DrawFullScreen is used to render the post process, input.texcoord needs to be multiplied by _RTHandleScale.xy + + float3 sourceColor = SAMPLE_TEXTURE2D_X(_MainTex, s_linear_clamp_sampler, input.texcoord).xyz; + + // Apply greyscale effect + float3 color = lerp(sourceColor, Luminance(sourceColor), _Intensity); - return float4(outColor, 1); + return float4(color, 1); } ENDHLSL diff --git a/com.unity.render-pipelines.high-definition/Editor/PostProcessing/Templates/CustomPostProcessingVolume.template b/com.unity.render-pipelines.high-definition/Editor/PostProcessing/Templates/CustomPostProcessingVolume.template index 10de1aebf56..a3e868188fd 100644 --- a/com.unity.render-pipelines.high-definition/Editor/PostProcessing/Templates/CustomPostProcessingVolume.template +++ b/com.unity.render-pipelines.high-definition/Editor/PostProcessing/Templates/CustomPostProcessingVolume.template @@ -32,8 +32,7 @@ public sealed class #SCRIPTNAME# : CustomPostProcessVolumeComponent, IPostProces return; m_Material.SetFloat("_Intensity", intensity.value); - m_Material.SetTexture("_InputTexture", source); - HDUtils.DrawFullScreen(cmd, m_Material, destination); + cmd.Blit(source, destination, m_Material, 0); } public override void Cleanup()