Skip to content

Commit

Permalink
Added utility custom pass to copy current depth buffer to custom depth
Browse files Browse the repository at this point in the history
  • Loading branch information
alelievr committed Jan 23, 2023
1 parent 81d5b53 commit d392da5
Show file tree
Hide file tree
Showing 6 changed files with 139 additions and 0 deletions.
8 changes: 8 additions & 0 deletions Assets/CustomPasses/CurrentDepthToCustomDepth.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
using UnityEditor.Rendering.HighDefinition;
using UnityEngine;
using UnityEngine.Rendering.HighDefinition;
using UnityEngine.Rendering;

#if UNITY_EDITOR
[CustomPassDrawer(typeof(CurrentDepthToCustomDepth))]
class CurrentDepthToCustomDepthDrawer : CustomPassDrawer
{
protected override PassUIFlag commonPassUIFlags => PassUIFlag.Name;
}
#endif

class CurrentDepthToCustomDepth : CustomPass
{
Material depthToCustomDepth;

[SerializeField, HideInInspector]
Shader depthToCustomDepthShader;

readonly int currentCameraDepth = Shader.PropertyToID("_CurrentCameraDepth");

protected override void Setup(ScriptableRenderContext renderContext, CommandBuffer cmd)
{
if (depthToCustomDepthShader == null)
depthToCustomDepthShader = Shader.Find("Hidden/FullScreen/CurrentDepthToCustomDepth");
depthToCustomDepth = CoreUtils.CreateEngineMaterial(depthToCustomDepthShader);
}

protected override void Execute(CustomPassContext ctx)
{
depthToCustomDepth.SetTexture(currentCameraDepth, ctx.cameraDepthBuffer);
CoreUtils.SetRenderTarget(ctx.cmd, ctx.customDepthBuffer.Value);
CoreUtils.DrawFullScreen(ctx.cmd, depthToCustomDepth, shaderPassId: 0);
}

protected override void Cleanup()
{
CoreUtils.Destroy(depthToCustomDepth);
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
Shader "Hidden/FullScreen/CurrentDepthToCustomDepth"
{
HLSLINCLUDE

#pragma vertex Vert

#pragma target 4.5
#pragma only_renderers d3d11 playstation xboxone xboxseries vulkan metal switch

#include "Packages/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/RenderPass/CustomPass/CustomPassCommon.hlsl"
#pragma enable_d3d11_debug_symbols

// The PositionInputs struct allow you to retrieve a lot of useful information for your fullScreenShader:
// struct PositionInputs
// {
// float3 positionWS; // World space position (could be camera-relative)
// float2 positionNDC; // Normalized screen coordinates within the viewport : [0, 1) (with the half-pixel offset)
// uint2 positionSS; // Screen space pixel coordinates : [0, NumPixels)
// uint2 tileCoord; // Screen tile coordinates : [0, NumTiles)
// float deviceDepth; // Depth from the depth buffer : [0, 1] (typically reversed)
// float linearDepth; // View space Z coordinate : [Near, Far]
// };

// To sample custom buffers, you have access to these functions:
// But be careful, on most platforms you can't sample to the bound color buffer. It means that you
// can't use the SampleCustomColor when the pass color buffer is set to custom (and same for camera the buffer).
// float4 CustomPassSampleCustomColor(float2 uv);
// float4 CustomPassLoadCustomColor(uint2 pixelCoords);
// float LoadCustomDepth(uint2 pixelCoords);
// float SampleCustomDepth(float2 uv);

// There are also a lot of utility function you can use inside Common.hlsl and Color.hlsl,
// you can check them out in the source code of the core SRP package.

TEXTURE2D_X(_CurrentCameraDepth);

float FullScreenPass(Varyings varyings) : SV_Depth
{
UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(varyings);

return LOAD_TEXTURE2D_X(_CurrentCameraDepth, uint2(varyings.positionCS.xy));
}

ENDHLSL

SubShader
{
Tags{ "RenderPipeline" = "HDRenderPipeline" }
Pass
{
Name "Custom Pass 0"

ZWrite On
ZTest Always
Blend Off
Cull Off

HLSLPROGRAM
#pragma fragment FullScreenPass
ENDHLSL
}
}
Fallback Off
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -157,3 +157,9 @@ Sources: [Assets/CustomPasses/Wetness](https://github.com/alelievr/HDRP-Custom-P
It's also compatible with ShaderGraph using the SubGraph "EncodeIntoToNormalBuffer".

https://user-images.githubusercontent.com/6877923/195407733-1e73b63c-5ba6-488f-829d-d4cb0d0b1412.mp4

## Current Depth To Custom Depth

Duing HDRP rendering, you only have access to the depth buffer that contains all opaque objects, the depth texture is not updated when rendering transparent with depth pre/post passes.

This custom pass copies the current camera depth (up to date with the current injection point) to the custom depth buffer. The custom depth buffer can then be sampled in a fullscreen shader graph using the custom depth node.

0 comments on commit d392da5

Please sign in to comment.