diff --git a/Packages/com.unity.render-pipelines.universal/Documentation~/TableOfContents.md b/Packages/com.unity.render-pipelines.universal/Documentation~/TableOfContents.md index 37554721642..f1eb16323a2 100644 --- a/Packages/com.unity.render-pipelines.universal/Documentation~/TableOfContents.md +++ b/Packages/com.unity.render-pipelines.universal/Documentation~/TableOfContents.md @@ -129,6 +129,7 @@ * [URP unlit shader with color input](writing-shaders-urp-unlit-color.md) * [Drawing a texture](writing-shaders-urp-unlit-texture.md) * [Visualizing normal vectors](writing-shaders-urp-unlit-normals.md) + * [Write depth only in a shader in URP](writing-shaders-urp-depth-only.md) * [Reconstruct the world space positions](writing-shaders-urp-reconstruct-world-position.md) * [Shader methods in URP](use-built-in-shader-methods.md) * [Import a file from the URP shader library](use-built-in-shader-methods-import.md) diff --git a/Packages/com.unity.render-pipelines.universal/Documentation~/urp-universal-renderer.md b/Packages/com.unity.render-pipelines.universal/Documentation~/urp-universal-renderer.md index 05d9a38fa1e..e37859d70e0 100644 --- a/Packages/com.unity.render-pipelines.universal/Documentation~/urp-universal-renderer.md +++ b/Packages/com.unity.render-pipelines.universal/Documentation~/urp-universal-renderer.md @@ -62,9 +62,9 @@ This section contains properties related to rendering. | Property | Description | |:-|:-| | **Rendering Path** | Select the Rendering Path.
Options: | -|   **Depth Priming Mode** | Specify whether Unity uses scene depth data to identify pixels the camera can't see, then skips the shader fragment stage for those pixels. This speeds up rendering, but has an upfront memory and performance cost. The amount rendering speeds up depends on how many pixels are hidden, and the complexity of the fragment shader code Unity skips.

The options are:
Depth priming isn't compatible with the following: | -|   **Accurate G-buffer normals** | Indicates whether to use a more resource-intensive normal encoding/decoding method to improve visual quality.

This property is available only if **Rendering Path** is set to **Deferred**. | -| **Depth Texture Mode** | Specifies at which stage in the render pipeline URP should copy the scene depth to a depth texture. The options are:**Note**: On mobile devices, the **After Transparents** option can lead to a significant improvement in memory bandwidth. This is because the Copy Depth pass causes a switch in render target between the Opaque pass and the Transparents pass. When this occurs, Unity stores the contents of the Color Buffer in the main memory, and then loads it again once the Copy Depth pass is complete. The impact increases significantly when MSAA is enabled, because Unity must also store and load the MSAA data alongside the Color Buffer. | +| **Depth Priming Mode** | Skips drawing overlapping pixels, to speed up rendering. Unity uses the depth texture to check which pixels overlap. The rendering improvement depends on the number of overlapping pixels and the complexity of the pixel shaders.

**Note**: If you use custom shaders, Unity renders opaque objects as invisible unless you add passes with `DepthOnly` and `DepthNormals` tags. For more information, refer to [Write depth only in a shader](writing-shaders-urp-depth-only.md).

The options are:**Note**: Depth priming isn't supported if you use a [deferred rendering path](rendering/deferred-rendering-path-landing.md) or [Multisample Anti-aliasing](anti-aliasing.md#multisample-anti-aliasing-msaa), or at runtime on mobile devices that use tile-based deferred rendering (TBDR).| +| **Accurate G-buffer normals** | Indicates whether to use a more resource-intensive normal encoding/decoding method to improve visual quality.

This property is available only if **Rendering Path** is set to **Deferred**. | +| **Depth Texture Mode** | Specifies the stage in the render pipeline at which to copy the scene depth to a depth texture. The options are:**Note**: On mobile devices, the **After Transparents** option can lead to a significant improvement in memory bandwidth. This is because the Copy Depth pass causes a switch in render target between the Opaque pass and the Transparents pass. When this occurs, Unity stores the contents of the Color Buffer in the main memory, and then loads it again once the Copy Depth pass is complete. The impact increases significantly when MSAA is enabled as Unity must also store and load the MSAA data alongside the Color Buffer. | ### Native RenderPass diff --git a/Packages/com.unity.render-pipelines.universal/Documentation~/writing-custom-shaders-urp.md b/Packages/com.unity.render-pipelines.universal/Documentation~/writing-custom-shaders-urp.md index 15eeb57dca6..6cc5c1f706e 100644 --- a/Packages/com.unity.render-pipelines.universal/Documentation~/writing-custom-shaders-urp.md +++ b/Packages/com.unity.render-pipelines.universal/Documentation~/writing-custom-shaders-urp.md @@ -10,6 +10,7 @@ The section contains the following topics: * [URP unlit shader with color input](writing-shaders-urp-unlit-color.md) * [Drawing a texture](writing-shaders-urp-unlit-texture.md) * [Visualizing normal vectors](writing-shaders-urp-unlit-normals.md) +* [Write depth only in a shader in URP](writing-shaders-urp-depth-only.md) * [Reconstruct the world space positions](writing-shaders-urp-reconstruct-world-position.md) * [Built-in shader methods in URP](use-built-in-shader-methods.md) diff --git a/Packages/com.unity.render-pipelines.universal/Documentation~/writing-shaders-urp-basic-unlit-structure.md b/Packages/com.unity.render-pipelines.universal/Documentation~/writing-shaders-urp-basic-unlit-structure.md index 5101f96e73c..e21d73b7be8 100644 --- a/Packages/com.unity.render-pipelines.universal/Documentation~/writing-shaders-urp-basic-unlit-structure.md +++ b/Packages/com.unity.render-pipelines.universal/Documentation~/writing-shaders-urp-basic-unlit-structure.md @@ -4,7 +4,9 @@ This example shows a basic URP-compatible shader. This shader fills the mesh sha To see the shader in action, copy and paste the following ShaderLab code into the Shader asset. -```c++ +**Note**: If you enable **Depth Priming Mode** in the [URP asset](universalrp-asset.md), this shader renders opaque objects as invisible. For more information, refer to [Write depth only in a shader](writing-shaders-urp-depth-only.md). + +``` lang-cpp // This shader fills the mesh shape with a color predefined in the code. Shader "Example/URPUnlitShaderBasic" { @@ -143,8 +145,6 @@ This block contains the HLSL program code. > **NOTE**: HLSL language is the preferred language for URP shaders. -> **NOTE**: URP supports the CG language. If you add the CGPROGRAM/ENDCGPROGRAM block in a shader, Unity includes shaders from the Built-in Render Pipeline library automatically. If you include shaders from the SRP shader library, some SRP shader macros and functions might conflict with the Built-in Render Pipeline shader functions. Shaders with the CGPROGRAM block are not SRP Batcher compatible. - This block contains the `#include` declaration with the reference to the `Core.hlsl` file. ```c++ @@ -164,6 +164,8 @@ Varyings vert(Attributes IN) } ``` +> **Note**: Don't import shader library files from both the Scriptable Render Pipeline (SRP) and the Built-In Render Pipeline in the same shader. Some SRP shader macros and functions might conflict with the Built-in Render Pipeline shader functions. For more information, refer to [Shader methods in URP](use-built-in-shader-methods) and [Import a file from the shader library in the Built-In Render Pipeline](../SL-BuiltinIncludes). + The fragment shader in this basic HLSL code outputs the single color predefined in the code: ```c++ diff --git a/Packages/com.unity.render-pipelines.universal/Documentation~/writing-shaders-urp-depth-only.md b/Packages/com.unity.render-pipelines.universal/Documentation~/writing-shaders-urp-depth-only.md new file mode 100644 index 00000000000..e1bdcdb598c --- /dev/null +++ b/Packages/com.unity.render-pipelines.universal/Documentation~/writing-shaders-urp-depth-only.md @@ -0,0 +1,79 @@ +# Write a depth-only pass in a Universal Render Pipeline shader + +To create a depth prepass that writes the depth of objects before the opaque and transparent passes, add a shader pass that has the `DepthOnly` tag. + +Add this pass if you enable a setting that requires a depth prepass, otherwise Unity might render incorrectly. For example, if you enable **Depth Priming** in the [Universal Render Pipleine (URP) Asset](urp-universal-renderer.md), opaque objects are invisible. + +**Important**: To render correctly, make sure that every shader pass renders the same number of fragments in the same positions. For example, use an include or a macro to share the same code between passes, especially if you change the positions of vertices, use alpha clipping, or use effects like dithering. + +## Add a depth-only pass + +The following example shows how to add a depth-only pass to a shader: + +```lang-hlsl +Shader "Example/CustomShader" +{ + SubShader + { + + Tags { "RenderType" = "Opaque" "RenderPipeline" = "UniversalPipeline" } + + // Add a depth-only pass + Pass + { + Name "DepthOnlyPass" + Tags { "LightMode" = "DepthOnly" } + + // Write depth to the depth buffer + ZWrite On + + // Don't write to the color buffer + ColorMask 0 + + ... + } + + // The forward pass. This pass also writes depth by default. + Pass + { + Name "ForwardPass" + Tags { "LightMode" = "UniversalForward" } + + ... + } + } +} +``` + +## Add a depth and normals pass + +The recommended best practice is to also add a pass that has the `DepthNormals` tag, to write both the depth and normals of objects. Otherwise, if you enable features like screen space ambient occlusion (SSAO), Unity might not render objects correctly. + +For more information about outputting normals, refer to [Visualize normal vectors in a shader in URP](writing-shaders-urp-unlit-normals.md). + +For example: + +```lang-hlsl +Pass +{ + Name "DepthNormalsPass" + Tags { "LightMode" = "DepthNormals" } + + // Write depth to the depth buffer + ZWrite On + + ... + + float4 frag(Varyings input) : SV_TARGET + { + // Return the normal as a value between 0 and 1 + float3 normalWS = normalize(input.normalWS); + return float4(normalWS * 0.5 + 0.5, 1); + }} +``` + +## Additional resources + +- [ShaderLab Pass tags](urp-shaders/urp-shaderlab-pass-tags.md) +- [Shader methods](use-built-in-shader-methods.md) +- [Universal Render Pipleine (URP) Asset](urp-universal-renderer.md) diff --git a/Packages/com.unity.render-pipelines.universal/Runtime/2D/Renderer2D.cs b/Packages/com.unity.render-pipelines.universal/Runtime/2D/Renderer2D.cs index 141d48ba087..b7f35c77069 100644 --- a/Packages/com.unity.render-pipelines.universal/Runtime/2D/Renderer2D.cs +++ b/Packages/com.unity.render-pipelines.universal/Runtime/2D/Renderer2D.cs @@ -390,6 +390,7 @@ public override void Setup(ScriptableRenderContext context, ref RenderingData re bool pixelPerfectCameraEnabled = ppc != null && ppc.enabled; bool hasCaptureActions = cameraData.captureActions != null && lastCameraInStack; bool resolvePostProcessingToCameraTarget = lastCameraInStack && !hasCaptureActions && !hasPassesAfterPostProcessing && !requireFinalPostProcessPass && !pixelPerfectCameraEnabled; + bool doSRGBEncoding = resolvePostProcessingToCameraTarget && needsColorEncoding; if (hasPostProcess) { @@ -404,7 +405,7 @@ public override void Setup(ScriptableRenderContext context, ref RenderingData re colorGradingLutHandle, null, requireFinalPostProcessPass, - afterPostProcessColorHandle.nameID == k_CameraTarget.nameID && needsColorEncoding); + doSRGBEncoding); EnqueuePass(postProcessPass); }