Skip to content

Commit

Permalink
Parallel LightLoop fixes and improvements (#74)
Browse files Browse the repository at this point in the history
* Native collection leak fix

* IsFromVisibleList improvement

Add null guards to HDProcessedVisibleLightsBuilder light count accessor functions. m_ProcessVisibleLightCounts is not allocated until BuildVisibleLightEntities(), but the counts can be queried outside of the lightloop, before build - in particular inside of HDAdditionalLightData::WillRenderShadowMap() (#77)
  • Loading branch information
GGijonUnity authored and pastasfuture committed Oct 19, 2022
1 parent 608db4a commit 2df4532
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ protected struct ProcessVisibleLightJob : IJobParallelFor
[ReadOnly]
public NativeArray<LightShadows> visibleLightShadows;
[ReadOnly]
public NativeArray<bool> visibleLightIsFromVisibleList; // GG: Instead of this array we could store the cutoff index such that any idx < cutoff is from the visible list
public int visibleLightFromVisibleListCutoffIndex;
#endregion

#region Parameters
Expand Down Expand Up @@ -251,8 +251,8 @@ private bool IncrementLightCounterAndTestLimit(LightCategory lightCategory, GPUL

public void Execute(int index)
{
bool isFromVisibleList = visibleLightIsFromVisibleList[index];
VisibleLight visibleLight = isFromVisibleList ? visibleLights[index] : visibleOffscreenVertexLights[index - visibleLights.Length];
bool isFromVisibleList = index < visibleLightFromVisibleListCutoffIndex;
VisibleLight visibleLight = isFromVisibleList ? visibleLights[index] : visibleOffscreenVertexLights[index - visibleLightFromVisibleListCutoffIndex];
int dataIndex = visibleLightEntityDataIndices[index];
LightBakingOutput bakingOutput = visibleLightBakingOutput[index];
LightShadows shadows = visibleLightShadows[index];
Expand Down Expand Up @@ -408,7 +408,7 @@ public void Execute(int index)
visibleLightEntityDataIndices = m_VisibleLightEntityDataIndices,
visibleLightBakingOutput = m_VisibleLightBakingOutput,
visibleLightShadows = m_VisibleLightShadows,
visibleLightIsFromVisibleList = m_VisibleLightIsFromVisibleList,
visibleLightFromVisibleListCutoffIndex = visibleLights.Length,

//Output processed lights.
processedVisibleLightCountsPtr = m_ProcessVisibleLightCounts,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ private void BuildVisibleLightEntities(in CullingResults cullResults)
var defaultEntity = HDLightRenderDatabase.instance.GetDefaultLightEntity();
for (int i = 0; i < totalLightCount; ++i)
{
Light light = GetLightByIndex(cullResults, i, out bool isFromVisibleList);
Light light = GetLightByIndex(cullResults, i);

int dataIndex = HDLightRenderDatabase.instance.FindEntityDataIndex(light);
if (dataIndex == HDLightRenderDatabase.InvalidDataIndex)
Expand All @@ -78,7 +78,6 @@ private void BuildVisibleLightEntities(in CullingResults cullResults)
m_VisibleLightBakingOutput[i] = light.bakingOutput;
m_VisibleLightShadowCasterMode[i] = light.lightShadowCasterMode;
m_VisibleLightShadows[i] = light.shadows;
m_VisibleLightIsFromVisibleList[i] = isFromVisibleList;
m_VisibleLightBounceIntensity[i] = light.bounceIntensity;
}
}
Expand Down Expand Up @@ -154,7 +153,7 @@ private void FilterVisibleLightsByAOV(AOVRequestData aovRequest)
}

protected abstract int GetTotalLightCount(in CullingResults cullResults);
protected abstract Light GetLightByIndex(in CullingResults cullResults, int index, out bool isFromVisibleList);
protected abstract Light GetLightByIndex(in CullingResults cullResults, int index);
}

internal partial class HDProcessedVisibleLightsRegularBuilder : HDProcessedVisibleLightsBuilder
Expand All @@ -164,9 +163,8 @@ protected override int GetTotalLightCount(in CullingResults cullResults)
return cullResults.visibleLights.Length;
}

protected override Light GetLightByIndex(in CullingResults cullResults, int index, out bool isFromVisibleList)
protected override Light GetLightByIndex(in CullingResults cullResults, int index)
{
isFromVisibleList = true;
return cullResults.visibleLights[index].light;
}
}
Expand All @@ -178,17 +176,15 @@ protected override int GetTotalLightCount(in CullingResults cullResults)
return cullResults.visibleLights.Length + cullResults.visibleOffscreenVertexLights.Length;
}

protected override Light GetLightByIndex(in CullingResults cullResults, int index, out bool isFromVisibleList)
protected override Light GetLightByIndex(in CullingResults cullResults, int index)
{
if (index < cullResults.visibleLights.Length)
{
isFromVisibleList = true;
return cullResults.visibleLights[index].light;
}
else
{
int offScreenLightIndex = index - cullResults.visibleLights.Length;
isFromVisibleList = false;
return cullResults.visibleOffscreenVertexLights[offScreenLightIndex].light;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@ internal enum ShadowMapFlags
}

//Member lights counts
public int sortedLightCounts => m_ProcessVisibleLightCounts[(int)ProcessLightsCountSlots.ProcessedLights];
public int sortedDirectionalLightCounts => m_ProcessVisibleLightCounts[(int)ProcessLightsCountSlots.DirectionalLights];
public int sortedLightCounts => m_ProcessVisibleLightCounts.IsCreated ? m_ProcessVisibleLightCounts[(int)ProcessLightsCountSlots.ProcessedLights] : 0;
public int sortedDirectionalLightCounts => m_ProcessVisibleLightCounts.IsCreated ? m_ProcessVisibleLightCounts[(int)ProcessLightsCountSlots.DirectionalLights] : 0;
public int sortedNonDirectionalLightCounts => sortedLightCounts - sortedDirectionalLightCounts;
public int bakedShadowsCount => m_ProcessVisibleLightCounts[(int)ProcessLightsCountSlots.BakedShadows];
public int bakedShadowsCount => m_ProcessVisibleLightCounts.IsCreated ? m_ProcessVisibleLightCounts[(int)ProcessLightsCountSlots.BakedShadows] : 0;

//Indexed by VisibleLights
public NativeArray<LightBakingOutput> visibleLightBakingOutput => m_VisibleLightBakingOutput;
Expand Down Expand Up @@ -99,7 +99,6 @@ private enum ProcessLightsCountSlots
private NativeArray<LightBakingOutput> m_VisibleLightBakingOutput;
private NativeArray<LightShadowCasterMode> m_VisibleLightShadowCasterMode;
private NativeArray<LightShadows> m_VisibleLightShadows;
private NativeArray<bool> m_VisibleLightIsFromVisibleList;
private NativeArray<float> m_VisibleLightBounceIntensity;
private NativeArray<LightVolumeType> m_ProcessedLightVolumeType;
private NativeArray<HDProcessedVisibleLight> m_ProcessedEntities;
Expand All @@ -118,7 +117,6 @@ private void ResizeArrays(int newCapacity)
m_VisibleLightBakingOutput.ResizeArray(m_Capacity);
m_VisibleLightShadowCasterMode.ResizeArray(m_Capacity);
m_VisibleLightShadows.ResizeArray(m_Capacity);
m_VisibleLightIsFromVisibleList.ResizeArray(m_Capacity);
m_VisibleLightBounceIntensity.ResizeArray(m_Capacity);

m_ProcessedLightVolumeType.ResizeArray(m_Capacity);
Expand All @@ -132,16 +130,16 @@ public void Cleanup()
if (m_SortSupportArray.IsCreated)
m_SortSupportArray.Dispose();

if (m_ProcessVisibleLightCounts.IsCreated)
m_ProcessVisibleLightCounts.Dispose();

if (m_Capacity == 0)
return;

m_ProcessVisibleLightCounts.Dispose();

m_VisibleLightEntityDataIndices.Dispose();
m_VisibleLightBakingOutput.Dispose();
m_VisibleLightShadowCasterMode.Dispose();
m_VisibleLightShadows.Dispose();
m_VisibleLightIsFromVisibleList.Dispose();
m_VisibleLightBounceIntensity.Dispose();

m_ProcessedLightVolumeType.Dispose();
Expand Down

0 comments on commit 2df4532

Please sign in to comment.