Advertisement
Guest User

Untitled

a guest
May 29th, 2024
263
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.79 KB | None | 0 0
  1.  unsigned char StorageIndex(unsigned char childIndex, unsigned char childMask)
  2. {
  3.   if (childIndex == 0) return 0;
  4.   unsigned char storageIndex = (childMask & 1);
  5.   if (childIndex == 1) return storageIndex; storageIndex += !!(childMask & 2);
  6.   if (childIndex == 2) return storageIndex; storageIndex += !!(childMask & 4);
  7.   if (childIndex == 3) return storageIndex; storageIndex += !!(childMask & 8);
  8.   if (childIndex == 4) return storageIndex; storageIndex += !!(childMask & 16);
  9.   if (childIndex == 5) return storageIndex; storageIndex += !!(childMask & 32);
  10.   if (childIndex == 6) return storageIndex; storageIndex += !!(childMask & 64);
  11.   // assume /*if (childIndex == 7) */
  12.   return storageIndex;
  13. }
  14.  
  15. float RayBoxIntersects(float3 rayPos, float3 rayDir, float3 boxMin, float3 boxMax)
  16. {
  17.   const float largestFloat = 9999999999.f;
  18.   const float smallestFloat = 0.000000001f;
  19.   float3 invDir = (float3)(largestFloat, largestFloat, largestFloat);
  20.   if (fabs(rayDir.x) > smallestFloat) invDir.x = (float)1.0f / rayDir.x;
  21.   if (fabs(rayDir.y) > smallestFloat) invDir.y = (float)1.0f / rayDir.y;
  22.   if (fabs(rayDir.z) > smallestFloat) invDir.z = (float)1.0f / rayDir.z;
  23.   float3 mins = ((float3)(boxMin) - rayPos) * invDir; float3 maxs = ((float3)(boxMax) - rayPos) * invDir;
  24.   float tmax = min(min(max(mins[0], maxs[0]), max(mins[1], maxs[1])), max(mins[2], maxs[2]));
  25.   if (tmax < 0) return largestFloat;
  26.   float tmin = max(max(min(mins[0], maxs[0]), min(mins[1], maxs[1])), min(mins[2], maxs[2]));
  27.   if (tmin <= tmax) return max(tmin, 0.f);
  28.   return largestFloat;
  29. }
  30.  
  31. kernel void Draw(__global unsigned int *pPixels, __global int *pTreeDepth, __global unsigned int *pChildData, __global unsigned int *pColorData, __global unsigned char *pMasksData, __global float *pRayDirs, __global float *pCamPos, __global float *pRotData)
  32. {
  33.   const bool colorBySteps = 0;
  34.  
  35.   const float largestFloat = 9999999999.f;
  36.   const float smallestFloat = 0.000000001f;
  37.  
  38.   // Rotate Rays
  39.   int id = get_global_id(0);
  40.   float3 rayPos = (float3)(pCamPos[0], pCamPos[1], pCamPos[2]);
  41.   float3 rayDir = (float3)(pRayDirs[id * 3 + 0], pRayDirs[id * 3 + 1], pRayDirs[id * 3 + 2]);
  42.   rayDir = (float3)(rayDir.x, rayDir.y * pRotData[2] - rayDir.z * pRotData[3], rayDir.y * pRotData[3] + rayDir.z * pRotData[2]);
  43.   rayDir = (float3)(rayDir.x * pRotData[0] - rayDir.y * pRotData[1], rayDir.x * pRotData[1] + rayDir.y * pRotData[0], rayDir.z);
  44.  
  45.   const int stackSize = 22;
  46.   int stackPtr = 1;
  47.   int descents = 0;
  48.  
  49.   float3 stackRegionPos[stackSize];
  50.   unsigned int stackNodeID[stackSize];
  51.   unsigned char stackDepth[stackSize];
  52.  
  53.   // Push Root Node
  54.   stackNodeID[0] = 0;
  55.   stackDepth[0] = *pTreeDepth;
  56.   stackRegionPos[0] = (float3)(0, 0, 0);
  57.  
  58.   while (stackPtr > 0)
  59.   {
  60.     --stackPtr;
  61.     float3 regionPos = stackRegionPos[stackPtr];
  62.     unsigned int nodeID = stackNodeID[stackPtr];
  63.     unsigned char depth = stackDepth[stackPtr];
  64.  
  65.     unsigned char childMask = pMasksData[nodeID];
  66.     unsigned int parentID = pChildData[nodeID];
  67.  
  68.     for (int z = 0; z < 2; z++) for (int y = 0; y < 2; y++) for (int x = 0; x < 2; x++)
  69.     {
  70.       unsigned char c = 0;
  71.       // Use HERO when on the last level of the trees
  72.       if (depth == 0) c = (x ^ (rayDir.x < 0)) + (y ^ (rayDir.y < 0)) * 2 + (z ^ (rayDir.z < 0)) * 4; // HERO Algorithm
  73.       if (depth > 0) c = (x ^ (rayDir.x > 0)) + (y ^ (rayDir.y > 0)) * 2 + (z ^ (rayDir.z > 0)) * 4; // Reverse HERO on higher layers (since were making a reversed stack of work todo)
  74.  
  75.       if (((childMask >> c) & 1) == 0) continue; // Skip empty children
  76.       float3 ep = (float3)((c & 1) > 0, (c & 2) > 0, (c & 4) > 0); // Calculate Nodes Position
  77.       float3 subRegionPos = (float3)(regionPos * 2) + ep;
  78.  
  79.       // Calculate Ray To Node Distance
  80.       bool nearPixelSize = false;
  81.       if (1)
  82.       {
  83.         float distToNode = RayBoxIntersects(rayPos, rayDir, (float3)(subRegionPos * (1 << depth)), (float3)((subRegionPos + (float3)(1,1,1)) * (1 << depth)));
  84.         if (distToNode == largestFloat) continue; // Check we hit it
  85.       }
  86.  
  87.       int childID = parentID + StorageIndex(c, childMask); // Calculate Child Index
  88.  
  89.       descents++;
  90.  
  91.       // Reached Bottom (color)
  92.       if (depth == 0 || nearPixelSize)
  93.       {
  94.         if (colorBySteps) // Color By Steps
  95.         {
  96.           int dist = descents; dist = min(dist, 255); pPixels[id] = dist | (dist << 8) | (dist << 16);
  97.         }
  98.         else pPixels[id] = pColorData[childID]; // Color By Surface Hit
  99.         return; // Finished!
  100.       }
  101.  
  102.       stackRegionPos[stackPtr] = subRegionPos;
  103.       stackNodeID[stackPtr] = childID;
  104.       stackDepth[stackPtr] = depth - 1;
  105.       ++stackPtr;
  106.     }
  107.   }
  108.   if (colorBySteps) // Color By Steps
  109.   {
  110.     int dist = descents; dist = min(dist, 255); pPixels[id] = dist | (dist << 8) | (dist << 16);
  111.   }
  112. }
  113.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement