浏览代码

Separated light and cascade data loading for directional lights.

Introduced clear rect rendering to avoid clearing the entire shadowmap.
/main
uygar 6 年前
当前提交
34443229
共有 7 个文件被更改,包括 106 次插入19 次删除
  1. 4
      ScriptableRenderPipeline/Core/CoreRP/ShaderLibrary/Shadow/Shadow.hlsl
  2. 36
      ScriptableRenderPipeline/Core/CoreRP/ShaderLibrary/Shadow/ShadowAlgorithms.hlsl
  3. 12
      ScriptableRenderPipeline/Core/CoreRP/Shadow/Shadow.cs
  4. 4
      ScriptableRenderPipeline/HDRenderPipeline/HDRP/Lighting/DeferredDirectionalShadow.compute
  5. 2
      ScriptableRenderPipeline/HDRenderPipeline/HDRP/Lighting/LightLoop/LightLoop.cs
  6. 58
      ScriptableRenderPipeline/Core/CoreRP/ShaderLibrary/Shadow/Resources/ShadowClear.shader
  7. 9
      ScriptableRenderPipeline/Core/CoreRP/ShaderLibrary/Shadow/Resources/ShadowClear.shader.meta

4
ScriptableRenderPipeline/Core/CoreRP/ShaderLibrary/Shadow/Shadow.hlsl


# pragma warning( disable : 3557 ) // loop only executes for 1 iteration(s)
#endif
#if SHADOW_OPTIMIZE_REGISTER_USAGE == 1
# pragma warning(disable : 3557) // loop only executes for 1 iteration(s)
#endif
#include "CoreRP/Shadow/ShadowBase.cs.hlsl" // ShadowData definition, auto generated (don't modify)
#include "ShadowTexFetch.hlsl" // Resource sampling definitions (don't modify)

36
ScriptableRenderPipeline/Core/CoreRP/ShaderLibrary/Shadow/ShadowAlgorithms.hlsl


[branch]
if( shadowType == GPUSHADOWTYPE_POINT )
{
sd.rot0 = shadowContext.shadowDatas[index + EvalShadow_GetCubeFaceID( L ) + 1].rot0; \
sd.rot1 = shadowContext.shadowDatas[index + EvalShadow_GetCubeFaceID( L ) + 1].rot1; \
sd.rot2 = shadowContext.shadowDatas[index + EvalShadow_GetCubeFaceID( L ) + 1].rot2; \
sd.rot0 = shadowContext.shadowDatas[index + EvalShadow_GetCubeFaceID( L ) + 1].rot0;
sd.rot1 = shadowContext.shadowDatas[index + EvalShadow_GetCubeFaceID( L ) + 1].rot1;
sd.rot2 = shadowContext.shadowDatas[index + EvalShadow_GetCubeFaceID( L ) + 1].rot2;
sd.shadowToWorld = shadowContext.shadowDatas[index + EvalShadow_GetCubeFaceID( L ) + 1].shadowToWorld;
sd.scaleOffset.zw = shadowContext.shadowDatas[index + EvalShadow_GetCubeFaceID( L ) + 1].scaleOffset.zw;
sd.slice = shadowContext.shadowDatas[index + EvalShadow_GetCubeFaceID( L ) + 1].slice;

return offset + 4;
}
void EvalShadow_LoadCascadeData( ShadowContext shadowContext, uint index, inout ShadowData sd )
{
sd.shadowToWorld = shadowContext.shadowDatas[index].shadowToWorld;
sd.proj = shadowContext.shadowDatas[index].proj;
sd.pos = shadowContext.shadowDatas[index].pos;
sd.scaleOffset.zw = shadowContext.shadowDatas[index].scaleOffset.zw;
sd.slice = shadowContext.shadowDatas[index].slice;
sd.viewBias.w = shadowContext.shadowDatas[index].viewBias.w;
}
real EvalShadow_CascadedDepth_Blend( ShadowContext shadowContext, real3 positionWS, real3 normalWS, int index, real3 L )
{
// load the right shadow data for the current face

real border = borders[shadowSplitIndex];
real alpha = border <= 0.0 ? 0.0 : saturate( (relDistance - (1.0 - border)) / border );
ShadowData sd = shadowContext.shadowDatas[index + 1 + shadowSplitIndex];
ShadowData sd = shadowContext.shadowDatas[index];
EvalShadow_LoadCascadeData( shadowContext, index + 1 + shadowSplitIndex, sd );
// sample the texture
uint texIdx, sampIdx;

if( alpha > 0.0 )
{
sd = shadowContext.shadowDatas[index + 1 + shadowSplitIndex];
EvalShadow_LoadCascadeData( shadowContext, index + 1 + shadowSplitIndex, sd );
positionWS = EvalShadow_ReceiverBias( sd, orig_pos, normalWS, L, 1.0, recvBiasWeight, false );
posTC = EvalShadow_GetTexcoords( sd, positionWS, posNDC, false, false );
// sample the texture

real border = borders[shadowSplitIndex]; \
real alpha = border <= 0.0 ? 0.0 : saturate( (relDistance - (1.0 - border)) / border ); \
\
ShadowData sd = shadowContext.shadowDatas[index + 1 + shadowSplitIndex]; \
ShadowData sd = shadowContext.shadowDatas[index]; \
EvalShadow_LoadCascadeData( shadowContext, index + 1 + shadowSplitIndex, sd ); \
\
/* normal based bias */ \
real3 orig_pos = positionWS; \

\
if( alpha > 0.0 ) \
{ \
sd = shadowContext.shadowDatas[index + 1 + shadowSplitIndex]; \
EvalShadow_LoadCascadeData( shadowContext, index + 1 + shadowSplitIndex, sd ); \
positionWS = EvalShadow_ReceiverBias( sd, orig_pos, normalWS, L, 1.0, recvBiasWeight, false ); \
posTC = EvalShadow_GetTexcoords( sd, positionWS, posNDC, false, false ); \
/* sample the texture */ \

real border = borders[shadowSplitIndex];
real alpha = border <= 0.0 ? 0.0 : saturate( (relDistance - (1.0 - border)) / border );
ShadowData sd = shadowContext.shadowDatas[index + 1 + shadowSplitIndex];
ShadowData sd = shadowContext.shadowDatas[index];
EvalShadow_LoadCascadeData( shadowContext, index + 1 + shadowSplitIndex, sd );
// get texture description
uint texIdx, sampIdx;

if( shadowSplitIndex < nextSplit && step( EvalShadow_hash12( posTC.xy ), alpha ) )
{
sd = shadowContext.shadowDatas[index + 1 + nextSplit];
EvalShadow_LoadCascadeData( shadowContext, index + 1 + nextSplit, sd );
positionWS = EvalShadow_ReceiverBias( sd, orig_pos, normalWS, L, 1.0, recvBiasWeight, false );
posTC = EvalShadow_GetTexcoords( sd, positionWS, false );
}

real border = borders[shadowSplitIndex]; \
real alpha = border <= 0.0 ? 0.0 : saturate( (relDistance - (1.0 - border)) / border ); \
\
ShadowData sd = shadowContext.shadowDatas[index + 1 + shadowSplitIndex]; \
ShadowData sd = shadowContext.shadowDatas[index]; \
EvalShadow_LoadCascadeData( shadowContext, index + 1 + shadowSplitIndex, sd ); \
\
/* normal based bias */ \
real3 orig_pos = positionWS; \

\
if( shadowSplitIndex != nextSplit && step( EvalShadow_hash12( posTC.xy ), alpha ) ) \
{ \
sd = shadowContext.shadowDatas[index + 1 + nextSplit]; \
EvalShadow_LoadCascadeData( shadowContext, index + 1 + nextSplit, sd ); \
positionWS = EvalShadow_ReceiverBias( sd, orig_pos, normalWS, L, 1.0, recvBiasWeight, false ); \
posTC = EvalShadow_GetTexcoords( sd, positionWS, false ); \
} \

12
ScriptableRenderPipeline/Core/CoreRP/Shadow/Shadow.cs


protected float[] m_TmpBorders = new float[((k_MaxCascadesInShader+3)/4)*4];
protected ShadowAlgoVector m_SupportedAlgorithms = new ShadowAlgoVector( 0, false );
protected Material m_DebugMaterial = null;
private Material m_ClearMat;
private readonly VectorArray<CachedEntry>.Cleanup m_Cleanup;
private readonly VectorArray<CachedEntry>.Comparator<Key> m_Comparator;
public bool captureFrame { get; set; }

public ShadowAtlas( ref AtlasInit init ) : base( ref init.baseInit )
{
m_ClearMat = new Material( Shader.Find( "Hidden/ScriptableRenderPipeline/ShadowClear" ) );
m_Cleanup = (CachedEntry entry) => { Free( entry ); };
m_Comparator = (ref Key k, ref CachedEntry entry) => { return k.id == entry.key.id && k.faceIdx == entry.key.faceIdx; };

m_Shadowmap = new RenderTexture( (int) m_Width, (int) m_Height, (int) m_ShadowmapBits, m_ShadowmapFormat, RenderTextureReadWrite.Linear );
CreateShadowmap( m_Shadowmap );
m_Shadowmap.Create();
#if false && UNITY_PS4 && !UNITY_EDITOR
if( m_Shadowmap != null )
UnityEngine.PS4.RenderSettings.DisableDepthBufferCompression( m_Shadowmap );
#endif
}
virtual protected void CreateShadowmap( RenderTexture shadowmap )

cb.GetTemporaryRT(m_TempDepthId, (int)m_Width, (int)m_Height, (int)m_ShadowmapBits, FilterMode.Bilinear, RenderTextureFormat.Shadowmap, RenderTextureReadWrite.Default);
cb.SetRenderTarget( new RenderTargetIdentifier( m_TempDepthId ) );
}
cb.ClearRenderTarget( true, !IsNativeDepth(), m_ClearColor );
}
override public void Update( FrameId frameId, ScriptableRenderContext renderContext, CommandBuffer cmd, CullResults cullResults, List<VisibleLight> lights)

}
cmd.SetViewport( m_EntryCache[i].current.viewport );
cbName = "Shadowmap.ClearRect";
cmd.BeginSample( cbName );
CoreUtils.DrawFullScreen( cmd, m_ClearMat, null, SystemInfo.usesReversedZBuffer ? 0 : 1 );
cmd.EndSample( cbName );
cmd.SetViewProjectionMatrices( m_EntryCache[i].current.view, m_EntryCache[i].current.proj );
cmd.SetGlobalVector( "g_vLightDirWs", m_EntryCache[i].current.lightDir );
cmd.SetGlobalFloat( m_ZClipId, m_EntryCache[i].zclip ? 1.0f : 0.0f );

4
ScriptableRenderPipeline/HDRenderPipeline/HDRP/Lighting/DeferredDirectionalShadow.compute


RWTexture2D<float4> _DeferredShadowTextureUAV;
CBUFFER_START(DeferredShadowParameters)
float _DirectionalShadowIndex;
uint _DirectionalShadowIndex;
float3 _LightDirection;
float4 _ScreenSpaceShadowsParameters;
int _SampleCount;

#endif
ShadowContext shadowContext = InitShadowContext();
float shadow = GetDirectionalShadowAttenuation(shadowContext, posInput.positionWS, nrm, (uint)_DirectionalShadowIndex, _LightDirection);
float shadow = GetDirectionalShadowAttenuation(shadowContext, posInput.positionWS, nrm, _DirectionalShadowIndex, _LightDirection);
#ifdef ENABLE_CONTACT_SHADOWS
float contactShadow = 1.0f;

2
ScriptableRenderPipeline/HDRenderPipeline/HDRP/Lighting/LightLoop/LightLoop.cs


cmd.SetComputeIntParam(deferredDirectionalShadowComputeShader, HDShaderIDs._DirectionalContactShadowSampleCount, (int)asd.contactShadowSampleCount);
}
cmd.SetComputeFloatParam(deferredDirectionalShadowComputeShader, HDShaderIDs._DirectionalShadowIndex, (float)m_CurrentSunLightShadowIndex);
cmd.SetComputeIntParam(deferredDirectionalShadowComputeShader, HDShaderIDs._DirectionalShadowIndex, m_CurrentSunLightShadowIndex);
cmd.SetComputeVectorParam(deferredDirectionalShadowComputeShader, HDShaderIDs._DirectionalLightDirection, -m_CurrentSunLight.transform.forward);
cmd.SetComputeTextureParam(deferredDirectionalShadowComputeShader, kernel, HDShaderIDs._DeferredShadowTextureUAV, deferredShadowRT);
cmd.SetComputeTextureParam(deferredDirectionalShadowComputeShader, kernel, HDShaderIDs._MainDepthTexture, depthTexture);

58
ScriptableRenderPipeline/Core/CoreRP/ShaderLibrary/Shadow/Resources/ShadowClear.shader


Shader "Hidden/ScriptableRenderPipeline/ShadowClear"
{
HLSLINCLUDE
#pragma target 4.5
#pragma only_renderers d3d11 ps4 xboxone vulkan metal
#include "CoreRP/ShaderLibrary/Common.hlsl"
float4 Frag() : SV_Target { return 0.0.xxxx; }
ENDHLSL
SubShader
{
Pass
{
Name "ClearShadow_0"
ZTest Always
Cull Off
ZWrite On
HLSLPROGRAM
#pragma vertex Vert_0
#pragma fragment Frag
float4 Vert_0( uint vertexID : VERTEXID_SEMANTIC ) : SV_POSITION
{
return GetFullScreenTriangleVertexPosition( vertexID, 0.0 );
}
ENDHLSL
}
Pass
{
Name "ClearShadow_1"
ZTest Always
Cull Off
ZWrite On
HLSLPROGRAM
#pragma vertex Vert_1
#pragma fragment Frag
float4 Vert_1( uint vertexID : VERTEXID_SEMANTIC ) : SV_POSITION
{
return GetFullScreenTriangleVertexPosition( vertexID, 1.0 );
}
ENDHLSL
}
}
Fallback Off
}

9
ScriptableRenderPipeline/Core/CoreRP/ShaderLibrary/Shadow/Resources/ShadowClear.shader.meta


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