subdivpatch1base_eval.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  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. #include "subdivpatch1base.h"
  17. namespace embree
  18. {
  19. namespace isa
  20. {
  21. Vec3fa patchEval(const SubdivPatch1Base& patch, const float uu, const float vv)
  22. {
  23. if (likely(patch.type == SubdivPatch1Base::BEZIER_PATCH))
  24. return ((BezierPatch3fa*)patch.patch_v)->eval(uu,vv);
  25. else if (likely(patch.type == SubdivPatch1Base::BSPLINE_PATCH))
  26. return ((BSplinePatch3fa*)patch.patch_v)->eval(uu,vv);
  27. else if (likely(patch.type == SubdivPatch1Base::GREGORY_PATCH))
  28. return ((DenseGregoryPatch3fa*)patch.patch_v)->eval(uu,vv);
  29. else if (likely(patch.type == SubdivPatch1Base::BILINEAR_PATCH))
  30. return ((BilinearPatch3fa*)patch.patch_v)->eval(uu,vv);
  31. return Vec3fa( zero );
  32. }
  33. Vec3fa patchNormal(const SubdivPatch1Base& patch, const float uu, const float vv)
  34. {
  35. if (likely(patch.type == SubdivPatch1Base::BEZIER_PATCH))
  36. return ((BezierPatch3fa*)patch.patch_v)->normal(uu,vv);
  37. else if (likely(patch.type == SubdivPatch1Base::BSPLINE_PATCH))
  38. return ((BSplinePatch3fa*)patch.patch_v)->normal(uu,vv);
  39. else if (likely(patch.type == SubdivPatch1Base::GREGORY_PATCH))
  40. return ((DenseGregoryPatch3fa*)patch.patch_v)->normal(uu,vv);
  41. else if (likely(patch.type == SubdivPatch1Base::BILINEAR_PATCH))
  42. return ((BilinearPatch3fa*)patch.patch_v)->normal(uu,vv);
  43. return Vec3fa( zero );
  44. }
  45. template<typename simdf>
  46. Vec3<simdf> patchEval(const SubdivPatch1Base& patch, const simdf& uu, const simdf& vv)
  47. {
  48. if (likely(patch.type == SubdivPatch1Base::BEZIER_PATCH))
  49. return ((BezierPatch3fa*)patch.patch_v)->eval(uu,vv);
  50. else if (likely(patch.type == SubdivPatch1Base::BSPLINE_PATCH))
  51. return ((BSplinePatch3fa*)patch.patch_v)->eval(uu,vv);
  52. else if (likely(patch.type == SubdivPatch1Base::GREGORY_PATCH))
  53. return ((DenseGregoryPatch3fa*)patch.patch_v)->eval(uu,vv);
  54. else if (likely(patch.type == SubdivPatch1Base::BILINEAR_PATCH))
  55. return ((BilinearPatch3fa*)patch.patch_v)->eval(uu,vv);
  56. return Vec3<simdf>( zero );
  57. }
  58. template<typename simdf>
  59. Vec3<simdf> patchNormal(const SubdivPatch1Base& patch, const simdf& uu, const simdf& vv)
  60. {
  61. if (likely(patch.type == SubdivPatch1Base::BEZIER_PATCH))
  62. return ((BezierPatch3fa*)patch.patch_v)->normal(uu,vv);
  63. else if (likely(patch.type == SubdivPatch1Base::BSPLINE_PATCH))
  64. return ((BSplinePatch3fa*)patch.patch_v)->normal(uu,vv);
  65. else if (likely(patch.type == SubdivPatch1Base::GREGORY_PATCH))
  66. return ((DenseGregoryPatch3fa*)patch.patch_v)->normal(uu,vv);
  67. else if (likely(patch.type == SubdivPatch1Base::BILINEAR_PATCH))
  68. return ((BilinearPatch3fa*)patch.patch_v)->normal(uu,vv);
  69. return Vec3<simdf>( zero );
  70. }
  71. /* eval grid over patch and stich edges when required */
  72. void evalGrid(const SubdivPatch1Base& patch,
  73. const unsigned x0, const unsigned x1,
  74. const unsigned y0, const unsigned y1,
  75. const unsigned swidth, const unsigned sheight,
  76. float *__restrict__ const grid_x,
  77. float *__restrict__ const grid_y,
  78. float *__restrict__ const grid_z,
  79. float *__restrict__ const grid_u,
  80. float *__restrict__ const grid_v,
  81. const SubdivMesh* const geom)
  82. {
  83. const unsigned dwidth = x1-x0+1;
  84. const unsigned dheight = y1-y0+1;
  85. const unsigned M = dwidth*dheight+VSIZEX;
  86. const unsigned grid_size_simd_blocks = (M-1)/VSIZEX;
  87. if (unlikely(patch.type == SubdivPatch1Base::EVAL_PATCH))
  88. {
  89. const bool displ = geom->displFunc || geom->displFunc2;
  90. const unsigned N = displ ? M : 0;
  91. dynamic_large_stack_array(float,grid_Ng_x,N,64*64*sizeof(float));
  92. dynamic_large_stack_array(float,grid_Ng_y,N,64*64*sizeof(float));
  93. dynamic_large_stack_array(float,grid_Ng_z,N,64*64*sizeof(float));
  94. if (geom->patch_eval_trees.size())
  95. {
  96. feature_adaptive_eval_grid<PatchEvalGrid>
  97. (geom->patch_eval_trees[geom->numTimeSteps*patch.prim+patch.time()], patch.subPatch(), patch.needsStitching() ? patch.level : nullptr,
  98. x0,x1,y0,y1,swidth,sheight,
  99. grid_x,grid_y,grid_z,grid_u,grid_v,
  100. displ ? (float*)grid_Ng_x : nullptr, displ ? (float*)grid_Ng_y : nullptr, displ ? (float*)grid_Ng_z : nullptr,
  101. dwidth,dheight);
  102. }
  103. else
  104. {
  105. GeneralCatmullClarkPatch3fa ccpatch(patch.edge(),geom->getVertexBuffer(patch.time()));
  106. feature_adaptive_eval_grid<FeatureAdaptiveEvalGrid,GeneralCatmullClarkPatch3fa>
  107. (ccpatch, patch.subPatch(), patch.needsStitching() ? patch.level : nullptr,
  108. x0,x1,y0,y1,swidth,sheight,
  109. grid_x,grid_y,grid_z,grid_u,grid_v,
  110. displ ? (float*)grid_Ng_x : nullptr, displ ? (float*)grid_Ng_y : nullptr, displ ? (float*)grid_Ng_z : nullptr,
  111. dwidth,dheight);
  112. }
  113. /* convert sub-patch UVs to patch UVs*/
  114. const Vec2f uv0 = patch.getUV(0);
  115. const Vec2f uv1 = patch.getUV(1);
  116. const Vec2f uv2 = patch.getUV(2);
  117. const Vec2f uv3 = patch.getUV(3);
  118. for (unsigned i=0; i<grid_size_simd_blocks; i++)
  119. {
  120. const vfloatx u = vfloatx::load(&grid_u[i*VSIZEX]);
  121. const vfloatx v = vfloatx::load(&grid_v[i*VSIZEX]);
  122. const vfloatx patch_u = lerp2(uv0.x,uv1.x,uv3.x,uv2.x,u,v);
  123. const vfloatx patch_v = lerp2(uv0.y,uv1.y,uv3.y,uv2.y,u,v);
  124. vfloatx::store(&grid_u[i*VSIZEX],patch_u);
  125. vfloatx::store(&grid_v[i*VSIZEX],patch_v);
  126. }
  127. /* call displacement shader */
  128. if (unlikely(geom->displFunc))
  129. geom->displFunc(geom->userPtr,patch.geom,patch.prim,grid_u,grid_v,grid_Ng_x,grid_Ng_y,grid_Ng_z,grid_x,grid_y,grid_z,dwidth*dheight);
  130. else if (unlikely(geom->displFunc2))
  131. geom->displFunc2(geom->userPtr,patch.geom,patch.prim,patch.time(),grid_u,grid_v,grid_Ng_x,grid_Ng_y,grid_Ng_z,grid_x,grid_y,grid_z,dwidth*dheight);
  132. /* set last elements in u,v array to 1.0f */
  133. const float last_u = grid_u[dwidth*dheight-1];
  134. const float last_v = grid_v[dwidth*dheight-1];
  135. const float last_x = grid_x[dwidth*dheight-1];
  136. const float last_y = grid_y[dwidth*dheight-1];
  137. const float last_z = grid_z[dwidth*dheight-1];
  138. for (unsigned i=dwidth*dheight;i<grid_size_simd_blocks*VSIZEX;i++)
  139. {
  140. grid_u[i] = last_u;
  141. grid_v[i] = last_v;
  142. grid_x[i] = last_x;
  143. grid_y[i] = last_y;
  144. grid_z[i] = last_z;
  145. }
  146. }
  147. else
  148. {
  149. /* grid_u, grid_v need to be padded as we write with SIMD granularity */
  150. gridUVTessellator(patch.level,swidth,sheight,x0,y0,dwidth,dheight,grid_u,grid_v);
  151. /* set last elements in u,v array to last valid point */
  152. const float last_u = grid_u[dwidth*dheight-1];
  153. const float last_v = grid_v[dwidth*dheight-1];
  154. for (unsigned i=dwidth*dheight;i<grid_size_simd_blocks*VSIZEX;i++) {
  155. grid_u[i] = last_u;
  156. grid_v[i] = last_v;
  157. }
  158. /* stitch edges if necessary */
  159. if (unlikely(patch.needsStitching()))
  160. stitchUVGrid(patch.level,swidth,sheight,x0,y0,dwidth,dheight,grid_u,grid_v);
  161. /* iterates over all grid points */
  162. for (unsigned i=0; i<grid_size_simd_blocks; i++)
  163. {
  164. const vfloatx u = vfloatx::load(&grid_u[i*VSIZEX]);
  165. const vfloatx v = vfloatx::load(&grid_v[i*VSIZEX]);
  166. Vec3<vfloatx> vtx = patchEval(patch,u,v);
  167. /* evaluate displacement function */
  168. if (unlikely(geom->displFunc != nullptr))
  169. {
  170. const Vec3<vfloatx> normal = normalize_safe(patchNormal(patch, u, v));
  171. geom->displFunc(geom->userPtr,patch.geom,patch.prim,
  172. &u[0],&v[0],&normal.x[0],&normal.y[0],&normal.z[0],
  173. &vtx.x[0],&vtx.y[0],&vtx.z[0],VSIZEX);
  174. }
  175. else if (unlikely(geom->displFunc2 != nullptr))
  176. {
  177. const Vec3<vfloatx> normal = normalize_safe(patchNormal(patch, u, v));
  178. geom->displFunc2(geom->userPtr,patch.geom,patch.prim,patch.time(),
  179. &u[0],&v[0],&normal.x[0],&normal.y[0],&normal.z[0],
  180. &vtx.x[0],&vtx.y[0],&vtx.z[0],VSIZEX);
  181. }
  182. vfloatx::store(&grid_x[i*VSIZEX],vtx.x);
  183. vfloatx::store(&grid_y[i*VSIZEX],vtx.y);
  184. vfloatx::store(&grid_z[i*VSIZEX],vtx.z);
  185. }
  186. }
  187. }
  188. /* eval grid over patch and stich edges when required */
  189. BBox3fa evalGridBounds(const SubdivPatch1Base& patch,
  190. const unsigned x0, const unsigned x1,
  191. const unsigned y0, const unsigned y1,
  192. const unsigned swidth, const unsigned sheight,
  193. const SubdivMesh* const geom)
  194. {
  195. BBox3fa b(empty);
  196. const unsigned dwidth = x1-x0+1;
  197. const unsigned dheight = y1-y0+1;
  198. const unsigned M = dwidth*dheight+VSIZEX;
  199. const unsigned grid_size_simd_blocks = (M-1)/VSIZEX;
  200. dynamic_large_stack_array(float,grid_u,M,64*64*sizeof(float));
  201. dynamic_large_stack_array(float,grid_v,M,64*64*sizeof(float));
  202. if (unlikely(patch.type == SubdivPatch1Base::EVAL_PATCH))
  203. {
  204. const bool displ = geom->displFunc || geom->displFunc2;
  205. dynamic_large_stack_array(float,grid_x,M,64*64*sizeof(float));
  206. dynamic_large_stack_array(float,grid_y,M,64*64*sizeof(float));
  207. dynamic_large_stack_array(float,grid_z,M,64*64*sizeof(float));
  208. dynamic_large_stack_array(float,grid_Ng_x,displ ? M : 0,64*64*sizeof(float));
  209. dynamic_large_stack_array(float,grid_Ng_y,displ ? M : 0,64*64*sizeof(float));
  210. dynamic_large_stack_array(float,grid_Ng_z,displ ? M : 0,64*64*sizeof(float));
  211. if (geom->patch_eval_trees.size())
  212. {
  213. feature_adaptive_eval_grid<PatchEvalGrid>
  214. (geom->patch_eval_trees[geom->numTimeSteps*patch.prim+patch.time()], patch.subPatch(), patch.needsStitching() ? patch.level : nullptr,
  215. x0,x1,y0,y1,swidth,sheight,
  216. grid_x,grid_y,grid_z,grid_u,grid_v,
  217. displ ? (float*)grid_Ng_x : nullptr, displ ? (float*)grid_Ng_y : nullptr, displ ? (float*)grid_Ng_z : nullptr,
  218. dwidth,dheight);
  219. }
  220. else
  221. {
  222. GeneralCatmullClarkPatch3fa ccpatch(patch.edge(),geom->getVertexBuffer(patch.time()));
  223. feature_adaptive_eval_grid <FeatureAdaptiveEvalGrid,GeneralCatmullClarkPatch3fa>
  224. (ccpatch, patch.subPatch(), patch.needsStitching() ? patch.level : nullptr,
  225. x0,x1,y0,y1,swidth,sheight,
  226. grid_x,grid_y,grid_z,grid_u,grid_v,
  227. displ ? (float*)grid_Ng_x : nullptr, displ ? (float*)grid_Ng_y : nullptr, displ ? (float*)grid_Ng_z : nullptr,
  228. dwidth,dheight);
  229. }
  230. /* call displacement shader */
  231. if (unlikely(geom->displFunc))
  232. geom->displFunc(geom->userPtr,patch.geom,patch.prim,grid_u,grid_v,grid_Ng_x,grid_Ng_y,grid_Ng_z,grid_x,grid_y,grid_z,dwidth*dheight);
  233. else if (unlikely(geom->displFunc2))
  234. geom->displFunc2(geom->userPtr,patch.geom,patch.prim,patch.time(),grid_u,grid_v,grid_Ng_x,grid_Ng_y,grid_Ng_z,grid_x,grid_y,grid_z,dwidth*dheight);
  235. /* set last elements in u,v array to 1.0f */
  236. const float last_u = grid_u[dwidth*dheight-1];
  237. const float last_v = grid_v[dwidth*dheight-1];
  238. const float last_x = grid_x[dwidth*dheight-1];
  239. const float last_y = grid_y[dwidth*dheight-1];
  240. const float last_z = grid_z[dwidth*dheight-1];
  241. for (unsigned i=dwidth*dheight;i<grid_size_simd_blocks*VSIZEX;i++)
  242. {
  243. grid_u[i] = last_u;
  244. grid_v[i] = last_v;
  245. grid_x[i] = last_x;
  246. grid_y[i] = last_y;
  247. grid_z[i] = last_z;
  248. }
  249. vfloatx bounds_min_x = pos_inf;
  250. vfloatx bounds_min_y = pos_inf;
  251. vfloatx bounds_min_z = pos_inf;
  252. vfloatx bounds_max_x = neg_inf;
  253. vfloatx bounds_max_y = neg_inf;
  254. vfloatx bounds_max_z = neg_inf;
  255. for (unsigned i = 0; i<grid_size_simd_blocks; i++)
  256. {
  257. vfloatx x = vfloatx::loadu(&grid_x[i * VSIZEX]);
  258. vfloatx y = vfloatx::loadu(&grid_y[i * VSIZEX]);
  259. vfloatx z = vfloatx::loadu(&grid_z[i * VSIZEX]);
  260. bounds_min_x = min(bounds_min_x,x);
  261. bounds_min_y = min(bounds_min_y,y);
  262. bounds_min_z = min(bounds_min_z,z);
  263. bounds_max_x = max(bounds_max_x,x);
  264. bounds_max_y = max(bounds_max_y,y);
  265. bounds_max_z = max(bounds_max_z,z);
  266. }
  267. b.lower.x = reduce_min(bounds_min_x);
  268. b.lower.y = reduce_min(bounds_min_y);
  269. b.lower.z = reduce_min(bounds_min_z);
  270. b.upper.x = reduce_max(bounds_max_x);
  271. b.upper.y = reduce_max(bounds_max_y);
  272. b.upper.z = reduce_max(bounds_max_z);
  273. b.lower.a = 0;
  274. b.upper.a = 0;
  275. }
  276. else
  277. {
  278. /* grid_u, grid_v need to be padded as we write with SIMD granularity */
  279. gridUVTessellator(patch.level,swidth,sheight,x0,y0,dwidth,dheight,grid_u,grid_v);
  280. /* set last elements in u,v array to last valid point */
  281. const float last_u = grid_u[dwidth*dheight-1];
  282. const float last_v = grid_v[dwidth*dheight-1];
  283. for (unsigned i=dwidth*dheight;i<grid_size_simd_blocks*VSIZEX;i++) {
  284. grid_u[i] = last_u;
  285. grid_v[i] = last_v;
  286. }
  287. /* stitch edges if necessary */
  288. if (unlikely(patch.needsStitching()))
  289. stitchUVGrid(patch.level,swidth,sheight,x0,y0,dwidth,dheight,grid_u,grid_v);
  290. /* iterates over all grid points */
  291. Vec3<vfloatx> bounds_min;
  292. bounds_min[0] = pos_inf;
  293. bounds_min[1] = pos_inf;
  294. bounds_min[2] = pos_inf;
  295. Vec3<vfloatx> bounds_max;
  296. bounds_max[0] = neg_inf;
  297. bounds_max[1] = neg_inf;
  298. bounds_max[2] = neg_inf;
  299. for (unsigned i=0; i<grid_size_simd_blocks; i++)
  300. {
  301. const vfloatx u = vfloatx::load(&grid_u[i*VSIZEX]);
  302. const vfloatx v = vfloatx::load(&grid_v[i*VSIZEX]);
  303. Vec3<vfloatx> vtx = patchEval(patch,u,v);
  304. /* evaluate displacement function */
  305. if (unlikely(geom->displFunc != nullptr))
  306. {
  307. const Vec3<vfloatx> normal = normalize_safe(patchNormal(patch,u,v));
  308. geom->displFunc(geom->userPtr,patch.geom,patch.prim,
  309. &u[0],&v[0],&normal.x[0],&normal.y[0],&normal.z[0],
  310. &vtx.x[0],&vtx.y[0],&vtx.z[0],VSIZEX);
  311. }
  312. else if (unlikely(geom->displFunc2 != nullptr))
  313. {
  314. const Vec3<vfloatx> normal = normalize_safe(patchNormal(patch,u,v));
  315. geom->displFunc2(geom->userPtr,patch.geom,patch.prim,patch.time(),
  316. &u[0],&v[0],&normal.x[0],&normal.y[0],&normal.z[0],
  317. &vtx.x[0],&vtx.y[0],&vtx.z[0],VSIZEX);
  318. }
  319. bounds_min[0] = min(bounds_min[0],vtx.x);
  320. bounds_max[0] = max(bounds_max[0],vtx.x);
  321. bounds_min[1] = min(bounds_min[1],vtx.y);
  322. bounds_max[1] = max(bounds_max[1],vtx.y);
  323. bounds_min[2] = min(bounds_min[2],vtx.z);
  324. bounds_max[2] = max(bounds_max[2],vtx.z);
  325. }
  326. b.lower.x = reduce_min(bounds_min[0]);
  327. b.lower.y = reduce_min(bounds_min[1]);
  328. b.lower.z = reduce_min(bounds_min[2]);
  329. b.upper.x = reduce_max(bounds_max[0]);
  330. b.upper.y = reduce_max(bounds_max[1]);
  331. b.upper.z = reduce_max(bounds_max[2]);
  332. b.lower.a = 0;
  333. b.upper.a = 0;
  334. }
  335. assert( std::isfinite(b.lower.x) );
  336. assert( std::isfinite(b.lower.y) );
  337. assert( std::isfinite(b.lower.z) );
  338. assert( std::isfinite(b.upper.x) );
  339. assert( std::isfinite(b.upper.y) );
  340. assert( std::isfinite(b.upper.z) );
  341. assert(b.lower.x <= b.upper.x);
  342. assert(b.lower.y <= b.upper.y);
  343. assert(b.lower.z <= b.upper.z);
  344. return b;
  345. }
  346. }
  347. }