Skip to content

Commit 9e3a043

Browse files
UnityAljoshaEvergreen
authored andcommitted
Fix URP pass name handling
This is a continuation of https://github.cds.internal.unity3d.com/unity/unity/pull/50405 The goal is to have identical URP render pass names in all our profiling tools (RG viewer, Frame Debugger and Profiler). The names are now also identical between RG and non-RG/Compatibility passes. This PR fixes a number of potential bugs with regards to setting ScriptableRenderPass.profilerSampler to null, it fixes incorrect profiler scopes (inside the static method shared between RG and non-RG), improves more pass names using the naming convention from the previous PR and could have a very minor performance benefit in release build. In this PR, we separately store the passName and the profileSampler because these samplers can be null in the release build. Therefore, we can't just use sampler.name. By using the ScriptableRenderPass properties (passName, profileSampler) these names are kept in sync automatically. The default name for a (user created) sub-class of a ScriptableRenderPass is now more descriptive, using the sub-class name instead of the base class. The default RenderFeature that users create from the menu is updated to using this. The profile samplers being null in a release build was introduced by this PR as an optimization https://github.cds.internal.unity3d.com/unity/unity/pull/41824 However, in URP, many samplers are created differently, leading to limited performance gain, and inconsistency with some samplers being null and some not in the release build. This PR improves this so more samplers are null in a release build. Potentially gaining around 0.1ms main thread CPU in release on low end CPUs. Before and after this PR (no change), URP, ProfileSamplers references are kept in a few different places. Depending on that, the samplers can be nullified in the release build. - ScriptableRenderPass.profileSampler: used by RG and non-RG, in this PR, when using RG, they are nullified - ProfileSampler.Get(URPProfileId): same pattern as with HDRP, these are nullified by the change from Julien - RenderGraph.m_DefaultProfilingSamplers: created automatically when adding a pass without a sampler, these are nullified - Other places of the URP code (outside of a pass): these are NOT nullified. Likely, part of the optimization for HDRP came from the dictionary .TryGetValue with large pass dictionaries. The samplers kept outside of dictionary don't have this cost. So it likely will not improve performance to clean up these. Since some samplers can be null in the release build, it's bad practice (errors!) to use sampler.name. However, the earlier PR to nullify this might have introduced issues already. To avoid issues with user project that do this (very low chance), in this PR, ScriptableRenderPass.profileSampler is only nullified in RenderGraph mode (not Compatibility). This makes it a bit more complicated to ensure that the pass name and the sampler name (shown in profiler) don't get out of sync. However, it's confusing for users to see different names in different tools and complicates matching them during analysis. Hence the changes in this PR. The RenderGraph add pass methods (eg AddRasterPass, ...) have overloads for both passing a passName and a sampler, and one with only a passName. The use of passing only the passName seems recommended for users since they can't go out of sync, RG creates a new profiler with that name, and will nullify it in a release build, and users don't have access to the profilerSampler. In non release builds, there might be a small perf overhead of this practice since the dictionary can become large.
1 parent 868e580 commit 9e3a043

32 files changed

+222
-176
lines changed

Packages/com.unity.render-pipelines.universal/Editor/RendererFeatures/NewRendererFeature.cs.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public class #SCRIPTNAME# : ScriptableRendererFeature
2323
// FrameData is a context container through which URP resources can be accessed and managed.
2424
public override void RecordRenderGraph(RenderGraph renderGraph, ContextContainer frameData)
2525
{
26-
const string passName = "Custom Render Pass";
26+
const string passName = "Render Custom Pass";
2727

2828
// This adds a raster render pass to the graph, specifying the name and the data type that will be passed to the ExecutePass function.
2929
using (var builder = renderGraph.AddRasterRenderPass<PassData>(passName, out var passData))

Packages/com.unity.render-pipelines.universal/Runtime/Decal/DBuffer/DBufferRenderPass.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@ public override void RecordRenderGraph(RenderGraph renderGraph, ContextContainer
238238
TextureHandle depthTarget = renderer.renderingModeActual == RenderingMode.Deferred ? resourceData.activeDepthTexture :
239239
(cameraData.cameraTargetDescriptor.msaaSamples > 1 ? resourceData.dBufferDepth : resourceData.activeDepthTexture);
240240

241-
using (var builder = renderGraph.AddRasterRenderPass<PassData>(profilingSampler.name, out var passData, profilingSampler))
241+
using (var builder = renderGraph.AddRasterRenderPass<PassData>(passName, out var passData, profilingSampler))
242242
{
243243
InitPassData(ref passData);
244244

Packages/com.unity.render-pipelines.universal/Runtime/Decal/DBuffer/DecalForwardEmissivePass.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ private static void ExecutePass(RasterCommandBuffer cmd, PassData passData, Rend
7979

8080
public override void RecordRenderGraph(RenderGraph renderGraph, ContextContainer frameData)
8181
{
82-
using (var builder = renderGraph.AddRasterRenderPass<PassData>(profilingSampler.name, out var passData, profilingSampler))
82+
using (var builder = renderGraph.AddRasterRenderPass<PassData>(passName, out var passData, profilingSampler))
8383
{
8484
UniversalResourceData resourceData = frameData.Get<UniversalResourceData>();
8585
UniversalRenderingData renderingData = frameData.Get<UniversalRenderingData>();

Packages/com.unity.render-pipelines.universal/Runtime/Decal/ScreenSpace/DecalGBufferRenderPass.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ public override void RecordRenderGraph(RenderGraph renderGraph, ContextContainer
164164
UniversalResourceData resourceData = frameData.Get<UniversalResourceData>();
165165
TextureHandle cameraDepthTexture = resourceData.cameraDepthTexture;
166166

167-
using (var builder = renderGraph.AddRasterRenderPass<PassData>(profilingSampler.name, out var passData, profilingSampler))
167+
using (var builder = renderGraph.AddRasterRenderPass<PassData>(passName, out var passData, profilingSampler))
168168
{
169169
UniversalRenderingData renderingData = frameData.Get<UniversalRenderingData>();
170170
UniversalCameraData cameraData = frameData.Get<UniversalCameraData>();

Packages/com.unity.render-pipelines.universal/Runtime/Decal/ScreenSpace/DecalScreenSpaceRenderPass.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ public override void RecordRenderGraph(RenderGraph renderGraph, ContextContainer
110110

111111
TextureHandle cameraDepthTexture = resourceData.cameraDepthTexture;
112112

113-
using (var builder = renderGraph.AddRasterRenderPass<PassData>(profilingSampler.name, out var passData, profilingSampler))
113+
using (var builder = renderGraph.AddRasterRenderPass<PassData>(passName, out var passData, profilingSampler))
114114
{
115115
UniversalRenderingData renderingData = frameData.Get<UniversalRenderingData>();
116116
UniversalCameraData cameraData = frameData.Get<UniversalCameraData>();

Packages/com.unity.render-pipelines.universal/Runtime/Passes/AdditionalLightsShadowCasterPass.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -895,7 +895,7 @@ internal TextureHandle Render(RenderGraph graph, ContextContainer frameData)
895895
UniversalLightData lightData = frameData.Get<UniversalLightData>();
896896
UniversalShadowData shadowData = frameData.Get<UniversalShadowData>();
897897

898-
using (var builder = graph.AddRasterRenderPass<PassData>(profilingSampler.name, out var passData, profilingSampler))
898+
using (var builder = graph.AddRasterRenderPass<PassData>(passName, out var passData, profilingSampler))
899899
{
900900
InitPassData(ref passData, cameraData, lightData, shadowData);
901901
InitRendererLists(ref renderingData.cullResults, ref passData, default(ScriptableRenderContext), graph, true);

Packages/com.unity.render-pipelines.universal/Runtime/Passes/CapturePass.cs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,10 @@ namespace UnityEngine.Rendering.Universal
1313
internal class CapturePass : ScriptableRenderPass
1414
{
1515
RTHandle m_CameraColorHandle;
16-
const string m_ProfilerTag = "Capture Pass";
17-
private static readonly ProfilingSampler m_ProfilingSampler = new ProfilingSampler(m_ProfilerTag);
16+
1817
public CapturePass(RenderPassEvent evt)
1918
{
20-
base.profilingSampler = new ProfilingSampler(nameof(CapturePass));
19+
base.profilingSampler = new ProfilingSampler("Capture Camera output");
2120
renderPassEvent = evt;
2221
}
2322

@@ -29,7 +28,7 @@ public override void Execute(ScriptableRenderContext context, ref RenderingData
2928

3029
m_CameraColorHandle = renderingData.cameraData.renderer.GetCameraColorBackBuffer(cmdBuf);
3130

32-
using (new ProfilingScope(cmdBuf, m_ProfilingSampler))
31+
using (new ProfilingScope(cmdBuf, profilingSampler))
3332
{
3433
var colorAttachmentIdentifier = m_CameraColorHandle.nameID;
3534
var captureActions = renderingData.cameraData.captureActions;
@@ -44,8 +43,6 @@ private class UnsafePassData
4443
public IEnumerator<Action<RenderTargetIdentifier, CommandBuffer>> captureActions;
4544
}
4645

47-
const string k_UnsafePassName = "CapturePass (Render Graph Unsafe Pass)";
48-
4946
// This function needs to add an unsafe render pass to Render Graph because a raster render pass, which is typically
5047
// used for rendering with Render Graph, cannot perform the texture readback operations performed with the command
5148
// buffer in CameraTextureProvider. Unsafe passes can do certain operations that raster render passes cannot do and
@@ -55,7 +52,7 @@ public override void RecordRenderGraph(RenderGraph renderGraph, ContextContainer
5552
UniversalResourceData resourceData = frameData.Get<UniversalResourceData>();
5653
UniversalCameraData cameraData = frameData.Get<UniversalCameraData>();
5754

58-
using (var builder = renderGraph.AddUnsafePass<UnsafePassData>(k_UnsafePassName, out var passData, profilingSampler))
55+
using (var builder = renderGraph.AddUnsafePass<UnsafePassData>(passName, out var passData, profilingSampler))
5956
{
6057
// Setup up the pass data with cameraColor, which has the correct orientation and position in a built player
6158
passData.source = resourceData.cameraColor;

Packages/com.unity.render-pipelines.universal/Runtime/Passes/ColorGradingLutPass.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public class ColorGradingLutPass : ScriptableRenderPass
2929
/// <seealso cref="PostProcessData"/>
3030
public ColorGradingLutPass(RenderPassEvent evt, PostProcessData data)
3131
{
32-
base.profilingSampler = new ProfilingSampler("Blit Color LUT");
32+
profilingSampler = new ProfilingSampler("Blit Color LUT");
3333
renderPassEvent = evt;
3434
overrideCameraTarget = true;
3535

@@ -280,7 +280,7 @@ internal void Render(RenderGraph renderGraph, ContextContainer frameData, out Te
280280
UniversalCameraData cameraData = frameData.Get<UniversalCameraData>();
281281
UniversalPostProcessingData postProcessingData= frameData.Get<UniversalPostProcessingData>();
282282

283-
using (var builder = renderGraph.AddRasterRenderPass<PassData>(base.profilingSampler.name, out var passData, base.profilingSampler))
283+
using (var builder = renderGraph.AddRasterRenderPass<PassData>(passName, out var passData, profilingSampler))
284284
{
285285
this.ConfigureDescriptor(in postProcessingData, out var lutDesc, out var filterMode);
286286
internalColorLut = UniversalRenderer.CreateRenderGraphTexture(renderGraph, lutDesc, "_InternalGradingLut", true, filterMode);

Packages/com.unity.render-pipelines.universal/Runtime/Passes/CopyColorPass.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,8 @@ public class CopyColorPass : ScriptableRenderPass
3535
/// <seealso cref="Downsampling"/>
3636
public CopyColorPass(RenderPassEvent evt, Material samplingMaterial, Material copyColorMaterial = null, string customPassName = null)
3737
{
38-
var passName = customPassName != null ? customPassName : "Copy Color";
38+
profilingSampler = customPassName != null ? new ProfilingSampler(customPassName) : ProfilingSampler.Get(URPProfileId.CopyColor);
3939

40-
base.profilingSampler = new ProfilingSampler(passName);
4140
m_PassData = new PassData();
4241

4342
m_SamplingMaterial = samplingMaterial;
@@ -214,7 +213,7 @@ internal void RenderToExistingTexture(RenderGraph renderGraph, ContextContainer
214213

215214
private void RenderInternal(RenderGraph renderGraph, in TextureHandle destination, in TextureHandle source, bool useProceduralBlit)
216215
{
217-
using (var builder = renderGraph.AddRasterRenderPass<PassData>(profilingSampler.name, out var passData, profilingSampler))
216+
using (var builder = renderGraph.AddRasterRenderPass<PassData>(passName, out var passData, profilingSampler))
218217
{
219218
passData.destination = destination;
220219
builder.SetRenderAttachment(destination, 0, AccessFlags.Write);

Packages/com.unity.render-pipelines.universal/Runtime/Passes/CopyDepthPass.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public class CopyDepthPass : ScriptableRenderPass
4141
/// <seealso cref="RenderPassEvent"/>
4242
public CopyDepthPass(RenderPassEvent evt, Shader copyDepthShader, bool shouldClear = false, bool copyToDepth = false, bool copyResolvedDepth = false, string customPassName = null)
4343
{
44-
base.profilingSampler = customPassName != null ? new ProfilingSampler(customPassName) : new ProfilingSampler("Copy Depth");
44+
profilingSampler = customPassName != null ? new ProfilingSampler(customPassName) : ProfilingSampler.Get(URPProfileId.CopyDepth);
4545
m_PassData = new PassData();
4646
CopyToDepth = copyToDepth;
4747
m_CopyDepthMaterial = copyDepthShader != null ? CoreUtils.CreateEngineMaterial(copyDepthShader) : null;

0 commit comments

Comments
 (0)