纹理引用API
纹理引用是编译时确定的(相对于纹理对象的运行时确定),私以为大概就对应着面向过程的编程方式(C)以及面向对象的编程方式(C++)。
CUDA programing guide里给的例子:
texture<DataType, Type, ReadMode> texRef;
很好懂,DataType就是指定纹理中的数据元素的类型;
Type是纹理引用的类型,包括一维纹理cudaTextureType1D,二维纹理cudaTextureType2D,和三维纹理cudaTextureType3D,另外还有一维层次纹理cudaTextureType1DLayered和二维层次纹理cudaTextureType2DLayered,。Type的类型是可选的,默认情况下是cudaTextureType1D。
readMode和纹理对象中的是一样的,默认是cudaReadModeElementType。
纹理引用只能声明为全局静态变量,不能作为函数参数传递。
纹理引用的其他特征是可以通过主机运行时改变的。CUDA参考手册中说明了运行时的API拥有一个高级的接口(C++风格的)和一个低级的接口(C风格的)。其中texture类型就是在高级API中定义的数据结构,它由低级API中的textureReference结构继承而来。
可以在CUDA API中找到其定义:
struct __device_builtin__ textureReference
{
/**
* Indicates whether texture reads are normalized or not
*/
int normalized;
/**
* Texture filter mode
*/
enum cudaTextureFilterMode filterMode;
/**
* Texture address mode for up to 3 dimensions
*/
enum cudaTextureAddressMode addressMode[3];
/**
* Channel descriptor for the texture reference
*/
struct cudaChannelFormatDesc channelDesc;
/**
* Perform sRGB->linear conversion during texture read
*/
int sRGB;
/**
* Limit to the anisotropy ratio
*/
unsigned int maxAnisotropy;
/**
* Mipmap filter mode
*/
enum cudaTextureFilterMode mipmapFilterMode;
/**
* Offset applied to the supplied mipmap level
*/
float mipmapLevelBias;
/**
* Lower end of the mipmap level range to clamp access to
*/
float minMipmapLevelClamp;
/**
* Upper end of the mipmap level range to clamp access to
*/
float maxMipmapLevelClamp;
int __cudaReserved[15];
};
这与纹理对象中使用的struct cudaTeextureDesc结构类似,都是定义纹理的一些特征。
纹理引用将纹理以绑定的形式与cuda数组或线性空间绑定在一起。
绑定到数组:cudaBindTextureToArray(&texRef, de_arr,&channel_format);
绑定到现行空间:cudaBindTexture2D(&offset, texRef, devPtr, channelDesc,width, height, pitch);