using System; using UnityEngine.Experimental.Rendering; using UnityEngine.Experimental.Rendering.RenderGraphModule; using System.Collections.Generic; namespace UnityEngine.Rendering.Universal.Internal { /// /// Renders a shadow map for the main Light. /// public class MainLightShadowCasterPass : ScriptableRenderPass { private static class MainLightShadowConstantBuffer { public static int _WorldToShadow; public static int _ShadowParams; public static int _CascadeShadowSplitSpheres0; public static int _CascadeShadowSplitSpheres1; public static int _CascadeShadowSplitSpheres2; public static int _CascadeShadowSplitSpheres3; public static int _CascadeShadowSplitSphereRadii; public static int _ShadowOffset0; public static int _ShadowOffset1; public static int _ShadowmapSize; } const int k_MaxCascades = 4; const int k_ShadowmapBufferBits = 16; float m_CascadeBorder; float m_MaxShadowDistanceSq; int m_ShadowCasterCascadesCount; int m_MainLightShadowmapID; internal RTHandle m_MainLightShadowmapTexture; internal RTHandle m_EmptyLightShadowmapTexture; Matrix4x4[] m_MainLightShadowMatrices; ShadowSliceData[] m_CascadeSlices; Vector4[] m_CascadeSplitDistances; bool m_CreateEmptyShadowmap; bool m_EmptyShadowmapNeedsClear = false; int renderTargetWidth; int renderTargetHeight; ProfilingSampler m_ProfilingSetupSampler = new ProfilingSampler("Setup Main Shadowmap"); private PassData m_PassData; /// /// Creates a new MainLightShadowCasterPass instance. /// /// The RenderPassEvent to use. /// public MainLightShadowCasterPass(RenderPassEvent evt) { base.profilingSampler = new ProfilingSampler(nameof(MainLightShadowCasterPass)); renderPassEvent = evt; m_PassData = new PassData(); m_MainLightShadowMatrices = new Matrix4x4[k_MaxCascades + 1]; m_CascadeSlices = new ShadowSliceData[k_MaxCascades]; m_CascadeSplitDistances = new Vector4[k_MaxCascades]; MainLightShadowConstantBuffer._WorldToShadow = Shader.PropertyToID("_MainLightWorldToShadow"); MainLightShadowConstantBuffer._ShadowParams = Shader.PropertyToID("_MainLightShadowParams"); MainLightShadowConstantBuffer._CascadeShadowSplitSpheres0 = Shader.PropertyToID("_CascadeShadowSplitSpheres0"); MainLightShadowConstantBuffer._CascadeShadowSplitSpheres1 = Shader.PropertyToID("_CascadeShadowSplitSpheres1"); MainLightShadowConstantBuffer._CascadeShadowSplitSpheres2 = Shader.PropertyToID("_CascadeShadowSplitSpheres2"); MainLightShadowConstantBuffer._CascadeShadowSplitSpheres3 = Shader.PropertyToID("_CascadeShadowSplitSpheres3"); MainLightShadowConstantBuffer._CascadeShadowSplitSphereRadii = Shader.PropertyToID("_CascadeShadowSplitSphereRadii"); MainLightShadowConstantBuffer._ShadowOffset0 = Shader.PropertyToID("_MainLightShadowOffset0"); MainLightShadowConstantBuffer._ShadowOffset1 = Shader.PropertyToID("_MainLightShadowOffset1"); MainLightShadowConstantBuffer._ShadowmapSize = Shader.PropertyToID("_MainLightShadowmapSize"); m_MainLightShadowmapID = Shader.PropertyToID("_MainLightShadowmapTexture"); m_EmptyLightShadowmapTexture = ShadowUtils.AllocShadowRT(1, 1, k_ShadowmapBufferBits, 1, 0, name: "_EmptyLightShadowmapTexture"); m_EmptyShadowmapNeedsClear = true; } /// /// Cleans up resources used by the pass. /// public void Dispose() { m_MainLightShadowmapTexture?.Release(); m_EmptyLightShadowmapTexture?.Release(); } /// /// Sets up the pass. /// /// /// True if the pass should be enqueued, otherwise false. /// public bool Setup(ref RenderingData renderingData) { if (!renderingData.shadowData.mainLightShadowsEnabled) return false; using var profScope = new ProfilingScope(m_ProfilingSetupSampler); ref ShadowData shadowData = ref renderingData.shadowData; if (!shadowData.supportsMainLightShadows) return SetupForEmptyRendering(ref renderingData); Clear(); int shadowLightIndex = renderingData.lightData.mainLightIndex; if (shadowLightIndex == -1) return SetupForEmptyRendering(ref renderingData); VisibleLight shadowLight = renderingData.lightData.visibleLights[shadowLightIndex]; Light light = shadowLight.light; if (light.shadows == LightShadows.None) return SetupForEmptyRendering(ref renderingData); if (shadowLight.lightType != LightType.Directional) { Debug.LogWarning("Only directional lights are supported as main light."); } Bounds bounds; if (!renderingData.cullResults.GetShadowCasterBounds(shadowLightIndex, out bounds)) return SetupForEmptyRendering(ref renderingData); m_ShadowCasterCascadesCount = shadowData.mainLightShadowCascadesCount; renderTargetWidth = shadowData.mainLightRenderTargetWidth; renderTargetHeight = shadowData.mainLightRenderTargetHeight; ref readonly URPLightShadowCullingInfos shadowCullingInfos = ref renderingData.visibleLightsShadowCullingInfos.UnsafeElementAt(shadowLightIndex); for (int cascadeIndex = 0; cascadeIndex < m_ShadowCasterCascadesCount; ++cascadeIndex) { ref readonly ShadowSliceData sliceData = ref shadowCullingInfos.slices.UnsafeElementAt(cascadeIndex); m_CascadeSplitDistances[cascadeIndex] = sliceData.splitData.cullingSphere; m_CascadeSlices[cascadeIndex] = sliceData; if (!shadowCullingInfos.IsSliceValid(cascadeIndex)) return SetupForEmptyRendering(ref renderingData); } ShadowUtils.ShadowRTReAllocateIfNeeded(ref m_MainLightShadowmapTexture, renderTargetWidth, renderTargetHeight, k_ShadowmapBufferBits, name: "_MainLightShadowmapTexture"); m_MaxShadowDistanceSq = renderingData.cameraData.maxShadowDistance * renderingData.cameraData.maxShadowDistance; m_CascadeBorder = renderingData.shadowData.mainLightShadowCascadeBorder; m_CreateEmptyShadowmap = false; useNativeRenderPass = true; return true; } bool SetupForEmptyRendering(ref RenderingData renderingData) { if (!renderingData.cameraData.renderer.stripShadowsOffVariants) return false; m_CreateEmptyShadowmap = true; useNativeRenderPass = false; // Required for scene view camera(URP renderer not initialized) if(ShadowUtils.ShadowRTReAllocateIfNeeded(ref m_EmptyLightShadowmapTexture, 1, 1, k_ShadowmapBufferBits, name: "_EmptyLightShadowmapTexture")) m_EmptyShadowmapNeedsClear = true; return true; } /// public override void Configure(CommandBuffer cmd, RenderTextureDescriptor cameraTextureDescriptor) { if (m_CreateEmptyShadowmap && !m_EmptyShadowmapNeedsClear) { // Reset pass RTs to null ResetTarget(); return; } if (m_CreateEmptyShadowmap) { ConfigureTarget(m_EmptyLightShadowmapTexture); m_EmptyShadowmapNeedsClear = false; } else ConfigureTarget(m_MainLightShadowmapTexture); ConfigureClear(ClearFlag.All, Color.black); } /// public override void Execute(ScriptableRenderContext context, ref RenderingData renderingData) { if (m_CreateEmptyShadowmap) { SetEmptyMainLightCascadeShadowmap(CommandBufferHelpers.GetRasterCommandBuffer(renderingData.commandBuffer)); renderingData.commandBuffer.SetGlobalTexture(m_MainLightShadowmapID, m_EmptyLightShadowmapTexture.nameID); return; } InitPassData(ref m_PassData, ref renderingData); InitRendererLists(ref renderingData, ref m_PassData, context, default(RenderGraph), false); RenderMainLightCascadeShadowmap(CommandBufferHelpers.GetRasterCommandBuffer(renderingData.commandBuffer), ref m_PassData, ref renderingData, false); renderingData.commandBuffer.SetGlobalTexture(m_MainLightShadowmapID, m_MainLightShadowmapTexture.nameID); } void Clear() { for (int i = 0; i < m_MainLightShadowMatrices.Length; ++i) m_MainLightShadowMatrices[i] = Matrix4x4.identity; for (int i = 0; i < m_CascadeSplitDistances.Length; ++i) m_CascadeSplitDistances[i] = new Vector4(0.0f, 0.0f, 0.0f, 0.0f); for (int i = 0; i < m_CascadeSlices.Length; ++i) m_CascadeSlices[i].Clear(); } void SetEmptyMainLightCascadeShadowmap(RasterCommandBuffer cmd) { CoreUtils.SetKeyword(cmd, ShaderKeywordStrings.MainLightShadows, true); cmd.SetGlobalVector(MainLightShadowConstantBuffer._ShadowParams, new Vector4(1, 0, 1, 0)); cmd.SetGlobalVector(MainLightShadowConstantBuffer._ShadowmapSize, new Vector4(1f / m_EmptyLightShadowmapTexture.rt.width, 1f / m_EmptyLightShadowmapTexture.rt.height, m_EmptyLightShadowmapTexture.rt.width, m_EmptyLightShadowmapTexture.rt.height)); } void RenderMainLightCascadeShadowmap(RasterCommandBuffer cmd, ref PassData data, ref RenderingData renderingData, bool isRenderGraph) { var lightData = renderingData.lightData; int shadowLightIndex = lightData.mainLightIndex; if (shadowLightIndex == -1) return; VisibleLight shadowLight = lightData.visibleLights[shadowLightIndex]; using (new ProfilingScope(cmd, ProfilingSampler.Get(URPProfileId.MainLightShadow))) { // Need to start by setting the Camera position as that is not set for passes executed before normal rendering cmd.SetGlobalVector(ShaderPropertyId.worldSpaceCameraPos, renderingData.cameraData.worldSpaceCameraPos); for (int cascadeIndex = 0; cascadeIndex < m_ShadowCasterCascadesCount; ++cascadeIndex) { Vector4 shadowBias = ShadowUtils.GetShadowBias(ref shadowLight, shadowLightIndex, ref renderingData.shadowData, m_CascadeSlices[cascadeIndex].projectionMatrix, m_CascadeSlices[cascadeIndex].resolution); ShadowUtils.SetupShadowCasterConstantBuffer(cmd, ref shadowLight, shadowBias); CoreUtils.SetKeyword(cmd, ShaderKeywordStrings.CastingPunctualLightShadow, false); RendererList shadowRendererList = isRenderGraph? data.shadowRendererListsHandle[cascadeIndex] : data.shadowRendererLists[cascadeIndex]; ShadowUtils.RenderShadowSlice(cmd, ref m_CascadeSlices[cascadeIndex], ref shadowRendererList, m_CascadeSlices[cascadeIndex].projectionMatrix, m_CascadeSlices[cascadeIndex].viewMatrix); } renderingData.shadowData.isKeywordSoftShadowsEnabled = shadowLight.light.shadows == LightShadows.Soft && renderingData.shadowData.supportsSoftShadows; CoreUtils.SetKeyword(cmd, ShaderKeywordStrings.MainLightShadows, renderingData.shadowData.mainLightShadowCascadesCount == 1); CoreUtils.SetKeyword(cmd, ShaderKeywordStrings.MainLightShadowCascades, renderingData.shadowData.mainLightShadowCascadesCount > 1); CoreUtils.SetKeyword(cmd, ShaderKeywordStrings.SoftShadows, renderingData.shadowData.isKeywordSoftShadowsEnabled); SetupMainLightShadowReceiverConstants(cmd, ref shadowLight, ref renderingData.shadowData); } } void SetupMainLightShadowReceiverConstants(RasterCommandBuffer cmd, ref VisibleLight shadowLight, ref ShadowData shadowData) { Light light = shadowLight.light; bool softShadows = shadowLight.light.shadows == LightShadows.Soft && shadowData.supportsSoftShadows; int cascadeCount = m_ShadowCasterCascadesCount; for (int i = 0; i < cascadeCount; ++i) m_MainLightShadowMatrices[i] = m_CascadeSlices[i].shadowTransform; // We setup and additional a no-op WorldToShadow matrix in the last index // because the ComputeCascadeIndex function in Shadows.hlsl can return an index // out of bounds. (position not inside any cascade) and we want to avoid branching Matrix4x4 noOpShadowMatrix = Matrix4x4.zero; noOpShadowMatrix.m22 = (SystemInfo.usesReversedZBuffer) ? 1.0f : 0.0f; for (int i = cascadeCount; i <= k_MaxCascades; ++i) m_MainLightShadowMatrices[i] = noOpShadowMatrix; float invShadowAtlasWidth = 1.0f / renderTargetWidth; float invShadowAtlasHeight = 1.0f / renderTargetHeight; float invHalfShadowAtlasWidth = 0.5f * invShadowAtlasWidth; float invHalfShadowAtlasHeight = 0.5f * invShadowAtlasHeight; float softShadowsProp = ShadowUtils.SoftShadowQualityToShaderProperty(light, softShadows); ShadowUtils.GetScaleAndBiasForLinearDistanceFade(m_MaxShadowDistanceSq, m_CascadeBorder, out float shadowFadeScale, out float shadowFadeBias); cmd.SetGlobalMatrixArray(MainLightShadowConstantBuffer._WorldToShadow, m_MainLightShadowMatrices); cmd.SetGlobalVector(MainLightShadowConstantBuffer._ShadowParams, new Vector4(light.shadowStrength, softShadowsProp, shadowFadeScale, shadowFadeBias)); if (m_ShadowCasterCascadesCount > 1) { cmd.SetGlobalVector(MainLightShadowConstantBuffer._CascadeShadowSplitSpheres0, m_CascadeSplitDistances[0]); cmd.SetGlobalVector(MainLightShadowConstantBuffer._CascadeShadowSplitSpheres1, m_CascadeSplitDistances[1]); cmd.SetGlobalVector(MainLightShadowConstantBuffer._CascadeShadowSplitSpheres2, m_CascadeSplitDistances[2]); cmd.SetGlobalVector(MainLightShadowConstantBuffer._CascadeShadowSplitSpheres3, m_CascadeSplitDistances[3]); cmd.SetGlobalVector(MainLightShadowConstantBuffer._CascadeShadowSplitSphereRadii, new Vector4( m_CascadeSplitDistances[0].w * m_CascadeSplitDistances[0].w, m_CascadeSplitDistances[1].w * m_CascadeSplitDistances[1].w, m_CascadeSplitDistances[2].w * m_CascadeSplitDistances[2].w, m_CascadeSplitDistances[3].w * m_CascadeSplitDistances[3].w)); } // Inside shader soft shadows are controlled through global keyword. // If any additional light has soft shadows it will force soft shadows on main light too. // As it is not trivial finding out which additional light has soft shadows, we will pass main light properties if soft shadows are supported. // This workaround will be removed once we will support soft shadows per light. if (shadowData.supportsSoftShadows) { cmd.SetGlobalVector(MainLightShadowConstantBuffer._ShadowOffset0, new Vector4(-invHalfShadowAtlasWidth, -invHalfShadowAtlasHeight, invHalfShadowAtlasWidth, -invHalfShadowAtlasHeight)); cmd.SetGlobalVector(MainLightShadowConstantBuffer._ShadowOffset1, new Vector4(-invHalfShadowAtlasWidth, invHalfShadowAtlasHeight, invHalfShadowAtlasWidth, invHalfShadowAtlasHeight)); cmd.SetGlobalVector(MainLightShadowConstantBuffer._ShadowmapSize, new Vector4(invShadowAtlasWidth, invShadowAtlasHeight, renderTargetWidth, renderTargetHeight)); } } private class PassData { internal MainLightShadowCasterPass pass; internal TextureHandle shadowmapTexture; internal RenderingData renderingData; internal int shadowmapID; internal bool emptyShadowmap; internal RendererListHandle[] shadowRendererListsHandle = new RendererListHandle[k_MaxCascades]; internal RendererList[] shadowRendererLists = new RendererList[k_MaxCascades]; } private void InitPassData(ref PassData passData, ref RenderingData renderingData) { passData.pass = this; passData.emptyShadowmap = m_CreateEmptyShadowmap; passData.shadowmapID = m_MainLightShadowmapID; passData.renderingData = renderingData; } void InitEmptyPassData(ref PassData passData, ref RenderingData renderingData) { passData.pass = this; passData.emptyShadowmap = m_CreateEmptyShadowmap; passData.shadowmapID = m_MainLightShadowmapID; passData.renderingData = renderingData; } private void InitRendererLists(ref RenderingData renderingData, ref PassData passData, ScriptableRenderContext context, RenderGraph renderGraph, bool useRenderGraph) { var cullResults = renderingData.cullResults; var lightData = renderingData.lightData; var shadowData = renderingData.shadowData; int shadowLightIndex = lightData.mainLightIndex; if (!m_CreateEmptyShadowmap && shadowLightIndex != -1) { var settings = new ShadowDrawingSettings(cullResults, shadowLightIndex); settings.useRenderingLayerMaskTest = UniversalRenderPipeline.asset.useRenderingLayers; for (int cascadeIndex = 0; cascadeIndex < m_ShadowCasterCascadesCount; ++cascadeIndex) { if (useRenderGraph) passData.shadowRendererListsHandle[cascadeIndex] = renderGraph.CreateShadowRendererList(ref settings); else passData.shadowRendererLists[cascadeIndex] = context.CreateShadowRendererList(ref settings); } } } internal TextureHandle Render(RenderGraph graph, ref RenderingData renderingData) { TextureHandle shadowTexture; using (var builder = graph.AddRasterRenderPass("Main Light Shadowmap", out var passData, base.profilingSampler)) { InitPassData(ref passData, ref renderingData); InitRendererLists(ref renderingData, ref passData, default(ScriptableRenderContext), graph, true); if (!m_CreateEmptyShadowmap) { for (int cascadeIndex = 0; cascadeIndex < m_ShadowCasterCascadesCount; ++cascadeIndex) { builder.UseRendererList(passData.shadowRendererListsHandle[cascadeIndex]); } passData.shadowmapTexture = UniversalRenderer.CreateRenderGraphTexture(graph, m_MainLightShadowmapTexture.rt.descriptor, "Main Shadowmap", true, ShadowUtils.m_ForceShadowPointSampling ? FilterMode.Point : FilterMode.Bilinear); builder.UseTextureFragmentDepth(passData.shadowmapTexture, IBaseRenderGraphBuilder.AccessFlags.Write); } // Need this as shadowmap is only used as Global Texture and not a buffer, so would get culled by RG builder.AllowPassCulling(false); builder.AllowGlobalStateModification(true); builder.SetRenderFunc((PassData data, RasterGraphContext context) => { if (!data.emptyShadowmap) data.pass.RenderMainLightCascadeShadowmap(context.cmd, ref data, ref data.renderingData, true); }); shadowTexture = passData.shadowmapTexture; } using (var builder = graph.AddRasterRenderPass("Set Main Shadow Globals", out var passData, base.profilingSampler)) { InitEmptyPassData(ref passData, ref renderingData); passData.shadowmapTexture = shadowTexture; if (shadowTexture.IsValid()) builder.UseTexture(shadowTexture, IBaseRenderGraphBuilder.AccessFlags.Read); builder.AllowPassCulling(false); builder.AllowGlobalStateModification(true); builder.SetRenderFunc((PassData data, RasterGraphContext context) => { if (data.emptyShadowmap) { data.pass.SetEmptyMainLightCascadeShadowmap(context.cmd); data.shadowmapTexture = context.defaultResources.defaultShadowTexture; } context.cmd.SetGlobalTexture(data.shadowmapID, data.shadowmapTexture); }); return passData.shadowmapTexture; } } }; }