SamplerState issue in the pixel shader

Viewed 765

I have a problem with the pixel shader, it compiles but does not renders anything and instead Directx gives out this error: D3D11 ERROR: ID3D11DeviceContext::DrawIndexed: The Pixel Shader unit expects a Sampler configured for default filtering to be set at Slot 0, but the sampler bound at this slot is configured for comparison filtering. This mismatch will produce undefined behavior if the sampler is used (e.g. it is not skipped due to shader code branching). [ EXECUTION ERROR #390: DEVICE_DRAW_SAMPLER_MISMATCH]. Here's my shader:

struct PixelInput
{
 float4 position: SV_POSITION;
 float4 color : COLOR;
 float2 UV: TEXCOORD0;

};

//globals
SamplerState ss;
Texture2D shaderTex;

float4 TexturePixelShader(PixelInput input) : SV_TARGET
{
 float4 texColors;

 texColors = shaderTex.Sample(ss, input.UV);

 return texColors;
}

Sampler creation:

samplerDesc.Filter = D3D11_FILTER_COMPARISON_MIN_MAG_MIP_LINEAR;
    samplerDesc.AddressU = D3D11_TEXTURE_ADDRESS_WRAP;
    samplerDesc.AddressV = D3D11_TEXTURE_ADDRESS_WRAP;
    samplerDesc.AddressW = D3D11_TEXTURE_ADDRESS_WRAP;
    samplerDesc.MipLODBias = 0.0f;
    samplerDesc.MaxAnisotropy = 1;
    samplerDesc.ComparisonFunc = D3D11_COMPARISON_ALWAYS;
    samplerDesc.BorderColor[0] = 0;
    samplerDesc.BorderColor[1] = 0;
    samplerDesc.BorderColor[2] = 0;
    samplerDesc.BorderColor[3] = 0;
    samplerDesc.MinLOD = 0;
    samplerDesc.MaxLOD = D3D11_FLOAT32_MAX;
    result = device->CreateSamplerState(&samplerDesc,  &m_SS);
    if (FAILED(result))
        return false;
    return true;

and rendering function:

void TextureShader::RenderShader(ID3D11DeviceContext* ctxt, int indexCount)
{
    ctxt->IASetInputLayout(m_layout);

    ctxt->VSSetShader(m_vertexShader, NULL, 0);
    ctxt->PSSetShader(m_pixelShader, NULL, 0);
    ctxt->PSSetSamplers(0, 1, &m_SS);
    ctxt->DrawIndexed(indexCount, 0, 0);

    return;
}
2 Answers

You are declaring your sampler as a comparison sampler :

samplerDesc.Filter = D3D11_FILTER_COMPARISON_MIN_MAG_MIP_LINEAR;

It should be :

samplerDesc.Filter = D3D11_FILTER_MIN_MAG_MIP_LINEAR;

Comparison sampler are used mostly for shadow maps, and declare as following in hlsl :

SamplerComparisonState myComparisonSampler;
myTexture.SampleCmp(myComparisonSampler, texCoord);

SamplerState in HLSL is a "Effects" construct that only applies to fx_* profiles and using the EFfects for Direct3D 11 runtime.

For shader binding in your case, use:

sampler ss : register(s0);
Texture2D<flaot4> shaderTex : register(t0);
Related