As I understand it, you can’t write directly to a texture (cudaArray). The way it is done is to write to another part of memory and copy it over to the texture. I can get this to work easily in 2D but am having some issues getting it do work in 3D. My code is below:
texture<uchar, 3, cudaReadModeNormalizedFloat> tex; uchar *d_writeable = 0; cudaArray *d_texArray = 0; size_t pitch = 0; int fieldSize = 64; cudaChannelFormatDesc channelDesc = cudaCreateChannelDesc<uchar>(); CUDA_SAFE_CALL( cudaMalloc3DArray(&d_texArray, &channelDesc, volumeSize) ); CUDA_SAFE_CALL( cudaMallocPitch((void**)&d_writeable, &pitch, sizeof(uchar)*fieldSize, fieldSize*fieldSize) ); CUDA_SAFE_CALL( cudaMemset2D(d_writeable, pitch, 0, fieldSize, fieldSize*fieldSize) ); CUDA_SAFE_CALL( cudaMemcpy2DToArray(d_texArray, 0, 0, d_writeable, pitch, sizeof(uchar)*fieldSize, fieldSize*fieldSize, cudaMemcpyDeviceToDevice) ); CUDA_SAFE_CALL(cudaBindTextureToArray(tex, d_texArray, channelDesc));
I’m trying to set up a 3D cuda array and bind it to a texture. Plus I’m trying to set up another part of memory that I can write to and then copy it across.
I have problems with the cudaMemcpy2DToArray (gives an invalid argument error at runtime). If I change the height argument from fieldSize*fieldSize to just fieldSize, it works fine (but obviously only copies part of the array). What am I missing? Thanks in advance.