godot/thirdparty/embree/kernels/common/context.h

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

174 lines
4.9 KiB
C++
Raw Normal View History

// Copyright 2009-2021 Intel Corporation
2021-04-20 16:38:09 +00:00
// SPDX-License-Identifier: Apache-2.0
#pragma once
#include "default.h"
#include "rtcore.h"
#include "point_query.h"
namespace embree
{
class Scene;
2024-02-24 11:40:55 +00:00
struct RayQueryContext
2021-04-20 16:38:09 +00:00
{
public:
2024-02-24 11:40:55 +00:00
__forceinline RayQueryContext(Scene* scene, RTCRayQueryContext* user_context, RTCIntersectArguments* args)
: scene(scene), user(user_context), args(args) {}
__forceinline RayQueryContext(Scene* scene, RTCRayQueryContext* user_context, RTCOccludedArguments* args)
: scene(scene), user(user_context), args((RTCIntersectArguments*)args) {}
2021-04-20 16:38:09 +00:00
__forceinline bool hasContextFilter() const {
2024-02-24 11:40:55 +00:00
return args->filter != nullptr;
}
RTCFilterFunctionN getFilter() const {
return args->filter;
}
RTCIntersectFunctionN getIntersectFunction() const {
return args->intersect;
}
RTCOccludedFunctionN getOccludedFunction() const {
return (RTCOccludedFunctionN) args->intersect;
2021-04-20 16:38:09 +00:00
}
__forceinline bool isCoherent() const {
2024-02-24 11:40:55 +00:00
return embree::isCoherent(args->flags);
2021-04-20 16:38:09 +00:00
}
__forceinline bool isIncoherent() const {
2024-02-24 11:40:55 +00:00
return embree::isIncoherent(args->flags);
2021-04-20 16:38:09 +00:00
}
2024-02-24 11:40:55 +00:00
__forceinline bool enforceArgumentFilterFunction() const {
return args->flags & RTC_RAY_QUERY_FLAG_INVOKE_ARGUMENT_FILTER;
}
#if RTC_MIN_WIDTH
__forceinline float getMinWidthDistanceFactor() const {
return args->minWidthDistanceFactor;
}
#endif
2021-04-20 16:38:09 +00:00
public:
2024-02-24 11:40:55 +00:00
Scene* scene = nullptr;
RTCRayQueryContext* user = nullptr;
RTCIntersectArguments* args = nullptr;
2021-04-20 16:38:09 +00:00
};
template<int M, typename Geometry>
2024-02-24 11:40:55 +00:00
__forceinline Vec4vf<M> enlargeRadiusToMinWidth(const RayQueryContext* context, const Geometry* geom, const Vec3vf<M>& ray_org, const Vec4vf<M>& v)
2021-04-20 16:38:09 +00:00
{
#if RTC_MIN_WIDTH
const vfloat<M> d = length(Vec3vf<M>(v) - ray_org);
2024-02-24 11:40:55 +00:00
const vfloat<M> r = clamp(context->getMinWidthDistanceFactor()*d, v.w, geom->maxRadiusScale*v.w);
2021-04-20 16:38:09 +00:00
return Vec4vf<M>(v.x,v.y,v.z,r);
#else
return v;
#endif
}
template<typename Geometry>
2024-02-24 11:40:55 +00:00
__forceinline Vec3ff enlargeRadiusToMinWidth(const RayQueryContext* context, const Geometry* geom, const Vec3fa& ray_org, const Vec3ff& v)
2021-04-20 16:38:09 +00:00
{
#if RTC_MIN_WIDTH
const float d = length(Vec3fa(v) - ray_org);
2024-02-24 11:40:55 +00:00
const float r = clamp(context->getMinWidthDistanceFactor()*d, v.w, geom->maxRadiusScale*v.w);
2021-04-20 16:38:09 +00:00
return Vec3ff(v.x,v.y,v.z,r);
#else
return v;
#endif
}
2024-02-24 11:40:55 +00:00
template<typename Geometry>
__forceinline Vec3ff enlargeRadiusToMinWidth(const RayQueryContext* context, const Geometry* geom, const Vec3fa& ray_org, const Vec4f& v) {
return enlargeRadiusToMinWidth(context,geom,ray_org,Vec3ff(v.x,v.y,v.z,v.w));
}
2021-04-20 16:38:09 +00:00
enum PointQueryType
{
POINT_QUERY_TYPE_UNDEFINED = 0,
POINT_QUERY_TYPE_SPHERE = 1,
POINT_QUERY_TYPE_AABB = 2,
};
typedef bool (*PointQueryFunction)(struct RTCPointQueryFunctionArguments* args);
2024-02-24 11:40:55 +00:00
2021-04-20 16:38:09 +00:00
struct PointQueryContext
{
public:
__forceinline PointQueryContext(Scene* scene,
PointQuery* query_ws,
PointQueryType query_type,
PointQueryFunction func,
RTCPointQueryContext* userContext,
float similarityScale,
void* userPtr)
: scene(scene)
2024-02-24 11:40:55 +00:00
, tstate(nullptr)
2021-04-20 16:38:09 +00:00
, query_ws(query_ws)
, query_type(query_type)
, func(func)
, userContext(userContext)
, similarityScale(similarityScale)
, userPtr(userPtr)
, primID(RTC_INVALID_GEOMETRY_ID)
, geomID(RTC_INVALID_GEOMETRY_ID)
, query_radius(query_ws->radius)
{
2024-02-24 11:40:55 +00:00
update();
}
public:
__forceinline void update()
{
2021-04-20 16:38:09 +00:00
if (query_type == POINT_QUERY_TYPE_AABB) {
assert(similarityScale == 0.f);
updateAABB();
}
2024-02-24 11:40:55 +00:00
else{
query_radius = Vec3fa(query_ws->radius * similarityScale);
}
2021-04-20 16:38:09 +00:00
if (userContext->instStackSize == 0) {
assert(similarityScale == 1.f);
}
}
__forceinline void updateAABB()
{
if (likely(query_ws->radius == (float)inf || userContext->instStackSize == 0)) {
query_radius = Vec3fa(query_ws->radius);
return;
}
const AffineSpace3fa m = AffineSpace3fa_load_unaligned((AffineSpace3fa*)userContext->world2inst[userContext->instStackSize-1]);
BBox3fa bbox(Vec3fa(-query_ws->radius), Vec3fa(query_ws->radius));
bbox = xfmBounds(m, bbox);
query_radius = 0.5f * (bbox.upper - bbox.lower);
}
public:
Scene* scene;
2024-02-24 11:40:55 +00:00
void* tstate;
2021-04-20 16:38:09 +00:00
PointQuery* query_ws; // the original world space point query
PointQueryType query_type;
PointQueryFunction func;
RTCPointQueryContext* userContext;
2024-02-24 11:40:55 +00:00
float similarityScale;
2021-04-20 16:38:09 +00:00
void* userPtr;
unsigned int primID;
unsigned int geomID;
Vec3fa query_radius; // used if the query is converted to an AABB internally
};
}