Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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);
}
Expand Down
1 change: 1 addition & 0 deletions com.unity.render-pipelines.high-definition/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,40 +61,45 @@ 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.

* The second parameter represents the minimum value to clamp the property to.

* 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.

<a name="Shader"></a>

Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down