在CUDA内核函数中,为什么tex3D不能返回任何值?
这里,我有两个不同版本的代码 第一个是一个完整的CUDA程序,来自CUDA SDK。在内核中,tex3D工作得很好 第二个版本更复杂,有许多OpenGL函数和OpenGL纹理。它的.cu文件与第一个文件相同。但是,我在同一个内核函数中使用cudaMalloc变量从tex3D获取值,我发现tex3D函数不返回任何值。实际上,两个程序使用与下面代码相同的方法创建3D纹理:在CUDA内核函数中,为什么tex3D不能返回任何值?,cuda,textures,multidimensional-array,volume-rendering,Cuda,Textures,Multidimensional Array,Volume Rendering,这里,我有两个不同版本的代码 第一个是一个完整的CUDA程序,来自CUDA SDK。在内核中,tex3D工作得很好 第二个版本更复杂,有许多OpenGL函数和OpenGL纹理。它的.cu文件与第一个文件相同。但是,我在同一个内核函数中使用cudaMalloc变量从tex3D获取值,我发现tex3D函数不返回任何值。实际上,两个程序使用与下面代码相同的方法创建3D纹理: #define SIZE_X 128 //numbers in elements #define SIZE_Y 128 #d
#define SIZE_X 128 //numbers in elements
#define SIZE_Y 128
#define SIZE_Z 128
typedef float VolumeType;
cudaExtent volumeSize = make_cudaExtent(SIZE_X, SIZE_Y, SIZE_Z);
cudaArray *d_volumeArray = 0; //for tex
cudaArray *d_transferFuncArray; //for transferTex
texture<VolumeType, 3, cudaReadModeElementType> tex; // 3D texture
texture<float4, 1, cudaReadModeElementType> transferTex; // 1D transfer function texture
//initialize the 3d texture "tex" with a 3D array "d_volumeArray"
cudaChannelFormatDesc channelDesc = cudaCreateChannelDesc<VolumeType>();
cutilSafeCall( cudaMalloc3DArray(&d_volumeArray, &channelDesc, volumeSize) );
// set texture parameters
tex.normalized = true; // access with normalized texture coordinates
tex.filterMode = cudaFilterModeLinear; // linear interpolation
tex.addressMode[0] = cudaAddressModeClamp; // clamp texture coordinates
tex.addressMode[1] = cudaAddressModeClamp;
CUDA_SAFE_CALL(cudaBindTextureToArray(tex, d_volumeArray, channelDesc));// bind array to 3D texture
//get the real value for 3D texture "tex"
float *d_volumeMem;
cutilSafeCall(cudaMalloc((void**)&d_volumeMem, SIZE_X*SIZE_Y*SIZE_Z*sizeof(float)));
.....//assign value to d_volumeMem in GPU. I've already checked the d_volumeMem is valid
//copy d_volumeMem to 3DArray
cudaMemcpy3DParms copyParams = {0};
copyParams.srcPtr = make_cudaPitchedPtr((void*)d_volumeMem, SIZE_X*sizeof(VolumeType), SIZE_X, SIZE_Y);
copyParams.dstArray = d_volumeArray;
copyParams.extent = volumeSize;
copyParams.kin = cudaMemcpyDeviceToDevice;
cutilSafeCall( cudaMemcpy3D(©Params) );
我认为这些代码很好,因为它们大部分来自CUDA SDK volumeRender,在我的第一个版本代码中工作良好
但我不知道为什么在第二个版本中tex3D突然失效。也许其他一些OpenGL纹理有一些负面影响?事实上,问题是我在选择CUDA设备之前做了绑定纹理的工作。因此,首先调用cudaChooseDevice和cudaSetGLDevice将使事情顺利进行 顺便说一句,通过进一步检查,我发现tex3D总是返回零。在您发布的代码中,我没有看到任何CUDA内核代码和
tex3D()。使用tex3D的CUDA内核非常正常,就像CUDA SDK volumeRender的内核一样。但是更清楚的是,我现在就发布它们。pos1、pos2、pos3是否在0和1之间?玛丽娜:是的,当然,我使用Cudamaloc变量来存储pos1 pos2 pos3,它们都在0和1之间。事实上,我非常怀疑这是因为在第二个版本中使用OpenGL中的许多纹理可能会导致CUDA缺少纹理内存。但是,没有错误消息。可能吗?
__global__ void d_render(....)
{
......//ray-casting progress
float temp = tex3D(tex, pos1, pos2, pos3);
//pos1 pos2 pos3 is valid
//Here actually I use a cudaMalloc variable "s" to store the temp
//In the first version, s has different value for different position
//In the second version, temp is 0 all the time
......//ray-casting progress
}