浏览代码

improve forward-compatibility with HDRP 10.x.x

/main
Lasse Jon Fuglsang Pedersen 4 年前
当前提交
0f3911b1
共有 3 个文件被更改,包括 64 次插入53 次删除
  1. 89
      Runtime/CustomPass/NormalBufferBlurPass.cs
  2. 2
      ShaderLibrary/Nodes_Common/IsForwardPass.hlsl
  3. 26
      ShaderLibrary/Nodes_Common/LoadNormalBuffer.hlsl

89
Runtime/CustomPass/NormalBufferBlurPass.cs


using UnityEngine.Rendering;
using UnityEngine.Rendering.HighDefinition;
using UnityEngine.Experimental.Rendering;
#if UNITY_2020_2_OR_NEWER
using UnityEngine.Experimental.Rendering.RenderGraphModule;
#endif
using System.Reflection;
namespace Unity.DemoTeam.DigitalHuman

"ForwardOnly",// HDShaderPassNames.s_ForwardOnlyStr
"SRPDefaultUnlit", // HDShaderPassNames.s_SRPDefaultUnlitStr
"DBufferMesh_3RT",// HDShaderPassNames.s_MeshDecalsMStr
"DBufferMesh",// HDShaderPassNames.s_DBufferMeshStr
};
static ShaderTagId[] NAME_PASS_REPLACE_TAG = null;

const int DBUFFER_NORMALS = 1;
const int DBUFFER_MASK = 2;
private RTHandle[] dbufferRTs;
private RenderTargetIdentifier[] dbufferRTIDs;
private RenderTargetIdentifier[] dbufferNormalMaskRTIDs;
RenderTargetIdentifier[] dbufferNormalMaskRTIDs;
private void FindDbufferRTs(HDRenderPipeline hdPipeline)
void FindDBufferNormalMaskRTIDs(HDRenderPipeline hdPipeline)
dbufferRTIDs = null;
dbufferNormalMaskRTIDs = null;
#if UNITY_2020_2_OR_NEWER
// require alternate path if rendergraph is enabled
var fieldInfo_m_EnableRenderGraph = typeof(HDRenderPipeline).GetField("m_EnableRenderGraph", BindingFlags.NonPublic | BindingFlags.Instance);
if (fieldInfo_m_EnableRenderGraph != null && (bool)fieldInfo_m_EnableRenderGraph.GetValue(hdPipeline) == true)
{
var fieldInfo_m_DBufferOutput = typeof(HDRenderPipeline).GetField("m_DBufferOutput", BindingFlags.NonPublic | BindingFlags.Instance);
if (fieldInfo_m_DBufferOutput != null)
{
var m_DBufferOutput = fieldInfo_m_DBufferOutput.GetValue(hdPipeline);
if (m_DBufferOutput != null)
{
var fieldInfo_mrt = m_DBufferOutput.GetType().GetField("mrt", BindingFlags.Public | BindingFlags.Instance);
if (fieldInfo_mrt != null)
{
var mrt = fieldInfo_mrt.GetValue(m_DBufferOutput) as TextureHandle[];
if (mrt != null)
{
dbufferNormalMaskRTIDs = new RenderTargetIdentifier[2];
dbufferNormalMaskRTIDs[0] = mrt[DBUFFER_NORMALS];
dbufferNormalMaskRTIDs[1] = mrt[DBUFFER_MASK];
}
}
}
}
return;
}
#endif
//Debug.Log("FindDbufferRTs : " + fieldInfo_m_DbufferManager);
var m_DbufferManager = fieldInfo_m_DbufferManager.GetValue(hdPipeline);
if (m_DbufferManager != null)
{

//Debug.Log("FindDbufferRTs : " + fieldInfo_m_RTs);
dbufferRTs = fieldInfo_m_RTs.GetValue(m_DbufferManager) as RTHandle[];
}
var fieldInfo_m_RTIDs = m_DbufferManager.GetType().GetField("m_RTIDs", BindingFlags.NonPublic | BindingFlags.Instance);
if (fieldInfo_m_RTIDs != null)
{
//Debug.Log("FindDbufferRTIDs : " + fieldInfo_m_RTIDs);
dbufferRTIDs = fieldInfo_m_RTIDs.GetValue(m_DbufferManager) as RenderTargetIdentifier[];
var m_RTs = fieldInfo_m_RTs.GetValue(m_DbufferManager) as RTHandle[];
if (m_RTs != null)
{
dbufferNormalMaskRTIDs = new RenderTargetIdentifier[2];
dbufferNormalMaskRTIDs[0] = m_RTs[DBUFFER_NORMALS].nameID;
dbufferNormalMaskRTIDs[1] = m_RTs[DBUFFER_MASK].nameID;
}
private RenderTargetIdentifier[] GetDbufferNormalMaskRTIDs()
{
if (dbufferRTs != null)
{
if (dbufferRTs[DBUFFER_NORMALS] == null || dbufferRTs[DBUFFER_MASK] == null)
{
return null;
}
}
if (dbufferRTIDs != null)
{
if (dbufferNormalMaskRTIDs == null)
dbufferNormalMaskRTIDs = new RenderTargetIdentifier[2];
dbufferNormalMaskRTIDs[0] = dbufferRTIDs[DBUFFER_NORMALS];
dbufferNormalMaskRTIDs[1] = dbufferRTIDs[DBUFFER_MASK];
}
return dbufferNormalMaskRTIDs;
}
static bool EnsureMaterial(ref Material material, string shaderName)
{
if (material != null && material.shader == null)

base.Setup(renderContext, cmd);
base.name = "NormalBufferBlurPass";
FindDbufferRTs(RenderPipelineManager.currentPipeline as HDRenderPipeline);
FindDBufferNormalMaskRTIDs(RenderPipelineManager.currentPipeline as HDRenderPipeline);
EnsureMaterial(ref passMaterial, NAME_SHADER);
if (passMaterial != null)

excludeObjectMotionVectors = false,
};
#if UNITY_2020_2_OR_NEWER
CoreUtils.DrawRendererList(renderContext, cmd, RendererList.Create(renderListDesc));
#else
#endif
// decode normal buffer in marked regions
CoreUtils.SetRenderTarget(cmd,

cmd.SetGlobalTexture(rtRegions, rtRegions);
cmd.SetGlobalTexture(rtDecoded, rtDecoded);
if (GetDbufferNormalMaskRTIDs() != null)
if (dbufferNormalMaskRTIDs != null)
GetDbufferNormalMaskRTIDs(),
dbufferNormalMaskRTIDs,
cameraDepth,
ClearFlag.None);
CoreUtils.SetViewport(cmd, cameraDepth);

2
ShaderLibrary/Nodes_Common/IsForwardPass.hlsl


void IsForwardPass_float(out bool Out)
{
#if defined(SHADERPASS) && (SHADERPASS == SHADERPASS_FORWARD)
#if defined(SHADERPASS) && (SHADERPASS == SHADERPASS_FORWARD)
Out = true;
#else
Out = false;

26
ShaderLibrary/Nodes_Common/LoadNormalBuffer.hlsl


#ifndef __LOADNORMALBUFFER_H__
#define __LOADNORMALBUFFER_H__
#include "Packages/com.unity.render-pipelines.high-definition/Runtime/ShaderLibrary/TextureXR.hlsl"
#ifndef UNITY_COMMON_MATERIAL_INCLUDED// in 7.3.1 there is no include guard in NormalBuffer.hlsl
#include "Packages/com.unity.render-pipelines.high-definition/Runtime/Material/NormalBuffer.hlsl"
#endif
void LoadNormalBuffer_float(in float2 positionSS, out float3 normalWS, out float smoothness)
{
void LoadNormalBuffer_float(in float2 positionSS, out float3 normalWS, out float smoothness)
{
NormalData normalData;
DecodeFromNormalBuffer(positionSS.xy, normalData);
normalWS = normalData.normalWS;
smoothness = 1.0 - normalData.perceptualRoughness;
}
NormalData normalData;
DecodeFromNormalBuffer(positionSS.xy, normalData);
normalWS = normalData.normalWS;
smoothness = 1.0 - normalData.perceptualRoughness;
void LoadNormalBuffer_float(in float2 positionSS, out float3 normalWS, out float smoothness)
{
normalWS = float3(0, 0, 1);
smoothness = 0.5;
}
normalWS = float3(0, 0, 1);
smoothness = 0.5;
}
#endif//__LOADNORMALBUFFER_H__
正在加载...
取消
保存