21# include <embree4/rtcore_geometry.h>
37 "Object and Embree max motion steps inconsistent");
39 "Object and Geometry max motion steps inconsistent");
41static size_t unaccounted_mem = 0;
43static bool rtc_memory_monitor_func(
void *userPtr,
const ssize_t bytes,
const bool )
66static void rtc_error_func(
void * ,
enum RTCError ,
const char *
str)
71static double progress_start_time = 0.0;
73static bool rtc_progress_func(
void *user_ptr,
const double n)
77 if (
time_dt() - progress_start_time < 0.25) {
81 const string msg =
string_printf(
"Building BVH %.0f%%", n * 100.0);
83 progress_start_time =
time_dt();
88BVHEmbree::BVHEmbree(
const BVHParams ¶ms_,
91 :
BVH(params_, geometry_, objects_),
94 rtc_device_is_sycl(
false),
95 build_quality(RTC_BUILD_QUALITY_REFIT)
100BVHEmbree::~BVHEmbree()
103 rtcReleaseScene(scene);
107void BVHEmbree::build(
Progress &progress,
109 RTCDevice rtc_device_,
110 const bool rtc_device_is_sycl_)
112 rtc_device = rtc_device_;
113 rtc_device_is_sycl = rtc_device_is_sycl_;
116 rtcSetDeviceErrorFunction(rtc_device, rtc_error_func,
nullptr);
117 rtcSetDeviceMemoryMonitorFunction(rtc_device, rtc_memory_monitor_func, stats);
122 rtcReleaseScene(scene);
127 const bool compact = params.use_compact_structure;
129 scene = rtcNewScene(rtc_device);
130 const RTCSceneFlags scene_flags = (dynamic ? RTC_SCENE_FLAG_DYNAMIC : RTC_SCENE_FLAG_NONE) |
131 (compact ? RTC_SCENE_FLAG_COMPACT : RTC_SCENE_FLAG_NONE) |
132 RTC_SCENE_FLAG_ROBUST |
133 RTC_SCENE_FLAG_FILTER_FUNCTION_IN_ARGUMENTS;
134 rtcSetSceneFlags(scene, scene_flags);
135 build_quality = dynamic ? RTC_BUILD_QUALITY_LOW :
136 (params.use_spatial_split ? RTC_BUILD_QUALITY_HIGH :
137 RTC_BUILD_QUALITY_MEDIUM);
138 if (build_quality == RTC_BUILD_QUALITY_HIGH && rtc_device_is_sycl) {
149 <<
"Due to a known issue in Intel GPU drivers, overriding RTC_BUILD_QUALITY_HIGH to "
150 "RTC_BUILD_QUALITY_MEDIUM to prevent crashes. This workaround will be removed only "
151 "in a future Blender release.";
152 build_quality = RTC_BUILD_QUALITY_MEDIUM;
154 rtcSetSceneBuildQuality(scene, build_quality);
157 for (
Object *ob : objects) {
158 if (params.top_level) {
159 if (!ob->is_traceable()) {
163 if (!ob->get_geometry()->is_instanced()) {
183 rtcSetSceneProgressMonitorFunction(scene, rtc_progress_func, &progress);
184 rtcCommitScene(scene);
187const char *BVHEmbree::get_error_string(RTCError error_code)
189# if RTC_VERSION >= 40303
190 return rtcGetErrorString(error_code);
192 switch (error_code) {
195 case RTC_ERROR_UNKNOWN:
196 return "unknown error";
197 case RTC_ERROR_INVALID_ARGUMENT:
198 return "invalid argument error";
199 case RTC_ERROR_INVALID_OPERATION:
200 return "invalid operation error";
201 case RTC_ERROR_OUT_OF_MEMORY:
202 return "out of memory error";
203 case RTC_ERROR_UNSUPPORTED_CPU:
204 return "unsupported cpu error";
205 case RTC_ERROR_CANCELLED:
209 return "unknown error";
214# if defined(WITH_EMBREE_GPU) && RTC_VERSION >= 40302
223 for (
const RTCScene &embree_scene : scenes) {
224 RTCSceneFlags scene_flags = rtcGetSceneFlags(embree_scene);
225 scene_flags = scene_flags | RTC_SCENE_FLAG_PREFETCH_USM_SHARED_ON_GPU;
226 rtcSetSceneFlags(embree_scene, scene_flags);
227 rtcCommitScene(embree_scene);
230 RTCError error_code = rtcGetDeviceError(rtc_device);
231 if (error_code != RTC_ERROR_NONE) {
235 return RTC_ERROR_NONE;
239void BVHEmbree::add_object(
Object *ob,
const int i)
241 Geometry *geom = ob->get_geometry();
244 Mesh *mesh =
static_cast<Mesh *
>(geom);
246 add_triangles(ob, mesh, i);
250 Hair *hair =
static_cast<Hair *
>(geom);
259 add_points(ob, pointcloud, i);
262 add_gsplats(ob, pointcloud, i);
268void BVHEmbree::add_instance(
Object *ob,
const int i)
270 BVHEmbree *instance_bvh =
static_cast<BVHEmbree *
>(ob->get_geometry()->bvh.get());
271 assert(instance_bvh !=
nullptr);
273 const size_t num_object_motion_steps = ob->
use_motion() ? ob->get_motion().size() : 1;
274 const size_t num_motion_steps =
min(num_object_motion_steps, (
size_t)RTC_MAX_TIME_STEP_COUNT);
275 assert(num_object_motion_steps <= RTC_MAX_TIME_STEP_COUNT);
277 RTCGeometry geom_id = rtcNewGeometry(rtc_device, RTC_GEOMETRY_TYPE_INSTANCE);
278 rtcSetGeometryInstancedScene(geom_id, instance_bvh->scene);
279 rtcSetGeometryTimeStepCount(geom_id, num_motion_steps);
284 for (
size_t step = 0;
step < num_motion_steps; ++
step) {
285 RTCQuaternionDecomposition rtc_decomp;
286 rtcInitQuaternionDecomposition(&rtc_decomp);
287 rtcQuaternionDecompositionSetQuaternion(
288 &rtc_decomp, decomp[
step].
x.w, decomp[
step].x.x, decomp[
step].x.y, decomp[
step].x.z);
289 rtcQuaternionDecompositionSetScale(
290 &rtc_decomp, decomp[
step].
y.w, decomp[
step].z.w, decomp[
step].w.w);
291 rtcQuaternionDecompositionSetTranslation(
292 &rtc_decomp, decomp[
step].
y.x, decomp[
step].y.y, decomp[
step].y.z);
293 rtcQuaternionDecompositionSetSkew(
294 &rtc_decomp, decomp[
step].
z.x, decomp[
step].z.y, decomp[
step].w.x);
295 rtcSetGeometryTransformQuaternion(geom_id,
step, &rtc_decomp);
299 rtcSetGeometryTransform(
300 geom_id, 0, RTC_FORMAT_FLOAT3X4_ROW_MAJOR, (
const float *)&ob->get_tfm());
303 rtcSetGeometryUserData(geom_id,
304#
if RTC_VERSION >= 40400
305 (
void *)rtcGetSceneTraversable(instance_bvh->scene)
307 (
void *)instance_bvh->scene
312 rtcSetGeometryEnableFilterFunctionFromArguments(geom_id,
true);
314 rtcCommitGeometry(geom_id);
315 rtcAttachGeometryByID(scene, geom_id, i * 2);
316 rtcReleaseGeometry(geom_id);
319void BVHEmbree::add_triangles(
const Object *ob,
const Mesh *mesh,
const int i)
322 size_t num_motion_steps = 1;
326 num_motion_steps = mesh->get_motion_steps();
330 assert(num_motion_steps <= RTC_MAX_TIME_STEP_COUNT);
331 num_motion_steps =
min(num_motion_steps, (
size_t)RTC_MAX_TIME_STEP_COUNT);
335 RTCGeometry geom_id = rtcNewGeometry(rtc_device, RTC_GEOMETRY_TYPE_TRIANGLE);
336 rtcSetGeometryBuildQuality(geom_id, build_quality);
337 rtcSetGeometryTimeStepCount(geom_id, num_motion_steps);
339 const int *triangles = mesh->get_triangles().data();
340 if (!rtc_device_is_sycl) {
341 rtcSetSharedGeometryBuffer(geom_id,
342 RTC_BUFFER_TYPE_INDEX,
355 int *triangles_buffer =
nullptr;
356# if RTC_VERSION >= 40400
357 rtcSetNewGeometryBufferHostDevice(
359 triangles_buffer = (
int *)rtcSetNewGeometryBuffer(
362 RTC_BUFFER_TYPE_INDEX,
367#
if RTC_VERSION >= 40400
369 (
void **)(&triangles_buffer),
374 if (triangles_buffer) {
375 static_assert(
sizeof(int) ==
sizeof(
uint));
376 std::memcpy(triangles_buffer, triangles,
sizeof(
int) * 3 * (num_triangles));
379 set_tri_vertex_buffer(geom_id, mesh,
false);
382 rtcSetGeometryEnableFilterFunctionFromArguments(geom_id,
true);
384 rtcCommitGeometry(geom_id);
385 rtcAttachGeometryByID(scene, geom_id, i * 2);
386 rtcReleaseGeometry(geom_id);
389void BVHEmbree::set_tri_vertex_buffer(RTCGeometry geom_id,
const Mesh *mesh,
const bool update)
392 size_t num_motion_steps = 1;
394 num_motion_steps = mesh->get_motion_steps();
395 if (num_motion_steps > RTC_MAX_TIME_STEP_COUNT) {
397 num_motion_steps = RTC_MAX_TIME_STEP_COUNT;
400 const size_t num_verts = mesh->
num_verts();
402 for (
int t = 0; t < num_motion_steps; ++t) {
406 rtcUpdateGeometryBuffer(geom_id, RTC_BUFFER_TYPE_VERTEX, t);
409 if (!rtc_device_is_sycl) {
410 rtcSetSharedGeometryBuffer(geom_id,
411 RTC_BUFFER_TYPE_VERTEX,
428# if RTC_VERSION >= 40400
429 rtcSetNewGeometryBufferHostDevice(
434 RTC_BUFFER_TYPE_VERTEX,
439#
if RTC_VERSION >= 40400
441 (
void **)(&verts_buffer),
447 for (
size_t i = (
size_t)0; i < num_verts; ++i) {
448 verts_buffer[i].
x = verts[i].
x;
449 verts_buffer[i].
y = verts[i].
y;
450 verts_buffer[i].
z = verts[i].
z;
462void pack_motion_verts(
const size_t num_curves,
465 const float *curve_radius,
469 for (
size_t j = 0; j < num_curves; ++j) {
474 for (
int k = 0; k < c.
num_keys; ++k, ++fk) {
475 rtc_verts[k].
x = verts[fk].x;
476 rtc_verts[k].
y = verts[fk].y;
477 rtc_verts[k].
z = verts[fk].z;
478 rtc_verts[k].
w = curve_radius[fk];
483 for (
int k = 1; k < c.
num_keys + 1; ++k, ++fk) {
484 rtc_verts[k].
x = verts[fk].x;
485 rtc_verts[k].
y = verts[fk].y;
486 rtc_verts[k].
z = verts[fk].z;
487 rtc_verts[k].
w = curve_radius[fk];
490 rtc_verts[0] = rtc_verts[1];
497void BVHEmbree::set_curve_vertex_buffer(RTCGeometry geom_id,
const Hair *hair,
const bool update)
501 size_t num_motion_steps = 1;
503 num_motion_steps = hair->get_motion_steps();
508 for (
size_t j = 0; j < num_curves; ++j) {
514 size_t num_keys_embree = num_keys;
515 num_keys_embree += num_curves * 2;
518 for (
int t = 0; t < num_motion_steps; ++t) {
519 float4 *rtc_verts =
nullptr;
521 rtc_verts = (
float4 *)rtcGetGeometryBufferData(geom_id, RTC_BUFFER_TYPE_VERTEX, t);
524# if RTC_VERSION >= 40400
525 rtcSetNewGeometryBufferHostDevice(
527 rtc_verts = (
float4 *)rtcSetNewGeometryBuffer(
530 RTC_BUFFER_TYPE_VERTEX,
535#
if RTC_VERSION >= 40400
537 (
void **)(&rtc_verts),
546 pack_motion_verts<packed_float3>(
556 rtcUpdateGeometryBuffer(geom_id, RTC_BUFFER_TYPE_VERTEX, t);
561void BVHEmbree::set_point_vertex_buffer(RTCGeometry geom_id,
567 size_t num_motion_steps = 1;
569 num_motion_steps = pointcloud->get_motion_steps();
572 const size_t num_points = pointcloud->
num_points();
575 for (
int t = 0; t < num_motion_steps; ++t) {
577 float4 *rtc_verts =
nullptr;
579 rtc_verts = (
float4 *)rtcGetGeometryBufferData(geom_id, RTC_BUFFER_TYPE_VERTEX, t);
582# if RTC_VERSION >= 40400
583 rtcSetNewGeometryBufferHostDevice(
585 rtc_verts = (
float4 *)rtcSetNewGeometryBuffer(
588 RTC_BUFFER_TYPE_VERTEX,
593#
if RTC_VERSION >= 40400
595 (
void **)(&rtc_verts),
605 for (
size_t j = 0; j < num_points; ++j) {
611 rtcUpdateGeometryBuffer(geom_id, RTC_BUFFER_TYPE_VERTEX, t);
616void BVHEmbree::add_points(
const Object *ob,
const PointCloud *pointcloud,
const int i)
620 size_t num_motion_steps = 1;
624 num_motion_steps = pointcloud->get_motion_steps();
628 const enum RTCGeometryType type = RTC_GEOMETRY_TYPE_SPHERE_POINT;
630 RTCGeometry geom_id = rtcNewGeometry(rtc_device, type);
632 rtcSetGeometryBuildQuality(geom_id, build_quality);
633 rtcSetGeometryTimeStepCount(geom_id, num_motion_steps);
635 set_point_vertex_buffer(geom_id, pointcloud,
false);
638 rtcSetGeometryEnableFilterFunctionFromArguments(geom_id,
true);
640 rtcCommitGeometry(geom_id);
641 rtcAttachGeometryByID(scene, geom_id, i * 2);
642 rtcReleaseGeometry(geom_id);
645static void gsplat_bounds_func(
const RTCBoundsFunctionArguments *args)
647 const auto *pointcloud =
reinterpret_cast<const PointCloud *
>(args->geometryUserPtr);
653 size_t num_motion_steps = 1;
656 num_motion_steps = pointcloud->get_motion_steps();
661 args->timeStep, num_motion_steps)[args->primID];
663 num_motion_steps)[args->primID];
665 RTCBounds *bounds_o = args->bounds_o;
666 bounds_o->lower_x = point.x -
radius;
667 bounds_o->lower_y = point.y -
radius;
668 bounds_o->lower_z = point.z -
radius;
669 bounds_o->upper_x = point.x +
radius;
670 bounds_o->upper_y = point.y +
radius;
671 bounds_o->upper_z = point.z +
radius;
674void BVHEmbree::add_gsplats(
const Object *ob,
const PointCloud *pointcloud,
const int i)
678 size_t num_motion_steps = 1;
682 num_motion_steps = pointcloud->get_motion_steps();
686 RTCGeometry geom_id = rtcNewGeometry(rtc_device, RTC_GEOMETRY_TYPE_USER);
688 rtcSetGeometryBuildQuality(geom_id, build_quality);
690 rtcSetGeometryUserData(geom_id,
const_cast<PointCloud *
>(pointcloud));
693 rtcSetGeometryUserPrimitiveCount(geom_id, pointcloud->
num_points());
694 rtcSetGeometryTimeStepCount(geom_id, num_motion_steps);
695 rtcSetGeometryBoundsFunction(geom_id, gsplat_bounds_func,
nullptr);
697 rtcCommitGeometry(geom_id);
698 rtcAttachGeometryByID(scene, geom_id, i * 2);
699 rtcReleaseGeometry(geom_id);
702void BVHEmbree::add_curves(
const Object *ob,
const Hair *hair,
const int i)
705 size_t num_motion_steps = 1;
707 num_motion_steps = hair->get_motion_steps();
710 assert(num_motion_steps <= RTC_MAX_TIME_STEP_COUNT);
711 num_motion_steps =
min(num_motion_steps, (
size_t)RTC_MAX_TIME_STEP_COUNT);
714 size_t num_segments = 0;
715 for (
size_t j = 0; j < num_curves; ++j) {
722 RTC_GEOMETRY_TYPE_ROUND_LINEAR_CURVE :
724 RTC_GEOMETRY_TYPE_FLAT_CATMULL_ROM_CURVE :
725 RTC_GEOMETRY_TYPE_ROUND_CATMULL_ROM_CURVE);
727 RTCGeometry geom_id = rtcNewGeometry(rtc_device, type);
728 rtcSetGeometryTessellationRate(geom_id, params.curve_subdivisions + 1);
729 unsigned *rtc_indices =
nullptr;
730# if RTC_VERSION >= 40400
731 rtcSetNewGeometryBufferHostDevice(
733 rtc_indices = (
unsigned *)rtcSetNewGeometryBuffer(
736 RTC_BUFFER_TYPE_INDEX,
741#
if RTC_VERSION >= 40400
743 (
void **)(&rtc_indices),
748 size_t rtc_index = 0;
749 for (
size_t j = 0; j < num_curves; ++j) {
752 rtc_indices[rtc_index] = c.
first_key + k;
755 rtc_indices[rtc_index] += j * 2;
762 rtcSetGeometryBuildQuality(geom_id, build_quality);
763 rtcSetGeometryTimeStepCount(geom_id, num_motion_steps);
765 set_curve_vertex_buffer(geom_id, hair,
false);
768 rtcSetGeometryEnableFilterFunctionFromArguments(geom_id,
true);
770 rtcCommitGeometry(geom_id);
771 rtcAttachGeometryByID(scene, geom_id, i * 2 + 1);
772 rtcReleaseGeometry(geom_id);
775void BVHEmbree::refit(
Progress &progress)
780 unsigned geom_id = 0;
781 for (
Object *ob : objects) {
782 if (!params.top_level || (ob->
is_traceable() && !ob->get_geometry()->is_instanced())) {
783 Geometry *geom = ob->get_geometry();
786 Mesh *mesh =
static_cast<Mesh *
>(geom);
788 RTCGeometry geom = rtcGetGeometry(scene, geom_id);
789 set_tri_vertex_buffer(geom, mesh,
true);
790 rtcCommitGeometry(geom);
794 Hair *hair =
static_cast<Hair *
>(geom);
796 RTCGeometry geom = rtcGetGeometry(scene, geom_id + 1);
797 set_curve_vertex_buffer(geom, hair,
true);
798 rtcCommitGeometry(geom);
804 RTCGeometry geom = rtcGetGeometry(scene, geom_id);
806 set_point_vertex_buffer(geom, pointcloud,
true);
810 rtcSetGeometryUserData(geom,
const_cast<PointCloud *
>(pointcloud));
811 rtcSetGeometryUserPrimitiveCount(geom, pointcloud->
num_points());
812 rtcSetGeometryBoundsFunction(geom, gsplat_bounds_func,
nullptr);
814 rtcCommitGeometry(geom);
821 rtcCommitScene(scene);
#define SIMD_SET_FLUSH_TO_ZERO
ATOMIC_INLINE size_t atomic_add_and_fetch_z(size_t *p, size_t x)
ATOMIC_INLINE size_t atomic_sub_and_fetch_z(size_t *p, size_t x)
SIMD_FORCE_INLINE const btScalar & z() const
Return the z value.
Attribute * find(ustring name) const
const T * data_at_time_step(const int step, const int num_time_steps) const
static const uint MAX_MOTION_STEPS
bool is_pointcloud() const
virtual bool has_motion_blur() const
bool is_traceable() const
Curve get_curve(const size_t i) const
size_t num_curves() const
CurveShapeType curve_shape
NODE_DECLARE size_t num_verts() const
bool has_motion_blur() const override
size_t num_triangles() const
static const uint MAX_MOTION_STEPS
bool is_traceable() const
uint visibility_for_tracing() const
size_t num_points() const
PrimitiveType primitive_type() const override
void set_substatus(const string &substatus_)
void mem_alloc(const size_t size)
void mem_free(const size_t size)
#define CCL_NAMESPACE_END
#define assert(assertion)
VecBase< float, D > step(VecOp< float, D >, VecOp< float, D >) RET
void add_curves(bke::CurvesGeometry &curves, const Span< int > new_sizes)
constexpr StringRefNull radius
CCL_NAMESPACE_BEGIN string string_printf(const char *format,...)
CCL_NAMESPACE_BEGIN double time_dt()