using System; using System.Collections.Generic; using System.ComponentModel; using UnityEngine.Scripting.APIUpdating; namespace UnityEngine.Rendering.Universal { /// /// Input requirements for ScriptableRenderPass. /// /// [Flags] public enum ScriptableRenderPassInput { None = 0, Depth = 1 << 0, Normal = 1 << 1, Color = 1 << 2, } // Note: Spaced built-in events so we can add events in between them // We need to leave room as we sort render passes based on event. // Users can also inject render pass events in a specific point by doing RenderPassEvent + offset /// /// Controls when the render pass executes. /// [MovedFrom("UnityEngine.Rendering.LWRP")] public enum RenderPassEvent { /// /// Executes a ScriptableRenderPass before rendering any other passes in the pipeline. /// Camera matrices and stereo rendering are not setup this point. /// You can use this to draw to custom input textures used later in the pipeline, f.ex LUT textures. /// BeforeRendering = 0, /// /// Executes a ScriptableRenderPass before rendering shadowmaps. /// Camera matrices and stereo rendering are not setup this point. /// BeforeRenderingShadows = 50, /// /// Executes a ScriptableRenderPass after rendering shadowmaps. /// Camera matrices and stereo rendering are not setup this point. /// AfterRenderingShadows = 100, /// /// Executes a ScriptableRenderPass before rendering prepasses, f.ex, depth prepass. /// Camera matrices and stereo rendering are already setup at this point. /// BeforeRenderingPrePasses = 150, [EditorBrowsable(EditorBrowsableState.Never)] [Obsolete("Obsolete, to match the capital from 'Prepass' to 'PrePass' (UnityUpgradable) -> BeforeRenderingPrePasses")] BeforeRenderingPrepasses = 151, /// /// Executes a ScriptableRenderPass after rendering prepasses, f.ex, depth prepass. /// Camera matrices and stereo rendering are already setup at this point. /// AfterRenderingPrePasses = 200, /// /// Executes a ScriptableRenderPass before rendering opaque objects. /// BeforeRenderingOpaques = 250, /// /// Executes a ScriptableRenderPass after rendering opaque objects. /// AfterRenderingOpaques = 300, /// /// Executes a ScriptableRenderPass before rendering the sky. /// BeforeRenderingSkybox = 350, /// /// Executes a ScriptableRenderPass after rendering the sky. /// AfterRenderingSkybox = 400, /// /// Executes a ScriptableRenderPass before rendering transparent objects. /// BeforeRenderingTransparents = 450, /// /// Executes a ScriptableRenderPass after rendering transparent objects. /// AfterRenderingTransparents = 500, /// /// Executes a ScriptableRenderPass before rendering post-processing effects. /// BeforeRenderingPostProcessing = 550, /// /// Executes a ScriptableRenderPass after rendering post-processing effects but before final blit, post-processing AA effects and color grading. /// AfterRenderingPostProcessing = 600, /// /// Executes a ScriptableRenderPass after rendering all effects. /// AfterRendering = 1000, } /// /// ScriptableRenderPass implements a logical rendering pass that can be used to extend Universal RP renderer. /// [MovedFrom("UnityEngine.Rendering.LWRP")] public abstract partial class ScriptableRenderPass { public RenderPassEvent renderPassEvent { get; set; } public RenderTargetIdentifier[] colorAttachments { get => m_ColorAttachments; } public RenderTargetIdentifier colorAttachment { get => m_ColorAttachments[0]; } public RenderTargetIdentifier depthAttachment { get => m_DepthAttachment; } /// /// The input requirements for the ScriptableRenderPass, which has been set using ConfigureInput /// /// public ScriptableRenderPassInput input { get => m_Input; } public ClearFlag clearFlag { get => m_ClearFlag; } public Color clearColor { get => m_ClearColor; } /// A ProfilingSampler for the entire pass. Used by higher level objects such as ScriptableRenderer etc. protected internal ProfilingSampler profilingSampler { get; set; } internal bool overrideCameraTarget { get; set; } internal bool isBlitRenderPass { get; set; } RenderTargetIdentifier[] m_ColorAttachments = new RenderTargetIdentifier[] {BuiltinRenderTextureType.CameraTarget}; RenderTargetIdentifier m_DepthAttachment = BuiltinRenderTextureType.CameraTarget; ScriptableRenderPassInput m_Input = ScriptableRenderPassInput.None; ClearFlag m_ClearFlag = ClearFlag.None; Color m_ClearColor = Color.black; public ScriptableRenderPass() { renderPassEvent = RenderPassEvent.AfterRenderingOpaques; m_ColorAttachments = new RenderTargetIdentifier[] {BuiltinRenderTextureType.CameraTarget, 0, 0, 0, 0, 0, 0, 0}; m_DepthAttachment = BuiltinRenderTextureType.CameraTarget; m_ClearFlag = ClearFlag.None; m_ClearColor = Color.black; overrideCameraTarget = false; isBlitRenderPass = false; profilingSampler = new ProfilingSampler(nameof(ScriptableRenderPass)); } /// /// Configures Input Requirements for this render pass. /// This method should be called inside ScriptableRendererFeature.AddRenderPasses. /// /// ScriptableRenderPassInput containing information about what requirements the pass needs. /// public void ConfigureInput(ScriptableRenderPassInput passInput) { m_Input = passInput; } /// /// Configures render targets for this render pass. Call this instead of CommandBuffer.SetRenderTarget. /// This method should be called inside Configure. /// /// Color attachment identifier. /// Depth attachment identifier. /// public void ConfigureTarget(RenderTargetIdentifier colorAttachment, RenderTargetIdentifier depthAttachment) { m_DepthAttachment = depthAttachment; ConfigureTarget(colorAttachment); } /// /// Configures render targets for this render pass. Call this instead of CommandBuffer.SetRenderTarget. /// This method should be called inside Configure. /// /// Color attachment identifier. /// Depth attachment identifier. /// public void ConfigureTarget(RenderTargetIdentifier[] colorAttachments, RenderTargetIdentifier depthAttachment) { overrideCameraTarget = true; uint nonNullColorBuffers = RenderingUtils.GetValidColorBufferCount(colorAttachments); if (nonNullColorBuffers > SystemInfo.supportedRenderTargetCount) Debug.LogError("Trying to set " + nonNullColorBuffers + " renderTargets, which is more than the maximum supported:" + SystemInfo.supportedRenderTargetCount); m_ColorAttachments = colorAttachments; m_DepthAttachment = depthAttachment; } /// /// Configures render targets for this render pass. Call this instead of CommandBuffer.SetRenderTarget. /// This method should be called inside Configure. /// /// Color attachment identifier. /// public void ConfigureTarget(RenderTargetIdentifier colorAttachment) { overrideCameraTarget = true; m_ColorAttachments[0] = colorAttachment; for (int i = 1; i < m_ColorAttachments.Length; ++i) m_ColorAttachments[i] = 0; } /// /// Configures render targets for this render pass. Call this instead of CommandBuffer.SetRenderTarget. /// This method should be called inside Configure. /// /// Color attachment identifier. /// public void ConfigureTarget(RenderTargetIdentifier[] colorAttachments) { ConfigureTarget(colorAttachments, BuiltinRenderTextureType.CameraTarget); } /// /// Configures clearing for the render targets for this render pass. Call this inside Configure. /// /// ClearFlag containing information about what targets to clear. /// Clear color. /// public void ConfigureClear(ClearFlag clearFlag, Color clearColor) { m_ClearFlag = clearFlag; m_ClearColor = clearColor; } /// /// This method is called by the renderer before rendering a camera /// Override this method if you need to to configure render targets and their clear state, and to create temporary render target textures. /// If a render pass doesn't override this method, this render pass renders to the active Camera's render target. /// You should never call CommandBuffer.SetRenderTarget. Instead call ConfigureTarget and ConfigureClear. /// /// CommandBuffer to enqueue rendering commands. This will be executed by the pipeline. /// Current rendering state information /// /// public virtual void OnCameraSetup(CommandBuffer cmd, ref RenderingData renderingData) {} /// /// This method is called by the renderer before executing the render pass. /// Override this method if you need to to configure render targets and their clear state, and to create temporary render target textures. /// If a render pass doesn't override this method, this render pass renders to the active Camera's render target. /// You should never call CommandBuffer.SetRenderTarget. Instead call ConfigureTarget and ConfigureClear. /// /// CommandBuffer to enqueue rendering commands. This will be executed by the pipeline. /// Render texture descriptor of the camera render target. /// /// public virtual void Configure(CommandBuffer cmd, RenderTextureDescriptor cameraTextureDescriptor) {} /// /// Called upon finish rendering a camera. You can use this callback to release any resources created /// by this render /// pass that need to be cleanup once camera has finished rendering. /// This method be called for all cameras in a camera stack. /// /// Use this CommandBuffer to cleanup any generated data public virtual void OnCameraCleanup(CommandBuffer cmd) { } /// /// Called upon finish rendering a camera stack. You can use this callback to release any resources created /// by this render pass that need to be cleanup once all cameras in the stack have finished rendering. /// This method will be called once after rendering the last camera in the camera stack. /// Cameras that don't have an explicit camera stack are also considered stacked rendering. /// In that case the Base camera is the first and last camera in the stack. /// /// Use this CommandBuffer to cleanup any generated data public virtual void OnFinishCameraStackRendering(CommandBuffer cmd) {} /// /// Execute the pass. This is where custom rendering occurs. Specific details are left to the implementation /// /// Use this render context to issue any draw commands during execution /// Current rendering state information public abstract void Execute(ScriptableRenderContext context, ref RenderingData renderingData); /// /// Add a blit command to the context for execution. This changes the active render target in the ScriptableRenderer to /// destination. /// /// Command buffer to record command for execution. /// Source texture or target identifier to blit from. /// Destination texture or target identifier to blit into. This becomes the renderer active render target. /// Material to use. /// Shader pass to use. Default is 0. /// public void Blit(CommandBuffer cmd, RenderTargetIdentifier source, RenderTargetIdentifier destination, Material material = null, int passIndex = 0) { ScriptableRenderer.SetRenderTarget(cmd, destination, BuiltinRenderTextureType.CameraTarget, clearFlag, clearColor); cmd.Blit(source, destination, material, passIndex); } /// /// Creates DrawingSettings based on current the rendering state. /// /// Shader pass tag to render. /// Current rendering state. /// Criteria to sort objects being rendered. /// /// public DrawingSettings CreateDrawingSettings(ShaderTagId shaderTagId, ref RenderingData renderingData, SortingCriteria sortingCriteria) { Camera camera = renderingData.cameraData.camera; SortingSettings sortingSettings = new SortingSettings(camera) { criteria = sortingCriteria }; DrawingSettings settings = new DrawingSettings(shaderTagId, sortingSettings) { perObjectData = renderingData.perObjectData, mainLightIndex = renderingData.lightData.mainLightIndex, enableDynamicBatching = renderingData.supportsDynamicBatching, // Disable instancing for preview cameras. This is consistent with the built-in forward renderer. Also fixes case 1127324. enableInstancing = camera.cameraType == CameraType.Preview ? false : true, }; return settings; } /// /// Creates DrawingSettings based on current rendering state. /// /// /// List of shader pass tag to render. /// Current rendering state. /// Criteria to sort objects being rendered. /// /// public DrawingSettings CreateDrawingSettings(List shaderTagIdList, ref RenderingData renderingData, SortingCriteria sortingCriteria) { if (shaderTagIdList == null || shaderTagIdList.Count == 0) { Debug.LogWarning("ShaderTagId list is invalid. DrawingSettings is created with default pipeline ShaderTagId"); return CreateDrawingSettings(new ShaderTagId("UniversalPipeline"), ref renderingData, sortingCriteria); } DrawingSettings settings = CreateDrawingSettings(shaderTagIdList[0], ref renderingData, sortingCriteria); for (int i = 1; i < shaderTagIdList.Count; ++i) settings.SetShaderPassName(i, shaderTagIdList[i]); return settings; } public static bool operator<(ScriptableRenderPass lhs, ScriptableRenderPass rhs) { return lhs.renderPassEvent < rhs.renderPassEvent; } public static bool operator>(ScriptableRenderPass lhs, ScriptableRenderPass rhs) { return lhs.renderPassEvent > rhs.renderPassEvent; } } }