feature_adaptive_eval_simd.h 11 KB

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