No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

CapturePass.cs 3.6KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. using System;
  2. using System.Collections.Generic;
  3. using UnityEngine.Rendering.RenderGraphModule;
  4. namespace UnityEngine.Rendering.Universal
  5. {
  6. /// <summary>
  7. /// Let customizable actions inject commands to capture the camera output.
  8. ///
  9. /// You can use this pass to inject capture commands into a command buffer
  10. /// with the goal of having camera capture happening in external code.
  11. /// </summary>
  12. internal class CapturePass : ScriptableRenderPass
  13. {
  14. RTHandle m_CameraColorHandle;
  15. const string m_ProfilerTag = "Capture Pass";
  16. private static readonly ProfilingSampler m_ProfilingSampler = new ProfilingSampler(m_ProfilerTag);
  17. public CapturePass(RenderPassEvent evt)
  18. {
  19. base.profilingSampler = new ProfilingSampler(nameof(CapturePass));
  20. renderPassEvent = evt;
  21. }
  22. /// <inheritdoc/>
  23. [Obsolete(DeprecationMessage.CompatibilityScriptingAPIObsolete, false)]
  24. public override void Execute(ScriptableRenderContext context, ref RenderingData renderingData)
  25. {
  26. CommandBuffer cmdBuf = renderingData.commandBuffer;
  27. m_CameraColorHandle = renderingData.cameraData.renderer.GetCameraColorBackBuffer(cmdBuf);
  28. using (new ProfilingScope(cmdBuf, m_ProfilingSampler))
  29. {
  30. var colorAttachmentIdentifier = m_CameraColorHandle.nameID;
  31. var captureActions = renderingData.cameraData.captureActions;
  32. for (captureActions.Reset(); captureActions.MoveNext();)
  33. captureActions.Current(colorAttachmentIdentifier, renderingData.commandBuffer);
  34. }
  35. }
  36. private class UnsafePassData
  37. {
  38. internal TextureHandle source;
  39. public IEnumerator<Action<RenderTargetIdentifier, CommandBuffer>> captureActions;
  40. }
  41. const string k_UnsafePassName = "CapturePass (Render Graph Unsafe Pass)";
  42. // This function needs to add an unsafe render pass to Render Graph because a raster render pass, which is typically
  43. // used for rendering with Render Graph, cannot perform the texture readback operations performed with the command
  44. // buffer in CameraTextureProvider. Unsafe passes can do certain operations that raster render passes cannot do and
  45. // have access to the full command buffer API.
  46. public override void RecordRenderGraph(RenderGraph renderGraph, ContextContainer frameData)
  47. {
  48. UniversalResourceData resourceData = frameData.Get<UniversalResourceData>();
  49. UniversalCameraData cameraData = frameData.Get<UniversalCameraData>();
  50. using (var builder = renderGraph.AddUnsafePass<UnsafePassData>(k_UnsafePassName, out var passData, profilingSampler))
  51. {
  52. // Setup up the pass data with cameraColor, which has the correct orientation and position in a built player
  53. passData.source = resourceData.cameraColor;
  54. passData.captureActions = cameraData.captureActions;
  55. // Setup up the builder
  56. builder.AllowPassCulling(false);
  57. builder.UseTexture(resourceData.cameraColor);
  58. builder.SetRenderFunc((UnsafePassData data, UnsafeGraphContext unsafeContext) =>
  59. {
  60. var nativeCommandBuffer = CommandBufferHelpers.GetNativeCommandBuffer(unsafeContext.cmd);
  61. var captureActions = data.captureActions;
  62. for (data.captureActions.Reset(); data.captureActions.MoveNext();)
  63. captureActions.Current(data.source, nativeCommandBuffer);
  64. });
  65. }
  66. }
  67. }
  68. }