curve_intersector_distance.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. // Copyright 2009-2021 Intel Corporation
  2. // SPDX-License-Identifier: Apache-2.0
  3. #pragma once
  4. #include "../common/ray.h"
  5. #include "curve_intersector_precalculations.h"
  6. namespace embree
  7. {
  8. namespace isa
  9. {
  10. template<typename NativeCurve3fa, int M>
  11. struct DistanceCurveHit
  12. {
  13. __forceinline DistanceCurveHit() {}
  14. __forceinline DistanceCurveHit(const vbool<M>& valid, const vfloat<M>& U, const vfloat<M>& V, const vfloat<M>& T, const int i, const int N,
  15. const NativeCurve3fa& curve3D)
  16. : U(U), V(V), T(T), i(i), N(N), curve3D(curve3D), valid(valid) {}
  17. __forceinline void finalize()
  18. {
  19. vu = (vfloat<M>(step)+U+vfloat<M>(float(i)))*(1.0f/float(N));
  20. vv = V;
  21. vt = T;
  22. }
  23. __forceinline Vec2f uv (const size_t i) const { return Vec2f(vu[i],vv[i]); }
  24. __forceinline float t (const size_t i) const { return vt[i]; }
  25. __forceinline Vec3fa Ng(const size_t i) const {
  26. return curve3D.eval_du(vu[i]);
  27. }
  28. public:
  29. vfloat<M> U;
  30. vfloat<M> V;
  31. vfloat<M> T;
  32. int i, N;
  33. NativeCurve3fa curve3D;
  34. public:
  35. vbool<M> valid;
  36. vfloat<M> vu;
  37. vfloat<M> vv;
  38. vfloat<M> vt;
  39. };
  40. template<typename NativeCurve3fa>
  41. struct DistanceCurveHit<NativeCurve3fa,1>
  42. {
  43. enum { M = 1 };
  44. __forceinline DistanceCurveHit() {}
  45. __forceinline DistanceCurveHit(const vbool<M>& valid, const vfloat<M>& U, const vfloat<M>& V, const vfloat<M>& T, const int i, const int N,
  46. const NativeCurve3fa& curve3D)
  47. : U(U), V(V), T(T), i(i), N(N), curve3D(curve3D), valid(valid) {}
  48. __forceinline void finalize()
  49. {
  50. vu = (vfloat<M>(step)+U+vfloat<M>(float(i)))*(1.0f/float(N));
  51. vv = V;
  52. vt = T;
  53. }
  54. __forceinline Vec2f uv () const { return Vec2f(vu,vv); }
  55. __forceinline float t () const { return vt; }
  56. __forceinline Vec3fa Ng() const { return curve3D.eval_du(vu); }
  57. public:
  58. vfloat<M> U;
  59. vfloat<M> V;
  60. vfloat<M> T;
  61. int i, N;
  62. NativeCurve3fa curve3D;
  63. public:
  64. vbool<M> valid;
  65. vfloat<M> vu;
  66. vfloat<M> vv;
  67. vfloat<M> vt;
  68. };
  69. template<typename NativeCurve3fa, int W = VSIZEX>
  70. struct DistanceCurve1Intersector1
  71. {
  72. using vboolx = vbool<W>;
  73. using vintx = vint<W>;
  74. using vfloatx = vfloat<W>;
  75. using Vec4vfx = Vec4vf<W>;
  76. template<typename Epilog>
  77. __forceinline bool intersect(const CurvePrecalculations1& pre, Ray& ray,
  78. RayQueryContext* context,
  79. const CurveGeometry* geom, const unsigned int primID,
  80. const Vec3ff& v0, const Vec3ff& v1, const Vec3ff& v2, const Vec3ff& v3,
  81. const Epilog& epilog)
  82. {
  83. const int N = geom->tessellationRate;
  84. /* transform control points into ray space */
  85. const NativeCurve3fa curve3Di(v0,v1,v2,v3);
  86. const NativeCurve3fa curve3D = enlargeRadiusToMinWidth(context,geom,ray.org,curve3Di);
  87. const NativeCurve3fa curve2D = curve3D.xfm_pr(pre.ray_space,ray.org);
  88. /* evaluate the bezier curve */
  89. vboolx valid = vfloatx(step) < vfloatx(float(N));
  90. const Vec4vfx p0 = curve2D.template eval0<W>(0,N);
  91. const Vec4vfx p1 = curve2D.template eval1<W>(0,N);
  92. /* approximative intersection with cone */
  93. const Vec4vfx v = p1-p0;
  94. const Vec4vfx w = -p0;
  95. const vfloatx d0 = madd(w.x,v.x,w.y*v.y);
  96. const vfloatx d1 = madd(v.x,v.x,v.y*v.y);
  97. const vfloatx u = clamp(d0*rcp(d1),vfloatx(zero),vfloatx(one));
  98. const Vec4vfx p = madd(u,v,p0);
  99. const vfloatx t = p.z*pre.depth_scale;
  100. const vfloatx d2 = madd(p.x,p.x,p.y*p.y);
  101. const vfloatx r = p.w;
  102. const vfloatx r2 = r*r;
  103. valid &= (d2 <= r2) & (vfloatx(ray.tnear()) <= t) & (t <= vfloatx(ray.tfar));
  104. if (EMBREE_CURVE_SELF_INTERSECTION_AVOIDANCE_FACTOR != 0.0f)
  105. valid &= t > float(EMBREE_CURVE_SELF_INTERSECTION_AVOIDANCE_FACTOR)*r*pre.depth_scale; // ignore self intersections
  106. /* update hit information */
  107. bool ishit = false;
  108. if (unlikely(any(valid))) {
  109. DistanceCurveHit<NativeCurve3fa,W> hit(valid,u,0.0f,t,0,N,curve3D);
  110. ishit = ishit | epilog(valid,hit);
  111. }
  112. if (unlikely(W < N))
  113. {
  114. /* process SIMD-size many segments per iteration */
  115. for (int i=W; i<N; i+=W)
  116. {
  117. /* evaluate the bezier curve */
  118. vboolx valid = vintx(i)+vintx(step) < vintx(N);
  119. const Vec4vfx p0 = curve2D.template eval0<W>(i,N);
  120. const Vec4vfx p1 = curve2D.template eval1<W>(i,N);
  121. /* approximative intersection with cone */
  122. const Vec4vfx v = p1-p0;
  123. const Vec4vfx w = -p0;
  124. const vfloatx d0 = madd(w.x,v.x,w.y*v.y);
  125. const vfloatx d1 = madd(v.x,v.x,v.y*v.y);
  126. const vfloatx u = clamp(d0*rcp(d1),vfloatx(zero),vfloatx(one));
  127. const Vec4vfx p = madd(u,v,p0);
  128. const vfloatx t = p.z*pre.depth_scale;
  129. const vfloatx d2 = madd(p.x,p.x,p.y*p.y);
  130. const vfloatx r = p.w;
  131. const vfloatx r2 = r*r;
  132. valid &= (d2 <= r2) & (vfloatx(ray.tnear()) <= t) & (t <= vfloatx(ray.tfar));
  133. if (EMBREE_CURVE_SELF_INTERSECTION_AVOIDANCE_FACTOR != 0.0f)
  134. valid &= t > float(EMBREE_CURVE_SELF_INTERSECTION_AVOIDANCE_FACTOR)*r*pre.depth_scale; // ignore self intersections
  135. /* update hit information */
  136. if (unlikely(any(valid))) {
  137. DistanceCurveHit<NativeCurve3fa,W> hit(valid,u,0.0f,t,i,N,curve3D);
  138. ishit = ishit | epilog(valid,hit);
  139. }
  140. }
  141. }
  142. return ishit;
  143. }
  144. };
  145. }
  146. }