Advertisement
Geometrian

_BVH_Node_2ary_FloatingPointPacked

Oct 12th, 2017
275
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.15 KB | None | 0 0
  1. //Packed version of previous (`_BVH_Node_2ary_FloatingPointBasic`).
  2. //  Crammed into one cache line by packing the pointers into the fourth components of the vectors.
  3. ATTRIB_ALIGN_BEFORE(64) class _BVH_Node_2ary_FloatingPointPacked final {
  4.     public:
  5.         union {
  6.             struct {
  7.                 uint32_t _pad0[3];
  8.                 struct Info info;
  9.                 uint8_t _pad1[3];
  10.                 uint32_t _pad2[12];
  11.             };
  12.  
  13.             union Internal final {
  14.                 struct {
  15.                     Math::AABB bound_child0;
  16.                     Math::AABB bound_child1;
  17.                 };
  18.                 struct {
  19.                     uint32_t _pad0[3]; //(child 0's lower point)
  20.                     struct Info info; //Information byte
  21.                     //Blank.  We could put stuff here (but e.g. extending the child indices from 6
  22.                     //  bytes to 7 bytes would make the calculation of both more complicated (slower).
  23.                     uint8_t _pad1[3];
  24.  
  25.                     uint32_t _pad2[3]; //(child 0's upper point)
  26.                     uint32_t index_child0_upper; //Child 0's index's upper four bytes
  27.  
  28.                     uint32_t _pad3[3]; //(child 1's lower point)
  29.                     uint16_t index_child0_lower; //Child 0's index's lower two bytes
  30.                     uint16_t index_child1_upper; //Child 1's index's upper two bytes
  31.  
  32.                     uint32_t _pad4[3]; //(child 1's upper point)
  33.                     uint32_t index_child1_lower; //Child 1's index's lower four bytes
  34.                 };
  35.  
  36.                 inline Math::AABB const& get_bound_child0(void) const { return bound_child0; }
  37.                 inline Math::AABB const& get_bound_child1(void) const { return bound_child1; }
  38.                 inline _BVH_Node_2ary_FloatingPointPacked const* get_child0(void) const {
  39.                     assert_term(!info.is_leaf,"Implementation error!"); //Not an internal node!
  40.                     uint64_t index_child0 = static_cast<uint64_t>(index_child0_lower) | (static_cast<uint64_t>(index_child0_upper)<<16);
  41.                     return reinterpret_cast<_BVH_Node_2ary_FloatingPointPacked const*>(this) + index_child0;
  42.                 }
  43.                 inline _BVH_Node_2ary_FloatingPointPacked const* get_child1(void) const {
  44.                     assert_term(!info.is_leaf,"Implementation error!"); //Not an internal node!
  45.                     uint64_t index_child1 = static_cast<uint64_t>(index_child1_lower) | (static_cast<uint64_t>(index_child1_upper)<<32);
  46.                     return reinterpret_cast<_BVH_Node_2ary_FloatingPointPacked const*>(this) + index_child1;
  47.                 }
  48.                 inline void set_child0(_BVH_Node_2ary_FloatingPointPacked const* child0) {
  49.                     uint64_t ichild0 = child0 - reinterpret_cast<_BVH_Node_2ary_FloatingPointPacked const*>(this);
  50.                     index_child0_lower = static_cast<uint16_t>(ichild0    );
  51.                     index_child0_upper = static_cast<uint32_t>(ichild0>>16);
  52.                     assert_term(get_child0()==child0,"Implementation error!");
  53.                 }
  54.                 inline void set_child1(_BVH_Node_2ary_FloatingPointPacked const* child1) {
  55.                     uint64_t ichild1 = child1 - reinterpret_cast<_BVH_Node_2ary_FloatingPointPacked const*>(this);
  56.                     index_child1_lower = static_cast<uint32_t>(ichild1    );
  57.                     index_child1_upper = static_cast<uint16_t>(ichild1>>32);
  58.                     assert_term(get_child1()==child1,"Implementation error!");
  59.                 }
  60.             } internal;
  61.  
  62.             union Leaf final {
  63.                 struct {
  64.                     Math::AABB bound_child0;
  65.                     Math::AABB bound_child1;
  66.                 };
  67.                 struct {
  68.                     uint32_t _pad0[3]; //(child 0's lower point)
  69.                     struct Info info; //Information byte
  70.                     //Blank.  We could put stuff here (but e.g. extending the child indices from 6
  71.                     //  bytes to 7 bytes would make the calculation of both more complicated (slower).
  72.                     uint8_t _pad1[3];
  73.  
  74.                     uint32_t _pad2[3]; //(child 0's upper point)
  75.                     uint32_t index_child0_upper; //Child 0's index's upper four bytes
  76.  
  77.                     uint32_t _pad3[3]; //(child 1's lower point)
  78.                     uint16_t index_child0_lower; //Child 0's index's lower two bytes
  79.                     uint16_t index_child1_upper; //Child 1's index's upper two bytes
  80.  
  81.                     uint32_t _pad4[3]; //(child 1's upper point)
  82.                     uint32_t index_child1_lower; //Child 1's index's lower four bytes
  83.                 };
  84.  
  85.                 inline Math::AABB const& get_bound_child0(void) const { return bound_child0; }
  86.                 inline Math::AABB const& get_bound_child1(void) const { return bound_child1; }
  87.                 inline Objects::ObjectBase const* get_child0(std::vector<Objects::ObjectBase*> const& objects) const {
  88.                     assert_term(info.is_leaf,"Implementation error!"); //Not a leaf node!
  89.                     uint64_t index_child0 = static_cast<uint64_t>(index_child0_lower) | (static_cast<uint64_t>(index_child0_upper)<<16);
  90.                     return objects[static_cast<size_t>(index_child0)];
  91.                 }
  92.                 inline Objects::ObjectBase const* get_child1(std::vector<Objects::ObjectBase*> const& objects) const {
  93.                     assert_term(info.is_leaf,"Implementation error!"); //Not a leaf node!
  94.                     uint64_t index_child1 = static_cast<uint64_t>(index_child1_lower) | (static_cast<uint64_t>(index_child1_upper)<<32);
  95.                     return objects[static_cast<size_t>(index_child1)];
  96.                 }
  97.                 inline void set_child0(std::vector<Objects::ObjectBase*> const& objects, uint64_t ichild0) {
  98.                     index_child0_lower = static_cast<uint16_t>(ichild0    );
  99.                     index_child0_upper = static_cast<uint32_t>(ichild0>>16);
  100.                     assert_term(get_child0(objects)==objects[static_cast<size_t>(ichild0)],"Implementation error!");
  101.                 }
  102.                 inline void set_child1(std::vector<Objects::ObjectBase*> const& objects, uint64_t ichild1) {
  103.                     index_child1_lower = static_cast<uint32_t>(ichild1    );
  104.                     index_child1_upper = static_cast<uint16_t>(ichild1>>32);
  105.                     assert_term(get_child1(objects)==objects[static_cast<size_t>(ichild1)],"Implementation error!");
  106.                 }
  107.             } leaf;
  108.         };
  109.  
  110.     public:
  111.         inline _BVH_Node_2ary_FloatingPointPacked(void) {}
  112.  
  113.         inline Math::AABB get_bound(void) const {
  114.             Math::AABB result = Math::AABB(VEC3_INFs,-VEC3_INFs);
  115.             if (!info.is_leaf) {
  116.                 result |= internal.get_bound_child0();
  117.                 result |= internal.get_bound_child1();
  118.             } else {
  119.                                       result|=leaf.get_bound_child0();
  120.                 if (info.num_objs==2) result|=leaf.get_bound_child1();
  121.             }
  122.             return result;
  123.         }
  124.  
  125.         inline void set_internal(_BVH_Node_2ary_FloatingPointPacked const* child0, _BVH_Node_2ary_FloatingPointPacked const* child1) {
  126.             assert_term(child0!=child1,"Implementation error!");
  127.             internal.bound_child0 = child0->get_bound();
  128.             internal.bound_child1 = child1->get_bound();
  129.             //Note needs to come after setting bounds; this data is packed inside it.
  130.             info.set_internal();
  131.             internal.set_child0(child0);
  132.             internal.set_child1(child1);
  133.         }
  134.         inline void set_leaf    (std::vector<Objects::ObjectBase*> const& objects, uint64_t ichild0,uint64_t ichild1) {
  135.             assert_term(ichild0!=ichild1,"Implementation error!");
  136.             //Note `info` needs to come after setting bounds; this data is packed inside it.
  137.             leaf.bound_child0 = objects[static_cast<size_t>(ichild0)]->get_bound_rt_alltime();
  138.             if (ichild1!=~0ull) {
  139.                 leaf.bound_child1 = objects[static_cast<size_t>(ichild1)]->get_bound_rt_alltime();
  140.                 info.set_leaf(2);
  141.                 leaf.set_child0(objects,ichild0);
  142.                 leaf.set_child1(objects,ichild1);
  143.             } else {
  144.                 leaf.bound_child1 = Math::AABB(VEC3_qNaNs,VEC3_qNaNs);
  145.                 info.set_leaf(1);
  146.                 leaf.set_child0(objects,ichild0);
  147.             }
  148.         }
  149.  
  150.         inline void print(uint32_t indent) const { _print_node(this,indent); }
  151. } ATTRIB_ALIGN_AFTER(64);
  152. static_assert(alignof(_BVH_Node_2ary_FloatingPointPacked)==64,"Implementation error!");
  153. static_assert(sizeof (_BVH_Node_2ary_FloatingPointPacked)==64,"Implementation error!");
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement