-
Notifications
You must be signed in to change notification settings - Fork 869
Expand file tree
/
Copy pathHDGpuLightsBuilder.cs
More file actions
267 lines (224 loc) · 12.9 KB
/
HDGpuLightsBuilder.cs
File metadata and controls
267 lines (224 loc) · 12.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
using System;
using System.Collections.Generic;
using Unity.Collections;
using Unity.Collections.LowLevel.Unsafe;
using Unity.Mathematics;
namespace UnityEngine.Rendering.HighDefinition
{
internal partial class HDGpuLightsBuilder
{
#region internal HDRP API
public struct LightsPerView
{
public Matrix4x4 worldToView;
public int boundsOffset;
public int boundsCount;
}
public const int ArrayCapacity = 100;
//Light GPU ready arrays
public NativeArray<LightData> lights => m_Lights;
public int lightsCount => m_LightCount;
public NativeArray<DirectionalLightData> directionalLights => m_DirectionalLights;
public int directionalLightCount => m_LightTypeCounters.IsCreated ? m_LightTypeCounters[(int)GPULightTypeCountSlots.Directional] : 0;
public int punctualLightCount => m_LightTypeCounters.IsCreated ? m_LightTypeCounters[(int)GPULightTypeCountSlots.Punctual] : 0;
public int areaLightCount => m_LightTypeCounters.IsCreated ? m_LightTypeCounters[(int)GPULightTypeCountSlots.Area] : 0;
//Auxiliary GPU arrays for coarse culling
public NativeArray<LightsPerView> lightsPerView => m_LightsPerView;
public NativeArray<SFiniteLightBound> lightBounds => m_LightBounds;
public NativeArray<LightVolumeData> lightVolumes => m_LightVolumes;
public int lightsPerViewCount => m_LightsPerViewCount;
public int lightBoundsCount => m_LightBoundsCount;
public int boundsEyeDataOffset => m_BoundsEyeDataOffset;
public int allLightBoundsCount => m_BoundsEyeDataOffset * lightsPerViewCount;
//Counters / singleton lights for shadows etc
public int currentShadowSortedSunLightIndex => m_CurrentShadowSortedSunLightIndex;
public HDProcessedVisibleLightsBuilder.ShadowMapFlags currentSunShadowMapFlags => m_CurrentSunShadowMapFlags;
public DirectionalLightData currentSunLightDirectionalLightData => m_CurrentSunLightDirectionalLightData;
public int contactShadowIndex => m_ContactShadowIndex;
public int screenSpaceShadowIndex => m_ScreenSpaceShadowIndex;
public int screenSpaceShadowChannelSlot => m_ScreenSpaceShadowChannelSlot;
public int debugSelectedLightShadowIndex => m_DebugSelectedLightShadowIndex;
public int debugSelectedLightShadowCount => m_DebugSelectedLightShadowCount;
public HDRenderPipeline.ScreenSpaceShadowData[] currentScreenSpaceShadowData => m_CurrentScreenSpaceShadowData;
//Packs a sort key for a light
public static uint PackLightSortKey(LightCategory lightCategory, GPULightType gpuLightType, LightVolumeType lightVolumeType, int lightIndex, bool offscreen)
{
//We sort directional lights to be in the beginning of the list.
//This ensures that we can access directional lights very easily after we sort them.
uint isDirectionalMSB = gpuLightType == GPULightType.Directional ? 0u : 1u;
uint isOffscreen = offscreen ? 1u : 0u;
uint sortKey = (uint)isDirectionalMSB << 31 | (uint)isOffscreen << 30 | (uint)lightCategory << 26 | (uint)gpuLightType << 21 | (uint)lightVolumeType << 16 | (uint)lightIndex;
return sortKey;
}
//Unpacks a sort key for a light
public static void UnpackLightSortKey(uint sortKey, out LightCategory lightCategory, out GPULightType gpuLightType, out LightVolumeType lightVolumeType, out int lightIndex, out bool offscreen)
{
lightCategory = (LightCategory)((sortKey >> 26) & 0xF);
gpuLightType = (GPULightType)((sortKey >> 21) & 0x1F);
lightVolumeType = (LightVolumeType)((sortKey >> 16) & 0x1F);
lightIndex = (int)(sortKey & 0xFFFF);
offscreen = ((sortKey >> 30) & 0x1) > 0;
}
//Initialization of builder
public void Initialize(HDRenderPipelineAsset asset, HDShadowManager shadowManager, HDRenderPipeline.LightLoopTextureCaches textureCaches)
{
m_Asset = asset;
m_TextureCaches = textureCaches;
m_ShadowManager = shadowManager;
// Screen space shadow
int numMaxShadows = Math.Max(m_Asset.currentPlatformRenderPipelineSettings.hdShadowInitParams.maxScreenSpaceShadowSlots, 1);
m_CurrentScreenSpaceShadowData = new HDRenderPipeline.ScreenSpaceShadowData[numMaxShadows];
//Allocate all the GPU critical buffers, for the case were there are no lights.
//This ensures we can bind an empty buffer on ComputeBuffer SetData() call
AllocateLightData(0, 0);
}
//Adds bounds for a new light type. Reflection probes / decals add their bounds here.
public void AddLightBounds(int viewId, in SFiniteLightBound lightBound, in LightVolumeData volumeData)
{
var viewData = m_LightsPerView[viewId];
m_LightBounds[viewData.boundsOffset + viewData.boundsCount] = lightBound;
m_LightVolumes[viewData.boundsOffset + viewData.boundsCount] = volumeData;
++viewData.boundsCount;
m_LightsPerView[viewId] = viewData;
}
//Cleans up / disposes light info.
public void Cleanup()
{
if (m_Lights.IsCreated)
m_Lights.Dispose();
if (m_DirectionalLights.IsCreated)
m_DirectionalLights.Dispose();
if (m_LightsPerView.IsCreated)
m_LightsPerView.Dispose();
if (m_LightBounds.IsCreated)
m_LightBounds.Dispose();
if (m_LightVolumes.IsCreated)
m_LightVolumes.Dispose();
if (m_LightTypeCounters.IsCreated)
m_LightTypeCounters.Dispose();
if (m_CachedPointUpdateInfos.IsCreated)
{
m_CachedPointUpdateInfos.Dispose();
m_CachedSpotUpdateInfos.Dispose();
m_CachedAreaRectangleUpdateInfos.Dispose();
m_CachedDirectionalUpdateInfos.Dispose();
m_DynamicPointUpdateInfos.Dispose();
m_DynamicSpotUpdateInfos.Dispose();
m_DynamicAreaRectangleUpdateInfos.Dispose();
m_DynamicDirectionalUpdateInfos.Dispose();
}
if (m_ShadowIndicesScratchpadArray.IsCreated)
{
m_ShadowIndicesScratchpadArray.Dispose();
m_ShadowIndicesScratchpadArray = default;
}
#if UNITY_EDITOR
if (m_ShadowRequestCountsScratchpad.IsCreated)
{
m_ShadowRequestCountsScratchpad.Dispose();
m_ShadowRequestCountsScratchpad = default;
}
#endif
}
#endregion
#region private definitions
internal enum GPULightTypeCountSlots
{
Directional,
Punctual,
Area
}
private NativeArray<LightsPerView> m_LightsPerView;
private int m_LighsPerViewCapacity = 0;
private int m_LightsPerViewCount = 0;
private NativeArray<SFiniteLightBound> m_LightBounds;
private NativeArray<LightVolumeData> m_LightVolumes;
private int m_LightBoundsCapacity = 0;
private int m_LightBoundsCount = 0;
private NativeArray<LightData> m_Lights;
private int m_LightCapacity = 0;
private int m_LightCount = 0;
private NativeArray<DirectionalLightData> m_DirectionalLights;
private int m_DirectionalLightCapacity = 0;
private int m_DirectionalLightCount = 0;
private NativeArray<int> m_LightTypeCounters;
private HDRenderPipelineAsset m_Asset;
private HDShadowManager m_ShadowManager;
private HDRenderPipeline.LightLoopTextureCaches m_TextureCaches;
private HashSet<HDAdditionalLightData> m_ScreenSpaceShadowsUnion = new HashSet<HDAdditionalLightData>();
private int m_CurrentShadowSortedSunLightIndex = -1;
private HDProcessedVisibleLightsBuilder.ShadowMapFlags m_CurrentSunShadowMapFlags = HDProcessedVisibleLightsBuilder.ShadowMapFlags.None;
private DirectionalLightData m_CurrentSunLightDirectionalLightData;
private int m_ContactShadowIndex = 0;
private int m_ScreenSpaceShadowIndex = 0;
private int m_ScreenSpaceShadowChannelSlot = 0;
private int m_DebugSelectedLightShadowIndex = 0;
private int m_DebugSelectedLightShadowCount = 0;
private HDRenderPipeline.ScreenSpaceShadowData[] m_CurrentScreenSpaceShadowData;
private int m_BoundsEyeDataOffset = 0;
private NativeArray<int> m_ShadowIndicesScratchpadArray;
#if UNITY_EDITOR
NativeArray<int> m_ShadowRequestCountsScratchpad;
#endif
private NativeList<ShadowRequestIntermediateUpdateData> m_CachedPointUpdateInfos = new NativeList<ShadowRequestIntermediateUpdateData>(Allocator.Persistent);
private NativeList<ShadowRequestIntermediateUpdateData> m_CachedSpotUpdateInfos = new NativeList<ShadowRequestIntermediateUpdateData>(Allocator.Persistent);
private NativeList<ShadowRequestIntermediateUpdateData> m_CachedAreaRectangleUpdateInfos = new NativeList<ShadowRequestIntermediateUpdateData>(Allocator.Persistent);
private NativeList<ShadowRequestIntermediateUpdateData> m_CachedDirectionalUpdateInfos = new NativeList<ShadowRequestIntermediateUpdateData>(Allocator.Persistent);
private NativeList<ShadowRequestIntermediateUpdateData> m_DynamicPointUpdateInfos = new NativeList<ShadowRequestIntermediateUpdateData>(Allocator.Persistent);
private NativeList<ShadowRequestIntermediateUpdateData> m_DynamicSpotUpdateInfos = new NativeList<ShadowRequestIntermediateUpdateData>(Allocator.Persistent);
private NativeList<ShadowRequestIntermediateUpdateData> m_DynamicAreaRectangleUpdateInfos = new NativeList<ShadowRequestIntermediateUpdateData>(Allocator.Persistent);
private NativeList<ShadowRequestIntermediateUpdateData> m_DynamicDirectionalUpdateInfos = new NativeList<ShadowRequestIntermediateUpdateData>(Allocator.Persistent);
private void AllocateLightData(int lightCount, int directionalLightCount)
{
int requestedLightCount = Math.Max(1, lightCount);
if (requestedLightCount > m_LightCapacity)
{
m_LightCapacity = Math.Max(Math.Max(m_LightCapacity * 2, requestedLightCount), ArrayCapacity);
m_Lights.ResizeArray(m_LightCapacity);
}
m_LightCount = lightCount;
int requestedDirectionalCount = Math.Max(1, directionalLightCount);
if (requestedDirectionalCount > m_DirectionalLightCapacity)
{
m_DirectionalLightCapacity = Math.Max(Math.Max(m_DirectionalLightCapacity * 2, requestedDirectionalCount), ArrayCapacity);
m_DirectionalLights.ResizeArray(m_DirectionalLightCapacity);
}
m_DirectionalLightCount = directionalLightCount;
}
private void EnsureScratchpadCapacity(int lightCount)
{
#if UNITY_EDITOR
if (m_ShadowRequestCountsScratchpad.IsCreated && m_ShadowRequestCountsScratchpad.Length < lightCount)
{
m_ShadowRequestCountsScratchpad.Dispose();
m_ShadowRequestCountsScratchpad = default;
}
if (!m_ShadowRequestCountsScratchpad.IsCreated)
{
m_ShadowRequestCountsScratchpad = new NativeArray<int>(lightCount, Allocator.Persistent);
}
#endif
if (m_ShadowIndicesScratchpadArray.IsCreated && m_ShadowIndicesScratchpadArray.Length < lightCount)
{
m_ShadowIndicesScratchpadArray.Dispose();
m_ShadowIndicesScratchpadArray = default;
}
if (!m_ShadowIndicesScratchpadArray.IsCreated)
{
m_ShadowIndicesScratchpadArray = new NativeArray<int>(lightCount, Allocator.Persistent);
}
if (!m_CachedPointUpdateInfos.IsCreated)
{
m_CachedPointUpdateInfos = new NativeList<ShadowRequestIntermediateUpdateData>(Allocator.Persistent);
m_CachedSpotUpdateInfos = new NativeList<ShadowRequestIntermediateUpdateData>(Allocator.Persistent);
m_CachedAreaRectangleUpdateInfos = new NativeList<ShadowRequestIntermediateUpdateData>(Allocator.Persistent);
m_CachedDirectionalUpdateInfos = new NativeList<ShadowRequestIntermediateUpdateData>(Allocator.Persistent);
m_DynamicPointUpdateInfos = new NativeList<ShadowRequestIntermediateUpdateData>(Allocator.Persistent);
m_DynamicSpotUpdateInfos = new NativeList<ShadowRequestIntermediateUpdateData>(Allocator.Persistent);
m_DynamicAreaRectangleUpdateInfos = new NativeList<ShadowRequestIntermediateUpdateData>(Allocator.Persistent);
m_DynamicDirectionalUpdateInfos = new NativeList<ShadowRequestIntermediateUpdateData>(Allocator.Persistent);
}
}
#endregion
}
}