I am trying to render a buffer mask using a custom render pass and set a global shader texture to use this texture. I have been able to achieve the masking I want, but when I try to pass the texture to the shaders it doesnt seem to be showing up. Im not sure what i am doing wrong. I was able to achieve this in URP with a custom pass almost identical to the setup here minus the obvious differences in creating a custom pass between URP and HDRP. The only real difference I can see is I was using a temporary render texture in URP with GetTemporaryRT
Here is the meat of my custom render pass.
protected override void Setup(ScriptableRenderContext renderContext, CommandBuffer cmd)
{
shaderTags = new ShaderTagId[3]
{
new ShaderTagId("Forward"),
new ShaderTagId("ForwardOnly"),
new ShaderTagId("")
};
maskBuffer = RTHandles.Alloc(
Vector2.one, TextureXR.slices, dimension: TextureXR.dimension,
colorFormat: GraphicsFormat.R16G16B16A16_SFloat,
useDynamicScale: true, enableRandomWrite: true,name: "_MaskBuffer"
);
maskDepthBuffer = RTHandles.Alloc(
Vector2.one, TextureXR.slices, dimension: TextureXR.dimension,
colorFormat: GraphicsFormat.R16_UInt, useDynamicScale: true,
name: "_MaskBufferDepth", depthBufferBits: DepthBits.Depth16
);
}
protected override void Execute(ScriptableRenderContext renderContext, CommandBuffer cmd, HDCamera hdCamera, CullingResults cullingResult)
{
if (staticMaterial == null || dynamicMaterial == null || (!drawStatic && !drawDynamic))
{
return;
}
//This is not working
//*****
cmd.SetGlobalTexture("_MaskBuffer", maskBuffer);
//I have also tried:
//cmd.SetGlobalTexture("_MaskBuffer", maskBuffer.nameID);
//cmd.SetGlobalTexture("_MaskBuffer", maskBuffer.rt);
//Shader.SetGlobalTexture("_MaskBuffer", maskBuffer);
//Shader.SetGlobalTexture("_MaskBuffer", maskBuffer.rt);
//*****
CoreUtils.SetRenderTarget(cmd, maskBuffer, maskDepthBuffer, ClearFlag.All);
if (drawStatic)
{
Render(renderContext, cmd, hdCamera, cullingResult, staticMask, staticMaterial);
}
if (drawDynamic)
{
Render(renderContext, cmd, hdCamera, cullingResult, dynamicMask, dynamicMaterial);
}
SetCameraRenderTarget(cmd);
}
void Render(ScriptableRenderContext renderContext, CommandBuffer cmd, HDCamera hdCamera, CullingResults cullingResult, LayerMask LayerMask, Material material)
{
var renstateBlock = new RenderStateBlock(RenderStateMask.Depth)
{
depthState = new DepthState(true, depthCompareFunction),
stencilState = new StencilState(false),
};
var result = new RendererListDesc(shaderTags, cullingResult, hdCamera.camera)
{
rendererConfiguration = PerObjectData.None,
renderQueueRange = GetRenderQueueRange(renderQueueType),
sortingCriteria = Sorting,
excludeObjectMotionVectors = false,
overrideMaterial = material,
overrideMaterialPassIndex = (material != null) ? material.FindPass(overrideMaterialPassName) : 0,
stateBlock = renstateBlock,
layerMask = LayerMask,
};
// Render objects into the custom buffer:
HDUtils.DrawRendererList(renderContext, cmd, RendererList.Create(result));
}
Here is the result of this pass I would like to set as a global texture as seen via frame debugger
Here is the shader and the custom function I am using to retrieve the texture.
The expected result is for the render texture to appear on the cube, but as you can see it isnt. Im not sure what I am doing wrong.