How to sample a texture in HDRP custom post process

Hi, I’m building a post process that takes in an input image and another texture that I need to sample. Now the input image sampling is alright (1 to 1 with the grayscale post-process example in HDRP scripting docs), but I can’t figure out how am I supposed to sample a normal texture.

This is what I’ve got so far:
Shader:

        TEXTURE2D_X(_InputTexture);
        TEXTURE2D_X(_WorldMask);
......
        float4 maskTexture = SAMPLE_TEXTURE2D_X(_WorldMask, s_linear_clamp_sampler, uv);

Post Process:

public BoolParameter UseDebug = new BoolParameter(false);
public TextureParameter DebugTexture = new TextureParameter(null);

public override void Render(CommandBuffer cmd, HDCamera hdCamera, RTHandle source, RTHandle destination)
{
    var cam = hdCamera.camera;
    var material = GetMaterial();
    material.SetTexture(_MaskProperty, GetMask());
    material.SetTexture(_InputTextureProperty, source);
    HDUtils.DrawFullScreen(cmd, material, destination);
}

private Texture GetMask()
{
#if UNITY_EDITOR
    return UseDebug.value ? DebugTexture.value : WorldMask;
#else
    return WorldMask;
#endif
}

When I render the image it is just gray-ish, regardless of what texture I put in. Does it have something to do with that RTHandle type?
There aren’t many resources on HDRP shader scripting I could find.

Update: replacing SAMPLE_TEXTURE2D_X with SAMPLE_TEXTURE2D and TEXTURE2D_X with TEXTURE2D makes it work.
Now anyone know why that is?

SAMPLE_TEXTURE2D_X is meant to be used with texture arrays which _WorldMask isn’t. Regular textures should just use SAMPLE_TEXTURE2D (ie. mask textures).

1 Like