Directx 11, send multiple textures to shader

Miguel P picture Miguel P · Jul 26, 2012 · Viewed 14.5k times · Source

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.

Answer

asmi84 picture asmi84 · Jul 26, 2012

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.