Search code examples
hlsltexture2dpixel-shaderdirect3d11

Can't get texture.Sample to work, although I can get texture.Load to work fine in Direct 3d 11 shader


In my HLSL for Direct3d 11 app, I'm having a problem where the texture.Sample intrinsic always return 0. I know my data and parameters are correct because if I use texture.Load instead of Sample the value returned is correct.

Here are my declarations:

extern Texture2D<float> texMask;

SamplerState TextureSampler : register (s2);

Here is the code in my pixel shader that works-- this confirms that my texture is available correctly to the shader and my texcoord values are correct:

float maskColor = texMask.Load(int3(8192*texcoord.x, 4096*texcoord.y, 0));

If I substitute for this the following line, maskColor is always 0, and I can't figure out why.

float maskColor = texMask.Sample(TextureSampler, texcoord);

TextureSampler has the default state values; texMask is defined with 1 mip level.

I've also tried:

float maskColor = texMask.SampleLevel(TextureSampler, texcoord, 0);

and that also always returns 0.

C++ code for setting up sampler:

D3D11_SAMPLER_DESC sd;
ZeroMemory(&sd, sizeof(D3D11_SAMPLER_DESC));
sd.Filter = D3D11_FILTER_MIN_MAG_MIP_LINEAR;
sd.AddressU = D3D11_TEXTURE_ADDRESS_WRAP;
sd.AddressV = D3D11_TEXTURE_ADDRESS_WRAP;
ID3D11SamplerState* pSampler;
dev->CreateSamplerState(&sd, &pSampler);

devcon->PSSetSamplers(2, 1, &pSampler);

Solution

  • I found my problem -- I needed to specify a register for the texture as well as the sampler in my HLSL. I can't find any documentation anywhere that describes why this is necessary, but it did fix my problem.