using UnityEngine.Rendering; namespace UnityEngine.Experimental.Rendering.LightweightPipeline { public class ScreenSpaceShadowResolvePass : ScriptableRenderPass { RenderTextureFormat m_ColorFormat; Material m_ScreenSpaceShadowsMaterial; public ScreenSpaceShadowResolvePass(LightweightForwardRenderer renderer) : base(renderer) { m_ColorFormat = SystemInfo.SupportsRenderTextureFormat(RenderTextureFormat.R8) ? RenderTextureFormat.R8 : RenderTextureFormat.ARGB32; m_ScreenSpaceShadowsMaterial = renderer.GetMaterial(MaterialHandles.ScrenSpaceShadow); } public override void Setup(CommandBuffer cmd, RenderTextureDescriptor baseDescriptor, int[] colorAttachmentHandles, int depthAttachmentHandle = -1, int samples = 1) { base.Setup(cmd, baseDescriptor, colorAttachmentHandles, depthAttachmentHandle, samples); baseDescriptor.depthBufferBits = 0; baseDescriptor.colorFormat = m_ColorFormat; cmd.GetTemporaryRT(colorAttachmentHandle, baseDescriptor, FilterMode.Bilinear); } public override void Execute(ref ScriptableRenderContext context, ref CullResults cullResults, ref RenderingData renderingData) { if (renderingData.shadowData.renderedDirectionalShadowQuality == LightShadows.None) return; CommandBuffer cmd = CommandBufferPool.Get("Collect Shadows"); SetShadowCollectPassKeywords(cmd, ref renderingData.shadowData); // Note: The source isn't actually 'used', but there's an engine peculiarity (bug) that // doesn't like null sources when trying to determine a stereo-ized blit. So for proper // stereo functionality, we use the screen-space shadow map as the source (until we have // a better solution). // An alternative would be DrawProcedural, but that would require further changes in the shader. RenderTargetIdentifier screenSpaceOcclusionTexture = GetSurface(colorAttachmentHandle); SetRenderTarget(cmd, screenSpaceOcclusionTexture, RenderBufferLoadAction.DontCare, RenderBufferStoreAction.Store, ClearFlag.Color | ClearFlag.Depth, Color.white); cmd.Blit(screenSpaceOcclusionTexture, screenSpaceOcclusionTexture, m_ScreenSpaceShadowsMaterial); if (renderingData.cameraData.isStereoEnabled) { Camera camera = renderingData.cameraData.camera; context.StartMultiEye(camera); context.ExecuteCommandBuffer(cmd); context.StopMultiEye(camera); } else context.ExecuteCommandBuffer(cmd); CommandBufferPool.Release(cmd); } void SetShadowCollectPassKeywords(CommandBuffer cmd, ref ShadowData shadowData) { CoreUtils.SetKeyword(cmd, LightweightKeywordStrings.SoftShadows, shadowData.renderedDirectionalShadowQuality == LightShadows.Soft); CoreUtils.SetKeyword(cmd, LightweightKeywordStrings.CascadeShadows, shadowData.directionalLightCascadeCount > 1); } } }