|
|
|
|
|
|
|
|
|
|
// 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); |
|
|
|
} |
|
|
|