| 1 | // Copyright 2009-2021 Intel Corporation |
| 2 | // SPDX-License-Identifier: Apache-2.0 |
| 3 | |
| 4 | #pragma once |
| 5 | |
| 6 | #include "default.h" |
| 7 | |
| 8 | namespace embree |
| 9 | { |
| 10 | /*! A primitive reference stores the bounds of the primitive and its ID. */ |
| 11 | struct __aligned(32) PrimRef |
| 12 | { |
| 13 | __forceinline PrimRef () {} |
| 14 | |
| 15 | #if defined(__AVX__) |
| 16 | __forceinline PrimRef(const PrimRef& v) { |
| 17 | vfloat8::store((float*)this,vfloat8::load((float*)&v)); |
| 18 | } |
| 19 | __forceinline PrimRef& operator=(const PrimRef& v) { |
| 20 | vfloat8::store((float*)this,vfloat8::load((float*)&v)); return *this; |
| 21 | } |
| 22 | #endif |
| 23 | |
| 24 | __forceinline PrimRef (const BBox3fa& bounds, unsigned int geomID, unsigned int primID) |
| 25 | { |
| 26 | lower = Vec3fx(bounds.lower, geomID); |
| 27 | upper = Vec3fx(bounds.upper, primID); |
| 28 | } |
| 29 | |
| 30 | __forceinline PrimRef (const BBox3fa& bounds, size_t id) |
| 31 | { |
| 32 | #if defined(__64BIT__) |
| 33 | lower = Vec3fx(bounds.lower, (unsigned)(id & 0xFFFFFFFF)); |
| 34 | upper = Vec3fx(bounds.upper, (unsigned)((id >> 32) & 0xFFFFFFFF)); |
| 35 | #else |
| 36 | lower = Vec3fx(bounds.lower, (unsigned)id); |
| 37 | upper = Vec3fx(bounds.upper, (unsigned)0); |
| 38 | #endif |
| 39 | } |
| 40 | |
| 41 | /*! calculates twice the center of the primitive */ |
| 42 | __forceinline const Vec3fa center2() const { |
| 43 | return lower+upper; |
| 44 | } |
| 45 | |
| 46 | /*! return the bounding box of the primitive */ |
| 47 | __forceinline const BBox3fa bounds() const { |
| 48 | return BBox3fa(lower,upper); |
| 49 | } |
| 50 | |
| 51 | /*! size for bin heuristic is 1 */ |
| 52 | __forceinline unsigned size() const { |
| 53 | return 1; |
| 54 | } |
| 55 | |
| 56 | /*! returns bounds and centroid used for binning */ |
| 57 | __forceinline void binBoundsAndCenter(BBox3fa& bounds_o, Vec3fa& center_o) const |
| 58 | { |
| 59 | bounds_o = bounds(); |
| 60 | center_o = embree::center2(bounds_o); |
| 61 | } |
| 62 | |
| 63 | __forceinline unsigned& geomIDref() { // FIXME: remove !!!!!!! |
| 64 | return lower.u; |
| 65 | } |
| 66 | __forceinline unsigned& primIDref() { // FIXME: remove !!!!!!! |
| 67 | return upper.u; |
| 68 | } |
| 69 | |
| 70 | /*! returns the geometry ID */ |
| 71 | __forceinline unsigned geomID() const { |
| 72 | return lower.a; |
| 73 | } |
| 74 | |
| 75 | /*! returns the primitive ID */ |
| 76 | __forceinline unsigned primID() const { |
| 77 | return upper.a; |
| 78 | } |
| 79 | |
| 80 | /*! returns an size_t sized ID */ |
| 81 | __forceinline size_t ID() const { |
| 82 | #if defined(__64BIT__) |
| 83 | return size_t(lower.u) + (size_t(upper.u) << 32); |
| 84 | #else |
| 85 | return size_t(lower.u); |
| 86 | #endif |
| 87 | } |
| 88 | |
| 89 | /*! special function for operator< */ |
| 90 | __forceinline uint64_t ID64() const { |
| 91 | return (((uint64_t)primID()) << 32) + (uint64_t)geomID(); |
| 92 | } |
| 93 | |
| 94 | /*! allows sorting the primrefs by ID */ |
| 95 | friend __forceinline bool operator<(const PrimRef& p0, const PrimRef& p1) { |
| 96 | return p0.ID64() < p1.ID64(); |
| 97 | } |
| 98 | |
| 99 | /*! Outputs primitive reference to a stream. */ |
| 100 | friend __forceinline embree_ostream operator<<(embree_ostream cout, const PrimRef& ref) { |
| 101 | return cout << "{ lower = " << ref.lower << ", upper = " << ref.upper << ", geomID = " << ref.geomID() << ", primID = " << ref.primID() << " }" ; |
| 102 | } |
| 103 | |
| 104 | public: |
| 105 | Vec3fx lower; //!< lower bounds and geomID |
| 106 | Vec3fx upper; //!< upper bounds and primID |
| 107 | }; |
| 108 | |
| 109 | /*! fast exchange for PrimRefs */ |
| 110 | __forceinline void xchg(PrimRef& a, PrimRef& b) |
| 111 | { |
| 112 | #if defined(__AVX__) |
| 113 | const vfloat8 aa = vfloat8::load((float*)&a); |
| 114 | const vfloat8 bb = vfloat8::load((float*)&b); |
| 115 | vfloat8::store((float*)&a,bb); |
| 116 | vfloat8::store((float*)&b,aa); |
| 117 | #else |
| 118 | std::swap(a,b); |
| 119 | #endif |
| 120 | } |
| 121 | |
| 122 | /************************************************************************************/ |
| 123 | /************************************************************************************/ |
| 124 | /************************************************************************************/ |
| 125 | /************************************************************************************/ |
| 126 | |
| 127 | struct SubGridBuildData { |
| 128 | unsigned short sx,sy; |
| 129 | unsigned int primID; |
| 130 | |
| 131 | __forceinline SubGridBuildData() {}; |
| 132 | __forceinline SubGridBuildData(const unsigned int sx, const unsigned int sy, const unsigned int primID) : sx(sx), sy(sy), primID(primID) {}; |
| 133 | |
| 134 | __forceinline size_t x() const { return (size_t)sx & 0x7fff; } |
| 135 | __forceinline size_t y() const { return (size_t)sy & 0x7fff; } |
| 136 | |
| 137 | }; |
| 138 | } |
| 139 | |