Dear all,
I am studying textures.
I want to find a simple example of using tex2D to read a 2D texture. I want to avoid cudamallocpitch, cuda arrays, fancy channel descriptions, etc.
I came up with the following code. (Lots of code online is too complicated for me to understand and use lots of parameters in their functions calls).
I want to map the 4 element linear array to a 2 by 2 2D texture.
However, I always get 0 as ouptput.
Any help would be greatly appreciated!
Best,
Mat
#include<stdio.h>
texture<int, 2> texarray;
__global__ void test(){
int s = tex2D<int>(texarray, 0 ,0);
printf("%d \n", s);
}
int main(){
int *host_array = (int *) malloc(4*sizeof(int));
host_array[0] = 3; host_array[1] = 4; host_array[2] = 5; host_array[3] = 6;
int *dev_array;
cudaMalloc( (void**) &dev_array, 4*sizeof(int) );
cudaChannelFormatDesc desc = cudaCreateChannelDesc<int>();
cudaBindTexture2D(NULL, texarray, dev_array, desc, 2, 2, 2*sizeof(int) );
cudaMemcpy(dev_array, host_array, 4*sizeof(int), cudaMemcpyHostToDevice );
test<<<1,1>>>();
cudaDeviceSynchronize();
cudaUnbindTexture( texarray );
cudaFree(dev_array);
free(host_array);
return 0;
}