Search code examples
directxhlsl

An array of Texture3D's in DirectX?


There is Texture2DArray in HLSL, but is there any workaround for an array of Texture3D's in HLSL/DirectX?


Solution

  • There are a few options, in both DirectX11 and DirectX12.

    DirectX11 has more limitations.

    You can declare an array of textures in HLSL as per :

    Texture3D textures[3] : register(t0);
    

    Please note that it is not really an array per se, it's actually equivalent to :

    Texture3D texture0 : register(t0);
    Texture3D texture1 : register(t1);
    Texture3D texture2 : register(t2);
    

    So you still need to bind 3 textures.

    Also note that your array size must be set in the shader, you cannot have a varying number.

    Also dynamic indexing is not allowed, so this is not valid for example:

    int3 myvoxel = //something
    
    int index = 0;
    if (voxel.x > 5)
    {
        index = 1;
    }
    return textures[index].Load(int4(voxel, 0));
    

    The following will not work either :

    cbuffer cbIndex : register(b0)
    {
        uint index;
    }
    
    return textures[index].Load(int4(voxel, 0));
    

    In DirectX12, this is less limited.

    You still need to set your 3 texture views in a contiguous location in your descriptor table, but :

    You can do dynamic indexing so both of the samples above will work.

    You can also declare unbounded arrays, so you as above need to make sure all your volumes are contiguous in the descriptor table.

    Texture3D textures[] : register(t0);