Declaring and using multiple (4) samplers for the same TextureArray in built-in render pipeline shader

Hi folks

I’m attempting to use TextureArrays in a (built-in render pipeline) shader and I’d like to have separate samplers for each of my texture array calls. I can’t find any useful information on this.

I’m not using shader graph - I’m writing hlsl code.

Essentially, my array contains 2 different kinds of textures:

  • At index is the diffuse and AO
  • At index+1 are some maps (smoothness, metallic, etc).

To reduce tiling artifacts I sample each (tiling) texture twice - one at scale 1 and the other at another scale, then blend them.

What I’m trying to do is use multiple samplers so the all kick off in parallel

  • Diffuse+AO at scale 1
  • Diffuse+AO at scale something else
  • Maps at scale 1
  • Maps at scale something else

Could someone point me in the direction of declaring (and using) 4 samplers for the same TextureArray?

Thanks for any info at all on this!

Hello future self (as I know we’re going to forget) and anyone else who may be interested in the same thing!

Someone at work suggested I take a stroll down the Unity GitHub shader source (didn’t know it existed, but now, what a gold mine!)

So, here’s snippets to my future self!

TextureArray layout:

Index 0 (mat1): R,G,B=Diffuse, A=AO
Index 1 (mat1): X=Metallic=x, Y,W=Normal, Z=Smoothness

Index 2 (mat2): R,G,B=Diffuse, A=AO
Index 3 (mat2): X=Metallic=x, Y,W=Normal, Z=Smoothness

Index 4 (mat3): R,G,B=Diffuse, A=AO
Index 5 (mat3): X=Metallic=x, Y,W=Normal, Z=Smoothness

etc.

Declare in the shader properties:

            _UberTextureArray("Uber texture array", 2DArray) = "white" {}`

Declare in HLSL var section:

UNITY_DECLARE_TEX2DARRAY_NOSAMPLER(_UberTextureArray);
SamplerState albedo1_trilinear_repeat_sampler;
SamplerState albedo2_trilinear_repeat_sampler;
SamplerState maps1_trilinear_repeat_sampler;
SamplerState maps2_trilinear_repeat_sampler;

Use:

float4 alb1 = _UberTextureArray.Sample(albedo1_trilinear_repeat_sampler, uva);
float4 alb2 = _UberTextureArray.Sample(albedo1_trilinear_repeat_sampler, uvb);            
float4 map1 = _UberTextureArray.Sample(maps1_trilinear_repeat_sampler, uvp);            
float4 map2 = _UberTextureArray.Sample(maps1_trilinear_repeat_sampler, uvq);

Where:

  • uva and uvb are the regular uv0 x,y with the texture layer as z (in my case uv0.z * 2)
  • uvp and uvq are the regular uv0 x,y with the texture layer as z (in my case uv0.z * 2 + 1)

(BTW: Can anyone confirm that, provided I’m under 8 samplers in total - including the above - I’m basically reading all these values in parallel?) @bgolus maybe?

Kind regards
Past Me :slight_smile:

1 Like