Skip to content

Commit

Permalink
DGI per volume mixed lights (#79)
Browse files Browse the repository at this point in the history
* Added a per-volume property defining if mixed lights were baked for this volume. Add all lights to the light list and skip mixed lights in shaders when required.
* Revert to the previous way of uncapping shadow distance when baking dynamic GI mixed lights.
  • Loading branch information
AndrewSaraevUnity committed May 19, 2022
1 parent ecccd06 commit 12913b7
Show file tree
Hide file tree
Showing 13 changed files with 45 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -315,8 +315,7 @@ private void IncrementCounter(ref NativeArray<int> counterSet, HDGpuLightsBuilde
lightData.hierarchicalVarianceScreenSpaceShadowsIndex = -1;
lightData.isRayTracedContactShadow = 0.0f;

// TODO: only apply for real-time lights, but lightComponent.lightmapBakeType is not available outside in built players, Editor only...
lightData.affectDynamicGI = lightRenderData.affectDynamicGI ? 1 : 0;
lightData.mixedDynamicGI = lightRenderData.mixedDynamicGI ? 1 : 0;

var distanceToCamera = processedEntity.distanceToCamera;
float shadowDistanceFade;
Expand Down Expand Up @@ -700,8 +699,8 @@ private void IncrementCounter(ref NativeArray<int> counterSet, HDGpuLightsBuilde

lightData.flareSize = Mathf.Max(lightRenderData.flareSize * Mathf.Deg2Rad, 5.960464478e-8f);
lightData.flareFalloff = lightRenderData.flareFalloff;
// TODO: only apply for real-time lights, but lightComponent.lightmapBakeType is not available outside in built players, Editor only...
lightData.affectDynamicGI = lightRenderData.affectDynamicGI ? 1 : 0;
lightData.mixedDynamicGI = lightRenderData.mixedDynamicGI ? 1 : 0;
lightData.flareTint = (Vector3)(Vector4)lightRenderData.flareTint;
lightData.surfaceTint = (Vector3)(Vector4)lightRenderData.surfaceTint;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,6 @@ protected struct ProcessVisibleLightJob : IJobParallelFor
public bool enableAreaLights;
[ReadOnly]
public bool enableDynamicGI;
[ReadOnly]
public bool dynamicGIUseRealtimeLights;
[ReadOnly]
public bool dynamicGIUseMixedLights;
#if UNITY_EDITOR
[ReadOnly]
public bool dynamicGIPreparingMixedLights;
Expand Down Expand Up @@ -262,8 +258,12 @@ public void Execute(int index)

bool affectsDynamicGI =
enableDynamicGI &&
lightRenderData.affectDynamicGI &&
(lightRenderData.mixedDynamicGI ? dynamicGIUseMixedLights : dynamicGIUseRealtimeLights);
lightRenderData.affectDynamicGI;

#if UNITY_EDITOR
if (dynamicGIPreparingMixedLights)
affectsDynamicGI &= lightRenderData.mixedDynamicGI;
#endif

if (enableRayTracing && !lightRenderData.includeForRayTracing)
return;
Expand Down Expand Up @@ -394,8 +394,6 @@ public void Execute(int index)
pixelCount = hdCamera.actualWidth * hdCamera.actualHeight,
enableAreaLights = ShaderConfig.s_AreaLights != 0,
enableDynamicGI = processDynamicGI,
dynamicGIUseRealtimeLights = dynamicGIMixedLightMode != ProbeVolumeDynamicGIMixedLightMode.MixedOnly,
dynamicGIUseMixedLights = dynamicGIMixedLightMode == ProbeVolumeDynamicGIMixedLightMode.ForceRealtime,
#if UNITY_EDITOR
dynamicGIPreparingMixedLights = ProbeVolume.preparingMixedLights,
#endif
Expand Down Expand Up @@ -430,14 +428,6 @@ public void Execute(int index)
shadowLightsDataIndices = m_ShadowLightsDataIndices
};

#if UNITY_EDITOR
if (ProbeVolume.preparingMixedLights)
{
processVisibleLightJob.dynamicGIUseRealtimeLights = false;
processVisibleLightJob.dynamicGIUseMixedLights = true;
}
#endif

m_ProcessVisibleLightJobHandle = processVisibleLightJob.Schedule(m_Size, 32);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,8 @@ struct DirectionalLightData
public float distanceFromCamera; // -1 -> no sky interaction
public float angularDiameter; // Units: radians
public float flareFalloff;
public float affectDynamicGI;
public int affectDynamicGI;
public int mixedDynamicGI;

public Vector3 flareTint;
public float flareSize; // Units: radians
Expand Down Expand Up @@ -176,7 +177,7 @@ struct LightData
public int contactShadowMask; // negative if unused (TODO: 16 bit)
public float diffuseDimmer;
public float specularDimmer;
public int affectDynamicGI;
public int mixedDynamicGI;

public float padding;
public float isRayTracedContactShadow;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,8 @@ struct DirectionalLightData
float distanceFromCamera;
float angularDiameter;
float flareFalloff;
float affectDynamicGI;
int affectDynamicGI;
int mixedDynamicGI;
float3 flareTint;
float flareSize;
float3 surfaceTint;
Expand Down Expand Up @@ -121,7 +122,7 @@ struct LightData
int contactShadowMask;
float diffuseDimmer;
float specularDimmer;
int affectDynamicGI;
int mixedDynamicGI;
float padding;
float isRayTracedContactShadow;
float boxLightSafeExtent;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1616,6 +1616,9 @@ internal int GetCurrentShadowCount()
void LightLoopUpdateCullingParameters(ref ScriptableCullingParameters cullingParams, HDCamera hdCamera)
{
#if UNITY_EDITOR
// TODO: Support dynamic GI mixed mode for directional lights.
// When baking mixed lights for dynamic GI we leave shadow distance not limited by settings.
// This doesn't work well for directional light shadows, so mixed mode is not really supported for them.
if (!ProbeVolume.preparingMixedLights)
#endif
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ float _IndirectScale;
float _MaxAlbedo;
float _BakedEmissionMultiplier;
float _MixedLightingMultiplier;
int _MixedLightsAsRealtimeEnabled;
float _RayBias;
float _InfiniteBounce;
float _Sharpness;
Expand Down Expand Up @@ -71,6 +72,10 @@ float3 BiasLightPositionNearHit(float3 lightPosition, float3 hitPosition)
return normalize(vec) * pushDistance;
}

bool CheckMixedLight(bool mixedDynamicGI)
{
return _MixedLightsAsRealtimeEnabled || !mixedDynamicGI;
}

float3 EvaluateDirectLightingAtHit(SurfaceHitData hit)
{
Expand All @@ -81,8 +86,11 @@ float3 EvaluateDirectLightingAtHit(SurfaceHitData hit)
for (uint puncLightIdx = 0; puncLightIdx < _DynamicGIPunctualLightCount; ++puncLightIdx)
{
light = _DynamicGILightDatas[puncLightIdx];
light.positionRWS += BiasLightPositionNearHit(light.positionRWS, GetCameraRelativePositionWS(hit.position));
directLighting += GetLightingForAxisPunctual(light, _IndirectScale, hit);
if (CheckMixedLight(light.mixedDynamicGI))
{
light.positionRWS += BiasLightPositionNearHit(light.positionRWS, GetCameraRelativePositionWS(hit.position));
directLighting += GetLightingForAxisPunctual(light, _IndirectScale, hit);
}
}

#if SUPPORTS_AREA_LIGHTS
Expand All @@ -91,7 +99,8 @@ float3 EvaluateDirectLightingAtHit(SurfaceHitData hit)
for (uint areaLightIdx = (uint)_DynamicGIPunctualLightCount; areaLightIdx < (uint)lastAreaLightIndex; ++areaLightIdx)
{
light = _DynamicGILightDatas[areaLightIdx];
directLighting += GetLightingForAxisArea(light, _IndirectScale, hit);
if (CheckMixedLight(light.mixedDynamicGI))
directLighting += GetLightingForAxisArea(light, _IndirectScale, hit);
}
#endif

Expand All @@ -100,10 +109,8 @@ float3 EvaluateDirectLightingAtHit(SurfaceHitData hit)
{
DirectionalLightData dirData = _DirectionalLightDatas[dirLightIdx];
// Only support the shadow casting one otherwise result is completely off.
if (dirData.affectDynamicGI > 0.0f && _DirectionalShadowIndex == (int)dirLightIdx)
{
if (dirData.affectDynamicGI && CheckMixedLight(dirData.mixedDynamicGI) && _DirectionalShadowIndex == (int)dirLightIdx)
directLighting += GetLightingForAxisDirectional(dirData, _IndirectScale * dirData.bounceIntensity, hit);
}
}

return directLighting;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -615,8 +615,9 @@ void DispatchClearPreviousRadianceCache(CommandBuffer cmd, ProbeVolumeHandle pro
// We bake raw unscaled lighting values so we could adjust mixed lights contribution
// with Indirect Scale at runtime in the same way as runtime lights.
cmd.SetComputeFloatParam(shader, "_IndirectScale", 1f);
cmd.SetComputeFloatParam(shader, "_MixedLightingMultiplier", 0f);
cmd.SetComputeFloatParam(shader, "_BakedEmissionMultiplier", 0f);
cmd.SetComputeFloatParam(shader, "_MixedLightingMultiplier", 0f);
cmd.SetComputeIntParam(shader, "_MixedLightsAsRealtimeEnabled", 1);
infBounce = 0f;

cmd.SetComputeFloatParam(shader, "_RangeBehindCamera", float.MaxValue);
Expand All @@ -626,8 +627,12 @@ void DispatchClearPreviousRadianceCache(CommandBuffer cmd, ProbeVolumeHandle pro
#endif
{
cmd.SetComputeFloatParam(shader, "_IndirectScale", mixedLightMode != ProbeVolumeDynamicGIMixedLightMode.MixedOnly ? giSettings.indirectMultiplier.value : 0f);
cmd.SetComputeFloatParam(shader, "_MixedLightingMultiplier", mixedLightMode != ProbeVolumeDynamicGIMixedLightMode.ForceRealtime ? giSettings.indirectMultiplier.value : 0f);
cmd.SetComputeFloatParam(shader, "_BakedEmissionMultiplier", giSettings.bakedEmissionMultiplier.value);

var forceRealtime = mixedLightMode == ProbeVolumeDynamicGIMixedLightMode.ForceRealtime;
cmd.SetComputeFloatParam(shader, "_MixedLightingMultiplier", !forceRealtime ? giSettings.indirectMultiplier.value : 0f);
cmd.SetComputeIntParam(shader, "_MixedLightsAsRealtimeEnabled", forceRealtime || !probeVolume.DynamicGIMixedLightsBaked() ? 1 : 0);

infBounce = infiniteBounces ? giSettings.infiniteBounce.value : 0f;

cmd.SetComputeFloatParam(shader, "_RangeBehindCamera", giSettings.rangeBehindCamera.value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,8 @@ internal void ConstructNeighborData(Vector3[] probePositionsWS, Quaternion rotat

GeneratePackedNeighborData(neighborBakeDatas, ref probeVolumeAsset, in parameters, hits);
ClearContent();

probeVolumeAsset.dynamicGIMixedLightsBaked = false;
}

internal void DebugDrawNeighborhood(ProbeVolumeHandle probeVolume, Camera camera)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ interface IProbeVolumeList
int GetNeighborAxisLength(int i);
void SetHitNeighborAxis(int i, ComputeBuffer buffer);
void SetNeighborAxis(int i, ComputeBuffer buffer);
bool DynamicGIMixedLightsBaked(int i);

#if UNITY_EDITOR
public bool IsHiddenInScene(int i);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1306,6 +1306,7 @@ internal void CopyDirectLightingToMixed()
for (int i = 0; i < hits.Length; i++)
hits[i].mixedLighting = ProbeVolumeDynamicGI.PackEmission(hitRandiance[i]);

probeVolumeAsset.dynamicGIMixedLightsBaked = true;
IncrementDataVersion();
UnityEditor.EditorUtility.SetDirty(probeVolumeAsset);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ internal enum AssetVersion

[SerializeField] internal VolumeGlobalUniqueID globalUniqueID;

[SerializeField] internal bool dynamicGIMixedLightsBaked;

// We are not saving it for now as it's only used when rebaking a loaded volume.
internal int dataVersion;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ public ProbeVolumeHandle(IProbeVolumeList list, int index)
public int NeighborAxisLength => m_List.GetNeighborAxisLength(m_Index);
public void SetHitNeighborAxis(ComputeBuffer buffer) => m_List.SetHitNeighborAxis(m_Index, buffer);
public void SetNeighborAxis(ComputeBuffer buffer) => m_List.SetNeighborAxis(m_Index, buffer);
public bool DynamicGIMixedLightsBaked() => m_List.DynamicGIMixedLightsBaked(m_Index);

public bool AbleToSimulateDynamicGI()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -245,5 +245,6 @@ bool IProbeVolumeList.HasNeighbors(int i)
int IProbeVolumeList.GetNeighborAxisLength(int i) => m_Volumes[i].GetPayload().neighborAxis.Length;
void IProbeVolumeList.SetHitNeighborAxis(int i, ComputeBuffer buffer) => buffer.SetData(m_Volumes[i].GetPayload().hitNeighborAxis);
void IProbeVolumeList.SetNeighborAxis(int i, ComputeBuffer buffer) => buffer.SetData(m_Volumes[i].GetPayload().neighborAxis);
bool IProbeVolumeList.DynamicGIMixedLightsBaked(int i) => m_Volumes[i].probeVolumeAsset.dynamicGIMixedLightsBaked;
}
}

0 comments on commit 12913b7

Please sign in to comment.