feature_adaptive_eval_simd.h 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. // Copyright 2009-2021 Intel Corporation
  2. // SPDX-License-Identifier: Apache-2.0
  3. #pragma once
  4. #include "patch.h"
  5. namespace embree
  6. {
  7. namespace isa
  8. {
  9. template<typename vbool, typename vint, typename vfloat, typename Vertex, typename Vertex_t = Vertex>
  10. struct FeatureAdaptiveEvalSimd
  11. {
  12. public:
  13. typedef PatchT<Vertex,Vertex_t> Patch;
  14. typedef typename Patch::Ref Ref;
  15. typedef GeneralCatmullClarkPatchT<Vertex,Vertex_t> GeneralCatmullClarkPatch;
  16. typedef CatmullClark1RingT<Vertex,Vertex_t> CatmullClarkRing;
  17. typedef CatmullClarkPatchT<Vertex,Vertex_t> CatmullClarkPatch;
  18. typedef BSplinePatchT<Vertex,Vertex_t> BSplinePatch;
  19. typedef BezierPatchT<Vertex,Vertex_t> BezierPatch;
  20. typedef GregoryPatchT<Vertex,Vertex_t> GregoryPatch;
  21. typedef BilinearPatchT<Vertex,Vertex_t> BilinearPatch;
  22. typedef BezierCurveT<Vertex> BezierCurve;
  23. FeatureAdaptiveEvalSimd (const HalfEdge* edge, const char* vertices, size_t stride, const vbool& valid, const vfloat& u, const vfloat& v,
  24. float* P, float* dPdu, float* dPdv, float* ddPdudu, float* ddPdvdv, float* ddPdudv, const size_t dstride, const size_t N)
  25. : P(P), dPdu(dPdu), dPdv(dPdv), ddPdudu(ddPdudu), ddPdvdv(ddPdvdv), ddPdudv(ddPdudv), dstride(dstride), N(N)
  26. {
  27. switch (edge->patch_type) {
  28. case HalfEdge::BILINEAR_PATCH: BilinearPatch(edge,vertices,stride).eval(valid,u,v,P,dPdu,dPdv,ddPdudu,ddPdvdv,ddPdudv,1.0f,dstride,N); break;
  29. case HalfEdge::REGULAR_QUAD_PATCH: RegularPatchT(edge,vertices,stride).eval(valid,u,v,P,dPdu,dPdv,ddPdudu,ddPdvdv,ddPdudv,1.0f,dstride,N); break;
  30. #if PATCH_USE_GREGORY == 2
  31. case HalfEdge::IRREGULAR_QUAD_PATCH: GregoryPatchT<Vertex,Vertex_t>(edge,vertices,stride).eval(valid,u,v,P,dPdu,dPdv,ddPdudu,ddPdvdv,ddPdudv,1.0f,dstride,N); break;
  32. #endif
  33. default: {
  34. GeneralCatmullClarkPatch patch(edge,vertices,stride);
  35. eval_direct(valid,patch,Vec2<vfloat>(u,v),0);
  36. break;
  37. }
  38. }
  39. }
  40. FeatureAdaptiveEvalSimd (const CatmullClarkPatch& patch, const vbool& valid, const vfloat& u, const vfloat& v, float dscale, size_t depth,
  41. float* P, float* dPdu, float* dPdv, float* ddPdudu, float* ddPdvdv, float* ddPdudv, const size_t dstride, const size_t N)
  42. : P(P), dPdu(dPdu), dPdv(dPdv), ddPdudu(ddPdudu), ddPdvdv(ddPdvdv), ddPdudv(ddPdudv), dstride(dstride), N(N)
  43. {
  44. eval_direct(valid,patch,Vec2<vfloat>(u,v),dscale,depth);
  45. }
  46. template<size_t N>
  47. __forceinline void eval_quad_direct(const vbool& valid, array_t<CatmullClarkPatch,N>& patches, const Vec2<vfloat>& uv, float dscale, size_t depth)
  48. {
  49. const vfloat u = uv.x, v = uv.y;
  50. const vbool u0_mask = u < 0.5f, u1_mask = u >= 0.5f;
  51. const vbool v0_mask = v < 0.5f, v1_mask = v >= 0.5f;
  52. const vbool u0v0_mask = valid & u0_mask & v0_mask;
  53. const vbool u0v1_mask = valid & u0_mask & v1_mask;
  54. const vbool u1v0_mask = valid & u1_mask & v0_mask;
  55. const vbool u1v1_mask = valid & u1_mask & v1_mask;
  56. if (any(u0v0_mask)) eval_direct(u0v0_mask,patches[0],Vec2<vfloat>(2.0f*u,2.0f*v),2.0f*dscale,depth+1);
  57. if (any(u1v0_mask)) eval_direct(u1v0_mask,patches[1],Vec2<vfloat>(2.0f*u-1.0f,2.0f*v),2.0f*dscale,depth+1);
  58. if (any(u1v1_mask)) eval_direct(u1v1_mask,patches[2],Vec2<vfloat>(2.0f*u-1.0f,2.0f*v-1.0f),2.0f*dscale,depth+1);
  59. if (any(u0v1_mask)) eval_direct(u0v1_mask,patches[3],Vec2<vfloat>(2.0f*u,2.0f*v-1.0f),2.0f*dscale,depth+1);
  60. }
  61. template<size_t N>
  62. __forceinline void eval_general_quad_direct(const vbool& valid, const GeneralCatmullClarkPatch& patch, array_t<CatmullClarkPatch,N>& patches, const Vec2<vfloat>& uv, float dscale, size_t depth)
  63. {
  64. #if PATCH_USE_GREGORY == 2
  65. BezierCurve borders[GeneralCatmullClarkPatch::SIZE]; patch.getLimitBorder(borders);
  66. BezierCurve border0l,border0r; borders[0].subdivide(border0l,border0r);
  67. BezierCurve border1l,border1r; borders[1].subdivide(border1l,border1r);
  68. BezierCurve border2l,border2r; borders[2].subdivide(border2l,border2r);
  69. BezierCurve border3l,border3r; borders[3].subdivide(border3l,border3r);
  70. #endif
  71. GeneralCatmullClarkPatch::fix_quad_ring_order(patches);
  72. const vfloat u = uv.x, v = uv.y;
  73. const vbool u0_mask = u < 0.5f, u1_mask = u >= 0.5f;
  74. const vbool v0_mask = v < 0.5f, v1_mask = v >= 0.5f;
  75. const vbool u0v0_mask = valid & u0_mask & v0_mask;
  76. const vbool u0v1_mask = valid & u0_mask & v1_mask;
  77. const vbool u1v0_mask = valid & u1_mask & v0_mask;
  78. const vbool u1v1_mask = valid & u1_mask & v1_mask;
  79. #if PATCH_USE_GREGORY == 2
  80. if (any(u0v0_mask)) eval_direct(u0v0_mask,patches[0],Vec2<vfloat>(2.0f*u,2.0f*v),2.0f*dscale,depth+1,&border0l,nullptr,nullptr,&border3r);
  81. if (any(u1v0_mask)) eval_direct(u1v0_mask,patches[1],Vec2<vfloat>(2.0f*u-1.0f,2.0f*v),2.0f*dscale,depth+1,&border0r,&border1l,nullptr,nullptr);
  82. if (any(u1v1_mask)) eval_direct(u1v1_mask,patches[2],Vec2<vfloat>(2.0f*u-1.0f,2.0f*v-1.0f),2.0f*dscale,depth+1,nullptr,&border1r,&border2l,nullptr);
  83. if (any(u0v1_mask)) eval_direct(u0v1_mask,patches[3],Vec2<vfloat>(2.0f*u,2.0f*v-1.0f),2.0f*dscale,depth+1,nullptr,nullptr,&border2r,&border3l);
  84. #else
  85. if (any(u0v0_mask)) eval_direct(u0v0_mask,patches[0],Vec2<vfloat>(2.0f*u,2.0f*v),2.0f*dscale,depth+1);
  86. if (any(u1v0_mask)) eval_direct(u1v0_mask,patches[1],Vec2<vfloat>(2.0f*u-1.0f,2.0f*v),2.0f*dscale,depth+1);
  87. if (any(u1v1_mask)) eval_direct(u1v1_mask,patches[2],Vec2<vfloat>(2.0f*u-1.0f,2.0f*v-1.0f),2.0f*dscale,depth+1);
  88. if (any(u0v1_mask)) eval_direct(u0v1_mask,patches[3],Vec2<vfloat>(2.0f*u,2.0f*v-1.0f),2.0f*dscale,depth+1);
  89. #endif
  90. }
  91. __forceinline bool final(const CatmullClarkPatch& patch, const typename CatmullClarkRing::Type type, size_t depth)
  92. {
  93. const size_t max_eval_depth = (type & CatmullClarkRing::TYPE_CREASES) ? PATCH_MAX_EVAL_DEPTH_CREASE : PATCH_MAX_EVAL_DEPTH_IRREGULAR;
  94. //#if PATCH_MIN_RESOLUTION
  95. // return patch.isFinalResolution(PATCH_MIN_RESOLUTION) || depth>=max_eval_depth;
  96. //#else
  97. return depth>=max_eval_depth;
  98. //#endif
  99. }
  100. void eval_direct(const vbool& valid, const CatmullClarkPatch& patch, const Vec2<vfloat>& uv, float dscale, size_t depth,
  101. BezierCurve* border0 = nullptr, BezierCurve* border1 = nullptr, BezierCurve* border2 = nullptr, BezierCurve* border3 = nullptr)
  102. {
  103. typename CatmullClarkPatch::Type ty = patch.type();
  104. if (unlikely(final(patch,ty,depth)))
  105. {
  106. if (ty & CatmullClarkRing::TYPE_REGULAR) {
  107. RegularPatch(patch,border0,border1,border2,border3).eval(valid,uv.x,uv.y,P,dPdu,dPdv,ddPdudu,ddPdvdv,ddPdudv,dscale,dstride,N);
  108. } else {
  109. IrregularFillPatch(patch,border0,border1,border2,border3).eval(valid,uv.x,uv.y,P,dPdu,dPdv,ddPdudu,ddPdvdv,ddPdudv,dscale,dstride,N);
  110. }
  111. }
  112. else if (ty & CatmullClarkRing::TYPE_REGULAR_CREASES) {
  113. assert(depth > 0); RegularPatch(patch,border0,border1,border2,border3).eval(valid,uv.x,uv.y,P,dPdu,dPdv,ddPdudu,ddPdvdv,ddPdudv,dscale,dstride,N);
  114. }
  115. #if PATCH_USE_GREGORY == 2
  116. else if (ty & CatmullClarkRing::TYPE_GREGORY_CREASES) {
  117. assert(depth > 0); GregoryPatch(patch,border0,border1,border2,border3).eval(valid,uv.x,uv.y,P,dPdu,dPdv,ddPdudu,ddPdvdv,ddPdudv,dscale,dstride,N);
  118. }
  119. #endif
  120. else
  121. {
  122. array_t<CatmullClarkPatch,4> patches;
  123. patch.subdivide(patches); // FIXME: only have to generate one of the patches
  124. eval_quad_direct(valid,patches,uv,dscale,depth);
  125. }
  126. }
  127. void eval_direct(const vbool& valid, const GeneralCatmullClarkPatch& patch, const Vec2<vfloat>& uv, const size_t depth)
  128. {
  129. /* convert into standard quad patch if possible */
  130. if (likely(patch.isQuadPatch())) {
  131. CatmullClarkPatch qpatch; patch.init(qpatch);
  132. return eval_direct(valid,qpatch,uv,1.0f,depth);
  133. }
  134. /* subdivide patch */
  135. unsigned Nc;
  136. array_t<CatmullClarkPatch,GeneralCatmullClarkPatch::SIZE> patches;
  137. patch.subdivide(patches,Nc); // FIXME: only have to generate one of the patches
  138. /* parametrization for quads */
  139. if (Nc == 4)
  140. eval_general_quad_direct(valid,patch,patches,uv,1.0f,depth);
  141. /* parametrization for arbitrary polygons */
  142. else
  143. {
  144. const vint l = (vint)floor(0.5f*uv.x); const vfloat u = 2.0f*frac(0.5f*uv.x)-0.5f;
  145. const vint h = (vint)floor(0.5f*uv.y); const vfloat v = 2.0f*frac(0.5f*uv.y)-0.5f;
  146. const vint i = (h<<2)+l; assert(all(valid,i<Nc));
  147. foreach_unique(valid,i,[&](const vbool& valid, const int i) {
  148. #if PATCH_USE_GREGORY == 2
  149. BezierCurve borders[2]; patch.getLimitBorder(borders,i);
  150. BezierCurve border0l,border0r; borders[0].subdivide(border0l,border0r);
  151. BezierCurve border2l,border2r; borders[1].subdivide(border2l,border2r);
  152. eval_direct(valid,patches[i],Vec2<vfloat>(u,v),1.0f,depth+1, &border0l, nullptr, nullptr, &border2r);
  153. #else
  154. eval_direct(valid,patches[i],Vec2<vfloat>(u,v),1.0f,depth+1);
  155. #endif
  156. });
  157. }
  158. }
  159. private:
  160. float* const P;
  161. float* const dPdu;
  162. float* const dPdv;
  163. float* const ddPdudu;
  164. float* const ddPdvdv;
  165. float* const ddPdudv;
  166. const size_t dstride;
  167. const size_t N;
  168. };
  169. }
  170. }