浏览代码

Added a shader that resolves a MSAA depth RT to execute on platforms that support tex2dms.

The only setup when we can skip depth copy and depth prepass is postfx + no screen-space shadows + no MSAA.
/main
Aleksandr Kirillov 7 年前
当前提交
34562958
共有 6 个文件被更改,包括 126 次插入9 次删除
  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. 43
      ScriptableRenderPipeline/LightweightPipeline/LWRP/LightweightPipeline.cs
  5. 71
      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;
}
}

43
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))
{
m_CopyDepthMSAAMaterial.SetFloat(m_SampleCount, (float)m_MSAASamples);
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;

{
configuration |= FrameRenderingConfiguration.Msaa;
intermediateTexture = intermediateTexture || !LightweightUtils.PlatformSupportsMSAABackBuffer();
canSkipDepthCopy = false;
bool supportsDepthCopy = m_CopyTextureSupport != CopyTextureSupport.None || m_Asset.CopyDepthShader.isSupported;
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 (supportsDepthCopy && !canSkipDepthCopy)
configuration |= FrameRenderingConfiguration.DepthCopy;
else if (supportsDepthCopy && !canSkipDepthCopy)
{
configuration |= FrameRenderingConfiguration.DepthCopy;
m_CopyDepthMaterial = msaaEnabled ? m_CopyDepthMSAAMaterial : m_CopyDepthNoMSAAMaterial;
}
}
Rect cameraRect = m_CurrCamera.rect;

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

71
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
// Required to compile gles 2.0 with standard srp library
#pragma prefer_hlslcc gles
#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:
正在加载...
取消
保存