Skip to content

Commit 0c9aaf6

Browse files
cinightEvergreen
authored andcommitted
[URP RenderGraph] Sample code fixes
Only URP package doc and sample scene script changes. No engine code changes. - Fixes GC.Alloc that happens every frame in URP package doc and package sample scene script [slack](https://unity.slack.com/archives/C010YL29H8F/p1718105805466559) - Blur sample do not use frame buffer fetch. Just use normal blit [UUM-74051](https://jira.unity3d.com/browse/UUM-74051)
1 parent 812fe0f commit 0c9aaf6

File tree

3 files changed

+60
-72
lines changed

3 files changed

+60
-72
lines changed

Packages/com.unity.render-pipelines.universal/Documentation~/renderer-features/create-custom-renderer-feature.md

Lines changed: 46 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -311,10 +311,7 @@ This section demonstrates how to implement the settings for the custom blur rend
311311

312312
// Blit from the camera color to the render graph texture,
313313
// using the first shader pass.
314-
builder.SetRenderFunc((PassData data, RasterGraphContext context) =>
315-
{
316-
Blitter.BlitTexture(context.cmd, data.src, m_ScaleBias, data.material, 0);
317-
});
314+
builder.SetRenderFunc((PassData data, RasterGraphContext context) => ExecutePass(data, context, 0));
318315
}
319316
```
320317

@@ -324,33 +321,25 @@ This section demonstrates how to implement the settings for the custom blur rend
324321
private Vector4 m_ScaleBias = new Vector4(1f, 1f, 0f, 0f);
325322
```
326323

327-
2. In the `RecordRenderGraph` method, using the `builder` variable, add the render pass for the horizontal blur. This pass uses the output of the previous pass as its input, it does that using the `FrameBufferFetch` method. In this example, using this method lets URP merge two blur passes into a single render pass. Refer to the complete shader code for the implementation details.
324+
2. In the `RecordRenderGraph` method, using the `builder` variable, add the render pass for the horizontal blur. This pass uses the output of the previous pass as its input. Refer to the complete shader code for the implementation details.
328325

329326
```C#
330327
// Horizontal blur pass
331-
using (var builder = renderGraph.AddRasterRenderPass<PassData>(k_HorizontalPassName,
332-
out var passData))
328+
using (var builder = renderGraph.AddRasterRenderPass<PassData>(k_HorizontalPassName, out var passData))
333329
{
334-
// Reset unused passData fields
335-
passData.src = TextureHandle.nullHandle;
336-
337-
// Use the same material as the previous pass
330+
// Configure pass data
331+
passData.src = dst;
338332
passData.material = material;
339333

340-
// Use the output of the previous pass as the input,
341-
// and bind it as FrameBufferFetch input
342-
builder.SetInputAttachment(dst, 0);
343-
334+
// Use the output of the previous pass as the input
335+
builder.UseTexture(passData.src);
336+
344337
// Use the input texture of the previous pass as the output
345338
builder.SetRenderAttachment(srcCamColor, 0);
346339

347340
// Blit from the render graph texture to the camera color,
348-
// using the second shader pass,
349-
// which reads the input texture using the FrameBufferFetch method.
350-
builder.SetRenderFunc((PassData data, RasterGraphContext context) =>
351-
{
352-
Blitter.BlitTexture(context.cmd, m_ScaleBias, data.material, 1);
353-
});
341+
// using the second shader pass.
342+
builder.SetRenderFunc((PassData data, RasterGraphContext context) => ExecutePass(data, context, 1));
354343
}
355344
```
356345

@@ -513,7 +502,7 @@ public class BlurRendererFeature : ScriptableRendererFeature
513502
material = new Material(shader);
514503
blurRenderPass = new BlurRenderPass(material, settings);
515504

516-
blurRenderPass.renderPassEvent = RenderPassEvent.AfterRenderingSkybox;
505+
blurRenderPass.renderPassEvent = RenderPassEvent.BeforeRenderingPostProcessing;
517506
}
518507

519508
public override void AddRenderPasses(ScriptableRenderer renderer,
@@ -569,7 +558,7 @@ public class BlurRenderPass : ScriptableRenderPass
569558
private const string k_VerticalPassName = "VerticalBlurRenderPass";
570559
private const string k_HorizontalPassName = "HorizontalBlurRenderPass";
571560

572-
private Vector4 m_ScaleBias = new Vector4(1f, 1f, 0f, 0f);
561+
private static Vector4 m_ScaleBias = new Vector4(1f, 1f, 0f, 0f);
573562

574563
private BlurSettings defaultSettings;
575564
private Material material;
@@ -606,6 +595,11 @@ public class BlurRenderPass : ScriptableRenderPass
606595
internal Material material;
607596
}
608597

598+
private static void ExecutePass(PassData data, RasterGraphContext context, int pass)
599+
{
600+
Blitter.BlitTexture(context.cmd, data.src, m_ScaleBias, data.material, pass);
601+
}
602+
609603
public override void RecordRenderGraph(RenderGraph renderGraph,
610604
ContextContainer frameData)
611605
{
@@ -648,36 +642,25 @@ public class BlurRenderPass : ScriptableRenderPass
648642

649643
// Blit from the camera color to the render graph texture,
650644
// using the first shader pass.
651-
builder.SetRenderFunc((PassData data, RasterGraphContext context) =>
652-
{
653-
Blitter.BlitTexture(context.cmd, data.src, m_ScaleBias, data.material, 0);
654-
});
645+
builder.SetRenderFunc((PassData data, RasterGraphContext context) => ExecutePass(data, context, 0));
655646
}
656-
647+
657648
// Horizontal blur pass
658-
using (var builder = renderGraph.AddRasterRenderPass<PassData>(k_HorizontalPassName,
659-
out var passData))
649+
using (var builder = renderGraph.AddRasterRenderPass<PassData>(k_HorizontalPassName, out var passData))
660650
{
661-
// Reset unused passData fields
662-
passData.src = TextureHandle.nullHandle;
663-
664-
// Use the same material as the previous pass
651+
// Configure pass data
652+
passData.src = dst;
665653
passData.material = material;
666654

667-
// Use the output of the previous pass as the input,
668-
// and bind it as FrameBufferFetch input
669-
builder.SetInputAttachment(dst, 0);
670-
655+
// Use the output of the previous pass as the input
656+
builder.UseTexture(passData.src);
657+
671658
// Use the input texture of the previous pass as the output
672659
builder.SetRenderAttachment(srcCamColor, 0);
673660

674661
// Blit from the render graph texture to the camera color,
675-
// using the second shader pass,
676-
// which reads the input texture using the FrameBufferFetch method.
677-
builder.SetRenderFunc((PassData data, RasterGraphContext context) =>
678-
{
679-
Blitter.BlitTexture(context.cmd, m_ScaleBias, data.material, 1);
680-
});
662+
// using the second shader pass.
663+
builder.SetRenderFunc((PassData data, RasterGraphContext context) => ExecutePass(data, context, 1));
681664
}
682665
}
683666
}
@@ -735,6 +718,23 @@ Shader "CustomEffects/Blur"
735718
736719
return float4(color.rgb / (BLUR_SAMPLES + 1), 1);
737720
}
721+
722+
float4 BlurHorizontal (Varyings input) : SV_Target
723+
{
724+
const float BLUR_SAMPLES = 64;
725+
const float BLUR_SAMPLES_RANGE = BLUR_SAMPLES / 2;
726+
727+
UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(input);
728+
float3 color = 0;
729+
float blurPixels = _HorizontalBlur * _ScreenParams.x;
730+
for(float i = -BLUR_SAMPLES_RANGE; i <= BLUR_SAMPLES_RANGE; i++)
731+
{
732+
float2 sampleOffset =
733+
float2 ((blurPixels / _BlitTexture_TexelSize.z) * (i / BLUR_SAMPLES_RANGE), 0);
734+
color += SAMPLE_TEXTURE2D(_BlitTexture, sampler_LinearClamp, input.texcoord + sampleOffset).rgb;
735+
}
736+
return float4(color / (BLUR_SAMPLES + 1), 1);
737+
}
738738

739739
ENDHLSL
740740

@@ -757,30 +757,12 @@ Shader "CustomEffects/Blur"
757757
758758
Pass
759759
{
760-
Name "BlurPassHorizontal_FrameBufferFetch"
760+
Name "BlurPassHorizontal"
761761

762762
HLSLPROGRAM
763763
764764
#pragma vertex Vert
765-
#pragma fragment Frag
766-
767-
FRAMEBUFFER_INPUT_X_HALF(0);
768-
769-
float4 Frag(Varyings input) : SV_Target
770-
{
771-
const float BLUR_SAMPLES = 64;
772-
const float BLUR_SAMPLES_RANGE = BLUR_SAMPLES / 2;
773-
774-
UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(input);
775-
float3 color = 0;
776-
float blurPixels = _HorizontalBlur * _ScreenParams.x;
777-
for(float i = -BLUR_SAMPLES_RANGE; i <= BLUR_SAMPLES_RANGE; i++)
778-
{
779-
float2 sampleOffset = float2 ((blurPixels / 1) * (i / BLUR_SAMPLES_RANGE), 0);
780-
color += LOAD_FRAMEBUFFER_X_INPUT(0, input.positionCS.xy + sampleOffset).rgb;
781-
}
782-
return float4(color / (BLUR_SAMPLES + 1), 1);
783-
}
765+
#pragma fragment BlurHorizontal
784766
785767
ENDHLSL
786768
}

Packages/com.unity.render-pipelines.universal/Samples~/URPPackageSamples/RendererFeatures/BlitToRTHandle/BlitToRTHandlePass.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,9 @@ class PassData
1111
{
1212
public TextureHandle source;
1313
public Material material;
14-
public Vector4 scaleBias;
1514
}
1615

17-
private Vector4 m_ScaleBias = new Vector4(1f, 1f, 0f, 0f);
16+
private static Vector4 m_ScaleBias = new Vector4(1f, 1f, 0f, 0f);
1817
private ProfilingSampler m_ProfilingSampler = new ProfilingSampler("BlitToRTHandle_CopyColor");
1918
private RTHandle m_InputHandle;
2019
private RTHandle m_OutputHandle;
@@ -93,15 +92,14 @@ public override void RecordRenderGraph(RenderGraph renderGraph, ContextContainer
9392

9493
passData.source = source;
9594
passData.material = m_Material;
96-
passData.scaleBias = m_ScaleBias;
9795
builder.UseTexture(source, AccessFlags.Read); // Set the camera color as the input
9896
builder.SetRenderAttachment(destination, 0, AccessFlags.Write); // Set the target texture
9997
builder.SetGlobalTextureAfterPass(destination, m_OutputId); // Make the output texture available for the shaders in the scene
10098

10199
builder.SetRenderFunc((PassData data, RasterGraphContext context) =>
102100
{
103101
// Blit the input texture to the destination target specified in the SetRenderAttachment method
104-
Blitter.BlitTexture(context.cmd, data.source, data.scaleBias, data.material, 0);
102+
Blitter.BlitTexture(context.cmd, data.source, m_ScaleBias, data.material, 0);
105103
});
106104
}
107105

Packages/com.unity.render-pipelines.universal/Samples~/URPPackageSamples/RendererFeatures/DepthBlit/DepthBlitCopyDepthPass.cs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ class PassData
2424
public TextureHandle source;
2525
public Vector4 scaleBias;
2626
public int depthBufferId;
27+
public GlobalKeyword keyword_DepthMsaa2;
28+
public GlobalKeyword keyword_DepthMsaa4;
29+
public GlobalKeyword keyword_DepthMsaa8;
30+
public GlobalKeyword keyword_OutputDepth;
2731
}
2832

2933
public DepthBlitCopyDepthPass(RenderPassEvent evt, Shader copyDepthShader, RTHandle destination)
@@ -103,6 +107,10 @@ public override void RecordRenderGraph(RenderGraph renderGraph, ContextContainer
103107
passData.source = src;
104108
passData.scaleBias = m_ScaleBias;
105109
passData.depthBufferId = m_DepthBufferId;
110+
passData.keyword_DepthMsaa2 = m_Keyword_DepthMsaa2;
111+
passData.keyword_DepthMsaa4 = m_Keyword_DepthMsaa4;
112+
passData.keyword_DepthMsaa8 = m_Keyword_DepthMsaa8;
113+
passData.keyword_OutputDepth = m_Keyword_OutputDepth;
106114

107115
builder.UseTexture(src, AccessFlags.Read);
108116
builder.SetRenderAttachment(dest, 0, AccessFlags.Write);
@@ -114,12 +122,12 @@ public override void RecordRenderGraph(RenderGraph renderGraph, ContextContainer
114122
// Enable an MSAA shader keyword based on the source texture MSAA sample count
115123
RTHandle sourceTex = data.source;
116124
int cameraSamples = sourceTex.rt.antiAliasing;
117-
context.cmd.SetKeyword(m_Keyword_DepthMsaa2, cameraSamples == 2);
118-
context.cmd.SetKeyword(m_Keyword_DepthMsaa4, cameraSamples == 4);
119-
context.cmd.SetKeyword(m_Keyword_DepthMsaa8, cameraSamples == 8);
125+
context.cmd.SetKeyword(data.keyword_DepthMsaa2, cameraSamples == 2);
126+
context.cmd.SetKeyword(data.keyword_DepthMsaa4, cameraSamples == 4);
127+
context.cmd.SetKeyword(data.keyword_DepthMsaa8, cameraSamples == 8);
120128

121129
// This example does not copy the depth values back to the depth buffer, so we disable this keyword.
122-
context.cmd.SetKeyword(m_Keyword_OutputDepth, false);
130+
context.cmd.SetKeyword(data.keyword_OutputDepth, false);
123131

124132
// Bind the depth buffer to the material
125133
data.copyDepthMaterial.SetTexture(data.depthBufferId, data.source);

0 commit comments

Comments
 (0)