浏览代码

Merge pull request #1562 from Unity-Technologies/Port-PCSS

Port PCSS shadows
/main
GitHub 6 年前
当前提交
5bbb2099
共有 7 个文件被更改,包括 324 次插入22 次删除
  1. 8
      com.unity.render-pipelines.core/CoreRP/ShaderLibrary/Macros.hlsl
  2. 52
      com.unity.render-pipelines.core/CoreRP/ShaderLibrary/Shadow/ShadowSampling.hlsl
  3. 100
      com.unity.render-pipelines.core/CoreRP/Shadow/Shadow.cs
  4. 2
      com.unity.render-pipelines.core/CoreRP/Shadow/ShadowBase.cs
  5. 1
      com.unity.render-pipelines.core/CoreRP/Shadow/ShadowBase.cs.hlsl
  6. 174
      com.unity.render-pipelines.core/CoreRP/ShaderLibrary/Shadow/PCSS.hlsl
  7. 9
      com.unity.render-pipelines.core/CoreRP/ShaderLibrary/Shadow/PCSS.hlsl.meta

8
com.unity.render-pipelines.core/CoreRP/ShaderLibrary/Macros.hlsl


#define TRANSFORM_TEX(tex, name) ((tex.xy) * name##_ST.xy + name##_ST.zw)
#define GET_TEXELSIZE_NAME(name) (name##_TexelSize)
#if UNITY_REVERSED_Z
# define COMPARE_DEVICE_DEPTH_CLOSER(shadowMapDepth, zDevice) (shadowMapDepth > zDevice)
# define COMPARE_DEVICE_DEPTH_CLOSEREQUAL(shadowMapDepth, zDevice) (shadowMapDepth >= zDevice)
#else
# define COMPARE_DEVICE_DEPTH_CLOSER(shadowMapDepth, zDevice) (shadowMapDepth < zDevice)
# define COMPARE_DEVICE_DEPTH_CLOSEREQUAL(shadowMapDepth, zDevice) (shadowMapDepth <= zDevice)
#endif
#endif // UNITY_MACROS_INCLUDED

52
com.unity.render-pipelines.core/CoreRP/ShaderLibrary/Shadow/ShadowSampling.hlsl


return (z[1] < 0.0 || z[2] > 1.0) ? ShadowMoments_SolveDelta4MSM( z, b, lightLeakBias ) : ShadowMoments_SolveDelta3MSM( z, b.xy, lightLeakBias );
}
#include "PCSS.hlsl"
real SampleShadow_PCSS( ShadowContext shadowContext, inout uint payloadOffset, real3 tcs, real4 scaleOffset, real2 sampleBias, float slice, uint texIdx, uint sampIdx )
{
real2 params = asfloat(shadowContext.payloads[payloadOffset].xy);
real shadowSoftnesss = params.x;
int sampleCount = params.y;
payloadOffset++;
real2 sampleJitter = real2(sin(GenerateHashedRandomFloat(tcs.x)),
cos(GenerateHashedRandomFloat(tcs.y)));
//1) Blocker Search
real averageBlockerDepth = 0.0;
real numBlockers = 0.0;
if (!BlockerSearch(averageBlockerDepth, numBlockers, shadowSoftnesss + 0.000001, tcs, sampleJitter, sampleBias, shadowContext, slice, texIdx, sampIdx, sampleCount))
return 1.0;
//2) Penumbra Estimation
real filterSize = shadowSoftnesss * PenumbraSize(tcs.z, averageBlockerDepth);
filterSize = max(filterSize, 0.000001);
//3) Filter
return PCSS(tcs, filterSize, scaleOffset, slice, sampleBias, sampleJitter, shadowContext, texIdx, sampIdx, sampleCount);
}
real SampleShadow_PCSS( ShadowContext shadowContext, inout uint payloadOffset, real3 tcs, real4 scaleOffset, real2 sampleBias, float slice, Texture2DArray tex, SamplerComparisonState compSamp, SamplerState samp )
{
real2 params = asfloat(shadowContext.payloads[payloadOffset].xy);
real shadowSoftnesss = params.x;
int sampleCount = params.y;
payloadOffset++;
real2 sampleJitter = real2(sin(GenerateHashedRandomFloat(tcs.x)),
cos(GenerateHashedRandomFloat(tcs.y)));
//1) Blocker Search
real averageBlockerDepth = 0.0;
real numBlockers = 0.0;
if (!BlockerSearch(averageBlockerDepth, numBlockers, shadowSoftnesss + 0.000001, tcs, slice, sampleJitter, sampleBias, tex, samp, sampleCount))
return 1.0;
//2) Penumbra Estimation
real filterSize = shadowSoftnesss * PenumbraSize(tcs.z, averageBlockerDepth);
filterSize = max(filterSize, 0.000001);
//3) Filter
return PCSS(tcs, filterSize, scaleOffset, slice, sampleBias, sampleJitter, tex, compSamp, sampleCount);
}
//-----------------------------------------------------------------------------------------------------
// helper function to dispatch a specific shadow algorithm
real SampleShadow_SelectAlgorithm( ShadowContext shadowContext, ShadowData shadowData, inout uint payloadOffset, real3 posTC, real2 sampleBias, uint algorithm, uint texIdx, uint sampIdx )

case GPUSHADOWALGORITHM_PCF_TENT_3X3 : return SampleShadow_PCF_Tent_3x3( shadowContext, payloadOffset, shadowData.textureSize, shadowData.texelSizeRcp, posTC, sampleBias, shadowData.slice, texIdx, sampIdx );
case GPUSHADOWALGORITHM_PCF_TENT_5X5 : return SampleShadow_PCF_Tent_5x5( shadowContext, payloadOffset, shadowData.textureSize, shadowData.texelSizeRcp, posTC, sampleBias, shadowData.slice, texIdx, sampIdx );
case GPUSHADOWALGORITHM_PCF_TENT_7X7 : return SampleShadow_PCF_Tent_7x7( shadowContext, payloadOffset, shadowData.textureSize, shadowData.texelSizeRcp, posTC, sampleBias, shadowData.slice, texIdx, sampIdx );
case GPUSHADOWALGORITHM_PCSS : return SampleShadow_PCSS( shadowContext, payloadOffset, posTC, shadowData.scaleOffset, sampleBias, shadowData.slice, texIdx, sampIdx );
case GPUSHADOWALGORITHM_VSM : return SampleShadow_VSM_1tap( shadowContext, payloadOffset, posTC, shadowData.slice, texIdx, sampIdx );
case GPUSHADOWALGORITHM_EVSM_2 : return SampleShadow_EVSM_1tap( shadowContext, payloadOffset, posTC, shadowData.slice, texIdx, sampIdx, false );
case GPUSHADOWALGORITHM_EVSM_4 : return SampleShadow_EVSM_1tap( shadowContext, payloadOffset, posTC, shadowData.slice, texIdx, sampIdx, true );

case GPUSHADOWALGORITHM_PCF_TENT_3X3 : return SampleShadow_PCF_Tent_3x3( shadowContext, payloadOffset, shadowData.textureSize, shadowData.texelSizeRcp, posTC, sampleBias, shadowData.slice, tex, compSamp );
case GPUSHADOWALGORITHM_PCF_TENT_5X5 : return SampleShadow_PCF_Tent_5x5( shadowContext, payloadOffset, shadowData.textureSize, shadowData.texelSizeRcp, posTC, sampleBias, shadowData.slice, tex, compSamp );
case GPUSHADOWALGORITHM_PCF_TENT_7X7 : return SampleShadow_PCF_Tent_7x7( shadowContext, payloadOffset, shadowData.textureSize, shadowData.texelSizeRcp, posTC, sampleBias, shadowData.slice, tex, compSamp );
case GPUSHADOWALGORITHM_PCSS : return SampleShadow_PCSS( shadowContext, payloadOffset, posTC, shadowData.scaleOffset, sampleBias, shadowData.slice, tex, compSamp, s_point_clamp_sampler );
default: return 1.0;
}

100
com.unity.render-pipelines.core/CoreRP/Shadow/Shadow.cs


readonly ValRange m_DefPCF_DepthBias = new ValRange("Depth Bias", 0.0f, 0.0f, 1.0f, 000.1f);
readonly ValRange m_DefPCF_FilterSize = new ValRange("Filter Size", 1.0f, 1.0f, 10.0f, 1.0f);
readonly ValRange m_DefPCSS_ShadowSoftness = new ValRange("Shadow Softness", 0.0f, 0.5f, 1.0f, 0.01f);
readonly ValRange m_DefPCSS_SampleCount = new ValRange("Sample Count", 1, 32, 64, 1);
public ShadowAtlas(ref AtlasInit init) : base(ref init.baseInit)
{

override protected void Register(GPUShadowType type, ShadowRegistry registry)
{
ShadowPrecision precision = m_ShadowmapBits == 32 ? ShadowPrecision.High : ShadowPrecision.Low;
m_SupportedAlgorithms.Reserve(5);
m_SupportedAlgorithms.Reserve(6);
m_SupportedAlgorithms.AddUniqueUnchecked((int)ShadowUtils.Pack(ShadowAlgorithm.PCSS, ShadowVariant.V0, precision));
ShadowRegistry.VariantDelegate del = (Light l, ShadowAlgorithm dataAlgorithm, ShadowVariant dataVariant, ShadowPrecision dataPrecision, ref int[] dataBlock) =>
{

if (dataVariant == ShadowVariant.V1)
m_DefPCF_FilterSize.Slider(ref dataBlock[1]);
};
ShadowRegistry.VariantDelegate pcssDel = (Light l, ShadowAlgorithm dataAlgorithm, ShadowVariant dataVariant, ShadowPrecision dataPrecision, ref int[] dataBlock) =>
{
CheckDataIntegrity(dataAlgorithm, dataVariant, dataPrecision, ref dataBlock);
m_DefPCSS_ShadowSoftness.Slider(ref dataBlock[0]);
m_DefPCSS_SampleCount.Slider(ref dataBlock[1]);
dataBlock[1] = ShadowUtils.Asint(Mathf.RoundToInt(ShadowUtils.Asfloat(dataBlock[1])));
};
registry.Register(type, precision, ShadowAlgorithm.PCSS, "Percentage Closer Soft Shadows (PCSS)",
new ShadowVariant[] { ShadowVariant.V0 },
new string[] { "poisson 64" },
new ShadowRegistry.VariantDelegate[] { pcssDel });
if (algorithm != ShadowAlgorithm.PCF ||
if ((algorithm != ShadowAlgorithm.PCF && algorithm != ShadowAlgorithm.PCSS) ||
(variant != ShadowVariant.V0 &&
variant != ShadowVariant.V1 &&
variant != ShadowVariant.V2 &&

const int k_BlockSize = 2;
if (dataBlock == null || dataBlock.Length != k_BlockSize)
switch (algorithm)
// set defaults
dataBlock = new int[k_BlockSize];
dataBlock[0] = m_DefPCF_DepthBias.Default();
dataBlock[1] = m_DefPCF_FilterSize.Default();
return false;
case ShadowAlgorithm.PCF:
const int k_PcfBlockSize = 2;
if (dataBlock == null || dataBlock.Length != k_PcfBlockSize)
{
// set defaults
dataBlock = new int[k_PcfBlockSize];
dataBlock[0] = m_DefPCF_DepthBias.Default();
dataBlock[1] = m_DefPCF_FilterSize.Default();
return false;
}
break;
case ShadowAlgorithm.PCSS:
const int k_PcssBlockSize = 2;
if (dataBlock == null || dataBlock.Length != k_PcssBlockSize)
{
// set defaults
dataBlock = new int[k_PcssBlockSize];
dataBlock[0] = m_DefPCSS_ShadowSoftness.Default();
dataBlock[1] = m_DefPCSS_SampleCount.Default();
return false;
}
break;
}
return true;
}

virtual protected uint ReservePayload(ShadowRequest sr)
{
uint payloadSize = sr.shadowType == GPUShadowType.Directional ? (1 + k_MaxCascadesInShader + ((uint)m_TmpBorders.Length / 4)) : 0;
payloadSize += ShadowUtils.ExtractAlgorithm(sr.shadowAlgorithm) == ShadowAlgorithm.PCF ? 1u : 0;
switch (ShadowUtils.ExtractAlgorithm(sr.shadowAlgorithm))
{
case ShadowAlgorithm.PCF:
payloadSize += 1;
break;
case ShadowAlgorithm.PCSS:
payloadSize += 1;
break;
default:
break;
}
return payloadSize;
}

}
ShadowAlgorithm algo; ShadowVariant vari; ShadowPrecision prec;
ShadowUtils.Unpack(sr.shadowAlgorithm, out algo, out vari, out prec);
if (algo == ShadowAlgorithm.PCF)
if (algo == ShadowAlgorithm.PCF || algo == ShadowAlgorithm.PCSS)
{
AdditionalShadowData asd = lights[sr.index].light.GetComponent<AdditionalShadowData>();
if (!asd)

Debug.Log("Fixed up shadow data for algorithm " + algo + ", variant " + vari);
}
switch (vari)
if (algo == ShadowAlgorithm.PCF)
case ShadowVariant.V0:
case ShadowVariant.V1:
case ShadowVariant.V2:
case ShadowVariant.V3:
case ShadowVariant.V4:
switch (vari)
sp.Set(shadowData[0] | (SystemInfo.usesReversedZBuffer ? 1 : 0), shadowData[1], 0, 0);
payload[payloadOffset] = sp;
payloadOffset++;
case ShadowVariant.V0:
case ShadowVariant.V1:
case ShadowVariant.V2:
case ShadowVariant.V3:
case ShadowVariant.V4:
{
sp.Set(shadowData[0] | (SystemInfo.usesReversedZBuffer ? 1 : 0), shadowData[1], 0, 0);
payload[payloadOffset] = sp;
payloadOffset++;
}
break;
break;
}
else if (algo == ShadowAlgorithm.PCSS)
{
sp.Set(shadowData[0], shadowData[1], 0, 0);
payload[payloadOffset] = sp;
payloadOffset++;
}
}
}

2
com.unity.render-pipelines.core/CoreRP/Shadow/ShadowBase.cs


VSM,
EVSM,
MSM,
PCSS,
Custom = 32
};

EVSM_4 = ShadowAlgorithm.EVSM << 3 | ShadowVariant.V1,
MSM_Ham = ShadowAlgorithm.MSM << 3 | ShadowVariant.V0,
MSM_Haus = ShadowAlgorithm.MSM << 3 | ShadowVariant.V1,
PCSS = ShadowAlgorithm.PCSS << 3 | ShadowVariant.V0,
Custom = ShadowAlgorithm.Custom << 3
}

1
com.unity.render-pipelines.core/CoreRP/Shadow/ShadowBase.cs.hlsl


#define GPUSHADOWALGORITHM_EVSM_4 (17)
#define GPUSHADOWALGORITHM_MSM_HAM (24)
#define GPUSHADOWALGORITHM_MSM_HAUS (25)
#define GPUSHADOWALGORITHM_PCSS (32)
#define GPUSHADOWALGORITHM_CUSTOM (256)
// Generated from UnityEngine.Experimental.Rendering.ShadowData

174
com.unity.render-pipelines.core/CoreRP/ShaderLibrary/Shadow/PCSS.hlsl


#include "CoreRP/ShaderLibrary/Common.hlsl"
static const float2 poissonDisk64[64] =
{
float2 ( 0.1187053, 0.7951565),
float2 ( 0.1173675, 0.6087878),
float2 (-0.09958518, 0.7248842),
float2 ( 0.4259812, 0.6152718),
float2 ( 0.3723574, 0.8892787),
float2 (-0.02289676, 0.9972908),
float2 (-0.08234791, 0.5048386),
float2 ( 0.1821235, 0.9673787),
float2 (-0.2137264, 0.9011746),
float2 ( 0.3115066, 0.4205415),
float2 ( 0.1216329, 0.383266),
float2 ( 0.5948939, 0.7594361),
float2 ( 0.7576465, 0.5336417),
float2 (-0.521125, 0.7599803),
float2 (-0.2923127, 0.6545699),
float2 ( 0.6782473, 0.22385),
float2 (-0.3077152, 0.4697627),
float2 ( 0.4484913, 0.2619455),
float2 (-0.5308799, 0.4998215),
float2 (-0.7379634, 0.5304936),
float2 ( 0.02613133, 0.1764302),
float2 (-0.1461073, 0.3047384),
float2 (-0.8451027, 0.3249073),
float2 (-0.4507707, 0.2101997),
float2 (-0.6137282, 0.3283674),
float2 (-0.2385868, 0.08716244),
float2 ( 0.3386548, 0.01528411),
float2 (-0.04230833, -0.1494652),
float2 ( 0.167115, -0.1098648),
float2 (-0.525606, 0.01572019),
float2 (-0.7966855, 0.1318727),
float2 ( 0.5704287, 0.4778273),
float2 (-0.9516637, 0.002725032),
float2 (-0.7068223, -0.1572321),
float2 ( 0.2173306, -0.3494083),
float2 ( 0.06100426, -0.4492816),
float2 ( 0.2333982, 0.2247189),
float2 ( 0.07270987, -0.6396734),
float2 ( 0.4670808, -0.2324669),
float2 ( 0.3729528, -0.512625),
float2 ( 0.5675077, -0.4054544),
float2 (-0.3691984, -0.128435),
float2 ( 0.8752473, 0.2256988),
float2 (-0.2680127, -0.4684393),
float2 (-0.1177551, -0.7205751),
float2 (-0.1270121, -0.3105424),
float2 ( 0.5595394, -0.06309237),
float2 (-0.9299136, -0.1870008),
float2 ( 0.974674, 0.03677348),
float2 ( 0.7726735, -0.06944724),
float2 (-0.4995361, -0.3663749),
float2 ( 0.6474168, -0.2315787),
float2 ( 0.1911449, -0.8858921),
float2 ( 0.3671001, -0.7970535),
float2 (-0.6970353, -0.4449432),
float2 (-0.417599, -0.7189326),
float2 (-0.5584748, -0.6026504),
float2 (-0.02624448, -0.9141423),
float2 ( 0.565636, -0.6585149),
float2 (-0.874976, -0.3997879),
float2 ( 0.9177843, -0.2110524),
float2 ( 0.8156927, -0.3969557),
float2 (-0.2833054, -0.8395444),
float2 ( 0.799141, -0.5886372)
};
real PenumbraSize(real Reciever, real Blocker)
{
return abs((Reciever - Blocker) / Blocker);
}
bool BlockerSearch(inout real averageBlockerDepth, inout real numBlockers, real lightArea, real3 coord, real2 sampleJitter, real2 sampleBias, ShadowContext shadowContext, float slice, uint texIdx, uint sampIdx, int sampleCount)
{
real blockerSum = 0.0;
for (int i = 0; i < sampleCount; ++i)
{
real2 offset = real2(poissonDisk64[i].x * sampleJitter.y + poissonDisk64[i].y * sampleJitter.x,
poissonDisk64[i].x * -sampleJitter.x + poissonDisk64[i].y * sampleJitter.y) * lightArea;
real shadowMapDepth = SampleShadow_T2DA(shadowContext, texIdx, sampIdx, coord.xy + offset, slice).x;
if (COMPARE_DEVICE_DEPTH_CLOSER(shadowMapDepth, coord.z + dot(sampleBias, offset)))
{
blockerSum += shadowMapDepth;
numBlockers += 1.0;
}
}
averageBlockerDepth = blockerSum / numBlockers;
return numBlockers >= 1;
}
bool BlockerSearch(inout real averageBlockerDepth, inout real numBlockers, real lightArea, real3 coord, float slice, real2 sampleJitter, real2 sampleBias, Texture2DArray shadowMap, SamplerState PointSampler, int sampleCount)
{
real blockerSum = 0.0;
for (int i = 0; i < sampleCount; ++i)
{
real2 offset = real2(poissonDisk64[i].x * sampleJitter.y + poissonDisk64[i].y * sampleJitter.x,
poissonDisk64[i].x * -sampleJitter.x + poissonDisk64[i].y * sampleJitter.y) * lightArea;
real shadowMapDepth = SAMPLE_TEXTURE2D_ARRAY_LOD(shadowMap, PointSampler, coord.xy + offset, slice, 0.0).x;
if (COMPARE_DEVICE_DEPTH_CLOSER(shadowMapDepth, coord.z + dot(sampleBias, offset)))
{
blockerSum += shadowMapDepth;
numBlockers += 1.0;
}
}
averageBlockerDepth = blockerSum / numBlockers;
return numBlockers >= 1;
}
real PCSS(real3 coord, real filterRadius, real4 scaleOffset, float slice, real2 sampleBias, real2 sampleJitter, ShadowContext shadowContext, uint texIdx, uint sampIdx, int sampleCount)
{
real UMin = scaleOffset.z;
real UMax = scaleOffset.z + scaleOffset.x;
real VMin = scaleOffset.w;
real VMax = scaleOffset.w + scaleOffset.y;
real sum = 0.0;
for(int i = 0; i < sampleCount; ++i)
{
real2 offset = real2(poissonDisk64[i].x * sampleJitter.y + poissonDisk64[i].y * sampleJitter.x,
poissonDisk64[i].x * -sampleJitter.x + poissonDisk64[i].y * sampleJitter.y) * filterRadius;
real U = coord.x + offset.x;
real V = coord.y + offset.y;
//NOTE: We must clamp the sampling within the bounds of the shadow atlas.
// Overfiltering will leak results from other shadow lights.
//TODO: Investigate moving this to blocker search.
if (U <= UMin || U >= UMax || V <= VMin || V >= VMax)
sum += SampleCompShadow_T2DA(shadowContext, texIdx, sampIdx, real3(coord.xy, coord.z), slice);
else
sum += SampleCompShadow_T2DA(shadowContext, texIdx, sampIdx, real3(U, V, coord.z + dot(sampleBias, offset)), slice);
}
return sum / sampleCount;
}
real PCSS(real3 coord, real filterRadius, real4 scaleOffset, float slice, real2 sampleBias, real2 sampleJitter, Texture2DArray shadowMap, SamplerComparisonState compSampler, int sampleCount)
{
real UMin = scaleOffset.z;
real UMax = scaleOffset.z + scaleOffset.x;
real VMin = scaleOffset.w;
real VMax = scaleOffset.w + scaleOffset.y;
real sum = 0.0;
for(int i = 0; i < sampleCount; ++i)
{
real2 offset = real2(poissonDisk64[i].x * sampleJitter.y + poissonDisk64[i].y * sampleJitter.x,
poissonDisk64[i].x * -sampleJitter.x + poissonDisk64[i].y * sampleJitter.y) * filterRadius;
real U = coord.x + offset.x;
real V = coord.y + offset.y;
//NOTE: We must clamp the sampling within the bounds of the shadow atlas.
// Overfiltering will leak results from other shadow lights.
//TODO: Investigate moving this to blocker search.
if (U <= UMin || U >= UMax || V <= VMin || V >= VMax)
sum += SAMPLE_TEXTURE2D_ARRAY_SHADOW(shadowMap, compSampler, real3(coord.xy, coord.z), slice);
else
sum += SAMPLE_TEXTURE2D_ARRAY_SHADOW(shadowMap, compSampler, real3(U, V, coord.z + dot(sampleBias, offset)), slice);
}
return sum / sampleCount;
}

9
com.unity.render-pipelines.core/CoreRP/ShaderLibrary/Shadow/PCSS.hlsl.meta


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