10.3_1D_Texturing

10.3 1D Texturing

For illustrative purposes, we will deal with 1D textures in detail and then expand the discussion to include 2D and 3D textures.

10.3.1 TEXTURE SETUP

The data in textures can consist of 1, 2, or 4 elements of any of the following types.

  • Signed or unsigned 8-, 16-, or 32-bit integers
    16-bit floating-point values
    32-bit floating-point values

In the .cu file (whether using the CUDA runtime or the driver API), the texture reference is declared as follows.

texture<ReturnType, Dimension, ReadMode> Name;

whereReturnType is the value returned by the texture intrinsic; Dimension is 1, 2, or 3 for 1D, 2D, or 3D, respectively; and ReadMode is an optional parameter type that defaults toCORDModeElementType. The read mode only affects integer-valued texture data. By default, the texture passes back integers when the texture data is integer-valued, promoting them to 32-bit if necessary. But whenCORDModeNormalizedFloat is specified as the read mode, 8- or 16-bit integers can be promoted to floating-point values in the range [0.0, 1.0] according to the formulas in Table 10.1.

Table 10.1 Floating-Point Promotion (Texture)

The C versions of this conversion operation are given in Listing 10.1.

Listing 10.1 Texture unit floating-point conversion.

float   
TexPromoteToFloat( signed char c ) { if ( c == (signed char) 0x80 ) { return -1.0f; } return(float)c/127.0f;   
}   
float   
TexPromoteToFloat(short s) { if(s==(short)0x8000){ return-1.0f; } return(float)s/32767.0f;   
}   
float   
TexPromoteToFloat(unsigned char uc) { return(float)uc/255.0f;   
}   
float   
TexPromoteToFloat(unsigned short us) { return(float)us/65535.0f;

Once the texture reference is declared, it can be used in kernels by invoking texture intrinsics. Different intrinsics are used for different types of texture, as shown in Table 10.2.

Texture references have file scope and behave similarly to global variables. They cannot be created, destroyed, or passed as parameters, so wrapping them in higher-level abstractions must be undertaken with care.

CUDA Runtime

Before invoking a kernel that uses a texture, the texture must be bound to a CUDA array or device memory by calling CUDATexture(), CUDATexture2D(), or CUDATextureToArray(). Due to the

Table 10.2 Texture Infrinnsics