1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415
|
// Copyright 2009-2021 Intel Corporation
// SPDX-License-Identifier: Apache-2.0
#include "subdivpatch1base.h"
namespace embree
{
namespace isa
{
Vec3fa patchEval(const SubdivPatch1Base& patch, const float uu, const float vv)
{
if (likely(patch.type == SubdivPatch1Base::BEZIER_PATCH))
return ((BezierPatch3fa*)patch.patch_v)->eval(uu,vv);
else if (likely(patch.type == SubdivPatch1Base::BSPLINE_PATCH))
return ((BSplinePatch3fa*)patch.patch_v)->eval(uu,vv);
else if (likely(patch.type == SubdivPatch1Base::GREGORY_PATCH))
return ((DenseGregoryPatch3fa*)patch.patch_v)->eval(uu,vv);
else if (likely(patch.type == SubdivPatch1Base::BILINEAR_PATCH))
return ((BilinearPatch3fa*)patch.patch_v)->eval(uu,vv);
return Vec3fa( zero );
}
Vec3fa patchNormal(const SubdivPatch1Base& patch, const float uu, const float vv)
{
if (likely(patch.type == SubdivPatch1Base::BEZIER_PATCH))
return ((BezierPatch3fa*)patch.patch_v)->normal(uu,vv);
else if (likely(patch.type == SubdivPatch1Base::BSPLINE_PATCH))
return ((BSplinePatch3fa*)patch.patch_v)->normal(uu,vv);
else if (likely(patch.type == SubdivPatch1Base::GREGORY_PATCH))
return ((DenseGregoryPatch3fa*)patch.patch_v)->normal(uu,vv);
else if (likely(patch.type == SubdivPatch1Base::BILINEAR_PATCH))
return ((BilinearPatch3fa*)patch.patch_v)->normal(uu,vv);
return Vec3fa( zero );
}
template<typename simdf>
Vec3<simdf> patchEval(const SubdivPatch1Base& patch, const simdf& uu, const simdf& vv)
{
if (likely(patch.type == SubdivPatch1Base::BEZIER_PATCH))
return ((BezierPatch3fa*)patch.patch_v)->eval(uu,vv);
else if (likely(patch.type == SubdivPatch1Base::BSPLINE_PATCH))
return ((BSplinePatch3fa*)patch.patch_v)->eval(uu,vv);
else if (likely(patch.type == SubdivPatch1Base::GREGORY_PATCH))
return ((DenseGregoryPatch3fa*)patch.patch_v)->eval(uu,vv);
else if (likely(patch.type == SubdivPatch1Base::BILINEAR_PATCH))
return ((BilinearPatch3fa*)patch.patch_v)->eval(uu,vv);
return Vec3<simdf>( zero );
}
template<typename simdf>
Vec3<simdf> patchNormal(const SubdivPatch1Base& patch, const simdf& uu, const simdf& vv)
{
if (likely(patch.type == SubdivPatch1Base::BEZIER_PATCH))
return ((BezierPatch3fa*)patch.patch_v)->normal(uu,vv);
else if (likely(patch.type == SubdivPatch1Base::BSPLINE_PATCH))
return ((BSplinePatch3fa*)patch.patch_v)->normal(uu,vv);
else if (likely(patch.type == SubdivPatch1Base::GREGORY_PATCH))
return ((DenseGregoryPatch3fa*)patch.patch_v)->normal(uu,vv);
else if (likely(patch.type == SubdivPatch1Base::BILINEAR_PATCH))
return ((BilinearPatch3fa*)patch.patch_v)->normal(uu,vv);
return Vec3<simdf>( zero );
}
/* eval grid over patch and stich edges when required */
void evalGrid(const SubdivPatch1Base& patch,
const unsigned x0, const unsigned x1,
const unsigned y0, const unsigned y1,
const unsigned swidth, const unsigned sheight,
float *__restrict__ const grid_x,
float *__restrict__ const grid_y,
float *__restrict__ const grid_z,
float *__restrict__ const grid_u,
float *__restrict__ const grid_v,
const SubdivMesh* const geom)
{
const unsigned dwidth = x1-x0+1;
const unsigned dheight = y1-y0+1;
const unsigned M = dwidth*dheight+VSIZEX;
const unsigned grid_size_simd_blocks = (M-1)/VSIZEX;
if (unlikely(patch.type == SubdivPatch1Base::EVAL_PATCH))
{
const bool displ = geom->displFunc;
const unsigned N = displ ? M : 0;
dynamic_large_stack_array(float,grid_Ng_x,N,32*32*sizeof(float));
dynamic_large_stack_array(float,grid_Ng_y,N,32*32*sizeof(float));
dynamic_large_stack_array(float,grid_Ng_z,N,32*32*sizeof(float));
if (geom->patch_eval_trees.size())
{
feature_adaptive_eval_grid<PatchEvalGrid>
(geom->patch_eval_trees[geom->numTimeSteps*patch.primID()+patch.time()], patch.subPatch(), patch.needsStitching() ? patch.level : nullptr,
x0,x1,y0,y1,swidth,sheight,
grid_x,grid_y,grid_z,grid_u,grid_v,
displ ? (float*)grid_Ng_x : nullptr, displ ? (float*)grid_Ng_y : nullptr, displ ? (float*)grid_Ng_z : nullptr,
dwidth,dheight);
}
else
{
GeneralCatmullClarkPatch3fa ccpatch(patch.edge(),geom->getVertexBuffer(patch.time()));
feature_adaptive_eval_grid<FeatureAdaptiveEvalGrid,GeneralCatmullClarkPatch3fa>
(ccpatch, patch.subPatch(), patch.needsStitching() ? patch.level : nullptr,
x0,x1,y0,y1,swidth,sheight,
grid_x,grid_y,grid_z,grid_u,grid_v,
displ ? (float*)grid_Ng_x : nullptr, displ ? (float*)grid_Ng_y : nullptr, displ ? (float*)grid_Ng_z : nullptr,
dwidth,dheight);
}
/* convert sub-patch UVs to patch UVs*/
const Vec2f uv0 = patch.getUV(0);
const Vec2f uv1 = patch.getUV(1);
const Vec2f uv2 = patch.getUV(2);
const Vec2f uv3 = patch.getUV(3);
for (unsigned i=0; i<grid_size_simd_blocks; i++)
{
const vfloatx u = vfloatx::load(&grid_u[i*VSIZEX]);
const vfloatx v = vfloatx::load(&grid_v[i*VSIZEX]);
const vfloatx patch_u = lerp2(uv0.x,uv1.x,uv3.x,uv2.x,u,v);
const vfloatx patch_v = lerp2(uv0.y,uv1.y,uv3.y,uv2.y,u,v);
vfloatx::store(&grid_u[i*VSIZEX],patch_u);
vfloatx::store(&grid_v[i*VSIZEX],patch_v);
}
/* call displacement shader */
if (unlikely(geom->displFunc)) {
RTCDisplacementFunctionNArguments args;
args.geometryUserPtr = geom->userPtr;
args.geometry = (RTCGeometry)geom;
//args.geomID = patch.geomID();
args.primID = patch.primID();
args.timeStep = patch.time();
args.u = grid_u;
args.v = grid_v;
args.Ng_x = grid_Ng_x;
args.Ng_y = grid_Ng_y;
args.Ng_z = grid_Ng_z;
args.P_x = grid_x;
args.P_y = grid_y;
args.P_z = grid_z;
args.N = dwidth*dheight;
geom->displFunc(&args);
}
/* set last elements in u,v array to 1.0f */
const float last_u = grid_u[dwidth*dheight-1];
const float last_v = grid_v[dwidth*dheight-1];
const float last_x = grid_x[dwidth*dheight-1];
const float last_y = grid_y[dwidth*dheight-1];
const float last_z = grid_z[dwidth*dheight-1];
for (unsigned i=dwidth*dheight;i<grid_size_simd_blocks*VSIZEX;i++)
{
grid_u[i] = last_u;
grid_v[i] = last_v;
grid_x[i] = last_x;
grid_y[i] = last_y;
grid_z[i] = last_z;
}
}
else
{
/* grid_u, grid_v need to be padded as we write with SIMD granularity */
gridUVTessellator(patch.level,swidth,sheight,x0,y0,dwidth,dheight,grid_u,grid_v);
/* set last elements in u,v array to last valid point */
const float last_u = grid_u[dwidth*dheight-1];
const float last_v = grid_v[dwidth*dheight-1];
for (unsigned i=dwidth*dheight;i<grid_size_simd_blocks*VSIZEX;i++) {
grid_u[i] = last_u;
grid_v[i] = last_v;
}
/* stitch edges if necessary */
if (unlikely(patch.needsStitching()))
stitchUVGrid(patch.level,swidth,sheight,x0,y0,dwidth,dheight,grid_u,grid_v);
/* iterates over all grid points */
for (unsigned i=0; i<grid_size_simd_blocks; i++)
{
const vfloatx u = vfloatx::load(&grid_u[i*VSIZEX]);
const vfloatx v = vfloatx::load(&grid_v[i*VSIZEX]);
Vec3vfx vtx = patchEval(patch,u,v);
/* evaluate displacement function */
if (unlikely(geom->displFunc != nullptr))
{
const Vec3vfx normal = normalize_safe(patchNormal(patch, u, v));
RTCDisplacementFunctionNArguments args;
args.geometryUserPtr = geom->userPtr;
args.geometry = (RTCGeometry)geom;
//args.geomID = patch.geomID();
args.primID = patch.primID();
args.timeStep = patch.time();
args.u = &u[0];
args.v = &v[0];
args.Ng_x = &normal.x[0];
args.Ng_y = &normal.y[0];
args.Ng_z = &normal.z[0];
args.P_x = &vtx.x[0];
args.P_y = &vtx.y[0];
args.P_z = &vtx.z[0];
args.N = VSIZEX;
geom->displFunc(&args);
}
vfloatx::store(&grid_x[i*VSIZEX],vtx.x);
vfloatx::store(&grid_y[i*VSIZEX],vtx.y);
vfloatx::store(&grid_z[i*VSIZEX],vtx.z);
}
}
}
/* eval grid over patch and stich edges when required */
BBox3fa evalGridBounds(const SubdivPatch1Base& patch,
const unsigned x0, const unsigned x1,
const unsigned y0, const unsigned y1,
const unsigned swidth, const unsigned sheight,
const SubdivMesh* const geom)
{
BBox3fa b(empty);
const unsigned dwidth = x1-x0+1;
const unsigned dheight = y1-y0+1;
const unsigned M = dwidth*dheight+VSIZEX;
const unsigned grid_size_simd_blocks = (M-1)/VSIZEX;
dynamic_large_stack_array(float,grid_u,M,64*64*sizeof(float));
dynamic_large_stack_array(float,grid_v,M,64*64*sizeof(float));
if (unlikely(patch.type == SubdivPatch1Base::EVAL_PATCH))
{
const bool displ = geom->displFunc;
dynamic_large_stack_array(float,grid_x,M,64*64*sizeof(float));
dynamic_large_stack_array(float,grid_y,M,64*64*sizeof(float));
dynamic_large_stack_array(float,grid_z,M,64*64*sizeof(float));
dynamic_large_stack_array(float,grid_Ng_x,displ ? M : 0,64*64*sizeof(float));
dynamic_large_stack_array(float,grid_Ng_y,displ ? M : 0,64*64*sizeof(float));
dynamic_large_stack_array(float,grid_Ng_z,displ ? M : 0,64*64*sizeof(float));
if (geom->patch_eval_trees.size())
{
feature_adaptive_eval_grid<PatchEvalGrid>
(geom->patch_eval_trees[geom->numTimeSteps*patch.primID()+patch.time()], patch.subPatch(), patch.needsStitching() ? patch.level : nullptr,
x0,x1,y0,y1,swidth,sheight,
grid_x,grid_y,grid_z,grid_u,grid_v,
displ ? (float*)grid_Ng_x : nullptr, displ ? (float*)grid_Ng_y : nullptr, displ ? (float*)grid_Ng_z : nullptr,
dwidth,dheight);
}
else
{
GeneralCatmullClarkPatch3fa ccpatch(patch.edge(),geom->getVertexBuffer(patch.time()));
feature_adaptive_eval_grid <FeatureAdaptiveEvalGrid,GeneralCatmullClarkPatch3fa>
(ccpatch, patch.subPatch(), patch.needsStitching() ? patch.level : nullptr,
x0,x1,y0,y1,swidth,sheight,
grid_x,grid_y,grid_z,grid_u,grid_v,
displ ? (float*)grid_Ng_x : nullptr, displ ? (float*)grid_Ng_y : nullptr, displ ? (float*)grid_Ng_z : nullptr,
dwidth,dheight);
}
/* call displacement shader */
if (unlikely(geom->displFunc))
{
RTCDisplacementFunctionNArguments args;
args.geometryUserPtr = geom->userPtr;
args.geometry = (RTCGeometry)geom;
//args.geomID = patch.geomID();
args.primID = patch.primID();
args.timeStep = patch.time();
args.u = grid_u;
args.v = grid_v;
args.Ng_x = grid_Ng_x;
args.Ng_y = grid_Ng_y;
args.Ng_z = grid_Ng_z;
args.P_x = grid_x;
args.P_y = grid_y;
args.P_z = grid_z;
args.N = dwidth*dheight;
geom->displFunc(&args);
}
/* set last elements in u,v array to 1.0f */
const float last_u = grid_u[dwidth*dheight-1];
const float last_v = grid_v[dwidth*dheight-1];
const float last_x = grid_x[dwidth*dheight-1];
const float last_y = grid_y[dwidth*dheight-1];
const float last_z = grid_z[dwidth*dheight-1];
for (unsigned i=dwidth*dheight;i<grid_size_simd_blocks*VSIZEX;i++)
{
grid_u[i] = last_u;
grid_v[i] = last_v;
grid_x[i] = last_x;
grid_y[i] = last_y;
grid_z[i] = last_z;
}
vfloatx bounds_min_x = pos_inf;
vfloatx bounds_min_y = pos_inf;
vfloatx bounds_min_z = pos_inf;
vfloatx bounds_max_x = neg_inf;
vfloatx bounds_max_y = neg_inf;
vfloatx bounds_max_z = neg_inf;
for (unsigned i = 0; i<grid_size_simd_blocks; i++)
{
vfloatx x = vfloatx::loadu(&grid_x[i * VSIZEX]);
vfloatx y = vfloatx::loadu(&grid_y[i * VSIZEX]);
vfloatx z = vfloatx::loadu(&grid_z[i * VSIZEX]);
bounds_min_x = min(bounds_min_x,x);
bounds_min_y = min(bounds_min_y,y);
bounds_min_z = min(bounds_min_z,z);
bounds_max_x = max(bounds_max_x,x);
bounds_max_y = max(bounds_max_y,y);
bounds_max_z = max(bounds_max_z,z);
}
b.lower.x = reduce_min(bounds_min_x);
b.lower.y = reduce_min(bounds_min_y);
b.lower.z = reduce_min(bounds_min_z);
b.upper.x = reduce_max(bounds_max_x);
b.upper.y = reduce_max(bounds_max_y);
b.upper.z = reduce_max(bounds_max_z);
//b.lower.a = 0;
//b.upper.a = 0;
}
else
{
/* grid_u, grid_v need to be padded as we write with SIMD granularity */
gridUVTessellator(patch.level,swidth,sheight,x0,y0,dwidth,dheight,grid_u,grid_v);
/* set last elements in u,v array to last valid point */
const float last_u = grid_u[dwidth*dheight-1];
const float last_v = grid_v[dwidth*dheight-1];
for (unsigned i=dwidth*dheight;i<grid_size_simd_blocks*VSIZEX;i++) {
grid_u[i] = last_u;
grid_v[i] = last_v;
}
/* stitch edges if necessary */
if (unlikely(patch.needsStitching()))
stitchUVGrid(patch.level,swidth,sheight,x0,y0,dwidth,dheight,grid_u,grid_v);
/* iterates over all grid points */
Vec3vfx bounds_min;
bounds_min[0] = pos_inf;
bounds_min[1] = pos_inf;
bounds_min[2] = pos_inf;
Vec3vfx bounds_max;
bounds_max[0] = neg_inf;
bounds_max[1] = neg_inf;
bounds_max[2] = neg_inf;
for (unsigned i=0; i<grid_size_simd_blocks; i++)
{
const vfloatx u = vfloatx::load(&grid_u[i*VSIZEX]);
const vfloatx v = vfloatx::load(&grid_v[i*VSIZEX]);
Vec3vfx vtx = patchEval(patch,u,v);
/* evaluate displacement function */
if (unlikely(geom->displFunc != nullptr))
{
const Vec3vfx normal = normalize_safe(patchNormal(patch,u,v));
RTCDisplacementFunctionNArguments args;
args.geometryUserPtr = geom->userPtr;
args.geometry = (RTCGeometry)geom;
//args.geomID = patch.geomID();
args.primID = patch.primID();
args.timeStep = patch.time();
args.u = &u[0];
args.v = &v[0];
args.Ng_x = &normal.x[0];
args.Ng_y = &normal.y[0];
args.Ng_z = &normal.z[0];
args.P_x = &vtx.x[0];
args.P_y = &vtx.y[0];
args.P_z = &vtx.z[0];
args.N = VSIZEX;
geom->displFunc(&args);
}
bounds_min[0] = min(bounds_min[0],vtx.x);
bounds_max[0] = max(bounds_max[0],vtx.x);
bounds_min[1] = min(bounds_min[1],vtx.y);
bounds_max[1] = max(bounds_max[1],vtx.y);
bounds_min[2] = min(bounds_min[2],vtx.z);
bounds_max[2] = max(bounds_max[2],vtx.z);
}
b.lower.x = reduce_min(bounds_min[0]);
b.lower.y = reduce_min(bounds_min[1]);
b.lower.z = reduce_min(bounds_min[2]);
b.upper.x = reduce_max(bounds_max[0]);
b.upper.y = reduce_max(bounds_max[1]);
b.upper.z = reduce_max(bounds_max[2]);
//b.lower.a = 0;
//b.upper.a = 0;
}
assert( std::isfinite(b.lower.x) );
assert( std::isfinite(b.lower.y) );
assert( std::isfinite(b.lower.z) );
assert( std::isfinite(b.upper.x) );
assert( std::isfinite(b.upper.y) );
assert( std::isfinite(b.upper.z) );
assert(b.lower.x <= b.upper.x);
assert(b.lower.y <= b.upper.y);
assert(b.lower.z <= b.upper.z);
return b;
}
}
}
|