Directx 11, send multiple textures to shader

吃可爱长大的小学妹 提交于 2019-12-07 02:01:15

问题


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.


回答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.




回答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.



来源:https://stackoverflow.com/questions/11668177/directx-11-send-multiple-textures-to-shader

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!