我有一个线性无符号字符数组,表示一个二维数组。我想将它放入CUDA 2D纹理中,并对其执行(浮点数)线性插值,即让纹理调用获取4个最近的无符号字符邻居,内部将它们转换为浮点数,插值计算后返回结果。
我在设置纹理并将其绑定到纹理引用时遇到了一些困难。我已经阅读了CUDA参考手册和附录,但是我没有任何进展。
下面是可运行的代码,用于设置和绑定1)浮点纹理和2)无符号字符纹理。浮点代码可以正常运行。但是,如果取消注释底部的两行无符号字符代码,则会抛出"无效参数"错误。
任何帮助/见解将不胜感激! Aaron
我在设置纹理并将其绑定到纹理引用时遇到了一些困难。我已经阅读了CUDA参考手册和附录,但是我没有任何进展。
下面是可运行的代码,用于设置和绑定1)浮点纹理和2)无符号字符纹理。浮点代码可以正常运行。但是,如果取消注释底部的两行无符号字符代码,则会抛出"无效参数"错误。
#include <cstdio>
#include <cuda_runtime.h>
typedef unsigned char uchar;
// Define (global) texture references; must use "cudaReadModeNormalizedFloat"
// for ordinal textures
texture<float, cudaTextureType2D, cudaReadModeNormalizedFloat> texRefFloat;
texture<uchar, cudaTextureType2D, cudaReadModeNormalizedFloat> texRefUChar;
// Define size of (row major) textures
size_t const WIDTH = 1000;
size_t const HEIGHT = 1000;
size_t const TOT_PIX = WIDTH*HEIGHT;
int main(void)
{
// Set texel formats
cudaChannelFormatDesc descFloat = cudaCreateChannelDesc<float>();
cudaChannelFormatDesc descUChar = cudaCreateChannelDesc<uchar>();
// Choose to perform texture 2D linear interpolation
texRefFloat.filterMode = cudaFilterModeLinear;
texRefUChar.filterMode = cudaFilterModeLinear;
// Allocate texture device memory
float * d_buffFloat; cudaMalloc(&d_buffFloat, sizeof(float)*TOT_PIX);
uchar * d_buffUChar; cudaMalloc(&d_buffUChar, sizeof(uchar)*TOT_PIX);
// Bind texture references to textures
cudaError_t errFloat = cudaSuccess;
cudaError_t errUChar = cudaSuccess;
errFloat = cudaBindTexture2D(0, texRefFloat, d_buffFloat, descFloat,
WIDTH, HEIGHT, sizeof(float)*WIDTH);
// Uncomment the following two lines for an error
//errUChar = cudaBindTexture2D(0, texRefUChar, d_buffUChar, descUChar,
// WIDTH, HEIGHT, sizeof(uchar)*WIDTH);
// Check for errors during binding
if (errFloat != cudaSuccess)
{
printf("Error binding float texture reference: %s\n",
cudaGetErrorString(errFloat));
exit(-1);
}
if (errUChar != cudaSuccess)
{
printf("Error binding unsigned char texture reference: %s\n",
cudaGetErrorString(errUChar));
exit(-1);
}
return 0;
}
任何帮助/见解将不胜感激! Aaron