1 | // Copyright 2009-2021 Intel Corporation |
2 | // SPDX-License-Identifier: Apache-2.0 |
3 | |
4 | #pragma once |
5 | |
6 | #include "default.h" |
7 | #include "geometry.h" |
8 | #include "buffer.h" |
9 | |
10 | #include "../subdiv/bezier_curve.h" |
11 | #include "../subdiv/hermite_curve.h" |
12 | #include "../subdiv/bspline_curve.h" |
13 | #include "../subdiv/catmullrom_curve.h" |
14 | #include "../subdiv/linear_bezier_patch.h" |
15 | |
16 | namespace embree |
17 | { |
18 | /*! represents an array of bicubic bezier curves */ |
19 | struct CurveGeometry : public Geometry |
20 | { |
21 | /*! type of this geometry */ |
22 | static const Geometry::GTypeMask geom_type = Geometry::MTY_CURVE4; |
23 | |
24 | public: |
25 | |
26 | /*! bezier curve construction */ |
27 | CurveGeometry (Device* device, Geometry::GType gtype); |
28 | |
29 | public: |
30 | void setMask(unsigned mask); |
31 | void setNumTimeSteps (unsigned int numTimeSteps); |
32 | void setVertexAttributeCount (unsigned int N); |
33 | void setBuffer(RTCBufferType type, unsigned int slot, RTCFormat format, const Ref<Buffer>& buffer, size_t offset, size_t stride, unsigned int num); |
34 | void* getBuffer(RTCBufferType type, unsigned int slot); |
35 | void updateBuffer(RTCBufferType type, unsigned int slot); |
36 | void commit(); |
37 | bool verify(); |
38 | void setTessellationRate(float N); |
39 | void setMaxRadiusScale(float s); |
40 | void addElementsToCount (GeometryCounts & counts) const; |
41 | |
42 | public: |
43 | |
44 | /*! returns the number of vertices */ |
45 | __forceinline size_t numVertices() const { |
46 | return vertices[0].size(); |
47 | } |
48 | |
49 | /*! returns the i'th curve */ |
50 | __forceinline const unsigned int& curve(size_t i) const { |
51 | return curves[i]; |
52 | } |
53 | |
54 | /*! returns i'th vertex of the first time step */ |
55 | __forceinline Vec3ff vertex(size_t i) const { |
56 | return vertices0[i]; |
57 | } |
58 | |
59 | /*! returns i'th normal of the first time step */ |
60 | __forceinline Vec3fa normal(size_t i) const { |
61 | return normals0[i]; |
62 | } |
63 | |
64 | /*! returns i'th tangent of the first time step */ |
65 | __forceinline Vec3ff tangent(size_t i) const { |
66 | return tangents0[i]; |
67 | } |
68 | |
69 | /*! returns i'th normal derivative of the first time step */ |
70 | __forceinline Vec3fa dnormal(size_t i) const { |
71 | return dnormals0[i]; |
72 | } |
73 | |
74 | /*! returns i'th radius of the first time step */ |
75 | __forceinline float radius(size_t i) const { |
76 | return vertices0[i].w; |
77 | } |
78 | |
79 | /*! returns i'th vertex of itime'th timestep */ |
80 | __forceinline Vec3ff vertex(size_t i, size_t itime) const { |
81 | return vertices[itime][i]; |
82 | } |
83 | |
84 | /*! returns i'th normal of itime'th timestep */ |
85 | __forceinline Vec3fa normal(size_t i, size_t itime) const { |
86 | return normals[itime][i]; |
87 | } |
88 | |
89 | /*! returns i'th tangent of itime'th timestep */ |
90 | __forceinline Vec3ff tangent(size_t i, size_t itime) const { |
91 | return tangents[itime][i]; |
92 | } |
93 | |
94 | /*! returns i'th normal derivative of itime'th timestep */ |
95 | __forceinline Vec3fa dnormal(size_t i, size_t itime) const { |
96 | return dnormals[itime][i]; |
97 | } |
98 | |
99 | /*! returns i'th radius of itime'th timestep */ |
100 | __forceinline float radius(size_t i, size_t itime) const { |
101 | return vertices[itime][i].w; |
102 | } |
103 | |
104 | /*! gathers the curve starting with i'th vertex */ |
105 | __forceinline void gather(Vec3ff& p0, Vec3ff& p1, Vec3ff& p2, Vec3ff& p3, size_t i) const |
106 | { |
107 | p0 = vertex(i+0); |
108 | p1 = vertex(i+1); |
109 | p2 = vertex(i+2); |
110 | p3 = vertex(i+3); |
111 | } |
112 | |
113 | /*! gathers the curve starting with i'th vertex of itime'th timestep */ |
114 | __forceinline void gather(Vec3ff& p0, Vec3ff& p1, Vec3ff& p2, Vec3ff& p3, size_t i, size_t itime) const |
115 | { |
116 | p0 = vertex(i+0,itime); |
117 | p1 = vertex(i+1,itime); |
118 | p2 = vertex(i+2,itime); |
119 | p3 = vertex(i+3,itime); |
120 | } |
121 | |
122 | /*! gathers the curve starting with i'th vertex */ |
123 | __forceinline void gather(Vec3ff& p0, Vec3ff& p1, Vec3ff& p2, Vec3ff& p3, Vec3fa& n0, Vec3fa& n1, Vec3fa& n2, Vec3fa& n3, size_t i) const |
124 | { |
125 | p0 = vertex(i+0); |
126 | p1 = vertex(i+1); |
127 | p2 = vertex(i+2); |
128 | p3 = vertex(i+3); |
129 | n0 = normal(i+0); |
130 | n1 = normal(i+1); |
131 | n2 = normal(i+2); |
132 | n3 = normal(i+3); |
133 | } |
134 | |
135 | /*! gathers the curve starting with i'th vertex of itime'th timestep */ |
136 | __forceinline void gather(Vec3ff& p0, Vec3ff& p1, Vec3ff& p2, Vec3ff& p3, Vec3fa& n0, Vec3fa& n1, Vec3fa& n2, Vec3fa& n3, size_t i, size_t itime) const |
137 | { |
138 | p0 = vertex(i+0,itime); |
139 | p1 = vertex(i+1,itime); |
140 | p2 = vertex(i+2,itime); |
141 | p3 = vertex(i+3,itime); |
142 | n0 = normal(i+0,itime); |
143 | n1 = normal(i+1,itime); |
144 | n2 = normal(i+2,itime); |
145 | n3 = normal(i+3,itime); |
146 | } |
147 | |
148 | /*! prefetches the curve starting with i'th vertex of itime'th timestep */ |
149 | __forceinline void prefetchL1_vertices(size_t i) const |
150 | { |
151 | prefetchL1(vertices0.getPtr(i)+0); |
152 | prefetchL1(vertices0.getPtr(i)+64); |
153 | } |
154 | |
155 | /*! prefetches the curve starting with i'th vertex of itime'th timestep */ |
156 | __forceinline void prefetchL2_vertices(size_t i) const |
157 | { |
158 | prefetchL2(vertices0.getPtr(i)+0); |
159 | prefetchL2(vertices0.getPtr(i)+64); |
160 | } |
161 | |
162 | /*! loads curve vertices for specified time */ |
163 | __forceinline void gather(Vec3ff& p0, Vec3ff& p1, Vec3ff& p2, Vec3ff& p3, size_t i, float time) const |
164 | { |
165 | float ftime; |
166 | const size_t itime = timeSegment(time, ftime); |
167 | |
168 | const float t0 = 1.0f - ftime; |
169 | const float t1 = ftime; |
170 | Vec3ff a0,a1,a2,a3; |
171 | gather(a0,a1,a2,a3,i,itime); |
172 | Vec3ff b0,b1,b2,b3; |
173 | gather(b0,b1,b2,b3,i,itime+1); |
174 | p0 = madd(Vec3ff(t0),a0,t1*b0); |
175 | p1 = madd(Vec3ff(t0),a1,t1*b1); |
176 | p2 = madd(Vec3ff(t0),a2,t1*b2); |
177 | p3 = madd(Vec3ff(t0),a3,t1*b3); |
178 | } |
179 | |
180 | /*! loads curve vertices for specified time */ |
181 | __forceinline void gather(Vec3ff& p0, Vec3ff& p1, Vec3ff& p2, Vec3ff& p3, Vec3fa& n0, Vec3fa& n1, Vec3fa& n2, Vec3fa& n3, size_t i, float time) const |
182 | { |
183 | float ftime; |
184 | const size_t itime = timeSegment(time, ftime); |
185 | |
186 | const float t0 = 1.0f - ftime; |
187 | const float t1 = ftime; |
188 | Vec3ff a0,a1,a2,a3; Vec3fa an0,an1,an2,an3; |
189 | gather(a0,a1,a2,a3,an0,an1,an2,an3,i,itime); |
190 | Vec3ff b0,b1,b2,b3; Vec3fa bn0,bn1,bn2,bn3; |
191 | gather(b0,b1,b2,b3,bn0,bn1,bn2,bn3,i,itime+1); |
192 | p0 = madd(Vec3ff(t0),a0,t1*b0); |
193 | p1 = madd(Vec3ff(t0),a1,t1*b1); |
194 | p2 = madd(Vec3ff(t0),a2,t1*b2); |
195 | p3 = madd(Vec3ff(t0),a3,t1*b3); |
196 | n0 = madd(Vec3ff(t0),an0,t1*bn0); |
197 | n1 = madd(Vec3ff(t0),an1,t1*bn1); |
198 | n2 = madd(Vec3ff(t0),an2,t1*bn2); |
199 | n3 = madd(Vec3ff(t0),an3,t1*bn3); |
200 | } |
201 | |
202 | template<typename SourceCurve3ff, typename SourceCurve3fa, typename TensorLinearCubicBezierSurface3fa> |
203 | __forceinline TensorLinearCubicBezierSurface3fa getNormalOrientedCurve(IntersectContext* context, const Vec3fa& ray_org, const unsigned int primID, const size_t itime) const |
204 | { |
205 | Vec3ff v0,v1,v2,v3; Vec3fa n0,n1,n2,n3; |
206 | unsigned int vertexID = curve(primID); |
207 | gather(v0,v1,v2,v3,n0,n1,n2,n3,vertexID,itime); |
208 | SourceCurve3ff ccurve(v0,v1,v2,v3); |
209 | SourceCurve3fa ncurve(n0,n1,n2,n3); |
210 | ccurve = enlargeRadiusToMinWidth(context,this,ray_org,ccurve); |
211 | return TensorLinearCubicBezierSurface3fa::fromCenterAndNormalCurve(ccurve,ncurve); |
212 | } |
213 | |
214 | template<typename SourceCurve3ff, typename SourceCurve3fa, typename TensorLinearCubicBezierSurface3fa> |
215 | __forceinline TensorLinearCubicBezierSurface3fa getNormalOrientedCurve(IntersectContext* context, const Vec3fa& ray_org, const unsigned int primID, const float time) const |
216 | { |
217 | float ftime; |
218 | const size_t itime = timeSegment(time, ftime); |
219 | const TensorLinearCubicBezierSurface3fa curve0 = getNormalOrientedCurve<SourceCurve3ff, SourceCurve3fa, TensorLinearCubicBezierSurface3fa>(context,ray_org,primID,itime+0); |
220 | const TensorLinearCubicBezierSurface3fa curve1 = getNormalOrientedCurve<SourceCurve3ff, SourceCurve3fa, TensorLinearCubicBezierSurface3fa>(context,ray_org,primID,itime+1); |
221 | return clerp(curve0,curve1,ftime); |
222 | } |
223 | |
224 | /*! gathers the hermite curve starting with i'th vertex */ |
225 | __forceinline void gather_hermite(Vec3ff& p0, Vec3ff& t0, Vec3ff& p1, Vec3ff& t1, size_t i) const |
226 | { |
227 | p0 = vertex (i+0); |
228 | p1 = vertex (i+1); |
229 | t0 = tangent(i+0); |
230 | t1 = tangent(i+1); |
231 | } |
232 | |
233 | /*! gathers the hermite curve starting with i'th vertex of itime'th timestep */ |
234 | __forceinline void gather_hermite(Vec3ff& p0, Vec3ff& t0, Vec3ff& p1, Vec3ff& t1, size_t i, size_t itime) const |
235 | { |
236 | p0 = vertex (i+0,itime); |
237 | p1 = vertex (i+1,itime); |
238 | t0 = tangent(i+0,itime); |
239 | t1 = tangent(i+1,itime); |
240 | } |
241 | |
242 | /*! loads curve vertices for specified time */ |
243 | __forceinline void gather_hermite(Vec3ff& p0, Vec3ff& t0, Vec3ff& p1, Vec3ff& t1, size_t i, float time) const |
244 | { |
245 | float ftime; |
246 | const size_t itime = timeSegment(time, ftime); |
247 | const float f0 = 1.0f - ftime, f1 = ftime; |
248 | Vec3ff ap0,at0,ap1,at1; |
249 | gather_hermite(ap0,at0,ap1,at1,i,itime); |
250 | Vec3ff bp0,bt0,bp1,bt1; |
251 | gather_hermite(bp0,bt0,bp1,bt1,i,itime+1); |
252 | p0 = madd(Vec3ff(f0),ap0,f1*bp0); |
253 | t0 = madd(Vec3ff(f0),at0,f1*bt0); |
254 | p1 = madd(Vec3ff(f0),ap1,f1*bp1); |
255 | t1 = madd(Vec3ff(f0),at1,f1*bt1); |
256 | } |
257 | |
258 | /*! gathers the hermite curve starting with i'th vertex */ |
259 | __forceinline void gather_hermite(Vec3ff& p0, Vec3ff& t0, Vec3fa& n0, Vec3fa& dn0, Vec3ff& p1, Vec3ff& t1, Vec3fa& n1, Vec3fa& dn1, size_t i) const |
260 | { |
261 | p0 = vertex (i+0); |
262 | p1 = vertex (i+1); |
263 | t0 = tangent(i+0); |
264 | t1 = tangent(i+1); |
265 | n0 = normal(i+0); |
266 | n1 = normal(i+1); |
267 | dn0 = dnormal(i+0); |
268 | dn1 = dnormal(i+1); |
269 | } |
270 | |
271 | /*! gathers the hermite curve starting with i'th vertex of itime'th timestep */ |
272 | __forceinline void gather_hermite(Vec3ff& p0, Vec3ff& t0, Vec3fa& n0, Vec3fa& dn0, Vec3ff& p1, Vec3ff& t1, Vec3fa& n1, Vec3fa& dn1, size_t i, size_t itime) const |
273 | { |
274 | p0 = vertex (i+0,itime); |
275 | p1 = vertex (i+1,itime); |
276 | t0 = tangent(i+0,itime); |
277 | t1 = tangent(i+1,itime); |
278 | n0 = normal(i+0,itime); |
279 | n1 = normal(i+1,itime); |
280 | dn0 = dnormal(i+0,itime); |
281 | dn1 = dnormal(i+1,itime); |
282 | } |
283 | |
284 | /*! loads curve vertices for specified time */ |
285 | __forceinline void gather_hermite(Vec3ff& p0, Vec3fa& t0, Vec3fa& n0, Vec3fa& dn0, Vec3ff& p1, Vec3fa& t1, Vec3fa& n1, Vec3fa& dn1, size_t i, float time) const |
286 | { |
287 | float ftime; |
288 | const size_t itime = timeSegment(time, ftime); |
289 | const float f0 = 1.0f - ftime, f1 = ftime; |
290 | Vec3ff ap0,at0,ap1,at1; Vec3fa an0,adn0,an1,adn1; |
291 | gather_hermite(ap0,at0,an0,adn0,ap1,at1,an1,adn1,i,itime); |
292 | Vec3ff bp0,bt0,bp1,bt1; Vec3fa bn0,bdn0,bn1,bdn1; |
293 | gather_hermite(bp0,bt0,bn0,bdn0,bp1,bt1,bn1,bdn1,i,itime+1); |
294 | p0 = madd(Vec3ff(f0),ap0,f1*bp0); |
295 | t0 = madd(Vec3ff(f0),at0,f1*bt0); |
296 | n0 = madd(Vec3ff(f0),an0,f1*bn0); |
297 | dn0= madd(Vec3ff(f0),adn0,f1*bdn0); |
298 | p1 = madd(Vec3ff(f0),ap1,f1*bp1); |
299 | t1 = madd(Vec3ff(f0),at1,f1*bt1); |
300 | n1 = madd(Vec3ff(f0),an1,f1*bn1); |
301 | dn1= madd(Vec3ff(f0),adn1,f1*bdn1); |
302 | } |
303 | |
304 | template<typename SourceCurve3ff, typename SourceCurve3fa, typename TensorLinearCubicBezierSurface3fa> |
305 | __forceinline TensorLinearCubicBezierSurface3fa getNormalOrientedHermiteCurve(IntersectContext* context, const Vec3fa& ray_org, const unsigned int primID, const size_t itime) const |
306 | { |
307 | Vec3ff v0,t0,v1,t1; Vec3fa n0,dn0,n1,dn1; |
308 | unsigned int vertexID = curve(primID); |
309 | gather_hermite(v0,t0,n0,dn0,v1,t1,n1,dn1,vertexID,itime); |
310 | |
311 | SourceCurve3ff ccurve(v0,t0,v1,t1); |
312 | SourceCurve3fa ncurve(n0,dn0,n1,dn1); |
313 | ccurve = enlargeRadiusToMinWidth(context,this,ray_org,ccurve); |
314 | return TensorLinearCubicBezierSurface3fa::fromCenterAndNormalCurve(ccurve,ncurve); |
315 | } |
316 | |
317 | template<typename SourceCurve3ff, typename SourceCurve3fa, typename TensorLinearCubicBezierSurface3fa> |
318 | __forceinline TensorLinearCubicBezierSurface3fa getNormalOrientedHermiteCurve(IntersectContext* context, const Vec3fa& ray_org, const unsigned int primID, const float time) const |
319 | { |
320 | float ftime; |
321 | const size_t itime = timeSegment(time, ftime); |
322 | const TensorLinearCubicBezierSurface3fa curve0 = getNormalOrientedHermiteCurve<SourceCurve3ff, SourceCurve3fa, TensorLinearCubicBezierSurface3fa>(context, ray_org, primID,itime+0); |
323 | const TensorLinearCubicBezierSurface3fa curve1 = getNormalOrientedHermiteCurve<SourceCurve3ff, SourceCurve3fa, TensorLinearCubicBezierSurface3fa>(context, ray_org, primID,itime+1); |
324 | return clerp(curve0,curve1,ftime); |
325 | } |
326 | |
327 | private: |
328 | void resizeBuffers(unsigned int numSteps); |
329 | |
330 | public: |
331 | BufferView<unsigned int> curves; //!< array of curve indices |
332 | BufferView<Vec3ff> vertices0; //!< fast access to first vertex buffer |
333 | BufferView<Vec3fa> normals0; //!< fast access to first normal buffer |
334 | BufferView<Vec3ff> tangents0; //!< fast access to first tangent buffer |
335 | BufferView<Vec3fa> dnormals0; //!< fast access to first normal derivative buffer |
336 | vector<BufferView<Vec3ff>> vertices; //!< vertex array for each timestep |
337 | vector<BufferView<Vec3fa>> normals; //!< normal array for each timestep |
338 | vector<BufferView<Vec3ff>> tangents; //!< tangent array for each timestep |
339 | vector<BufferView<Vec3fa>> dnormals; //!< normal derivative array for each timestep |
340 | BufferView<char> flags; //!< start, end flag per segment |
341 | vector<BufferView<char>> vertexAttribs; //!< user buffers |
342 | int tessellationRate; //!< tessellation rate for flat curve |
343 | float maxRadiusScale = 1.0; //!< maximal min-width scaling of curve radii |
344 | }; |
345 | |
346 | namespace isa |
347 | { |
348 | |
349 | template<template<typename Ty> class Curve> |
350 | struct CurveGeometryInterface : public CurveGeometry |
351 | { |
352 | typedef Curve<Vec3ff> Curve3ff; |
353 | typedef Curve<Vec3fa> Curve3fa; |
354 | |
355 | CurveGeometryInterface (Device* device, Geometry::GType gtype) |
356 | : CurveGeometry(device,gtype) {} |
357 | |
358 | __forceinline const Curve3ff getCurveScaledRadius(size_t i, size_t itime = 0) const |
359 | { |
360 | const unsigned int index = curve(i); |
361 | Vec3ff v0 = vertex(index+0,itime); |
362 | Vec3ff v1 = vertex(index+1,itime); |
363 | Vec3ff v2 = vertex(index+2,itime); |
364 | Vec3ff v3 = vertex(index+3,itime); |
365 | v0.w *= maxRadiusScale; |
366 | v1.w *= maxRadiusScale; |
367 | v2.w *= maxRadiusScale; |
368 | v3.w *= maxRadiusScale; |
369 | return Curve3ff (v0,v1,v2,v3); |
370 | } |
371 | |
372 | __forceinline const Curve3ff getCurveScaledRadius(const LinearSpace3fa& space, size_t i, size_t itime = 0) const |
373 | { |
374 | const unsigned int index = curve(i); |
375 | const Vec3ff v0 = vertex(index+0,itime); |
376 | const Vec3ff v1 = vertex(index+1,itime); |
377 | const Vec3ff v2 = vertex(index+2,itime); |
378 | const Vec3ff v3 = vertex(index+3,itime); |
379 | const Vec3ff w0(xfmPoint(space,(Vec3fa)v0), maxRadiusScale*v0.w); |
380 | const Vec3ff w1(xfmPoint(space,(Vec3fa)v1), maxRadiusScale*v1.w); |
381 | const Vec3ff w2(xfmPoint(space,(Vec3fa)v2), maxRadiusScale*v2.w); |
382 | const Vec3ff w3(xfmPoint(space,(Vec3fa)v3), maxRadiusScale*v3.w); |
383 | return Curve3ff(w0,w1,w2,w3); |
384 | } |
385 | |
386 | __forceinline const Curve3ff getCurveScaledRadius(const Vec3fa& ofs, const float scale, const float r_scale0, const LinearSpace3fa& space, size_t i, size_t itime = 0) const |
387 | { |
388 | const float r_scale = r_scale0*scale; |
389 | const unsigned int index = curve(i); |
390 | const Vec3ff v0 = vertex(index+0,itime); |
391 | const Vec3ff v1 = vertex(index+1,itime); |
392 | const Vec3ff v2 = vertex(index+2,itime); |
393 | const Vec3ff v3 = vertex(index+3,itime); |
394 | const Vec3ff w0(xfmPoint(space,((Vec3fa)v0-ofs)*Vec3fa(scale)), maxRadiusScale*v0.w*r_scale); |
395 | const Vec3ff w1(xfmPoint(space,((Vec3fa)v1-ofs)*Vec3fa(scale)), maxRadiusScale*v1.w*r_scale); |
396 | const Vec3ff w2(xfmPoint(space,((Vec3fa)v2-ofs)*Vec3fa(scale)), maxRadiusScale*v2.w*r_scale); |
397 | const Vec3ff w3(xfmPoint(space,((Vec3fa)v3-ofs)*Vec3fa(scale)), maxRadiusScale*v3.w*r_scale); |
398 | return Curve3ff(w0,w1,w2,w3); |
399 | } |
400 | |
401 | __forceinline const Curve3fa getNormalCurve(size_t i, size_t itime = 0) const |
402 | { |
403 | const unsigned int index = curve(i); |
404 | const Vec3fa n0 = normal(index+0,itime); |
405 | const Vec3fa n1 = normal(index+1,itime); |
406 | const Vec3fa n2 = normal(index+2,itime); |
407 | const Vec3fa n3 = normal(index+3,itime); |
408 | return Curve3fa (n0,n1,n2,n3); |
409 | } |
410 | |
411 | __forceinline const TensorLinearCubicBezierSurface3fa getOrientedCurveScaledRadius(size_t i, size_t itime = 0) const |
412 | { |
413 | const Curve3ff center = getCurveScaledRadius(i,itime); |
414 | const Curve3fa normal = getNormalCurve(i,itime); |
415 | const TensorLinearCubicBezierSurface3fa ocurve = TensorLinearCubicBezierSurface3fa::fromCenterAndNormalCurve(center,normal); |
416 | return ocurve; |
417 | } |
418 | |
419 | __forceinline const TensorLinearCubicBezierSurface3fa getOrientedCurveScaledRadius(const LinearSpace3fa& space, size_t i, size_t itime = 0) const { |
420 | return getOrientedCurveScaledRadius(i,itime).xfm(space); |
421 | } |
422 | |
423 | __forceinline const TensorLinearCubicBezierSurface3fa getOrientedCurveScaledRadius(const Vec3fa& ofs, const float scale, const LinearSpace3fa& space, size_t i, size_t itime = 0) const { |
424 | return getOrientedCurveScaledRadius(i,itime).xfm(space,ofs,scale); |
425 | } |
426 | |
427 | /*! check if the i'th primitive is valid at the itime'th time step */ |
428 | __forceinline bool valid(Geometry::GType ctype, size_t i, const range<size_t>& itime_range) const |
429 | { |
430 | const unsigned int index = curve(i); |
431 | if (index+3 >= numVertices()) return false; |
432 | |
433 | for (size_t itime = itime_range.begin(); itime <= itime_range.end(); itime++) |
434 | { |
435 | const float r0 = radius(index+0,itime); |
436 | const float r1 = radius(index+1,itime); |
437 | const float r2 = radius(index+2,itime); |
438 | const float r3 = radius(index+3,itime); |
439 | if (!isvalid(r0) || !isvalid(r1) || !isvalid(r2) || !isvalid(r3)) |
440 | return false; |
441 | |
442 | const Vec3fa v0 = vertex(index+0,itime); |
443 | const Vec3fa v1 = vertex(index+1,itime); |
444 | const Vec3fa v2 = vertex(index+2,itime); |
445 | const Vec3fa v3 = vertex(index+3,itime); |
446 | if (!isvalid(v0) || !isvalid(v1) || !isvalid(v2) || !isvalid(v3)) |
447 | return false; |
448 | |
449 | if (ctype == Geometry::GTY_SUBTYPE_ORIENTED_CURVE) |
450 | { |
451 | const Vec3fa n0 = normal(index+0,itime); |
452 | const Vec3fa n1 = normal(index+1,itime); |
453 | if (!isvalid(n0) || !isvalid(n1)) |
454 | return false; |
455 | |
456 | const BBox3fa b = getOrientedCurveScaledRadius(i,itime).accurateBounds(); |
457 | if (!isvalid(b)) |
458 | return false; |
459 | } |
460 | } |
461 | |
462 | return true; |
463 | } |
464 | |
465 | template<int N> |
466 | void interpolate_impl(const RTCInterpolateArguments* const args) |
467 | { |
468 | unsigned int primID = args->primID; |
469 | float u = args->u; |
470 | RTCBufferType bufferType = args->bufferType; |
471 | unsigned int bufferSlot = args->bufferSlot; |
472 | float* P = args->P; |
473 | float* dPdu = args->dPdu; |
474 | float* ddPdudu = args->ddPdudu; |
475 | unsigned int valueCount = args->valueCount; |
476 | |
477 | /* calculate base pointer and stride */ |
478 | assert((bufferType == RTC_BUFFER_TYPE_VERTEX && bufferSlot < numTimeSteps) || |
479 | (bufferType == RTC_BUFFER_TYPE_VERTEX_ATTRIBUTE && bufferSlot <= vertexAttribs.size())); |
480 | const char* src = nullptr; |
481 | size_t stride = 0; |
482 | if (bufferType == RTC_BUFFER_TYPE_VERTEX_ATTRIBUTE) { |
483 | src = vertexAttribs[bufferSlot].getPtr(); |
484 | stride = vertexAttribs[bufferSlot].getStride(); |
485 | } else { |
486 | src = vertices[bufferSlot].getPtr(); |
487 | stride = vertices[bufferSlot].getStride(); |
488 | } |
489 | |
490 | for (unsigned int i=0; i<valueCount; i+=N) |
491 | { |
492 | size_t ofs = i*sizeof(float); |
493 | const size_t index = curves[primID]; |
494 | const vbool<N> valid = vint<N>((int)i)+vint<N>(step) < vint<N>((int)valueCount); |
495 | const vfloat<N> p0 = mem<vfloat<N>>::loadu(valid,(float*)&src[(index+0)*stride+ofs]); |
496 | const vfloat<N> p1 = mem<vfloat<N>>::loadu(valid,(float*)&src[(index+1)*stride+ofs]); |
497 | const vfloat<N> p2 = mem<vfloat<N>>::loadu(valid,(float*)&src[(index+2)*stride+ofs]); |
498 | const vfloat<N> p3 = mem<vfloat<N>>::loadu(valid,(float*)&src[(index+3)*stride+ofs]); |
499 | |
500 | const Curve<vfloat<N>> curve(p0,p1,p2,p3); |
501 | if (P ) mem<vfloat<N>>::storeu(valid,P+i, curve.eval(u)); |
502 | if (dPdu ) mem<vfloat<N>>::storeu(valid,dPdu+i, curve.eval_du(u)); |
503 | if (ddPdudu) mem<vfloat<N>>::storeu(valid,ddPdudu+i,curve.eval_dudu(u)); |
504 | } |
505 | } |
506 | |
507 | void interpolate(const RTCInterpolateArguments* const args) { |
508 | interpolate_impl<4>(args); |
509 | } |
510 | }; |
511 | |
512 | template<template<typename Ty> class Curve> |
513 | struct HermiteCurveGeometryInterface : public CurveGeometry |
514 | { |
515 | typedef Curve<Vec3ff> HermiteCurve3ff; |
516 | typedef Curve<Vec3fa> HermiteCurve3fa; |
517 | |
518 | HermiteCurveGeometryInterface (Device* device, Geometry::GType gtype) |
519 | : CurveGeometry(device,gtype) {} |
520 | |
521 | __forceinline const HermiteCurve3ff getCurveScaledRadius(size_t i, size_t itime = 0) const |
522 | { |
523 | const unsigned int index = curve(i); |
524 | Vec3ff v0 = vertex(index+0,itime); |
525 | Vec3ff v1 = vertex(index+1,itime); |
526 | Vec3ff t0 = tangent(index+0,itime); |
527 | Vec3ff t1 = tangent(index+1,itime); |
528 | v0.w *= maxRadiusScale; |
529 | v1.w *= maxRadiusScale; |
530 | t0.w *= maxRadiusScale; |
531 | t1.w *= maxRadiusScale; |
532 | return HermiteCurve3ff (v0,t0,v1,t1); |
533 | } |
534 | |
535 | __forceinline const HermiteCurve3ff getCurveScaledRadius(const LinearSpace3fa& space, size_t i, size_t itime = 0) const |
536 | { |
537 | const unsigned int index = curve(i); |
538 | const Vec3ff v0 = vertex(index+0,itime); |
539 | const Vec3ff v1 = vertex(index+1,itime); |
540 | const Vec3ff t0 = tangent(index+0,itime); |
541 | const Vec3ff t1 = tangent(index+1,itime); |
542 | const Vec3ff V0(xfmPoint(space,(Vec3fa)v0),maxRadiusScale*v0.w); |
543 | const Vec3ff V1(xfmPoint(space,(Vec3fa)v1),maxRadiusScale*v1.w); |
544 | const Vec3ff T0(xfmVector(space,(Vec3fa)t0),maxRadiusScale*t0.w); |
545 | const Vec3ff T1(xfmVector(space,(Vec3fa)t1),maxRadiusScale*t1.w); |
546 | return HermiteCurve3ff(V0,T0,V1,T1); |
547 | } |
548 | |
549 | __forceinline const HermiteCurve3ff getCurveScaledRadius(const Vec3fa& ofs, const float scale, const float r_scale0, const LinearSpace3fa& space, size_t i, size_t itime = 0) const |
550 | { |
551 | const float r_scale = r_scale0*scale; |
552 | const unsigned int index = curve(i); |
553 | const Vec3ff v0 = vertex(index+0,itime); |
554 | const Vec3ff v1 = vertex(index+1,itime); |
555 | const Vec3ff t0 = tangent(index+0,itime); |
556 | const Vec3ff t1 = tangent(index+1,itime); |
557 | const Vec3ff V0(xfmPoint(space,(v0-ofs)*Vec3fa(scale)), maxRadiusScale*v0.w*r_scale); |
558 | const Vec3ff V1(xfmPoint(space,(v1-ofs)*Vec3fa(scale)), maxRadiusScale*v1.w*r_scale); |
559 | const Vec3ff T0(xfmVector(space,t0*Vec3fa(scale)), maxRadiusScale*t0.w*r_scale); |
560 | const Vec3ff T1(xfmVector(space,t1*Vec3fa(scale)), maxRadiusScale*t1.w*r_scale); |
561 | return HermiteCurve3ff(V0,T0,V1,T1); |
562 | } |
563 | |
564 | __forceinline const HermiteCurve3fa getNormalCurve(size_t i, size_t itime = 0) const |
565 | { |
566 | const unsigned int index = curve(i); |
567 | const Vec3fa n0 = normal(index+0,itime); |
568 | const Vec3fa n1 = normal(index+1,itime); |
569 | const Vec3fa dn0 = dnormal(index+0,itime); |
570 | const Vec3fa dn1 = dnormal(index+1,itime); |
571 | return HermiteCurve3fa (n0,dn0,n1,dn1); |
572 | } |
573 | |
574 | __forceinline const TensorLinearCubicBezierSurface3fa getOrientedCurveScaledRadius(size_t i, size_t itime = 0) const |
575 | { |
576 | const HermiteCurve3ff center = getCurveScaledRadius(i,itime); |
577 | const HermiteCurve3fa normal = getNormalCurve(i,itime); |
578 | const TensorLinearCubicBezierSurface3fa ocurve = TensorLinearCubicBezierSurface3fa::fromCenterAndNormalCurve(center,normal); |
579 | return ocurve; |
580 | } |
581 | |
582 | __forceinline const TensorLinearCubicBezierSurface3fa getOrientedCurveScaledRadius(const LinearSpace3fa& space, size_t i, size_t itime = 0) const { |
583 | return getOrientedCurveScaledRadius(i,itime).xfm(space); |
584 | } |
585 | |
586 | __forceinline const TensorLinearCubicBezierSurface3fa getOrientedCurveScaledRadius(const Vec3fa& ofs, const float scale, const LinearSpace3fa& space, size_t i, size_t itime = 0) const { |
587 | return getOrientedCurveScaledRadius(i,itime).xfm(space,ofs,scale); |
588 | } |
589 | |
590 | /*! check if the i'th primitive is valid at the itime'th time step */ |
591 | __forceinline bool valid(Geometry::GType ctype, size_t i, const range<size_t>& itime_range) const |
592 | { |
593 | const unsigned int index = curve(i); |
594 | if (index+1 >= numVertices()) return false; |
595 | |
596 | for (size_t itime = itime_range.begin(); itime <= itime_range.end(); itime++) |
597 | { |
598 | const Vec3ff v0 = vertex(index+0,itime); |
599 | const Vec3ff v1 = vertex(index+1,itime); |
600 | if (!isvalid4(v0) || !isvalid4(v1)) |
601 | return false; |
602 | |
603 | const Vec3ff t0 = tangent(index+0,itime); |
604 | const Vec3ff t1 = tangent(index+1,itime); |
605 | if (!isvalid4(t0) || !isvalid4(t1)) |
606 | return false; |
607 | |
608 | if (ctype == Geometry::GTY_SUBTYPE_ORIENTED_CURVE) |
609 | { |
610 | const Vec3fa n0 = normal(index+0,itime); |
611 | const Vec3fa n1 = normal(index+1,itime); |
612 | if (!isvalid(n0) || !isvalid(n1)) |
613 | return false; |
614 | |
615 | const Vec3fa dn0 = dnormal(index+0,itime); |
616 | const Vec3fa dn1 = dnormal(index+1,itime); |
617 | if (!isvalid(dn0) || !isvalid(dn1)) |
618 | return false; |
619 | |
620 | const BBox3fa b = getOrientedCurveScaledRadius(i,itime).accurateBounds(); |
621 | if (!isvalid(b)) |
622 | return false; |
623 | } |
624 | } |
625 | |
626 | return true; |
627 | } |
628 | |
629 | template<int N> |
630 | void interpolate_impl(const RTCInterpolateArguments* const args) |
631 | { |
632 | unsigned int primID = args->primID; |
633 | float u = args->u; |
634 | RTCBufferType bufferType = args->bufferType; |
635 | unsigned int bufferSlot = args->bufferSlot; |
636 | float* P = args->P; |
637 | float* dPdu = args->dPdu; |
638 | float* ddPdudu = args->ddPdudu; |
639 | unsigned int valueCount = args->valueCount; |
640 | |
641 | /* we interpolate vertex attributes linearly for hermite basis */ |
642 | if (bufferType == RTC_BUFFER_TYPE_VERTEX_ATTRIBUTE) |
643 | { |
644 | assert(bufferSlot <= vertexAttribs.size()); |
645 | const char* vsrc = vertexAttribs[bufferSlot].getPtr(); |
646 | const size_t vstride = vertexAttribs[bufferSlot].getStride(); |
647 | |
648 | for (unsigned int i=0; i<valueCount; i+=N) |
649 | { |
650 | const size_t ofs = i*sizeof(float); |
651 | const size_t index = curves[primID]; |
652 | const vbool<N> valid = vint<N>((int)i)+vint<N>(step) < vint<N>((int)valueCount); |
653 | const vfloat<N> p0 = mem<vfloat<N>>::loadu(valid,(float*)&vsrc[(index+0)*vstride+ofs]); |
654 | const vfloat<N> p1 = mem<vfloat<N>>::loadu(valid,(float*)&vsrc[(index+1)*vstride+ofs]); |
655 | |
656 | if (P ) mem<vfloat<N>>::storeu(valid,P+i, madd(1.0f-u,p0,u*p1)); |
657 | if (dPdu ) mem<vfloat<N>>::storeu(valid,dPdu+i, p1-p0); |
658 | if (ddPdudu) mem<vfloat<N>>::storeu(valid,ddPdudu+i,vfloat<N>(zero)); |
659 | } |
660 | } |
661 | |
662 | /* interpolation for vertex buffers */ |
663 | else |
664 | { |
665 | assert(bufferSlot < numTimeSteps); |
666 | const char* vsrc = vertices[bufferSlot].getPtr(); |
667 | const char* tsrc = tangents[bufferSlot].getPtr(); |
668 | const size_t vstride = vertices[bufferSlot].getStride(); |
669 | const size_t tstride = vertices[bufferSlot].getStride(); |
670 | |
671 | for (unsigned int i=0; i<valueCount; i+=N) |
672 | { |
673 | const size_t ofs = i*sizeof(float); |
674 | const size_t index = curves[primID]; |
675 | const vbool<N> valid = vint<N>((int)i)+vint<N>(step) < vint<N>((int)valueCount); |
676 | const vfloat<N> p0 = mem<vfloat<N>>::loadu(valid,(float*)&vsrc[(index+0)*vstride+ofs]); |
677 | const vfloat<N> p1 = mem<vfloat<N>>::loadu(valid,(float*)&vsrc[(index+1)*vstride+ofs]); |
678 | const vfloat<N> t0 = mem<vfloat<N>>::loadu(valid,(float*)&tsrc[(index+0)*tstride+ofs]); |
679 | const vfloat<N> t1 = mem<vfloat<N>>::loadu(valid,(float*)&tsrc[(index+1)*tstride+ofs]); |
680 | |
681 | const HermiteCurveT<vfloat<N>> curve(p0,t0,p1,t1); |
682 | if (P ) mem<vfloat<N>>::storeu(valid,P+i, curve.eval(u)); |
683 | if (dPdu ) mem<vfloat<N>>::storeu(valid,dPdu+i, curve.eval_du(u)); |
684 | if (ddPdudu) mem<vfloat<N>>::storeu(valid,ddPdudu+i,curve.eval_dudu(u)); |
685 | } |
686 | } |
687 | } |
688 | |
689 | void interpolate(const RTCInterpolateArguments* const args) { |
690 | interpolate_impl<4>(args); |
691 | } |
692 | }; |
693 | } |
694 | |
695 | DECLARE_ISA_FUNCTION(CurveGeometry*, createCurves, Device* COMMA Geometry::GType); |
696 | } |
697 | |