简体   繁体   中英

Directx 11, send multiple textures to shader

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.

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.

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 (ie 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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM