-
Notifications
You must be signed in to change notification settings - Fork 138
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added utility custom pass to copy current depth buffer to custom depth
- Loading branch information
Showing
6 changed files
with
139 additions
and
0 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
41 changes: 41 additions & 0 deletions
41
Assets/CustomPasses/CurrentDepthToCustomDepth/CurrentDepthToCustomDepth.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
Assets/CustomPasses/CurrentDepthToCustomDepth/CurrentDepthToCustomDepth.cs.meta
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
64 changes: 64 additions & 0 deletions
64
Assets/CustomPasses/CurrentDepthToCustomDepth/CurrentDepthToCustomDepth.shader
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
9 changes: 9 additions & 0 deletions
9
Assets/CustomPasses/CurrentDepthToCustomDepth/CurrentDepthToCustomDepth.shader.meta
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters