I've been attempting to create this stencil/mask buffer using Unity's HDRP C# Custom Pass feature and I've been having trouble getting it to work ONLY in build mode. Here is my code:
In my Setup function, I create the mask buffer
this.maskBuffer = RTHandles.Alloc(
Vector2.one,
TextureXR.slices,
dimension: TextureDimension.Tex2D,
colorFormat: GraphicsFormat.R16G16B16A16_SFloat,
useDynamicScale: true,
name: "MaskBuffer"
);
and in my Execute function, I create a Renderer List to build the stencil using a layer mask:
CoreUtils.SetRenderTarget(ctx.cmd, maskBuffer, ClearFlag.Color, Color.black);
var passes = new[] { new ShaderTagId("Forward"), new ShaderTagId("GBuffer"), new ShaderTagId("SRPDefaultUnlit") };
var mat = new Material(Shader.Find("HDRP/Unlit"));
mat.SetColor("_BaseColor", Color.white);
var desc = new RendererListDesc(passes, ctx.cullingResults, ctx.hdCamera.camera)
{
layerMask = maskLayer,
renderQueueRange = RenderQueueRange.all,
overrideMaterialPassIndex = 0,
overrideMaterial = mat
};
var list = ctx.renderContext.CreateRendererList(desc);
ctx.cmd.DrawRendererList(list);
And then I'm passing the buffer to a shader graph shader that I made.
Let me be clear: this code does work in the editor. The only thing that does not not work is the mask buffer when I build the game. In the build, the shader itself works because I can output a solid color just fine. I've also tried copying the camera's color buffer into the mask buffer temporarily and that works just fine too. But for some reason, I absolutely cannot get the stencil itself to draw. It always just renders as completely black.
What I've tried:
- Removing the layer mask (maybe layers didn't get transferred to the build mode of the game)
- Checking the build/runtime logs (there are no errors)
- Using a different setup which, once again, works in the editor, but not in build mode:
CoreUtils.SetRenderTarget(ctx.cmd, this.maskBuffer, ClearFlag.Color);
CustomPassUtils.DrawRenderers(ctx, this.maskLayer);
- Upgrading Unity versions (I tried two different versions of 6.2 and the newest 6.3)
- Using many different variations of the
colorFormatand disablinguseDynamicScalein theRTHandles.Allocfunction - Changing the
Injection Pointto different values in the Custom Pass Volume
Any help would be greatly appreciated. I'm nearly convinced that this is either some sort of config issue with the default HDRP project, or I've found a bug in the engine.
Thank you in advance!!
P.S. Some of this code may be slightly inefficient but I'm just using it for testing purposes