patch.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  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 "catmullclark_patch.h"
  18. #include "bilinear_patch.h"
  19. #include "bspline_patch.h"
  20. #include "bezier_patch.h"
  21. #include "gregory_patch.h"
  22. #include "tessellation_cache.h"
  23. #if 1
  24. #define PATCH_DEBUG_SUBDIVISION(ptr,x,y,z)
  25. #else
  26. #define PATCH_DEBUG_SUBDIVISION(ptr,x,y,z) \
  27. { \
  28. size_t hex = (size_t)ptr; \
  29. for (size_t i=0; i<4; i++) hex = hex ^ (hex >> 8); \
  30. const float c = (float)(((hex >> 0) ^ (hex >> 4) ^ (hex >> 8) ^ (hex >> 12) ^ (hex >> 16))&0xf)/15.0f; \
  31. if (P) *P = Vertex(0.5f+0.5f*x,0.5f+0.5f*y,0.5f+0.5f*z,0.0f); \
  32. }
  33. #endif
  34. #define PATCH_MAX_CACHE_DEPTH 2
  35. //#define PATCH_MIN_RESOLUTION 1 // FIXME: not yet completely implemented
  36. #define PATCH_MAX_EVAL_DEPTH_IRREGULAR 2 // maximal evaluation depth at irregular vertices (has to be larger or equal than PATCH_MAX_CACHE_DEPTH)
  37. #define PATCH_MAX_EVAL_DEPTH_CREASE 10 // maximal evaluation depth at crease features (has to be larger or equal than PATCH_MAX_CACHE_DEPTH)
  38. #define PATCH_USE_GREGORY 1 // 0 = no gregory, 1 = fill, 2 = as early as possible
  39. #if PATCH_USE_GREGORY==2
  40. #define PATCH_USE_BEZIER_PATCH 1 // enable use of bezier instead of b-spline patches
  41. #else
  42. #define PATCH_USE_BEZIER_PATCH 0 // enable use of bezier instead of b-spline patches
  43. #endif
  44. #if PATCH_USE_BEZIER_PATCH
  45. # define RegularPatch BezierPatch
  46. # define RegularPatchT BezierPatchT<Vertex,Vertex_t>
  47. #else
  48. # define RegularPatch BSplinePatch
  49. # define RegularPatchT BSplinePatchT<Vertex,Vertex_t>
  50. #endif
  51. #if PATCH_USE_GREGORY
  52. #define IrregularFillPatch GregoryPatch
  53. #define IrregularFillPatchT GregoryPatchT<Vertex,Vertex_t>
  54. #else
  55. #define IrregularFillPatch BilinearPatch
  56. #define IrregularFillPatchT BilinearPatchT<Vertex,Vertex_t>
  57. #endif
  58. namespace embree
  59. {
  60. template<typename Vertex, typename Vertex_t = Vertex>
  61. struct __aligned(64) PatchT
  62. {
  63. public:
  64. typedef GeneralCatmullClarkPatchT<Vertex,Vertex_t> GeneralCatmullClarkPatch;
  65. typedef CatmullClarkPatchT<Vertex,Vertex_t> CatmullClarkPatch;
  66. typedef CatmullClark1RingT<Vertex,Vertex_t> CatmullClarkRing;
  67. typedef BezierCurveT<Vertex> BezierCurve;
  68. enum Type {
  69. INVALID_PATCH = 0,
  70. BILINEAR_PATCH = 1,
  71. BSPLINE_PATCH = 2,
  72. BEZIER_PATCH = 3,
  73. GREGORY_PATCH = 4,
  74. SUBDIVIDED_GENERAL_PATCH = 7,
  75. SUBDIVIDED_QUAD_PATCH = 8,
  76. EVAL_PATCH = 9,
  77. };
  78. struct Ref
  79. {
  80. __forceinline Ref(void* p = nullptr)
  81. : ptr((size_t)p) {}
  82. __forceinline operator bool() const { return ptr != 0; }
  83. __forceinline operator size_t() const { return ptr; }
  84. __forceinline Ref (Type ty, void* in)
  85. : ptr(((size_t)in)+ty) { assert((((size_t)in) & 0xF) == 0); }
  86. __forceinline Type type () const { return (Type)(ptr & 0xF); }
  87. __forceinline void* object() const { return (void*) (ptr & ~0xF); }
  88. size_t ptr;
  89. };
  90. struct EvalPatch
  91. {
  92. /* creates EvalPatch from a CatmullClarkPatch */
  93. template<typename Allocator>
  94. __noinline static Ref create(const Allocator& alloc, const CatmullClarkPatch& patch)
  95. {
  96. size_t ofs = 0, bytes = patch.bytes();
  97. void* ptr = alloc(bytes);
  98. patch.serialize(ptr,ofs);
  99. assert(ofs == bytes);
  100. return Ref(EVAL_PATCH, ptr);
  101. }
  102. };
  103. struct BilinearPatch
  104. {
  105. /* creates BilinearPatch from a CatmullClarkPatch */
  106. template<typename Allocator>
  107. __noinline static Ref create(const Allocator& alloc, const CatmullClarkPatch& patch,
  108. const BezierCurve* border0, const BezierCurve* border1, const BezierCurve* border2, const BezierCurve* border3) {
  109. return Ref(BILINEAR_PATCH, new (alloc(sizeof(BilinearPatch))) BilinearPatch(patch));
  110. }
  111. __forceinline BilinearPatch (const CatmullClarkPatch& patch)
  112. : patch(patch) {}
  113. /* creates BilinearPatch from 4 vertices */
  114. template<typename Allocator>
  115. __noinline static Ref create(const Allocator& alloc, const HalfEdge* edge, const char* vertices, size_t stride) {
  116. return Ref(BILINEAR_PATCH, new (alloc(sizeof(BilinearPatch))) BilinearPatch(edge,vertices,stride));
  117. }
  118. __forceinline BilinearPatch (const HalfEdge* edge, const char* vertices, size_t stride)
  119. : patch(edge,vertices,stride) {}
  120. public:
  121. BilinearPatchT<Vertex,Vertex_t> patch;
  122. };
  123. struct BSplinePatch
  124. {
  125. /* creates BSplinePatch from a half edge */
  126. template<typename Allocator>
  127. __noinline static Ref create(const Allocator& alloc, const HalfEdge* edge, const char* vertices, size_t stride) {
  128. return Ref(BSPLINE_PATCH, new (alloc(sizeof(BSplinePatch))) BSplinePatch(edge,vertices,stride));
  129. }
  130. __forceinline BSplinePatch (const HalfEdge* edge, const char* vertices, size_t stride)
  131. : patch(edge,vertices,stride) {}
  132. /* creates BSplinePatch from a CatmullClarkPatch */
  133. template<typename Allocator>
  134. __noinline static Ref create(const Allocator& alloc, const CatmullClarkPatch& patch,
  135. const BezierCurve* border0, const BezierCurve* border1, const BezierCurve* border2, const BezierCurve* border3) {
  136. return Ref(BSPLINE_PATCH, new (alloc(sizeof(BSplinePatch))) BSplinePatch(patch,border0,border1,border2,border3));
  137. }
  138. __forceinline BSplinePatch (const CatmullClarkPatch& patch, const BezierCurve* border0, const BezierCurve* border1, const BezierCurve* border2, const BezierCurve* border3)
  139. : patch(patch,border0,border1,border2,border3) {}
  140. public:
  141. BSplinePatchT<Vertex,Vertex_t> patch;
  142. };
  143. struct BezierPatch
  144. {
  145. /* creates BezierPatch from a half edge */
  146. template<typename Allocator>
  147. __noinline static Ref create(const Allocator& alloc, const HalfEdge* edge, const char* vertices, size_t stride) {
  148. return Ref(BEZIER_PATCH, new (alloc(sizeof(BezierPatch))) BezierPatch(edge,vertices,stride));
  149. }
  150. __forceinline BezierPatch (const HalfEdge* edge, const char* vertices, size_t stride)
  151. : patch(edge,vertices,stride) {}
  152. /* creates Bezier from a CatmullClarkPatch */
  153. template<typename Allocator>
  154. __noinline static Ref create(const Allocator& alloc, const CatmullClarkPatch& patch,
  155. const BezierCurve* border0, const BezierCurve* border1, const BezierCurve* border2, const BezierCurve* border3) {
  156. return Ref(BEZIER_PATCH, new (alloc(sizeof(BezierPatch))) BezierPatch(patch,border0,border1,border2,border3));
  157. }
  158. __forceinline BezierPatch (const CatmullClarkPatch& patch, const BezierCurve* border0, const BezierCurve* border1, const BezierCurve* border2, const BezierCurve* border3)
  159. : patch(patch,border0,border1,border2,border3) {}
  160. public:
  161. BezierPatchT<Vertex,Vertex_t> patch;
  162. };
  163. struct GregoryPatch
  164. {
  165. /* creates GregoryPatch from half edge */
  166. template<typename Allocator>
  167. __noinline static Ref create(const Allocator& alloc, const HalfEdge* edge, const char* vertices, size_t stride) {
  168. return Ref(GREGORY_PATCH, new (alloc(sizeof(GregoryPatch))) GregoryPatch(edge,vertices,stride));
  169. }
  170. __forceinline GregoryPatch (const HalfEdge* edge, const char* vertices, size_t stride)
  171. : patch(CatmullClarkPatch(edge,vertices,stride)) {}
  172. /* creates GregoryPatch from CatmullClarkPatch */
  173. template<typename Allocator>
  174. __noinline static Ref create(const Allocator& alloc, const CatmullClarkPatch& patch,
  175. const BezierCurve* border0, const BezierCurve* border1, const BezierCurve* border2, const BezierCurve* border3) {
  176. return Ref(GREGORY_PATCH, new (alloc(sizeof(GregoryPatch))) GregoryPatch(patch,border0,border1,border2,border3));
  177. }
  178. __forceinline GregoryPatch (const CatmullClarkPatch& patch, const BezierCurve* border0, const BezierCurve* border1, const BezierCurve* border2, const BezierCurve* border3)
  179. : patch(patch,border0,border1,border2,border3) {}
  180. public:
  181. GregoryPatchT<Vertex,Vertex_t> patch;
  182. };
  183. struct SubdividedQuadPatch
  184. {
  185. template<typename Allocator>
  186. __noinline static Ref create(const Allocator& alloc, Ref children[4]) {
  187. return Ref(SUBDIVIDED_QUAD_PATCH, new (alloc(sizeof(SubdividedQuadPatch))) SubdividedQuadPatch(children));
  188. }
  189. __forceinline SubdividedQuadPatch(Ref children[4]) {
  190. for (size_t i=0; i<4; i++) child[i] = children[i];
  191. }
  192. public:
  193. Ref child[4];
  194. };
  195. struct SubdividedGeneralPatch
  196. {
  197. template<typename Allocator>
  198. __noinline static Ref create(const Allocator& alloc, Ref* children, const unsigned N) {
  199. return Ref(SUBDIVIDED_GENERAL_PATCH, new (alloc(sizeof(SubdividedGeneralPatch))) SubdividedGeneralPatch(children,N));
  200. }
  201. __forceinline SubdividedGeneralPatch(Ref* children, const unsigned N) : N(N) {
  202. for (unsigned i=0; i<N; i++) child[i] = children[i];
  203. }
  204. unsigned N;
  205. Ref child[MAX_PATCH_VALENCE];
  206. };
  207. /*! Default constructor. */
  208. __forceinline PatchT () {}
  209. template<typename Allocator>
  210. __noinline static Ref create(const Allocator& alloc, const HalfEdge* edge, const char* vertices, size_t stride)
  211. {
  212. if (PATCH_MAX_CACHE_DEPTH == 0)
  213. return nullptr;
  214. Ref child(0);
  215. switch (edge->patch_type) {
  216. case HalfEdge::BILINEAR_PATCH: child = BilinearPatch::create(alloc,edge,vertices,stride); break;
  217. case HalfEdge::REGULAR_QUAD_PATCH: child = RegularPatch::create(alloc,edge,vertices,stride); break;
  218. #if PATCH_USE_GREGORY == 2
  219. case HalfEdge::IRREGULAR_QUAD_PATCH: child = GregoryPatch::create(alloc,edge,vertices,stride); break;
  220. #endif
  221. default: {
  222. GeneralCatmullClarkPatch patch(edge,vertices,stride);
  223. child = PatchT::create(alloc,patch,edge,vertices,stride,0);
  224. }
  225. }
  226. return child;
  227. }
  228. template<typename Allocator>
  229. __noinline static Ref create(const Allocator& alloc, GeneralCatmullClarkPatch& patch, const HalfEdge* edge, const char* vertices, size_t stride, size_t depth)
  230. {
  231. /* convert into standard quad patch if possible */
  232. if (likely(patch.isQuadPatch()))
  233. {
  234. CatmullClarkPatch qpatch; patch.init(qpatch);
  235. return PatchT::create(alloc,qpatch,edge,vertices,stride,depth);
  236. }
  237. /* do only cache up to some depth */
  238. if (depth >= PATCH_MAX_CACHE_DEPTH)
  239. return nullptr;
  240. /* subdivide patch */
  241. unsigned N;
  242. array_t<CatmullClarkPatch,GeneralCatmullClarkPatch::SIZE> patches;
  243. patch.subdivide(patches,N);
  244. if (N == 4)
  245. {
  246. Ref child[4];
  247. #if PATCH_USE_GREGORY == 2
  248. BezierCurve borders[GeneralCatmullClarkPatch::SIZE]; patch.getLimitBorder(borders);
  249. BezierCurve border0l,border0r; borders[0].subdivide(border0l,border0r);
  250. BezierCurve border1l,border1r; borders[1].subdivide(border1l,border1r);
  251. BezierCurve border2l,border2r; borders[2].subdivide(border2l,border2r);
  252. BezierCurve border3l,border3r; borders[3].subdivide(border3l,border3r);
  253. GeneralCatmullClarkPatch::fix_quad_ring_order(patches);
  254. child[0] = PatchT::create(alloc,patches[0],edge,vertices,stride,depth+1,&border0l,nullptr,nullptr,&border3r);
  255. child[1] = PatchT::create(alloc,patches[1],edge,vertices,stride,depth+1,&border0r,&border1l,nullptr,nullptr);
  256. child[2] = PatchT::create(alloc,patches[2],edge,vertices,stride,depth+1,nullptr,&border1r,&border2l,nullptr);
  257. child[3] = PatchT::create(alloc,patches[3],edge,vertices,stride,depth+1,nullptr,nullptr,&border2r,&border3l);
  258. #else
  259. GeneralCatmullClarkPatch::fix_quad_ring_order(patches);
  260. for (size_t i=0; i<4; i++)
  261. child[i] = PatchT::create(alloc,patches[i],edge,vertices,stride,depth+1);
  262. #endif
  263. return SubdividedQuadPatch::create(alloc,child);
  264. }
  265. else
  266. {
  267. assert(N<MAX_PATCH_VALENCE);
  268. Ref child[MAX_PATCH_VALENCE];
  269. #if PATCH_USE_GREGORY == 2
  270. BezierCurve borders[GeneralCatmullClarkPatch::SIZE];
  271. patch.getLimitBorder(borders);
  272. for (size_t i0=0; i0<N; i0++) {
  273. const size_t i2 = i0==0 ? N-1 : i0-1;
  274. BezierCurve border0l,border0r; borders[i0].subdivide(border0l,border0r);
  275. BezierCurve border2l,border2r; borders[i2].subdivide(border2l,border2r);
  276. child[i0] = PatchT::create(alloc,patches[i0],edge,vertices,stride,depth+1, &border0l, nullptr, nullptr, &border2r);
  277. }
  278. #else
  279. for (size_t i=0; i<N; i++)
  280. child[i] = PatchT::create(alloc,patches[i],edge,vertices,stride,depth+1);
  281. #endif
  282. return SubdividedGeneralPatch::create(alloc,child,N);
  283. }
  284. return nullptr;
  285. }
  286. static __forceinline bool final(const CatmullClarkPatch& patch, const typename CatmullClarkRing::Type type, size_t depth)
  287. {
  288. const size_t max_eval_depth = (type & CatmullClarkRing::TYPE_CREASES) ? PATCH_MAX_EVAL_DEPTH_CREASE : PATCH_MAX_EVAL_DEPTH_IRREGULAR;
  289. //#if PATCH_MIN_RESOLUTION
  290. // return patch.isFinalResolution(PATCH_MIN_RESOLUTION) || depth>=max_eval_depth;
  291. //#else
  292. return depth>=max_eval_depth;
  293. //#endif
  294. }
  295. template<typename Allocator>
  296. __noinline static Ref create(const Allocator& alloc, CatmullClarkPatch& patch, const HalfEdge* edge, const char* vertices, size_t stride, size_t depth,
  297. const BezierCurve* border0 = nullptr, const BezierCurve* border1 = nullptr, const BezierCurve* border2 = nullptr, const BezierCurve* border3 = nullptr)
  298. {
  299. const typename CatmullClarkPatch::Type ty = patch.type();
  300. if (unlikely(final(patch,ty,depth))) {
  301. if (ty & CatmullClarkRing::TYPE_REGULAR) return RegularPatch::create(alloc,patch,border0,border1,border2,border3);
  302. else return IrregularFillPatch::create(alloc,patch,border0,border1,border2,border3);
  303. }
  304. else if (ty & CatmullClarkRing::TYPE_REGULAR_CREASES) {
  305. assert(depth > 0); return RegularPatch::create(alloc,patch,border0,border1,border2,border3);
  306. }
  307. #if PATCH_USE_GREGORY == 2
  308. else if (ty & CatmullClarkRing::TYPE_GREGORY_CREASES) {
  309. assert(depth > 0); return GregoryPatch::create(alloc,patch,border0,border1,border2,border3);
  310. }
  311. #endif
  312. else if (depth >= PATCH_MAX_CACHE_DEPTH) {
  313. return EvalPatch::create(alloc,patch);
  314. }
  315. else
  316. {
  317. Ref child[4];
  318. array_t<CatmullClarkPatch,4> patches;
  319. patch.subdivide(patches);
  320. for (size_t i=0; i<4; i++)
  321. child[i] = PatchT::create(alloc,patches[i],edge,vertices,stride,depth+1);
  322. return SubdividedQuadPatch::create(alloc,child);
  323. }
  324. }
  325. };
  326. typedef PatchT<Vec3fa,Vec3fa_t> Patch3fa;
  327. }