Skip to content

Commit 3b66d6c

Browse files
author
runes
committed
TilePass support for arealights
1 parent 775617b commit 3b66d6c

File tree

7 files changed

+161
-80
lines changed

7 files changed

+161
-80
lines changed
Lines changed: 49 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
Shader "Hidden/HDRenderLoop/DebugViewTiles"
22
{
3-
SubShader
4-
{
3+
SubShader
4+
{
55

6-
Pass
7-
{
8-
ZWrite Off
9-
Blend SrcAlpha OneMinusSrcAlpha
6+
Pass
7+
{
8+
ZWrite Off
9+
Blend SrcAlpha OneMinusSrcAlpha
1010

11-
HLSLPROGRAM
11+
HLSLPROGRAM
1212
#pragma target 5.0
1313
#pragma only_renderers d3d11 // TEMP: unitl we go futher in dev
1414

@@ -20,35 +20,35 @@ Shader "Hidden/HDRenderLoop/DebugViewTiles"
2020

2121
#pragma multi_compile USE_FPTL_LIGHTLIST USE_CLUSTERED_LIGHTLIST
2222

23-
//-------------------------------------------------------------------------------------
24-
// Include
25-
//-------------------------------------------------------------------------------------
23+
//-------------------------------------------------------------------------------------
24+
// Include
25+
//-------------------------------------------------------------------------------------
2626

2727
#include "Common.hlsl"
2828

29-
// Note: We have fix as guidelines that we have only one deferred material (with control of GBuffer enabled). Mean a users that add a new
30-
// deferred material must replace the old one here. If in the future we want to support multiple layout (cause a lot of consistency problem),
31-
// the deferred shader will require to use multicompile.
29+
// Note: We have fix as guidelines that we have only one deferred material (with control of GBuffer enabled). Mean a users that add a new
30+
// deferred material must replace the old one here. If in the future we want to support multiple layout (cause a lot of consistency problem),
31+
// the deferred shader will require to use multicompile.
3232
#define UNITY_MATERIAL_LIT // Need to be define before including Material.hlsl
3333
#include "Assets/ScriptableRenderLoop/HDRenderLoop/ShaderConfig.cs.hlsl"
3434
#include "Assets/ScriptableRenderLoop/HDRenderLoop/ShaderVariables.hlsl"
3535
#include "Assets/ScriptableRenderLoop/HDRenderLoop/Lighting/Lighting.hlsl" // This include Material.hlsl
3636

37-
//-------------------------------------------------------------------------------------
38-
// variable declaration
39-
//-------------------------------------------------------------------------------------
37+
//-------------------------------------------------------------------------------------
38+
// variable declaration
39+
//-------------------------------------------------------------------------------------
4040

41-
uint _ViewTilesFlags;
41+
uint _ViewTilesFlags;
4242

4343
TEXTURE2D(_CameraDepthTexture);
4444
SAMPLER2D(sampler_CameraDepthTexture);
4545

4646
float4x4 _InvViewProjMatrix;
4747

48-
float4 Vert(float3 positionOS : POSITION): SV_POSITION
49-
{
50-
return TransformWorldToHClip(TransformObjectToWorld(positionOS));
51-
}
48+
float4 Vert(float3 positionOS : POSITION): SV_POSITION
49+
{
50+
return TransformWorldToHClip(TransformObjectToWorld(positionOS));
51+
}
5252

5353
float4 OverlayHeatMap(uint2 pixCoord, uint numLights)
5454
{
@@ -87,9 +87,9 @@ Shader "Hidden/HDRenderLoop/DebugViewTiles"
8787
return color;
8888
}
8989

90-
float4 Frag(float4 positionCS : SV_POSITION) : SV_Target
91-
{
92-
Coordinate coord = GetCoordinate(positionCS.xy, _ScreenSize.zw);
90+
float4 Frag(float4 positionCS : SV_POSITION) : SV_Target
91+
{
92+
Coordinate coord = GetCoordinate(positionCS.xy, _ScreenSize.zw);
9393

9494
#ifdef USE_CLUSTERED_LIGHTLIST
9595
// Perform same calculation than in deferred.shader
@@ -100,35 +100,28 @@ Shader "Hidden/HDRenderLoop/DebugViewTiles"
100100
float linearDepth = 0.0; // unused
101101
#endif
102102

103-
int n = 0;
104-
if (_ViewTilesFlags & DEBUGVIEWTILESFLAGS_DIRECT_LIGHTING)
105-
{
106-
uint punctualLightStart;
107-
uint punctualLightCount;
108-
GetCountAndStart(coord, DIRECT_LIGHT, linearDepth, punctualLightStart, punctualLightCount);
109-
n += punctualLightCount;
110-
}
111-
112-
if (_ViewTilesFlags & DEBUGVIEWTILESFLAGS_REFLECTION)
113-
{
114-
uint envLightStart;
115-
uint envLightCount;
116-
GetCountAndStart(coord, REFLECTION_LIGHT, linearDepth, envLightStart, envLightCount);
117-
n += envLightCount;
118-
}
119-
120-
if (n > 0)
121-
{
122-
return OverlayHeatMap(int2(coord.unPositionSS.xy) & 15, n);
123-
}
124-
else
125-
{
126-
return 0.0;
127-
}
128-
}
129-
130-
ENDHLSL
131-
}
132-
}
133-
Fallback Off
134-
}
103+
int n = 0;
104+
for(int category = 0; category < NR_LIGHT_MODELS; category++)
105+
{
106+
uint mask = 1u << category;
107+
uint start;
108+
uint count;
109+
GetCountAndStart(coord, category, linearDepth, start, count);
110+
n += count;
111+
}
112+
113+
if (n > 0)
114+
{
115+
return OverlayHeatMap(int2(coord.unPositionSS.xy) & 15, n);
116+
}
117+
else
118+
{
119+
return 0.0;
120+
}
121+
}
122+
123+
ENDHLSL
124+
}
125+
}
126+
Fallback Off
127+
}

Assets/ScriptableRenderLoop/HDRenderLoop/Editor/HDRenderLoopInspector.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ private class Styles
4545
public readonly GUIContent shadowsCascades = new GUIContent("Cascade values");
4646

4747
public readonly GUIContent tileLightLoopSettings = new GUIContent("Tile Light Loop settings");
48-
public readonly GUIContent tileLightLoopDebugMode = new GUIContent("Enable Debug mode", "Toggle overheat map mode");
48+
public readonly string[] tileLightLoopDebugTileFlagStrings = new string[] { "Direct Light", "Reflection Light", "Area Light"};
4949
public readonly GUIContent directIndirectSinglePass = new GUIContent("Enable direct and indirect lighting in single pass", "Toggle");
5050
public readonly GUIContent bigTilePrepass = new GUIContent("Enable big tile prepass", "Toggle");
5151
public readonly GUIContent clustered = new GUIContent("Enable clustered", "Toggle");
@@ -271,7 +271,7 @@ public override void OnInspectorGUI()
271271
EditorGUI.indentLevel++;
272272
EditorGUI.BeginChangeCheck();
273273

274-
renderLoop.tilePassLightLoop.debugViewTilesFlags = (TilePass.DebugViewTilesFlags)EditorGUILayout.EnumMaskField("DebugView Tiles", renderLoop.tilePassLightLoop.debugViewTilesFlags);
274+
renderLoop.tilePassLightLoop.debugViewTilesFlags = EditorGUILayout.MaskField("DebugView Tiles", renderLoop.tilePassLightLoop.debugViewTilesFlags, styles.tileLightLoopDebugTileFlagStrings);
275275
renderLoop.tilePassLightLoop.enableDirectIndirectSinglePass = EditorGUILayout.Toggle(styles.directIndirectSinglePass, renderLoop.tilePassLightLoop.enableDirectIndirectSinglePass);
276276
renderLoop.tilePassLightLoop.enableBigTilePrepass = EditorGUILayout.Toggle(styles.bigTilePrepass, renderLoop.tilePassLightLoop.enableBigTilePrepass);
277277
renderLoop.tilePassLightLoop.enableClustered = EditorGUILayout.Toggle(styles.clustered, renderLoop.tilePassLightLoop.enableClustered);

Assets/ScriptableRenderLoop/HDRenderLoop/Lighting/TilePass/Resources/lightlistbuild-clustered.compute

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,7 @@ void LIGHTLISTGEN(uint threadID : SV_GroupIndex, uint3 u3GroupID : SV_GroupID)
331331
InterlockedAdd(g_LayeredSingleIdxBuffer[0], (uint) iSpaceAvail, start); // alloc list memory
332332
}
333333

334-
int modelListCount[NR_LIGHT_MODELS]={0,0}; // direct light count and reflection lights
334+
int modelListCount[NR_LIGHT_MODELS]={0,0,0}; // direct light count and reflection lights
335335
uint offs = start;
336336
for(int ll=0; ll<iNrCoarseLights; ll+=4)
337337
{

Assets/ScriptableRenderLoop/HDRenderLoop/Lighting/TilePass/Resources/lightlistbuild.compute

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ groupshared uint ldsDoesLightIntersect[2];
5252
#endif
5353
groupshared int ldsNrLightsFinal;
5454

55-
groupshared int ldsModelListCount[NR_LIGHT_MODELS]; // since NR_LIGHT_MODELS is 2
55+
groupshared int ldsModelListCount[NR_LIGHT_MODELS]; // since NR_LIGHT_MODELS is 3
5656

5757
#ifdef PERFORM_SPHERICAL_INTERSECTION_TESTS
5858
groupshared uint lightOffsSph;

Assets/ScriptableRenderLoop/HDRenderLoop/Lighting/TilePass/TilePass.cs

Lines changed: 94 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -37,18 +37,12 @@ public class LightDefinitions
3737
public static int DIRECTIONAL_LIGHT = 3;
3838

3939
// direct lights and reflection probes for now
40-
public static int NR_LIGHT_MODELS = 2;
40+
public static int NR_LIGHT_MODELS = 3;
4141
public static int DIRECT_LIGHT = 0;
4242
public static int REFLECTION_LIGHT = 1;
43+
public static int AREA_LIGHT = 2;
4344
}
4445

45-
[GenerateHLSL]
46-
public enum DebugViewTilesFlags
47-
{
48-
DirectLighting = 1,
49-
Reflection = 2
50-
};
51-
5246
[GenerateHLSL]
5347
public struct SFiniteLightBound
5448
{
@@ -76,7 +70,7 @@ public struct LightShapeData
7670
public float cotan;
7771

7872
public Vector3 boxInnerDist;
79-
public uint lightCategory; // DIRECT_LIGHT=0, REFLECTION_LIGHT=1
73+
public uint lightCategory; // DIRECT_LIGHT=0, REFLECTION_LIGHT=1, AREA_LIGHT=2
8074

8175
public Vector3 boxInvRange;
8276
public float unused2;
@@ -111,7 +105,7 @@ string GetKeyword()
111105
private static int s_GenListPerBigTileKernel;
112106

113107
// clustered light list specific buffers and data begin
114-
public DebugViewTilesFlags debugViewTilesFlags = 0;
108+
public int debugViewTilesFlags = 0;
115109
public bool enableClustered = false;
116110
public bool disableFptlWhenClustered = true; // still useful on opaques. Should be false by default to force tile on opaque.
117111
public bool enableBigTilePrepass = true;
@@ -348,7 +342,6 @@ public void PrepareLightsForGPU(CullResults cullResults, Camera camera, HDRender
348342
// Use for the second pass (fine pruning)
349343
var numEntries2nd = new int[numModels, numVolTypes];
350344

351-
// TODO manage area lights
352345
foreach (var punctualLight in lightList.punctualLights)
353346
{
354347
var volType = punctualLight.lightType == GPULightType.Spot ? LightDefinitions.SPOT_LIGHT : (punctualLight.lightType == GPULightType.Point ? LightDefinitions.SPHERE_LIGHT : -1);
@@ -361,8 +354,15 @@ public void PrepareLightsForGPU(CullResults cullResults, Camera camera, HDRender
361354
{
362355
var volType = LightDefinitions.BOX_LIGHT; // always a box for now
363356
++numEntries[LightDefinitions.REFLECTION_LIGHT, volType];
357+
}
358+
359+
foreach (var areaLight in lightList.areaLights)
360+
{
361+
var volType = LightDefinitions.BOX_LIGHT;
362+
++numEntries[LightDefinitions.AREA_LIGHT, volType];
364363
}
365364

365+
366366
// add decals here too similar to the above
367367

368368
// establish offsets
@@ -508,7 +508,7 @@ public void PrepareLightsForGPU(CullResults cullResults, Camera camera, HDRender
508508
//Vector3 C = bnds.center; // P + boxOffset;
509509
var C = mat.MultiplyPoint(boxOffset); // same as commented out line above when rot is identity
510510

511-
var combinedExtent = e + new Vector3(blendDistance, blendDistance, blendDistance);
511+
var combinedExtent = e + new Vector3(blendDistance, blendDistance, blendDistance);
512512

513513
Vector3 vx = mat.GetColumn(0);
514514
Vector3 vy = mat.GetColumn(1);
@@ -547,6 +547,86 @@ public void PrepareLightsForGPU(CullResults cullResults, Camera camera, HDRender
547547
m_lightShapeData[index] = lightShapeData;
548548
}
549549

550+
for (int areaLightIndex = 0; areaLightIndex < lightList.areaLights.Count; areaLightIndex++)
551+
{
552+
LightData areaLightData = lightList.areaLights[areaLightIndex];
553+
VisibleLight light = cullResults.visibleLights[lightList.areaCullIndices[areaLightIndex]];
554+
555+
// Fill bounds
556+
var bound = new SFiniteLightBound();
557+
var lightShapeData = new LightShapeData();
558+
559+
lightShapeData.lightType = (uint)LightDefinitions.BOX_LIGHT;
560+
lightShapeData.lightCategory = (uint)LightDefinitions.AREA_LIGHT;
561+
lightShapeData.lightIndex = (uint)areaLightIndex;
562+
563+
564+
if (areaLightData.lightType == GPULightType.Rectangle)
565+
{
566+
Vector3 centerVS = worldToView.MultiplyPoint(areaLightData.positionWS);
567+
Vector3 xAxisVS = worldToView.MultiplyVector(areaLightData.right);
568+
Vector3 yAxisVS = worldToView.MultiplyVector(areaLightData.up);
569+
Vector3 zAxisVS = worldToView.MultiplyVector(areaLightData.forward);
570+
float radius = 1.0f / Mathf.Sqrt(areaLightData.invSqrAttenuationRadius);
571+
572+
Vector3 dimensions = new Vector3(areaLightData.size.x * 0.5f + radius, areaLightData.size.y * 0.5f + radius, radius);
573+
574+
if(!areaLightData.twoSided)
575+
{
576+
centerVS -= zAxisVS * radius * 0.5f;
577+
dimensions.z *= 0.5f;
578+
}
579+
580+
bound.center = centerVS;
581+
bound.boxAxisX = dimensions.x * xAxisVS;
582+
bound.boxAxisY = dimensions.y * yAxisVS;
583+
bound.boxAxisZ = dimensions.z * zAxisVS;
584+
bound.scaleXY.Set(1.0f, 1.0f);
585+
bound.radius = dimensions.magnitude;
586+
587+
lightShapeData.lightPos = centerVS;
588+
lightShapeData.lightAxisX = xAxisVS;
589+
lightShapeData.lightAxisY = yAxisVS;
590+
lightShapeData.lightAxisZ = zAxisVS;
591+
lightShapeData.boxInnerDist = dimensions;
592+
lightShapeData.boxInvRange.Set(1e5f, 1e5f, 1e5f);
593+
}
594+
else if (areaLightData.lightType == GPULightType.Line)
595+
{
596+
Vector3 centerVS = worldToView.MultiplyPoint(areaLightData.positionWS);
597+
Vector3 xAxisVS = worldToView.MultiplyVector(areaLightData.right);
598+
Vector3 yAxisVS = worldToView.MultiplyVector(areaLightData.up);
599+
Vector3 zAxisVS = worldToView.MultiplyVector(areaLightData.forward);
600+
float radius = 1.0f / Mathf.Sqrt(areaLightData.invSqrAttenuationRadius);
601+
602+
Vector3 dimensions = new Vector3(areaLightData.size.x * 0.5f + radius, radius, radius);
603+
604+
bound.center = centerVS;
605+
bound.boxAxisX = dimensions.x * xAxisVS;
606+
bound.boxAxisY = dimensions.y * yAxisVS;
607+
bound.boxAxisZ = dimensions.z * zAxisVS;
608+
bound.scaleXY.Set(1.0f, 1.0f);
609+
bound.radius = dimensions.magnitude;
610+
611+
lightShapeData.lightPos = centerVS;
612+
lightShapeData.lightAxisX = xAxisVS;
613+
lightShapeData.lightAxisY = yAxisVS;
614+
lightShapeData.lightAxisZ = zAxisVS;
615+
lightShapeData.boxInnerDist = new Vector3(areaLightData.size.x * 0.5f, 0.01f, 0.01f);
616+
lightShapeData.boxInvRange.Set(1.0f / radius, 1.0f / radius, 1.0f / radius);
617+
}
618+
else
619+
{
620+
Debug.Assert(false);
621+
}
622+
623+
int i = LightDefinitions.AREA_LIGHT, j = LightDefinitions.BOX_LIGHT;
624+
int index = numEntries2nd[i, j] + offsets[i, j]; ++numEntries2nd[i, j];
625+
626+
m_boundData[index] = bound;
627+
m_lightShapeData[index] = lightShapeData;
628+
}
629+
550630
// Sanity check
551631
for (var m = 0; m < numModels; m++)
552632
{
@@ -556,7 +636,7 @@ public void PrepareLightsForGPU(CullResults cullResults, Camera camera, HDRender
556636
}
557637
}
558638

559-
m_lightCount = lightList.punctualLights.Count + lightList.envLights.Count;
639+
m_lightCount = lightList.punctualLights.Count + lightList.envLights.Count + lightList.areaLights.Count;
560640
s_ConvexBoundsBuffer.SetData(m_boundData); // TODO: check with Vlad what is happening here, do we copy 1024 element always ? Could we setup the size we want to copy ?
561641
s_LightShapeDataBuffer.SetData(m_lightShapeData);
562642
}
@@ -757,7 +837,7 @@ public void RenderDeferredLighting(Camera camera, RenderLoop renderLoop, RenderT
757837

758838
m_DebugViewTilesMaterial.SetMatrix("_InvViewProjMatrix", invViewProj);
759839
m_DebugViewTilesMaterial.SetVector("_ScreenSize", screenSize);
760-
m_DebugViewTilesMaterial.SetInt("_ViewTilesFlags", (int)debugViewTilesFlags);
840+
m_DebugViewTilesMaterial.SetInt("_ViewTilesFlags", debugViewTilesFlags);
761841
m_DebugViewTilesMaterial.EnableKeyword(bUseClusteredForDeferred ? "USE_CLUSTERED_LIGHTLIST" : "USE_FPTL_LIGHTLIST");
762842
m_DebugViewTilesMaterial.DisableKeyword(!bUseClusteredForDeferred ? "USE_CLUSTERED_LIGHTLIST" : "USE_FPTL_LIGHTLIST");
763843

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,10 @@
2020
#define SPHERE_LIGHT (1)
2121
#define BOX_LIGHT (2)
2222
#define DIRECTIONAL_LIGHT (3)
23-
#define NR_LIGHT_MODELS (2)
23+
#define NR_LIGHT_MODELS (3)
2424
#define DIRECT_LIGHT (0)
2525
#define REFLECTION_LIGHT (1)
26+
#define AREA_LIGHT (2)
2627

2728
//
2829
// UnityEngine.Experimental.ScriptableRenderLoop.TilePass.DebugViewTilesFlags: static fields

0 commit comments

Comments
 (0)