How to pass a parameter to a Texture2DArray in HLSL?

I’m working on a shadow mapping shader for multiple lights. I’d like to pass the various shadow maps into a Texture2DArray or Texture2D[] variable in my .fx file.

But this code doesn’t work:
// HLSL:
Texture2DArray ShadowMap;
// or:
Texture2D[] ShadowMap;

// C#:
RenderTarget2D[] shadowRenderTargets;
// later...
Effect.Parameters["ShadowMap"].SetValue(shadowRenderTargets);

The C# code doesn’t compile because there is no SetValue(Texture2D[]) overload for an EffectParameter. There is also no overload for SetValue that lets me pass an array index.

In the mean time, I’ve set it up to pass individual shadow maps one at a time, but this means my declarations in HLSL are like:
Texture2D ShadowMap0;
Texture2D ShadowMap1;
Texture2D ShadowMap2;
// etc…

sampler ShadowMapSampler0 = sampler_state { Texture = ; MinFilter = LINEAR; MagFilter = LINEAR; MipFilter = LINEAR; AddressU = CLAMP; AddressV = CLAMP; };
sampler ShadowMapSampler1 = sampler_state { Texture = ; MinFilter = LINEAR; MagFilter = LINEAR; MipFilter = LINEAR; AddressU = CLAMP; AddressV = CLAMP; };
sampler ShadowMapSampler2 = sampler_state { Texture = ; MinFilter = LINEAR; MagFilter = LINEAR; MipFilter = LINEAR; AddressU = CLAMP; AddressV = CLAMP; };
// etc…

and I’d much rather use a Texture2DArray in HLSL. Can anyone point me in the right direction?

You should not use an array of RenderTarget2D, but a single RenderTarget2D with ArraySize > 1 :slight_smile:

1 Like

Ah thanks, I knew I was missing something basic. I got it working now.

1 Like