Skip to content

Commit 032f504

Browse files
committed
Merge branch 'LineLights'
2 parents 52657f2 + aa38c4e commit 032f504

File tree

13 files changed

+1159
-149
lines changed

13 files changed

+1159
-149
lines changed

Assets/ScriptableRenderLoop/AdditionalLightData.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
namespace UnityEngine.Experimental.ScriptableRenderLoop
22
{
3-
//@TODO: We should continously move these values
3+
public enum LightArchetype {Punctual, Rectangle, Line};
4+
5+
//@TODO: We should continuously move these values
46
// into the engine when we can see them being generally useful
57
[RequireComponent(typeof(Light))]
68
public class AdditionalLightData : MonoBehaviour
79
{
10+
811
public const int DefaultShadowResolution = 512;
912

1013
public int shadowResolution = DefaultShadowResolution;
@@ -31,9 +34,8 @@ public float GetInnerSpotPercent01()
3134
public bool affectDiffuse = true;
3235
public bool affectSpecular = true;
3336

34-
// Area Light Hack
35-
public bool treatAsAreaLight = false;
36-
public bool isDoubleSided = false;
37+
public LightArchetype archetype = LightArchetype.Punctual;
38+
public bool isDoubleSided = false;
3739

3840
[RangeAttribute(0.0f, 20.0f)]
3941
public float areaLightLength = 0.0f;

Assets/ScriptableRenderLoop/HDRenderLoop/HDRenderLoop.asset.meta

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Assets/ScriptableRenderLoop/HDRenderLoop/HDRenderLoop.cs

Lines changed: 70 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ public void BindBuffers(Material mat)
137137
public const int k_MaxAreaLightsOnSCreen = 128;
138138
public const int k_MaxEnvLightsOnSCreen = 64;
139139
public const int k_MaxShadowOnScreen = 16;
140+
public const int k_MaxCascadeCount = 4; //Should be not less than m_Settings.directionalLightCascadeCount;
140141

141142
[SerializeField]
142143
TextureSettings m_TextureSettings = TextureSettings.Default;
@@ -157,12 +158,26 @@ public void BindBuffers(Material mat)
157158
public class LightList
158159
{
159160
public List<DirectionalLightData> directionalLights;
161+
public List<DirectionalShadowData> directionalShadows;
160162
public List<LightData> punctualLights;
163+
public List<PunctualShadowData> punctualShadows;
161164
public List<LightData> areaLights;
162165
public List<EnvLightData> envLights;
163-
public List<PunctualShadowData> punctualShadows;
166+
public Vector4[] directionalShadowSplitSphereSqr;
167+
168+
public void Clear()
169+
{
170+
directionalLights.Clear();
171+
directionalShadows.Clear();
172+
punctualLights.Clear();
173+
punctualShadows.Clear();
174+
areaLights.Clear();
175+
envLights.Clear();
176+
}
164177
}
165178

179+
LightList m_lightList;
180+
166181
// Detect when windows size is changing
167182
int m_WidthOnRecord;
168183
int m_HeightOnRecord;
@@ -254,6 +269,15 @@ public override void Rebuild()
254269
m_SinglePassLightLoop.Rebuild();
255270
// m_TilePassLightLoop = new TilePass.LightLoop();
256271
// m_TilePassLightLoop.Rebuild();
272+
273+
m_lightList = new LightList();
274+
m_lightList.directionalLights = new List<DirectionalLightData>();
275+
m_lightList.punctualLights = new List<LightData>();
276+
m_lightList.areaLights = new List<LightData>();
277+
m_lightList.envLights = new List<EnvLightData>();
278+
m_lightList.punctualShadows = new List<PunctualShadowData>();
279+
m_lightList.directionalShadows = new List<DirectionalShadowData>();
280+
m_lightList.directionalShadowSplitSphereSqr = new Vector4[k_MaxCascadeCount];
257281
}
258282

259283
void OnDisable()
@@ -680,15 +704,9 @@ void FinalPass(RenderLoop renderLoop)
680704
}
681705

682706
// Function to prepare light structure for GPU lighting
683-
void ConvertLightForGPU(CullResults cullResults, ref ShadowOutput shadowOutput, out LightList lightList)
707+
void ConvertLightForGPU(CullResults cullResults, ref ShadowOutput shadowOutput, ref LightList lightList)
684708
{
685-
// Init light list
686-
lightList = new LightList();
687-
lightList.directionalLights = new List<DirectionalLightData>();
688-
lightList.punctualLights = new List<LightData>();
689-
lightList.areaLights = new List<LightData>();
690-
lightList.envLights = new List<EnvLightData>();
691-
lightList.punctualShadows = new List<PunctualShadowData>();
709+
lightList.Clear();
692710

693711
for (int lightIndex = 0, numLights = cullResults.visibleLights.Length; lightIndex < numLights; ++lightIndex)
694712
{
@@ -710,7 +728,7 @@ void ConvertLightForGPU(CullResults cullResults, ref ShadowOutput shadowOutput,
710728

711729
if (light.lightType == LightType.Directional)
712730
{
713-
if (lightList.areaLights.Count >= k_MaxDirectionalLightsOnSCreen)
731+
if (lightList.directionalLights.Count >= k_MaxDirectionalLightsOnSCreen)
714732
continue;
715733

716734
var directionalLightData = new DirectionalLightData();
@@ -723,7 +741,32 @@ void ConvertLightForGPU(CullResults cullResults, ref ShadowOutput shadowOutput,
723741
directionalLightData.sinAngle = 0.0f;
724742
directionalLightData.shadowIndex = -1;
725743

726-
// TODO: shadow
744+
bool hasDirectionalShadows = light.light.shadows != LightShadows.None && shadowOutput.GetShadowSliceCountLightIndex(lightIndex) != 0;
745+
bool hasDirectionalNotReachMaxLimit = lightList.directionalShadows.Count == 0; // Only one cascade shadow allowed
746+
747+
if (hasDirectionalShadows && hasDirectionalNotReachMaxLimit) // Note < MaxShadows should be check at shadowOutput creation
748+
{
749+
// When we have a point light, we assumed that there is 6 consecutive PunctualShadowData
750+
directionalLightData.shadowIndex = 0;
751+
752+
for (int sliceIndex = 0; sliceIndex < shadowOutput.GetShadowSliceCountLightIndex(lightIndex); ++sliceIndex)
753+
{
754+
DirectionalShadowData directionalShadowData = new DirectionalShadowData();
755+
756+
int shadowSliceIndex = shadowOutput.GetShadowSliceIndex(lightIndex, sliceIndex);
757+
directionalShadowData.worldToShadow = shadowOutput.shadowSlices[shadowSliceIndex].shadowTransform.transpose; // Transpose for hlsl reading ?
758+
759+
directionalShadowData.bias = light.light.shadowBias;
760+
761+
lightList.directionalShadows.Add(directionalShadowData);
762+
}
763+
764+
// Fill split information for shaders
765+
for (int s = 0; s < k_MaxCascadeCount; ++s)
766+
{
767+
lightList.directionalShadowSplitSphereSqr[s] = shadowOutput.directionalShadowSplitSphereSqr[s];
768+
}
769+
}
727770

728771
lightList.directionalLights.Add(directionalLightData);
729772

@@ -733,15 +776,22 @@ void ConvertLightForGPU(CullResults cullResults, ref ShadowOutput shadowOutput,
733776
// Note: LightType.Area is offline only, use for baking, no need to test it
734777
var lightData = new LightData();
735778

736-
// Early out if we reach the maximum
737779
// Test whether we should treat this punctual light as an area light.
738780
// It's a temporary hack until the proper UI support is added.
739-
if (additionalData.treatAsAreaLight)
781+
if (additionalData.archetype != LightArchetype.Punctual)
740782
{
783+
// Early out if we reach the maximum
741784
if (lightList.areaLights.Count >= k_MaxAreaLightsOnSCreen)
742785
continue;
743786

744-
lightData.lightType = GPULightType.Rectangle;
787+
if (additionalData.archetype == LightArchetype.Rectangle)
788+
{
789+
lightData.lightType = GPULightType.Rectangle;
790+
}
791+
else
792+
{
793+
lightData.lightType = GPULightType.Line;
794+
}
745795
}
746796
else
747797
{
@@ -831,13 +881,14 @@ void ConvertLightForGPU(CullResults cullResults, ref ShadowOutput shadowOutput,
831881
lightData.size = new Vector2(additionalData.areaLightLength, additionalData.areaLightWidth);
832882
lightData.twoSided = additionalData.isDoubleSided;
833883

834-
if (additionalData.treatAsAreaLight)
884+
if (additionalData.archetype == LightArchetype.Punctual)
835885
{
836-
lightList.areaLights.Add(lightData);
886+
lightList.punctualLights.Add(lightData);
837887
}
838888
else
839889
{
840-
lightList.punctualLights.Add(lightData);
890+
// Area and line lights are both currently stored as area lights on the GPU.
891+
lightList.areaLights.Add(lightData);
841892
}
842893
}
843894

@@ -967,9 +1018,8 @@ public override void Render(Camera[] cameras, RenderLoop renderLoop)
9671018

9681019
renderLoop.SetupCameraProperties(camera); // Need to recall SetupCameraProperties after m_ShadowPass.Render
9691020

970-
LightList lightList;
971-
ConvertLightForGPU(cullResults, ref shadows, out lightList);
972-
PushGlobalParams(camera, renderLoop, lightList);
1021+
ConvertLightForGPU(cullResults, ref shadows, ref m_lightList);
1022+
PushGlobalParams(camera, renderLoop, m_lightList);
9731023

9741024
// build per tile light lists
9751025
//var numLights = 0; // GenerateSourceLightBuffers(camera, cullResults);

Assets/ScriptableRenderLoop/HDRenderLoop/Lighting/LightDefinition.cs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ public struct DirectionalLightData
6969
public float cosAngle; // Distance to disk
7070
public float sinAngle; // Disk radius
7171
public int shadowIndex;
72-
public float unsued;
72+
public float unused;
7373
};
7474

7575

@@ -88,6 +88,18 @@ public struct PunctualShadowData
8888
public float unused;
8989
};
9090

91+
[GenerateHLSL]
92+
public struct DirectionalShadowData
93+
{
94+
// World to ShadowMap matrix
95+
// Include scale and bias for shadow atlas if any
96+
public Matrix4x4 worldToShadow;
97+
98+
public float bias;
99+
public float quality;
100+
public Vector2 unused2;
101+
};
102+
91103
[GenerateHLSL]
92104
public enum EnvShapeType
93105
{

Assets/ScriptableRenderLoop/HDRenderLoop/Lighting/LightDefinition.cs.hlsl

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ struct DirectionalLightData
6060
float cosAngle;
6161
float sinAngle;
6262
int shadowIndex;
63-
float unsued;
63+
float unused;
6464
};
6565

6666
// Generated from UnityEngine.Experimental.ScriptableRenderLoop.PunctualShadowData
@@ -74,6 +74,16 @@ struct PunctualShadowData
7474
float unused;
7575
};
7676

77+
// Generated from UnityEngine.Experimental.ScriptableRenderLoop.DirectionalShadowData
78+
// PackingRules = Exact
79+
struct DirectionalShadowData
80+
{
81+
float4x4 worldToShadow;
82+
float bias;
83+
float quality;
84+
float2 unused2;
85+
};
86+
7787
// Generated from UnityEngine.Experimental.ScriptableRenderLoop.EnvLightData
7888
// PackingRules = Exact
7989
struct EnvLightData
@@ -195,9 +205,9 @@ int GetShadowIndex(DirectionalLightData value)
195205
{
196206
return value.shadowIndex;
197207
}
198-
float GetUnsued(DirectionalLightData value)
208+
float GetUnused(DirectionalLightData value)
199209
{
200-
return value.unsued;
210+
return value.unused;
201211
}
202212

203213
//
@@ -224,6 +234,26 @@ float GetUnused(PunctualShadowData value)
224234
return value.unused;
225235
}
226236

237+
//
238+
// Accessors for UnityEngine.Experimental.ScriptableRenderLoop.DirectionalShadowData
239+
//
240+
float4x4 GetWorldToShadow(DirectionalShadowData value)
241+
{
242+
return value.worldToShadow;
243+
}
244+
float GetBias(DirectionalShadowData value)
245+
{
246+
return value.bias;
247+
}
248+
float GetQuality(DirectionalShadowData value)
249+
{
250+
return value.quality;
251+
}
252+
float2 GetUnused2(DirectionalShadowData value)
253+
{
254+
return value.unused2;
255+
}
256+
227257
//
228258
// Accessors for UnityEngine.Experimental.ScriptableRenderLoop.EnvLightData
229259
//

Assets/ScriptableRenderLoop/HDRenderLoop/Lighting/SinglePass/SinglePass.cs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,16 @@ string GetKeyword()
2323
static ComputeBuffer s_EnvLightList;
2424
static ComputeBuffer s_AreaLightList;
2525
static ComputeBuffer s_PunctualShadowList;
26+
static ComputeBuffer s_DirectionalShadowList;
2627

2728
void ClearComputeBuffers()
2829
{
2930
if (s_DirectionalLights != null)
3031
s_DirectionalLights.Release();
3132

33+
if (s_DirectionalShadowList != null)
34+
s_DirectionalShadowList.Release();
35+
3236
if (s_PunctualLightList != null)
3337
s_PunctualLightList.Release();
3438

@@ -47,6 +51,7 @@ public void Rebuild()
4751
ClearComputeBuffers();
4852

4953
s_DirectionalLights = new ComputeBuffer(HDRenderLoop.k_MaxDirectionalLightsOnSCreen, System.Runtime.InteropServices.Marshal.SizeOf(typeof(DirectionalLightData)));
54+
s_DirectionalShadowList = new ComputeBuffer(HDRenderLoop.k_MaxCascadeCount, System.Runtime.InteropServices.Marshal.SizeOf(typeof(DirectionalShadowData)));
5055
s_PunctualLightList = new ComputeBuffer(HDRenderLoop.k_MaxPunctualLightsOnSCreen, System.Runtime.InteropServices.Marshal.SizeOf(typeof(LightData)));
5156
s_AreaLightList = new ComputeBuffer(HDRenderLoop.k_MaxAreaLightsOnSCreen, System.Runtime.InteropServices.Marshal.SizeOf(typeof(LightData)));
5257
s_EnvLightList = new ComputeBuffer(HDRenderLoop.k_MaxEnvLightsOnSCreen, System.Runtime.InteropServices.Marshal.SizeOf(typeof(EnvLightData)));
@@ -57,6 +62,8 @@ public void OnDisable()
5762
{
5863
s_DirectionalLights.Release();
5964
s_DirectionalLights = null;
65+
s_DirectionalShadowList.Release();
66+
s_DirectionalShadowList = null;
6067
s_PunctualLightList.Release();
6168
s_PunctualLightList = null;
6269
s_AreaLightList.Release();
@@ -70,20 +77,24 @@ public void OnDisable()
7077
public void PushGlobalParams(Camera camera, RenderLoop loop, HDRenderLoop.LightList lightList)
7178
{
7279
s_DirectionalLights.SetData(lightList.directionalLights.ToArray());
80+
s_DirectionalShadowList.SetData(lightList.directionalShadows.ToArray());
7381
s_PunctualLightList.SetData(lightList.punctualLights.ToArray());
7482
s_AreaLightList.SetData(lightList.areaLights.ToArray());
7583
s_EnvLightList.SetData(lightList.envLights.ToArray());
7684
s_PunctualShadowList.SetData(lightList.punctualShadows.ToArray());
7785

7886
Shader.SetGlobalBuffer("_DirectionalLightList", s_DirectionalLights);
79-
Shader.SetGlobalInt("_DirectionalLightCount", lightList.directionalLights.Count);
87+
Shader.SetGlobalBuffer("_DirectionalShadowList", s_DirectionalShadowList);
8088
Shader.SetGlobalBuffer("_PunctualLightList", s_PunctualLightList);
8189
Shader.SetGlobalInt("_PunctualLightCount", lightList.punctualLights.Count);
8290
Shader.SetGlobalBuffer("_AreaLightList", s_AreaLightList);
8391
Shader.SetGlobalInt("_AreaLightCount", lightList.areaLights.Count);
8492
Shader.SetGlobalBuffer("_PunctualShadowList", s_PunctualShadowList);
8593
Shader.SetGlobalBuffer("_EnvLightList", s_EnvLightList);
86-
Shader.SetGlobalInt("_EnvLightCount", lightList.envLights.Count);
94+
Shader.SetGlobalInt("_EnvLightCount", lightList.envLights.Count);
95+
96+
Shader.SetGlobalVectorArray("_DirShadowSplitSpheres", lightList.directionalShadowSplitSphereSqr);
97+
8798
}
8899
}
89100
}

0 commit comments

Comments
 (0)