RenderPipelineDetector.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. using UnityEngine;
  2. using UnityEngine.Rendering;
  3. #if UNITY_PIPELINE_URP
  4. using UnityEngine.Rendering.Universal;
  5. using System.Reflection;
  6. using System.Collections.Generic;
  7. #endif
  8. namespace com.zibra.common.Utilities
  9. {
  10. /// <summary>
  11. /// Class responsible for querying data about Render Pipelines.
  12. /// </summary>
  13. public static class RenderPipelineDetector
  14. {
  15. /// <summary>
  16. /// Type of render pipeline.
  17. /// </summary>
  18. public enum RenderPipeline
  19. {
  20. BuiltInRP,
  21. URP,
  22. HDRP
  23. }
  24. /// <summary>
  25. /// Detects which render pipeline is currently used.
  26. /// </summary>
  27. /// <returns>
  28. /// <see cref="RenderPipeline"/>.
  29. /// </returns>
  30. public static RenderPipeline GetRenderPipelineType()
  31. {
  32. if (GraphicsSettings.currentRenderPipeline)
  33. {
  34. if (GraphicsSettings.currentRenderPipeline.GetType().ToString().Contains("HighDefinition"))
  35. {
  36. #if !UNITY_PIPELINE_HDRP
  37. Debug.LogError("Current detected render pipeline is HDRP, but UNITY_PIPELINE_HDRP is not defined");
  38. #endif
  39. return RenderPipeline.HDRP;
  40. }
  41. else
  42. {
  43. #if !UNITY_PIPELINE_URP
  44. Debug.LogError("Current detected render pipeline is URP, but UNITY_PIPELINE_URP is not defined");
  45. #endif
  46. return RenderPipeline.URP;
  47. }
  48. }
  49. else
  50. {
  51. return RenderPipeline.BuiltInRP;
  52. }
  53. }
  54. #if UNITY_PIPELINE_URP
  55. /// <summary>
  56. /// Checks whether ZibraURPRenderComponent is missing in URP options.
  57. /// </summary>
  58. /// <returns>
  59. /// True if current render pipeline is URP, but ZibraURPRenderComponent is missing,
  60. /// and false otherwise.
  61. /// </returns>
  62. /// <remarks>
  63. /// Please note that you may have different URP options for mobile and desktop,
  64. /// but we can only check current options.
  65. /// </remarks>
  66. internal static bool IsURPMissingRenderComponent<T>()
  67. where T : ScriptableRendererFeature
  68. {
  69. if (GetRenderPipelineType() == RenderPipeline.URP)
  70. {
  71. // Getting non public list of render features via reflection
  72. var URPAsset = GraphicsSettings.renderPipelineAsset as UniversalRenderPipelineAsset;
  73. var field = typeof(ScriptableRenderer)
  74. .GetField("m_RendererFeatures", BindingFlags.NonPublic | BindingFlags.Instance);
  75. if (field != null && URPAsset != null)
  76. {
  77. var scriptableRendererFeatures =
  78. field.GetValue(URPAsset.scriptableRenderer) as List<ScriptableRendererFeature>;
  79. if (scriptableRendererFeatures != null)
  80. {
  81. foreach (var renderFeature in scriptableRendererFeatures)
  82. {
  83. if (renderFeature is T)
  84. {
  85. return false;
  86. }
  87. }
  88. }
  89. }
  90. return true;
  91. }
  92. return false;
  93. }
  94. #endif
  95. internal static bool IsURPMissingRenderComponent(string name)
  96. {
  97. #if UNITY_PIPELINE_URP
  98. if (GetRenderPipelineType() == RenderPipeline.URP)
  99. {
  100. var URPAsset = GraphicsSettings.renderPipelineAsset as UniversalRenderPipelineAsset;
  101. var field = typeof(ScriptableRenderer)
  102. .GetField("m_RendererFeatures", BindingFlags.NonPublic | BindingFlags.Instance);
  103. if (field != null && URPAsset != null)
  104. {
  105. var scriptableRendererFeatures =
  106. field.GetValue(URPAsset.scriptableRenderer) as List<ScriptableRendererFeature>;
  107. if (scriptableRendererFeatures != null)
  108. {
  109. foreach (var renderFeature in scriptableRendererFeatures)
  110. {
  111. if (renderFeature.GetType().Name == name)
  112. {
  113. return false;
  114. }
  115. }
  116. }
  117. }
  118. return true;
  119. }
  120. #endif
  121. return false;
  122. }
  123. /// <summary>
  124. /// Checks whether depth buffer is not enabled in URP options.
  125. /// </summary>
  126. /// <returns>
  127. /// True if current render pipeline is URP, but depth buffer is not enabled,
  128. /// and false otherwise.
  129. /// </returns>
  130. /// <remarks>
  131. /// Please note that you may have differnet URP options for mobile and desktop,
  132. /// but we can only check current options.
  133. /// </remarks>
  134. public static bool IsURPMissingDepthBuffer()
  135. {
  136. #if UNITY_PIPELINE_URP
  137. if (GetRenderPipelineType() == RenderPipeline.URP)
  138. {
  139. // Getting non public list of render features via reflection
  140. var URPAsset = GraphicsSettings.renderPipelineAsset as UniversalRenderPipelineAsset;
  141. return URPAsset != null && !URPAsset.supportsCameraDepthTexture;
  142. }
  143. #endif
  144. return false;
  145. }
  146. }
  147. }