title: Different behaviour between opencl c99 and c++ with the same implementation of a realtime voxel raycaster
body: <p>I am working with opencl to develop a voxel raycasting engine. I am trying to do something similar to <a href="http://maverick.inria.fr/Publications/2011/Cra11/" rel="nofollow">Gigavoxels</a> 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.</p>
  
  <p>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.</p>
  
  <p>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.</p>
  
  <p>Here is the C++ Code:</p>
  
  <pre><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 &lt; 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]);
  }   
  </code></pre>
  
  <p>Here is the OpenCL Code:</p>
  
  <pre><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 &lt; 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]);
  }
  </code></pre>
  
  <p>Here is extractOctreeNodeAddress, as requested:</p>
  
  <p>Both functions just do some bit operations:</p>
  
  <p>OpenCL version:</p>
  
  <pre><code>inline char extractOctreeNodeLeaf(uint value) {
   value = value &gt;&gt; 1;
   return value &amp; 1;
  }
  
  inline uint extractOctreeNodeAddress(uint value) {
   return value &gt;&gt; 2;
  }
  </code></pre>
  
  <p>C++ version:</p>
  
  <pre><code>inline byte GetLeafBit(uint value)
  {
   value = value &gt;&gt; 0x1;
   return value &amp; 0x1;
  }
  
  inline uint GetChildAddress(uint value)
  {
   return value &gt;&gt; 0x2;
  }
  </code></pre>
  
  <p>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?</p>
  
  <pre><code>if ((x == 265) &amp;&amp; (y == 209)) {
      /*float epsilon = 0.01f;
      float4 stuff = (float4)(0.7604471f, 0.9088342f, 0.9999924f, 0);
      if(fabs(pos.x - stuff.x) &lt; epsilon)  
          temp = (float4)(1, 0, 0, 1);
      else
          temp = (float4)(1, 1, 1, 1);
  
      break;*/
  
      if(pos.x &gt; 5.5)
      {
          temp = (float4)(1, 1, 1, 1);
          break;
      }
  }
  </code></pre>