Official Example for Blitting Screen to Texture with Render Graph in Unity 6000.0.12f1 doesn't work

Been pulling my hair out trying to get the code posted on this page to work:

https://docs.unity3d.com/Packages/com.unity.render-pipelines.universal@17.0/manual/render-graph-write-render-pass.html

It’s giving me this stack:

ArgumentNullException: Value cannot be null.
Parameter name: value
UnityEngine.Bindings.ThrowHelper.ThrowArgumentNullException (System.Object obj, System.String parameterName) (at <5a87366a6dc74b3aa0e0421cf80e3ae5>:0)
UnityEngine.MaterialPropertyBlock.SetTextureImpl (System.Int32 name, UnityEngine.Texture value) (at <5a87366a6dc74b3aa0e0421cf80e3ae5>:0)
UnityEngine.MaterialPropertyBlock.SetTexture (System.Int32 nameID, UnityEngine.Texture value) (at <5a87366a6dc74b3aa0e0421cf80e3ae5>:0)
UnityEngine.Rendering.Blitter.BlitTexture (UnityEngine.Rendering.CommandBuffer cmd, UnityEngine.Rendering.RTHandle source, UnityEngine.Vector4 scaleBias, UnityEngine.Material material, System.Int32 pass) (at ./Library/PackageCache/com.unity.render-pipelines.core/Runtime/Utilities/Blitter.cs:437)
UnityEngine.Rendering.Blitter.BlitTexture (UnityEngine.Rendering.CommandBuffer cmd, UnityEngine.Rendering.RTHandle source, UnityEngine.Vector4 scaleBias, System.Single mipLevel, System.Boolean bilinear) (at ./Library/PackageCache/com.unity.render-pipelines.core/Runtime/Utilities/Blitter.cs:350)
UnityEngine.Rendering.Blitter.BlitTexture (UnityEngine.Rendering.RasterCommandBuffer cmd, UnityEngine.Rendering.RTHandle source, UnityEngine.Vector4 scaleBias, System.Single mipLevel, System.Boolean bilinear) (at ./Library/PackageCache/com.unity.render-pipelines.core/Runtime/Utilities/Blitter.cs:336)
GrabScreenFeature+GrabPass.Execute (GrabScreenFeature+GrabPass+GrabPassData passData, UnityEngine.Rendering.RenderGraphModule.RasterGraphContext rasterContext) (at Assets/Script/Game/Rendering/GrabScreenFeature.cs:64)

Running the debugger, it seems like the ‘activeColorTexture’ referred in the documentation doesn’t have an external texture or an rt, so it’s trying to assign a null texture to the Blitter, it seems.

Is there something else I should be using? I’ve tried using the camera color texture as well, but it’s also not working.

Here’s my actual code:

public class GrabScreenFeature : ScriptableRendererFeature
{
    class GrabPass : ScriptableRenderPass
    {
        public GrabPass()
        {
            renderPassEvent = RenderPassEvent.AfterRenderingOpaques;
        }

        private static void Execute(GrabPassData passData, RasterGraphContext rasterContext)
        {
            RTHandle source = passData.source;
            Blitter.BlitTexture(rasterContext.cmd, source, new Vector4(1, 1, 0.0f, 0.0f), 0, false);
        }

        private class GrabPassData
        {
            public TextureHandle source;
            public TextureHandle destination;
        }

        public override void RecordRenderGraph(UnityEngine.Rendering.RenderGraphModule.RenderGraph renderGraph, UnityEngine.Rendering.ContextContainer frameData)
        {
            using (var builder = renderGraph.AddRasterRenderPass("GrabPass", out GrabPassData passData))
            {
                UniversalResourceData resourceData = frameData.Get<UniversalResourceData>();
                passData.source = resourceData.activeColorTexture;

                // Creating and assigning 
                UniversalCameraData cameraData = frameData.Get<UniversalCameraData>();
                RenderTextureDescriptor desc = cameraData.cameraTargetDescriptor;
                desc.msaaSamples = 1;
                desc.depthBufferBits = 0;
                TextureHandle destination = UniversalRenderer.CreateRenderGraphTexture(renderGraph, desc, "CopyTexture", false);
                passData.destination = destination;

                builder.UseTexture(resourceData.activeColorTexture);
                builder.SetRenderAttachment(destination, 0);
                builder.AllowPassCulling(false);

                builder.SetRenderFunc<GrabPassData>(Execute);
            }
        }
    }

    
    GrabPass grabPass;

    public override void Create()
    {
        grabPass = new GrabPass();
    }

    public override void AddRenderPasses(ScriptableRenderer renderer, ref RenderingData renderingData)
    {
        renderer.EnqueuePass(grabPass);
    }
}

Bump. Has anyone from Unity encountered this? I’ve been trying to get this working for a while and simply can’t find anything I’m doing wrong. As far as I can tell my code is the same as what’s shown in the manual.

Ok, some additional bizarre context. It’s mostly working now, but I’ll continue to get error spam in the logs anytime I select specific objects in the scene view.

I actually changed nothing about the code; I just suddenly realized it was working and didn’t know why. After I selected one of the objects in question in the viewport, suddenly the error spam started again. After unselecting, the error spam stopped. Maybe a bug in Render Graph?

These are the warnings/errors that are spammed in the log whenever certain (seemingly random) objects are selected.

UnityEngine.GUIUtility:ProcessEvent (int,intptr,bool&)
RenderPassContext: Found a rogue context, did you call EndRenderPass?
UnityEngine.GUIUtility:ProcessEvent (int,intptr,bool&)

Assertion failed
UnityEngine.Rendering.Blitter:BlitTexture (UnityEngine.Rendering.RasterCommandBuffer,UnityEngine.Rendering.RTHandle,UnityEngine.Vector4,single,bool)
IVF.PostTransparencyPassFeature/PostTransparencyGrabPass:Execute (IVF.PostTransparencyPassFeature/PostTransparencyGrabPass/PassData,UnityEngine.Rendering.RenderGraphModule.RasterGraphContext) (at Assets/Script/Game/Rendering/PostTransparencyPassFeature.cs:44)
UnityEngine.GUIUtility:ProcessEvent (int,intptr,bool&)

Render Graph Execution error
UnityEngine.GUIUtility:ProcessEvent (int,intptr,bool&)

ArgumentNullException: Value cannot be null.
Parameter name: value
UnityEngine.Bindings.ThrowHelper.ThrowArgumentNullException (System.Object obj, System.String parameterName) (at <5a87366a6dc74b3aa0e0421cf80e3ae5>:0)
UnityEngine.MaterialPropertyBlock.SetTextureImpl (System.Int32 name, UnityEngine.Texture value) (at <5a87366a6dc74b3aa0e0421cf80e3ae5>:0)
UnityEngine.MaterialPropertyBlock.SetTexture (System.Int32 nameID, UnityEngine.Texture value) (at <5a87366a6dc74b3aa0e0421cf80e3ae5>:0)
UnityEngine.Rendering.Blitter.BlitTexture (UnityEngine.Rendering.CommandBuffer cmd, UnityEngine.Rendering.RTHandle source, UnityEngine.Vector4 scaleBias, UnityEngine.Material material, System.Int32 pass) (at ./Library/PackageCache/com.unity.render-pipelines.core/Runtime/Utilities/Blitter.cs:437)
UnityEngine.Rendering.Blitter.BlitTexture (UnityEngine.Rendering.CommandBuffer cmd, UnityEngine.Rendering.RTHandle source, UnityEngine.Vector4 scaleBias, System.Single mipLevel, System.Boolean bilinear) (at ./Library/PackageCache/com.unity.render-pipelines.core/Runtime/Utilities/Blitter.cs:350)
UnityEngine.Rendering.Blitter.BlitTexture (UnityEngine.Rendering.RasterCommandBuffer cmd, UnityEngine.Rendering.RTHandle source, UnityEngine.Vector4 scaleBias, System.Single mipLevel, System.Boolean bilinear) (at ./Library/PackageCache/com.unity.render-pipelines.core/Runtime/Utilities/Blitter.cs:336)
IVF.PostTransparencyPassFeature+PostTransparencyGrabPass.Execute (IVF.PostTransparencyPassFeature+PostTransparencyGrabPass+PassData passData, UnityEngine.Rendering.RenderGraphModule.RasterGraphContext rasterContext) (at Assets/Script/Game/Rendering/PostTransparencyPassFeature.cs:44)
UnityEngine.Rendering.RenderGraphModule.RasterRenderGraphPass`1[PassData].Execute (UnityEngine.Rendering.RenderGraphModule.InternalRenderGraphContext renderGraphContext) (at ./Library/PackageCache/com.unity.render-pipelines.core/Runtime/RenderGraph/RenderGraphPass.cs:611)
UnityEngine.Rendering.RenderGraphModule.NativeRenderPassCompiler.NativePassCompiler.ExecuteGraphNode (UnityEngine.Rendering.RenderGraphModule.InternalRenderGraphContext& rgContext, UnityEngine.Rendering.RenderGraphModule.RenderGraphResourceRegistry resources, UnityEngine.Rendering.RenderGraphModule.RenderGraphPass pass) (at ./Library/PackageCache/com.unity.render-pipelines.core/Runtime/RenderGraph/Compiler/NativePassCompiler.cs:1309)
UnityEngine.Rendering.RenderGraphModule.NativeRenderPassCompiler.NativePassCompiler.ExecuteGraph (UnityEngine.Rendering.RenderGraphModule.InternalRenderGraphContext rgContext, UnityEngine.Rendering.RenderGraphModule.RenderGraphResourceRegistry resources, System.Collections.Generic.List`1[UnityEngine.Rendering.RenderGraphModule.RenderGraphPass]& passes) (at ./Library/PackageCache/com.unity.render-pipelines.core/Runtime/RenderGraph/Compiler/NativePassCompiler.cs:1411)
UnityEngine.Rendering.RenderGraphModule.RenderGraph.ExecuteNativeRenderGraph () (at ./Library/PackageCache/com.unity.render-pipelines.core/Runtime/RenderGraph/RenderGraph.Compiler.cs:42)
UnityEngine.Rendering.RenderGraphModule.RenderGraph.Execute () (at ./Library/PackageCache/com.unity.render-pipelines.core/Runtime/RenderGraph/RenderGraph.cs:1233)
UnityEngine.GUIUtility:ProcessEvent(Int32, IntPtr, Boolean&)

As an FYI make sure your render features list is not empty.

I discovered an error where an empty list of render features causes weird problems. Like hitting shift+alt+ctrl will cause the view to flash and errors to spawn.

Maybe thats related?

Best just keep at least something in the render features list.

Your code looks ok and seems to work here. There might be a bug triggered due to a specific configuration (your URP asset). Or it might be fixed in the latest patch release that I’m using. You can try the RenderGraph samples to see if other approaches work for you.