Path: blob/master/thirdparty/embree/kernels/subdiv/patch_eval.h
9913 views
// Copyright 2009-2021 Intel Corporation1// SPDX-License-Identifier: Apache-2.023#pragma once45#include "patch.h"6#include "feature_adaptive_eval.h"78namespace embree9{10namespace isa11{12template<typename Vertex, typename Vertex_t = Vertex>13struct PatchEval14{15public:1617typedef PatchT<Vertex,Vertex_t> Patch;18typedef typename Patch::Ref Ref;19typedef CatmullClarkPatchT<Vertex,Vertex_t> CatmullClarkPatch;2021PatchEval (SharedLazyTessellationCache::CacheEntry& entry, size_t commitCounter,22const HalfEdge* edge, const char* vertices, size_t stride, const float u, const float v,23Vertex* P, Vertex* dPdu, Vertex* dPdv, Vertex* ddPdudu, Vertex* ddPdvdv, Vertex* ddPdudv)24: P(P), dPdu(dPdu), dPdv(dPdv), ddPdudu(ddPdudu), ddPdvdv(ddPdvdv), ddPdudv(ddPdudv)25{26/* conservative time for the very first allocation */27auto time = SharedLazyTessellationCache::sharedLazyTessellationCache.getTime(commitCounter);2829Ref patch = SharedLazyTessellationCache::lookup(entry,commitCounter,[&] () {30auto alloc = [&](size_t bytes) { return SharedLazyTessellationCache::malloc(bytes); };31return Patch::create(alloc,edge,vertices,stride);32},true);3334auto curTime = SharedLazyTessellationCache::sharedLazyTessellationCache.getTime(commitCounter);35const bool allAllocationsValid = SharedLazyTessellationCache::validTime(time,curTime);3637if (patch && allAllocationsValid && eval(patch,u,v,1.0f,0)) {38SharedLazyTessellationCache::unlock();39return;40}41SharedLazyTessellationCache::unlock();42FeatureAdaptiveEval<Vertex,Vertex_t>(edge,vertices,stride,u,v,P,dPdu,dPdv,ddPdudu,ddPdvdv,ddPdudv);43PATCH_DEBUG_SUBDIVISION(edge,c,-1,-1);44}4546__forceinline bool eval_quad(const typename Patch::SubdividedQuadPatch* This, const float u, const float v, const float dscale, const size_t depth)47{48if (v < 0.5f) {49if (u < 0.5f) return eval(This->child[0],2.0f*u,2.0f*v,2.0f*dscale,depth+1);50else return eval(This->child[1],2.0f*u-1.0f,2.0f*v,2.0f*dscale,depth+1);51} else {52if (u > 0.5f) return eval(This->child[2],2.0f*u-1.0f,2.0f*v-1.0f,2.0f*dscale,depth+1);53else return eval(This->child[3],2.0f*u,2.0f*v-1.0f,2.0f*dscale,depth+1);54}55}5657bool eval_general(const typename Patch::SubdividedGeneralPatch* This, const float U, const float V, const size_t depth)58{59const unsigned l = (unsigned) floor(0.5f*U); const float u = 2.0f*frac(0.5f*U)-0.5f;60const unsigned h = (unsigned) floor(0.5f*V); const float v = 2.0f*frac(0.5f*V)-0.5f;61const unsigned i = 4*h+l; assert(i<This->N);62return eval(This->child[i],u,v,1.0f,depth+1);63}6465bool eval(Ref This, const float& u, const float& v, const float dscale, const size_t depth)66{67if (!This) return false;68//PRINT(depth);69//PRINT2(u,v);7071switch (This.type())72{73case Patch::BILINEAR_PATCH: {74//PRINT("bilinear");75((typename Patch::BilinearPatch*)This.object())->patch.eval(u,v,P,dPdu,dPdv,ddPdudu,ddPdvdv,ddPdudv,dscale);76PATCH_DEBUG_SUBDIVISION(This,-1,c,c);77return true;78}79case Patch::BSPLINE_PATCH: {80//PRINT("bspline");81((typename Patch::BSplinePatch*)This.object())->patch.eval(u,v,P,dPdu,dPdv,ddPdudu,ddPdvdv,ddPdudv,dscale);82PATCH_DEBUG_SUBDIVISION(This,-1,c,-1);83return true;84}85case Patch::BEZIER_PATCH: {86//PRINT("bezier");87((typename Patch::BezierPatch*)This.object())->patch.eval(u,v,P,dPdu,dPdv,ddPdudu,ddPdvdv,ddPdudv,dscale);88PATCH_DEBUG_SUBDIVISION(This,-1,c,-1);89return true;90}91case Patch::GREGORY_PATCH: {92//PRINT("gregory");93((typename Patch::GregoryPatch*)This.object())->patch.eval(u,v,P,dPdu,dPdv,ddPdudu,ddPdvdv,ddPdudv,dscale);94PATCH_DEBUG_SUBDIVISION(This,-1,-1,c);95return true;96}97case Patch::SUBDIVIDED_QUAD_PATCH: {98//PRINT("subdivided quad");99return eval_quad(((typename Patch::SubdividedQuadPatch*)This.object()),u,v,dscale,depth);100}101case Patch::SUBDIVIDED_GENERAL_PATCH: {102//PRINT("general_patch");103assert(dscale == 1.0f);104return eval_general(((typename Patch::SubdividedGeneralPatch*)This.object()),u,v,depth);105}106case Patch::EVAL_PATCH: {107//PRINT("eval_patch");108CatmullClarkPatch patch; patch.deserialize(This.object());109FeatureAdaptiveEval<Vertex,Vertex_t>(patch,u,v,dscale,depth,P,dPdu,dPdv,ddPdudu,ddPdvdv,ddPdudv);110return true;111}112default:113assert(false);114return false;115}116}117118private:119Vertex* const P;120Vertex* const dPdu;121Vertex* const dPdv;122Vertex* const ddPdudu;123Vertex* const ddPdvdv;124Vertex* const ddPdudv;125};126}127}128129130131