No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

GlobalGbuffersRendererFeature.cs 6.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. using UnityEngine;
  2. using UnityEngine.Rendering;
  3. using UnityEngine.Rendering.Universal;
  4. using UnityEngine.Rendering.RenderGraphModule;
  5. // This example feature sets the gBuffer components as globals (it renders nothing itself). By adding this
  6. // feature to the scriptable renderer, other passes after it can access the gBuffers as globals.
  7. // Make sure to set the rendering path to Deferred for it to work.
  8. // Setting the gBuffers as globals may lead to reduced performance and memory use. Ideally, it's better to manage the
  9. // textures yourself and do builder.UseTexture only for the textures you actually need.
  10. public class GlobalGbuffersRendererFeature : ScriptableRendererFeature
  11. {
  12. class GlobalGBuffersRenderPass : ScriptableRenderPass
  13. {
  14. Material m_Material;
  15. string m_PassName = "Make gBuffer Components Global";
  16. private static readonly int GBufferNormalSmoothnessIndex = 2;
  17. private static readonly int GbufferLightingIndex = 3;
  18. private static readonly int GBufferRenderingLayersIndex = 5;
  19. // The pipeline already sets the gBuffer depth component to be global in a few places, so uncomment this code as needed
  20. // private static readonly int GbufferDepthIndex = 4;
  21. // Components marked as optional are only present when the pipeline requests it.
  22. // If for example there is no rendering layers texture, _GBuffer5 will contain the ShadowMask texture
  23. private static readonly int[] s_GBufferShaderPropertyIDs = new int[]
  24. {
  25. // Contains Albedo Texture
  26. Shader.PropertyToID("_GBuffer0"),
  27. // Contains Specular Metallic Texture
  28. Shader.PropertyToID("_GBuffer1"),
  29. // Contains Normals and Smoothness, referenced as _CameraNormalsTexture in other shaders
  30. Shader.PropertyToID("_GBuffer2"),
  31. // Contains Lighting texture
  32. Shader.PropertyToID("_GBuffer3"),
  33. // Contains Depth texture, referenced as _CameraDepthTexture in other shaders (optional)
  34. Shader.PropertyToID("_GBuffer4"),
  35. // Contains Rendering Layers Texture, referenced as _CameraRenderingLayersTexture in other shaders (optional)
  36. Shader.PropertyToID("_GBuffer5"),
  37. // Contains ShadowMask texture (optional)
  38. Shader.PropertyToID("_GBuffer6")
  39. };
  40. private class PassData
  41. {
  42. }
  43. // This sets the gBuffer components as global after the current pass. After the pass, the gBuffers components made global
  44. // will be made accessible using 'builder.UseAllGlobalTextures(true)' instead of 'builder.UseTexture(gBuffer[i])
  45. // Shaders that use global texture will be able to fetch them without the need to call 'material.SetTexture()'
  46. // like we do in the ExecutePass function of this pass.
  47. private void SetGlobalGBufferTextures(IRasterRenderGraphBuilder builder, TextureHandle[] gBuffer)
  48. {
  49. // This loop will make the gBuffers accessible by all shaders using _GBufferX texture shader IDs
  50. for (int i = 0; i < gBuffer.Length; i++)
  51. {
  52. if (i != GbufferLightingIndex && gBuffer[i].IsValid())
  53. builder.SetGlobalTextureAfterPass(gBuffer[i], s_GBufferShaderPropertyIDs[i]);
  54. }
  55. // Some global textures are accessed using specific shader IDs that are internal to URP. To use the gBuffer in these places, we
  56. // need to set the ID to point to the corresponding gBuffer component.
  57. if (gBuffer[GBufferNormalSmoothnessIndex].IsValid())
  58. {
  59. // After this pass, shaders that use the _CameraNormalsTexture will get the gBuffer's NormalsSmoothnessTexture component
  60. builder.SetGlobalTextureAfterPass(gBuffer[GBufferNormalSmoothnessIndex],
  61. Shader.PropertyToID("_CameraNormalsTexture"));
  62. }
  63. // The pipeline already sets the gBuffer depth component to be global in a few places, so uncomment this code as needed
  64. // if (GbufferDepthIndex < gBuffer.Length && gBuffer[GbufferDepthIndex].IsValid())
  65. // {
  66. // // After this pass, shaders that use the _CameraDepthTexture will get the gBuffer's Depth component (note that it is also set global by the copy depth pass)
  67. // builder.SetGlobalTextureAfterPass(gBuffer[GbufferDepthIndex],
  68. // Shader.PropertyToID("_CameraDepthTexture"));
  69. // }
  70. if (GBufferRenderingLayersIndex < gBuffer.Length && gBuffer[GBufferRenderingLayersIndex].IsValid())
  71. {
  72. // After this pass, shaders that use the _CameraRenderingLayersTexture will get the gBuffer's RenderingLayersTexture component
  73. builder.SetGlobalTextureAfterPass(gBuffer[GBufferRenderingLayersIndex],
  74. Shader.PropertyToID("_CameraRenderingLayersTexture"));
  75. }
  76. }
  77. // RecordRenderGraph is where the RenderGraph handle can be accessed, through which render passes can be added to the graph.
  78. // FrameData is a context container through which URP resources can be accessed and managed.
  79. public override void RecordRenderGraph(RenderGraph renderGraph, ContextContainer frameData)
  80. {
  81. UniversalRenderingData universalRenderingData = frameData.Get<UniversalRenderingData>();
  82. // The gBuffer components are only used in deferred mode
  83. if (universalRenderingData.renderingMode != RenderingMode.Deferred)
  84. return;
  85. // Get the gBuffer texture handles are stored in the resourceData
  86. UniversalResourceData resourceData = frameData.Get<UniversalResourceData>();
  87. TextureHandle[] gBuffer = resourceData.gBuffer;
  88. using (var builder = renderGraph.AddRasterRenderPass<PassData>(m_PassName, out var passData))
  89. {
  90. builder.AllowPassCulling(false);
  91. // Set the gBuffers to be global after the pass
  92. SetGlobalGBufferTextures(builder, gBuffer);
  93. builder.SetRenderFunc((PassData data, RasterGraphContext context) => { /* nothing to be rendered */ });
  94. }
  95. }
  96. }
  97. GlobalGBuffersRenderPass m_GlobalGbuffersRenderPass;
  98. /// <inheritdoc/>
  99. public override void Create()
  100. {
  101. m_GlobalGbuffersRenderPass = new GlobalGBuffersRenderPass
  102. {
  103. // This pass must be injected after rendering the deferred lights or later.
  104. renderPassEvent = RenderPassEvent.AfterRenderingDeferredLights
  105. };
  106. }
  107. public override void AddRenderPasses(ScriptableRenderer renderer, ref RenderingData renderingData)
  108. {
  109. renderer.EnqueuePass(m_GlobalGbuffersRenderPass);
  110. }
  111. }