I have an HLSL pixel shader:
struct PixelShaderInput
{
float4 pos : SV_POSITION;
float2 texCoord : TEXCOORD0;
};
Texture2D s_texture : register(t0);
cbuffer ColorConstantBuffer : register(b1)
{
float4 m_color;
};
SamplerState s_sampleParams
{
Filter = MIN_MAG_MIP_LINEAR;
AddressU = CLAMP;
AddressV = CLAMP;
};
float4 main(PixelShaderInput input) : SV_TARGET
{
float4 t = s_texture.Sample(s_sampleParams, input.texCoord);
return t * m_color;
}
It works fine visually, but spams output with warnings:
D3D11 WARNING: ID3D11DeviceContext::Draw: The Pixel Shader unit expects a Sampler to be set at Slot 0, but none is bound. This is perfectly valid, as a NULL Sampler maps to default Sampler state. However, the developer may not want to rely on the defaults. [ EXECUTION WARNING #352: DEVICE_DRAW_SAMPLER_NOT_SET]
As you can see, I have set s_sampleParams
so can anybody explain what is wrong?
Unless you use the effects framework, which will create a sampler for you (and bind it when you call Apply on EffectPass), using plain hlsl will not create sampler state by default, so you need to create one (as described here )
Here is a verbose version here that matches the sampler you need;
D3D11_SAMPLER_DESC samplerDesc;
samplerDesc.Filter = D3D11_FILTER_MIN_MAG_MIP_LINEAR;
samplerDesc.AddressU = D3D11_TEXTURE_ADDRESS_CLAMP;
samplerDesc.AddressV = D3D11_TEXTURE_ADDRESS_CLAMP;
samplerDesc.AddressW = D3D11_TEXTURE_ADDRESS_CLAMP;
samplerDesc.MipLODBias = 0.0f;
samplerDesc.MaxAnisotropy = 1;
samplerDesc.ComparisonFunc= D3D11_COMPARISON_NEVER;
samplerDesc.MinLOD = -FLT_MAX;
samplerDesc.MaxLOD = FLT_MAX;
Please note I left Border color as anything, since you want wrap it will nerver be used.
To create your sampler :
ID3D11SamplerState* myLinearWrapSampler;
HRESULT hr = d3dDevice->CreateSamplerState(&samplerDesc, &myLinearWrapSampler);
Then call:
deviceContext->PSSetSamplers(0,1, myLinearWrapSampler);
In order to attach the sampler to your pixel shader.
Since in your case your sampler settings matches the default one, setting sampler state to null is also valid (but I would still recommend to create one to clarify the intent, and avoid the runtime warning)