primref.h 4.1 KB

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