像素着色器中的 SamplerState 问题

SamplerState issue in the pixel shader

我的像素着色器有问题,它编译但不渲染任何东西,而是 Directx 给出了这个错误: D3D11 错误:ID3D11DeviceContext::DrawIndexed:像素着色器单元期望为默认过滤配置的采样器设置在插槽 0,但在此插槽绑定的采样器配置为比较过滤。如果使用采样器,这种不匹配将产生未定义的行为(例如,它不会由于着色器代码分支而被跳过)。 [执行错误 #390:DEVICE_DRAW_SAMPLER_MISMATCH]。 这是我的着色器:

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;
}

采样器创建:

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;

和渲染函数:

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;
}
HLSL 中的

SamplerState 是一个 "Effects" 构造,仅适用于 fx_* 配置文件并使用 EFfects for Direct3D 11 运行时。

对于您的着色器绑定,请使用:

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

您将采样器声明为比较采样器:

samplerDesc.Filter = D3D11_FILTER_COMPARISON_MIN_MAG_MIP_LINEAR;

应该是:

samplerDesc.Filter = D3D11_FILTER_MIN_MAG_MIP_LINEAR;

比较采样器主要用于阴影贴图,在hlsl中声明如下:

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