8
\$\begingroup\$

The question is simply that: What is the difference between texture.Load() and texture.Sample() in DirectX?

I've used a Texture2D to store the depth in a pass of a DirectX Shader (with SharpDX), and now that I want to extract the depth from the texture I'm not sure which of the two methods I should use

\$\endgroup\$

1 Answer 1

14
\$\begingroup\$

The Sample method accepts a UV coordinate (where the texture covers the [0, 1] range), does mipmap selection based on the UV derivatives, applies addressing modes (clamp, wrap, border) and does filtering (bilinear, trilinear, aniso).

The Load method accepts a texel coordinate in the [0, textureWidth - 1] x [0, textureHeight - 1] range, and the desired mip level, and simply loads a single texel. Coordinates outside the texture's range just return zero, and no filtering is done.

When trying to map a texture 1:1 to the screen, it's convenient to combine Load with the SV_Position input semantic in a pixel shader, as they're in the same units. So you can do this:

void ps_main(float4 screenPos : SV_Position, ...)
{
    float depth = depthTexture.Load(int3(screenPos.xy, 0));
    ...
}

That way you don't need to calculate any UVs. However, this only works if the texture is the same size as the screen; so you can't load half-res or quarter-res textures this way. For those, you'd have to calculate UVs and use Sample.

\$\endgroup\$
2
  • 1
    \$\begingroup\$ @c4sh Keep in mind that depending on your usage, you should avoid linear filtering of depth, since the interpolated value usually isn't what you want. For the specific case of shadow buffers, you should actually consider the Comparison filter and the SampleCmp method. \$\endgroup\$
    – MooseBoys
    Commented Nov 14, 2013 at 23:12
  • \$\begingroup\$ Thanks, I think I was using Sample considering it's usage as in Load... \$\endgroup\$
    – c4sh
    Commented Nov 15, 2013 at 13:57

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .