hit.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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 RTCIntersectContext* 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. instance_id_stack::copy_UV<K>(context->instID, instID);
  22. }
  23. /* Returns the size of the hit */
  24. static __forceinline size_t size() { return K; }
  25. public:
  26. Vec3vf<K> Ng; // geometry normal
  27. vfloat<K> u; // barycentric u coordinate of hit
  28. vfloat<K> v; // barycentric v coordinate of hit
  29. vuint<K> primID; // primitive ID
  30. vuint<K> geomID; // geometry ID
  31. vuint<K> instID[RTC_MAX_INSTANCE_LEVEL_COUNT]; // instance ID
  32. };
  33. /* Specialization for a single hit */
  34. template<>
  35. struct __aligned(16) HitK<1>
  36. {
  37. /* Default construction does nothing */
  38. __forceinline HitK() {}
  39. /* Constructs a hit */
  40. __forceinline HitK(const RTCIntersectContext* context, unsigned int geomID, unsigned int primID, float u, float v, const Vec3fa& Ng)
  41. : Ng(Ng.x,Ng.y,Ng.z), u(u), v(v), primID(primID), geomID(geomID)
  42. {
  43. instance_id_stack::copy_UU(context->instID, instID);
  44. }
  45. /* Returns the size of the hit */
  46. static __forceinline size_t size() { return 1; }
  47. public:
  48. Vec3<float> Ng; // geometry normal
  49. float u; // barycentric u coordinate of hit
  50. float v; // barycentric v coordinate of hit
  51. unsigned int primID; // primitive ID
  52. unsigned int geomID; // geometry ID
  53. unsigned int instID[RTC_MAX_INSTANCE_LEVEL_COUNT]; // instance ID
  54. };
  55. /* Shortcuts */
  56. typedef HitK<1> Hit;
  57. typedef HitK<4> Hit4;
  58. typedef HitK<8> Hit8;
  59. typedef HitK<16> Hit16;
  60. /* Outputs hit to stream */
  61. template<int K>
  62. __forceinline embree_ostream operator<<(embree_ostream cout, const HitK<K>& ray)
  63. {
  64. cout << "{ " << embree_endl
  65. << " Ng = " << ray.Ng << embree_endl
  66. << " u = " << ray.u << embree_endl
  67. << " v = " << ray.v << embree_endl
  68. << " primID = " << ray.primID << embree_endl
  69. << " geomID = " << ray.geomID << embree_endl
  70. << " instID =";
  71. for (unsigned l = 0; l < RTC_MAX_INSTANCE_LEVEL_COUNT; ++l)
  72. {
  73. cout << " " << ray.instID[l];
  74. }
  75. cout << embree_endl;
  76. return cout << "}";
  77. }
  78. template<typename Hit>
  79. __forceinline void copyHitToRay(RayHit& ray, const Hit& hit)
  80. {
  81. ray.Ng = hit.Ng;
  82. ray.u = hit.u;
  83. ray.v = hit.v;
  84. ray.primID = hit.primID;
  85. ray.geomID = hit.geomID;
  86. instance_id_stack::copy_UU(hit.instID, ray.instID);
  87. }
  88. template<int K>
  89. __forceinline void copyHitToRay(const vbool<K> &mask, RayHitK<K> &ray, const HitK<K> &hit)
  90. {
  91. vfloat<K>::storeu(mask,&ray.Ng.x, hit.Ng.x);
  92. vfloat<K>::storeu(mask,&ray.Ng.y, hit.Ng.y);
  93. vfloat<K>::storeu(mask,&ray.Ng.z, hit.Ng.z);
  94. vfloat<K>::storeu(mask,&ray.u, hit.u);
  95. vfloat<K>::storeu(mask,&ray.v, hit.v);
  96. vuint<K>::storeu(mask,&ray.primID, hit.primID);
  97. vuint<K>::storeu(mask,&ray.geomID, hit.geomID);
  98. instance_id_stack::copy_VV<K>(hit.instID, ray.instID, mask);
  99. }
  100. }