Skip to content

Commit 073d986

Browse files
HDRenderLoop: Draft of refator not working
1 parent de31d19 commit 073d986

File tree

7 files changed

+501
-408
lines changed

7 files changed

+501
-408
lines changed

Assets/ScriptableRenderLoop/HDRenderLoop/HDRenderLoop.cs

Lines changed: 39 additions & 214 deletions
Large diffs are not rendered by default.

Assets/ScriptableRenderLoop/HDRenderLoop/Lighting/LightDefinition.cs

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ public struct DirectionalLightData
8585
// TODO: we may have to add various parameters here for shadow - was suppose to be coupled with a light loop
8686
// A point light is 6x PunctualShadowData
8787
[GenerateHLSL]
88-
public struct PunctualShadowData
88+
public struct ShadowData
8989
{
9090
// World to ShadowMap matrix
9191
// Include scale and bias for shadow atlas if any
@@ -97,18 +97,6 @@ public struct PunctualShadowData
9797
public float unused;
9898
};
9999

100-
[GenerateHLSL]
101-
public struct DirectionalShadowData
102-
{
103-
// World to ShadowMap matrix
104-
// Include scale and bias for shadow atlas if any
105-
public Matrix4x4 worldToShadow;
106-
107-
public float bias;
108-
public float quality;
109-
public Vector2 unused2;
110-
};
111-
112100
[GenerateHLSL]
113101
public enum EnvShapeType
114102
{
Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
using UnityEngine;
2+
using UnityEngine.Rendering;
3+
using UnityEngine.Experimental.Rendering;
4+
using System;
5+
6+
namespace UnityEngine.Experimental.ScriptableRenderLoop
7+
{
8+
public class LightLoop
9+
{
10+
public virtual string GetKeyword()
11+
{
12+
return "";
13+
}
14+
15+
public virtual void Rebuild()
16+
{
17+
m_lightList = new LightList();
18+
19+
s_DirectionalLights = new ComputeBuffer(HDRenderLoop.k_MaxDirectionalLightsOnSCreen, System.Runtime.InteropServices.Marshal.SizeOf(typeof(DirectionalLightData)));
20+
s_DirectionalShadowList = new ComputeBuffer(HDRenderLoop.k_MaxCascadeCount, System.Runtime.InteropServices.Marshal.SizeOf(typeof(DirectionalShadowData)));
21+
s_PunctualLightList = new ComputeBuffer(HDRenderLoop.k_MaxPunctualLightsOnSCreen, System.Runtime.InteropServices.Marshal.SizeOf(typeof(LightData)));
22+
s_AreaLightList = new ComputeBuffer(HDRenderLoop.k_MaxAreaLightsOnSCreen, System.Runtime.InteropServices.Marshal.SizeOf(typeof(LightData)));
23+
s_EnvLightList = new ComputeBuffer(HDRenderLoop.k_MaxEnvLightsOnSCreen, System.Runtime.InteropServices.Marshal.SizeOf(typeof(EnvLightData)));
24+
s_PunctualShadowList = new ComputeBuffer(HDRenderLoop.k_MaxShadowOnScreen, System.Runtime.InteropServices.Marshal.SizeOf(typeof(PunctualShadowData)));
25+
26+
m_CookieTexArray = new TextureCache2D();
27+
m_CookieTexArray.AllocTextureArray(8, m_TextureSettings.spotCookieSize, m_TextureSettings.spotCookieSize, TextureFormat.RGBA32, true);
28+
m_CubeCookieTexArray = new TextureCacheCubemap();
29+
m_CubeCookieTexArray.AllocTextureArray(4, m_TextureSettings.pointCookieSize, TextureFormat.RGBA32, true);
30+
m_CubeReflTexArray = new TextureCacheCubemap();
31+
m_CubeReflTexArray.AllocTextureArray(32, m_TextureSettings.reflectionCubemapSize, TextureFormat.BC6H, true);
32+
}
33+
34+
public virtual void Cleanup()
35+
{
36+
Utilities.SafeRelease(s_DirectionalLights);
37+
Utilities.SafeRelease(s_DirectionalShadowList);
38+
Utilities.SafeRelease(s_PunctualLightList);
39+
Utilities.SafeRelease(s_AreaLightList);
40+
Utilities.SafeRelease(s_EnvLightList);
41+
Utilities.SafeRelease(s_PunctualShadowList);
42+
43+
if (m_CubeReflTexArray != null)
44+
{
45+
m_CubeReflTexArray.Release();
46+
m_CubeReflTexArray = null;
47+
}
48+
if (m_CookieTexArray != null)
49+
{
50+
m_CookieTexArray.Release();
51+
m_CookieTexArray = null;
52+
}
53+
if (m_CubeCookieTexArray != null)
54+
{
55+
m_CubeCookieTexArray.Release();
56+
m_CubeCookieTexArray = null;
57+
}
58+
}
59+
60+
virtual void NewFrame()
61+
{
62+
m_CookieTexArray.NewFrame();
63+
m_CubeCookieTexArray.NewFrame();
64+
m_CubeReflTexArray.NewFrame();
65+
}
66+
67+
68+
69+
public ShadowData GetShadowData(VisibleLight light, AdditionalLightData additionalData, int lightIndex, ref ShadowOutput shadowOutput)
70+
{
71+
bool hasDirectionalShadows = light.light.shadows != LightShadows.None && shadowOutput.GetShadowSliceCountLightIndex(lightIndex) != 0;
72+
73+
if (hasDirectionalShadows)
74+
{
75+
for (int sliceIndex = 0; sliceIndex < shadowOutput.GetShadowSliceCountLightIndex(lightIndex); ++sliceIndex)
76+
{
77+
ShadowData shadowData = new ShadowData();
78+
79+
int shadowSliceIndex = shadowOutput.GetShadowSliceIndex(lightIndex, sliceIndex);
80+
shadowData.worldToShadow = shadowOutput.shadowSlices[shadowSliceIndex].shadowTransform.transpose; // Transpose for hlsl reading ?
81+
shadowData.lightType = lightData.lightType;
82+
83+
shadowData.bias = light.light.shadowBias;
84+
85+
m_lightList.directionalShadows.Add(shadowData);
86+
}
87+
}
88+
}
89+
90+
public DirectionalLightData GetDirectionalLightData(VisibleLight light, AdditionalLightData additionalData)
91+
{
92+
Debug.Assert(light.lightType == LightType.Directional);
93+
94+
var directionalLightData = new DirectionalLightData();
95+
// Light direction for directional and is opposite to the forward direction
96+
directionalLightData.direction = -light.light.transform.forward;
97+
// up and right are use for cookie
98+
directionalLightData.up = light.light.transform.up;
99+
directionalLightData.right = light.light.transform.right;
100+
directionalLightData.positionWS = light.light.transform.position;
101+
directionalLightData.color = GetLightColor(light);
102+
directionalLightData.diffuseScale = additionalData.affectDiffuse ? 1.0f : 0.0f;
103+
directionalLightData.specularScale = additionalData.affectSpecular ? 1.0f : 0.0f;
104+
directionalLightData.invScaleX = 1.0f / light.light.transform.localScale.x;
105+
directionalLightData.invScaleY = 1.0f / light.light.transform.localScale.y;
106+
directionalLightData.cosAngle = 0.0f;
107+
directionalLightData.sinAngle = 0.0f;
108+
directionalLightData.shadowIndex = -1;
109+
directionalLightData.cookieIndex = -1;
110+
111+
if (light.light.cookie != null)
112+
{
113+
directionalLightData.tileCookie = (light.light.cookie.wrapMode == TextureWrapMode.Repeat);
114+
directionalLightData.cookieIndex = m_CookieTexArray.FetchSlice(light.light.cookie);
115+
}
116+
117+
directionalLightData.shadowIndex = 0;
118+
}
119+
120+
public virtual void PrepareLightsForGPU(CullResults cullResults, Camera camera) { }
121+
122+
public virtual void PushGlobalParams(Camera camera, RenderLoop loop)
123+
{
124+
Shader.SetGlobalTexture("_CookieTextures", m_CookieTexArray.GetTexCache());
125+
Shader.SetGlobalTexture("_CookieCubeTextures", m_CubeCookieTexArray.GetTexCache());
126+
Shader.SetGlobalTexture("_EnvTextures", m_CubeReflTexArray.GetTexCache());
127+
128+
s_DirectionalLights.SetData(m_lightList.directionalLights.ToArray());
129+
s_DirectionalShadowList.SetData(m_lightList.directionalShadows.ToArray());
130+
s_PunctualLightList.SetData(m_lightList.punctualLights.ToArray());
131+
s_AreaLightList.SetData(m_lightList.areaLights.ToArray());
132+
s_EnvLightList.SetData(m_lightList.envLights.ToArray());
133+
s_PunctualShadowList.SetData(m_lightList.punctualShadows.ToArray());
134+
135+
Shader.SetGlobalBuffer("_DirectionalLightList", s_DirectionalLights);
136+
Shader.SetGlobalInt("_DirectionalLightCount", m_lightList.directionalLights.Count);
137+
Shader.SetGlobalBuffer("_DirectionalShadowList", s_DirectionalShadowList);
138+
Shader.SetGlobalBuffer("_PunctualLightList", s_PunctualLightList);
139+
Shader.SetGlobalInt("_PunctualLightCount", m_lightList.punctualLights.Count);
140+
Shader.SetGlobalBuffer("_AreaLightList", s_AreaLightList);
141+
Shader.SetGlobalInt("_AreaLightCount", m_lightList.areaLights.Count);
142+
Shader.SetGlobalBuffer("_PunctualShadowList", s_PunctualShadowList);
143+
Shader.SetGlobalBuffer("_EnvLightList", s_EnvLightList);
144+
Shader.SetGlobalInt("_EnvLightCount", m_lightList.envLights.Count);
145+
146+
Shader.SetGlobalVectorArray("_DirShadowSplitSpheres", m_lightList.directionalShadowSplitSphereSqr);
147+
}
148+
149+
public virtual void RenderDeferredLighting(Camera camera, RenderLoop renderLoop, RenderTargetIdentifier cameraColorBufferRT) {}
150+
}
151+
}

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

Lines changed: 45 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -5,100 +5,66 @@
55

66
namespace UnityEngine.Experimental.ScriptableRenderLoop
77
{
8-
namespace SinglePass
8+
public class SinglePass : LightLoop
99
{
10-
//-----------------------------------------------------------------------------
11-
// structure definition
12-
//-----------------------------------------------------------------------------
13-
14-
public class LightLoop
10+
string GetKeyword()
1511
{
16-
string GetKeyword()
17-
{
18-
return "LIGHTLOOP_SINGLE_PASS";
19-
}
20-
21-
// Static keyword is required here else we get a "DestroyBuffer can only be call in main thread"
22-
static ComputeBuffer s_DirectionalLights = null;
23-
static ComputeBuffer s_PunctualLightList = null;
24-
static ComputeBuffer s_EnvLightList = null;
25-
static ComputeBuffer s_AreaLightList = null;
26-
static ComputeBuffer s_PunctualShadowList = null;
27-
static ComputeBuffer s_DirectionalShadowList = null;
28-
29-
Material m_DeferredMaterial = null;
12+
return "LIGHTLOOP_SINGLE_PASS";
13+
}
3014

31-
public void Rebuild()
32-
{
33-
s_DirectionalLights = new ComputeBuffer(HDRenderLoop.k_MaxDirectionalLightsOnSCreen, System.Runtime.InteropServices.Marshal.SizeOf(typeof(DirectionalLightData)));
34-
s_DirectionalShadowList = new ComputeBuffer(HDRenderLoop.k_MaxCascadeCount, System.Runtime.InteropServices.Marshal.SizeOf(typeof(DirectionalShadowData)));
35-
s_PunctualLightList = new ComputeBuffer(HDRenderLoop.k_MaxPunctualLightsOnSCreen, System.Runtime.InteropServices.Marshal.SizeOf(typeof(LightData)));
36-
s_AreaLightList = new ComputeBuffer(HDRenderLoop.k_MaxAreaLightsOnSCreen, System.Runtime.InteropServices.Marshal.SizeOf(typeof(LightData)));
37-
s_EnvLightList = new ComputeBuffer(HDRenderLoop.k_MaxEnvLightsOnSCreen, System.Runtime.InteropServices.Marshal.SizeOf(typeof(EnvLightData)));
38-
s_PunctualShadowList = new ComputeBuffer(HDRenderLoop.k_MaxShadowOnScreen, System.Runtime.InteropServices.Marshal.SizeOf(typeof(PunctualShadowData)));
15+
public const int k_MaxDirectionalLightsOnSCreen = 3;
16+
public const int k_MaxPunctualLightsOnSCreen = 512;
17+
public const int k_MaxAreaLightsOnSCreen = 128;
18+
public const int k_MaxEnvLightsOnSCreen = 64;
19+
public const int k_MaxShadowOnScreen = 16;
20+
public const int k_MaxCascadeCount = 4; //Should be not less than m_Settings.directionalLightCascadeCount;
3921

40-
m_DeferredMaterial = Utilities.CreateEngineMaterial("Hidden/HDRenderLoop/Deferred");
41-
m_DeferredMaterial.EnableKeyword("LIGHTLOOP_SINGLE_PASS");
42-
}
22+
Material m_DeferredMaterial = null;
4323

44-
public void Cleanup()
45-
{
46-
Utilities.SafeRelease(s_DirectionalLights);
47-
Utilities.SafeRelease(s_DirectionalShadowList);
48-
Utilities.SafeRelease(s_PunctualLightList);
49-
Utilities.SafeRelease(s_AreaLightList);
50-
Utilities.SafeRelease(s_EnvLightList);
51-
Utilities.SafeRelease(s_PunctualShadowList);
24+
public override void Rebuild()
25+
{
26+
base.Rebuild();
5227

53-
Utilities.Destroy(m_DeferredMaterial);
54-
}
28+
m_DeferredMaterial = Utilities.CreateEngineMaterial("Hidden/HDRenderLoop/Deferred");
29+
m_DeferredMaterial.EnableKeyword(GetKeyword());
30+
}
5531

56-
public void PrepareLightsForGPU(CullResults cullResults, Camera camera, HDRenderLoop.LightList lightList) {}
32+
public override void Cleanup()
33+
{
34+
base.Cleanup();
5735

58-
public void PushGlobalParams(Camera camera, RenderLoop loop, HDRenderLoop.LightList lightList)
59-
{
60-
s_DirectionalLights.SetData(lightList.directionalLights.ToArray());
61-
s_DirectionalShadowList.SetData(lightList.directionalShadows.ToArray());
62-
s_PunctualLightList.SetData(lightList.punctualLights.ToArray());
63-
s_AreaLightList.SetData(lightList.areaLights.ToArray());
64-
s_EnvLightList.SetData(lightList.envLights.ToArray());
65-
s_PunctualShadowList.SetData(lightList.punctualShadows.ToArray());
36+
Utilities.Destroy(m_DeferredMaterial);
37+
}
6638

67-
Shader.SetGlobalBuffer("_DirectionalLightList", s_DirectionalLights);
68-
Shader.SetGlobalInt("_DirectionalLightCount", lightList.directionalLights.Count);
69-
Shader.SetGlobalBuffer("_DirectionalShadowList", s_DirectionalShadowList);
70-
Shader.SetGlobalBuffer("_PunctualLightList", s_PunctualLightList);
71-
Shader.SetGlobalInt("_PunctualLightCount", lightList.punctualLights.Count);
72-
Shader.SetGlobalBuffer("_AreaLightList", s_AreaLightList);
73-
Shader.SetGlobalInt("_AreaLightCount", lightList.areaLights.Count);
74-
Shader.SetGlobalBuffer("_PunctualShadowList", s_PunctualShadowList);
75-
Shader.SetGlobalBuffer("_EnvLightList", s_EnvLightList);
76-
Shader.SetGlobalInt("_EnvLightCount", lightList.envLights.Count);
39+
public override void PrepareLightsForGPU(CullResults cullResults, Camera camera)
40+
{
41+
}
7742

78-
Shader.SetGlobalVectorArray("_DirShadowSplitSpheres", lightList.directionalShadowSplitSphereSqr);
79-
}
43+
public override void PushGlobalParams(Camera camera, RenderLoop loop)
44+
{
45+
base.PushGlobalParams();
46+
}
8047

81-
public void RenderDeferredLighting(Camera camera, RenderLoop renderLoop, RenderTargetIdentifier cameraColorBufferRT)
82-
{
83-
var invViewProj = Utilities.GetViewProjectionMatrix(camera).inverse;
84-
m_DeferredMaterial.SetMatrix("_InvViewProjMatrix", invViewProj);
48+
public override void RenderDeferredLighting(Camera camera, RenderLoop renderLoop, RenderTargetIdentifier cameraColorBufferRT)
49+
{
50+
var invViewProj = Utilities.GetViewProjectionMatrix(camera).inverse;
51+
m_DeferredMaterial.SetMatrix("_InvViewProjMatrix", invViewProj);
8552

86-
var screenSize = Utilities.ComputeScreenSize(camera);
87-
m_DeferredMaterial.SetVector("_ScreenSize", screenSize);
53+
var screenSize = Utilities.ComputeScreenSize(camera);
54+
m_DeferredMaterial.SetVector("_ScreenSize", screenSize);
8855

89-
m_DeferredMaterial.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.One);
90-
m_DeferredMaterial.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.Zero);
56+
m_DeferredMaterial.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.One);
57+
m_DeferredMaterial.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.Zero);
9158

92-
// m_gbufferManager.BindBuffers(m_DeferredMaterial);
93-
// TODO: Bind depth textures
59+
// m_gbufferManager.BindBuffers(m_DeferredMaterial);
60+
// TODO: Bind depth textures
9461

95-
using (new Utilities.ProfilingSample("SinglePass - Deferred Lighting Pass", renderLoop))
96-
{
97-
var cmd = new CommandBuffer { name = "" };
98-
cmd.Blit(null, cameraColorBufferRT, m_DeferredMaterial, 0);
99-
renderLoop.ExecuteCommandBuffer(cmd);
100-
cmd.Dispose();
101-
}
62+
using (new Utilities.ProfilingSample("SinglePass - Deferred Lighting Pass", renderLoop))
63+
{
64+
var cmd = new CommandBuffer { name = "" };
65+
cmd.Blit(null, cameraColorBufferRT, m_DeferredMaterial, 0);
66+
renderLoop.ExecuteCommandBuffer(cmd);
67+
cmd.Dispose();
10268
}
10369
}
10470
}

0 commit comments

Comments
 (0)