patch.h 15 KB

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