I've been working on some HLSL code for DirectCompute, and I'm finding that randomly a texture sampling will return zero. This is causing all sorts of problems, since I'm using the sampled texel in a differential equation that explodes in a spectacular manner when fed garbage data.

The texture in question is a Texture2D created as DXGI_FORMAT_R32_FLOAT (i.e. a single float for each texel). The texture sampler is defined to clamp on the edge of the texture as well as use D3D11_FILTER_MIN_MAG_MIP_LINEAR. There's a single mip map level.

The HLSL code that does the sampling is as follows:

Texture2D<float> inputTex : register(t0); SamplerState linSamp : register(s0); float4 getSSPDerivatives(float range, float depth) { float2 tex1; float2 tex2; //Find the initial sound speed. tex1.x = (range / 10000.0); tex1.y = (depth / 5000.0); //Find the sound speed a little deeper (finite difference) tex2.x = (range/10000.0); tex2.y = ((depth + 10)/5000.0); float localC = inputTex.SampleLevel(linSamp, tex1,0); float dcdz = (inputTex.SampleLevel(linSamp, tex2,0) - localC)/10.0; //Find the sound speed a little farther away (more finite difference) tex2.x = ((range + 10) /10000.0); tex2.y = tex1.y; float dcdr = (inputTex.SampleLevel(linSamp, tex2,0) - localC)/10.0; return float4(dcdr, dcdz, localC, tex1.y); }

The symptoms of the problem are that the variable localC, which represents the first texture sampling operation, goes to 0. tex1.y is included in the return for debugging purposes, as it's the only texture coordinate rapidly changing (the X coordinate increases monotonically as the diff EQ marches out in range, whereas the Y coordinate varies based on the diff EQ's previous iteration).

I've been unable to determine why the texture sampling operation returns 0, as there are no 0s in the texture. The texture is currently created as a 2x1024 texel piece, with the two X columns duplicated (eventually the data in the X direction will be different, but I'm just working on getting the Y coords working atm). I've looked at the output of different threads in the shader, and none of them seem to fail at the same iteration step.

Is there anything obviously wrong with my texture sampling function? Is there a limit to the number of texture sampling operations that can be done across all threads in a shader?