Skip to content

Commit 85e33e6

Browse files
gmitrano-unityEvergreen
authored andcommitted
Clear Global Render Graph Texture Bindings After Execution
This change adds logic to render graph that clears global texture bindings after execution. This behavior prevents code that follows the render graph execution from depending on resources that are only valid within the graph's scope. The behavior is controlled via a C# define for now because it uncovers several other issues that need to be resolved before this is enabled unconditionally. This PR is an temporary change that's a prerequisite for fixing UUM-74470. It has no functional impact when the C# define is not set.
1 parent 0c9aaf6 commit 85e33e6

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

Packages/com.unity.render-pipelines.core/Runtime/RenderGraph/IRenderGraphBuilder.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ public interface IBaseRenderGraphBuilder : IDisposable
4646
/// Although counter-intuitive at first, this call doesn't actually have a dependency on the passed in texture handle. It's only when a subsequent pass has
4747
/// a dependency on the global texture slot that subsequent pass will get a dependency on the currently set global texture for that slot. This means
4848
/// globals slots can be set without overhead if you're unsure if a resource will be used or not, the graph will still maintain the correct lifetimes.
49+
///
50+
/// NOTE: When the `RENDER_GRAPH_CLEAR_GLOBALS` define is set, all shader bindings set through this function will be cleared once graph execution completes.
4951
/// </summary>
5052
/// <param name="input">The texture value to set in the global texture slot. This can be an null handle to clear the global texture slot.</param>
5153
/// <param name="propertyId">The global texture slot to set the value for. Use Shader.PropertyToID to generate the id.</param>

Packages/com.unity.render-pipelines.core/Runtime/RenderGraph/RenderGraph.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1233,6 +1233,11 @@ internal void Execute()
12331233
ExecuteNativeRenderGraph();
12341234
else
12351235
ExecuteRenderGraph();
1236+
1237+
#if RENDER_GRAPH_CLEAR_GLOBALS
1238+
// Clear the shader bindings for all global textures to make sure bindings don't leak outside the graph
1239+
ClearGlobalBindings();
1240+
#endif
12361241
}
12371242
}
12381243
catch (Exception e)
@@ -2588,6 +2593,22 @@ internal TextureHandle GetGlobal(int globalPropertyId)
25882593
registeredGlobals.TryGetValue(globalPropertyId, out h);
25892594
return h;
25902595
}
2596+
2597+
/// <summary>
2598+
/// Clears the shader bindings associated with the registered globals in the graph
2599+
///
2600+
/// This prevents later rendering logic from accidentally relying on stale shader bindings that were set
2601+
/// earlier during graph execution.
2602+
/// </summary>
2603+
internal void ClearGlobalBindings()
2604+
{
2605+
// Set all the global texture shader bindings to the default black texture.
2606+
// This doesn't technically "clear" the shader bindings, but it's the closest we can do.
2607+
foreach (var globalTex in registeredGlobals)
2608+
{
2609+
m_RenderGraphContext.cmd.SetGlobalTexture(globalTex.Key, defaultResources.blackTexture);
2610+
}
2611+
}
25912612
}
25922613

25932614
/// <summary>

0 commit comments

Comments
 (0)