Texture array in HLSL

I want to access a bunch of textures in the shader and want to access them as an array. How can I do that

What I want to do is like:

float i = tex2D(texture[a mod 3], texCoord);


Without using an array I have to to do it like:
if (a mod 3 == 0) i = tex2D(texture1, texCoord);
else if (a mod 3 == 1) i = tex2D(texture2, texCoord);
else if (a mod 3 == 2) i = tex2D(texture3, texCoord);

which looks urgly.

 



Answer this question

Texture array in HLSL

  • sfleck

    is there a way to index into the sampler array using a variable it seems to work fine if i do

    int i = 2; sampler[ i ];

    but if i try to pass a variable from the vertex shader and use that as an index it gives me

    error X3512: sampler array index must be a literal expression

    maybe i'm doing something wrong if i set the index to a variable instead of a number like 1 it gives me this error, is there any way to do it

  • Chris Knight

    Correct - DX9 doesn't have the concept of an "array" of textures, there's just a bunch of separate textures and samplers. You could put them all in a 3D texture as different 2D slices, but they're kinda slow as well. Try both!

  • AntonioCanaveral

    In an effect (.fx) I tryed to do:

    shared sampler2D textures [ 8 ];

    and got the following compiler error message:

    shared.fxh(182): ID3DXEffectCompiler: Arrays must be either numeric, structure, string or shader
    shared.fxh(182): ID3DXEffectCompiler: Error initializing variable type
    shared.fxh(199): ID3DXEffectCompiler: State 'TEXTURE' cannot be assigned an array or structure
    ID3DXEffectCompiler: There was an error initializing the compiler

    So I bet YOU CAN'T used texture arrays in HLSL, sorry !!!

  • harrier_coder

    In ps_2_0 you can have an array of samplers and index into the sampler array. (In ps_1_1 you can have the array but you have to use the elements in order. ) I think you may still have to hard-associate each element of the sampler array with an individual texture, but that's probably OK.

    sampler Samplers[3] = {
    sampler_state
    {
    texture = <TextureX>;
    magfilter = LINEAR;
    minfilter = LINEAR;
    mipfilter = LINEAR;
    AddressU = mirror;
    AddressV = mirror;
    },

    sampler_state
    {
    texture = <TextureY>;
    magfilter = LINEAR;
    minfilter = LINEAR;
    mipfilter = LINEAR;
    AddressU = mirror;
    AddressV = mirror;
    },

    sampler_state
    {
    texture = <TextureZ>;
    magfilter = LINEAR;
    minfilter = LINEAR;
    mipfilter = LINEAR;
    AddressU = mirror;
    AddressV = mirror;
    },
    };

    And then you can do stuff like this:

    int x = 1;
    float4 color = tex2D(Samplers[x], coord);


  • Texture array in HLSL