浏览代码

Raw PCSS import from DREAM-2018.2 project

/main
Antoine Lelievre 6 年前
当前提交
447b05b3
共有 11 个文件被更改,包括 217 次插入16 次删除
  1. 25
      com.unity.render-pipelines.core/CoreRP/ShaderLibrary/Shadow/ShadowSampling.hlsl
  2. 6
      com.unity.render-pipelines.core/CoreRP/Shadow/AdditionalShadowData.cs
  3. 38
      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. 2
      com.unity.render-pipelines.high-definition/HDRP/Editor/Lighting/HDLightEditor.Styles.cs
  7. 8
      com.unity.render-pipelines.high-definition/HDRP/Editor/Lighting/HDLightEditor.cs
  8. 9
      com.unity.render-pipelines.high-definition/HDRP/Lighting/LightLoop/LightLoop.cs
  9. 9
      com.unity.render-pipelines.high-definition/HDRP/Lighting/LightLoop/Shadow.hlsl
  10. 124
      com.unity.render-pipelines.core/CoreRP/ShaderLibrary/Shadow/PCSS.hlsl
  11. 9
      com.unity.render-pipelines.core/CoreRP/ShaderLibrary/Shadow/PCSS.hlsl.meta

25
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 coord, real4 scaleOffset, float slice, Texture2DArray tex, SamplerComparisonState compSamp, SamplerState samp)
{
real2 params = asfloat(shadowContext.payloads[payloadOffset].xy);
real LightArea = (params.x / 255.0); //TODO: Floats through payload.
real MinimumFilterSize = (params.y / 255.0);
payloadOffset++;
real2 SampleBias = real2(sin(GenerateHashedRandomFloat(asuint(coord.x))),
cos(GenerateHashedRandomFloat(asuint(coord.y))));
//1) Blocker Search
real AverageBlockerDepth = 0.0;
real NumBlockers = 0.0;
if (!BlockerSearch(AverageBlockerDepth, NumBlockers, LightArea + MinimumFilterSize, coord, slice, SampleBias, tex, samp))
return 1.0;
//2) Penumbra Estimation
real FilterSize = LightArea * PenumbraSize(coord.z, AverageBlockerDepth);
FilterSize = max(FilterSize, MinimumFilterSize);
//3) Filter
return PCSS(coord, FilterSize, scaleOffset, slice, SampleBias, tex, compSamp);
}
//-----------------------------------------------------------------------------------------------------
// 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, 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, shadowData.slice, tex, compSamp, s_point_clamp_sampler);
default: return 1.0;
}

6
com.unity.render-pipelines.core/CoreRP/Shadow/AdditionalShadowData.cs


return DefaultShadowResolution;
}
//TODO: put these elsewhere ?
[Range(0.0f, 1.0f)]
public float shadowSoftness = 0.5f;
[Range(0.0f, 0.001f)]
public float shadowMinimumSoftness = 0.0f;
[Range(0.0f, 1.0f)]
public float shadowDimmer = 1.0f;
public float shadowFadeDistance = 10000.0f;

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


m_SupportedAlgorithms.AddUniqueUnchecked((int)ShadowUtils.Pack(ShadowAlgorithm.PCF, ShadowVariant.V2, precision));
m_SupportedAlgorithms.AddUniqueUnchecked((int)ShadowUtils.Pack(ShadowAlgorithm.PCF, ShadowVariant.V3, precision));
m_SupportedAlgorithms.AddUniqueUnchecked((int)ShadowUtils.Pack(ShadowAlgorithm.PCF, ShadowVariant.V4, precision));
m_SupportedAlgorithms.AddUniqueUnchecked((int)ShadowUtils.Pack(ShadowAlgorithm.PCSS, ShadowVariant.V0, precision)); //TODO: Consider the highest precision for blocker search.
ShadowRegistry.VariantDelegate del = (Light l, ShadowAlgorithm dataAlgorithm, ShadowVariant dataVariant, ShadowPrecision dataPrecision, ref int[] dataBlock) =>
{

new ShadowVariant[] { ShadowVariant.V0, ShadowVariant.V1, ShadowVariant.V2, ShadowVariant.V3, ShadowVariant.V4 },
new string[] {"1 tap", "9 tap adaptive", "tent 3x3 (4 taps)", "tent 5x5 (9 taps)", "tent 7x7 (16 taps)" },
new ShadowRegistry.VariantDelegate[] { del, del, del, del, del });
registry.Register(type, precision, ShadowAlgorithm.PCSS, "Percentage Closer Soft Shadows (PCSS)",
new ShadowVariant[] { ShadowVariant.V0 },
new string[] { "poisson 64" },
new ShadowRegistry.VariantDelegate[] { del });
}
// returns true if the original data passed integrity checks, false if the data had to be modified

{
uint payloadSize = sr.shadowType == GPUShadowType.Directional ? (1 + k_MaxCascadesInShader + ((uint)m_TmpBorders.Length / 4)) : 0;
payloadSize += ShadowUtils.ExtractAlgorithm(sr.shadowAlgorithm) == ShadowAlgorithm.PCF ? 1u : 0;
payloadSize += ShadowUtils.ExtractAlgorithm(sr.shadowAlgorithm) == ShadowAlgorithm.PCSS ? 1u : 0;
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 // PCSS
{
sp.Set((0.01f * asd.shadowSoftness) * 255, asd.shadowMinimumSoftness * 255, 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

2
com.unity.render-pipelines.high-definition/HDRP/Editor/Lighting/HDLightEditor.Styles.cs


public readonly GUIContent shadowFadeDistance = new GUIContent("Fade Distance", "The shadow will fade at distance ShadowFadeDistance before being culled to minimize popping.");
public readonly GUIContent shadowDimmer = new GUIContent("Dimmer", "Aim to be use with script, timeline or animation. It allows dimming one or multiple shadows. This can also be used as an optimization to fit in shadow budget manually and minimize popping.");
public readonly GUIContent contactShadows = new GUIContent("Enable Contact Shadows", "Enable support for contact shadows on this light. Better for lights with a lot of visible shadows.");
public readonly GUIContent shadowSoftness = new GUIContent("Penumbra Softness", "TODO !");
public readonly GUIContent shadowMinimumSoftness = new GUIContent("Minimum Penumbra Softness", "TODO !");
// Bias control
public readonly GUIContent viewBiasMin = new GUIContent("View Bias");

8
com.unity.render-pipelines.high-definition/HDRP/Editor/Lighting/HDLightEditor.cs


public SerializedProperty fadeDistance;
public SerializedProperty resolution;
public SerializedProperty contactShadows;
public SerializedProperty softness;
public SerializedProperty minimumSoftness;
// Bias control
public SerializedProperty viewBiasMin;

fadeDistance = o.Find(x => x.shadowFadeDistance),
resolution = o.Find(x => x.shadowResolution),
contactShadows = o.Find(x => x.contactShadows),
softness = o.Find(x => x.shadowSoftness),
minimumSoftness = o.Find(x => x.shadowMinimumSoftness),
viewBiasMin = o.Find(x => x.viewBiasMin),
viewBiasMax = o.Find(x => x.viewBiasMax),

EditorGUILayout.Space();
EditorGUILayout.LabelField("Additional Settings", EditorStyles.boldLabel);
EditorGUI.indentLevel++;
//TOOD: check the current shadowing algorithm and hide these fields
EditorGUILayout.Slider(m_AdditionalShadowData.softness, 0.0f, 1.0f, s_Styles.shadowSoftness);
EditorGUILayout.Slider(m_AdditionalShadowData.minimumSoftness, 0.0f, 0.001f, s_Styles.shadowMinimumSoftness);
EditorGUILayout.PropertyField(m_AdditionalShadowData.contactShadows, s_Styles.contactShadows);

9
com.unity.render-pipelines.high-definition/HDRP/Lighting/LightLoop/LightLoop.cs


m_ShadowMgr = new ShadowManager(shadowSettings, ref scInit, ref budgets, m_Shadowmaps);
// set global overrides - these need to match the override specified in LightLoop/Shadow.hlsl
bool useGlobalOverrides = true;
/*
m_ShadowMgr.SetGlobalShadowOverride(GPUShadowType.Directional , ShadowAlgorithm.PCF, ShadowVariant.V3, ShadowPrecision.High, useGlobalOverrides);
m_ShadowMgr.SetGlobalShadowOverride(GPUShadowType.Directional , ShadowAlgorithm.PCF, ShadowVariant.V3, ShadowPrecision.High, useGlobalOverrides);*/
//TODO: do not include this in the final PR !
m_ShadowMgr.SetGlobalShadowOverride(GPUShadowType.Point , ShadowAlgorithm.PCSS, ShadowVariant.V0, ShadowPrecision.High, useGlobalOverrides);
m_ShadowMgr.SetGlobalShadowOverride(GPUShadowType.Spot , ShadowAlgorithm.PCSS, ShadowVariant.V0, ShadowPrecision.High, useGlobalOverrides);
m_ShadowMgr.SetGlobalShadowOverride(GPUShadowType.Directional , ShadowAlgorithm.PCSS, ShadowVariant.V0, ShadowPrecision.High, useGlobalOverrides);
m_ShadowMgr.SetShadowLightTypeDelegate(HDShadowLightType);

9
com.unity.render-pipelines.high-definition/HDRP/Lighting/LightLoop/Shadow.hlsl


// directional
#define SHADOW_DISPATCH_DIR_TEX 0
#define SHADOW_DISPATCH_DIR_SMP 0
#define SHADOW_DISPATCH_DIR_ALG GPUSHADOWALGORITHM_PCF_TENT_5X5 // all cascades
//TODO: do not push this in the final PR !
#define SHADOW_DISPATCH_DIR_ALG GPUSHADOWALGORITHM_PCSS // all cascades
#define SHADOW_DISPATCH_DIR_ALG_0 GPUSHADOWALGORITHM_PCF_TENT_7X7 // 1st cascade
#define SHADOW_DISPATCH_DIR_ALG_1 GPUSHADOWALGORITHM_PCF_TENT_5X5 // 2nd cascade
#define SHADOW_DISPATCH_DIR_ALG_2 GPUSHADOWALGORITHM_PCF_TENT_3X3 // 3rd cascade

#define SHADOW_DISPATCH_POINT_SMP 0
#define SHADOW_DISPATCH_POINT_ALG GPUSHADOWALGORITHM_PCF_TENT_3X3
#define SHADOW_DISPATCH_POINT_ALG GPUSHADOWALGORITHM_PCSS
#define SHADOW_DISPATCH_SPOT_ALG GPUSHADOWALGORITHM_PCF_TENT_3X3
#define SHADOW_DISPATCH_SPOT_ALG GPUSHADOWALGORITHM_PCSS
#define SHADOW_DISPATCH_PUNC_ALG GPUSHADOWALGORITHM_PCF_TENT_3X3
#define SHADOW_DISPATCH_PUNC_ALG GPUSHADOWALGORITHM_PCSS
// example of overriding directional lights
#ifdef SHADOW_DISPATCH_USE_CUSTOM_DIRECTIONAL

124
com.unity.render-pipelines.core/CoreRP/ShaderLibrary/Shadow/PCSS.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, float Slice, real2 SampleBias, Texture2DArray ShadowMap, SamplerState PointSampler)
{
real BlockerSum = 0.0;
for (int i = 0; i < 64; ++i)
{
real2 Offset = real2(PoissonDisk64[i].x * SampleBias.y + PoissonDisk64[i].y * SampleBias.x,
PoissonDisk64[i].x * -SampleBias.x + PoissonDisk64[i].y * SampleBias.y) * LightArea;
real ShadowMapDepth = SAMPLE_TEXTURE2D_ARRAY_LOD( ShadowMap, PointSampler, Coord.xy + Offset, Slice, 0.0 ).x;
if(ShadowMapDepth > Coord.z)
{
BlockerSum += ShadowMapDepth;
NumBlockers += 1.0;
}
}
AverageBlockerDepth = BlockerSum / NumBlockers;
if (NumBlockers < 1)
return false;
return true;
}
real PCSS(real3 Coord, real FilterRadius, real4 ScaleOffset, float Slice, real2 SampleBias, Texture2DArray ShadowMap, SamplerComparisonState CompSampler)
{
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 < 64; ++i)
{
real2 Offset = real2(PoissonDisk64[i].x * SampleBias.y + PoissonDisk64[i].y * SampleBias.x,
PoissonDisk64[i].x * -SampleBias.x + PoissonDisk64[i].y * SampleBias.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), Slice);
}
return Sum / 64.0;
}

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


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