Skip to content

Commit 2df4532

Browse files
GGijonUnitypastasfuture
authored andcommitted
Parallel LightLoop fixes and improvements (#74)
* 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)
1 parent 608db4a commit 2df4532

File tree

3 files changed

+14
-20
lines changed

3 files changed

+14
-20
lines changed

com.unity.render-pipelines.high-definition/Runtime/Lighting/Light/HDProcessedVisibleLightsBuilder.Jobs.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ protected struct ProcessVisibleLightJob : IJobParallelFor
3333
[ReadOnly]
3434
public NativeArray<LightShadows> visibleLightShadows;
3535
[ReadOnly]
36-
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
36+
public int visibleLightFromVisibleListCutoffIndex;
3737
#endregion
3838

3939
#region Parameters
@@ -251,8 +251,8 @@ private ref HDLightRenderData GetLightData(int dataIndex)
251251

252252
public void Execute(int index)
253253
{
254-
bool isFromVisibleList = visibleLightIsFromVisibleList[index];
255-
VisibleLight visibleLight = isFromVisibleList ? visibleLights[index] : visibleOffscreenVertexLights[index - visibleLights.Length];
254+
bool isFromVisibleList = index < visibleLightFromVisibleListCutoffIndex;
255+
VisibleLight visibleLight = isFromVisibleList ? visibleLights[index] : visibleOffscreenVertexLights[index - visibleLightFromVisibleListCutoffIndex];
256256
int dataIndex = visibleLightEntityDataIndices[index];
257257
LightBakingOutput bakingOutput = visibleLightBakingOutput[index];
258258
LightShadows shadows = visibleLightShadows[index];
@@ -408,7 +408,7 @@ public void StartProcessVisibleLightJob(
408408
visibleLightEntityDataIndices = m_VisibleLightEntityDataIndices,
409409
visibleLightBakingOutput = m_VisibleLightBakingOutput,
410410
visibleLightShadows = m_VisibleLightShadows,
411-
visibleLightIsFromVisibleList = m_VisibleLightIsFromVisibleList,
411+
visibleLightFromVisibleListCutoffIndex = visibleLights.Length,
412412

413413
//Output processed lights.
414414
processedVisibleLightCountsPtr = m_ProcessVisibleLightCounts,

com.unity.render-pipelines.high-definition/Runtime/Lighting/Light/HDProcessedVisibleLightsBuilder.LightLoop.cs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ private void BuildVisibleLightEntities(in CullingResults cullResults)
5858
var defaultEntity = HDLightRenderDatabase.instance.GetDefaultLightEntity();
5959
for (int i = 0; i < totalLightCount; ++i)
6060
{
61-
Light light = GetLightByIndex(cullResults, i, out bool isFromVisibleList);
61+
Light light = GetLightByIndex(cullResults, i);
6262

6363
int dataIndex = HDLightRenderDatabase.instance.FindEntityDataIndex(light);
6464
if (dataIndex == HDLightRenderDatabase.InvalidDataIndex)
@@ -78,7 +78,6 @@ private void BuildVisibleLightEntities(in CullingResults cullResults)
7878
m_VisibleLightBakingOutput[i] = light.bakingOutput;
7979
m_VisibleLightShadowCasterMode[i] = light.lightShadowCasterMode;
8080
m_VisibleLightShadows[i] = light.shadows;
81-
m_VisibleLightIsFromVisibleList[i] = isFromVisibleList;
8281
m_VisibleLightBounceIntensity[i] = light.bounceIntensity;
8382
}
8483
}
@@ -154,7 +153,7 @@ private void FilterVisibleLightsByAOV(AOVRequestData aovRequest)
154153
}
155154

156155
protected abstract int GetTotalLightCount(in CullingResults cullResults);
157-
protected abstract Light GetLightByIndex(in CullingResults cullResults, int index, out bool isFromVisibleList);
156+
protected abstract Light GetLightByIndex(in CullingResults cullResults, int index);
158157
}
159158

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

167-
protected override Light GetLightByIndex(in CullingResults cullResults, int index, out bool isFromVisibleList)
166+
protected override Light GetLightByIndex(in CullingResults cullResults, int index)
168167
{
169-
isFromVisibleList = true;
170168
return cullResults.visibleLights[index].light;
171169
}
172170
}
@@ -178,17 +176,15 @@ protected override int GetTotalLightCount(in CullingResults cullResults)
178176
return cullResults.visibleLights.Length + cullResults.visibleOffscreenVertexLights.Length;
179177
}
180178

181-
protected override Light GetLightByIndex(in CullingResults cullResults, int index, out bool isFromVisibleList)
179+
protected override Light GetLightByIndex(in CullingResults cullResults, int index)
182180
{
183181
if (index < cullResults.visibleLights.Length)
184182
{
185-
isFromVisibleList = true;
186183
return cullResults.visibleLights[index].light;
187184
}
188185
else
189186
{
190187
int offScreenLightIndex = index - cullResults.visibleLights.Length;
191-
isFromVisibleList = false;
192188
return cullResults.visibleOffscreenVertexLights[offScreenLightIndex].light;
193189
}
194190
}

com.unity.render-pipelines.high-definition/Runtime/Lighting/Light/HDProcessedVisibleLightsBuilder.cs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,10 @@ internal enum ShadowMapFlags
3232
}
3333

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

4040
//Indexed by VisibleLights
4141
public NativeArray<LightBakingOutput> visibleLightBakingOutput => m_VisibleLightBakingOutput;
@@ -99,7 +99,6 @@ private enum ProcessLightsCountSlots
9999
private NativeArray<LightBakingOutput> m_VisibleLightBakingOutput;
100100
private NativeArray<LightShadowCasterMode> m_VisibleLightShadowCasterMode;
101101
private NativeArray<LightShadows> m_VisibleLightShadows;
102-
private NativeArray<bool> m_VisibleLightIsFromVisibleList;
103102
private NativeArray<float> m_VisibleLightBounceIntensity;
104103
private NativeArray<LightVolumeType> m_ProcessedLightVolumeType;
105104
private NativeArray<HDProcessedVisibleLight> m_ProcessedEntities;
@@ -118,7 +117,6 @@ private void ResizeArrays(int newCapacity)
118117
m_VisibleLightBakingOutput.ResizeArray(m_Capacity);
119118
m_VisibleLightShadowCasterMode.ResizeArray(m_Capacity);
120119
m_VisibleLightShadows.ResizeArray(m_Capacity);
121-
m_VisibleLightIsFromVisibleList.ResizeArray(m_Capacity);
122120
m_VisibleLightBounceIntensity.ResizeArray(m_Capacity);
123121

124122
m_ProcessedLightVolumeType.ResizeArray(m_Capacity);
@@ -132,16 +130,16 @@ public void Cleanup()
132130
if (m_SortSupportArray.IsCreated)
133131
m_SortSupportArray.Dispose();
134132

133+
if (m_ProcessVisibleLightCounts.IsCreated)
134+
m_ProcessVisibleLightCounts.Dispose();
135+
135136
if (m_Capacity == 0)
136137
return;
137138

138-
m_ProcessVisibleLightCounts.Dispose();
139-
140139
m_VisibleLightEntityDataIndices.Dispose();
141140
m_VisibleLightBakingOutput.Dispose();
142141
m_VisibleLightShadowCasterMode.Dispose();
143142
m_VisibleLightShadows.Dispose();
144-
m_VisibleLightIsFromVisibleList.Dispose();
145143
m_VisibleLightBounceIntensity.Dispose();
146144

147145
m_ProcessedLightVolumeType.Dispose();

0 commit comments

Comments
 (0)