Windows DirectX Documentation

DirectX Sampler Resources

This section provides comprehensive information on sampler states and how to utilize them effectively within your DirectX applications. Samplers are crucial for controlling how texture data is accessed and filtered.

Understanding Sampler States

Sampler states define parameters that govern how a texture is sampled. These parameters include:

  • Addressing Modes: How texture coordinates outside the 0-1 range are handled (e.g., wrap, clamp, mirror).
  • Filtering: How texels are interpolated when the texture resolution doesn't match the screen resolution (e.g., point, bilinear, anisotropic).
  • Mipmap Filtering: How mipmap levels are selected and blended.
  • Comparison Function: Used for depth and stencil testing during texture sampling.

Creating and Managing Sampler States

In DirectX, sampler states are typically created using the graphics device. The process involves defining a D3D11_SAMPLER_DESC structure and then calling ID3D11Device::CreateSamplerState.

// Example of creating a sampler state description
D3D11_SAMPLER_DESC samplerDesc;
ZeroMemory(&samplerDesc, sizeof(samplerDesc));

samplerDesc.Filter = D3D11_FILTER_ANISOTROPIC; // High-quality anisotropic filtering
samplerDesc.AddressU = D3D11_TEXTURE_ADDRESS_WRAP; // Wrap texture coordinates
samplerDesc.AddressV = D3D11_TEXTURE_ADDRESS_WRAP;
samplerDesc.AddressW = D3D11_TEXTURE_ADDRESS_WRAP;
samplerDesc.MipLODBias = 0.0f;
samplerDesc.MaxAnisotropy = 16; // Maximum anisotropy level
samplerDesc.ComparisonFunc = D3D11_COMPARISON_ALWAYS_DESC; // No comparison
samplerDesc.BorderColor[0] = 1.0f; // RGBA for border color
samplerDesc.BorderColor[1] = 1.0f;
samplerDesc.BorderColor[2] = 1.0f;
samplerDesc.BorderColor[3] = 1.0f;
samplerDesc.MinLOD = 0.0f;
samplerDesc.MaxLOD = D3D11_FLOAT32_MAX;

ID3D11SamplerState* pSamplerState;
HRESULT hr = pDevice->CreateSamplerState(&samplerDesc, &pSamplerState);

if (SUCCEEDED(hr)) {
    // Bind the sampler state to a shader stage
    pImmediateContext->PSSetSamplers(0, 1, &pSamplerState);
    pSamplerState->Release(); // Release after binding if not needed elsewhere
}

Sampler States in Shaders

Sampler states are accessed within shaders using the sampler keyword. The sampler object is then used to sample from a texture object.

// HLSL Pixel Shader Example
Texture2D myTexture;
SamplerState mySampler;

float4 PSMain(PS_INPUT input) : SV_Target
{
    // Sample the texture using the sampler
    float4 texColor = myTexture.Sample(mySampler, input.texCoord);
    return texColor;
}

Key Sampler Parameters and Their Effects

Filtering Modes

  • D3D11_FILTER_POINT: Nearest texel.
  • D3D11_FILTER_LINEAR: Bilinear interpolation.
  • D3D11_FILTER_ANISOTROPIC: Anisotropic filtering (high quality, computationally intensive).
  • Also includes variations with mipmaps (e.g., D3D11_FILTER_MIN_MAG_MIP_LINEAR).

Addressing Modes

  • D3D11_TEXTURE_ADDRESS_WRAP: Repeats the texture.
  • D3D11_TEXTURE_ADDRESS_CLAMP: Clamps texture coordinates to the edge.
  • D3D11_TEXTURE_ADDRESS_MIRROR: Mirrors the texture.
  • D3D11_TEXTURE_ADDRESS_BORDER: Uses the specified border color.

Best Practices

  • Create sampler states that are specific to your needs to avoid unnecessary overhead.
  • Batch sampler state changes where possible.
  • Leverage anisotropic filtering for surfaces viewed at oblique angles to improve visual quality.
  • Understand the trade-offs between filtering quality and performance.

Further Reading