我的像素着色器出了问题,它编译后没有渲染任何东西,而是Directx给出了这个错误。D3D11 ERROR: ID3D11DeviceContext::DrawIndexed: Pixel Shader单元期望在Slot 0处设置一个为默认过滤配置的采样器,但该槽处绑定的采样器是为比较过滤配置的。 如果使用Sampler,这种不匹配将产生未定义的行为(例如,由于着色器代码分支,它不会被跳过)。[ EXECUTION ERROR #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;
}
你正在声明你的采样器是一个比较采样器。
samplerDesc.Filter = D3D11_FILTER_COMPARISON_MIN_MAG_MIP_LINEAR;
应该是。
samplerDesc.Filter = D3D11_FILTER_MIN_MAG_MIP_LINEAR;
比较采样器主要用于阴影贴图,在hlsl中声明如下。
SamplerComparisonState myComparisonSampler;
myTexture.SampleCmp(myComparisonSampler, texCoord);
SamplerState
在HLSL中是一个 "Effects "结构,只适用于以下情况 fx_*
并使用 EFfects for Direct3D 11 运行时。
在你的例子中,对于着色器绑定,使用。
sampler ss : register(s0);
Texture2D<flaot4> shaderTex : register(t0);