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
|
// Copyright 2009-2021 Intel Corporation
// SPDX-License-Identifier: Apache-2.0
#pragma once
#include "default.h"
#include "builder.h"
#include "geometry.h"
#include "ray.h"
#include "hit.h"
namespace embree
{
struct IntersectFunctionNArguments;
struct OccludedFunctionNArguments;
struct IntersectFunctionNArguments : public RTCIntersectFunctionNArguments
{
Geometry* geometry;
RTCScene forward_scene;
RTCIntersectArguments* args;
};
struct OccludedFunctionNArguments : public RTCOccludedFunctionNArguments
{
Geometry* geometry;
RTCScene forward_scene;
RTCIntersectArguments* args;
};
/*! Base class for set of acceleration structures. */
class AccelSet : public Geometry
{
public:
typedef RTCIntersectFunctionN IntersectFuncN;
typedef RTCOccludedFunctionN OccludedFuncN;
typedef void (*ErrorFunc) ();
struct IntersectorN
{
IntersectorN (ErrorFunc error = nullptr) ;
IntersectorN (IntersectFuncN intersect, OccludedFuncN occluded, const char* name);
operator bool() const { return name; }
public:
static const char* type;
IntersectFuncN intersect;
OccludedFuncN occluded;
const char* name;
};
public:
/*! construction */
AccelSet (Device* device, Geometry::GType gtype, size_t items, size_t numTimeSteps);
/*! makes the acceleration structure immutable */
virtual void immutable () {}
/*! build accel */
virtual void build () = 0;
/*! check if the i'th primitive is valid between the specified time range */
__forceinline bool valid(size_t i, const range<size_t>& itime_range) const
{
for (size_t itime = itime_range.begin(); itime <= itime_range.end(); itime++)
if (!isvalid_non_empty(bounds(i,itime))) return false;
return true;
}
/*! Calculates the bounds of an item */
__forceinline BBox3fa bounds(size_t i, size_t itime = 0) const
{
BBox3fa box;
assert(i < size());
RTCBoundsFunctionArguments args;
args.geometryUserPtr = userPtr;
args.primID = (unsigned int)i;
args.timeStep = (unsigned int)itime;
args.bounds_o = (RTCBounds*)&box;
boundsFunc(&args);
return box;
}
/*! calculates the linear bounds of the i'th item at the itime'th time segment */
__forceinline LBBox3fa linearBounds(size_t i, size_t itime) const
{
BBox3fa box[2];
assert(i < size());
RTCBoundsFunctionArguments args;
args.geometryUserPtr = userPtr;
args.primID = (unsigned int)i;
args.timeStep = (unsigned int)(itime+0);
args.bounds_o = (RTCBounds*)&box[0];
boundsFunc(&args);
args.timeStep = (unsigned int)(itime+1);
args.bounds_o = (RTCBounds*)&box[1];
boundsFunc(&args);
return LBBox3fa(box[0],box[1]);
}
/*! calculates the build bounds of the i'th item, if it's valid */
__forceinline bool buildBounds(size_t i, BBox3fa* bbox = nullptr) const
{
const BBox3fa b = bounds(i);
if (bbox) *bbox = b;
return isvalid_non_empty(b);
}
/*! calculates the build bounds of the i'th item at the itime'th time segment, if it's valid */
__forceinline bool buildBounds(size_t i, size_t itime, BBox3fa& bbox) const
{
const LBBox3fa bounds = linearBounds(i,itime);
bbox = bounds.bounds0; // use bounding box of first timestep to build BVH
return isvalid_non_empty(bounds);
}
/*! calculates the linear bounds of the i'th primitive for the specified time range */
__forceinline LBBox3fa linearBounds(size_t primID, const BBox1f& dt) const {
return LBBox3fa([&] (size_t itime) { return bounds(primID, itime); }, dt, time_range, fnumTimeSegments);
}
/*! calculates the linear bounds of the i'th primitive for the specified time range */
__forceinline bool linearBounds(size_t i, const BBox1f& time_range, LBBox3fa& bbox) const {
if (!valid(i, timeSegmentRange(time_range))) return false;
bbox = linearBounds(i, time_range);
return true;
}
/* gets version info of topology */
unsigned int getTopologyVersion() const {
return numPrimitives;
}
/* returns true if topology changed */
bool topologyChanged(unsigned int otherVersion) const {
return numPrimitives != otherVersion;
}
public:
/*! Intersects a single ray with the scene. */
__forceinline bool intersect (RayHit& ray, unsigned int geomID, unsigned int primID, RayQueryContext* context)
{
assert(primID < size());
int mask = -1;
IntersectFunctionNArguments args;
args.valid = &mask;
args.geometryUserPtr = userPtr;
args.context = context->user;
args.rayhit = (RTCRayHitN*)&ray;
args.N = 1;
args.geomID = geomID;
args.primID = primID;
args.geometry = this;
args.forward_scene = nullptr;
args.args = context->args;
IntersectFuncN intersectFunc = nullptr;
intersectFunc = intersectorN.intersect;
if (context->getIntersectFunction())
intersectFunc = context->getIntersectFunction();
assert(intersectFunc);
intersectFunc(&args);
return mask != 0;
}
/*! Tests if single ray is occluded by the scene. */
__forceinline bool occluded (Ray& ray, unsigned int geomID, unsigned int primID, RayQueryContext* context)
{
assert(primID < size());
int mask = -1;
OccludedFunctionNArguments args;
args.valid = &mask;
args.geometryUserPtr = userPtr;
args.context = context->user;
args.ray = (RTCRayN*)&ray;
args.N = 1;
args.geomID = geomID;
args.primID = primID;
args.geometry = this;
args.forward_scene = nullptr;
args.args = context->args;
OccludedFuncN occludedFunc = nullptr;
occludedFunc = intersectorN.occluded;
if (context->getOccludedFunction())
occludedFunc = context->getOccludedFunction();
assert(occludedFunc);
occludedFunc(&args);
return mask != 0;
}
/*! Intersects a single ray with the scene. */
__forceinline bool intersect (RayHit& ray, unsigned int geomID, unsigned int primID, RayQueryContext* context, RTCScene& forward_scene)
{
assert(primID < size());
int mask = -1;
IntersectFunctionNArguments args;
args.valid = &mask;
args.geometryUserPtr = userPtr;
args.context = context->user;
args.rayhit = (RTCRayHitN*)&ray;
args.N = 1;
args.geomID = geomID;
args.primID = primID;
args.geometry = this;
args.forward_scene = nullptr;
args.args = nullptr;
typedef void (*RTCIntersectFunctionSYCL)(const void* args);
RTCIntersectFunctionSYCL intersectFunc = nullptr;
#if EMBREE_SYCL_GEOMETRY_CALLBACK
if (context->args->feature_mask & RTC_FEATURE_FLAG_USER_GEOMETRY_CALLBACK_IN_GEOMETRY)
intersectFunc = (RTCIntersectFunctionSYCL) intersectorN.intersect;
#endif
if (context->args->feature_mask & RTC_FEATURE_FLAG_USER_GEOMETRY_CALLBACK_IN_ARGUMENTS)
if (context->getIntersectFunction())
intersectFunc = (RTCIntersectFunctionSYCL) context->getIntersectFunction();
if (intersectFunc)
intersectFunc(&args);
forward_scene = args.forward_scene;
return mask != 0;
}
/*! Tests if single ray is occluded by the scene. */
__forceinline bool occluded (Ray& ray, unsigned int geomID, unsigned int primID, RayQueryContext* context, RTCScene& forward_scene)
{
assert(primID < size());
int mask = -1;
OccludedFunctionNArguments args;
args.valid = &mask;
args.geometryUserPtr = userPtr;
args.context = context->user;
args.ray = (RTCRayN*)&ray;
args.N = 1;
args.geomID = geomID;
args.primID = primID;
args.geometry = this;
args.forward_scene = nullptr;
args.args = nullptr;
typedef void (*RTCOccludedFunctionSYCL)(const void* args);
RTCOccludedFunctionSYCL occludedFunc = nullptr;
#if EMBREE_SYCL_GEOMETRY_CALLBACK
if (context->args->feature_mask & RTC_FEATURE_FLAG_USER_GEOMETRY_CALLBACK_IN_GEOMETRY)
occludedFunc = (RTCOccludedFunctionSYCL) intersectorN.occluded;
#endif
if (context->args->feature_mask & RTC_FEATURE_FLAG_USER_GEOMETRY_CALLBACK_IN_ARGUMENTS)
if (context->getOccludedFunction())
occludedFunc = (RTCOccludedFunctionSYCL) context->getOccludedFunction();
if (occludedFunc)
occludedFunc(&args);
forward_scene = args.forward_scene;
return mask != 0;
}
/*! Intersects a packet of K rays with the scene. */
template<int K>
__forceinline void intersect (const vbool<K>& valid, RayHitK<K>& ray, unsigned int geomID, unsigned int primID, RayQueryContext* context)
{
assert(primID < size());
vint<K> mask = valid.mask32();
IntersectFunctionNArguments args;
args.valid = (int*)&mask;
args.geometryUserPtr = userPtr;
args.context = context->user;
args.rayhit = (RTCRayHitN*)&ray;
args.N = K;
args.geomID = geomID;
args.primID = primID;
args.geometry = this;
args.forward_scene = nullptr;
args.args = context->args;
IntersectFuncN intersectFunc = nullptr;
intersectFunc = intersectorN.intersect;
if (context->getIntersectFunction())
intersectFunc = context->getIntersectFunction();
assert(intersectFunc);
intersectFunc(&args);
}
/*! Tests if a packet of K rays is occluded by the scene. */
template<int K>
__forceinline void occluded (const vbool<K>& valid, RayK<K>& ray, unsigned int geomID, unsigned int primID, RayQueryContext* context)
{
assert(primID < size());
vint<K> mask = valid.mask32();
OccludedFunctionNArguments args;
args.valid = (int*)&mask;
args.geometryUserPtr = userPtr;
args.context = context->user;
args.ray = (RTCRayN*)&ray;
args.N = K;
args.geomID = geomID;
args.primID = primID;
args.geometry = this;
args.forward_scene = nullptr;
args.args = context->args;
OccludedFuncN occludedFunc = nullptr;
occludedFunc = intersectorN.occluded;
if (context->getOccludedFunction())
occludedFunc = context->getOccludedFunction();
assert(occludedFunc);
occludedFunc(&args);
}
public:
RTCBoundsFunction boundsFunc;
IntersectorN intersectorN;
};
#define DEFINE_SET_INTERSECTORN(symbol,intersector) \
AccelSet::IntersectorN symbol() { \
return AccelSet::IntersectorN(intersector::intersect, \
intersector::occluded, \
TOSTRING(isa) "::" TOSTRING(symbol)); \
}
}
|