Skip to content

Commit 51890cb

Browse files
HDRenderLoop: Fix depthVS issue + introduce HDCamera
1 parent fdace4c commit 51890cb

File tree

12 files changed

+165
-155
lines changed

12 files changed

+165
-155
lines changed

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,6 @@ Shader "Hidden/HDRenderLoop/DebugViewMaterialGBuffer"
2323
#include "Assets/ScriptableRenderLoop/HDRenderLoop/ShaderVariables.hlsl"
2424
#include "Assets/ScriptableRenderLoop/HDRenderLoop/Debug/DebugViewMaterial.cs.hlsl"
2525
#include "Assets/ScriptableRenderLoop/HDRenderLoop/Material/Material.hlsl"
26-
27-
float4x4 _InvViewProjMatrix;
2826

2927
DECLARE_GBUFFER_TEXTURE(_GBufferTexture);
3028

@@ -57,7 +55,7 @@ Shader "Hidden/HDRenderLoop/DebugViewMaterialGBuffer"
5755
// input.positionCS is SV_Position
5856
PositionInputs posInput = GetPositionInput(input.positionCS.xy, _ScreenSize.zw);
5957
float depth = LOAD_TEXTURE2D(_CameraDepthTexture, posInput.unPositionSS).x;
60-
UpdatePositionInput(depth, _InvViewProjMatrix, GetWorldToViewMatrix(), posInput);
58+
UpdatePositionInput(depth, _InvViewProjMatrix, _ViewProjMatrix, posInput);
6159

6260
FETCH_GBUFFER(gbuffer, _GBufferTexture, posInput.unPositionSS);
6361
BSDFData bsdfData;

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

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,6 @@ Shader "Hidden/HDRenderLoop/DebugViewTiles"
4141
TEXTURE2D(_CameraDepthTexture);
4242
SAMPLER2D(sampler_CameraDepthTexture);
4343

44-
float4x4 _InvViewProjMatrix;
45-
4644
uint _ViewTilesFlags;
4745
float2 _MousePixelCoord;
4846

@@ -99,7 +97,7 @@ Shader "Hidden/HDRenderLoop/DebugViewTiles"
9997
// positionCS is SV_Position
10098
PositionInputs posInput = GetPositionInput(positionCS.xy, _ScreenSize.zw);
10199
float depth = LOAD_TEXTURE2D(_CameraDepthTexture, posInput.unPositionSS).x;
102-
UpdatePositionInput(depth, _InvViewProjMatrix, GetWorldToViewMatrix(), posInput);
100+
UpdatePositionInput(depth, _InvViewProjMatrix, _ViewProjMatrix, posInput);
103101

104102
int2 pixelCoord = posInput.unPositionSS.xy;
105103
int2 tileCoord = pixelCoord / TILE_SIZE;
@@ -141,7 +139,7 @@ Shader "Hidden/HDRenderLoop/DebugViewTiles"
141139
{
142140
PositionInputs mousePosInput = GetPositionInput(_MousePixelCoord, _ScreenSize.zw);
143141
float depthMouse = LOAD_TEXTURE2D(_CameraDepthTexture, mousePosInput.unPositionSS).x;
144-
UpdatePositionInput(depthMouse, _InvViewProjMatrix, GetWorldToViewMatrix(), mousePosInput);
142+
UpdatePositionInput(depthMouse, _InvViewProjMatrix, _ViewProjMatrix, mousePosInput);
145143

146144
int category = (LIGHTCATEGORY_COUNT - 1) - tileCoord.y;
147145
int start;

Assets/ScriptableRenderLoop/HDRenderLoop/HDRenderLoop.cs

Lines changed: 27 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,14 @@ public BaseLightLoop lightLoop
171171
// TODO TO CHECK: SebL I move allocation from Build() to here, but there was a comment "// Our object can be garbage collected, so need to be allocate here", it is still true ?
172172
Lit.RenderLoop m_LitRenderLoop = new Lit.RenderLoop();
173173

174+
public struct HDCamera
175+
{
176+
public Camera camera;
177+
public Vector4 screenSize;
178+
public Matrix4x4 viewProjectionMatrix;
179+
public Matrix4x4 invViewProjectionMatrix;
180+
}
181+
174182
public override void Build()
175183
{
176184
#if UNITY_EDITOR
@@ -374,7 +382,7 @@ void RenderForwardOnlyDepthPrepass(CullResults cull, Camera camera, RenderLoop r
374382
}
375383
}
376384

377-
void RenderDebugViewMaterial(CullResults cull, Camera camera, RenderLoop renderLoop)
385+
void RenderDebugViewMaterial(CullResults cull, HDCamera hdCamera, RenderLoop renderLoop)
378386
{
379387
using (new Utilities.ProfilingSample("DebugView Material Mode Pass", renderLoop))
380388
// Render Opaque forward
@@ -383,17 +391,14 @@ void RenderDebugViewMaterial(CullResults cull, Camera camera, RenderLoop renderL
383391

384392
Shader.SetGlobalInt("_DebugViewMaterial", (int)debugParameters.debugViewMaterial);
385393

386-
RenderOpaqueRenderList(cull, camera, renderLoop, "DebugViewMaterial");
394+
RenderOpaqueRenderList(cull, hdCamera.camera, renderLoop, "DebugViewMaterial");
387395
}
388396

389397
// Render GBuffer opaque
390398
if (!debugParameters.useForwardRenderingOnly)
391399
{
392-
var invViewProj = Utilities.GetViewProjectionMatrix(camera).inverse;
393-
var screenSize = Utilities.ComputeScreenSize(camera);
394-
m_DebugViewMaterialGBuffer.SetVector("_ScreenSize", screenSize);
400+
Utilities.SetupMaterialHDCamera(hdCamera, m_DebugViewMaterialGBuffer);
395401
m_DebugViewMaterialGBuffer.SetFloat("_DebugViewMaterial", (float)debugParameters.debugViewMaterial);
396-
m_DebugViewMaterialGBuffer.SetMatrix("_InvViewProjMatrix", invViewProj);
397402

398403
// m_gbufferManager.BindBuffers(m_DebugViewMaterialGBuffer);
399404
// TODO: Bind depth textures
@@ -405,7 +410,7 @@ void RenderDebugViewMaterial(CullResults cull, Camera camera, RenderLoop renderL
405410

406411
// Render forward transparent
407412
{
408-
RenderTransparentRenderList(cull, camera, renderLoop, "DebugViewMaterial");
413+
RenderTransparentRenderList(cull, hdCamera.camera, renderLoop, "DebugViewMaterial");
409414
}
410415

411416
// Last blit
@@ -417,7 +422,7 @@ void RenderDebugViewMaterial(CullResults cull, Camera camera, RenderLoop renderL
417422
}
418423
}
419424

420-
void RenderDeferredLighting(Camera camera, RenderLoop renderLoop)
425+
void RenderDeferredLighting(HDCamera hdCamera, RenderLoop renderLoop)
421426
{
422427
if (debugParameters.useForwardRenderingOnly)
423428
{
@@ -426,12 +431,12 @@ void RenderDeferredLighting(Camera camera, RenderLoop renderLoop)
426431

427432
// Bind material data
428433
m_LitRenderLoop.Bind();
429-
m_lightLoop.RenderDeferredLighting(camera, renderLoop, m_CameraColorBuffer);
434+
m_lightLoop.RenderDeferredLighting(hdCamera, renderLoop, m_CameraColorBuffer);
430435
}
431436

432-
void RenderSky(Camera camera, RenderLoop renderLoop)
437+
void RenderSky(HDCamera hdCamera, RenderLoop renderLoop)
433438
{
434-
m_SkyRenderer.RenderSky(camera, m_SkyParameters, m_CameraColorBufferRT, m_CameraDepthBufferRT, renderLoop);
439+
m_SkyRenderer.RenderSky(hdCamera, m_SkyParameters, m_CameraColorBufferRT, m_CameraDepthBufferRT, renderLoop);
435440
}
436441

437442
void RenderForward(CullResults cullResults, Camera camera, RenderLoop renderLoop, bool renderOpaque)
@@ -606,7 +611,7 @@ void Resize(Camera camera)
606611
}
607612
}
608613

609-
public void PushGlobalParams(Camera camera, RenderLoop renderLoop)
614+
public void PushGlobalParams(HDCamera hdCamera, RenderLoop renderLoop)
610615
{
611616
if (m_SkyRenderer.IsSkyValid(m_SkyParameters))
612617
{
@@ -618,18 +623,16 @@ public void PushGlobalParams(Camera camera, RenderLoop renderLoop)
618623
Shader.SetGlobalInt("_EnvLightSkyEnabled", 0);
619624
}
620625

621-
var invViewProj = Utilities.GetViewProjectionMatrix(camera).inverse;
622-
var screenSize = Utilities.ComputeScreenSize(camera);
623-
624626
var cmd = new CommandBuffer { name = "Push Global Parameters" };
625627

626-
cmd.SetGlobalVector("_ScreenSize", screenSize);
627-
cmd.SetGlobalMatrix("_InvViewProjMatrix", invViewProj);
628+
cmd.SetGlobalVector("_ScreenSize", hdCamera.screenSize);
629+
cmd.SetGlobalMatrix("_ViewProjMatrix", hdCamera.viewProjectionMatrix);
630+
cmd.SetGlobalMatrix("_InvViewProjMatrix", hdCamera.invViewProjectionMatrix);
628631

629632
renderLoop.ExecuteCommandBuffer(cmd);
630633
cmd.Dispose();
631634

632-
m_lightLoop.PushGlobalParams(camera, renderLoop);
635+
m_lightLoop.PushGlobalParams(hdCamera.camera, renderLoop);
633636
}
634637

635638
public override void Render(Camera[] cameras, RenderLoop renderLoop)
@@ -662,6 +665,8 @@ public override void Render(Camera[] cameras, RenderLoop renderLoop)
662665

663666
renderLoop.SetupCameraProperties(camera);
664667

668+
HDCamera hdCamera = Utilities.GetHDCamera(camera);
669+
665670
InitAndClearBuffer(camera, renderLoop);
666671

667672
RenderDepthPrepass(cullResults, camera, renderLoop);
@@ -674,7 +679,7 @@ public override void Render(Camera[] cameras, RenderLoop renderLoop)
674679

675680
if (debugParameters.debugViewMaterial != 0)
676681
{
677-
RenderDebugViewMaterial(cullResults, camera, renderLoop);
682+
RenderDebugViewMaterial(cullResults, hdCamera, renderLoop);
678683
}
679684
else
680685
{
@@ -691,14 +696,14 @@ public override void Render(Camera[] cameras, RenderLoop renderLoop)
691696
m_lightLoop.PrepareLightsForGPU(cullResults, camera, ref shadows);
692697
m_lightLoop.BuildGPULightLists(camera, renderLoop, m_CameraDepthBufferRT);
693698

694-
PushGlobalParams(camera, renderLoop);
699+
PushGlobalParams(hdCamera, renderLoop);
695700
}
696-
RenderDeferredLighting(camera, renderLoop);
701+
RenderDeferredLighting(hdCamera, renderLoop);
697702

698703
RenderForward(cullResults, camera, renderLoop, true);
699704
RenderForwardOnly(cullResults, camera, renderLoop);
700705

701-
RenderSky(camera, renderLoop);
706+
RenderSky(hdCamera, renderLoop);
702707

703708
RenderForward(cullResults, camera, renderLoop, false);
704709

Assets/ScriptableRenderLoop/HDRenderLoop/Lighting/LightLoop.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public virtual void BuildGPULightLists(Camera camera, RenderLoop loop, RenderTar
2727

2828
public virtual void PushGlobalParams(Camera camera, RenderLoop loop) {}
2929

30-
public virtual void RenderDeferredLighting(Camera camera, RenderLoop renderLoop, RenderTargetIdentifier cameraColorBufferRT) {}
30+
public virtual void RenderDeferredLighting(HDRenderLoop.HDCamera hdCamera, RenderLoop renderLoop, RenderTargetIdentifier cameraColorBufferRT) {}
3131

3232
public virtual void RenderForward(Camera camera, RenderLoop renderLoop, bool renderOpaque) {}
3333
}

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,6 @@ Shader "Hidden/HDRenderLoop/Deferred"
5454
TEXTURE2D(_CameraDepthTexture);
5555
SAMPLER2D(sampler_CameraDepthTexture);
5656

57-
float4x4 _InvViewProjMatrix;
58-
5957
struct Attributes
6058
{
6159
float3 positionOS : POSITION;
@@ -82,7 +80,7 @@ Shader "Hidden/HDRenderLoop/Deferred"
8280
// input.positionCS is SV_Position
8381
PositionInputs posInput = GetPositionInput(input.positionCS.xy, _ScreenSize.zw);
8482
float depth = LOAD_TEXTURE2D(_CameraDepthTexture, posInput.unPositionSS).x;
85-
UpdatePositionInput(depth, _InvViewProjMatrix, GetWorldToViewMatrix(), posInput);
83+
UpdatePositionInput(depth, _InvViewProjMatrix, _ViewProjMatrix, posInput);
8684
float3 V = GetWorldSpaceNormalizeViewDir(posInput.positionWS);
8785

8886
FETCH_GBUFFER(gbuffer, _GBufferTexture, posInput.unPositionSS);

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

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1201,52 +1201,44 @@ private void OnSceneGUI(UnityEditor.SceneView sceneview)
12011201
}
12021202
#endif
12031203

1204-
public override void RenderDeferredLighting(Camera camera, RenderLoop renderLoop, RenderTargetIdentifier cameraColorBufferRT)
1204+
public override void RenderDeferredLighting(HDRenderLoop.HDCamera hdCamera, RenderLoop renderLoop, RenderTargetIdentifier cameraColorBufferRT)
12051205
{
12061206
var bUseClusteredForDeferred = !usingFptl;
12071207

1208-
var invViewProj = Utilities.GetViewProjectionMatrix(camera).inverse;
1209-
var screenSize = Utilities.ComputeScreenSize(camera);
1210-
12111208
Vector2 mousePixelCoord = Input.mousePosition;
12121209
#if UNITY_EDITOR
12131210
if (!UnityEditor.EditorApplication.isPlayingOrWillChangePlaymode)
12141211
{
12151212
mousePixelCoord = m_mousePosition;
1216-
mousePixelCoord.y = (screenSize.y - 1.0f) - mousePixelCoord.y;
1213+
mousePixelCoord.y = (hdCamera.screenSize.y - 1.0f) - mousePixelCoord.y;
12171214
}
12181215
#endif
12191216

1220-
m_DeferredDirectMaterial.SetMatrix("_InvViewProjMatrix", invViewProj);
1221-
m_DeferredDirectMaterial.SetVector("_ScreenSize", screenSize);
1217+
Utilities.SetupMaterialHDCamera(hdCamera, m_DeferredDirectMaterial);
12221218
m_DeferredDirectMaterial.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.One);
12231219
m_DeferredDirectMaterial.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.Zero);
12241220
m_DeferredDirectMaterial.EnableKeyword(bUseClusteredForDeferred ? "USE_CLUSTERED_LIGHTLIST" : "USE_FPTL_LIGHTLIST");
12251221
m_DeferredDirectMaterial.DisableKeyword(!bUseClusteredForDeferred ? "USE_CLUSTERED_LIGHTLIST" : "USE_FPTL_LIGHTLIST");
12261222

1227-
m_DeferredIndirectMaterial.SetMatrix("_InvViewProjMatrix", invViewProj);
1228-
m_DeferredIndirectMaterial.SetVector("_ScreenSize", screenSize);
1223+
Utilities.SetupMaterialHDCamera(hdCamera, m_DeferredIndirectMaterial);
12291224
m_DeferredIndirectMaterial.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.One);
12301225
m_DeferredIndirectMaterial.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.One); // Additive
12311226
m_DeferredIndirectMaterial.EnableKeyword(bUseClusteredForDeferred ? "USE_CLUSTERED_LIGHTLIST" : "USE_FPTL_LIGHTLIST");
12321227
m_DeferredIndirectMaterial.DisableKeyword(!bUseClusteredForDeferred ? "USE_CLUSTERED_LIGHTLIST" : "USE_FPTL_LIGHTLIST");
12331228

1234-
m_DeferredAllMaterial.SetMatrix("_InvViewProjMatrix", invViewProj);
1235-
m_DeferredAllMaterial.SetVector("_ScreenSize", screenSize);
1229+
Utilities.SetupMaterialHDCamera(hdCamera, m_DeferredAllMaterial);
12361230
m_DeferredAllMaterial.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.One);
12371231
m_DeferredAllMaterial.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.Zero);
12381232
m_DeferredAllMaterial.EnableKeyword(bUseClusteredForDeferred ? "USE_CLUSTERED_LIGHTLIST" : "USE_FPTL_LIGHTLIST");
12391233
m_DeferredAllMaterial.DisableKeyword(!bUseClusteredForDeferred ? "USE_CLUSTERED_LIGHTLIST" : "USE_FPTL_LIGHTLIST");
12401234

1241-
m_DebugViewTilesMaterial.SetMatrix("_InvViewProjMatrix", invViewProj);
1242-
m_DebugViewTilesMaterial.SetVector("_ScreenSize", screenSize);
1235+
Utilities.SetupMaterialHDCamera(hdCamera, m_DebugViewTilesMaterial);
12431236
m_DebugViewTilesMaterial.SetInt("_ViewTilesFlags", debugViewTilesFlags);
12441237
m_DebugViewTilesMaterial.SetVector("_MousePixelCoord", mousePixelCoord);
12451238
m_DebugViewTilesMaterial.EnableKeyword(bUseClusteredForDeferred ? "USE_CLUSTERED_LIGHTLIST" : "USE_FPTL_LIGHTLIST");
12461239
m_DebugViewTilesMaterial.DisableKeyword(!bUseClusteredForDeferred ? "USE_CLUSTERED_LIGHTLIST" : "USE_FPTL_LIGHTLIST");
12471240

1248-
m_SingleDeferredMaterial.SetMatrix("_InvViewProjMatrix", invViewProj);
1249-
m_SingleDeferredMaterial.SetVector("_ScreenSize", screenSize);
1241+
Utilities.SetupMaterialHDCamera(hdCamera, m_SingleDeferredMaterial);
12501242
m_SingleDeferredMaterial.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.One);
12511243
m_SingleDeferredMaterial.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.Zero);
12521244

0 commit comments

Comments
 (0)