Path: blob/master/thirdparty/embree/kernels/subdiv/patch.h
9913 views
// Copyright 2009-2021 Intel Corporation1// SPDX-License-Identifier: Apache-2.023#pragma once45#include "catmullclark_patch.h"6#include "bilinear_patch.h"7#include "bspline_patch.h"8#include "bezier_patch.h"9#include "gregory_patch.h"10#include "tessellation_cache.h"1112#if 113#define PATCH_DEBUG_SUBDIVISION(ptr,x,y,z)14#else15#define PATCH_DEBUG_SUBDIVISION(ptr,x,y,z) \16{ \17size_t hex = (size_t)ptr; \18for (size_t i=0; i<4; i++) hex = hex ^ (hex >> 8); \19const float c = (float)(((hex >> 0) ^ (hex >> 4) ^ (hex >> 8) ^ (hex >> 12) ^ (hex >> 16))&0xf)/15.0f; \20if (P) *P = Vertex(0.5f+0.5f*x,0.5f+0.5f*y,0.5f+0.5f*z,0.0f); \21}22#endif2324#define PATCH_MAX_CACHE_DEPTH 225//#define PATCH_MIN_RESOLUTION 1 // FIXME: not yet completely implemented26#define PATCH_MAX_EVAL_DEPTH_IRREGULAR 10 // maximum evaluation depth at irregular vertices (has to be larger or equal than PATCH_MAX_CACHE_DEPTH)27#define PATCH_MAX_EVAL_DEPTH_CREASE 10 // maximum evaluation depth at crease features (has to be larger or equal than PATCH_MAX_CACHE_DEPTH)28#define PATCH_USE_GREGORY 1 // 0 = no gregory, 1 = fill, 2 = as early as possible2930#if PATCH_USE_GREGORY==231#define PATCH_USE_BEZIER_PATCH 1 // enable use of bezier instead of b-spline patches32#else33#define PATCH_USE_BEZIER_PATCH 0 // enable use of bezier instead of b-spline patches34#endif3536#if PATCH_USE_BEZIER_PATCH37# define RegularPatch BezierPatch38# define RegularPatchT BezierPatchT<Vertex,Vertex_t>39#else40# define RegularPatch BSplinePatch41# define RegularPatchT BSplinePatchT<Vertex,Vertex_t>42#endif4344#if PATCH_USE_GREGORY45#define IrregularFillPatch GregoryPatch46#define IrregularFillPatchT GregoryPatchT<Vertex,Vertex_t>47#else48#define IrregularFillPatch BilinearPatch49#define IrregularFillPatchT BilinearPatchT<Vertex,Vertex_t>50#endif5152namespace embree53{54template<typename Vertex, typename Vertex_t = Vertex>55struct __aligned(64) PatchT56{57public:5859typedef GeneralCatmullClarkPatchT<Vertex,Vertex_t> GeneralCatmullClarkPatch;60typedef CatmullClarkPatchT<Vertex,Vertex_t> CatmullClarkPatch;61typedef CatmullClark1RingT<Vertex,Vertex_t> CatmullClarkRing;62typedef BezierCurveT<Vertex> BezierCurve;6364enum Type {65INVALID_PATCH = 0,66BILINEAR_PATCH = 1,67BSPLINE_PATCH = 2,68BEZIER_PATCH = 3,69GREGORY_PATCH = 4,70SUBDIVIDED_GENERAL_PATCH = 7,71SUBDIVIDED_QUAD_PATCH = 8,72EVAL_PATCH = 9,73};7475struct Ref76{77__forceinline Ref(void* p = nullptr)78: ptr((size_t)p) {}7980__forceinline operator bool() const { return ptr != 0; }81__forceinline operator size_t() const { return ptr; }8283__forceinline Ref (Type ty, void* in)84: ptr(((size_t)in)+ty) { assert((((size_t)in) & 0xF) == 0); }8586__forceinline Type type () const { return (Type)(ptr & 0xF); }87__forceinline void* object() const { return (void*) (ptr & ~0xF); }8889size_t ptr;90};9192struct EvalPatch93{94/* creates EvalPatch from a CatmullClarkPatch */95template<typename Allocator>96__noinline static Ref create(const Allocator& alloc, const CatmullClarkPatch& patch)97{98size_t ofs = 0, bytes = patch.bytes();99void* ptr = alloc(bytes);100patch.serialize(ptr,ofs);101assert(ofs == bytes);102return Ref(EVAL_PATCH, ptr);103}104};105106struct BilinearPatch107{108/* creates BilinearPatch from a CatmullClarkPatch */109template<typename Allocator>110__noinline static Ref create(const Allocator& alloc, const CatmullClarkPatch& patch,111const BezierCurve* border0, const BezierCurve* border1, const BezierCurve* border2, const BezierCurve* border3) {112return Ref(BILINEAR_PATCH, new (alloc(sizeof(BilinearPatch))) BilinearPatch(patch));113}114115__forceinline BilinearPatch (const CatmullClarkPatch& patch)116: patch(patch) {}117118/* creates BilinearPatch from 4 vertices */119template<typename Allocator>120__noinline static Ref create(const Allocator& alloc, const HalfEdge* edge, const char* vertices, size_t stride) {121return Ref(BILINEAR_PATCH, new (alloc(sizeof(BilinearPatch))) BilinearPatch(edge,vertices,stride));122}123124__forceinline BilinearPatch (const HalfEdge* edge, const char* vertices, size_t stride)125: patch(edge,vertices,stride) {}126127public:128BilinearPatchT<Vertex,Vertex_t> patch;129};130131struct BSplinePatch132{133/* creates BSplinePatch from a half edge */134template<typename Allocator>135__noinline static Ref create(const Allocator& alloc, const HalfEdge* edge, const char* vertices, size_t stride) {136return Ref(BSPLINE_PATCH, new (alloc(sizeof(BSplinePatch))) BSplinePatch(edge,vertices,stride));137}138139__forceinline BSplinePatch (const HalfEdge* edge, const char* vertices, size_t stride)140: patch(edge,vertices,stride) {}141142/* creates BSplinePatch from a CatmullClarkPatch */143template<typename Allocator>144__noinline static Ref create(const Allocator& alloc, const CatmullClarkPatch& patch,145const BezierCurve* border0, const BezierCurve* border1, const BezierCurve* border2, const BezierCurve* border3) {146return Ref(BSPLINE_PATCH, new (alloc(sizeof(BSplinePatch))) BSplinePatch(patch,border0,border1,border2,border3));147}148149__forceinline BSplinePatch (const CatmullClarkPatch& patch, const BezierCurve* border0, const BezierCurve* border1, const BezierCurve* border2, const BezierCurve* border3)150: patch(patch,border0,border1,border2,border3) {}151152public:153BSplinePatchT<Vertex,Vertex_t> patch;154};155156struct BezierPatch157{158/* creates BezierPatch from a half edge */159template<typename Allocator>160__noinline static Ref create(const Allocator& alloc, const HalfEdge* edge, const char* vertices, size_t stride) {161return Ref(BEZIER_PATCH, new (alloc(sizeof(BezierPatch))) BezierPatch(edge,vertices,stride));162}163164__forceinline BezierPatch (const HalfEdge* edge, const char* vertices, size_t stride)165: patch(edge,vertices,stride) {}166167/* creates Bezier from a CatmullClarkPatch */168template<typename Allocator>169__noinline static Ref create(const Allocator& alloc, const CatmullClarkPatch& patch,170const BezierCurve* border0, const BezierCurve* border1, const BezierCurve* border2, const BezierCurve* border3) {171return Ref(BEZIER_PATCH, new (alloc(sizeof(BezierPatch))) BezierPatch(patch,border0,border1,border2,border3));172}173174__forceinline BezierPatch (const CatmullClarkPatch& patch, const BezierCurve* border0, const BezierCurve* border1, const BezierCurve* border2, const BezierCurve* border3)175: patch(patch,border0,border1,border2,border3) {}176177public:178BezierPatchT<Vertex,Vertex_t> patch;179};180181struct GregoryPatch182{183/* creates GregoryPatch from half edge */184template<typename Allocator>185__noinline static Ref create(const Allocator& alloc, const HalfEdge* edge, const char* vertices, size_t stride) {186return Ref(GREGORY_PATCH, new (alloc(sizeof(GregoryPatch))) GregoryPatch(edge,vertices,stride));187}188189__forceinline GregoryPatch (const HalfEdge* edge, const char* vertices, size_t stride)190: patch(CatmullClarkPatch(edge,vertices,stride)) {}191192/* creates GregoryPatch from CatmullClarkPatch */193template<typename Allocator>194__noinline static Ref create(const Allocator& alloc, const CatmullClarkPatch& patch,195const BezierCurve* border0, const BezierCurve* border1, const BezierCurve* border2, const BezierCurve* border3) {196return Ref(GREGORY_PATCH, new (alloc(sizeof(GregoryPatch))) GregoryPatch(patch,border0,border1,border2,border3));197}198199__forceinline GregoryPatch (const CatmullClarkPatch& patch, const BezierCurve* border0, const BezierCurve* border1, const BezierCurve* border2, const BezierCurve* border3)200: patch(patch,border0,border1,border2,border3) {}201202public:203GregoryPatchT<Vertex,Vertex_t> patch;204};205206struct SubdividedQuadPatch207{208template<typename Allocator>209__noinline static Ref create(const Allocator& alloc, Ref children[4]) {210return Ref(SUBDIVIDED_QUAD_PATCH, new (alloc(sizeof(SubdividedQuadPatch))) SubdividedQuadPatch(children));211}212213__forceinline SubdividedQuadPatch(Ref children[4]) {214for (size_t i=0; i<4; i++) child[i] = children[i];215}216217public:218Ref child[4];219};220221struct SubdividedGeneralPatch222{223template<typename Allocator>224__noinline static Ref create(const Allocator& alloc, Ref* children, const unsigned N) {225return Ref(SUBDIVIDED_GENERAL_PATCH, new (alloc(sizeof(SubdividedGeneralPatch))) SubdividedGeneralPatch(children,N));226}227228__forceinline SubdividedGeneralPatch(Ref* children, const unsigned N) : N(N) {229for (unsigned i=0; i<N; i++) child[i] = children[i];230}231232unsigned N;233Ref child[MAX_PATCH_VALENCE];234};235236/*! Default constructor. */237__forceinline PatchT () {}238239template<typename Allocator>240__noinline static Ref create(const Allocator& alloc, const HalfEdge* edge, const char* vertices, size_t stride)241{242if (PATCH_MAX_CACHE_DEPTH == 0)243return nullptr;244245Ref child(0);246switch (edge->patch_type) {247case HalfEdge::BILINEAR_PATCH: child = BilinearPatch::create(alloc,edge,vertices,stride); break;248case HalfEdge::REGULAR_QUAD_PATCH: child = RegularPatch::create(alloc,edge,vertices,stride); break;249#if PATCH_USE_GREGORY == 2250case HalfEdge::IRREGULAR_QUAD_PATCH: child = GregoryPatch::create(alloc,edge,vertices,stride); break;251#endif252default: {253GeneralCatmullClarkPatch patch(edge,vertices,stride);254child = PatchT::create(alloc,patch,edge,vertices,stride,0);255}256}257return child;258}259260template<typename Allocator>261__noinline static Ref create(const Allocator& alloc, GeneralCatmullClarkPatch& patch, const HalfEdge* edge, const char* vertices, size_t stride, size_t depth)262{263/* convert into standard quad patch if possible */264if (likely(patch.isQuadPatch()))265{266CatmullClarkPatch qpatch; patch.init(qpatch);267return PatchT::create(alloc,qpatch,edge,vertices,stride,depth);268}269270/* do only cache up to some depth */271if (depth >= PATCH_MAX_CACHE_DEPTH)272return nullptr;273274/* subdivide patch */275unsigned N;276array_t<CatmullClarkPatch,GeneralCatmullClarkPatch::SIZE> patches;277patch.subdivide(patches,N);278279if (N == 4)280{281Ref child[4];282#if PATCH_USE_GREGORY == 2283BezierCurve borders[GeneralCatmullClarkPatch::SIZE]; patch.getLimitBorder(borders);284BezierCurve border0l,border0r; borders[0].subdivide(border0l,border0r);285BezierCurve border1l,border1r; borders[1].subdivide(border1l,border1r);286BezierCurve border2l,border2r; borders[2].subdivide(border2l,border2r);287BezierCurve border3l,border3r; borders[3].subdivide(border3l,border3r);288GeneralCatmullClarkPatch::fix_quad_ring_order(patches);289child[0] = PatchT::create(alloc,patches[0],edge,vertices,stride,depth+1,&border0l,nullptr,nullptr,&border3r);290child[1] = PatchT::create(alloc,patches[1],edge,vertices,stride,depth+1,&border0r,&border1l,nullptr,nullptr);291child[2] = PatchT::create(alloc,patches[2],edge,vertices,stride,depth+1,nullptr,&border1r,&border2l,nullptr);292child[3] = PatchT::create(alloc,patches[3],edge,vertices,stride,depth+1,nullptr,nullptr,&border2r,&border3l);293#else294GeneralCatmullClarkPatch::fix_quad_ring_order(patches);295for (size_t i=0; i<4; i++)296child[i] = PatchT::create(alloc,patches[i],edge,vertices,stride,depth+1);297#endif298return SubdividedQuadPatch::create(alloc,child);299}300else301{302assert(N<MAX_PATCH_VALENCE);303Ref child[MAX_PATCH_VALENCE];304305#if PATCH_USE_GREGORY == 2306BezierCurve borders[GeneralCatmullClarkPatch::SIZE];307patch.getLimitBorder(borders);308309for (size_t i0=0; i0<N; i0++) {310const size_t i2 = i0==0 ? N-1 : i0-1;311BezierCurve border0l,border0r; borders[i0].subdivide(border0l,border0r);312BezierCurve border2l,border2r; borders[i2].subdivide(border2l,border2r);313child[i0] = PatchT::create(alloc,patches[i0],edge,vertices,stride,depth+1, &border0l, nullptr, nullptr, &border2r);314}315#else316for (size_t i=0; i<N; i++)317child[i] = PatchT::create(alloc,patches[i],edge,vertices,stride,depth+1);318#endif319return SubdividedGeneralPatch::create(alloc,child,N);320}321322return nullptr;323}324325static __forceinline bool final(const CatmullClarkPatch& patch, const typename CatmullClarkRing::Type type, size_t depth)326{327const size_t max_eval_depth = (type & CatmullClarkRing::TYPE_CREASES) ? PATCH_MAX_EVAL_DEPTH_CREASE : PATCH_MAX_EVAL_DEPTH_IRREGULAR;328//#if PATCH_MIN_RESOLUTION329// return patch.isFinalResolution(PATCH_MIN_RESOLUTION) || depth>=max_eval_depth;330//#else331return depth>=max_eval_depth;332//#endif333}334335template<typename Allocator>336__noinline static Ref create(const Allocator& alloc, CatmullClarkPatch& patch, const HalfEdge* edge, const char* vertices, size_t stride, size_t depth,337const BezierCurve* border0 = nullptr, const BezierCurve* border1 = nullptr, const BezierCurve* border2 = nullptr, const BezierCurve* border3 = nullptr)338{339const typename CatmullClarkPatch::Type ty = patch.type();340if (unlikely(final(patch,ty,depth))) {341if (ty & CatmullClarkRing::TYPE_REGULAR) return RegularPatch::create(alloc,patch,border0,border1,border2,border3);342else return IrregularFillPatch::create(alloc,patch,border0,border1,border2,border3);343}344else if (ty & CatmullClarkRing::TYPE_REGULAR_CREASES) {345assert(depth > 0); return RegularPatch::create(alloc,patch,border0,border1,border2,border3);346}347#if PATCH_USE_GREGORY == 2348else if (ty & CatmullClarkRing::TYPE_GREGORY_CREASES) {349assert(depth > 0); return GregoryPatch::create(alloc,patch,border0,border1,border2,border3);350}351#endif352else if (depth >= PATCH_MAX_CACHE_DEPTH) {353return EvalPatch::create(alloc,patch);354}355356else357{358Ref child[4];359array_t<CatmullClarkPatch,4> patches;360patch.subdivide(patches);361362for (size_t i=0; i<4; i++)363child[i] = PatchT::create(alloc,patches[i],edge,vertices,stride,depth+1);364return SubdividedQuadPatch::create(alloc,child);365}366}367};368369typedef PatchT<Vec3fa,Vec3fa_t> Patch3fa;370}371372373