DirectX Clamp and Border Texture Addressing Modes

This article provides an overview of how the Microsoft DirectX API and modern graphics hardware process texture sampling beyond standard normalized coordinates using clamp and border addressing modes. It details the underlying mechanics of the sampler unit, explores how coordinates outside the standard range are evaluated during filtering, and explains the API configuration for both Direct3D 11 and Direct3D 12.

Texture Addressing Modes in DirectX

In DirectX (Direct3D 11 and Direct3D 12), texture coordinates \((u, v, w)\) typically range from \(0.0\) to \(1.0\), mapping directly across the dimensions of a texture resource. When a shader requests a sample using coordinates outside this normalized \([0.0, 1.0]\) range, the Texture Mapping Unit (TMU) relies on the addressing mode defined in the active sampler state (D3D11_SAMPLER_DESC or D3D12_SAMPLER_DESC) to determine what color data to return.

DirectX exposes these behaviors through the D3D12_TEXTURE_ADDRESS_MODE (or D3D11_TEXTURE_ADDRESS_MODE) enumeration, where CLAMP and BORDER serve distinct visual and mathematical purposes.

Clamp Mode (TEXTURE_ADDRESS_MODE_CLAMP)

Clamp mode restricts texture coordinates to the extreme edges of the \([0.0, 1.0]\) range. Mathematically, the coordinate \(x\) is evaluated as:

\[x_{\text{clamped}} = \max(0.0, \min(x, 1.0))\]

Hardware Execution

When a coordinate less than \(0.0\) is sampled, the hardware assigns it the coordinate of the first texel center along that axis (index \(0\)). When a coordinate greater than \(1.0\) is sampled, the hardware assigns it the coordinate of the final texel center along that axis (index \(N - 1\), where \(N\) is the dimension size).

Filtering Behavior

  • Point Filtering: Any sample requested outside \([0.0, 1.0]\) immediately resolves to the nearest valid border texel on that edge.
  • Linear Filtering: The sampling footprint clamps each coordinate independently before computing the weighted average. As a result, coordinates extending far beyond the edge do not interpolate toward zero or an empty space; they continually sample the edge texel column or row, creating a streaked, stretched appearance of the boundary pixels.

Clamp mode is primarily used to eliminate sampling seams on skyboxes, UI elements, and sprite sheets where neighboring texture data must not bleed into the current primitive.

Border Mode (TEXTURE_ADDRESS_MODE_BORDER)

Border mode specifies that any coordinate outside the range \([0.0, 1.0]\) evaluates to an arbitrary, fixed color defined by the application.

In the sampler state description, the developer defines an array of four floating-point values:

float BorderColor[4]; // RGBA values

Hardware Execution

When a texture coordinate falls outside \([0.0, 1.0]\), the TMU substitutes the fetch result with the stored BorderColor constant rather than fetching from the texture memory.

Filtering Behavior

  • Point Filtering: Coordinates within \([0.0, 1.0]\) return the exact texel color, while coordinates outside return the exact BorderColor.
  • Linear Filtering: The TMU filters between the edge texels of the texture and the border color. Because bilinear and trilinear filters evaluate a footprint (typically spanning across adjacent texels), a sample located at \(1.0\) along an axis with linear filtering enabled will blend half of the edge texel's color with half of the border color. Once the sampling footprint moves completely outside \([0.0, 1.0]\), the return value becomes \(100\%\) BorderColor.

Border mode is widely used in shadow mapping (where coordinates outside the light frustum must return a maximum depth value to avoid casting false shadows) and decal projection (to cleanly fade or clip geometry outside the projection bounds).

DirectX API Differences: Static vs. Dynamic Samplers

In Direct3D 11, arbitrary border colors are fully supported through D3D11_SAMPLER_DESC::BorderColor.

In Direct3D 12, developers have two options:

  1. Descriptor Heap Samplers: Standard sampler descriptors support arbitrary RGBA border colors via D3D12_SAMPLER_DESC.
  2. Root Signature Static Samplers: Direct3D 12 introduces static samplers directly inside root signatures for performance optimization. Static samplers restrict the BorderColor parameter to an enumeration (D3D12_STATIC_BORDER_COLOR), supporting only predefined presets:
    • TRANSPARENT_BLACK \((0, 0, 0, 0)\)
    • OPAQUE_BLACK \((0, 0, 0, 1)\)
    • OPAQUE_WHITE \((1, 1, 1, 1)\)

This restriction enables hardware to bake the border color directly into shader execution units without allocating dedicated descriptor storage.