1// Copyright 2009-2021 Intel Corporation
2// SPDX-License-Identifier: Apache-2.0
3
4#pragma once
5
6#include "primitive.h"
7#include "curve_intersector_precalculations.h"
8
9namespace embree
10{
11 template<int M>
12 struct CurveNiMB
13 {
14 struct Type : public PrimitiveType {
15 const char* name() const;
16 size_t sizeActive(const char* This) const;
17 size_t sizeTotal(const char* This) const;
18 size_t getBytes(const char* This) const;
19 };
20 static Type type;
21
22 public:
23
24 /* Returns maximum number of stored primitives */
25 static __forceinline size_t max_size() { return M; }
26
27 /* Returns required number of primitive blocks for N primitives */
28 static __forceinline size_t blocks(size_t N) { return (N+M-1)/M; }
29
30 static __forceinline size_t bytes(size_t N)
31 {
32 const size_t f = N/M, r = N%M;
33 static_assert(sizeof(CurveNiMB) == 6+37*M+24, "internal data layout issue");
34 return f*sizeof(CurveNiMB) + (r!=0)*(6+37*r+24);
35 }
36
37 public:
38
39 /*! Default constructor. */
40 __forceinline CurveNiMB () {}
41
42 /*! fill curve from curve list */
43 __forceinline LBBox3fa fillMB(const PrimRefMB* prims, size_t& begin, size_t _end, Scene* scene, const BBox1f time_range)
44 {
45 size_t end = min(a: begin+M,b: _end);
46 N = (unsigned char)(end-begin);
47 const unsigned int geomID0 = prims[begin].geomID();
48 this->geomID(N) = geomID0;
49 ty = (unsigned char) scene->get(i: geomID0)->getType();
50
51 /* encode all primitives */
52 LBBox3fa lbounds = empty;
53 for (size_t i=0; i<N; i++)
54 {
55 const PrimRefMB& prim = prims[begin+i];
56 const unsigned int geomID = prim.geomID(); assert(geomID == geomID0);
57 const unsigned int primID = prim.primID();
58 lbounds.extend(other: scene->get(i: geomID)->vlinearBounds(primID,time_range));
59 }
60 BBox3fa bounds = lbounds.bounds();
61
62 /* calculate offset and scale */
63 Vec3fa loffset = bounds.lower;
64 float lscale = reduce_min(v: 256.0f/(bounds.size()*sqrt(x: 3.0f)));
65 if (bounds.size() == Vec3fa(zero)) lscale = 0.0f;
66 *this->offset(N) = loffset;
67 *this->scale(N) = lscale;
68 this->time_offset(N) = time_range.lower;
69 this->time_scale(N) = 1.0f/time_range.size();
70
71 /* encode all primitives */
72 for (size_t i=0; i<M && begin<end; i++, begin++)
73 {
74 const PrimRefMB& prim = prims[begin];
75 const unsigned int geomID = prim.geomID();
76 const unsigned int primID = prim.primID();
77 const LinearSpace3fa space2 = scene->get(i: geomID)->computeAlignedSpaceMB(primID,time_range);
78
79 const LinearSpace3fa space3(trunc(a: 126.0f*space2.vx),trunc(a: 126.0f*space2.vy),trunc(a: 126.0f*space2.vz));
80 const LBBox3fa bounds = scene->get(i: geomID)->vlinearBounds(ofs: loffset,scale: lscale,r_scale0: max(a: length(a: space3.vx),b: length(a: space3.vy),c: length(a: space3.vz)),space: space3.transposed(),primID,time_range);
81
82 // NOTE: this weird (char) (short) cast works around VS2015 Win32 compiler bug
83 bounds_vx_x(N)[i] = (char) (short) space3.vx.x;
84 bounds_vx_y(N)[i] = (char) (short) space3.vx.y;
85 bounds_vx_z(N)[i] = (char) (short) space3.vx.z;
86 bounds_vx_lower0(N)[i] = (short) clamp(x: floor(x: bounds.bounds0.lower.x),lower: -32767.0f,upper: 32767.0f);
87 bounds_vx_upper0(N)[i] = (short) clamp(x: ceil (x: bounds.bounds0.upper.x),lower: -32767.0f,upper: 32767.0f);
88 bounds_vx_lower1(N)[i] = (short) clamp(x: floor(x: bounds.bounds1.lower.x),lower: -32767.0f,upper: 32767.0f);
89 bounds_vx_upper1(N)[i] = (short) clamp(x: ceil (x: bounds.bounds1.upper.x),lower: -32767.0f,upper: 32767.0f);
90 assert(-32767.0f <= floor(bounds.bounds0.lower.x) && floor(bounds.bounds0.lower.x) <= 32767.0f);
91 assert(-32767.0f <= ceil (bounds.bounds0.upper.x) && ceil (bounds.bounds0.upper.x) <= 32767.0f);
92 assert(-32767.0f <= floor(bounds.bounds1.lower.x) && floor(bounds.bounds1.lower.x) <= 32767.0f);
93 assert(-32767.0f <= ceil (bounds.bounds1.upper.x) && ceil (bounds.bounds1.upper.x) <= 32767.0f);
94
95 bounds_vy_x(N)[i] = (char) (short) space3.vy.x;
96 bounds_vy_y(N)[i] = (char) (short) space3.vy.y;
97 bounds_vy_z(N)[i] = (char) (short) space3.vy.z;
98 bounds_vy_lower0(N)[i] = (short) clamp(x: floor(x: bounds.bounds0.lower.y),lower: -32767.0f,upper: 32767.0f);
99 bounds_vy_upper0(N)[i] = (short) clamp(x: ceil (x: bounds.bounds0.upper.y),lower: -32767.0f,upper: 32767.0f);
100 bounds_vy_lower1(N)[i] = (short) clamp(x: floor(x: bounds.bounds1.lower.y),lower: -32767.0f,upper: 32767.0f);
101 bounds_vy_upper1(N)[i] = (short) clamp(x: ceil (x: bounds.bounds1.upper.y),lower: -32767.0f,upper: 32767.0f);
102 assert(-32767.0f <= floor(bounds.bounds0.lower.y) && floor(bounds.bounds0.lower.y) <= 32767.0f);
103 assert(-32767.0f <= ceil (bounds.bounds0.upper.y) && ceil (bounds.bounds0.upper.y) <= 32767.0f);
104 assert(-32767.0f <= floor(bounds.bounds1.lower.y) && floor(bounds.bounds1.lower.y) <= 32767.0f);
105 assert(-32767.0f <= ceil (bounds.bounds1.upper.y) && ceil (bounds.bounds1.upper.y) <= 32767.0f);
106
107 bounds_vz_x(N)[i] = (char) (short) space3.vz.x;
108 bounds_vz_y(N)[i] = (char) (short) space3.vz.y;
109 bounds_vz_z(N)[i] = (char) (short) space3.vz.z;
110 bounds_vz_lower0(N)[i] = (short) clamp(x: floor(x: bounds.bounds0.lower.z),lower: -32767.0f,upper: 32767.0f);
111 bounds_vz_upper0(N)[i] = (short) clamp(x: ceil (x: bounds.bounds0.upper.z),lower: -32767.0f,upper: 32767.0f);
112 bounds_vz_lower1(N)[i] = (short) clamp(x: floor(x: bounds.bounds1.lower.z),lower: -32767.0f,upper: 32767.0f);
113 bounds_vz_upper1(N)[i] = (short) clamp(x: ceil (x: bounds.bounds1.upper.z),lower: -32767.0f,upper: 32767.0f);
114 assert(-32767.0f <= floor(bounds.bounds0.lower.z) && floor(bounds.bounds0.lower.z) <= 32767.0f);
115 assert(-32767.0f <= ceil (bounds.bounds0.upper.z) && ceil (bounds.bounds0.upper.z) <= 32767.0f);
116 assert(-32767.0f <= floor(bounds.bounds1.lower.z) && floor(bounds.bounds1.lower.z) <= 32767.0f);
117 assert(-32767.0f <= ceil (bounds.bounds1.upper.z) && ceil (bounds.bounds1.upper.z) <= 32767.0f);
118
119 this->primID(N)[i] = primID;
120 }
121
122 return lbounds;
123 }
124
125 template<typename BVH, typename SetMB, typename Allocator>
126 __forceinline static typename BVH::NodeRecordMB4D createLeafMB(BVH* bvh, const SetMB& prims, const Allocator& alloc)
127 {
128 size_t start = prims.begin();
129 size_t end = prims.end();
130 size_t items = CurveNiMB::blocks(N: prims.size());
131 size_t numbytes = CurveNiMB::bytes(N: prims.size());
132 CurveNiMB* accel = (CurveNiMB*) alloc.malloc1(numbytes,BVH::byteAlignment);
133 const typename BVH::NodeRef node = bvh->encodeLeaf((char*)accel,items);
134
135 LBBox3fa bounds = empty;
136 for (size_t i=0; i<items; i++)
137 bounds.extend(other: accel[i].fillMB(prims.prims->data(),start,end,bvh->scene,prims.time_range));
138
139 return typename BVH::NodeRecordMB4D(node,bounds,prims.time_range);
140 };
141
142
143 public:
144
145 // 27.6 - 46 bytes per primitive
146 unsigned char ty;
147 unsigned char N;
148 unsigned char data[4+37*M+24];
149
150 /*
151 struct Layout
152 {
153 unsigned int geomID;
154 unsigned int primID[N];
155
156 char bounds_vx_x[N];
157 char bounds_vx_y[N];
158 char bounds_vx_z[N];
159 short bounds_vx_lower0[N];
160 short bounds_vx_upper0[N];
161 short bounds_vx_lower1[N];
162 short bounds_vx_upper1[N];
163
164 char bounds_vy_x[N];
165 char bounds_vy_y[N];
166 char bounds_vy_z[N];
167 short bounds_vy_lower0[N];
168 short bounds_vy_upper0[N];
169 short bounds_vy_lower1[N];
170 short bounds_vy_upper1[N];
171
172 char bounds_vz_x[N];
173 char bounds_vz_y[N];
174 char bounds_vz_z[N];
175 short bounds_vz_lower0[N];
176 short bounds_vz_upper0[N];
177 short bounds_vz_lower1[N];
178 short bounds_vz_upper1[N];
179
180 Vec3f offset;
181 float scale;
182
183 float time_offset;
184 float time_scale;
185 };
186 */
187
188 __forceinline unsigned int& geomID(size_t N) { return *(unsigned int*)((char*)this+2); }
189 __forceinline const unsigned int& geomID(size_t N) const { return *(unsigned int*)((char*)this+2); }
190
191 __forceinline unsigned int* primID(size_t N) { return (unsigned int*)((char*)this+6); }
192 __forceinline const unsigned int* primID(size_t N) const { return (unsigned int*)((char*)this+6); }
193
194 __forceinline char* bounds_vx_x(size_t N) { return (char*)((char*)this+6+4*N); }
195 __forceinline const char* bounds_vx_x(size_t N) const { return (char*)((char*)this+6+4*N); }
196
197 __forceinline char* bounds_vx_y(size_t N) { return (char*)((char*)this+6+5*N); }
198 __forceinline const char* bounds_vx_y(size_t N) const { return (char*)((char*)this+6+5*N); }
199
200 __forceinline char* bounds_vx_z(size_t N) { return (char*)((char*)this+6+6*N); }
201 __forceinline const char* bounds_vx_z(size_t N) const { return (char*)((char*)this+6+6*N); }
202
203 __forceinline short* bounds_vx_lower0(size_t N) { return (short*)((char*)this+6+7*N); }
204 __forceinline const short* bounds_vx_lower0(size_t N) const { return (short*)((char*)this+6+7*N); }
205
206 __forceinline short* bounds_vx_upper0(size_t N) { return (short*)((char*)this+6+9*N); }
207 __forceinline const short* bounds_vx_upper0(size_t N) const { return (short*)((char*)this+6+9*N); }
208
209 __forceinline short* bounds_vx_lower1(size_t N) { return (short*)((char*)this+6+11*N); }
210 __forceinline const short* bounds_vx_lower1(size_t N) const { return (short*)((char*)this+6+11*N); }
211
212 __forceinline short* bounds_vx_upper1(size_t N) { return (short*)((char*)this+6+13*N); }
213 __forceinline const short* bounds_vx_upper1(size_t N) const { return (short*)((char*)this+6+13*N); }
214
215 __forceinline char* bounds_vy_x(size_t N) { return (char*)((char*)this+6+15*N); }
216 __forceinline const char* bounds_vy_x(size_t N) const { return (char*)((char*)this+6+15*N); }
217
218 __forceinline char* bounds_vy_y(size_t N) { return (char*)((char*)this+6+16*N); }
219 __forceinline const char* bounds_vy_y(size_t N) const { return (char*)((char*)this+6+16*N); }
220
221 __forceinline char* bounds_vy_z(size_t N) { return (char*)((char*)this+6+17*N); }
222 __forceinline const char* bounds_vy_z(size_t N) const { return (char*)((char*)this+6+17*N); }
223
224 __forceinline short* bounds_vy_lower0(size_t N) { return (short*)((char*)this+6+18*N); }
225 __forceinline const short* bounds_vy_lower0(size_t N) const { return (short*)((char*)this+6+18*N); }
226
227 __forceinline short* bounds_vy_upper0(size_t N) { return (short*)((char*)this+6+20*N); }
228 __forceinline const short* bounds_vy_upper0(size_t N) const { return (short*)((char*)this+6+20*N); }
229
230 __forceinline short* bounds_vy_lower1(size_t N) { return (short*)((char*)this+6+22*N); }
231 __forceinline const short* bounds_vy_lower1(size_t N) const { return (short*)((char*)this+6+22*N); }
232
233 __forceinline short* bounds_vy_upper1(size_t N) { return (short*)((char*)this+6+24*N); }
234 __forceinline const short* bounds_vy_upper1(size_t N) const { return (short*)((char*)this+6+24*N); }
235
236 __forceinline char* bounds_vz_x(size_t N) { return (char*)((char*)this+6+26*N); }
237 __forceinline const char* bounds_vz_x(size_t N) const { return (char*)((char*)this+6+26*N); }
238
239 __forceinline char* bounds_vz_y(size_t N) { return (char*)((char*)this+6+27*N); }
240 __forceinline const char* bounds_vz_y(size_t N) const { return (char*)((char*)this+6+27*N); }
241
242 __forceinline char* bounds_vz_z(size_t N) { return (char*)((char*)this+6+28*N); }
243 __forceinline const char* bounds_vz_z(size_t N) const { return (char*)((char*)this+6+28*N); }
244
245 __forceinline short* bounds_vz_lower0(size_t N) { return (short*)((char*)this+6+29*N); }
246 __forceinline const short* bounds_vz_lower0(size_t N) const { return (short*)((char*)this+6+29*N); }
247
248 __forceinline short* bounds_vz_upper0(size_t N) { return (short*)((char*)this+6+31*N); }
249 __forceinline const short* bounds_vz_upper0(size_t N) const { return (short*)((char*)this+6+31*N); }
250
251 __forceinline short* bounds_vz_lower1(size_t N) { return (short*)((char*)this+6+33*N); }
252 __forceinline const short* bounds_vz_lower1(size_t N) const { return (short*)((char*)this+6+33*N); }
253
254 __forceinline short* bounds_vz_upper1(size_t N) { return (short*)((char*)this+6+35*N); }
255 __forceinline const short* bounds_vz_upper1(size_t N) const { return (short*)((char*)this+6+35*N); }
256
257 __forceinline Vec3f* offset(size_t N) { return (Vec3f*)((char*)this+6+37*N); }
258 __forceinline const Vec3f* offset(size_t N) const { return (Vec3f*)((char*)this+6+37*N); }
259
260 __forceinline float* scale(size_t N) { return (float*)((char*)this+6+37*N+12); }
261 __forceinline const float* scale(size_t N) const { return (float*)((char*)this+6+37*N+12); }
262
263 __forceinline float& time_offset(size_t N) { return *(float*)((char*)this+6+37*N+16); }
264 __forceinline const float& time_offset(size_t N) const { return *(float*)((char*)this+6+37*N+16); }
265
266 __forceinline float& time_scale(size_t N) { return *(float*)((char*)this+6+37*N+20); }
267 __forceinline const float& time_scale(size_t N) const { return *(float*)((char*)this+6+37*N+20); }
268
269 __forceinline char* end(size_t N) { return (char*)this+6+37*N+24; }
270 __forceinline const char* end(size_t N) const { return (char*)this+6+37*N+24; }
271 };
272
273 template<int M>
274 typename CurveNiMB<M>::Type CurveNiMB<M>::type;
275
276 typedef CurveNiMB<4> Curve4iMB;
277 typedef CurveNiMB<8> Curve8iMB;
278}
279

source code of qtquick3d/src/3rdparty/embree/kernels/geometry/curveNi_mb.h