Skip to content

Commit 673f88f

Browse files
HDRenderLoop: Merge FTPL - application of opaque tiled lists (untested)
1 parent 7c59a7e commit 673f88f

File tree

16 files changed

+306
-122
lines changed

16 files changed

+306
-122
lines changed

Assets/ScriptableRenderLoop/HDRenderLoop/HDRenderLoop.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -468,7 +468,7 @@ void RenderDeferredLighting(Camera camera, RenderLoop renderLoop)
468468
m_LitRenderLoop.Bind();
469469

470470
m_SinglePassLightLoop.RenderDeferredLighting(camera, renderLoop, m_CameraColorBuffer);
471-
// m_TilePassLightLoop.RenderDeferredLighting(camera, renderLoop, );
471+
// m_TilePassLightLoop.RenderDeferredLighting(camera, renderLoop, m_CameraColorBuffer);
472472
}
473473

474474
void RenderSky(Camera camera, RenderLoop renderLoop)
@@ -810,7 +810,7 @@ void PrepareLightsForGPU(CullResults cullResults, Camera camera, ref ShadowOutpu
810810

811811
// build per tile light lists
812812
m_SinglePassLightLoop.PrepareLightsForGPU(cullResults, camera, m_lightList);
813-
m_TilePassLightLoop.PrepareLightsForGPU(cullResults, camera, m_lightList);
813+
//m_TilePassLightLoop.PrepareLightsForGPU(cullResults, camera, m_lightList);
814814
}
815815

816816
void Resize(Camera camera)
@@ -891,7 +891,7 @@ public override void Render(Camera[] cameras, RenderLoop renderLoop)
891891
renderLoop.SetupCameraProperties(camera); // Need to recall SetupCameraProperties after m_ShadowPass.Render
892892

893893
PrepareLightsForGPU(cullResults, camera, ref shadows, ref m_lightList);
894-
m_TilePassLightLoop.BuildGPULightLists(camera, renderLoop, m_lightList, m_CameraDepthBuffer);
894+
//m_TilePassLightLoop.BuildGPULightLists(camera, renderLoop, m_lightList, m_CameraDepthBuffer);
895895

896896
PushGlobalParams(camera, renderLoop, m_lightList);
897897

Assets/ScriptableRenderLoop/HDRenderLoop/Lighting/Lighting.hlsl

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,10 @@
1818

1919
#include "Assets/ScriptableRenderLoop/HDRenderLoop/Lighting/LightDefinition.cs.hlsl"
2020

21-
#ifdef LIGHTLOOP_SINGLE_PASS
21+
#ifdef LIGHTLOOP_SINGLE_PASS
2222
#include "Assets/ScriptableRenderLoop/HDRenderLoop/Lighting/SinglePass/SinglePass.hlsl"
23+
#elif LIGHTLOOP_TILED_PASS
24+
#include "Assets/ScriptableRenderLoop/HDRenderLoop/Lighting/TilePass/TilePass.hlsl"
2325
#endif
2426

2527
// Shadow use samling function define in header above and must be include before Material.hlsl
@@ -29,6 +31,8 @@
2931
// LightLoop use evaluation BSDF function for light type define in Material.hlsl
3032
#ifdef LIGHTLOOP_SINGLE_PASS
3133
#include "Assets/ScriptableRenderLoop/HDRenderLoop/Lighting/SinglePass/SinglePassLoop.hlsl"
34+
#elif LIGHTLOOP_TILED_PASS
35+
#include "Assets/ScriptableRenderLoop/HDRenderLoop/Lighting/TilePass/TilePassLoop.hlsl"
3236
#endif
3337

3438

Assets/ScriptableRenderLoop/HDRenderLoop/Lighting/Resources/Deferred.shader

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,13 @@ Shader "Hidden/HDRenderLoop/Deferred"
1616
#pragma fragment FragDeferred
1717

1818
// Chose supported lighting architecture in case of deferred rendering
19-
#pragma multi_compile LIGHTLOOP_SINGLE_PASS
19+
#pragma multi_compile LIGHTLOOP_SINGLE_PASS LIGHTLOOP_TILE_PASS
2020
//#pragma multi_compile SHADOWFILTERING_FIXED_SIZE_PCF
2121

22-
// TODO: This must be on lightloop side and include here....
23-
#pragma multi_compile USE_FPTL_LIGHTLIST USE_CLUSTERED_LIGHTLIST
24-
#pragma multi_compile __ ENABLE_DEBUG
22+
// TODO: Workflow problem here, I would like to only generate variant for the LIGHTLOOP_TILE_PASS case, not the LIGHTLOOP_SINGLE_PASS case. This must be on lightloop side and include here.... (Can we codition
23+
#pragma multi_compile LIGHTLOOP_TILE_DIRECT LIGHTLOOP_TILE_INDIRECT LIGHTLOOP_TILE_ALL
24+
#pragma multi_compile USE_FPTL_LIGHTLIST USE_CLUSTERED_LIGHTLIST
25+
#pragma multi_compile _ ENABLE_DEBUG
2526

2627
//-------------------------------------------------------------------------------------
2728
// Include
@@ -75,7 +76,7 @@ Shader "Hidden/HDRenderLoop/Deferred"
7576
Coordinate coord = GetCoordinate(unPositionSS.xy, _ScreenSize.zw);
7677

7778
// No need to manage inverse depth, this is handled by the projection matrix
78-
float depth = LOAD_TEXTURE2D(_CameraDepthTexture, uint3(coord.unPositionSS, 0)).x;
79+
float depth = LOAD_TEXTURE2D(_CameraDepthTexture, coord.unPositionSS).x;
7980
float3 positionWS = UnprojectToWorld(depth, coord.positionSS, _InvViewProjMatrix);
8081
float3 V = GetWorldSpaceNormalizeViewDir(positionWS);
8182

@@ -88,7 +89,7 @@ Shader "Hidden/HDRenderLoop/Deferred"
8889

8990
float3 diffuseLighting;
9091
float3 specularLighting;
91-
LightLoop(V, positionWS, preLightData, bsdfData, bakeDiffuseLighting, diffuseLighting, specularLighting);
92+
LightLoop(V, positionWS, coord, preLightData, bsdfData, bakeDiffuseLighting, diffuseLighting, specularLighting);
9293

9394
return float4(diffuseLighting + specularLighting, 1.0);
9495
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ public void Rebuild()
6161
s_PunctualShadowList = new ComputeBuffer(HDRenderLoop.k_MaxShadowOnScreen, System.Runtime.InteropServices.Marshal.SizeOf(typeof(PunctualShadowData)));
6262

6363
m_DeferredMaterial = Utilities.CreateEngineMaterial("Hidden/HDRenderLoop/Deferred");
64+
m_DeferredMaterial.EnableKeyword("LIGHTLOOP_SINGLE_PASS");
6465
}
6566

6667
public void OnDisable()

Assets/ScriptableRenderLoop/HDRenderLoop/Lighting/SinglePass/SinglePassLoop.hlsl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
// ----------------------------------------------------------------------------
44

55
// 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)
6-
void LightLoop( float3 V, float3 positionWS, PreLightData prelightData, BSDFData bsdfData, float3 bakeDiffuseLighting,
6+
void LightLoop( float3 V, float3 positionWS, Coordinate coord, PreLightData prelightData, BSDFData bsdfData, float3 bakeDiffuseLighting,
77
out float3 diffuseLighting,
88
out float3 specularLighting)
99
{

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@ uniform float4x4 g_mProjection;
1414
StructuredBuffer<SFiniteLightBound> g_data : register( t0 );
1515

1616

17-
18-
#define FLT_EPSILON 1.192092896e-07F // smallest such that 1.0+FLT_EPSILON != 1.0
1917
#define NR_THREADS 64
2018

2119
// output buffer

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,12 @@
1919

2020
float FetchDepth(Texture2D depthTexture, uint2 pixCoord)
2121
{
22-
return 1 - LOAD_TEXTURE2D(depthTexture, uint3(pixCoord.xy, 0)).x;
22+
return 1.0 - LOAD_TEXTURE2D(depthTexture, pixCoord.xy).x;
2323
}
2424

2525
float FetchDepthMSAA(Texture2DMS<float> depthTexture, uint2 pixCoord, uint sampleIdx)
2626
{
27-
return 1 - LOAD_TEXTURE2D_MSAA(depthTexture, uint3(pixCoord.xy, 0), sampleIdx).x;
27+
return 1.0 - LOAD_TEXTURE2D_MSAA(depthTexture, pixCoord.xy, sampleIdx).x;
2828
}
2929

3030
#endif

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

Lines changed: 45 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ public class LightLoop
7878
{
7979
string GetKeyword()
8080
{
81-
return "LIGHTLOOP_SINGLE_PASS";
81+
return "LIGHTLOOP_TILE_PASS";
8282
}
8383

8484
public const int MaxNumLights = 1024;
@@ -121,8 +121,6 @@ string GetKeyword()
121121
private static ComputeBuffer s_GlobalLightListAtomic;
122122
// clustered light list specific buffers and data end
123123

124-
const int k_TileSize = 16;
125-
126124
SFiniteLightBound[] m_boundData;
127125
SFiniteLightData[] m_lightData;
128126
int m_lightCount;
@@ -149,6 +147,18 @@ bool usingFptl
149147
Material m_DeferredMaterial;
150148
Material m_DeferredReflectionMaterial;
151149

150+
const int k_TileSize = 16;
151+
152+
int GetNumTileX(Camera camera)
153+
{
154+
return (camera.pixelWidth + (k_TileSize - 1)) / k_TileSize;
155+
}
156+
157+
int GetNumTileY(Camera camera)
158+
{
159+
return (camera.pixelWidth + (k_TileSize - 1)) / k_TileSize;
160+
}
161+
152162
// Local function
153163
void ClearComputeBuffers()
154164
{
@@ -239,8 +249,13 @@ public void Rebuild()
239249
s_EnvLightList = new ComputeBuffer(HDRenderLoop.k_MaxEnvLightsOnSCreen, System.Runtime.InteropServices.Marshal.SizeOf(typeof(EnvLightData)));
240250
s_PunctualShadowList = new ComputeBuffer(HDRenderLoop.k_MaxShadowOnScreen, System.Runtime.InteropServices.Marshal.SizeOf(typeof(PunctualShadowData)));
241251

242-
// m_DeferredMaterial = Utilities.CreateEngineMaterial("Hidden/HDRenderLoop/TileDeferred");
243-
// m_DeferredReflectionMaterial = Utilities.CreateEngineMaterial("Hidden/HDRenderLoop/TileDeferredReflection");
252+
m_DeferredMaterial = Utilities.CreateEngineMaterial("Hidden/HDRenderLoop/Deferred");
253+
m_DeferredMaterial.EnableKeyword("LIGHTLOOP_TILE_PASS");
254+
m_DeferredMaterial.EnableKeyword("LIGHTLOOP_TILE_DIRECT");
255+
256+
m_DeferredReflectionMaterial = Utilities.CreateEngineMaterial("Hidden/HDRenderLoop/Deferred");
257+
m_DeferredReflectionMaterial.EnableKeyword("LIGHTLOOP_TILE_PASS");
258+
m_DeferredReflectionMaterial.EnableKeyword("LIGHTLOOP_TILE_INDIRECT");
244259
}
245260

246261
public void OnDisable()
@@ -269,10 +284,9 @@ public void OnDisable()
269284
s_EnvLightList = null;
270285
s_PunctualShadowList.Release();
271286
s_PunctualShadowList = null;
272-
/*
287+
273288
Utilities.Destroy(m_DeferredMaterial);
274289
Utilities.Destroy(m_DeferredReflectionMaterial);
275-
*/
276290
}
277291

278292
public bool NeedResize()
@@ -339,43 +353,6 @@ public void AllocResolutionDependentBuffers(int width, int height)
339353
}
340354
}
341355

342-
// TEMP: These functions should be implemented C++ side, for now do it in C#
343-
private static void SetMatrixCS(CommandBuffer cmd, ComputeShader shadercs, string name, Matrix4x4 mat)
344-
{
345-
var data = new float[16];
346-
347-
for (int c = 0; c < 4; c++)
348-
for (int r = 0; r < 4; r++)
349-
data[4 * c + r] = mat[r, c];
350-
351-
cmd.SetComputeFloatParams(shadercs, name, data);
352-
}
353-
354-
private static void SetMatrixArrayCS(CommandBuffer cmd, ComputeShader shadercs, string name, Matrix4x4[] matArray)
355-
{
356-
int numMatrices = matArray.Length;
357-
var data = new float[numMatrices * 16];
358-
359-
for (int n = 0; n < numMatrices; n++)
360-
for (int c = 0; c < 4; c++)
361-
for (int r = 0; r < 4; r++)
362-
data[16 * n + 4 * c + r] = matArray[n][r, c];
363-
364-
cmd.SetComputeFloatParams(shadercs, name, data);
365-
}
366-
367-
private static void SetVectorArrayCS(CommandBuffer cmd, ComputeShader shadercs, string name, Vector4[] vecArray)
368-
{
369-
int numVectors = vecArray.Length;
370-
var data = new float[numVectors * 4];
371-
372-
for (int n = 0; n < numVectors; n++)
373-
for (int i = 0; i < 4; i++)
374-
data[4 * n + i] = vecArray[n][i];
375-
376-
cmd.SetComputeFloatParams(shadercs, name, data);
377-
}
378-
379356
static Matrix4x4 GetFlipMatrix()
380357
{
381358
Matrix4x4 flip = Matrix4x4.identity;
@@ -617,8 +594,8 @@ void VoxelLightListGeneration(CommandBuffer cmd, Camera camera, Matrix4x4 projsc
617594
cmd.DispatchCompute(buildPerVoxelLightListShader, s_ClearVoxelAtomicKernel, 1, 1, 1);
618595

619596
cmd.SetComputeIntParam(buildPerVoxelLightListShader, "g_iNrVisibLights", m_lightCount);
620-
SetMatrixCS(cmd, buildPerVoxelLightListShader, "g_mScrProjection", projscr);
621-
SetMatrixCS(cmd, buildPerVoxelLightListShader, "g_mInvScrProjection", invProjscr);
597+
Utilities.SetMatrixCS(cmd, buildPerVoxelLightListShader, "g_mScrProjection", projscr);
598+
Utilities.SetMatrixCS(cmd, buildPerVoxelLightListShader, "g_mInvScrProjection", invProjscr);
622599

623600
cmd.SetComputeIntParam(buildPerVoxelLightListShader, "g_iLog2NumClusters", k_Log2NumClusters);
624601

@@ -649,17 +626,17 @@ void VoxelLightListGeneration(CommandBuffer cmd, Camera camera, Matrix4x4 projsc
649626
cmd.SetComputeBufferParam(buildPerVoxelLightListShader, s_GenListPerVoxelKernel, "g_logBaseBuffer", s_PerTileLogBaseTweak);
650627
}
651628

652-
var numTilesX = (camera.pixelWidth + 15) / 16;
653-
var numTilesY = (camera.pixelHeight + 15) / 16;
629+
var numTilesX = GetNumTileX(camera);
630+
var numTilesY = GetNumTileY(camera);
654631
cmd.DispatchCompute(buildPerVoxelLightListShader, s_GenListPerVoxelKernel, numTilesX, numTilesY, 1);
655632
}
656633

657634
public void BuildGPULightLists(Camera camera, RenderLoop loop, HDRenderLoop.LightList lightList, int cameraDepthBuffer)
658635
{
659636
var w = camera.pixelWidth;
660637
var h = camera.pixelHeight;
661-
var numTilesX = (w + 15) / 16;
662-
var numTilesY = (h + 15) / 16;
638+
var numTilesX = GetNumTileX(camera);
639+
var numTilesY = GetNumTileY(camera);
663640
var numBigTilesX = (w + 63) / 64;
664641
var numBigTilesY = (h + 63) / 64;
665642

@@ -685,8 +662,8 @@ public void BuildGPULightLists(Camera camera, RenderLoop loop, HDRenderLoop.Ligh
685662
var invProjh = projh.inverse;
686663

687664
cmd.SetComputeIntParam(buildScreenAABBShader, "g_iNrVisibLights", m_lightCount);
688-
SetMatrixCS(cmd, buildScreenAABBShader, "g_mProjection", projh);
689-
SetMatrixCS(cmd, buildScreenAABBShader, "g_mInvProjection", invProjh);
665+
Utilities.SetMatrixCS(cmd, buildScreenAABBShader, "g_mProjection", projh);
666+
Utilities.SetMatrixCS(cmd, buildScreenAABBShader, "g_mInvProjection", invProjh);
690667
cmd.SetComputeBufferParam(buildScreenAABBShader, s_GenAABBKernel, "g_vBoundsBuffer", s_AABBBoundsBuffer);
691668
cmd.DispatchCompute(buildScreenAABBShader, s_GenAABBKernel, (m_lightCount + 7) / 8, 1, 1);
692669
}
@@ -696,20 +673,20 @@ public void BuildGPULightLists(Camera camera, RenderLoop loop, HDRenderLoop.Ligh
696673
{
697674
cmd.SetComputeIntParams(buildPerBigTileLightListShader, "g_viDimensions", new int[2] { w, h });
698675
cmd.SetComputeIntParam(buildPerBigTileLightListShader, "g_iNrVisibLights", m_lightCount);
699-
SetMatrixCS(cmd, buildPerBigTileLightListShader, "g_mScrProjection", projscr);
700-
SetMatrixCS(cmd, buildPerBigTileLightListShader, "g_mInvScrProjection", invProjscr);
676+
Utilities.SetMatrixCS(cmd, buildPerBigTileLightListShader, "g_mScrProjection", projscr);
677+
Utilities.SetMatrixCS(cmd, buildPerBigTileLightListShader, "g_mInvScrProjection", invProjscr);
701678
cmd.SetComputeFloatParam(buildPerBigTileLightListShader, "g_fNearPlane", camera.nearClipPlane);
702679
cmd.SetComputeFloatParam(buildPerBigTileLightListShader, "g_fFarPlane", camera.farClipPlane);
703680
cmd.SetComputeBufferParam(buildPerBigTileLightListShader, s_GenListPerBigTileKernel, "g_vLightList", s_BigTileLightList);
704681
cmd.DispatchCompute(buildPerBigTileLightListShader, s_GenListPerBigTileKernel, numBigTilesX, numBigTilesY, 1);
705682
}
706-
/*
683+
707684
if (usingFptl) // optimized for opaques only
708685
{
709686
cmd.SetComputeIntParams(buildPerTileLightListShader, "g_viDimensions", new int[2] { w, h });
710687
cmd.SetComputeIntParam(buildPerTileLightListShader, "g_iNrVisibLights", m_lightCount);
711-
SetMatrixCS(cmd, buildPerTileLightListShader, "g_mScrProjection", projscr);
712-
SetMatrixCS(cmd, buildPerTileLightListShader, "g_mInvScrProjection", invProjscr);
688+
Utilities.SetMatrixCS(cmd, buildPerTileLightListShader, "g_mScrProjection", projscr);
689+
Utilities.SetMatrixCS(cmd, buildPerTileLightListShader, "g_mInvScrProjection", invProjscr);
713690
cmd.SetComputeTextureParam(buildPerTileLightListShader, s_GenListPerTileKernel, "g_depth_tex", new RenderTargetIdentifier(cameraDepthBuffer));
714691
cmd.SetComputeBufferParam(buildPerTileLightListShader, s_GenListPerTileKernel, "g_vLightList", s_LightList);
715692
if (enableBigTilePrepass) cmd.SetComputeBufferParam(buildPerTileLightListShader, s_GenListPerTileKernel, "g_vBigTileLightList", s_BigTileLightList);
@@ -720,7 +697,7 @@ public void BuildGPULightLists(Camera camera, RenderLoop loop, HDRenderLoop.Ligh
720697
{
721698
VoxelLightListGeneration(cmd, camera, projscr, invProjscr, cameraDepthBuffer);
722699
}
723-
*/
700+
724701
loop.ExecuteCommandBuffer(cmd);
725702
cmd.Dispose();
726703
}
@@ -741,10 +718,14 @@ public void PushGlobalParams(Camera camera, RenderLoop loop, HDRenderLoop.LightL
741718
Shader.SetGlobalBuffer("_AreaLightList", s_AreaLightList);
742719
Shader.SetGlobalBuffer("_PunctualShadowList", s_PunctualShadowList);
743720
Shader.SetGlobalBuffer("_EnvLightList", s_EnvLightList);
744-
721+
745722
Shader.SetGlobalVectorArray("_DirShadowSplitSpheres", lightList.directionalShadowSplitSphereSqr);
746723

747-
/*
724+
var cmd = new CommandBuffer { name = "Push Global Parameters" };
725+
726+
cmd.SetGlobalFloat("_NumTileX", (float)GetNumTileX(camera));
727+
cmd.SetGlobalFloat("_NumTileY", (float)GetNumTileY(camera));
728+
748729
if (enableBigTilePrepass)
749730
cmd.SetGlobalBuffer("g_vBigTileLightList", s_BigTileLightList);
750731

@@ -766,17 +747,12 @@ public void PushGlobalParams(Camera camera, RenderLoop loop, HDRenderLoop.LightL
766747
}
767748
}
768749

769-
cmd.SetGlobalFloat("g_nNumDirLights", numDirLights);
770-
cmd.SetGlobalBuffer("g_dirLightData", s_DirLightList);
771-
772750
loop.ExecuteCommandBuffer(cmd);
773751
cmd.Dispose();
774-
*/
775752
}
776753

777754
public void RenderDeferredLighting(Camera camera, RenderLoop renderLoop, RenderTargetIdentifier colorBuffer)
778755
{
779-
/*
780756
var bUseClusteredForDeferred = !usingFptl; // doesn't work on reflections yet but will soon
781757
var cmd = new CommandBuffer();
782758

@@ -887,12 +863,12 @@ public void RenderDeferredLighting(Camera camera, RenderLoop renderLoop, RenderT
887863
}
888864
else
889865
{*/
890-
// cmd.Blit(0, colorBuffer, m_DeferredMaterial, 0);
891-
// cmd.Blit(0, colorBuffer, m_DeferredReflectionMaterial, 0);
866+
cmd.Blit(0, colorBuffer, m_DeferredMaterial, 0);
867+
cmd.Blit(0, colorBuffer, m_DeferredReflectionMaterial, 0);
892868
//}
893869

894-
// renderLoop.ExecuteCommandBuffer(cmd);
895-
// cmd.Dispose();
870+
renderLoop.ExecuteCommandBuffer(cmd);
871+
cmd.Dispose();
896872
}
897873
}
898874
}

0 commit comments

Comments
 (0)