UniversalRendererData.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. #if UNITY_EDITOR
  2. using UnityEditor;
  3. using UnityEditor.ProjectWindowCallback;
  4. #endif
  5. using System;
  6. using UnityEngine.Scripting.APIUpdating;
  7. using UnityEngine.Assertions;
  8. namespace UnityEngine.Rendering.Universal
  9. {
  10. [Serializable, ReloadGroup, ExcludeFromPreset]
  11. public class UniversalRendererData : ScriptableRendererData, ISerializationCallbackReceiver
  12. {
  13. #if UNITY_EDITOR
  14. [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance", "CA1812")]
  15. internal class CreateUniversalRendererAsset : EndNameEditAction
  16. {
  17. public override void Action(int instanceId, string pathName, string resourceFile)
  18. {
  19. var instance = UniversalRenderPipelineAsset.CreateRendererAsset(pathName, RendererType.UniversalRenderer, false) as UniversalRendererData;
  20. Selection.activeObject = instance;
  21. }
  22. }
  23. [MenuItem("Assets/Create/Rendering/URP Universal Renderer", priority = CoreUtils.Sections.section3 + CoreUtils.Priorities.assetsCreateRenderingMenuPriority + 2)]
  24. static void CreateUniversalRendererData()
  25. {
  26. ProjectWindowUtil.StartNameEditingIfProjectWindowExists(0, CreateInstance<CreateUniversalRendererAsset>(), "New Custom Universal Renderer Data.asset", null, null);
  27. }
  28. #endif
  29. [Serializable, ReloadGroup]
  30. public sealed class ShaderResources
  31. {
  32. [Reload("Shaders/Utils/Blit.shader")]
  33. public Shader blitPS;
  34. [Reload("Shaders/Utils/CopyDepth.shader")]
  35. public Shader copyDepthPS;
  36. [Obsolete("Obsolete, this feature will be supported by new 'ScreenSpaceShadows' renderer feature")]
  37. public Shader screenSpaceShadowPS;
  38. [Reload("Shaders/Utils/Sampling.shader")]
  39. public Shader samplingPS;
  40. [Reload("Shaders/Utils/StencilDeferred.shader")]
  41. public Shader stencilDeferredPS;
  42. [Reload("Shaders/Utils/FallbackError.shader")]
  43. public Shader fallbackErrorPS;
  44. [Reload("Shaders/Utils/MaterialError.shader")]
  45. public Shader materialErrorPS;
  46. // Core blitter shaders, adapted from HDRP
  47. // TODO: move to core and share with HDRP
  48. [Reload("Shaders/Utils/CoreBlit.shader"), SerializeField]
  49. internal Shader coreBlitPS;
  50. [Reload("Shaders/Utils/CoreBlitColorAndDepth.shader"), SerializeField]
  51. internal Shader coreBlitColorAndDepthPS;
  52. [Reload("Shaders/CameraMotionVectors.shader")]
  53. public Shader cameraMotionVector;
  54. [Reload("Shaders/ObjectMotionVectors.shader")]
  55. public Shader objectMotionVector;
  56. }
  57. public PostProcessData postProcessData = null;
  58. #if ENABLE_VR && ENABLE_XR_MODULE
  59. [Reload("Runtime/Data/XRSystemData.asset")]
  60. public XRSystemData xrSystemData = null;
  61. #endif
  62. public ShaderResources shaders = null;
  63. const int k_LatestAssetVersion = 1;
  64. [SerializeField] int m_AssetVersion = 0;
  65. [SerializeField] LayerMask m_OpaqueLayerMask = -1;
  66. [SerializeField] LayerMask m_TransparentLayerMask = -1;
  67. [SerializeField] StencilStateData m_DefaultStencilState = new StencilStateData() { passOperation = StencilOp.Replace }; // This default state is compatible with deferred renderer.
  68. [SerializeField] bool m_ShadowTransparentReceive = true;
  69. [SerializeField] RenderingMode m_RenderingMode = RenderingMode.Forward;
  70. [SerializeField] DepthPrimingMode m_DepthPrimingMode = DepthPrimingMode.Disabled; // Default disabled because there are some outstanding issues with Text Mesh rendering.
  71. [SerializeField] bool m_AccurateGbufferNormals = false;
  72. //[SerializeField] bool m_TiledDeferredShading = false;
  73. [SerializeField] bool m_ClusteredRendering = false;
  74. const TileSize k_DefaultTileSize = TileSize._32;
  75. [SerializeField] TileSize m_TileSize = k_DefaultTileSize;
  76. [SerializeField] IntermediateTextureMode m_IntermediateTextureMode = IntermediateTextureMode.Auto;
  77. protected override ScriptableRenderer Create()
  78. {
  79. if (!Application.isPlaying)
  80. {
  81. ReloadAllNullProperties();
  82. }
  83. return new UniversalRenderer(this);
  84. }
  85. /// <summary>
  86. /// Use this to configure how to filter opaque objects.
  87. /// </summary>
  88. public LayerMask opaqueLayerMask
  89. {
  90. get => m_OpaqueLayerMask;
  91. set
  92. {
  93. SetDirty();
  94. m_OpaqueLayerMask = value;
  95. }
  96. }
  97. /// <summary>
  98. /// Use this to configure how to filter transparent objects.
  99. /// </summary>
  100. public LayerMask transparentLayerMask
  101. {
  102. get => m_TransparentLayerMask;
  103. set
  104. {
  105. SetDirty();
  106. m_TransparentLayerMask = value;
  107. }
  108. }
  109. public StencilStateData defaultStencilState
  110. {
  111. get => m_DefaultStencilState;
  112. set
  113. {
  114. SetDirty();
  115. m_DefaultStencilState = value;
  116. }
  117. }
  118. /// <summary>
  119. /// True if transparent objects receive shadows.
  120. /// </summary>
  121. public bool shadowTransparentReceive
  122. {
  123. get => m_ShadowTransparentReceive;
  124. set
  125. {
  126. SetDirty();
  127. m_ShadowTransparentReceive = value;
  128. }
  129. }
  130. /// <summary>
  131. /// Rendering mode.
  132. /// </summary>
  133. public RenderingMode renderingMode
  134. {
  135. get => m_RenderingMode;
  136. set
  137. {
  138. SetDirty();
  139. m_RenderingMode = value;
  140. }
  141. }
  142. /// <summary>
  143. /// Depth priming mode.
  144. /// </summary>
  145. public DepthPrimingMode depthPrimingMode
  146. {
  147. get => m_DepthPrimingMode;
  148. set
  149. {
  150. SetDirty();
  151. m_DepthPrimingMode = value;
  152. }
  153. }
  154. /// <summary>
  155. /// Use Octaedron Octahedron normal vector encoding for gbuffer normals.
  156. /// The overhead is negligible from desktop GPUs, while it should be avoided for mobile GPUs.
  157. /// </summary>
  158. public bool accurateGbufferNormals
  159. {
  160. get => m_AccurateGbufferNormals;
  161. set
  162. {
  163. SetDirty();
  164. m_AccurateGbufferNormals = value;
  165. }
  166. }
  167. /*
  168. public bool tiledDeferredShading
  169. {
  170. get => m_TiledDeferredShading;
  171. set
  172. {
  173. SetDirty();
  174. m_TiledDeferredShading = value;
  175. }
  176. }
  177. */
  178. internal bool clusteredRendering
  179. {
  180. get => m_ClusteredRendering;
  181. set
  182. {
  183. SetDirty();
  184. m_ClusteredRendering = value;
  185. }
  186. }
  187. internal TileSize tileSize
  188. {
  189. get => m_TileSize;
  190. set
  191. {
  192. Assert.IsTrue(value.IsValid());
  193. SetDirty();
  194. m_TileSize = value;
  195. }
  196. }
  197. /// <summary>
  198. /// Controls when URP renders via an intermediate texture.
  199. /// </summary>
  200. public IntermediateTextureMode intermediateTextureMode
  201. {
  202. get => m_IntermediateTextureMode;
  203. set
  204. {
  205. SetDirty();
  206. m_IntermediateTextureMode = value;
  207. }
  208. }
  209. protected override void OnValidate()
  210. {
  211. base.OnValidate();
  212. if (!m_TileSize.IsValid())
  213. {
  214. m_TileSize = k_DefaultTileSize;
  215. }
  216. }
  217. protected override void OnEnable()
  218. {
  219. base.OnEnable();
  220. // Upon asset creation, OnEnable is called and `shaders` reference is not yet initialized
  221. // We need to call the OnEnable for data migration when updating from old versions of UniversalRP that
  222. // serialized resources in a different format. Early returning here when OnEnable is called
  223. // upon asset creation is fine because we guarantee new assets get created with all resources initialized.
  224. if (shaders == null)
  225. return;
  226. ReloadAllNullProperties();
  227. }
  228. private void ReloadAllNullProperties()
  229. {
  230. #if UNITY_EDITOR
  231. ResourceReloader.TryReloadAllNullIn(this, UniversalRenderPipelineAsset.packagePath);
  232. #if ENABLE_VR && ENABLE_XR_MODULE
  233. ResourceReloader.TryReloadAllNullIn(xrSystemData, UniversalRenderPipelineAsset.packagePath);
  234. #endif
  235. #endif
  236. }
  237. void ISerializationCallbackReceiver.OnBeforeSerialize()
  238. {
  239. m_AssetVersion = k_LatestAssetVersion;
  240. }
  241. void ISerializationCallbackReceiver.OnAfterDeserialize()
  242. {
  243. if (m_AssetVersion <= 0)
  244. {
  245. var anyNonUrpRendererFeatures = false;
  246. foreach (var feature in m_RendererFeatures)
  247. {
  248. try
  249. {
  250. if (feature.GetType().Assembly == typeof(UniversalRendererData).Assembly)
  251. {
  252. continue;
  253. }
  254. }
  255. catch
  256. {
  257. // If we hit any exceptions while poking around assemblies,
  258. // conservatively assume there was a non URP renderer feature.
  259. }
  260. anyNonUrpRendererFeatures = true;
  261. }
  262. // Replicate old intermediate texture behaviour in case of any non-URP renderer features,
  263. // where we cannot know if they properly declare needed inputs.
  264. m_IntermediateTextureMode = anyNonUrpRendererFeatures ? IntermediateTextureMode.Always : IntermediateTextureMode.Auto;
  265. }
  266. m_AssetVersion = k_LatestAssetVersion;
  267. }
  268. }
  269. }