hit.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. // Copyright 2009-2021 Intel Corporation
  2. // SPDX-License-Identifier: Apache-2.0
  3. #pragma once
  4. #include "default.h"
  5. #include "ray.h"
  6. #include "instance_stack.h"
  7. namespace embree
  8. {
  9. /* Hit structure for K hits */
  10. template<int K>
  11. struct HitK
  12. {
  13. /* Default construction does nothing */
  14. __forceinline HitK() {}
  15. /* Constructs a hit */
  16. __forceinline HitK(const RTCRayQueryContext* context, const vuint<K>& geomID, const vuint<K>& primID, const vfloat<K>& u, const vfloat<K>& v, const Vec3vf<K>& Ng)
  17. : Ng(Ng), u(u), v(v), primID(primID), geomID(geomID)
  18. {
  19. for (unsigned l = 0; l < RTC_MAX_INSTANCE_LEVEL_COUNT; ++l) {
  20. instID[l] = RTC_INVALID_GEOMETRY_ID;
  21. #if defined(RTC_GEOMETRY_INSTANCE_ARRAY)
  22. instPrimID[l] = RTC_INVALID_GEOMETRY_ID;
  23. #endif
  24. }
  25. instance_id_stack::copy_UV<K>(context->instID, instID);
  26. #if defined(RTC_GEOMETRY_INSTANCE_ARRAY)
  27. instance_id_stack::copy_UV<K>(context->instPrimID, instPrimID);
  28. #endif
  29. }
  30. /* Constructs a hit */
  31. __forceinline HitK(const RTCRayQueryContext* context, const vuint<K>& geomID, const vuint<K>& primID, const Vec2vf<K>& uv, const Vec3vf<K>& Ng)
  32. : HitK(context,geomID,primID,uv.x,uv.y,Ng) {}
  33. /* Returns the size of the hit */
  34. static __forceinline size_t size() { return K; }
  35. public:
  36. Vec3vf<K> Ng; // geometry normal
  37. vfloat<K> u; // barycentric u coordinate of hit
  38. vfloat<K> v; // barycentric v coordinate of hit
  39. vuint<K> primID; // primitive ID
  40. vuint<K> geomID; // geometry ID
  41. vuint<K> instID[RTC_MAX_INSTANCE_LEVEL_COUNT]; // instance ID
  42. #if defined(RTC_GEOMETRY_INSTANCE_ARRAY)
  43. vuint<K> instPrimID[RTC_MAX_INSTANCE_LEVEL_COUNT]; // instance primitive ID
  44. #endif
  45. };
  46. /* Specialization for a single hit */
  47. template<>
  48. struct __aligned(16) HitK<1>
  49. {
  50. /* Default construction does nothing */
  51. __forceinline HitK() {}
  52. /* Constructs a hit */
  53. __forceinline HitK(const RTCRayQueryContext* context, unsigned int geomID, unsigned int primID, float u, float v, const Vec3fa& Ng)
  54. : Ng(Ng.x,Ng.y,Ng.z), u(u), v(v), primID(primID), geomID(geomID)
  55. {
  56. instance_id_stack::copy_UU(context, context->instID, instID);
  57. #if defined(RTC_GEOMETRY_INSTANCE_ARRAY)
  58. instance_id_stack::copy_UU(context, context->instPrimID, instPrimID);
  59. #endif
  60. }
  61. /* Constructs a hit */
  62. __forceinline HitK(const RTCRayQueryContext* context, unsigned int geomID, unsigned int primID, const Vec2f& uv, const Vec3fa& Ng)
  63. : HitK<1>(context,geomID,primID,uv.x,uv.y,Ng) {}
  64. /* Returns the size of the hit */
  65. static __forceinline size_t size() { return 1; }
  66. public:
  67. Vec3<float> Ng; // geometry normal
  68. float u; // barycentric u coordinate of hit
  69. float v; // barycentric v coordinate of hit
  70. unsigned int primID; // primitive ID
  71. unsigned int geomID; // geometry ID
  72. unsigned int instID[RTC_MAX_INSTANCE_LEVEL_COUNT]; // instance ID
  73. #if defined(RTC_GEOMETRY_INSTANCE_ARRAY)
  74. unsigned int instPrimID[RTC_MAX_INSTANCE_LEVEL_COUNT]; // instance primitive ID
  75. #endif
  76. };
  77. /* Shortcuts */
  78. typedef HitK<1> Hit;
  79. typedef HitK<4> Hit4;
  80. typedef HitK<8> Hit8;
  81. typedef HitK<16> Hit16;
  82. typedef HitK<VSIZEX> Hitx;
  83. /* Outputs hit to stream */
  84. template<int K>
  85. __forceinline embree_ostream operator<<(embree_ostream cout, const HitK<K>& ray)
  86. {
  87. cout << "{ " << embree_endl
  88. << " Ng = " << ray.Ng << embree_endl
  89. << " u = " << ray.u << embree_endl
  90. << " v = " << ray.v << embree_endl
  91. << " primID = " << ray.primID << embree_endl
  92. << " geomID = " << ray.geomID << embree_endl
  93. << " instID =";
  94. for (unsigned l = 0; l < RTC_MAX_INSTANCE_LEVEL_COUNT; ++l)
  95. {
  96. cout << " " << ray.instID[l];
  97. }
  98. #if defined(RTC_GEOMETRY_INSTANCE_ARRAY)
  99. cout << " instPrimID =";
  100. for (unsigned l = 0; l < RTC_MAX_INSTANCE_LEVEL_COUNT; ++l)
  101. {
  102. cout << " " << ray.instPrimID[l];
  103. }
  104. #endif
  105. cout << embree_endl;
  106. return cout << "}";
  107. }
  108. template<typename Hit>
  109. __forceinline void copyHitToRay(RayHit& ray, const Hit& hit)
  110. {
  111. ray.Ng = hit.Ng;
  112. ray.u = hit.u;
  113. ray.v = hit.v;
  114. ray.primID = hit.primID;
  115. ray.geomID = hit.geomID;
  116. instance_id_stack::copy_UU(hit.instID, ray.instID);
  117. #if defined(RTC_GEOMETRY_INSTANCE_ARRAY)
  118. instance_id_stack::copy_UU(hit.instPrimID, ray.instPrimID);
  119. #endif
  120. }
  121. template<int K>
  122. __forceinline void copyHitToRay(const vbool<K>& mask, RayHitK<K>& ray, const HitK<K>& hit)
  123. {
  124. vfloat<K>::storeu(mask,&ray.Ng.x, hit.Ng.x);
  125. vfloat<K>::storeu(mask,&ray.Ng.y, hit.Ng.y);
  126. vfloat<K>::storeu(mask,&ray.Ng.z, hit.Ng.z);
  127. vfloat<K>::storeu(mask,&ray.u, hit.u);
  128. vfloat<K>::storeu(mask,&ray.v, hit.v);
  129. vuint<K>::storeu(mask,&ray.primID, hit.primID);
  130. vuint<K>::storeu(mask,&ray.geomID, hit.geomID);
  131. instance_id_stack::copy_VV<K>(hit.instID, ray.instID, mask);
  132. #if defined(RTC_GEOMETRY_INSTANCE_ARRAY)
  133. instance_id_stack::copy_VV<K>(hit.instPrimID, ray.instPrimID, mask);
  134. #endif
  135. }
  136. }