FinalBlitPass.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. namespace UnityEngine.Rendering.Universal.Internal
  2. {
  3. /// <summary>
  4. /// Copy the given color target to the current camera target
  5. ///
  6. /// You can use this pass to copy the result of rendering to
  7. /// the camera target. The pass takes the screen viewport into
  8. /// consideration.
  9. /// </summary>
  10. public class FinalBlitPass : ScriptableRenderPass
  11. {
  12. const string m_ProfilerTag = "Final Blit Pass";
  13. RenderTargetHandle m_Source;
  14. Material m_BlitMaterial;
  15. TextureDimension m_TargetDimension;
  16. public FinalBlitPass(RenderPassEvent evt, Material blitMaterial)
  17. {
  18. m_BlitMaterial = blitMaterial;
  19. renderPassEvent = evt;
  20. }
  21. /// <summary>
  22. /// Configure the pass
  23. /// </summary>
  24. /// <param name="baseDescriptor"></param>
  25. /// <param name="colorHandle"></param>
  26. public void Setup(RenderTextureDescriptor baseDescriptor, RenderTargetHandle colorHandle)
  27. {
  28. m_Source = colorHandle;
  29. m_TargetDimension = baseDescriptor.dimension;
  30. }
  31. /// <inheritdoc/>
  32. public override void Execute(ScriptableRenderContext context, ref RenderingData renderingData)
  33. {
  34. if (m_BlitMaterial == null)
  35. {
  36. Debug.LogErrorFormat("Missing {0}. {1} render pass will not execute. Check for missing reference in the renderer resources.", m_BlitMaterial, GetType().Name);
  37. return;
  38. }
  39. // Note: We need to get the cameraData.targetTexture as this will get the targetTexture of the camera stack.
  40. // Overlay cameras need to output to the target described in the base camera while doing camera stack.
  41. ref CameraData cameraData = ref renderingData.cameraData;
  42. RenderTargetIdentifier cameraTarget = (cameraData.targetTexture != null) ? new RenderTargetIdentifier(cameraData.targetTexture) : BuiltinRenderTextureType.CameraTarget;
  43. bool requiresSRGBConvertion = Display.main.requiresSrgbBlitToBackbuffer;
  44. // For stereo case, eye texture always want color data in sRGB space.
  45. // If eye texture color format is linear, we do explicit sRGB convertion
  46. #if ENABLE_VR && ENABLE_VR_MODULE
  47. if (cameraData.isStereoEnabled)
  48. requiresSRGBConvertion = !XRGraphics.eyeTextureDesc.sRGB;
  49. #endif
  50. CommandBuffer cmd = CommandBufferPool.Get(m_ProfilerTag);
  51. if (requiresSRGBConvertion)
  52. cmd.EnableShaderKeyword(ShaderKeywordStrings.LinearToSRGBConversion);
  53. else
  54. cmd.DisableShaderKeyword(ShaderKeywordStrings.LinearToSRGBConversion);
  55. // Use default blit for XR as we are not sure the UniversalRP blit handles stereo.
  56. // The blit will be reworked for stereo along the XRSDK work.
  57. Material blitMaterial = (cameraData.isStereoEnabled) ? null : m_BlitMaterial;
  58. cmd.SetGlobalTexture("_BlitTex", m_Source.Identifier());
  59. if (cameraData.isStereoEnabled || cameraData.isSceneViewCamera || cameraData.isDefaultViewport)
  60. {
  61. // This set render target is necessary so we change the LOAD state to DontCare.
  62. cmd.SetRenderTarget(BuiltinRenderTextureType.CameraTarget,
  63. RenderBufferLoadAction.DontCare, RenderBufferStoreAction.Store, // color
  64. RenderBufferLoadAction.DontCare, RenderBufferStoreAction.DontCare); // depth
  65. cmd.Blit(m_Source.Identifier(), cameraTarget, blitMaterial);
  66. }
  67. else
  68. {
  69. // TODO: Final blit pass should always blit to backbuffer. The first time we do we don't need to Load contents to tile.
  70. // We need to keep in the pipeline of first render pass to each render target to propertly set load/store actions.
  71. // meanwhile we set to load so split screen case works.
  72. SetRenderTarget(
  73. cmd,
  74. cameraTarget,
  75. RenderBufferLoadAction.Load,
  76. RenderBufferStoreAction.Store,
  77. ClearFlag.None,
  78. Color.black,
  79. m_TargetDimension);
  80. Camera camera = cameraData.camera;
  81. cmd.SetViewProjectionMatrices(Matrix4x4.identity, Matrix4x4.identity);
  82. cmd.SetViewport(cameraData.pixelRect);
  83. cmd.DrawMesh(RenderingUtils.fullscreenMesh, Matrix4x4.identity, blitMaterial);
  84. cmd.SetViewProjectionMatrices(camera.worldToCameraMatrix, camera.projectionMatrix);
  85. }
  86. context.ExecuteCommandBuffer(cmd);
  87. CommandBufferPool.Release(cmd);
  88. }
  89. }
  90. }