NodeCodecQuadTreeHalfFloat.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. // SPDX-FileCopyrightText: 2021 Jorrit Rouwe
  2. // SPDX-License-Identifier: MIT
  3. #pragma once
  4. #include <Jolt/Core/ByteBuffer.h>
  5. #include <Jolt/Math/HalfFloat.h>
  6. #include <Jolt/AABBTree/AABBTreeBuilder.h>
  7. JPH_NAMESPACE_BEGIN
  8. template <int Alignment>
  9. class NodeCodecQuadTreeHalfFloat
  10. {
  11. public:
  12. /// Number of child nodes of this node
  13. static constexpr int NumChildrenPerNode = 4;
  14. /// Header for the tree
  15. struct Header
  16. {
  17. Float3 mRootBoundsMin;
  18. Float3 mRootBoundsMax;
  19. uint32 mRootProperties;
  20. };
  21. /// Size of the header (an empty struct is always > 0 bytes so this needs a separate variable)
  22. static constexpr int HeaderSize = sizeof(Header);
  23. /// Stack size to use during DecodingContext::sWalkTree
  24. static constexpr int StackSize = 128;
  25. /// Node properties
  26. enum : uint32
  27. {
  28. TRIANGLE_COUNT_BITS = 4,
  29. TRIANGLE_COUNT_SHIFT = 28,
  30. TRIANGLE_COUNT_MASK = (1 << TRIANGLE_COUNT_BITS) - 1,
  31. OFFSET_BITS = 28,
  32. OFFSET_MASK = (1 << OFFSET_BITS) - 1,
  33. OFFSET_NON_SIGNIFICANT_BITS = 2,
  34. OFFSET_NON_SIGNIFICANT_MASK = (1 << OFFSET_NON_SIGNIFICANT_BITS) - 1,
  35. };
  36. /// Node structure
  37. struct Node
  38. {
  39. HalfFloat mBoundsMinX[4]; ///< 4 child bounding boxes
  40. HalfFloat mBoundsMinY[4];
  41. HalfFloat mBoundsMinZ[4];
  42. HalfFloat mBoundsMaxX[4];
  43. HalfFloat mBoundsMaxY[4];
  44. HalfFloat mBoundsMaxZ[4];
  45. uint32 mNodeProperties[4]; ///< 4 child node properties
  46. };
  47. static_assert(sizeof(Node) == 64, "Node should be 64 bytes");
  48. /// This class encodes and compresses quad tree nodes
  49. class EncodingContext
  50. {
  51. public:
  52. /// Get an upper bound on the amount of bytes needed for a node tree with inNodeCount nodes
  53. uint GetPessimisticMemoryEstimate(uint inNodeCount) const
  54. {
  55. return inNodeCount * (sizeof(Node) + Alignment - 1);
  56. }
  57. /// Allocate a new node for inNode.
  58. /// Algorithm can modify the order of ioChildren to indicate in which order children should be compressed
  59. /// Algorithm can enlarge the bounding boxes of the children during compression and returns these in outChildBoundsMin, outChildBoundsMax
  60. /// inNodeBoundsMin, inNodeBoundsMax is the bounding box if inNode possibly widened by compressing the parent node
  61. /// Returns uint(-1) on error and reports the error in outError
  62. uint NodeAllocate(const AABBTreeBuilder::Node *inNode, Vec3Arg inNodeBoundsMin, Vec3Arg inNodeBoundsMax, Array<const AABBTreeBuilder::Node *> &ioChildren, Vec3 outChildBoundsMin[NumChildrenPerNode], Vec3 outChildBoundsMax[NumChildrenPerNode], ByteBuffer &ioBuffer, const char *&outError) const
  63. {
  64. // We don't emit nodes for leafs
  65. if (!inNode->HasChildren())
  66. return (uint)ioBuffer.size();
  67. // Align the buffer
  68. ioBuffer.Align(Alignment);
  69. uint node_start = (uint)ioBuffer.size();
  70. // Fill in bounds
  71. Node *node = ioBuffer.Allocate<Node>();
  72. for (size_t i = 0; i < 4; ++i)
  73. {
  74. if (i < ioChildren.size())
  75. {
  76. const AABBTreeBuilder::Node *this_node = ioChildren[i];
  77. // Copy bounding box
  78. node->mBoundsMinX[i] = HalfFloatConversion::FromFloat<HalfFloatConversion::ROUND_TO_NEG_INF>(this_node->mBounds.mMin.GetX());
  79. node->mBoundsMinY[i] = HalfFloatConversion::FromFloat<HalfFloatConversion::ROUND_TO_NEG_INF>(this_node->mBounds.mMin.GetY());
  80. node->mBoundsMinZ[i] = HalfFloatConversion::FromFloat<HalfFloatConversion::ROUND_TO_NEG_INF>(this_node->mBounds.mMin.GetZ());
  81. node->mBoundsMaxX[i] = HalfFloatConversion::FromFloat<HalfFloatConversion::ROUND_TO_POS_INF>(this_node->mBounds.mMax.GetX());
  82. node->mBoundsMaxY[i] = HalfFloatConversion::FromFloat<HalfFloatConversion::ROUND_TO_POS_INF>(this_node->mBounds.mMax.GetY());
  83. node->mBoundsMaxZ[i] = HalfFloatConversion::FromFloat<HalfFloatConversion::ROUND_TO_POS_INF>(this_node->mBounds.mMax.GetZ());
  84. // Store triangle count
  85. node->mNodeProperties[i] = this_node->GetTriangleCount() << TRIANGLE_COUNT_SHIFT;
  86. if (this_node->GetTriangleCount() >= TRIANGLE_COUNT_MASK)
  87. {
  88. outError = "NodeCodecQuadTreeHalfFloat: Too many triangles";
  89. return uint(-1);
  90. }
  91. }
  92. else
  93. {
  94. // Make this an invalid triangle node
  95. node->mNodeProperties[i] = uint32(TRIANGLE_COUNT_MASK) << TRIANGLE_COUNT_SHIFT;
  96. // Make bounding box invalid
  97. node->mBoundsMinX[i] = HALF_FLT_MAX;
  98. node->mBoundsMinY[i] = HALF_FLT_MAX;
  99. node->mBoundsMinZ[i] = HALF_FLT_MAX;
  100. node->mBoundsMaxX[i] = HALF_FLT_MAX;
  101. node->mBoundsMaxY[i] = HALF_FLT_MAX;
  102. node->mBoundsMaxZ[i] = HALF_FLT_MAX;
  103. }
  104. }
  105. // Since we don't keep track of the bounding box while descending the tree, we keep the root bounds at all levels for triangle compression
  106. for (int i = 0; i < NumChildrenPerNode; ++i)
  107. {
  108. outChildBoundsMin[i] = inNodeBoundsMin;
  109. outChildBoundsMax[i] = inNodeBoundsMax;
  110. }
  111. return node_start;
  112. }
  113. /// Once all nodes have been added, this call finalizes all nodes by patching in the offsets of the child nodes (that were added after the node itself was added)
  114. bool NodeFinalize(const AABBTreeBuilder::Node *inNode, uint inNodeStart, uint inNumChildren, const uint *inChildrenNodeStart, const uint *inChildrenTrianglesStart, ByteBuffer &ioBuffer, const char *&outError) const
  115. {
  116. if (!inNode->HasChildren())
  117. return true;
  118. Node *node = ioBuffer.Get<Node>(inNodeStart);
  119. for (uint i = 0; i < inNumChildren; ++i)
  120. {
  121. // If there are triangles, use the triangle offset otherwise use the node offset
  122. uint offset = node->mNodeProperties[i] != 0? inChildrenTrianglesStart[i] : inChildrenNodeStart[i];
  123. if (offset & OFFSET_NON_SIGNIFICANT_MASK)
  124. {
  125. outError = "NodeCodecQuadTreeHalfFloat: Internal Error: Offset has non-signifiant bits set";
  126. return false;
  127. }
  128. offset >>= OFFSET_NON_SIGNIFICANT_BITS;
  129. if (offset & ~OFFSET_MASK)
  130. {
  131. outError = "NodeCodecQuadTreeHalfFloat: Offset too large. Too much data.";
  132. return false;
  133. }
  134. // Store offset of next node / triangles
  135. node->mNodeProperties[i] |= offset;
  136. }
  137. return true;
  138. }
  139. /// Once all nodes have been finalized, this will finalize the header of the nodes
  140. bool Finalize(Header *outHeader, const AABBTreeBuilder::Node *inRoot, uint inRootNodeStart, uint inRootTrianglesStart, const char *&outError) const
  141. {
  142. uint offset = inRoot->HasChildren()? inRootNodeStart : inRootTrianglesStart;
  143. if (offset & OFFSET_NON_SIGNIFICANT_MASK)
  144. {
  145. outError = "NodeCodecQuadTreeHalfFloat: Internal Error: Offset has non-signifiant bits set";
  146. return false;
  147. }
  148. offset >>= OFFSET_NON_SIGNIFICANT_BITS;
  149. if (offset & ~OFFSET_MASK)
  150. {
  151. outError = "NodeCodecQuadTreeHalfFloat: Offset too large. Too much data.";
  152. return false;
  153. }
  154. inRoot->mBounds.mMin.StoreFloat3(&outHeader->mRootBoundsMin);
  155. inRoot->mBounds.mMax.StoreFloat3(&outHeader->mRootBoundsMax);
  156. outHeader->mRootProperties = offset + (inRoot->GetTriangleCount() << TRIANGLE_COUNT_SHIFT);
  157. if (inRoot->GetTriangleCount() >= TRIANGLE_COUNT_MASK)
  158. {
  159. outError = "NodeCodecQuadTreeHalfFloat: Too many triangles";
  160. return false;
  161. }
  162. return true;
  163. }
  164. };
  165. /// This class decodes and decompresses quad tree nodes
  166. class DecodingContext
  167. {
  168. public:
  169. /// Get the amount of bits needed to store an ID to a triangle block
  170. inline static uint sTriangleBlockIDBits(const ByteBuffer &inTree)
  171. {
  172. return 32 - CountLeadingZeros((uint32)inTree.size()) - OFFSET_NON_SIGNIFICANT_BITS;
  173. }
  174. /// Convert a triangle block ID to the start of the triangle buffer
  175. inline static const void * sGetTriangleBlockStart(const uint8 *inBufferStart, uint inTriangleBlockID)
  176. {
  177. return inBufferStart + (inTriangleBlockID << OFFSET_NON_SIGNIFICANT_BITS);
  178. }
  179. /// Constructor
  180. JPH_INLINE explicit DecodingContext(const Header *inHeader)
  181. {
  182. // Start with the root node on the stack
  183. mNodeStack[0] = inHeader->mRootProperties;
  184. }
  185. /// Walk the node tree calling the Visitor::VisitNodes for each node encountered and Visitor::VisitTriangles for each triangle encountered
  186. template <class TriangleContext, class Visitor>
  187. JPH_INLINE void WalkTree(const uint8 *inBufferStart, const TriangleContext &inTriangleContext, Visitor &ioVisitor)
  188. {
  189. do
  190. {
  191. // Test if node contains triangles
  192. uint32 node_properties = mNodeStack[mTop];
  193. uint32 tri_count = node_properties >> TRIANGLE_COUNT_SHIFT;
  194. if (tri_count == 0)
  195. {
  196. const Node *node = reinterpret_cast<const Node *>(inBufferStart + (node_properties << OFFSET_NON_SIGNIFICANT_BITS));
  197. // Unpack bounds
  198. UVec4 bounds_minxy = UVec4::sLoadInt4(reinterpret_cast<const uint32 *>(&node->mBoundsMinX[0]));
  199. Vec4 bounds_minx = HalfFloatConversion::ToFloat(bounds_minxy);
  200. Vec4 bounds_miny = HalfFloatConversion::ToFloat(bounds_minxy.Swizzle<SWIZZLE_Z, SWIZZLE_W, SWIZZLE_UNUSED, SWIZZLE_UNUSED>());
  201. UVec4 bounds_minzmaxx = UVec4::sLoadInt4(reinterpret_cast<const uint32 *>(&node->mBoundsMinZ[0]));
  202. Vec4 bounds_minz = HalfFloatConversion::ToFloat(bounds_minzmaxx);
  203. Vec4 bounds_maxx = HalfFloatConversion::ToFloat(bounds_minzmaxx.Swizzle<SWIZZLE_Z, SWIZZLE_W, SWIZZLE_UNUSED, SWIZZLE_UNUSED>());
  204. UVec4 bounds_maxyz = UVec4::sLoadInt4(reinterpret_cast<const uint32 *>(&node->mBoundsMaxY[0]));
  205. Vec4 bounds_maxy = HalfFloatConversion::ToFloat(bounds_maxyz);
  206. Vec4 bounds_maxz = HalfFloatConversion::ToFloat(bounds_maxyz.Swizzle<SWIZZLE_Z, SWIZZLE_W, SWIZZLE_UNUSED, SWIZZLE_UNUSED>());
  207. // Load properties for 4 children
  208. UVec4 properties = UVec4::sLoadInt4(&node->mNodeProperties[0]);
  209. // Check which sub nodes to visit
  210. int num_results = ioVisitor.VisitNodes(bounds_minx, bounds_miny, bounds_minz, bounds_maxx, bounds_maxy, bounds_maxz, properties, mTop);
  211. // Push them onto the stack
  212. JPH_ASSERT(mTop + 4 < StackSize);
  213. properties.StoreInt4(&mNodeStack[mTop]);
  214. mTop += num_results;
  215. }
  216. else if (tri_count != TRIANGLE_COUNT_MASK) // TRIANGLE_COUNT_MASK indicates a padding node, normally we shouldn't visit these nodes but when querying with a big enough box you could touch HALF_FLT_MAX (about 65K)
  217. {
  218. // Node contains triangles, do individual tests
  219. uint32 triangle_block_id = node_properties & OFFSET_MASK;
  220. const void *triangles = sGetTriangleBlockStart(inBufferStart, triangle_block_id);
  221. ioVisitor.VisitTriangles(inTriangleContext, triangles, tri_count, triangle_block_id);
  222. }
  223. // Check if we're done
  224. if (ioVisitor.ShouldAbort())
  225. break;
  226. // Fetch next node until we find one that the visitor wants to see
  227. do
  228. --mTop;
  229. while (mTop >= 0 && !ioVisitor.ShouldVisitNode(mTop));
  230. }
  231. while (mTop >= 0);
  232. }
  233. /// This can be used to have the visitor early out (ioVisitor.ShouldAbort() returns true) and later continue again (call WalkTree() again)
  234. bool IsDoneWalking() const
  235. {
  236. return mTop < 0;
  237. }
  238. private:
  239. uint32 mNodeStack[StackSize];
  240. int mTop = 0;
  241. };
  242. };
  243. JPH_NAMESPACE_END