Search code examples
c++directxhlsl

Sending texture array to shader in DirectX 11


I have few textures that I want to set in my HLSL shader as array. Each texture is represented as ID3D11ShaderResourceView*. Each texture may be DIFFERENT size.

Now, If I set them in D3D as array:

ID3D11ShaderResourceView* m_array[3];

    m_array[0] = ...;
    m_array[1] = ...;
    m_array[2] = ...;

    m_deviceContext->PSSetShaderResources(
        0,  // Start slot
        3,  // Nb of textures
        m_array); // Array

And in my HLSL shader I declared:

Texture2D       g_textures[3];

Will it be mapped correctly?


Solution

  • This is in general a method you can use to map texture arrays from runtime to shader execution. It does not matter that the texture dimensions in the array match, however, you may need to account for this within your shader code, depending on exactly how you are sampling the textures.

    Also, in your HLSL, you making the assumption that the g_textures array is assigned to slot 0, so if for some reason it doesn't actually go there (eg. there is another texture resource that comes before it in the shader source), then you won't be setting the intended resource to the correct slot. I find it's better to map them explicitly, eg:

    Texture2D g_textures[3] : register(t0);
    

    If there is a collision, it will be found at (shader) compile time.