|
| 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 | +} |
0 commit comments