Hi guys,
I’m programming a sort of real time raycaster working with voxels in an octree, but if I copy the calculated data from the device to the host it is just complete gray (the image is represented as an array of unsigned chars in the format RGBA). First I thought it was a problem with communicating between cuda c, c++ (which is compiled to a dll) and c# (which uses this dll) but i wrote a little program which uses these functions directly and it doesn’t work too (the raycasting algorithm isn’t the problem too, I’ve changed it to code which just colors the pixel computed by the kernel red and it is still gray). So I think the problem must be the data exchange from the device to the host.
Here is my current kernel invocation code:
unsigned char* h_image = (unsigned char*)malloc(sizeof(unsigned char) * width * height * 4);
unsigned char* d_image;
cudaMalloc((void**)&d_image, sizeof(unsigned char) * width * height * 4)
cudaDeviceProp deviceProp;
cudaGetDeviceProperties(&deviceProp, 0);
dim3 blockDim(sqrt((float)deviceProp.maxThreadsPerBlock),sqrt((float)deviceProp.maxThreadsPerBlock));
dim3 gridDim(imageWidth/blockDim.x, imageHeight/blockDim.y);
sendRayKernel<<<gridDim,blockDim>>>(d_image, d_root, d_cam, scale);
cudaMemcpy(d_image,h_image, sizeof(unsigned char) * imageWidth * imageHeight * 4, cudaMemcpyDeviceToHost);
return h_image;
and the red painting kernel:
__global__ void sendRayKernel(unsigned char* image, node* root, cam* camera, int scale)
{
vector3 pixel;
pixel.x = (blockIdx.x * blockDim.x) + threadIdx.x;
pixel.y = (blockIdx.y * blockDim.y) + threadIdx.y;
pixel.z = 0;
image[(camera->screenWidth * 4) * pixel.y + pixel.x * 4] = 255;
image[(camera->screenWidth * 4) * pixel.y + pixel.x * 4 + 1] = 0;
image[(camera->screenWidth * 4) * pixel.y + pixel.x * 4 + 2] = 0;
image[(camera->screenWidth * 4) * pixel.y + pixel.x * 4 + 3] = 255;
}
All elements of the array should be 255 or 0 but all are 205 after running this code (and some more for initialising some vars and writing the array to a file, printing some of the chars or drawing a picture of them).
Please help me.