Skip to content

Commit da3a6ee

Browse files
HDRenderLoop: First no working version of cluster FPTL
1 parent b4b6152 commit da3a6ee

File tree

7 files changed

+50
-61
lines changed

7 files changed

+50
-61
lines changed

Assets/ScriptableRenderLoop/HDRenderLoop/Debug/Resources/DebugViewTiles.shader

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,10 @@ Shader "Hidden/HDRenderLoop/DebugViewTiles"
4040

4141
uint _ViewTilesFlags;
4242

43-
TEXTURE2D(_CameraDepthTexture);
44-
SAMPLER2D(sampler_CameraDepthTexture);
43+
TEXTURE2D(_CameraDepthTexture);
44+
SAMPLER2D(sampler_CameraDepthTexture);
45+
46+
float4x4 _InvViewProjMatrix;
4547

4648
float4 Vert(float3 positionOS : POSITION): SV_POSITION
4749
{
@@ -90,8 +92,10 @@ Shader "Hidden/HDRenderLoop/DebugViewTiles"
9092
Coordinate coord = GetCoordinate(positionCS.xy, _ScreenSize.zw);
9193

9294
#ifdef USE_CLUSTERED_LIGHTLIST
95+
// Perform same calculation than in deferred.shader
9396
float depth = LOAD_TEXTURE2D(_CameraDepthTexture, coord.unPositionSS).x;
94-
float linearDepth = GetLinearDepth(depth); // View space linear depth
97+
float3 positionWS = UnprojectToWorld(depth, coord.positionSS, _InvViewProjMatrix);
98+
float linearDepth = TransformWorldToView(positionWS).z; // View space linear depth
9599
#else
96100
float linearDepth = 0.0; // unused
97101
#endif

Assets/ScriptableRenderLoop/HDRenderLoop/Editor/HDRenderLoopInspector.cs

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ public override void OnInspectorGUI()
171171
FillWithProperties(typeof(Builtin.BuiltinData), styles.debugViewMaterialStrings, styles.debugViewMaterialValues, false, GetSubNameSpaceName(typeof(Lit.SurfaceData)), ref index);
172172
FillWithProperties(typeof(Lit.SurfaceData), styles.debugViewMaterialStrings, styles.debugViewMaterialValues, false, GetSubNameSpaceName(typeof(Lit.SurfaceData)), ref index);
173173
FillWithProperties(typeof(Builtin.BuiltinData), styles.debugViewMaterialStrings, styles.debugViewMaterialValues, false, GetSubNameSpaceName(typeof(Unlit.SurfaceData)), ref index);
174-
FillWithProperties(typeof(Unlit.SurfaceData), styles.debugViewMaterialStrings, styles.debugViewMaterialValues, false, GetSubNameSpaceName(typeof(Unlit.SurfaceData)), ref index);
174+
FillWithProperties(typeof(Unlit.SurfaceData), styles.debugViewMaterialStrings, styles.debugViewMaterialValues, false, GetSubNameSpaceName(typeof(Unlit.SurfaceData)), ref index);
175175

176176
// Engine
177177
FillWithPropertiesEnum(typeof(Attributes.DebugViewGbuffer), styles.debugViewMaterialStrings, styles.debugViewMaterialValues, "", true, ref index);
@@ -190,9 +190,9 @@ public override void OnInspectorGUI()
190190
EditorGUILayout.Space();
191191
debugParameters.displayOpaqueObjects = EditorGUILayout.Toggle(styles.displayOpaqueObjects, debugParameters.displayOpaqueObjects);
192192
debugParameters.displayTransparentObjects = EditorGUILayout.Toggle(styles.displayTransparentObjects, debugParameters.displayTransparentObjects);
193-
debugParameters.useForwardRenderingOnly = EditorGUILayout.Toggle(styles.useForwardRenderingOnly, debugParameters.useForwardRenderingOnly);
193+
debugParameters.useForwardRenderingOnly = EditorGUILayout.Toggle(styles.useForwardRenderingOnly, debugParameters.useForwardRenderingOnly);
194194
debugParameters.useDepthPrepass = EditorGUILayout.Toggle(styles.useDepthPrepass, debugParameters.useDepthPrepass);
195-
debugParameters.useSinglePassLightLoop = EditorGUILayout.Toggle(styles.useSinglePassLightLoop, debugParameters.useSinglePassLightLoop);
195+
debugParameters.useSinglePassLightLoop = EditorGUILayout.Toggle(styles.useSinglePassLightLoop, debugParameters.useSinglePassLightLoop);
196196

197197
if (EditorGUI.EndChangeCheck())
198198
{
@@ -234,7 +234,7 @@ public override void OnInspectorGUI()
234234
shadowParameters.directionalLightCascadeCount = EditorGUILayout.IntPopup(styles.shadowsDirectionalLightCascadeCount, shadowParameters.directionalLightCascadeCount, styles.shadowsCascadeCounts, styles.shadowsCascadeCountValues);
235235

236236
EditorGUI.indentLevel++;
237-
for (int i = 0; i < shadowParameters.directionalLightCascadeCount-1; i++)
237+
for (int i = 0; i < shadowParameters.directionalLightCascadeCount - 1; i++)
238238
{
239239
shadowParameters.directionalLightCascades[i] = Mathf.Max(0, EditorGUILayout.FloatField(shadowParameters.directionalLightCascades[i]));
240240
}
@@ -255,7 +255,7 @@ public override void OnInspectorGUI()
255255
textureParameters.spotCookieSize = Mathf.NextPowerOfTwo(Mathf.Clamp(EditorGUILayout.IntField(styles.spotCookieSize, textureParameters.spotCookieSize), 16, 1024));
256256
textureParameters.pointCookieSize = Mathf.NextPowerOfTwo(Mathf.Clamp(EditorGUILayout.IntField(styles.pointCookieSize, textureParameters.pointCookieSize), 16, 1024));
257257
textureParameters.reflectionCubemapSize = Mathf.NextPowerOfTwo(Mathf.Clamp(EditorGUILayout.IntField(styles.reflectionCubemapSize, textureParameters.reflectionCubemapSize), 64, 1024));
258-
258+
259259
if (EditorGUI.EndChangeCheck())
260260
{
261261
EditorUtility.SetDirty(renderLoop); // Repaint
@@ -264,23 +264,27 @@ public override void OnInspectorGUI()
264264

265265

266266
EditorGUILayout.Space();
267-
EditorGUILayout.LabelField(styles.tileLightLoopSettings);
268-
EditorGUI.indentLevel++;
269-
EditorGUI.BeginChangeCheck();
270267

271268
if (renderLoop.tilePassLightLoop != null)
272269
{
270+
EditorGUILayout.LabelField(styles.tileLightLoopSettings);
271+
EditorGUI.indentLevel++;
272+
EditorGUI.BeginChangeCheck();
273+
273274
renderLoop.tilePassLightLoop.debugViewTilesFlags = (TilePass.DebugViewTilesFlags)EditorGUILayout.EnumMaskField("DebugView Tiles", renderLoop.tilePassLightLoop.debugViewTilesFlags);
274275
renderLoop.tilePassLightLoop.enableDirectIndirectSinglePass = EditorGUILayout.Toggle(styles.directIndirectSinglePass, renderLoop.tilePassLightLoop.enableDirectIndirectSinglePass);
275276
renderLoop.tilePassLightLoop.enableBigTilePrepass = EditorGUILayout.Toggle(styles.bigTilePrepass, renderLoop.tilePassLightLoop.enableBigTilePrepass);
276277
renderLoop.tilePassLightLoop.enableClustered = EditorGUILayout.Toggle(styles.clustered, renderLoop.tilePassLightLoop.enableClustered);
277-
}
278278

279-
if (EditorGUI.EndChangeCheck())
280-
{
281-
EditorUtility.SetDirty(renderLoop); // Repaint
279+
if (EditorGUI.EndChangeCheck())
280+
{
281+
EditorUtility.SetDirty(renderLoop); // Repaint
282+
283+
// If something is chanage on tilePassLightLoop we need to force a OnValidate() OnHDRenderLoop, else change Rebuild() will not be call
284+
renderLoop.OnValidate();
285+
}
286+
EditorGUI.indentLevel--;
282287
}
283-
EditorGUI.indentLevel--;
284288
}
285289
}
286290
}

Assets/ScriptableRenderLoop/HDRenderLoop/HDRenderLoop.cs

Lines changed: 11 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ public TextureSettings textureSettings
155155
int m_VelocityBuffer;
156156
int m_DistortionBuffer;
157157

158-
bool m_Dirty = false;
158+
public bool m_Dirty = false;
159159

160160
RenderTargetIdentifier m_CameraColorBufferRT;
161161
RenderTargetIdentifier m_CameraDepthBufferRT;
@@ -217,21 +217,23 @@ public void Allocate()
217217
int m_HeightOnRecord;
218218

219219
// TODO: Find a way to automatically create/iterate through lightloop
220-
SinglePass.LightLoop m_SinglePassLightLoop;
221-
TilePass.LightLoop m_TilePassLightLoop = null;
220+
// This must be init externally else The value can't be set in the inspector... (as it will recreate the class with default value)
221+
SinglePass.LightLoop m_SinglePassLightLoop = new SinglePass.LightLoop();
222+
TilePass.LightLoop m_TilePassLightLoop = new TilePass.LightLoop();
222223
public TilePass.LightLoop tilePassLightLoop
223224
{
224225
get { return m_TilePassLightLoop; }
225226
}
226227

227228
// TODO: Find a way to automatically create/iterate through deferred material
228-
Lit.RenderLoop m_LitRenderLoop;
229+
// TODO TO CHECK: SebL I move allocation from Rebuild() to here, but there was a comment "// Our object can be garbage collected, so need to be allocate here", it is still true ?
230+
Lit.RenderLoop m_LitRenderLoop = new Lit.RenderLoop();
229231

230232
TextureCacheCubemap m_CubeReflTexArray;
231233
TextureCache2D m_CookieTexArray;
232234
TextureCacheCubemap m_CubeCookieTexArray;
233235

234-
private void OnValidate()
236+
public void OnValidate()
235237
{
236238
// Calling direction Rebuild() here cause this warning:
237239
// "SendMessage cannot be called during Awake, CheckConsistency, or OnValidate UnityEngine.Experimental.ScriptableRenderLoop.HDRenderLoop:OnValidate()"
@@ -259,8 +261,7 @@ public override void Rebuild()
259261

260262
m_ShadowPass = new ShadowRenderPass(m_ShadowSettings);
261263

262-
// Init Gbuffer description
263-
m_LitRenderLoop = new Lit.RenderLoop(); // Our object can be garbage collected, so need to be allocate here
264+
// Init Gbuffer description
264265

265266
m_gbufferManager.gbufferCount = m_LitRenderLoop.GetMaterialGBufferCount();
266267
RenderTextureFormat[] RTFormat; RenderTextureReadWrite[] RTReadWrite;
@@ -295,9 +296,7 @@ public override void Rebuild()
295296
m_CubeReflTexArray.AllocTextureArray(32, m_TextureSettings.reflectionCubemapSize, TextureFormat.BC6H, true);
296297

297298
// Init various light loop
298-
m_SinglePassLightLoop = new SinglePass.LightLoop();
299299
m_SinglePassLightLoop.Rebuild();
300-
m_TilePassLightLoop = new TilePass.LightLoop();
301300
tilePassLightLoop.Rebuild();
302301

303302
m_lightList = new LightList();
@@ -320,23 +319,9 @@ public override void Initialize()
320319

321320
public override void Cleanup()
322321
{
323-
if (m_LitRenderLoop != null)
324-
{
325-
m_LitRenderLoop.Cleanup();
326-
m_LitRenderLoop = null;
327-
}
328-
329-
if (m_SinglePassLightLoop != null)
330-
{
331-
m_SinglePassLightLoop.Cleanup();
332-
m_SinglePassLightLoop = null;
333-
}
334-
335-
if (m_TilePassLightLoop != null)
336-
{
337-
m_TilePassLightLoop.Cleanup();
338-
m_TilePassLightLoop = null;
339-
}
322+
m_LitRenderLoop.Cleanup();
323+
m_SinglePassLightLoop.Cleanup();
324+
m_TilePassLightLoop.Cleanup();
340325

341326
Utilities.Destroy(m_FinalPassMaterial);
342327
Utilities.Destroy(m_DebugViewMaterialGBuffer);

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ string GetKeyword()
113113
// clustered light list specific buffers and data begin
114114
public DebugViewTilesFlags debugViewTilesFlags = 0;
115115
public bool enableClustered = false;
116-
public bool disableFptlWhenClustered = false; // still useful on opaques
116+
public bool disableFptlWhenClustered = true; // still useful on opaques. Should be false by default to force tile on opaque.
117117
public bool enableBigTilePrepass = true;
118118
public bool enableDrawLightBoundsDebug = false;
119119
public bool enableDirectIndirectSinglePass = false;
@@ -754,7 +754,8 @@ public void RenderDeferredLighting(Camera camera, RenderLoop renderLoop, RenderT
754754
m_DeferredAllMaterial.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.Zero);
755755
m_DeferredAllMaterial.EnableKeyword(bUseClusteredForDeferred ? "USE_CLUSTERED_LIGHTLIST" : "USE_FPTL_LIGHTLIST");
756756
m_DeferredAllMaterial.DisableKeyword(!bUseClusteredForDeferred ? "USE_CLUSTERED_LIGHTLIST" : "USE_FPTL_LIGHTLIST");
757-
757+
758+
m_DebugViewTilesMaterial.SetMatrix("_InvViewProjMatrix", invViewProj);
758759
m_DebugViewTilesMaterial.SetVector("_ScreenSize", screenSize);
759760
m_DebugViewTilesMaterial.SetInt("_ViewTilesFlags", (int)debugViewTilesFlags);
760761
m_DebugViewTilesMaterial.EnableKeyword(bUseClusteredForDeferred ? "USE_CLUSTERED_LIGHTLIST" : "USE_FPTL_LIGHTLIST");

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ Buffer<uint> g_vLightListGlobal;
2121
// these uniforms are only needed for when OPAQUES_ONLY is NOT defined
2222
// but there's a problem with our front-end compilation of compute shaders with multiple kernels causing it to error
2323
//#ifdef USE_CLUSTERED_LIGHTLIST
24+
float4x4 g_mInvScrProjection;
25+
2426
float g_fClustScale;
2527
float g_fClustBase;
2628
float g_fNearPlane;

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

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -82,19 +82,6 @@ uint FetchIndex(uint tileOffset, uint lightIndex)
8282
return FetchIndexCluster(tileOffset, lightIndex);
8383
}
8484

85-
float GetLinearDepth(float zDptBufSpace) // 0 is near 1 is far
86-
{
87-
// todo (simplify): m22 is zero and m23 is +1/-1 (depends on left/right hand proj)
88-
float m22 = g_mInvScrProjection[2].z, m23 = g_mInvScrProjection[2].w;
89-
float m32 = g_mInvScrProjection[3].z, m33 = g_mInvScrProjection[3].w;
90-
91-
return (m22 * zDptBufSpace + m23) / (m32 * zDptBufSpace + m33);
92-
93-
//float3 vP = float3(0.0f,0.0f,zDptBufSpace);
94-
//float4 v4Pres = mul(g_mInvScrProjection, float4(vP,1.0));
95-
//return v4Pres.z / v4Pres.w;
96-
}
97-
9885
#endif
9986

10087
// bakeDiffuseLighting is part of the prototype so a user is able to implement a "base pass" with GI and multipass direct light (aka old unity rendering path)
@@ -103,9 +90,7 @@ void LightLoop( float3 V, float3 positionWS, Coordinate coord, PreLightData prel
10390
out float3 specularLighting)
10491
{
10592
#ifdef USE_CLUSTERED_LIGHTLIST
106-
// TODO: Think more about the design, it is ok to do that ? hope the compiler could optimize it out as we already depth before LightLoop call, else need to pass it as argument...
107-
float depth = LOAD_TEXTURE2D(_CameraDepthTexture, coord.unPositionSS).x;
108-
float linearDepth = GetLinearDepth(depth); // View space linear depth
93+
float linearDepth = TransformWorldToView(positionWS).z; // View space linear depth
10994
#else
11095
float linearDepth = 0.0; // unused
11196
#endif

Assets/ScriptableRenderLoop/HDRenderLoop/ShaderVariables.hlsl

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,10 @@ CBUFFER_END
198198
// TODO: move this to constant buffer by Pass
199199
float4 _ScreenSize;
200200

201+
float4x4 GetWorldToViewMatrix()
202+
{
203+
return unity_MatrixV;
204+
}
201205

202206
float4x4 GetObjectToWorldMatrix()
203207
{
@@ -226,12 +230,16 @@ float GetOdddNegativeScale()
226230
return unity_WorldTransformParams.w;
227231
}
228232

229-
230233
float4x4 GetObjectToWorldViewMatrix()
231234
{
232235
return glstate_matrix_modelview0;
233236
}
234237

238+
float3 TransformWorldToView(float3 positionWS)
239+
{
240+
return mul(GetWorldToViewMatrix(), float4(positionWS, 1.0)).xyz;
241+
}
242+
235243
float3 TransformObjectToWorld(float3 positionOS)
236244
{
237245
return mul(GetObjectToWorldMatrix(), float4(positionOS, 1.0)).xyz;

0 commit comments

Comments
 (0)