ScriptableRenderPass.cs 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.ComponentModel;
  5. using Unity.Collections;
  6. using UnityEngine.Scripting.APIUpdating;
  7. using UnityEngine.Experimental.Rendering;
  8. namespace UnityEngine.Rendering.Universal
  9. {
  10. /// <summary>
  11. /// Input requirements for <c>ScriptableRenderPass</c>.
  12. /// </summary>
  13. /// <seealso cref="ConfigureInput"/>
  14. [Flags]
  15. public enum ScriptableRenderPassInput
  16. {
  17. None = 0,
  18. Depth = 1 << 0,
  19. Normal = 1 << 1,
  20. Color = 1 << 2,
  21. Motion = 1 << 3
  22. }
  23. // Note: Spaced built-in events so we can add events in between them
  24. // We need to leave room as we sort render passes based on event.
  25. // Users can also inject render pass events in a specific point by doing RenderPassEvent + offset
  26. /// <summary>
  27. /// Controls when the render pass executes.
  28. /// </summary>
  29. public enum RenderPassEvent
  30. {
  31. /// <summary>
  32. /// Executes a <c>ScriptableRenderPass</c> before rendering any other passes in the pipeline.
  33. /// Camera matrices and stereo rendering are not setup this point.
  34. /// You can use this to draw to custom input textures used later in the pipeline, f.ex LUT textures.
  35. /// </summary>
  36. BeforeRendering = 0,
  37. /// <summary>
  38. /// Executes a <c>ScriptableRenderPass</c> before rendering shadowmaps.
  39. /// Camera matrices and stereo rendering are not setup this point.
  40. /// </summary>
  41. BeforeRenderingShadows = 50,
  42. /// <summary>
  43. /// Executes a <c>ScriptableRenderPass</c> after rendering shadowmaps.
  44. /// Camera matrices and stereo rendering are not setup this point.
  45. /// </summary>
  46. AfterRenderingShadows = 100,
  47. /// <summary>
  48. /// Executes a <c>ScriptableRenderPass</c> before rendering prepasses, f.ex, depth prepass.
  49. /// Camera matrices and stereo rendering are already setup at this point.
  50. /// </summary>
  51. BeforeRenderingPrePasses = 150,
  52. [EditorBrowsable(EditorBrowsableState.Never)]
  53. [Obsolete("Obsolete, to match the capital from 'Prepass' to 'PrePass' (UnityUpgradable) -> BeforeRenderingPrePasses")]
  54. BeforeRenderingPrepasses = 151,
  55. /// <summary>
  56. /// Executes a <c>ScriptableRenderPass</c> after rendering prepasses, f.ex, depth prepass.
  57. /// Camera matrices and stereo rendering are already setup at this point.
  58. /// </summary>
  59. AfterRenderingPrePasses = 200,
  60. /// <summary>
  61. /// Executes a <c>ScriptableRenderPass</c> before rendering gbuffer pass.
  62. /// </summary>
  63. BeforeRenderingGbuffer = 210,
  64. /// <summary>
  65. /// Executes a <c>ScriptableRenderPass</c> after rendering gbuffer pass.
  66. /// </summary>
  67. AfterRenderingGbuffer = 220,
  68. /// <summary>
  69. /// Executes a <c>ScriptableRenderPass</c> before rendering deferred shading pass.
  70. /// </summary>
  71. BeforeRenderingDeferredLights = 230,
  72. /// <summary>
  73. /// Executes a <c>ScriptableRenderPass</c> after rendering deferred shading pass.
  74. /// </summary>
  75. AfterRenderingDeferredLights = 240,
  76. /// <summary>
  77. /// Executes a <c>ScriptableRenderPass</c> before rendering opaque objects.
  78. /// </summary>
  79. BeforeRenderingOpaques = 250,
  80. /// <summary>
  81. /// Executes a <c>ScriptableRenderPass</c> after rendering opaque objects.
  82. /// </summary>
  83. AfterRenderingOpaques = 300,
  84. /// <summary>
  85. /// Executes a <c>ScriptableRenderPass</c> before rendering the sky.
  86. /// </summary>
  87. BeforeRenderingSkybox = 350,
  88. /// <summary>
  89. /// Executes a <c>ScriptableRenderPass</c> after rendering the sky.
  90. /// </summary>
  91. AfterRenderingSkybox = 400,
  92. /// <summary>
  93. /// Executes a <c>ScriptableRenderPass</c> before rendering transparent objects.
  94. /// </summary>
  95. BeforeRenderingTransparents = 450,
  96. /// <summary>
  97. /// Executes a <c>ScriptableRenderPass</c> after rendering transparent objects.
  98. /// </summary>
  99. AfterRenderingTransparents = 500,
  100. /// <summary>
  101. /// Executes a <c>ScriptableRenderPass</c> before rendering post-processing effects.
  102. /// </summary>
  103. BeforeRenderingPostProcessing = 550,
  104. /// <summary>
  105. /// Executes a <c>ScriptableRenderPass</c> after rendering post-processing effects but before final blit, post-processing AA effects and color grading.
  106. /// </summary>
  107. AfterRenderingPostProcessing = 600,
  108. /// <summary>
  109. /// Executes a <c>ScriptableRenderPass</c> after rendering all effects.
  110. /// </summary>
  111. AfterRendering = 1000,
  112. }
  113. /// <summary>
  114. /// <c>ScriptableRenderPass</c> implements a logical rendering pass that can be used to extend Universal RP renderer.
  115. /// </summary>
  116. public abstract partial class ScriptableRenderPass
  117. {
  118. public RenderPassEvent renderPassEvent { get; set; }
  119. public RenderTargetIdentifier[] colorAttachments
  120. {
  121. get => m_ColorAttachments;
  122. }
  123. public RenderTargetIdentifier colorAttachment
  124. {
  125. get => m_ColorAttachments[0];
  126. }
  127. public RenderTargetIdentifier depthAttachment
  128. {
  129. get => m_DepthAttachment;
  130. }
  131. public RenderBufferStoreAction[] colorStoreActions
  132. {
  133. get => m_ColorStoreActions;
  134. }
  135. public RenderBufferStoreAction depthStoreAction
  136. {
  137. get => m_DepthStoreAction;
  138. }
  139. internal bool[] overriddenColorStoreActions
  140. {
  141. get => m_OverriddenColorStoreActions;
  142. }
  143. internal bool overriddenDepthStoreAction
  144. {
  145. get => m_OverriddenDepthStoreAction;
  146. }
  147. /// <summary>
  148. /// The input requirements for the <c>ScriptableRenderPass</c>, which has been set using <c>ConfigureInput</c>
  149. /// </summary>
  150. /// <seealso cref="ConfigureInput"/>
  151. public ScriptableRenderPassInput input
  152. {
  153. get => m_Input;
  154. }
  155. public ClearFlag clearFlag
  156. {
  157. get => m_ClearFlag;
  158. }
  159. public Color clearColor
  160. {
  161. get => m_ClearColor;
  162. }
  163. RenderBufferStoreAction[] m_ColorStoreActions = new RenderBufferStoreAction[] { RenderBufferStoreAction.Store };
  164. RenderBufferStoreAction m_DepthStoreAction = RenderBufferStoreAction.Store;
  165. // by default all store actions are Store. The overridden flags are used to keep track of explicitly requested store actions, to
  166. // help figuring out the correct final store action for merged render passes when using the RenderPass API.
  167. private bool[] m_OverriddenColorStoreActions = new bool[] { false };
  168. private bool m_OverriddenDepthStoreAction = false;
  169. /// <summary>
  170. /// A ProfilingSampler for the entire render pass. Used as a profiling name by <c>ScriptableRenderer</c> when executing the pass.
  171. /// Default is <c>Unnamed_ScriptableRenderPass</c>.
  172. /// Set <c>base.profilingSampler</c> from the sub-class constructor to set a profiling name for a custom <c>ScriptableRenderPass</c>.
  173. /// </summary>
  174. protected internal ProfilingSampler profilingSampler { get; set; }
  175. internal bool overrideCameraTarget { get; set; }
  176. internal bool isBlitRenderPass { get; set; }
  177. internal bool useNativeRenderPass { get; set; }
  178. internal int renderTargetWidth { get; set; }
  179. internal int renderTargetHeight { get; set; }
  180. internal int renderTargetSampleCount { get; set; }
  181. internal bool depthOnly { get; set; }
  182. // this flag is updated each frame to keep track of which pass is the last for the current camera
  183. internal bool isLastPass { get; set; }
  184. // index to track the position in the current frame
  185. internal int renderPassQueueIndex { get; set; }
  186. internal NativeArray<int> m_ColorAttachmentIndices;
  187. internal NativeArray<int> m_InputAttachmentIndices;
  188. internal GraphicsFormat[] renderTargetFormat { get; set; }
  189. RenderTargetIdentifier[] m_ColorAttachments = new RenderTargetIdentifier[] { BuiltinRenderTextureType.CameraTarget };
  190. internal RenderTargetIdentifier[] m_InputAttachments = new RenderTargetIdentifier[8];
  191. internal bool[] m_InputAttachmentIsTransient = new bool[8];
  192. RenderTargetIdentifier m_DepthAttachment = BuiltinRenderTextureType.CameraTarget;
  193. ScriptableRenderPassInput m_Input = ScriptableRenderPassInput.None;
  194. ClearFlag m_ClearFlag = ClearFlag.None;
  195. Color m_ClearColor = Color.black;
  196. internal DebugHandler GetActiveDebugHandler(RenderingData renderingData)
  197. {
  198. var debugHandler = renderingData.cameraData.renderer.DebugHandler;
  199. if ((debugHandler != null) && debugHandler.IsActiveForCamera(ref renderingData.cameraData))
  200. return debugHandler;
  201. return null;
  202. }
  203. public ScriptableRenderPass()
  204. {
  205. renderPassEvent = RenderPassEvent.AfterRenderingOpaques;
  206. m_ColorAttachments = new RenderTargetIdentifier[] { BuiltinRenderTextureType.CameraTarget, 0, 0, 0, 0, 0, 0, 0 };
  207. m_InputAttachments = new RenderTargetIdentifier[] { -1, -1, -1, -1, -1, -1, -1, -1 };
  208. m_InputAttachmentIsTransient = new bool[] { false, false, false, false, false, false, false, false };
  209. m_DepthAttachment = BuiltinRenderTextureType.CameraTarget;
  210. m_ColorStoreActions = new RenderBufferStoreAction[] { RenderBufferStoreAction.Store, 0, 0, 0, 0, 0, 0, 0 };
  211. m_DepthStoreAction = RenderBufferStoreAction.Store;
  212. m_OverriddenColorStoreActions = new bool[] { false, false, false, false, false, false, false, false };
  213. m_OverriddenDepthStoreAction = false;
  214. m_ClearFlag = ClearFlag.None;
  215. m_ClearColor = Color.black;
  216. overrideCameraTarget = false;
  217. isBlitRenderPass = false;
  218. profilingSampler = new ProfilingSampler($"Unnamed_{nameof(ScriptableRenderPass)}");
  219. useNativeRenderPass = true;
  220. renderTargetWidth = -1;
  221. renderTargetHeight = -1;
  222. renderTargetSampleCount = -1;
  223. renderPassQueueIndex = -1;
  224. renderTargetFormat = new GraphicsFormat[]
  225. {
  226. GraphicsFormat.None, GraphicsFormat.None, GraphicsFormat.None,
  227. GraphicsFormat.None, GraphicsFormat.None, GraphicsFormat.None, GraphicsFormat.None, GraphicsFormat.None
  228. };
  229. depthOnly = false;
  230. }
  231. /// <summary>
  232. /// Configures Input Requirements for this render pass.
  233. /// This method should be called inside <c>ScriptableRendererFeature.AddRenderPasses</c>.
  234. /// </summary>
  235. /// <param name="passInput">ScriptableRenderPassInput containing information about what requirements the pass needs.</param>
  236. /// <seealso cref="ScriptableRendererFeature.AddRenderPasses"/>
  237. public void ConfigureInput(ScriptableRenderPassInput passInput)
  238. {
  239. m_Input = passInput;
  240. }
  241. /// <summary>
  242. /// Configures the Store Action for a color attachment of this render pass.
  243. /// </summary>
  244. /// <param name="storeAction">RenderBufferStoreAction to use</param>
  245. /// <param name="attachmentIndex">Index of the color attachment</param>
  246. public void ConfigureColorStoreAction(RenderBufferStoreAction storeAction, uint attachmentIndex = 0)
  247. {
  248. m_ColorStoreActions[attachmentIndex] = storeAction;
  249. m_OverriddenColorStoreActions[attachmentIndex] = true;
  250. }
  251. /// <summary>
  252. /// Configures the Store Actions for all the color attachments of this render pass.
  253. /// </summary>
  254. /// <param name="storeActions">Array of RenderBufferStoreActions to use</param>
  255. public void ConfigureColorStoreActions(RenderBufferStoreAction[] storeActions)
  256. {
  257. int count = Math.Min(storeActions.Length, m_ColorStoreActions.Length);
  258. for (uint i = 0; i < count; ++i)
  259. {
  260. m_ColorStoreActions[i] = storeActions[i];
  261. m_OverriddenColorStoreActions[i] = true;
  262. }
  263. }
  264. /// <summary>
  265. /// Configures the Store Action for the depth attachment of this render pass.
  266. /// </summary>
  267. /// <param name="storeAction">RenderBufferStoreAction to use</param>
  268. public void ConfigureDepthStoreAction(RenderBufferStoreAction storeAction)
  269. {
  270. m_DepthStoreAction = storeAction;
  271. m_OverriddenDepthStoreAction = true;
  272. }
  273. internal void ConfigureInputAttachments(RenderTargetIdentifier input, bool isTransient = false)
  274. {
  275. m_InputAttachments[0] = input;
  276. m_InputAttachmentIsTransient[0] = isTransient;
  277. }
  278. internal void ConfigureInputAttachments(RenderTargetIdentifier[] inputs)
  279. {
  280. m_InputAttachments = inputs;
  281. }
  282. internal void ConfigureInputAttachments(RenderTargetIdentifier[] inputs, bool[] isTransient)
  283. {
  284. ConfigureInputAttachments(inputs);
  285. m_InputAttachmentIsTransient = isTransient;
  286. }
  287. internal void SetInputAttachmentTransient(int idx, bool isTransient)
  288. {
  289. m_InputAttachmentIsTransient[idx] = isTransient;
  290. }
  291. internal bool IsInputAttachmentTransient(int idx)
  292. {
  293. return m_InputAttachmentIsTransient[idx];
  294. }
  295. /// <summary>
  296. /// Configures render targets for this render pass. Call this instead of CommandBuffer.SetRenderTarget.
  297. /// This method should be called inside Configure.
  298. /// </summary>
  299. /// <param name="colorAttachment">Color attachment identifier.</param>
  300. /// <param name="depthAttachment">Depth attachment identifier.</param>
  301. /// <seealso cref="Configure"/>
  302. public void ConfigureTarget(RenderTargetIdentifier colorAttachment, RenderTargetIdentifier depthAttachment)
  303. {
  304. m_DepthAttachment = depthAttachment;
  305. ConfigureTarget(colorAttachment);
  306. }
  307. internal void ConfigureTarget(RenderTargetIdentifier colorAttachment, RenderTargetIdentifier depthAttachment, GraphicsFormat format)
  308. {
  309. m_DepthAttachment = depthAttachment;
  310. ConfigureTarget(colorAttachment, format);
  311. }
  312. /// <summary>
  313. /// Configures render targets for this render pass. Call this instead of CommandBuffer.SetRenderTarget.
  314. /// This method should be called inside Configure.
  315. /// </summary>
  316. /// <param name="colorAttachment">Color attachment identifier.</param>
  317. /// <param name="depthAttachment">Depth attachment identifier.</param>
  318. /// <seealso cref="Configure"/>
  319. public void ConfigureTarget(RenderTargetIdentifier[] colorAttachments, RenderTargetIdentifier depthAttachment)
  320. {
  321. overrideCameraTarget = true;
  322. uint nonNullColorBuffers = RenderingUtils.GetValidColorBufferCount(colorAttachments);
  323. if (nonNullColorBuffers > SystemInfo.supportedRenderTargetCount)
  324. Debug.LogError("Trying to set " + nonNullColorBuffers + " renderTargets, which is more than the maximum supported:" + SystemInfo.supportedRenderTargetCount);
  325. m_ColorAttachments = colorAttachments;
  326. m_DepthAttachment = depthAttachment;
  327. }
  328. internal void ConfigureTarget(RenderTargetIdentifier[] colorAttachments, RenderTargetIdentifier depthAttachment, GraphicsFormat[] formats)
  329. {
  330. ConfigureTarget(colorAttachments, depthAttachment);
  331. for (int i = 0; i < formats.Length; ++i)
  332. renderTargetFormat[i] = formats[i];
  333. }
  334. /// <summary>
  335. /// Configures render targets for this render pass. Call this instead of CommandBuffer.SetRenderTarget.
  336. /// This method should be called inside Configure.
  337. /// </summary>
  338. /// <param name="colorAttachment">Color attachment identifier.</param>
  339. /// <seealso cref="Configure"/>
  340. public void ConfigureTarget(RenderTargetIdentifier colorAttachment)
  341. {
  342. overrideCameraTarget = true;
  343. m_ColorAttachments[0] = colorAttachment;
  344. for (int i = 1; i < m_ColorAttachments.Length; ++i)
  345. m_ColorAttachments[i] = 0;
  346. }
  347. internal void ConfigureTarget(RenderTargetIdentifier colorAttachment, GraphicsFormat format, int width = -1, int height = -1, int sampleCount = -1, bool depth = false)
  348. {
  349. ConfigureTarget(colorAttachment);
  350. for (int i = 1; i < m_ColorAttachments.Length; ++i)
  351. renderTargetFormat[i] = GraphicsFormat.None;
  352. if (depth == true && !GraphicsFormatUtility.IsDepthFormat(format))
  353. {
  354. throw new ArgumentException("When configuring a depth only target the passed in format must be a depth format.");
  355. }
  356. renderTargetWidth = width;
  357. renderTargetHeight = height;
  358. renderTargetSampleCount = sampleCount;
  359. depthOnly = depth;
  360. renderTargetFormat[0] = format;
  361. }
  362. /// <summary>
  363. /// Configures render targets for this render pass. Call this instead of CommandBuffer.SetRenderTarget.
  364. /// This method should be called inside Configure.
  365. /// </summary>
  366. /// <param name="colorAttachment">Color attachment identifier.</param>
  367. /// <seealso cref="Configure"/>
  368. public void ConfigureTarget(RenderTargetIdentifier[] colorAttachments)
  369. {
  370. ConfigureTarget(colorAttachments, BuiltinRenderTextureType.CameraTarget);
  371. }
  372. /// <summary>
  373. /// Configures clearing for the render targets for this render pass. Call this inside Configure.
  374. /// </summary>
  375. /// <param name="clearFlag">ClearFlag containing information about what targets to clear.</param>
  376. /// <param name="clearColor">Clear color.</param>
  377. /// <seealso cref="Configure"/>
  378. public void ConfigureClear(ClearFlag clearFlag, Color clearColor)
  379. {
  380. m_ClearFlag = clearFlag;
  381. m_ClearColor = clearColor;
  382. }
  383. /// <summary>
  384. /// This method is called by the renderer before rendering a camera
  385. /// Override this method if you need to to configure render targets and their clear state, and to create temporary render target textures.
  386. /// If a render pass doesn't override this method, this render pass renders to the active Camera's render target.
  387. /// You should never call CommandBuffer.SetRenderTarget. Instead call <c>ConfigureTarget</c> and <c>ConfigureClear</c>.
  388. /// </summary>
  389. /// <param name="cmd">CommandBuffer to enqueue rendering commands. This will be executed by the pipeline.</param>
  390. /// <param name="renderingData">Current rendering state information</param>
  391. /// <seealso cref="ConfigureTarget"/>
  392. /// <seealso cref="ConfigureClear"/>
  393. public virtual void OnCameraSetup(CommandBuffer cmd, ref RenderingData renderingData)
  394. { }
  395. /// <summary>
  396. /// This method is called by the renderer before executing the render pass.
  397. /// Override this method if you need to to configure render targets and their clear state, and to create temporary render target textures.
  398. /// If a render pass doesn't override this method, this render pass renders to the active Camera's render target.
  399. /// You should never call CommandBuffer.SetRenderTarget. Instead call <c>ConfigureTarget</c> and <c>ConfigureClear</c>.
  400. /// </summary>
  401. /// <param name="cmd">CommandBuffer to enqueue rendering commands. This will be executed by the pipeline.</param>
  402. /// <param name="cameraTextureDescriptor">Render texture descriptor of the camera render target.</param>
  403. /// <seealso cref="ConfigureTarget"/>
  404. /// <seealso cref="ConfigureClear"/>
  405. public virtual void Configure(CommandBuffer cmd, RenderTextureDescriptor cameraTextureDescriptor)
  406. { }
  407. /// <summary>
  408. /// Called upon finish rendering a camera. You can use this callback to release any resources created
  409. /// by this render
  410. /// pass that need to be cleanup once camera has finished rendering.
  411. /// This method be called for all cameras in a camera stack.
  412. /// </summary>
  413. /// <param name="cmd">Use this CommandBuffer to cleanup any generated data</param>
  414. public virtual void OnCameraCleanup(CommandBuffer cmd)
  415. {
  416. }
  417. /// <summary>
  418. /// Called upon finish rendering a camera stack. You can use this callback to release any resources created
  419. /// by this render pass that need to be cleanup once all cameras in the stack have finished rendering.
  420. /// This method will be called once after rendering the last camera in the camera stack.
  421. /// Cameras that don't have an explicit camera stack are also considered stacked rendering.
  422. /// In that case the Base camera is the first and last camera in the stack.
  423. /// </summary>
  424. /// <param name="cmd">Use this CommandBuffer to cleanup any generated data</param>
  425. public virtual void OnFinishCameraStackRendering(CommandBuffer cmd)
  426. { }
  427. /// <summary>
  428. /// Execute the pass. This is where custom rendering occurs. Specific details are left to the implementation
  429. /// </summary>
  430. /// <param name="context">Use this render context to issue any draw commands during execution</param>
  431. /// <param name="renderingData">Current rendering state information</param>
  432. public abstract void Execute(ScriptableRenderContext context, ref RenderingData renderingData);
  433. /// <summary>
  434. /// Add a blit command to the context for execution. This changes the active render target in the ScriptableRenderer to
  435. /// destination.
  436. /// </summary>
  437. /// <param name="cmd">Command buffer to record command for execution.</param>
  438. /// <param name="source">Source texture or target identifier to blit from.</param>
  439. /// <param name="destination">Destination texture or target identifier to blit into. This becomes the renderer active render target.</param>
  440. /// <param name="material">Material to use.</param>
  441. /// <param name="passIndex">Shader pass to use. Default is 0.</param>
  442. /// <seealso cref="ScriptableRenderer"/>
  443. public void Blit(CommandBuffer cmd, RenderTargetIdentifier source, RenderTargetIdentifier destination, Material material = null, int passIndex = 0)
  444. {
  445. ScriptableRenderer.SetRenderTarget(cmd, destination, BuiltinRenderTextureType.CameraTarget, clearFlag, clearColor);
  446. cmd.Blit(source, destination, material, passIndex);
  447. }
  448. /// <summary>
  449. /// Add a blit command to the context for execution. This applies the material to the color target.
  450. /// </summary>
  451. /// <param name="cmd">Command buffer to record command for execution.</param>
  452. /// <param name="data">RenderingData to access the active renderer.</param>
  453. /// <param name="material">Material to use.</param>
  454. /// <param name="passIndex">Shader pass to use. Default is 0.</param>
  455. public void Blit(CommandBuffer cmd, ref RenderingData data, Material material, int passIndex = 0)
  456. {
  457. var renderer = data.cameraData.renderer;
  458. Blit(cmd, renderer.cameraColorTarget, renderer.GetCameraColorFrontBuffer(cmd), material, passIndex);
  459. renderer.SwapColorBuffer(cmd);
  460. }
  461. /// <summary>
  462. /// Creates <c>DrawingSettings</c> based on current the rendering state.
  463. /// </summary>
  464. /// <param name="shaderTagId">Shader pass tag to render.</param>
  465. /// <param name="renderingData">Current rendering state.</param>
  466. /// <param name="sortingCriteria">Criteria to sort objects being rendered.</param>
  467. /// <returns></returns>
  468. /// <seealso cref="DrawingSettings"/>
  469. public DrawingSettings CreateDrawingSettings(ShaderTagId shaderTagId, ref RenderingData renderingData, SortingCriteria sortingCriteria)
  470. {
  471. Camera camera = renderingData.cameraData.camera;
  472. SortingSettings sortingSettings = new SortingSettings(camera) { criteria = sortingCriteria };
  473. DrawingSettings settings = new DrawingSettings(shaderTagId, sortingSettings)
  474. {
  475. perObjectData = renderingData.perObjectData,
  476. mainLightIndex = renderingData.lightData.mainLightIndex,
  477. enableDynamicBatching = renderingData.supportsDynamicBatching,
  478. // Disable instancing for preview cameras. This is consistent with the built-in forward renderer. Also fixes case 1127324.
  479. enableInstancing = camera.cameraType == CameraType.Preview ? false : true,
  480. };
  481. return settings;
  482. }
  483. /// <summary>
  484. /// Creates <c>DrawingSettings</c> based on current rendering state.
  485. /// </summary>
  486. /// /// <param name="shaderTagIdList">List of shader pass tag to render.</param>
  487. /// <param name="renderingData">Current rendering state.</param>
  488. /// <param name="sortingCriteria">Criteria to sort objects being rendered.</param>
  489. /// <returns></returns>
  490. /// <seealso cref="DrawingSettings"/>
  491. public DrawingSettings CreateDrawingSettings(List<ShaderTagId> shaderTagIdList,
  492. ref RenderingData renderingData, SortingCriteria sortingCriteria)
  493. {
  494. if (shaderTagIdList == null || shaderTagIdList.Count == 0)
  495. {
  496. Debug.LogWarning("ShaderTagId list is invalid. DrawingSettings is created with default pipeline ShaderTagId");
  497. return CreateDrawingSettings(new ShaderTagId("UniversalPipeline"), ref renderingData, sortingCriteria);
  498. }
  499. DrawingSettings settings = CreateDrawingSettings(shaderTagIdList[0], ref renderingData, sortingCriteria);
  500. for (int i = 1; i < shaderTagIdList.Count; ++i)
  501. settings.SetShaderPassName(i, shaderTagIdList[i]);
  502. return settings;
  503. }
  504. public static bool operator <(ScriptableRenderPass lhs, ScriptableRenderPass rhs)
  505. {
  506. return lhs.renderPassEvent < rhs.renderPassEvent;
  507. }
  508. public static bool operator >(ScriptableRenderPass lhs, ScriptableRenderPass rhs)
  509. {
  510. return lhs.renderPassEvent > rhs.renderPassEvent;
  511. }
  512. }
  513. }