Skip to content

Commit 344ec13

Browse files
Merge pull request #44 from Unity-Technologies/pixel-depth-offset
Prepare for depth pixel offset
2 parents 8b52b7b + 2c759ca commit 344ec13

33 files changed

+352
-234
lines changed

Assets/ScriptableRenderLoop/HDRenderLoop/Debug/DebugViewMaterial.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,14 @@ public enum DebugViewVarying
1818
VertexNormalWS,
1919
VertexColor,
2020
VertexColorAlpha,
21+
// caution if you add something here, it must start below
2122
};
2223

2324
// Number must be contiguous
2425
[GenerateHLSL]
2526
public enum DebugViewGbuffer
2627
{
27-
Depth = DebugViewVarying.VertexColor + 1,
28+
Depth = DebugViewVarying.VertexColorAlpha + 1,
2829
BakeDiffuseLighting,
2930
}
3031
}

Assets/ScriptableRenderLoop/HDRenderLoop/Debug/DebugViewMaterial.cs.hlsl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@
2020
//
2121
// UnityEngine.Experimental.ScriptableRenderLoop.Attributes.DebugViewGbuffer: static fields
2222
//
23-
#define DEBUGVIEWGBUFFER_DEPTH (9)
24-
#define DEBUGVIEWGBUFFER_BAKE_DIFFUSE_LIGHTING (10)
23+
#define DEBUGVIEWGBUFFER_DEPTH (10)
24+
#define DEBUGVIEWGBUFFER_BAKE_DIFFUSE_LIGHTING (11)
2525

2626

2727
#endif

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

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ Shader "Hidden/HDRenderLoop/DebugViewMaterialGBuffer"
2424
#include "Assets/ScriptableRenderLoop/HDRenderLoop/Debug/DebugViewMaterial.cs.hlsl"
2525
#include "Assets/ScriptableRenderLoop/HDRenderLoop/Material/Material.hlsl"
2626

27+
float4x4 _InvViewProjMatrix;
2728

2829
DECLARE_GBUFFER_TEXTURE(_GBufferTexture);
2930

@@ -53,12 +54,12 @@ Shader "Hidden/HDRenderLoop/DebugViewMaterialGBuffer"
5354

5455
float4 Frag(Varyings input) : SV_Target
5556
{
56-
float4 unPositionSS = input.positionCS; // as input we have the vpos
57-
Coordinate coord = GetCoordinate(unPositionSS.xy, _ScreenSize.zw);
57+
// input.positionCS is SV_Position
58+
PositionInputs posInput = GetPositionInput(input.positionCS.xy, _ScreenSize.zw);
59+
float depth = LOAD_TEXTURE2D(_CameraDepthTexture, posInput.unPositionSS).x;
60+
UpdatePositionInput(depth, _InvViewProjMatrix, GetWorldToViewMatrix(), posInput);
5861

59-
float depth = LOAD_TEXTURE2D(_CameraDepthTexture, coord.unPositionSS).x;
60-
61-
FETCH_GBUFFER(gbuffer, _GBufferTexture, coord.unPositionSS);
62+
FETCH_GBUFFER(gbuffer, _GBufferTexture, posInput.unPositionSS);
6263
BSDFData bsdfData;
6364
float3 bakeDiffuseLighting;
6465
DECODE_FROM_GBUFFER(gbuffer, bsdfData, bakeDiffuseLighting);
@@ -69,7 +70,7 @@ Shader "Hidden/HDRenderLoop/DebugViewMaterialGBuffer"
6970

7071
if (_DebugViewMaterial == DEBUGVIEWGBUFFER_DEPTH)
7172
{
72-
float linearDepth = frac(LinearEyeDepth(depth, _ZBufferParams) * 0.1);
73+
float linearDepth = frac(posInput.depthVS * 0.1);
7374
result = linearDepth.xxx;
7475
}
7576
else if (_DebugViewMaterial == DEBUGVIEWGBUFFER_BAKE_DIFFUSE_LIGHTING)

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

Lines changed: 21 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -96,21 +96,15 @@ Shader "Hidden/HDRenderLoop/DebugViewTiles"
9696

9797
float4 Frag(float4 positionCS : SV_POSITION) : SV_Target
9898
{
99-
Coordinate coord = GetCoordinate(positionCS.xy, _ScreenSize.zw);
100-
101-
int2 pixelCoord = coord.unPositionSS.xy;
99+
// positionCS is SV_Position
100+
PositionInputs posInput = GetPositionInput(positionCS.xy, _ScreenSize.zw);
101+
float depth = LOAD_TEXTURE2D(_CameraDepthTexture, posInput.unPositionSS).x;
102+
UpdatePositionInput(depth, _InvViewProjMatrix, GetWorldToViewMatrix(), posInput);
103+
104+
int2 pixelCoord = posInput.unPositionSS.xy;
102105
int2 tileCoord = pixelCoord / TILE_SIZE;
103106
int2 mouseTileCoord = _MousePixelCoord / TILE_SIZE;
104107
int2 offsetInTile = pixelCoord - tileCoord * TILE_SIZE;
105-
106-
#ifdef USE_CLUSTERED_LIGHTLIST
107-
// Perform same calculation than in deferred.shader
108-
float depth = LOAD_TEXTURE2D(_CameraDepthTexture, coord.unPositionSS).x;
109-
float3 positionWS = UnprojectToWorld(depth, coord.positionSS, _InvViewProjMatrix);
110-
float linearDepth = TransformWorldToView(positionWS).z; // View space linear depth
111-
#else
112-
float linearDepth = 0.0; // unused
113-
#endif
114108

115109
int n = 0;
116110
for (int category = 0; category < LIGHTCATEGORY_COUNT; category++)
@@ -120,37 +114,39 @@ Shader "Hidden/HDRenderLoop/DebugViewTiles"
120114
{
121115
uint start;
122116
uint count;
123-
GetCountAndStart(coord, category, linearDepth, start, count);
117+
GetCountAndStart(posInput, category, start, count);
124118
n += count;
125119
}
126120
}
127121

128-
float4 result = 0.0f;
122+
float4 result = float4(0.0, 0.0, 0.0, 0.0);
129123

130124
// Tile overlap counter
131125
if (n > 0)
132126
{
133-
result = OverlayHeatMap(int2(coord.unPositionSS.xy) & (TILE_SIZE - 1), n);
127+
result = OverlayHeatMap(int2(posInput.unPositionSS.xy) & (TILE_SIZE - 1), n);
134128
}
135129

136130
// Highlight selected tile
137-
if(all(mouseTileCoord == tileCoord))
131+
if (all(mouseTileCoord == tileCoord))
138132
{
139133
bool border = any(offsetInTile == 0 || offsetInTile == TILE_SIZE - 1);
140-
float4 result2 = float4(1, 1, 1, border ? 1.0f : 0.5);
134+
float4 result2 = float4(1.0, 1.0, 1.0, border ? 1.0 : 0.5);
141135
result = AlphaBlend(result, result2);
142136
}
143137

144138
// Print light lists for selected tile at the bottom of the screen
145139
int maxLights = 32;
146-
if(tileCoord.y < LIGHTCATEGORY_COUNT && tileCoord.x < maxLights + 3)
140+
if (tileCoord.y < LIGHTCATEGORY_COUNT && tileCoord.x < maxLights + 3)
147141
{
148-
Coordinate mouseCoord = GetCoordinate(_MousePixelCoord, _ScreenSize.zw);
142+
PositionInput mousePosInput = GetPositionInput(_MousePixelCoord, _ScreenSize.zw);
143+
float depthMouse = LOAD_TEXTURE2D(_CameraDepthTexture, mousePosInput.unPositionSS).x;
144+
UpdatePositionInput(depthMouse, _InvViewProjMatrix, GetWorldToViewMatrix(), mousePosInput);
149145

150146
int category = (LIGHTCATEGORY_COUNT - 1) - tileCoord.y;
151147
int start;
152148
int count;
153-
GetCountAndStart(mouseCoord, category, linearDepth, start, count);
149+
GetCountAndStart(mousePosInput, category, start, count);
154150

155151
float4 result2 = float4(.1,.1,.1,.9);
156152
int2 fontCoord = int2(pixelCoord.x, offsetInTile.y);
@@ -166,12 +162,12 @@ Shader "Hidden/HDRenderLoop/DebugViewTiles"
166162
n = FetchIndex(start, lightListIndex);
167163
}
168164

169-
if(n >= 0)
165+
if (n >= 0)
170166
{
171-
if(SampleDebugFontNumber(offsetInTile, n))
172-
result2 = float4(0, 0, 0, 1);
173-
if(SampleDebugFontNumber(offsetInTile + 1, n))
174-
result2 = float4(1, 1, 1, 1);
167+
if (SampleDebugFontNumber(offsetInTile, n))
168+
result2 = float4(0.0, 0.0, 0.0, 1.0);
169+
if (SampleDebugFontNumber(offsetInTile + 1, n))
170+
result2 = float4(1.0, 1.0, 1.0, 1.0);
175171
}
176172

177173
result = AlphaBlend(result, result2);

Assets/ScriptableRenderLoop/HDRenderLoop/HDRenderLoop.asset

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,3 @@ MonoBehaviour:
2828
spotCookieSize: 128
2929
pointCookieSize: 512
3030
reflectionCubemapSize: 128
31-
m_Dirty: 0

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: 42 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -245,8 +245,8 @@ public override void Cleanup()
245245
#if UNITY_EDITOR
246246
UnityEditor.SupportedRenderingFeatures.active = UnityEditor.SupportedRenderingFeatures.Default;
247247
#endif
248-
UnityEngine.Rendering.GraphicsSettings.lightsUseLinearIntensity = previousLightsUseLinearIntensity;
249-
UnityEngine.Rendering.GraphicsSettings.lightsUseCCT = previousLightsUseCCT;
248+
// UnityEngine.Rendering.GraphicsSettings.lightsUseLinearIntensity = previousLightsUseLinearIntensity;
249+
// UnityEngine.Rendering.GraphicsSettings.lightsUseCCT = previousLightsUseCCT;
250250
}
251251

252252
void InitAndClearBuffer(Camera camera, RenderLoop renderLoop)
@@ -360,18 +360,17 @@ void RenderGBuffer(CullResults cull, Camera camera, RenderLoop renderLoop)
360360
}
361361

362362
// This pass is use in case of forward opaque and deferred rendering. We need to render forward objects before tile lighting pass
363-
void RenderForwardOpaqueDepth(CullResults cull, Camera camera, RenderLoop renderLoop)
363+
void RenderForwardOnlyDepthPrepass(CullResults cull, Camera camera, RenderLoop renderLoop)
364364
{
365-
// If we have render a depth prepass, no need for this pass
366-
if (debugParameters.useDepthPrepass)
365+
// If we are forward only we don't need to render ForwardOpaqueDepth object
366+
// But in case we request a prepass we render it
367+
if (debugParameters.useForwardRenderingOnly && !debugParameters.useDepthPrepass)
367368
return;
368369

369370
using (new Utilities.ProfilingSample("Forward opaque depth", renderLoop))
370371
{
371-
// TODO: Use the render queue index to only send the forward opaque!
372-
// or use the new MAterial.SetPassEnable ?
373372
Utilities.SetRenderTarget(renderLoop, m_CameraDepthBufferRT);
374-
RenderOpaqueRenderList(cull, camera, renderLoop, "DepthOnly");
373+
RenderOpaqueRenderList(cull, camera, renderLoop, "ForwardOnlyDepthOnly");
375374
}
376375
}
377376

@@ -390,9 +389,11 @@ void RenderDebugViewMaterial(CullResults cull, Camera camera, RenderLoop renderL
390389
// Render GBuffer opaque
391390
if (!debugParameters.useForwardRenderingOnly)
392391
{
393-
Vector4 screenSize = Utilities.ComputeScreenSize(camera);
392+
var invViewProj = Utilities.GetViewProjectionMatrix(camera).inverse;
393+
var screenSize = Utilities.ComputeScreenSize(camera);
394394
m_DebugViewMaterialGBuffer.SetVector("_ScreenSize", screenSize);
395395
m_DebugViewMaterialGBuffer.SetFloat("_DebugViewMaterial", (float)debugParameters.debugViewMaterial);
396+
m_DebugViewMaterialGBuffer.SetMatrix("_InvViewProjMatrix", invViewProj);
396397

397398
// m_gbufferManager.BindBuffers(m_DebugViewMaterialGBuffer);
398399
// TODO: Bind depth textures
@@ -460,6 +461,22 @@ void RenderForward(CullResults cullResults, Camera camera, RenderLoop renderLoop
460461
}
461462
}
462463

464+
// Render material that are forward opaque only (like eye)
465+
// TODO: Think about hair that could be render both as opaque and transparent...
466+
void RenderForwardOnly(CullResults cullResults, Camera camera, RenderLoop renderLoop)
467+
{
468+
using (new Utilities.ProfilingSample("Forward Only Pass", renderLoop))
469+
{
470+
// Bind material data
471+
m_LitRenderLoop.Bind();
472+
473+
Utilities.SetRenderTarget(renderLoop, m_CameraColorBufferRT, m_CameraDepthBufferRT);
474+
475+
m_lightLoop.RenderForward(camera, renderLoop, true);
476+
RenderOpaqueRenderList(cullResults, camera, renderLoop, "ForwardOnly");
477+
}
478+
}
479+
463480
void RenderForwardUnlit(CullResults cullResults, Camera camera, RenderLoop renderLoop)
464481
{
465482
using (new Utilities.ProfilingSample("Forward Unlit Pass", renderLoop))
@@ -601,6 +618,17 @@ public void PushGlobalParams(Camera camera, RenderLoop renderLoop)
601618
Shader.SetGlobalInt("_EnvLightSkyEnabled", 0);
602619
}
603620

621+
var invViewProj = Utilities.GetViewProjectionMatrix(camera).inverse;
622+
var screenSize = Utilities.ComputeScreenSize(camera);
623+
624+
var cmd = new CommandBuffer { name = "Push Global Parameters" };
625+
626+
cmd.SetGlobalVector("_ScreenSize", screenSize);
627+
cmd.SetGlobalMatrix("_InvViewProjMatrix", invViewProj);
628+
629+
renderLoop.ExecuteCommandBuffer(cmd);
630+
cmd.Dispose();
631+
604632
m_lightLoop.PushGlobalParams(camera, renderLoop);
605633
}
606634

@@ -638,16 +666,11 @@ public override void Render(Camera[] cameras, RenderLoop renderLoop)
638666

639667
RenderDepthPrepass(cullResults, camera, renderLoop);
640668

641-
RenderGBuffer(cullResults, camera, renderLoop);
642-
643-
// Forward opaque with deferred tile require that we fill the depth buffer
669+
// Forward opaque with deferred/cluster tile require that we fill the depth buffer
644670
// correctly to build the light list.
645671
// TODO: avoid double lighting by tagging stencil or gbuffer that we must not lit.
646-
// TODO: ask Morten why this pass is not before GBuffer ? Will make more sense and avoid
647-
// to do gbuffer pass on unseen mesh.
648-
// TODO: how do we select only the object that must be render forward ?
649-
// this is all object with gbuffer pass disabled ?
650-
//RenderForwardOpaqueDepth(cullResults, camera, renderLoop);
672+
RenderForwardOnlyDepthPrepass(cullResults, camera, renderLoop);
673+
RenderGBuffer(cullResults, camera, renderLoop);
651674

652675
if (debugParameters.debugViewMaterial != 0)
653676
{
@@ -672,8 +695,8 @@ public override void Render(Camera[] cameras, RenderLoop renderLoop)
672695
}
673696
RenderDeferredLighting(camera, renderLoop);
674697

675-
// TODO: enable this for tile forward opaque
676-
// RenderForward(cullResults, camera, renderLoop, true);
698+
RenderForward(cullResults, camera, renderLoop, true);
699+
RenderForwardOnly(cullResults, camera, renderLoop);
677700

678701
RenderSky(camera, renderLoop);
679702

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

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -79,24 +79,22 @@ Shader "Hidden/HDRenderLoop/Deferred"
7979

8080
float4 Frag(Varyings input) : SV_Target
8181
{
82-
float4 unPositionSS = input.positionCS; // as input we have the vpos
83-
Coordinate coord = GetCoordinate(unPositionSS.xy, _ScreenSize.zw);
82+
// input.positionCS is SV_Position
83+
PositionInputs posInput = GetPositionInput(input.positionCS.xy, _ScreenSize.zw);
84+
float depth = LOAD_TEXTURE2D(_CameraDepthTexture, posInput.unPositionSS).x;
85+
UpdatePositionInput(depth, _InvViewProjMatrix, GetWorldToViewMatrix(), posInput);
86+
float3 V = GetWorldSpaceNormalizeViewDir(posInput.positionWS);
8487

85-
// No need to manage inverse depth, this is handled by the projection matrix
86-
float depth = LOAD_TEXTURE2D(_CameraDepthTexture, coord.unPositionSS).x;
87-
float3 positionWS = UnprojectToWorld(depth, coord.positionSS, _InvViewProjMatrix);
88-
float3 V = GetWorldSpaceNormalizeViewDir(positionWS);
89-
90-
FETCH_GBUFFER(gbuffer, _GBufferTexture, coord.unPositionSS);
88+
FETCH_GBUFFER(gbuffer, _GBufferTexture, posInput.unPositionSS);
9189
BSDFData bsdfData;
9290
float3 bakeDiffuseLighting;
9391
DECODE_FROM_GBUFFER(gbuffer, bsdfData, bakeDiffuseLighting);
9492

95-
PreLightData preLightData = GetPreLightData(V, positionWS, coord, bsdfData);
93+
PreLightData preLightData = GetPreLightData(V, posInput, bsdfData);
9694

9795
float3 diffuseLighting;
9896
float3 specularLighting;
99-
LightLoop(V, positionWS, coord, preLightData, bsdfData, bakeDiffuseLighting, diffuseLighting, specularLighting);
97+
LightLoop(V, posInput, preLightData, bsdfData, bakeDiffuseLighting, diffuseLighting, specularLighting);
10098

10199
return float4(diffuseLighting + specularLighting, 1.0);
102100
}

0 commit comments

Comments
 (0)