Different behaviour between opencl c99 and c++ with the same implementation of a realtime voxel raycaster

StackOverflow https://stackoverflow.com/questions/10981493

  •  13-06-2021
  •  | 
  •  

Question

I am working with opencl to develop a voxel raycasting engine. I am trying to do something similar to Gigavoxels by Crassin. In this paper, they are using an octree to store the voxel data. For the moment I am just trying to descend inside the octree until I reach a leaf that contains rendering data.

I have made two implementations: one in OpenCl on the GPU and another in C++ on the CPU. The problem I am experiencing is that on the GPU the algorithm is going through a wrong number of levels until reaching a leaf inside the octree. The CPU version gives correct results. The algorithm for both versions is the same and the code is almost similar.

Do you guys know what could be the problem? Could it be a hardware problem, an OpenCl problem or am I doing something wrong? I am experiencing the same result on three different nVidia GPUs.

Here is the C++ Code:

// Calculate actual ray stepping position
glm::vec4 pos = eyeRay_o + eyeRay_d * t;

uint offset = 0;
//check if root is leaf
uint leafFlag = GetLeafBit(octreeNodes[0]);
//get children address of root
uint childrenAddress = GetChildAddress(octreeNodes[0]);

while (iterations < 30) {  
    iterations++; 

    // Calculate subdivision offset
    offset = (uint)(pos.x * 2) + (uint)(pos.y * 2) * 2 + (uint)(pos.z * 2) * 4;
     
    if (leafFlag == 1) {
         //return some colour and exit the loop
         break;
    }
    else 
    {
         glm::uvec4 off = glm::uvec4(pos.x * 2, pos.y * 2, pos.z * 2, pos.w * 2);
         pos.x = 2 * pos.x - off.x;
         pos.y = 2 * pos.y - off.y;
         pos.z = 2 * pos.z - off.z;
         pos.w = 2 * pos.w - off.w;   
    }

    // Extract node data from the children
    finalAddress = childrenAddress + offset;    
    leafFlag = GetLeafBit(nodes[finalAddress]);
    childrenAddress = GetChildAddress(nodes[finalAddress]);
}   

Here is the OpenCL Code:

// Calculate actual ray stepping position
float4 position = rayOrigin + rayDirection * t;
uint offset = 0;
//check if root is leaf
uint leafFlag = extractOctreeNodeLeaf(octreeNodes[0]);
//get children address of root
uint childrenAddress = extractOctreeNodeAddress(octreeNodes[0]);

//position will be in the [0, 1] interval
//size of octree is 1
while (iterations < 30) {  
    iterations++; 

    //calculate the index of the next child based on the position in the current subdivision
    offset = (uint)(position.x * 2) + (uint)(position.y * 2) * 2 + (uint)(position.z * 2) * 4;
     
    if (leafFlag == 1) {
        //return some colour and exit the loop
        break;
    }
    else 
    {
         //transform the position inside the parent 
         //to the position inside the child subdivision
         //size of child will be considered to be 1
         uint4 off; 
         off.x = floor(position.x * 2);
         off.y = floor(position.y * 2);
         off.z = floor(position.z * 2);
         off.w = floor(position.w * 2);
         position = 2 * position - off;  
    }
     
    // Extract node data from the children
    finalAddress = childrenAddress + offset; 
    leafFlag = extractOctreeNodeLeaf(octreeNodes[finalAddress]);
    //each node has an index to an array of 8 children - the index points to the first child
    childrenAddress = extractOctreeNodeAddress(octreeNodes[finalAddress]);
}

Here is extractOctreeNodeAddress, as requested:

Both functions just do some bit operations:

OpenCL version:

inline char extractOctreeNodeLeaf(uint value) {
 value = value >> 1;
 return value & 1;
}

inline uint extractOctreeNodeAddress(uint value) {
 return value >> 2;
}

C++ version:

inline byte GetLeafBit(uint value)
{
 value = value >> 0x1;
 return value & 0x1;
}

inline uint GetChildAddress(uint value)
{
 return value >> 0x2;
}

Hi, I found something interesting. I tried to test manually different variables comparing their CPU and GPU version on a single precise pixel and camera position and orientation. In the code below if I run the program like is now the pixel is being printed white, and the value (> 5.5 is totally wrong compared to the CPU implementation), but if I comment the last if structure, and uncomment the first one, the result I get is red....this is a bit unexplainable to me. Any ideas?

if ((x == 265) && (y == 209)) {
    /*float epsilon = 0.01f;
    float4 stuff = (float4)(0.7604471f, 0.9088342f, 0.9999924f, 0);
    if(fabs(pos.x - stuff.x) < epsilon)  
        temp = (float4)(1, 0, 0, 1);
    else
        temp = (float4)(1, 1, 1, 1);

    break;*/

    if(pos.x > 5.5)
    {
        temp = (float4)(1, 1, 1, 1);
        break;
    }
}
Was it helpful?

Solution

The main problem was the implicit cast from a float4 to a uint4.

Doing the cast element by element (still implicit) solved the problem.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top