浏览代码

Merge pull request #1223 from Unity-Technologies/lw/get_rid_of_depth_prepass_when_possible

Lw/get rid of depth prepass when possible
/main
GitHub 6 年前
当前提交
98944227
共有 6 个文件被更改,包括 137 次插入23 次删除
  1. 6
      ScriptableRenderPipeline/LightweightPipeline/LWRP/Data/LightweightPipelineAsset.cs
  2. 3
      ScriptableRenderPipeline/LightweightPipeline/LWRP/Data/LightweightPipelineResources.asset
  3. 3
      ScriptableRenderPipeline/LightweightPipeline/LWRP/Data/LightweightPipelineResources.cs
  4. 70
      ScriptableRenderPipeline/LightweightPipeline/LWRP/LightweightPipeline.cs
  5. 69
      ScriptableRenderPipeline/LightweightPipeline/LWRP/Shaders/LightweightCopyDepthMSAA.shader
  6. 9
      ScriptableRenderPipeline/LightweightPipeline/LWRP/Shaders/LightweightCopyDepthMSAA.shader.meta

6
ScriptableRenderPipeline/LightweightPipeline/LWRP/Data/LightweightPipelineAsset.cs


{
get { return resources != null ? resources.CopyDepthShader : null; }
}
public Shader CopyDepthMSAAShader
{
get { return resources != null ? resources.CopyDepthMSAAShader : null; }
}
public Shader ScreenSpaceShadowShader
{
get { return resources != null ? resources.ScreenSpaceShadowShader : null; }

3
ScriptableRenderPipeline/LightweightPipeline/LWRP/Data/LightweightPipelineResources.asset


--- !u!114 &11400000
MonoBehaviour:
m_ObjectHideFlags: 0
m_PrefabParentObject: {fileID: 0}
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInternal: {fileID: 0}
m_GameObject: {fileID: 0}
m_Enabled: 1

m_EditorClassIdentifier:
BlitShader: {fileID: 4800000, guid: c17132b1f77d20942aa75f8429c0f8bc, type: 3}
CopyDepthShader: {fileID: 4800000, guid: d6dae50ee9e1bfa4db75f19f99355220, type: 3}
CopyDepthMSAAShader: {fileID: 4800000, guid: 3aaf3bbc87e49413fa5019b840757406, type: 3}
ScreenSpaceShadowShader: {fileID: 4800000, guid: 0f854b35a0cf61a429bd5dcfea30eddd,
type: 3}
SamplingShader: {fileID: 4800000, guid: 04c410c9937594faa893a11dceb85f7e, type: 3}

3
ScriptableRenderPipeline/LightweightPipeline/LWRP/Data/LightweightPipelineResources.cs


{
public Shader BlitShader;
public Shader CopyDepthShader;
public Shader CopyDepthMSAAShader;
}
}

70
ScriptableRenderPipeline/LightweightPipeline/LWRP/LightweightPipeline.cs


private float[] m_OpaqueScalerValues = {1.0f, 0.5f, 0.25f, 0.25f};
private float m_RenderScale;
private int m_MSAASamples;
private bool m_IntermediateTextureArray;
private bool m_RequireDepthTexture;

private CameraComparer m_CameraComparer = new CameraComparer();
private Material m_BlitMaterial;
private Material m_CopyDepthNoMSAAMaterial;
private Material m_CopyDepthMSAAMaterial;
private int m_SampleCount;
private int m_BlitTexID = Shader.PropertyToID("_BlitTex");
private CopyTextureSupport m_CopyTextureSupport;

CameraRenderTargetID.opaque = Shader.PropertyToID("_CameraOpaqueTexture");
m_SampleOffset = Shader.PropertyToID("_SampleOffset");
m_SampleCount = Shader.PropertyToID("_SampleCount");
m_ColorRT = new RenderTargetIdentifier(CameraRenderTargetID.color);
m_CopyColorRT = new RenderTargetIdentifier(CameraRenderTargetID.copyColor);

Shader.globalRenderPipeline = "LightweightPipeline";
m_BlitMaterial = CoreUtils.CreateEngineMaterial(m_Asset.BlitShader);
m_CopyDepthMaterial = CoreUtils.CreateEngineMaterial(m_Asset.CopyDepthShader);
m_CopyDepthNoMSAAMaterial = CoreUtils.CreateEngineMaterial(m_Asset.CopyDepthShader);
m_CopyDepthMSAAMaterial = CoreUtils.CreateEngineMaterial(m_Asset.CopyDepthMSAAShader);
m_SamplingMaterial = CoreUtils.CreateEngineMaterial(m_Asset.SamplingShader);
m_ErrorMaterial = CoreUtils.CreateEngineMaterial("Hidden/InternalErrorShader");
}

SupportedRenderingFeatures.active = new SupportedRenderingFeatures();
m_CopyDepthMaterial = null;
CoreUtils.Destroy(m_CopyDepthMaterial);
CoreUtils.Destroy(m_CopyDepthNoMSAAMaterial);
CoreUtils.Destroy(m_CopyDepthMSAAMaterial);
CoreUtils.Destroy(m_BlitMaterial);
CoreUtils.Destroy(m_SamplingMaterial);

if (LightweightUtils.HasFlag(config, FrameRenderingConfiguration.DepthCopy))
{
CopyTexture(cmd, m_DepthRT, m_CopyDepth, m_CopyDepthMaterial);
m_CopyDepthMSAAMaterial.SetFloat(m_SampleCount, (float)m_MSAASamples);
bool forceBlit = m_CopyDepthMaterial == m_CopyDepthMSAAMaterial;
CopyTexture(cmd, m_DepthRT, m_CopyDepth, m_CopyDepthMaterial, forceBlit);
cmd.SetGlobalTexture(CameraRenderTargetID.depth, m_CopyDepth);
}
if (setRenderTarget)

}
if(m_Asset.RequireOpaqueTexture)
if (m_Asset.RequireOpaqueTexture)
{
OpaqueTexturePass(ref context, config);
}

private void SetupFrameRenderingConfiguration(out FrameRenderingConfiguration configuration, bool screenspaceShadows, bool stereoEnabled, bool sceneViewCamera)
{
m_CopyDepthMaterial = null;
configuration = (stereoEnabled) ? FrameRenderingConfiguration.Stereo : FrameRenderingConfiguration.None;
if (stereoEnabled && XRSettings.eyeTextureDesc.dimension == TextureDimension.Tex2DArray)
m_IntermediateTextureArray = true;

// TODO: PostProcessing and SoftParticles are currently not support for VR
bool postProcessEnabled = m_CameraPostProcessLayer != null && m_CameraPostProcessLayer.enabled && !stereoEnabled;
m_RequireDepthTexture = m_Asset.RequireDepthTexture && !stereoEnabled;
bool canSkipDepthCopy = !m_RequireDepthTexture;
if (postProcessEnabled)
{
m_RequireDepthTexture = true;

}
if (sceneViewCamera)
{
canSkipDepthCopy = false;
}
canSkipDepthCopy = canSkipDepthCopy && !screenspaceShadows;
canSkipDepthCopy = false;
// If msaa is enabled we don't use a depth renderbuffer as we might not have support to Texture2DMS to resolve depth.
// Instead we use a depth prepass and whenever depth is needed we use the 1 sample depth from prepass.
// Screen space shadows require depth before opaque shading.
if (!msaaEnabled && !screenspaceShadows)
{
bool supportsDepthCopy = m_CopyTextureSupport != CopyTextureSupport.None && m_Asset.CopyDepthShader.isSupported;
m_DepthRenderBuffer = true;
intermediateTexture = true;
bool copyShaderSupported = (msaaEnabled && m_Asset.CopyDepthMSAAShader.isSupported) || (!msaaEnabled && m_Asset.CopyDepthShader.isSupported);
bool supportsDepthCopy = m_CopyTextureSupport != CopyTextureSupport.None || copyShaderSupported;
bool requiresDepthPrepassToResolveMSAA = msaaEnabled && (SystemInfo.supportsMultisampledTextures == 0);
bool requiresDepthPrepass = !supportsDepthCopy || screenspaceShadows || requiresDepthPrepassToResolveMSAA;
// If requiring a camera depth texture we need separate depth as it reads/write to depth at same time
// Post process doesn't need the copy
if (!m_Asset.RequireDepthTexture && postProcessEnabled)
configuration |= (supportsDepthCopy) ? FrameRenderingConfiguration.DepthCopy : FrameRenderingConfiguration.DepthPrePass;
}
else
m_DepthRenderBuffer = !requiresDepthPrepass;
intermediateTexture = intermediateTexture || m_DepthRenderBuffer;
if (requiresDepthPrepass)
configuration |= FrameRenderingConfiguration.DepthPrePass;
else if (supportsDepthCopy && !canSkipDepthCopy)
configuration |= FrameRenderingConfiguration.DepthPrePass;
configuration |= FrameRenderingConfiguration.DepthCopy;
m_CopyDepthMaterial = msaaEnabled ? m_CopyDepthMSAAMaterial : m_CopyDepthNoMSAAMaterial;
}
}

int msaaSamples = (m_IsOffscreenCamera) ? Math.Min(m_CurrCamera.targetTexture.antiAliasing, m_Asset.MSAASampleCount) : m_Asset.MSAASampleCount;
msaaSamples = (LightweightUtils.HasFlag(renderingConfig, FrameRenderingConfiguration.Msaa)) ? msaaSamples : 1;
m_MSAASamples = msaaSamples;
m_CurrCameraColorRT = BuiltinRenderTextureType.CameraTarget;
if (LightweightUtils.HasFlag(renderingConfig, FrameRenderingConfiguration.IntermediateTexture) || m_RequireDepthTexture)

depthRTDesc.colorFormat = RenderTextureFormat.Depth;
depthRTDesc.depthBufferBits = kDepthStencilBufferBits;
// This also means we don't do a depth copy
bool doesDepthPrepass = LightweightUtils.HasFlag(renderingConfig, FrameRenderingConfiguration.DepthPrePass);
if (!doesDepthPrepass)
{
depthRTDesc.bindMS = msaaSamples > 1;
depthRTDesc.msaaSamples = msaaSamples;
}
if (LightweightUtils.HasFlag(renderingConfig, FrameRenderingConfiguration.DepthCopy))
if (!doesDepthPrepass)
{
depthRTDesc.bindMS = false;
depthRTDesc.msaaSamples = 1;
}
m_ShadowPass.InitializeResources(cmd, baseDesc);
}

if (!m_IsOffscreenCamera)
colorRT = m_CurrCameraColorRT;
if (m_RequireDepthTexture)
if (m_DepthRenderBuffer)
depthRT = m_DepthRT;
}

69
ScriptableRenderPipeline/LightweightPipeline/LWRP/Shaders/LightweightCopyDepthMSAA.shader


Shader "Hidden/LightweightPipeline/CopyDepthMSAA"
{
Properties
{
[HideInInspector] _SampleCount("MSAA sample count", Float) = 1.0
}
SubShader
{
Tags { "RenderType" = "Opaque" "RenderPipeline" = "LightiweightPipeline"}
Pass
{
ZTest Always ZWrite On ColorMask 0
HLSLPROGRAM
#pragma exclude_renderers d3d11_9x
#pragma vertex vert
#pragma fragment frag
#pragma require msaatex
#include "LWRP/ShaderLibrary/Core.hlsl"
Texture2DMS<float> _CameraDepthTexture;
float _SampleCount;
float4 _CameraDepthTexture_TexelSize;
struct VertexInput
{
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
};
struct VertexOutput
{
float4 position : SV_POSITION;
float2 uv : TEXCOORD0;
};
VertexOutput vert(VertexInput i)
{
VertexOutput o;
o.uv = i.uv;
o.position = TransformObjectToHClip(i.vertex.xyz);
return o;
}
float frag(VertexOutput i) : SV_Depth
{
int2 coord = int2(i.uv * _CameraDepthTexture_TexelSize.zw);
int samples = (int)_SampleCount;
#if UNITY_REVERSED_Z
float outDepth = 1.0;
#define DEPTH_OP min
#else
float outDepth = 0.0;
#define DEPTH_OP max
#endif
for (int i = 0; i < samples; ++i)
outDepth = DEPTH_OP(LOAD_TEXTURE2D_MSAA(_CameraDepthTexture, coord, i), outDepth);
return outDepth;
}
ENDHLSL
}
}
}

9
ScriptableRenderPipeline/LightweightPipeline/LWRP/Shaders/LightweightCopyDepthMSAA.shader.meta


fileFormatVersion: 2
guid: 3aaf3bbc87e49413fa5019b840757406
ShaderImporter:
externalObjects: {}
defaultTextures: []
nonModifiableTextures: []
userData:
assetBundleName:
assetBundleVariant:
正在加载...
取消
保存