ScreenSpaceShadowResolvePass.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. using System;
  2. using UnityEngine.Experimental.Rendering;
  3. namespace UnityEngine.Rendering.Universal.Internal
  4. {
  5. /// <summary>
  6. /// Resolves shadows in a screen space texture.
  7. /// </summary>
  8. public class ScreenSpaceShadowResolvePass : ScriptableRenderPass
  9. {
  10. Material m_ScreenSpaceShadowsMaterial;
  11. RenderTargetHandle m_ScreenSpaceShadowmap;
  12. RenderTextureDescriptor m_RenderTextureDescriptor;
  13. public ScreenSpaceShadowResolvePass(RenderPassEvent evt, Material screenspaceShadowsMaterial)
  14. {
  15. base.profilingSampler = new ProfilingSampler(nameof(ScreenSpaceShadowResolvePass));
  16. m_ScreenSpaceShadowsMaterial = screenspaceShadowsMaterial;
  17. m_ScreenSpaceShadowmap.Init("_ScreenSpaceShadowmapTexture");
  18. renderPassEvent = evt;
  19. }
  20. public void Setup(RenderTextureDescriptor baseDescriptor)
  21. {
  22. m_RenderTextureDescriptor = baseDescriptor;
  23. m_RenderTextureDescriptor.depthBufferBits = 0;
  24. m_RenderTextureDescriptor.msaaSamples = 1;
  25. m_RenderTextureDescriptor.graphicsFormat = RenderingUtils.SupportsGraphicsFormat(GraphicsFormat.R8_UNorm, FormatUsage.Linear | FormatUsage.Render)
  26. ? GraphicsFormat.R8_UNorm
  27. : GraphicsFormat.B8G8R8A8_UNorm;
  28. }
  29. public override void Configure(CommandBuffer cmd, RenderTextureDescriptor cameraTextureDescriptor)
  30. {
  31. cmd.GetTemporaryRT(m_ScreenSpaceShadowmap.id, m_RenderTextureDescriptor, FilterMode.Bilinear);
  32. RenderTargetIdentifier screenSpaceOcclusionTexture = m_ScreenSpaceShadowmap.Identifier();
  33. ConfigureTarget(screenSpaceOcclusionTexture);
  34. ConfigureClear(ClearFlag.All, Color.white);
  35. }
  36. /// <inheritdoc/>
  37. public override void Execute(ScriptableRenderContext context, ref RenderingData renderingData)
  38. {
  39. if (m_ScreenSpaceShadowsMaterial == null)
  40. {
  41. Debug.LogErrorFormat("Missing {0}. {1} render pass will not execute. Check for missing reference in the renderer resources.", m_ScreenSpaceShadowsMaterial, GetType().Name);
  42. return;
  43. }
  44. if (renderingData.lightData.mainLightIndex == -1)
  45. return;
  46. Camera camera = renderingData.cameraData.camera;
  47. CommandBuffer cmd = CommandBufferPool.Get();
  48. using (new ProfilingScope(cmd, ProfilingSampler.Get(URPProfileId.ResolveShadows)))
  49. {
  50. if (!renderingData.cameraData.xr.enabled)
  51. {
  52. cmd.SetViewProjectionMatrices(Matrix4x4.identity, Matrix4x4.identity);
  53. cmd.DrawMesh(RenderingUtils.fullscreenMesh, Matrix4x4.identity, m_ScreenSpaceShadowsMaterial);
  54. cmd.SetViewProjectionMatrices(camera.worldToCameraMatrix, camera.projectionMatrix);
  55. }
  56. else
  57. {
  58. // Avoid setting and restoring camera view and projection matrices when in stereo.
  59. RenderTargetIdentifier screenSpaceOcclusionTexture = m_ScreenSpaceShadowmap.Identifier();
  60. Blit(cmd, screenSpaceOcclusionTexture, screenSpaceOcclusionTexture, m_ScreenSpaceShadowsMaterial);
  61. }
  62. }
  63. context.ExecuteCommandBuffer(cmd);
  64. CommandBufferPool.Release(cmd);
  65. }
  66. /// <inheritdoc/>
  67. public override void OnCameraCleanup(CommandBuffer cmd)
  68. {
  69. if (cmd == null)
  70. throw new ArgumentNullException("cmd");
  71. cmd.ReleaseTemporaryRT(m_ScreenSpaceShadowmap.id);
  72. }
  73. }
  74. }