Textures are not being used by the pixel shader correctly. I am loading two separate textures in my pixel shader. However, most of the time, I can only ever display Texture1, even when I am trying to display Texture2.
Texture2D Texture1;
Texture2D Texture2;
SamplerState ss;
float4 main(float4 position: SV_POSITION, float4 color: COLOR, float2 texCoord: TEXCOORD) : SV_TARGET
{
float4 color1 = Texture1.Sample(ss, texCoord);
float4 color2 = Texture2.Sample(ss, texCoord);
// even though color2 is specified here, color1 ie Texture1 is still displayed
float4 finalColor = color2;
return finalColor;
}
The above code always displays texture 1 (bricks): texture 1
The weird part is, I CAN get the second texture to display, but only if I modify the pixel shader to look like:
float4 main(float4 position: SV_POSITION, float4 color: COLOR, float2 texCoord: TEXCOORD) : SV_TARGET
{
float4 color1 = Texture1.Sample(ss, texCoord);
float4 color2 = Texture2.Sample(ss, texCoord);
float4 finalColor = {0,0,0,0};
if (color1.z != color2.z) {
// no idea why, but setting finalColor to color2 outside of this if statement
// doesn't work.
finalColor = color2;
}
return finalColor;
}
This displays the second texture (wood): texture 2
Here is the code for loading the textures:
// load the texture
HRESULT hr = CreateWICTextureFromFile(m_dev.Get(), nullptr, L"bricks.png", nullptr, &m_texture1, 0);
hr = CreateWICTextureFromFile(m_dev.Get(), nullptr, L"wood.png", nullptr, &m_texture2, 0);
...
m_devCon->PSSetShaderResources(0, 1, m_texture1.GetAddressOf());
m_devCon->PSSetShaderResources(1, 1, m_texture2.GetAddressOf());
What is going on here? I know for a fact that both textures are loaded in the pixel shader, since I can get the second texture to display if I use this weird hack.
These declarations in HLSL declare two texture slots and a sampler slot, but they do not indicate where they are bound. As declared, you'd have to use shader reflection to determine that, such as through the legacy Effects system.
Texture2D Texture1;
Texture2D Texture2;
SamplerState ss;
In order to use explicit binding, you need to indicate which slot each item is bound to. It's also good form to explicitly declare the expected number of channels in the texture (in this case I'm assuming 4 channels):
Texture2D<float4> Texture1 : register(t0);
Texture2D<float4> Texture2 : register(t1);
SamplerState ss : register(s0);
Since your code is not actually shown as creating a sampler with CreateSamplerState
and binding it to the 0 slot with PSSetSamplers
, you are relying on the 'default' sampler state spelled out in Microsoft Docs of D3D11_FILTER_MIN_MAG_MIP_LINEAR
with D3D11_TEXTURE_ADDRESS_CLAMP
.