(HDRP) Fullscreen Custom Pass & Buffer Scaling Issue With Shader Graph

I have been wanting to make a simple fullscreen custom pass for HDRP, with a shader made in shader graph. The custom pass would render objects to a buffer, and pass the buffer to the fullscreen material, like so:

CoreUtils.SetRenderTarget(ctx.cmd, buffer, ctx.cameraDepthBuffer, ClearFlag.Color);

for (int i = 0; i < renderer.sharedMaterials.Length; i++)
    ctx.cmd.DrawRenderer(renderer, rendererMaterial, i, 6);

ctx.propertyBlock.SetTexture("_Buffer", buffer);

HDUtils.DrawFullScreen(ctx.cmd, fullscreenMaterial, ctx.cameraColorBuffer, ctx.cameraDepthBuffer, shaderPassId: pass, properties: ctx.propertyBlock);

And the shader graph samples it simply like so:

Unfortunately I ran into issues with this when changing window resolution (like Scene & Game view), where the buffer scaling is completely off:

In hlsl, this is fixed by multiplying the UVs with _RTHandleScale, as noted in the custom pass troubleshooting. But I’m not sure how to access _RTHandleScale through shader graph. I’ve tried importing "Packages/com.unity.render-pipelines.high-definition/Runtime/ShaderLibrary/ShaderVariablesGlobal.cs.hlsl" into a custom function, but I get an exception of "Redefinition of '_ViewMatrix' at line 20.

Thank you for your help!

Sorry if this is not exactly relating to your issue, but there is a known RT handles scale bug that I’ve reported and is currently in progress to be fixed:

Unity Issue Tracker - [HDRP] Wrong scene-color sampling when injecting a custom pass on Before PostProcess

1 Like

Figured it out - apparently the issue lied within the shader graph node preview. Adding an if check inside the custom function worked, no includes required.

void RTHandleScale_float(out float2 scale)
{
    #if SHADERGRAPH_PREVIEW
    scale = float2(0, 0);
    #else
    scale = _RTHandleScale.xy;
    #endif
}