Advertisement
Zgragselus

Intersect vs BVH8

Apr 22nd, 2024
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.77 KB | None | 0 0
  1. uint IntersectNode(float4 origin, float4 direction, uint octantInverse, float maxDistance, float4 node0, float4 node1, float4 node2, float4 node3, float4 node4)
  2. {
  3. float3 p = node0.xyz;
  4.  
  5. uint emask = asuint(node0.w);
  6. uint eX = ExtractByte(emask, 0);
  7. uint eY = ExtractByte(emask, 1);
  8. uint eZ = ExtractByte(emask, 2);
  9.  
  10. float3 adjDirection = float3(
  11. asfloat(eX << 23) / direction.x,
  12. asfloat(eY << 23) / direction.y,
  13. asfloat(eZ << 23) / direction.z
  14. );
  15.  
  16. float3 adjOrigin = (p - origin.xyz) / direction.xyz;
  17.  
  18. uint hitMask = 0;
  19.  
  20. [unroll]
  21. for (int i = 0; i < 2; i++)
  22. {
  23. uint meta4 = asuint(i == 0 ? node1.z : node1.w);
  24.  
  25. uint isInner4 = (meta4 & (meta4 << 1)) & 0x10101010;
  26. uint innerMask4 = (((isInner4 << 3) >> 7) & 0x01010101) * 0xff;
  27. uint bitIndex4 = (meta4 ^ (octantInverse & innerMask4)) & 0x1F1F1F1F;
  28. uint childBits4 = (meta4 >> 5) & 0x07070707;
  29.  
  30. uint qLoX = asuint(i == 0 ? node2.x : node2.y);
  31. uint qHiX = asuint(i == 0 ? node2.z : node2.w);
  32.  
  33. uint qLoY = asuint(i == 0 ? node3.x : node3.y);
  34. uint qHiY = asuint(i == 0 ? node3.z : node3.w);
  35.  
  36. uint qLoZ = asuint(i == 0 ? node4.x : node4.y);
  37. uint qHiZ = asuint(i == 0 ? node4.z : node4.w);
  38.  
  39. uint xMin = direction.x < 0.0f ? qHiX : qLoX;
  40. uint xMax = direction.x < 0.0f ? qLoX : qHiX;
  41.  
  42. uint yMin = direction.y < 0.0f ? qHiY : qLoY;
  43. uint yMax = direction.y < 0.0f ? qLoY : qHiY;
  44.  
  45. uint zMin = direction.z < 0.0f ? qHiZ : qLoZ;
  46. uint zMax = direction.z < 0.0f ? qLoZ : qHiZ;
  47.  
  48. [unroll]
  49. for (int j = 0; j < 4; j++)
  50. {
  51. float3 tmin3 = float3(
  52. float(ExtractByte(xMin, j)),
  53. float(ExtractByte(yMin, j)),
  54. float(ExtractByte(zMin, j)));
  55.  
  56. float3 tmax3 = float3(
  57. float(ExtractByte(xMax, j)),
  58. float(ExtractByte(yMax, j)),
  59. float(ExtractByte(zMax, j)));
  60.  
  61. tmin3 = tmin3 * adjDirection + adjOrigin;
  62. tmax3 = tmax3 * adjDirection + adjOrigin;
  63.  
  64. float tmin = max(max(tmin3.x, tmin3.y), max(tmin3.z, 0.0f));
  65. float tmax = min(min(tmax3.x, tmax3.y), min(tmax3.z, maxDistance));
  66.  
  67. bool intersection = tmin < tmax;
  68. [branch]
  69. if (intersection)
  70. {
  71. uint childBits = ExtractByte(childBits4, j);
  72. uint bitIndex = ExtractByte(bitIndex4, j);
  73.  
  74. hitMask |= childBits << bitIndex;
  75. }
  76. }
  77. }
  78.  
  79. return hitMask;
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement