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