Prefer to read without ads? Become a member — from $10/month — and support the work. Already a member? Log in to read ad-free on this device.

10.6 Texturing With Normalized Coordinates

When texturing with normalized coordinates, the texture is addressed by coordinates in the range [0.0, 1.0) instead of the range [0, MaxDim). For a 1D texture with 16 elements, the normalized coordinates are as follows:

Figure 10-6. Texturing with Normalized Coordinates

Other than having texture coordinates that are independent of the texture dimension, the texture is dealt with in largely the same way, except that the full range of CUDA’s texturing capabilities become available. With normalized coordinates, more texture addressing modes besides clamp and border addressing become available: the wrap and mirror addressing modes, whose formulas are as follows:

Wrap \[x^{'} = x - \left\lfloor x \right\rfloor\]
Mirror \[x^{'} = \left\{ \begin{matrix} x - \left\lfloor x \right\rfloor,\ \left\lfloor x \right\rfloor\ is\ even \\ 1 - x - \left\lfloor x \right\rfloor,\ \left\lfloor x \right\rfloor\ is\ odd \end{matrix} \right.\ \]

The four texture addressing modes supported in CUDA are depicted below, showing which in-range texture element is fetched by the first two out-of-range coordinates on each end.

Figure 10-7. Texture Addressing Modes

If you are having trouble visualizing the behavior of these addressing modes, check out the tex2d_opengl.cu microdemo in the next section.

IMPORTANT NOTE: The addressing and filtering modes are baked into a texture object when it is created. To texture the same memory with a different addressing mode, create a second texture object with a different cudaTextureDesc; the modes cannot be changed on an existing object.

Floating Point Coordinates With 1D Device Memory

For applications that wish to use floating point coordinates to address the texture, or use texturing features that are only available for normalized coordinates, build the texture object from a cudaResourceTypePitch2D resource that describes the device memory: specify a height of 1 and a pitch of N*sizeof(T). The kernel can then call tex2D()<T>(tex, x, 0.0f) to read the 1D texture with floating point coordinates.