PostProcessRenderContext.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  1. using System.Collections.Generic;
  2. namespace UnityEngine.Rendering.PostProcessing
  3. {
  4. #if UNITY_2017_2_OR_NEWER
  5. using XRSettings = UnityEngine.XR.XRSettings;
  6. #elif UNITY_5_6_OR_NEWER
  7. using XRSettings = UnityEngine.VR.VRSettings;
  8. #endif
  9. /// <summary>
  10. /// A context object passed around all post-processing effects in a frame.
  11. /// </summary>
  12. public sealed class PostProcessRenderContext
  13. {
  14. // -----------------------------------------------------------------------------------------
  15. // The following should be filled by the render pipeline
  16. Camera m_Camera;
  17. /// <summary>
  18. /// The camera currently being rendered.
  19. /// </summary>
  20. public Camera camera
  21. {
  22. get { return m_Camera; }
  23. set
  24. {
  25. m_Camera = value;
  26. #if !UNITY_SWITCH && ENABLE_VR
  27. if (m_Camera.stereoEnabled)
  28. {
  29. #if UNITY_2017_2_OR_NEWER
  30. var xrDesc = XRSettings.eyeTextureDesc;
  31. stereoRenderingMode = StereoRenderingMode.SinglePass;
  32. #if UNITY_STANDALONE || UNITY_EDITOR
  33. if (xrDesc.dimension == TextureDimension.Tex2DArray)
  34. stereoRenderingMode = StereoRenderingMode.SinglePassInstanced;
  35. #endif
  36. if (stereoRenderingMode == StereoRenderingMode.SinglePassInstanced)
  37. numberOfEyes = 2;
  38. #if UNITY_2019_1_OR_NEWER
  39. if (stereoRenderingMode == StereoRenderingMode.SinglePass)
  40. {
  41. numberOfEyes = 2;
  42. xrDesc.width /= 2;
  43. xrDesc.vrUsage = VRTextureUsage.None;
  44. }
  45. #else
  46. //before 2019.1 double-wide still issues two drawcalls
  47. if (stereoRenderingMode == StereoRenderingMode.SinglePass)
  48. {
  49. numberOfEyes = 1;
  50. }
  51. #endif
  52. width = xrDesc.width;
  53. height = xrDesc.height;
  54. m_sourceDescriptor = xrDesc;
  55. #else
  56. // Single-pass is only supported with 2017.2+ because
  57. // that is when XRSettings.eyeTextureDesc is available.
  58. // Without it, we don't have a robust method of determining
  59. // if we are in single-pass. Users can just double the width
  60. // here if they KNOW they are using single-pass.
  61. width = XRSettings.eyeTextureWidth;
  62. height = XRSettings.eyeTextureHeight;
  63. #endif
  64. if (m_Camera.stereoActiveEye == Camera.MonoOrStereoscopicEye.Right)
  65. xrActiveEye = (int)Camera.StereoscopicEye.Right;
  66. screenWidth = XRSettings.eyeTextureWidth;
  67. screenHeight = XRSettings.eyeTextureHeight;
  68. #if UNITY_2019_1_OR_NEWER
  69. if (stereoRenderingMode == StereoRenderingMode.SinglePass)
  70. screenWidth /= 2;
  71. #endif
  72. stereoActive = true;
  73. }
  74. else
  75. #endif
  76. {
  77. width = m_Camera.pixelWidth;
  78. height = m_Camera.pixelHeight;
  79. #if UNITY_2017_2_OR_NEWER
  80. m_sourceDescriptor.width = width;
  81. m_sourceDescriptor.height = height;
  82. #endif
  83. screenWidth = width;
  84. screenHeight = height;
  85. stereoActive = false;
  86. numberOfEyes = 1;
  87. }
  88. }
  89. }
  90. /// <summary>
  91. /// The command buffer to fill render commands in.
  92. /// </summary>
  93. public CommandBuffer command { get; set; }
  94. /// <summary>
  95. /// The source target for this pass (can't be the same as <see cref="destination"/>).
  96. /// </summary>
  97. public RenderTargetIdentifier source { get; set; }
  98. /// <summary>
  99. /// The destination target for this pass (can't be the same as <see cref="source"/>).
  100. /// </summary>
  101. public RenderTargetIdentifier destination { get; set; }
  102. /// <summary>
  103. /// The texture format used for the source target.
  104. /// </summary>
  105. // We need this to be set explictely as we don't have any way of knowing if we're rendering
  106. // using HDR or not as scriptable render pipelines may ignore the HDR toggle on camera
  107. // completely
  108. public RenderTextureFormat sourceFormat { get; set; }
  109. /// <summary>
  110. /// Should we flip the last pass?
  111. /// </summary>
  112. public bool flip { get; set; }
  113. // -----------------------------------------------------------------------------------------
  114. // The following is auto-populated by the post-processing stack
  115. /// <summary>
  116. /// The resource asset contains reference to external resources (shaders, textures...).
  117. /// </summary>
  118. public PostProcessResources resources { get; internal set; }
  119. /// <summary>
  120. /// The property sheet factory handled by the currently active <see cref="PostProcessLayer"/>.
  121. /// </summary>
  122. public PropertySheetFactory propertySheets { get; internal set; }
  123. /// <summary>
  124. /// A dictionary to store custom user data objects. This is handy to share data between
  125. /// custom effects.
  126. /// </summary>
  127. public Dictionary<string, object> userData { get; private set; }
  128. /// <summary>
  129. /// A reference to the internal debug layer.
  130. /// </summary>
  131. public PostProcessDebugLayer debugLayer { get; internal set; }
  132. /// <summary>
  133. /// The current camera width (in pixels).
  134. /// </summary>
  135. public int width { get; private set; }
  136. /// <summary>
  137. /// The current camera height (in pixels).
  138. /// </summary>
  139. public int height { get; private set; }
  140. /// <summary>
  141. /// Is stereo rendering active?
  142. /// </summary>
  143. public bool stereoActive { get; private set; }
  144. /// <summary>
  145. /// The current active rendering eye (for XR).
  146. /// </summary>
  147. public int xrActiveEye { get; private set; }
  148. /// <summary>
  149. /// The number of eyes for XR outputs.
  150. /// </summary>
  151. public int numberOfEyes { get; private set; }
  152. /// <summary>
  153. /// Available XR rendering modes.
  154. /// </summary>
  155. public enum StereoRenderingMode
  156. {
  157. MultiPass = 0,
  158. SinglePass,
  159. SinglePassInstanced,
  160. SinglePassMultiview
  161. }
  162. /// <summary>
  163. /// The current rendering mode for XR.
  164. /// </summary>
  165. public StereoRenderingMode stereoRenderingMode { get; private set; }
  166. /// <summary>
  167. /// The width of the logical screen size.
  168. /// </summary>
  169. public int screenWidth { get; private set; }
  170. /// <summary>
  171. /// The height of the logical screen size.
  172. /// </summary>
  173. public int screenHeight { get; private set; }
  174. /// <summary>
  175. /// Are we currently rendering in the scene view?
  176. /// </summary>
  177. public bool isSceneView { get; internal set; }
  178. /// <summary>
  179. /// The current anti-aliasing method used by the camera.
  180. /// </summary>
  181. public PostProcessLayer.Antialiasing antialiasing { get; internal set; }
  182. /// <summary>
  183. /// A reference to the temporal anti-aliasing settings for the rendering layer. This is
  184. /// mostly used to grab the jitter vector and other TAA-related values when an effect needs
  185. /// to do temporal reprojection.
  186. /// </summary>
  187. public TemporalAntialiasing temporalAntialiasing { get; internal set; }
  188. // Internal values used for builtin effects
  189. // Beware, these may not have been set before a specific builtin effect has been executed
  190. internal PropertySheet uberSheet;
  191. internal Texture autoExposureTexture;
  192. internal LogHistogram logHistogram;
  193. internal Texture logLut;
  194. internal AutoExposure autoExposure;
  195. internal int bloomBufferNameID;
  196. #if UNITY_2018_2_OR_NEWER
  197. internal bool physicalCamera;
  198. #endif
  199. /// <summary>
  200. /// Resets the state of this context object. This is called by the render pipeline on every
  201. /// frame and allows re-using the same context object between frames without having to
  202. /// recreate a new one.
  203. /// </summary>
  204. public void Reset()
  205. {
  206. m_Camera = null;
  207. width = 0;
  208. height = 0;
  209. #if UNITY_2017_2_OR_NEWER
  210. m_sourceDescriptor = new RenderTextureDescriptor(0, 0);
  211. #endif
  212. #if UNITY_2018_2_OR_NEWER
  213. physicalCamera = false;
  214. #endif
  215. stereoActive = false;
  216. xrActiveEye = (int)Camera.StereoscopicEye.Left;
  217. screenWidth = 0;
  218. screenHeight = 0;
  219. command = null;
  220. source = 0;
  221. destination = 0;
  222. sourceFormat = RenderTextureFormat.ARGB32;
  223. flip = false;
  224. resources = null;
  225. propertySheets = null;
  226. debugLayer = null;
  227. isSceneView = false;
  228. antialiasing = PostProcessLayer.Antialiasing.None;
  229. temporalAntialiasing = null;
  230. uberSheet = null;
  231. autoExposureTexture = null;
  232. logLut = null;
  233. autoExposure = null;
  234. bloomBufferNameID = -1;
  235. if (userData == null)
  236. userData = new Dictionary<string, object>();
  237. userData.Clear();
  238. }
  239. /// <summary>
  240. /// Checks if temporal anti-aliasing is supported and enabled.
  241. /// </summary>
  242. /// <returns><c>true</c> if temporal anti-aliasing is supported and enabled, <c>false</c>
  243. /// otherwise</returns>
  244. public bool IsTemporalAntialiasingActive()
  245. {
  246. return antialiasing == PostProcessLayer.Antialiasing.TemporalAntialiasing
  247. && !isSceneView
  248. && temporalAntialiasing.IsSupported();
  249. }
  250. /// <summary>
  251. /// Checks if a specific debug overlay is enabled.
  252. /// </summary>
  253. /// <param name="overlay">The debug overlay to look for</param>
  254. /// <returns><c>true</c> if the specified debug overlay is enable, <c>false</c>
  255. /// otherwise</returns>
  256. public bool IsDebugOverlayEnabled(DebugOverlay overlay)
  257. {
  258. return debugLayer.debugOverlay == overlay;
  259. }
  260. /// <summary>
  261. /// Blit a source render target to the debug overlay target. This is a direct shortcut to
  262. /// <see cref="PostProcessDebugLayer.PushDebugOverlay"/>.
  263. /// </summary>
  264. /// <param name="cmd">The command buffer to send render commands to</param>
  265. /// <param name="source">The source target</param>
  266. /// <param name="sheet">The property sheet to use for the blit</param>
  267. /// <param name="pass">The pass to use for the property sheet</param>
  268. /// <seealso cref="PostProcessDebugLayer.PushDebugOverlay"/>
  269. public void PushDebugOverlay(CommandBuffer cmd, RenderTargetIdentifier source, PropertySheet sheet, int pass)
  270. {
  271. debugLayer.PushDebugOverlay(cmd, source, sheet, pass);
  272. }
  273. // TODO: Change w/h name to texture w/h in order to make
  274. // size usages explicit
  275. #if UNITY_2017_2_OR_NEWER
  276. RenderTextureDescriptor m_sourceDescriptor;
  277. RenderTextureDescriptor GetDescriptor(int depthBufferBits = 0, RenderTextureFormat colorFormat = RenderTextureFormat.Default, RenderTextureReadWrite readWrite = RenderTextureReadWrite.Default)
  278. {
  279. var modifiedDesc = new RenderTextureDescriptor(m_sourceDescriptor.width, m_sourceDescriptor.height,
  280. m_sourceDescriptor.colorFormat, depthBufferBits);
  281. modifiedDesc.dimension = m_sourceDescriptor.dimension;
  282. modifiedDesc.volumeDepth = m_sourceDescriptor.volumeDepth;
  283. modifiedDesc.vrUsage = m_sourceDescriptor.vrUsage;
  284. modifiedDesc.msaaSamples = m_sourceDescriptor.msaaSamples;
  285. modifiedDesc.memoryless = m_sourceDescriptor.memoryless;
  286. modifiedDesc.useMipMap = m_sourceDescriptor.useMipMap;
  287. modifiedDesc.autoGenerateMips = m_sourceDescriptor.autoGenerateMips;
  288. modifiedDesc.enableRandomWrite = m_sourceDescriptor.enableRandomWrite;
  289. modifiedDesc.shadowSamplingMode = m_sourceDescriptor.shadowSamplingMode;
  290. if (colorFormat != RenderTextureFormat.Default)
  291. modifiedDesc.colorFormat = colorFormat;
  292. modifiedDesc.sRGB = readWrite != RenderTextureReadWrite.Linear;
  293. return modifiedDesc;
  294. }
  295. #endif
  296. /// <summary>
  297. /// Grabs a temporary render target with the current display size.
  298. /// </summary>
  299. /// <param name="cmd">The command buffer to grab a render target from</param>
  300. /// <param name="nameID">The shader property name for this texture</param>
  301. /// <param name="depthBufferBits">The number of bits to use for the depth buffer</param>
  302. /// <param name="colorFormat">The render texture format</param>
  303. /// <param name="readWrite">The color space conversion mode</param>
  304. /// <param name="filter">The texture filtering mode</param>
  305. /// <param name="widthOverride">Override the display width; use <c>0</c> to disable the override</param>
  306. /// <param name="heightOverride">Override the display height; use <c>0</c> to disable the override</param>
  307. public void GetScreenSpaceTemporaryRT(CommandBuffer cmd, int nameID,
  308. int depthBufferBits = 0, RenderTextureFormat colorFormat = RenderTextureFormat.Default, RenderTextureReadWrite readWrite = RenderTextureReadWrite.Default,
  309. FilterMode filter = FilterMode.Bilinear, int widthOverride = 0, int heightOverride = 0)
  310. {
  311. #if UNITY_2017_2_OR_NEWER
  312. var desc = GetDescriptor(depthBufferBits, colorFormat, readWrite);
  313. if (widthOverride > 0)
  314. desc.width = widthOverride;
  315. if (heightOverride > 0)
  316. desc.height = heightOverride;
  317. //intermediates in VR are unchanged
  318. if (stereoActive && desc.dimension == Rendering.TextureDimension.Tex2DArray)
  319. desc.dimension = Rendering.TextureDimension.Tex2D;
  320. cmd.GetTemporaryRT(nameID, desc, filter);
  321. #else
  322. int actualWidth = width;
  323. int actualHeight = height;
  324. if (widthOverride > 0)
  325. actualWidth = widthOverride;
  326. if (heightOverride > 0)
  327. actualHeight = heightOverride;
  328. cmd.GetTemporaryRT(nameID, actualWidth, actualHeight, depthBufferBits, filter, colorFormat, readWrite);
  329. // TODO: How to handle MSAA for XR in older versions? Query cam?
  330. // TODO: Pass in vrUsage into the args
  331. #endif
  332. }
  333. /// <summary>
  334. /// Grabs a temporary render target with the current display size.
  335. /// </summary>
  336. /// <param name="depthBufferBits">The number of bits to use for the depth buffer</param>
  337. /// <param name="colorFormat">The render texture format</param>
  338. /// <param name="readWrite">The color space conversion mode</param>
  339. /// <param name="widthOverride">Override the display width; use <c>0</c> to disable the override</param>
  340. /// <param name="heightOverride">Override the display height; use <c>0</c> to disable the override</param>
  341. /// <returns>A temporary render target</returns>
  342. public RenderTexture GetScreenSpaceTemporaryRT(int depthBufferBits = 0, RenderTextureFormat colorFormat = RenderTextureFormat.Default,
  343. RenderTextureReadWrite readWrite = RenderTextureReadWrite.Default, int widthOverride = 0, int heightOverride = 0)
  344. {
  345. #if UNITY_2017_2_OR_NEWER
  346. var desc = GetDescriptor(depthBufferBits, colorFormat, readWrite);
  347. if (widthOverride > 0)
  348. desc.width = widthOverride;
  349. if (heightOverride > 0)
  350. desc.height = heightOverride;
  351. return RenderTexture.GetTemporary(desc);
  352. #else
  353. int actualWidth = width;
  354. int actualHeight = height;
  355. if (widthOverride > 0)
  356. actualWidth = widthOverride;
  357. if (heightOverride > 0)
  358. actualHeight = heightOverride;
  359. return RenderTexture.GetTemporary(actualWidth, actualHeight, depthBufferBits, colorFormat, readWrite);
  360. #endif
  361. }
  362. }
  363. }