Path: blob/master/thirdparty/embree/kernels/subdiv/feature_adaptive_eval_grid.h
9913 views
// Copyright 2009-2021 Intel Corporation1// SPDX-License-Identifier: Apache-2.023#pragma once45#include "patch.h"6#include "catmullclark_patch.h"7#include "bspline_patch.h"8#include "gregory_patch.h"9#include "tessellation.h"1011namespace embree12{13namespace isa14{15struct FeatureAdaptiveEvalGrid16{17typedef CatmullClark1Ring3fa CatmullClarkRing;18typedef CatmullClarkPatch3fa CatmullClarkPatch;19typedef BilinearPatch3fa BilinearPatch;20typedef BSplinePatch3fa BSplinePatch;21typedef BezierPatch3fa BezierPatch;22typedef GregoryPatch3fa GregoryPatch;2324private:25const unsigned x0,x1;26const unsigned y0,y1;27const unsigned swidth,sheight;28const float rcp_swidth, rcp_sheight;29float* const Px;30float* const Py;31float* const Pz;32float* const U;33float* const V;34float* const Nx;35float* const Ny;36float* const Nz;37const unsigned dwidth;38//const unsigned dheight;39unsigned count;404142public:43FeatureAdaptiveEvalGrid (const GeneralCatmullClarkPatch3fa& patch, unsigned subPatch,44const unsigned x0, const unsigned x1, const unsigned y0, const unsigned y1, const unsigned swidth, const unsigned sheight,45float* Px, float* Py, float* Pz, float* U, float* V,46float* Nx, float* Ny, float* Nz,47const unsigned dwidth, const unsigned dheight)48: x0(x0), x1(x1), y0(y0), y1(y1), swidth(swidth), sheight(sheight), rcp_swidth(1.0f/(swidth-1.0f)), rcp_sheight(1.0f/(sheight-1.0f)),49Px(Px), Py(Py), Pz(Pz), U(U), V(V), Nx(Nx), Ny(Ny), Nz(Nz), dwidth(dwidth), /*dheight(dheight),*/ count(0)50{51assert(swidth < (2<<20) && sheight < (2<<20));52const BBox2f srange(Vec2f(0.0f,0.0f),Vec2f(float(swidth-1),float(sheight-1)));53const BBox2f erange(Vec2f((float)x0,(float)y0),Vec2f((float)x1,(float)y1));5455/* convert into standard quad patch if possible */56if (likely(patch.isQuadPatch()))57{58CatmullClarkPatch3fa qpatch; patch.init(qpatch);59eval(qpatch, srange, erange, 0);60assert(count == (x1-x0+1)*(y1-y0+1));61return;62}6364/* subdivide patch */65unsigned N;66array_t<CatmullClarkPatch3fa,GeneralCatmullClarkPatch3fa::SIZE> patches;67patch.subdivide(patches,N);6869if (N == 4)70{71const Vec2f c = srange.center();72const BBox2f srange0(srange.lower,c);73const BBox2f srange1(Vec2f(c.x,srange.lower.y),Vec2f(srange.upper.x,c.y));74const BBox2f srange2(c,srange.upper);75const BBox2f srange3(Vec2f(srange.lower.x,c.y),Vec2f(c.x,srange.upper.y));7677#if PATCH_USE_GREGORY == 278BezierCurve3fa borders[GeneralCatmullClarkPatch3fa::SIZE]; patch.getLimitBorder(borders);79BezierCurve3fa border0l,border0r; borders[0].subdivide(border0l,border0r);80BezierCurve3fa border1l,border1r; borders[1].subdivide(border1l,border1r);81BezierCurve3fa border2l,border2r; borders[2].subdivide(border2l,border2r);82BezierCurve3fa border3l,border3r; borders[3].subdivide(border3l,border3r);83GeneralCatmullClarkPatch3fa::fix_quad_ring_order(patches);84eval(patches[0],srange0,intersect(srange0,erange),1,&border0l,nullptr,nullptr,&border3r);85eval(patches[1],srange1,intersect(srange1,erange),1,&border0r,&border1l,nullptr,nullptr);86eval(patches[2],srange2,intersect(srange2,erange),1,nullptr,&border1r,&border2l,nullptr);87eval(patches[3],srange3,intersect(srange3,erange),1,nullptr,nullptr,&border2r,&border3l);88#else89GeneralCatmullClarkPatch3fa::fix_quad_ring_order(patches);90eval(patches[0],srange0,intersect(srange0,erange),1);91eval(patches[1],srange1,intersect(srange1,erange),1);92eval(patches[2],srange2,intersect(srange2,erange),1);93eval(patches[3],srange3,intersect(srange3,erange),1);94#endif95}96else97{98assert(subPatch < N);99100#if PATCH_USE_GREGORY == 2101BezierCurve3fa borders[2]; patch.getLimitBorder(borders,subPatch);102BezierCurve3fa border0l,border0r; borders[0].subdivide(border0l,border0r);103BezierCurve3fa border2l,border2r; borders[1].subdivide(border2l,border2r);104eval(patches[subPatch], srange, erange, 1, &border0l, nullptr, nullptr, &border2r);105#else106eval(patches[subPatch], srange, erange, 1);107#endif108109}110assert(count == (x1-x0+1)*(y1-y0+1));111}112113FeatureAdaptiveEvalGrid (const CatmullClarkPatch3fa& patch,114const BBox2f& srange, const BBox2f& erange, const unsigned depth,115const unsigned x0, const unsigned x1, const unsigned y0, const unsigned y1, const unsigned swidth, const unsigned sheight,116float* Px, float* Py, float* Pz, float* U, float* V,117float* Nx, float* Ny, float* Nz,118const unsigned dwidth, const unsigned dheight)119: x0(x0), x1(x1), y0(y0), y1(y1), swidth(swidth), sheight(sheight), rcp_swidth(1.0f/(swidth-1.0f)), rcp_sheight(1.0f/(sheight-1.0f)),120Px(Px), Py(Py), Pz(Pz), U(U), V(V), Nx(Nx), Ny(Ny), Nz(Nz), dwidth(dwidth), /*dheight(dheight),*/ count(0)121{122eval(patch,srange,erange,depth);123}124125template<typename Patch>126void evalLocalGrid(const Patch& patch, const BBox2f& srange, const int lx0, const int lx1, const int ly0, const int ly1)127{128const float scale_x = rcp(srange.upper.x-srange.lower.x);129const float scale_y = rcp(srange.upper.y-srange.lower.y);130count += (lx1-lx0)*(ly1-ly0);131132#if 0133for (unsigned iy=ly0; iy<ly1; iy++) {134for (unsigned ix=lx0; ix<lx1; ix++) {135const float lu = select(ix == swidth -1, float(1.0f), (float(ix)-srange.lower.x)*scale_x);136const float lv = select(iy == sheight-1, float(1.0f), (float(iy)-srange.lower.y)*scale_y);137const Vec3fa p = patch.eval(lu,lv);138const float u = float(ix)*rcp_swidth;139const float v = float(iy)*rcp_sheight;140const int ofs = (iy-y0)*dwidth+(ix-x0);141Px[ofs] = p.x;142Py[ofs] = p.y;143Pz[ofs] = p.z;144U[ofs] = u;145V[ofs] = v;146}147}148#else149foreach2(lx0,lx1,ly0,ly1,[&](const vboolx& valid, const vintx& ix, const vintx& iy) {150const vfloatx lu = select(ix == swidth -1, vfloatx(1.0f), (vfloatx(ix)-srange.lower.x)*scale_x);151const vfloatx lv = select(iy == sheight-1, vfloatx(1.0f), (vfloatx(iy)-srange.lower.y)*scale_y);152const Vec3vfx p = patch.eval(lu,lv);153Vec3vfx n = zero;154if (unlikely(Nx != nullptr)) n = normalize_safe(patch.normal(lu,lv));155const vfloatx u = vfloatx(ix)*rcp_swidth;156const vfloatx v = vfloatx(iy)*rcp_sheight;157const vintx ofs = (iy-y0)*dwidth+(ix-x0);158if (likely(all(valid)) && all(iy==iy[0])) {159const unsigned ofs2 = ofs[0];160vfloatx::storeu(Px+ofs2,p.x);161vfloatx::storeu(Py+ofs2,p.y);162vfloatx::storeu(Pz+ofs2,p.z);163vfloatx::storeu(U+ofs2,u);164vfloatx::storeu(V+ofs2,v);165if (unlikely(Nx != nullptr)) {166vfloatx::storeu(Nx+ofs2,n.x);167vfloatx::storeu(Ny+ofs2,n.y);168vfloatx::storeu(Nz+ofs2,n.z);169}170} else {171foreach_unique_index(valid,iy,[&](const vboolx& valid, const int iy0, const int j) {172const unsigned ofs2 = ofs[j]-j;173vfloatx::storeu(valid,Px+ofs2,p.x);174vfloatx::storeu(valid,Py+ofs2,p.y);175vfloatx::storeu(valid,Pz+ofs2,p.z);176vfloatx::storeu(valid,U+ofs2,u);177vfloatx::storeu(valid,V+ofs2,v);178if (unlikely(Nx != nullptr)) {179vfloatx::storeu(valid,Nx+ofs2,n.x);180vfloatx::storeu(valid,Ny+ofs2,n.y);181vfloatx::storeu(valid,Nz+ofs2,n.z);182}183});184}185});186#endif187}188189__forceinline bool final(const CatmullClarkPatch3fa& patch, const CatmullClarkRing::Type type, unsigned depth)190{191const unsigned max_eval_depth = (type & CatmullClarkRing::TYPE_CREASES) ? PATCH_MAX_EVAL_DEPTH_CREASE : PATCH_MAX_EVAL_DEPTH_IRREGULAR;192//#if PATCH_MIN_RESOLUTION193// return patch.isFinalResolution(PATCH_MIN_RESOLUTION) || depth>=max_eval_depth;194//#else195return depth>=max_eval_depth;196//#endif197}198199void eval(const CatmullClarkPatch3fa& patch, const BBox2f& srange, const BBox2f& erange, const unsigned depth,200const BezierCurve3fa* border0 = nullptr, const BezierCurve3fa* border1 = nullptr, const BezierCurve3fa* border2 = nullptr, const BezierCurve3fa* border3 = nullptr)201{202if (erange.empty())203return;204205int lx0 = (int) ceilf(erange.lower.x);206int lx1 = (int) ceilf(erange.upper.x) + (erange.upper.x == x1 && (srange.lower.x < erange.upper.x || erange.upper.x == 0));207int ly0 = (int) ceilf(erange.lower.y);208int ly1 = (int) ceilf(erange.upper.y) + (erange.upper.y == y1 && (srange.lower.y < erange.upper.y || erange.upper.y == 0));209if (lx0 >= lx1 || ly0 >= ly1) return;210211CatmullClarkPatch::Type ty = patch.type();212213if (unlikely(final(patch,ty,depth)))214{215if (ty & CatmullClarkRing::TYPE_REGULAR) {216RegularPatch rpatch(patch,border0,border1,border2,border3);217evalLocalGrid(rpatch,srange,lx0,lx1,ly0,ly1);218return;219} else {220IrregularFillPatch ipatch(patch,border0,border1,border2,border3);221evalLocalGrid(ipatch,srange,lx0,lx1,ly0,ly1);222return;223}224}225else if (ty & CatmullClarkRing::TYPE_REGULAR_CREASES) {226assert(depth > 0);227RegularPatch rpatch(patch,border0,border1,border2,border3);228evalLocalGrid(rpatch,srange,lx0,lx1,ly0,ly1);229return;230}231#if PATCH_USE_GREGORY == 2232else if (ty & CatmullClarkRing::TYPE_GREGORY_CREASES) {233assert(depth > 0);234GregoryPatch gpatch(patch,border0,border1,border2,border3);235evalLocalGrid(gpatch,srange,lx0,lx1,ly0,ly1);236}237#endif238else239{240array_t<CatmullClarkPatch3fa,4> patches;241patch.subdivide(patches);242243const Vec2f c = srange.center();244const BBox2f srange0(srange.lower,c);245const BBox2f srange1(Vec2f(c.x,srange.lower.y),Vec2f(srange.upper.x,c.y));246const BBox2f srange2(c,srange.upper);247const BBox2f srange3(Vec2f(srange.lower.x,c.y),Vec2f(c.x,srange.upper.y));248249eval(patches[0],srange0,intersect(srange0,erange),depth+1);250eval(patches[1],srange1,intersect(srange1,erange),depth+1);251eval(patches[2],srange2,intersect(srange2,erange),depth+1);252eval(patches[3],srange3,intersect(srange3,erange),depth+1);253}254}255};256257template<typename Eval, typename Patch>258bool stitch_col(const Patch& patch, int subPatch,259const bool right, const unsigned y0, const unsigned y1, const int fine_y, const int coarse_y,260float* Px, float* Py, float* Pz, float* U, float* V, float* Nx, float* Ny, float* Nz, const unsigned dx0, const unsigned dwidth, const unsigned dheight)261{262assert(coarse_y <= fine_y);263if (likely(fine_y == coarse_y))264return false;265266const unsigned y0s = stitch(y0,fine_y,coarse_y);267const unsigned y1s = stitch(y1,fine_y,coarse_y);268const unsigned M = y1s-y0s+1 + VSIZEX;269270dynamic_large_stack_array(float,px,M,64*sizeof(float));271dynamic_large_stack_array(float,py,M,64*sizeof(float));272dynamic_large_stack_array(float,pz,M,64*sizeof(float));273dynamic_large_stack_array(float,u,M,64*sizeof(float));274dynamic_large_stack_array(float,v,M,64*sizeof(float));275dynamic_large_stack_array(float,nx,M,64*sizeof(float));276dynamic_large_stack_array(float,ny,M,64*sizeof(float));277dynamic_large_stack_array(float,nz,M,64*sizeof(float));278const bool has_Nxyz = Nx; assert(!Nx || (Ny && Nz));279Eval(patch,subPatch, right,right, y0s,y1s, 2,coarse_y+1, px,py,pz,u,v,280has_Nxyz ? (float*)nx : nullptr,has_Nxyz ? (float*)ny : nullptr ,has_Nxyz ? (float*)nz : nullptr, 1,4097);281282for (unsigned y=y0; y<=y1; y++)283{284const unsigned ys = stitch(y,fine_y,coarse_y)-y0s;285Px[(y-y0)*dwidth+dx0] = px[ys];286Py[(y-y0)*dwidth+dx0] = py[ys];287Pz[(y-y0)*dwidth+dx0] = pz[ys];288U [(y-y0)*dwidth+dx0] = u[ys];289V [(y-y0)*dwidth+dx0] = v[ys];290if (unlikely(has_Nxyz)) {291Nx[(y-y0)*dwidth+dx0] = nx[ys];292Ny[(y-y0)*dwidth+dx0] = ny[ys];293Nz[(y-y0)*dwidth+dx0] = nz[ys];294}295}296return true;297}298299template<typename Eval, typename Patch>300bool stitch_row(const Patch& patch, int subPatch,301const bool bottom, const unsigned x0, const unsigned x1, const int fine_x, const int coarse_x,302float* Px, float* Py, float* Pz, float* U, float* V, float* Nx, float* Ny, float* Nz, const unsigned dy0, const unsigned dwidth, const unsigned dheight)303{304assert(coarse_x <= fine_x);305if (likely(fine_x == coarse_x))306return false;307308const unsigned x0s = stitch(x0,fine_x,coarse_x);309const unsigned x1s = stitch(x1,fine_x,coarse_x);310const unsigned M = x1s-x0s+1 + VSIZEX;311312dynamic_large_stack_array(float,px,M,32*sizeof(float));313dynamic_large_stack_array(float,py,M,32*sizeof(float));314dynamic_large_stack_array(float,pz,M,32*sizeof(float));315dynamic_large_stack_array(float,u,M,32*sizeof(float));316dynamic_large_stack_array(float,v,M,32*sizeof(float));317dynamic_large_stack_array(float,nx,M,32*sizeof(float));318dynamic_large_stack_array(float,ny,M,32*sizeof(float));319dynamic_large_stack_array(float,nz,M,32*sizeof(float));320const bool has_Nxyz = Nx; assert(!Nx || (Ny && Nz));321Eval(patch,subPatch, x0s,x1s, bottom,bottom, coarse_x+1,2, px,py,pz,u,v,322has_Nxyz ? (float*)nx :nullptr, has_Nxyz ? (float*)ny : nullptr , has_Nxyz ? (float*)nz : nullptr, 4097,1);323324for (unsigned x=x0; x<=x1; x++)325{326const unsigned xs = stitch(x,fine_x,coarse_x)-x0s;327Px[dy0*dwidth+x-x0] = px[xs];328Py[dy0*dwidth+x-x0] = py[xs];329Pz[dy0*dwidth+x-x0] = pz[xs];330U [dy0*dwidth+x-x0] = u[xs];331V [dy0*dwidth+x-x0] = v[xs];332if (unlikely(has_Nxyz)) {333Nx[dy0*dwidth+x-x0] = nx[xs];334Ny[dy0*dwidth+x-x0] = ny[xs];335Nz[dy0*dwidth+x-x0] = nz[xs];336}337}338return true;339}340341template<typename Eval, typename Patch>342void feature_adaptive_eval_grid (const Patch& patch, unsigned subPatch, const float levels[4],343const unsigned x0, const unsigned x1, const unsigned y0, const unsigned y1, const unsigned swidth, const unsigned sheight,344float* Px, float* Py, float* Pz, float* U, float* V, float* Nx, float* Ny, float* Nz, const unsigned dwidth, const unsigned dheight)345{346bool sl = false, sr = false, st = false, sb = false;347if (levels) {348sl = x0 == 0 && stitch_col<Eval,Patch>(patch,subPatch,0,y0,y1,sheight-1,int(levels[3]), Px,Py,Pz,U,V,Nx,Ny,Nz, 0 ,dwidth,dheight);349sr = x1 == swidth-1 && stitch_col<Eval,Patch>(patch,subPatch,1,y0,y1,sheight-1,int(levels[1]), Px,Py,Pz,U,V,Nx,Ny,Nz, x1-x0,dwidth,dheight);350st = y0 == 0 && stitch_row<Eval,Patch>(patch,subPatch,0,x0,x1,swidth-1,int(levels[0]), Px,Py,Pz,U,V,Nx,Ny,Nz, 0 ,dwidth,dheight);351sb = y1 == sheight-1 && stitch_row<Eval,Patch>(patch,subPatch,1,x0,x1,swidth-1,int(levels[2]), Px,Py,Pz,U,V,Nx,Ny,Nz, y1-y0,dwidth,dheight);352}353const unsigned ofs = st*dwidth+sl;354Eval(patch,subPatch,x0+sl,x1-sr,y0+st,y1-sb, swidth,sheight, Px+ofs,Py+ofs,Pz+ofs,U+ofs,V+ofs,Nx?Nx+ofs:nullptr,Ny?Ny+ofs:nullptr,Nz?Nz+ofs:nullptr, dwidth,dheight);355}356}357}358359360361