浏览代码

replace custom stencil with user stencil bit (UserStencilUsage.UserBit0)

/main
Lasse Jon Fuglsang Pedersen 5 年前
当前提交
c328229e
共有 2 个文件被更改,包括 44 次插入76 次删除
  1. 79
      Runtime/CustomPass/NormalBufferBlurPass.cs
  2. 41
      Runtime/CustomPass/NormalBufferBlurPass.shader

79
Runtime/CustomPass/NormalBufferBlurPass.cs


// basic procedure:
//
// 1. allocate temp
// a. custom stencil
// 1. alloc temp
// a. write 0 -> custom stencil
// 3. copy depth
// a. write camera depth -> custom depth
//
// 4. render decals
// a. write 1 -> custom stencil
// b. write 0 -> custom color
// 3. mark decals
// a. write 0 -> custom color
// b. write 1 -> custom stencil
// 5. enable stencil
// 4. enable stencil
// 6. render fullscreen
// 5. render fullscreen
// 7. render fullscreen
// 6. render fullscreen
// 8. free temp
// 7. free temp
public class NormalBufferBlurPass : CustomPass
{

static readonly int idInputDepth = Shader.PropertyToID("_InputDepth");
static readonly int rtStencil = Shader.PropertyToID("_NormalBufferBlur_Stencil");
static readonly int rtRegions = Shader.PropertyToID("_NormalBufferBlur_Regions");
static readonly int rtDecoded = Shader.PropertyToID("_NormalBufferBlur_Decoded");

};
static ShaderTagId[] NAME_PASS_REPLACE_TAG = null;
const int PASS_COPY_DEPTH = 0;
const int PASS_MARK = 1;
const int PASS_DECODE = 2;
const int PASS_BLUR_AND_ENCODE = 3;
const int PASS_BLUR_AND_ENCODE_AND_DECAL = 4;
const int PASS_MARK = 0;
const int PASS_DECODE = 1;
const int PASS_BLUR_AND_ENCODE = 2;
const int PASS_BLUR_AND_ENCODE_AND_DECAL = 3;
Material passMaterial;

private RTHandle[] dbufferRTs;
RTHandle[] dbufferRTs = null;
dbufferRTs = null;
var fieldInfo_m_DbufferManager = typeof(HDRenderPipeline).GetField("m_DbufferManager", BindingFlags.NonPublic | BindingFlags.Instance);
if (fieldInfo_m_DbufferManager != null)

FindDbufferRTs(RenderPipelineManager.currentPipeline as HDRenderPipeline);
EnsureMaterial(ref passMaterial, NAME_SHADER);
if (passMaterial != null)
{
passMaterial.SetInt("_StencilBit", (int)UserStencilUsage.UserBit0);
}
if (NAME_PASS_REPLACE_TAG == null)
{

if (!hdCamera.frameSettings.IsEnabled(FrameSettingsField.Decals))
return;
int viewportW = hdCamera.actualWidth;
int viewportH = hdCamera.actualHeight;
//Debug.Log("custom blur pass, w = " + viewportW + ", h = " + viewportH);
int bufferW = cameraColor.rt.width;
int bufferH = cameraColor.rt.height;
cmd.GetTemporaryRT(rtStencil, viewportW, viewportH, (int)DepthBits.Depth24, FilterMode.Point, RenderTextureFormat.Depth);
cmd.GetTemporaryRT(rtRegions, viewportW, viewportH, (int)DepthBits.None, FilterMode.Point, RenderTextureFormat.R8, RenderTextureReadWrite.Linear, 1, false);
cmd.GetTemporaryRT(rtDecoded, viewportW, viewportH, (int)DepthBits.None, FilterMode.Point, RenderTextureFormat.ARGBHalf, RenderTextureReadWrite.Linear, 1, false);
cmd.GetTemporaryRT(rtRegions, bufferW, bufferH, (int)DepthBits.None, FilterMode.Point, RenderTextureFormat.R8, RenderTextureReadWrite.Linear, 1, false);
cmd.GetTemporaryRT(rtDecoded, bufferW, bufferH, (int)DepthBits.None, FilterMode.Point, RenderTextureFormat.ARGBHalf, RenderTextureReadWrite.Linear, 1, false);
// copy depth from camera depth
// render decals to mark blur regions
rtStencil, RenderBufferLoadAction.Load, RenderBufferStoreAction.DontCare,
ClearFlag.All, Color.white
cameraDepth, RenderBufferLoadAction.Load, RenderBufferStoreAction.Store,
ClearFlag.Color, Color.white
CoreUtils.SetViewport(cmd, cameraDepth);
cmd.SetGlobalTexture(idInputDepth, cameraDepth);
cmd.DrawProcedural(Matrix4x4.identity, passMaterial, PASS_COPY_DEPTH, MeshTopology.Triangles, 3, 1);
// render decals to mark blur regions
var renderListDesc = new RendererListDesc(NAME_PASS_REPLACE_TAG, cullingResults, hdCamera.camera)
RendererListDesc renderListDesc = new RendererListDesc(NAME_PASS_REPLACE_TAG, cullingResults, hdCamera.camera)
stateBlock = null,
excludeObjectMotionVectors = false,
};

// decode normal buffer in marked regions
CoreUtils.SetRenderTarget(cmd,
rtDecoded, RenderBufferLoadAction.DontCare, RenderBufferStoreAction.Store,
rtStencil, RenderBufferLoadAction.Load, RenderBufferStoreAction.DontCare,
cameraDepth, RenderBufferLoadAction.Load, RenderBufferStoreAction.DontCare,
CoreUtils.SetViewport(cmd, cameraDepth);
cmd.SetRandomWriteTarget(2, GetNormalBuffer());
cmd.DrawProcedural(Matrix4x4.identity, passMaterial, PASS_DECODE, MeshTopology.Triangles, 3, 1);

{
CoreUtils.SetRenderTarget(cmd,
dbufferNormalMaskRTI,
rtStencil);
cameraDepth,
ClearFlag.None);
CoreUtils.SetViewport(cmd, cameraDepth);
cmd.SetRandomWriteTarget(2, GetNormalBuffer());
cmd.DrawProcedural(Matrix4x4.identity, passMaterial, PASS_BLUR_AND_ENCODE_AND_DECAL, MeshTopology.Triangles, 3, 1);

{
CoreUtils.SetRenderTarget(cmd,
rtStencil, RenderBufferLoadAction.Load, RenderBufferStoreAction.DontCare,
cameraDepth, RenderBufferLoadAction.Load, RenderBufferStoreAction.Store,
CoreUtils.SetViewport(cmd, cameraDepth);
cmd.SetRandomWriteTarget(2, GetNormalBuffer());
cmd.DrawProcedural(Matrix4x4.identity, passMaterial, PASS_BLUR_AND_ENCODE, MeshTopology.Triangles, 3, 1);

// free temporary buffers
cmd.ReleaseTemporaryRT(rtStencil);
cmd.ReleaseTemporaryRT(rtRegions);
cmd.ReleaseTemporaryRT(rtDecoded);
}

41
Runtime/CustomPass/NormalBufferBlurPass.shader


{
Properties
{
[HideInInspector] _StencilBit("_StencilBit", Int) = 4
[HideInInspector] _StencilBit("_StencilBit", Int) = 64// UserStencilUsage.UserBit0
}
HLSLINCLUDE

#include "Packages/com.unity.render-pipelines.high-definition/Runtime/ShaderLibrary/ShaderVariables.hlsl"
#include "Packages/com.unity.render-pipelines.high-definition/Runtime/Material/NormalBuffer.hlsl"
#include "Packages/com.unity.render-pipelines.high-definition/Runtime/Material/Decal/DecalUtilities.hlsl"
TEXTURE2D_X(_InputDepth);
Texture2D<float> _NormalBufferBlur_Regions;
Texture2D<float4> _NormalBufferBlur_Decoded;

{
float3 positionOS : POSITION;
};
struct SurfaceVaryings
{
float4 positionCS : SV_POSITION;

uint vertexID : SV_VertexID;
UNITY_VERTEX_INPUT_INSTANCE_ID
};
struct FullScreenVaryings
{
float4 positionCS : SV_POSITION;

return output;
}
float FragSurface_Mark(SurfaceVaryings input) : SV_Target0
{
return 0.0;// 0 == interior
}
FullScreenVaryings VertFullScreen(FullScreenAttributes input)
{
FullScreenVaryings output;

return output;
}
float FragFullScreen_CopyDepth(FullScreenVaryings input) : SV_Depth
float FragSurface_Mark(SurfaceVaryings input) : SV_Target0
UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(input);
uint2 positionSS = uint2(input.positionCS.xy);
return _InputDepth[COORD_TEXTURE2D_X(positionSS)].x;
return 0.0;// 0 == interior
}
[earlydepthstencil]

Pass// == 0
{
Name "CopyDepth"
ZTest Always
ZWrite On
HLSLPROGRAM
#pragma vertex VertFullScreen
#pragma fragment FragFullScreen_CopyDepth
ENDHLSL
}
Pass// == 1
{
Name "Mark"
ZTest LEqual

{
WriteMask [_StencilBit]
ReadMask 0
Comp Equal
Comp Always
Pass Replace
}

ENDHLSL
}
Pass// == 2
Pass// == 1
{
Name "Decode"

ENDHLSL
}
Pass// == 3
Pass// == 2
{
Name "BlurAndEncode"

ENDHLSL
}
Pass// == 4
Pass// == 3
{
Name "BlurAndEncodeAndDecal"

正在加载...
取消
保存