简体   繁体   English

Directx 11,将多个纹理发送到着色器

[英]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 当然我通过以下方式制作了pTexture:D3DX11CreateShaderResourceViewFromFile

Shader: Texture2D Texture; 着色器:Texture2D纹理;

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: 这是一个例子:HLSL代码:

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

C++ Code: C ++代码:

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

So for example for above HLSL code it will be: 例如,对于上面的HLSL代码,它将是:

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. HLSL名称可以是任意的,不必与任何特定名称相对应 - 只有索引很重要。 While "register(tXX);" 而“注册(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. 当您填写D3D11_TEXTURE2D_DESC查看ArraySize成员。 This desc struct is the one that gets passed to ID3D11Device::CreateTexture2D . 这个desc结构是传递给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. 然后在着色器中使用第三个texcoord采样索引,该索引指示您所引用的数组中的哪个2D纹理。

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 . 如果您正在使用效果框架,则可以使用ID3DX11EffectShaderResourceVariable :: SetResource ,或者使用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. 您可能还想查看3D纹理,它提供了一种自然的方式,可以通过texcoord中的第3个元素在数组中的相邻纹理之间进行插值(而2D数组会自动钳位到最接近的整数)。 See the HLSL sample remarks . 请参阅HLSL sample 备注

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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