Directx 11, send multiple textures to shader

15,792

Solution 1

By using Texture Arrays. When you fill out your D3D11_TEXTURE2D_DESC look at the ArraySize member. This desc struct is the one that gets passed to ID3D11Device::CreateTexture2D. Then in your shader you use a 3rd texcoord sampling index which indicates which 2D texture in the array you are referring to.

Update: I just realised you might be talking about doing it over multiple calls (i.e. for different geo), in which case you update the shader's texture resource view. If you are using the effects framework you can use ID3DX11EffectShaderResourceVariable::SetResource, or alternatively rebind a new texture using PSSetShaderResources. However, if you are trying to blend between multiple textures, then you should use texture arrays.

You may also want to look into 3D textures, which provide a natural way to interpolate between adjacent textures in the array (whereas 2D arrays are automatically clamped to the nearest integer) via the 3rd element in the texcoord. See the HLSL sample remarks.

Solution 2

You can use multiple textures as long as their count does not exceed your shader profile specs. Here is an example: HLSL Code:

Texture2D diffuseTexture : register(t0);
Texture2D anotherTexture : register(t1);

C++ Code:

devcon->V[P|D|G|C|H]SSetShaderResources(texture_index, 1, &texture);

So for example for above HLSL code it will be:

devcon->PSSetShaderResources(0, 1, &diffuseTextureSRV);
devcon->PSSetShaderResources(1, 1, &anotherTextureSRV); (SRV stands for Shader Texture View)

OR:

ID3D11ShaderResourceView * textures[] = { diffuseTextureSRV, anotherTextureSRV};
devcon->PSSetShaderResources(0, 2, &textures);

HLSL names can be arbitrary and doesn't have to correspond to any specific name - only indexes matter. While "register(tXX);" statements are not required, I'd recommend you to use them to avoid confusion as to which texture corresponds to which slot.

Share:
15,792
Miguel P
Author by

Miguel P

Updated on July 27, 2022

Comments

  • Miguel P
    Miguel P almost 2 years

    using this code I can send one texture to the shader:

     devcon->PSSetShaderResources(0, 1, &pTexture); 
    

    Of course i made the pTexture by: D3DX11CreateShaderResourceViewFromFile

    Shader: Texture2D Texture;

    return color * Texture.Sample(ss, texcoord);
    

    I'm currently only sending one texture to the shader, but I would like to send multiple textures, how is this possible?

    Thank You.