BlitPass.cs 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. namespace UnityEngine.Rendering.LWRP
  2. {
  3. /// <summary>
  4. /// Copy the given color buffer to the given destination color buffer.
  5. ///
  6. /// You can use this pass to copy a color buffer to the destination,
  7. /// so you can use it later in rendering. For example, you can copy
  8. /// the opaque texture to use it for distortion effects.
  9. /// </summary>
  10. internal class BlitPass : UnityEngine.Rendering.Universal.ScriptableRenderPass
  11. {
  12. public enum RenderTarget
  13. {
  14. Color,
  15. RenderTexture,
  16. }
  17. public Material blitMaterial = null;
  18. public int blitShaderPassIndex = 0;
  19. public FilterMode filterMode { get; set; }
  20. private RenderTargetIdentifier source { get; set; }
  21. private UnityEngine.Rendering.Universal.RenderTargetHandle destination { get; set; }
  22. UnityEngine.Rendering.Universal.RenderTargetHandle m_TemporaryColorTexture;
  23. string m_ProfilerTag;
  24. /// <summary>
  25. /// Create the CopyColorPass
  26. /// </summary>
  27. public BlitPass(UnityEngine.Rendering.Universal.RenderPassEvent renderPassEvent, Material blitMaterial, int blitShaderPassIndex, string tag)
  28. {
  29. this.renderPassEvent = renderPassEvent;
  30. this.blitMaterial = blitMaterial;
  31. this.blitShaderPassIndex = blitShaderPassIndex;
  32. m_ProfilerTag = tag;
  33. m_TemporaryColorTexture.Init("_TemporaryColorTexture");
  34. }
  35. /// <summary>
  36. /// Configure the pass with the source and destination to execute on.
  37. /// </summary>
  38. /// <param name="source">Source Render Target</param>
  39. /// <param name="destination">Destination Render Target</param>
  40. public void Setup(RenderTargetIdentifier source, UnityEngine.Rendering.Universal.RenderTargetHandle destination)
  41. {
  42. this.source = source;
  43. this.destination = destination;
  44. }
  45. /// <inheritdoc/>
  46. public override void Execute(ScriptableRenderContext context, ref UnityEngine.Rendering.Universal.RenderingData renderingData)
  47. {
  48. CommandBuffer cmd = CommandBufferPool.Get(m_ProfilerTag);
  49. RenderTextureDescriptor opaqueDesc = renderingData.cameraData.cameraTargetDescriptor;
  50. opaqueDesc.depthBufferBits = 0;
  51. // Can't read and write to same color target, create a temp render target to blit.
  52. if (destination == UnityEngine.Rendering.Universal.RenderTargetHandle.CameraTarget)
  53. {
  54. cmd.GetTemporaryRT(m_TemporaryColorTexture.id, opaqueDesc, filterMode);
  55. Blit(cmd, source, m_TemporaryColorTexture.Identifier(), blitMaterial, blitShaderPassIndex);
  56. Blit(cmd, m_TemporaryColorTexture.Identifier(), source);
  57. }
  58. else
  59. {
  60. Blit(cmd, source, destination.Identifier(), blitMaterial, blitShaderPassIndex);
  61. }
  62. context.ExecuteCommandBuffer(cmd);
  63. CommandBufferPool.Release(cmd);
  64. }
  65. /// <inheritdoc/>
  66. public override void FrameCleanup(CommandBuffer cmd)
  67. {
  68. if (destination == UnityEngine.Rendering.Universal.RenderTargetHandle.CameraTarget)
  69. cmd.ReleaseTemporaryRT(m_TemporaryColorTexture.id);
  70. }
  71. }
  72. }