Newbie question: Texture writes How to write to texture memory

I’m just starting with CUDA 2.0, having programmed GPUs with Direct3D and HLSL for some time.

I’ve been reading the Programming Guide, but so far I haven’t seen anything that answers the following question.

I can read from texture memory in a kernel by declaring a texture reference, allocating an array, binding the texture reference to the array, and finally using the tex?D functions within the kernel. Great.

What I’d like to do next is to write to a texture inside a kernel function.

I’m used to doing this in Direct3D by rendering some geometry to a render target with a pixel shader that outputs the required value. This causes the pixel shader output to be written to a texture.

What is the equivalent paradigm within CUDA? How does one write to texture memory within a kernel?

Many thanks.

CUDA unfortunately doesn’t provide any way for a kernel to directly write to Array memory. The only way to write to memory allocated by cudaMallocArray is through cudaMemcpyToArray. So you must allocate a block of device memory (cudaMalloc), write to that in your kernel and then copy use cudaMemcpyToArray to update the texture.

If you use 1D textures bound to device memory and tex1Dfetch, you can write to that memory directly, but due to the data-parallel nature of the threads you can’t relay on a write from one thread to be read by another and thus still requiring a double buffer in some circumstances.

Thanks. This is great info.