纹理寄存器的使用

extern “C” void caculate_kernel(unsigned char* pixels,unsigned char* gpudata,int lWidth, int lHeight, float fA, float fB)
{
unsigned int size = lWidth * lHeight * sizeof(unsigned char);
cudaChannelFormatDesc channelDesc = cudaCreateChannelDesc(8, 0, 0, 0,cudaChannelFormatKindUnsigned);
cudaArray* cu_array;
cudaMallocArray( &cu_array, &channelDesc, lWidth, lHeight);
cudaMemcpyToArray( cu_array, 0, 0, pixels, size, cudaMemcpyHostToDevice);

tex.addressMode[0] = cudaAddressModeWrap;
tex.addressMode[1] = cudaAddressModeWrap;
tex.filterMode = cudaFilterModeLinear;
tex.normalized = 0;  

cudaBindTextureToArray( tex, cu_array, channelDesc);

dim3 threads(16,16);
dim3 blocks((lWidth+threads.x-1)/threads.x,(lHeight+threads.y-1)/threads.y);
calKernel<<<blocks,threads>>>(gpudata, lWidth, lHeight, fA, fB);

}运行结果h_odata里的所有数据都是0,不知道是哪里错了,应该是没绑定好,这个问题折腾我很久了,高手请帮帮忙看看

[ 本帖最后由 siheng303 于 2010-6-2 19:53 编辑 ]

tex.normalized = true;

归一化,坐标全都越界了

[
tex.normalized =0运行结果,屏幕是全黑的。

我进行了跟踪调试,问题处在
cudaChannelFormatDesc channelDesc = cudaCreateChannelDesc(8, 0, 0, 0,cudaChannelFormatKindUnsigned);
cudaArray* cu_array;
cudaMallocArray( &cu_array, &channelDesc, lWidth, lHeight);
cudaMemcpyToArray( cu_array, 0, 0, pixels, size, cudaMemcpyHostToDevice);

tex.addressMode[0] = cudaAddressModeWrap;
tex.addressMode[1] = cudaAddressModeWrap;
tex.filterMode = cudaFilterModeLinear;
tex.normalized = 0;

cudaBindTextureToArray( tex, cu_array, channelDesc);
和texture<char, 2, cudaReadModeElementType> tex;这条语句中,cuda中有uchar这种类型?

怎么把内核文件给删了啊!其实你在内核里处理一下就可以了,就是将查询坐标映射到[0,1]

因为程序错误和内核没有关系,是 tex.filterMode = cudaFilterModeLinear;错误,因为数据是unsigned char类型,tex.filterMode = cudaFilterModePoint就对了,谢谢风辰