This is a kernel to calculate the normal vector.
__global__ void getNormalMapKernel(const ushort *dev_depth, float3 *normalMap, const float *K ,const float *T)
{
int x = threadIdx.x;
int y = blockIdx.x;
float3 normal = make_float3(0.0, 0.0, 0.0);
if (x < IMG_WIDTH - 1 && y < IMG_HEIGHT - 1)
{
ushort depth = dev_depth[y * IMG_WIDTH + x];
ushort depth_right = dev_depth[y * IMG_WIDTH + x + 1];
ushort depth_down = dev_depth[(y + 1) * IMG_WIDTH + x];
if ( depth && absDevUshort(depth, depth_right) < 20 && absDevUshort(depth, depth_down) < 20)
{
float3 cameraPos = make_float3((x - K[1]) * depth / K[0] - T[0], (y - K[3]) * depth / K[2] - T[1], depth - T[2]);
float3 cameraPosRight = make_float3((x + 1 - K[1]) * depth_right / K[0] - T[0], (y - K[3]) * depth_right / K[2] - T[1], depth_right - T[2]);
float3 cameraPosDown = make_float3((x - K[1]) * depth_down / K[0] - T[0], (y + 1 - K[3]) * depth_down / K[2] - T[1], depth_down - T[2]);
normal = normalize(cross(cameraPosRight - cameraPos, cameraPosDown - cameraPos));
}
}
normalMap[y * IMG_WIDTH + x] = normal;
}
When executing the kernel function, the computer screen begin to blink and then blue and then crash.
If I comment the two ifs, everything is right.
Then I try to move the last assignment into the if like:
__global__ void getNormalMapKernel(const ushort *dev_depth, float3 *normalMap, const float *K ,const float *T)
{
int x = threadIdx.x;
int y = blockIdx.x;
float3 normal = make_float3(0.0, 0.0, 0.0);
if (x < IMG_WIDTH - 1 && y < IMG_HEIGHT - 1)
{
ushort depth = dev_depth[y * IMG_WIDTH + x];
ushort depth_right = dev_depth[y * IMG_WIDTH+ x + 1];
ushort depth_down = dev_depth[(y + 1) * IMG_WIDTH + x];
if ( depth && absDevUshort(depth, depth_right) < 20 && absDevUshort(depth, depth_down) < 20)
{
float3 cameraPos = make_float3((x - K[1]) * depth / K[0] - T[0], (y - K[3]) * depth / K[2] - T[1], depth - T[2]);
float3 cameraPosRight = make_float3((x + 1 - K[1]) * depth_right / K[0] - T[0], (y - K[3]) * depth_right / K[2] - T[1], depth_right - T[2]);
float3 cameraPosDown = make_float3((x - K[1]) * depth_down / K[0] - T[0], (y + 1 - K[3]) * depth_down / K[2] - T[1], depth_down - T[2]);
normalMap[y * IMG_WIDTH + x]= normalize(cross(cameraPosRight - cameraPos, cameraPosDown - cameraPos));
}
}
}
It crashes again. It seems that if I change the value of normal
in the ifs or assign a new value to the normalMap[y * IMG_WIDTH + x]
in the ifs, it will cause crash.
Anyone can help? I will be deeply grateful to who can solve the problem.