暂无描述
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

UpscalePass.cs 3.2KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. using System;
  2. using UnityEngine.Experimental.Rendering;
  3. using UnityEngine.Rendering.RenderGraphModule;
  4. namespace UnityEngine.Rendering.Universal
  5. {
  6. internal class UpscalePass : ScriptableRenderPass
  7. {
  8. static readonly string k_UpscalePass = "Upscale2D Pass";
  9. private static readonly ProfilingSampler m_ProfilingSampler = new ProfilingSampler(k_UpscalePass);
  10. private static readonly ProfilingSampler m_ExecuteProfilingSampler = new ProfilingSampler("Draw Upscale");
  11. static Material m_BlitMaterial;
  12. private RTHandle source;
  13. private RTHandle destination;
  14. private class PassData
  15. {
  16. internal TextureHandle source;
  17. }
  18. public UpscalePass(RenderPassEvent evt, Material blitMaterial)
  19. {
  20. renderPassEvent = evt;
  21. m_BlitMaterial = blitMaterial;
  22. }
  23. public void Setup(RTHandle colorTargetHandle, int width, int height, FilterMode mode, RenderTextureDescriptor cameraTargetDescriptor, out RTHandle upscaleHandle)
  24. {
  25. source = colorTargetHandle;
  26. RenderTextureDescriptor desc = cameraTargetDescriptor;
  27. desc.width = width;
  28. desc.height = height;
  29. desc.depthBufferBits = 0;
  30. RenderingUtils.ReAllocateHandleIfNeeded(ref destination, desc, mode, TextureWrapMode.Clamp, name: "_UpscaleTexture");
  31. upscaleHandle = destination;
  32. }
  33. public void Dispose()
  34. {
  35. destination?.Release();
  36. }
  37. [Obsolete(DeprecationMessage.CompatibilityScriptingAPIObsolete, false)]
  38. public override void Execute(ScriptableRenderContext context, ref RenderingData renderingData)
  39. {
  40. var cmd = renderingData.commandBuffer;
  41. cmd.SetRenderTarget(destination);
  42. ExecutePass(CommandBufferHelpers.GetRasterCommandBuffer(cmd), source);
  43. }
  44. private static void ExecutePass(RasterCommandBuffer cmd, RTHandle source)
  45. {
  46. using (new ProfilingScope(cmd, m_ExecuteProfilingSampler))
  47. {
  48. Vector2 viewportScale = source.useScaling ? new Vector2(source.rtHandleProperties.rtHandleScale.x, source.rtHandleProperties.rtHandleScale.y) : Vector2.one;
  49. Blitter.BlitTexture(cmd, source, viewportScale, m_BlitMaterial, source.rt.filterMode == FilterMode.Bilinear ? 1 : 0);
  50. }
  51. }
  52. public void Render(RenderGraph graph, Camera camera, in TextureHandle cameraColorAttachment, in TextureHandle upscaleHandle)
  53. {
  54. camera.TryGetComponent<PixelPerfectCamera>(out var ppc);
  55. if (ppc == null || !ppc.enabled || !ppc.requiresUpscalePass)
  56. return;
  57. using (var builder = graph.AddRasterRenderPass<PassData>(k_UpscalePass, out var passData, m_ProfilingSampler))
  58. {
  59. passData.source = cameraColorAttachment;
  60. builder.SetRenderAttachment(upscaleHandle, 0);
  61. builder.UseTexture(cameraColorAttachment);
  62. builder.AllowPassCulling(false);
  63. builder.SetRenderFunc((PassData data, RasterGraphContext context) =>
  64. {
  65. ExecutePass(context.cmd, data.source);
  66. });
  67. }
  68. }
  69. }
  70. }