【发布时间】:2016-11-28 14:56:18
【问题描述】:
我是 directx11 的新手,我想为我正在使用的着色器添加纹理。 (到目前为止,我还没有在directx11中使用过任何纹理)
但是采样似乎不起作用(它总是 float4(0,0,0,0)),我收到警告:“D3D11 WARNING: ID3D11DeviceContext::DrawIndexed: The Pixel Shader unit requires a Sampler to be设置在 Slot 0,但没有绑定。"
我搜索了这个,但所有答案都说要使用
_d3dContext->PSSetSamplers(0, 1, &_sampler);
但我已经这样做了。 有谁知道可能出了什么问题?
这是我的整个着色器:
Texture2D tex;
SamplerState TexSampler : register(S0)
{
Texture = (Slope);
Filter = MIN_MAG_MIP_LINEAR;
AddressU = clamp;
AddressV = clamp;
};
struct PixelShaderInput
{
float4 color : COLOR0;
//.r : slope
//.g : relative distance on road (compared the the rider)
//.b : shadow (currently darker on lower parts)
//.a : cameradepth
float fogfactor : COLOR1;
};
float4 main(PixelShaderInput input) : SV_TARGET
{
const float4 fogColor = float4(0.9, 0.9, 1, 1);
const float halfWidth = 0.0016f;
float4 color;
if (input.color.g < -halfWidth * input.color.a)
{
color = float4(0.5, 0.85, 1, 1);
}
else if (input.color.g < halfWidth * input.color.a)
{
color = float4(1, 1, 1, 1);
}
else
{
color = tex.Sample(TexSampler, float2(input.color.r, 0.5));
}
if (input.color.b < 0.999)
{
input.color.b *= 0.94; //makes the sides a bit darker
}
color.rgb *= input.color.b;
color.rgb = lerp(color.rgb, fogColor.rgb, input.fogfactor);
return color;
}
在 c++/cx 中我这样做(不是完整的代码,只是片段):
Microsoft::WRL::ComPtr<ID3D11ShaderResourceView> _slopeGradient;
Microsoft::WRL::ComPtr<ID3D11SamplerState> _sampler;
D3D11_SAMPLER_DESC sampDesc;
ZeroMemory(&sampDesc, sizeof(sampDesc));
sampDesc.Filter = D3D11_FILTER_MIN_MAG_MIP_LINEAR;
sampDesc.AddressU = D3D11_TEXTURE_ADDRESS_CLAMP;
sampDesc.AddressV = D3D11_TEXTURE_ADDRESS_CLAMP;
sampDesc.AddressW = D3D11_TEXTURE_ADDRESS_CLAMP;
sampDesc.ComparisonFunc = D3D11_COMPARISON_NEVER;
sampDesc.MinLOD = 0;
sampDesc.MaxLOD = D3D11_FLOAT32_MAX;
_d3dDevice->CreateSamplerState(&sampDesc, &_sampler);
_d3dContext->PSSetShaderResources(0, 1, &_slopeGradient);
_d3dContext->PSSetSamplers(0, 1, &_sampler);
【问题讨论】:
标签: shader directx-11 hlsl c++-cx pixel-shader