Blender V5.3
embree.cpp
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2018-2022 Blender Foundation
2 *
3 * SPDX-License-Identifier: Apache-2.0 */
4
5/* This class implements a ray accelerator for Cycles using Intel's Embree library.
6 * It supports triangles, curves, object and deformation blur and instancing.
7 *
8 * Since Embree allows object to be either curves or triangles but not both, Cycles object IDs are
9 * mapped to Embree IDs by multiplying by two and adding one for curves.
10 *
11 * This implementation shares RTCDevices between Cycles instances. Eventually each instance should
12 * get a separate RTCDevice to correctly keep track of memory usage.
13 *
14 * Vertex and index buffers are duplicated between Cycles device arrays and Embree. These could be
15 * merged, which would require changes to intersection refinement, shader setup, mesh light
16 * sampling and a few other places in Cycles where direct access to vertex data is required.
17 */
18
19#ifdef WITH_EMBREE
20
21# include <embree4/rtcore_geometry.h>
22
23# include "bvh/embree.h"
24
25# include "scene/hair.h"
26# include "scene/mesh.h"
27# include "scene/object.h"
28# include "scene/pointcloud.h"
29
30# include "util/log.h"
31# include "util/progress.h"
32# include "util/stats.h"
33
35
36static_assert(Object::MAX_MOTION_STEPS <= RTC_MAX_TIME_STEP_COUNT,
37 "Object and Embree max motion steps inconsistent");
39 "Object and Geometry max motion steps inconsistent");
40
41static size_t unaccounted_mem = 0;
42
43static bool rtc_memory_monitor_func(void *userPtr, const ssize_t bytes, const bool /*unused*/)
44{
45 Stats *stats = (Stats *)userPtr;
46 if (stats) {
47 if (bytes > 0) {
48 stats->mem_alloc(bytes);
49 }
50 else {
51 stats->mem_free(-bytes);
52 }
53 }
54 else {
55 /* A stats pointer may not yet be available. Keep track of the memory usage for later. */
56 if (bytes >= 0) {
57 atomic_add_and_fetch_z(&unaccounted_mem, bytes);
58 }
59 else {
60 atomic_sub_and_fetch_z(&unaccounted_mem, -bytes);
61 }
62 }
63 return true;
64}
65
66static void rtc_error_func(void * /*unused*/, enum RTCError /*unused*/, const char *str)
67{
69}
70
71static double progress_start_time = 0.0;
72
73static bool rtc_progress_func(void *user_ptr, const double n)
74{
75 Progress *progress = (Progress *)user_ptr;
76
77 if (time_dt() - progress_start_time < 0.25) {
78 return true;
79 }
80
81 const string msg = string_printf("Building BVH %.0f%%", n * 100.0);
82 progress->set_substatus(msg);
83 progress_start_time = time_dt();
84
85 return !progress->get_cancel();
86}
87
88BVHEmbree::BVHEmbree(const BVHParams &params_,
89 const vector<Geometry *> &geometry_,
90 const vector<Object *> &objects_)
91 : BVH(params_, geometry_, objects_),
92 scene(nullptr),
93 rtc_device(nullptr),
94 rtc_device_is_sycl(false),
95 build_quality(RTC_BUILD_QUALITY_REFIT)
96{
98}
99
100BVHEmbree::~BVHEmbree()
101{
102 if (scene) {
103 rtcReleaseScene(scene);
104 }
105}
106
107void BVHEmbree::build(Progress &progress,
108 Stats *stats,
109 RTCDevice rtc_device_,
110 const bool rtc_device_is_sycl_)
111{
112 rtc_device = rtc_device_;
113 rtc_device_is_sycl = rtc_device_is_sycl_;
114 assert(rtc_device);
115
116 rtcSetDeviceErrorFunction(rtc_device, rtc_error_func, nullptr);
117 rtcSetDeviceMemoryMonitorFunction(rtc_device, rtc_memory_monitor_func, stats);
118
119 progress.set_substatus("Building BVH");
120
121 if (scene) {
122 rtcReleaseScene(scene);
123 scene = nullptr;
124 }
125
126 const bool dynamic = params.bvh_type == BVH_TYPE_DYNAMIC;
127 const bool compact = params.use_compact_structure;
128
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) {
139 /* To work around a known issue in the Intel GPU driver regarding the High
140 * quality BVH build option, we reduce it to Medium. There is no impact on
141 * render quality from this change. There is a small expected performance
142 * impact on intersection speed and BVH building speed, but this is
143 * unavoidable at the moment - using High quality leads to crashes, so we
144 * have no choice. This workaround can be removed once the fix appears in
145 * public drivers and we raise the minimum oneAPI backend driver version
146 * accordingly. */
147 /* This workaround is applied only to GPU, per Sergey. See #158123. */
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;
153 }
154 rtcSetSceneBuildQuality(scene, build_quality);
155
156 int i = 0;
157 for (Object *ob : objects) {
158 if (params.top_level) {
159 if (!ob->is_traceable()) {
160 ++i;
161 continue;
162 }
163 if (!ob->get_geometry()->is_instanced()) {
164 add_object(ob, i);
165 }
166 else {
167 add_instance(ob, i);
168 }
169 }
170 else {
171 add_object(ob, i);
172 }
173 ++i;
174 if (progress.get_cancel()) {
175 return;
176 }
177 }
178
179 if (progress.get_cancel()) {
180 return;
181 }
182
183 rtcSetSceneProgressMonitorFunction(scene, rtc_progress_func, &progress);
184 rtcCommitScene(scene);
185}
186
187const char *BVHEmbree::get_error_string(RTCError error_code)
188{
189# if RTC_VERSION >= 40303
190 return rtcGetErrorString(error_code);
191# else
192 switch (error_code) {
193 case RTC_ERROR_NONE:
194 return "no error";
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:
206 return "cancelled";
207 default:
208 /* We should never end here unless enum for RTC errors would change. */
209 return "unknown error";
210 }
211# endif
212}
213
214# if defined(WITH_EMBREE_GPU) && RTC_VERSION >= 40302
215/* offload_scenes_to_gpu() uses rtcGetDeviceError() which also resets Embree error status,
216 * we propagate its value so it doesn't get lost. */
217RTCError BVHEmbree::offload_scenes_to_gpu(const vector<RTCScene> &scenes)
218{
219 /* Having BVH on GPU is more performance-critical than texture data.
220 * In order to ensure good performance even when running out of GPU
221 * memory, we force BVH to migrate to GPU before allocating other textures
222 * that may not fit. */
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);
228 /* In case of any errors from Embree, we should stop
229 * the execution and propagate the error. */
230 RTCError error_code = rtcGetDeviceError(rtc_device);
231 if (error_code != RTC_ERROR_NONE) {
232 return error_code;
233 }
234 }
235 return RTC_ERROR_NONE;
236}
237# endif
238
239void BVHEmbree::add_object(Object *ob, const int i)
240{
241 Geometry *geom = ob->get_geometry();
242
243 if (geom->is_mesh() || geom->is_volume()) {
244 Mesh *mesh = static_cast<Mesh *>(geom);
245 if (mesh->num_triangles() > 0) {
246 add_triangles(ob, mesh, i);
247 }
248 }
249 else if (geom->is_hair()) {
250 Hair *hair = static_cast<Hair *>(geom);
251 if (hair->is_traceable()) {
252 add_curves(ob, hair, i);
253 }
254 }
255 else if (geom->is_pointcloud()) {
256 PointCloud *pointcloud = static_cast<PointCloud *>(geom);
257 if (pointcloud->num_points() > 0) {
258 if (pointcloud->primitive_type() & PRIMITIVE_POINT) {
259 add_points(ob, pointcloud, i);
260 }
261 else {
262 add_gsplats(ob, pointcloud, i);
263 }
264 }
265 }
266}
267
268void BVHEmbree::add_instance(Object *ob, const int i)
269{
270 BVHEmbree *instance_bvh = static_cast<BVHEmbree *>(ob->get_geometry()->bvh.get());
271 assert(instance_bvh != nullptr);
272
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);
276
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);
280
281 if (ob->use_motion()) {
282 array<DecomposedTransform> decomp(ob->get_motion().size());
283 transform_motion_decompose(decomp.data(), ob->get_motion().data(), ob->get_motion().size());
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);
296 }
297 }
298 else {
299 rtcSetGeometryTransform(
300 geom_id, 0, RTC_FORMAT_FLOAT3X4_ROW_MAJOR, (const float *)&ob->get_tfm());
301 }
302
303 rtcSetGeometryUserData(geom_id,
304# if RTC_VERSION >= 40400
305 (void *)rtcGetSceneTraversable(instance_bvh->scene)
306# else
307 (void *)instance_bvh->scene
308# endif
309 );
310
311 rtcSetGeometryMask(geom_id, ob->visibility_for_tracing());
312 rtcSetGeometryEnableFilterFunctionFromArguments(geom_id, true);
313
314 rtcCommitGeometry(geom_id);
315 rtcAttachGeometryByID(scene, geom_id, i * 2);
316 rtcReleaseGeometry(geom_id);
317}
318
319void BVHEmbree::add_triangles(const Object *ob, const Mesh *mesh, const int i)
320{
321 const Attribute *attr_P = nullptr;
322 size_t num_motion_steps = 1;
323 if (mesh->has_motion_blur()) {
324 attr_P = mesh->attributes.find(ATTR_STD_POSITION);
325 if (attr_P->has_motion()) {
326 num_motion_steps = mesh->get_motion_steps();
327 }
328 }
329
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);
332
333 const size_t num_triangles = mesh->num_triangles();
334
335 RTCGeometry geom_id = rtcNewGeometry(rtc_device, RTC_GEOMETRY_TYPE_TRIANGLE);
336 rtcSetGeometryBuildQuality(geom_id, build_quality);
337 rtcSetGeometryTimeStepCount(geom_id, num_motion_steps);
338
339 const int *triangles = mesh->get_triangles().data();
340 if (!rtc_device_is_sycl) {
341 rtcSetSharedGeometryBuffer(geom_id,
342 RTC_BUFFER_TYPE_INDEX,
343 0,
344 RTC_FORMAT_UINT3,
345 triangles,
346 0,
347 sizeof(int) * 3,
348 num_triangles);
349 }
350 else {
351 /* NOTE(sirgienko): If the Embree device is a SYCL device, then Embree execution will
352 * happen on GPU, and we cannot use standard host pointers at this point. So instead
353 * of making a shared geometry buffer - a new Embree buffer will be created and data
354 * will be copied. */
355 int *triangles_buffer = nullptr;
356# if RTC_VERSION >= 40400
357 rtcSetNewGeometryBufferHostDevice(
358# else
359 triangles_buffer = (int *)rtcSetNewGeometryBuffer(
360# endif
361 geom_id,
362 RTC_BUFFER_TYPE_INDEX,
363 0,
364 RTC_FORMAT_UINT3,
365 sizeof(int) * 3,
366 num_triangles
367# if RTC_VERSION >= 40400
368 ,
369 (void **)(&triangles_buffer),
370 nullptr
371# endif
372 );
373 assert(triangles_buffer);
374 if (triangles_buffer) {
375 static_assert(sizeof(int) == sizeof(uint));
376 std::memcpy(triangles_buffer, triangles, sizeof(int) * 3 * (num_triangles));
377 }
378 }
379 set_tri_vertex_buffer(geom_id, mesh, false);
380
381 rtcSetGeometryMask(geom_id, ob->visibility_for_tracing());
382 rtcSetGeometryEnableFilterFunctionFromArguments(geom_id, true);
383
384 rtcCommitGeometry(geom_id);
385 rtcAttachGeometryByID(scene, geom_id, i * 2);
386 rtcReleaseGeometry(geom_id);
387}
388
389void BVHEmbree::set_tri_vertex_buffer(RTCGeometry geom_id, const Mesh *mesh, const bool update)
390{
391 const Attribute *attr_P = mesh->attributes.find(ATTR_STD_POSITION);
392 size_t num_motion_steps = 1;
393 if (mesh->has_motion_blur() && attr_P->has_motion()) {
394 num_motion_steps = mesh->get_motion_steps();
395 if (num_motion_steps > RTC_MAX_TIME_STEP_COUNT) {
396 assert(0);
397 num_motion_steps = RTC_MAX_TIME_STEP_COUNT;
398 }
399 }
400 const size_t num_verts = mesh->num_verts();
401
402 for (int t = 0; t < num_motion_steps; ++t) {
403 const packed_float3 *verts = attr_P->data_at_time_step<packed_float3>(t, num_motion_steps);
404
405 if (update) {
406 rtcUpdateGeometryBuffer(geom_id, RTC_BUFFER_TYPE_VERTEX, t);
407 }
408 else {
409 if (!rtc_device_is_sycl) {
410 rtcSetSharedGeometryBuffer(geom_id,
411 RTC_BUFFER_TYPE_VERTEX,
412 t,
413 RTC_FORMAT_FLOAT3,
414 verts,
415 0,
416 sizeof(packed_float3),
417 num_verts);
418 }
419 else {
420 /* NOTE(sirgienko): If the Embree device is a SYCL device, then Embree execution will
421 * happen on GPU, and we cannot use standard host pointers at this point. So instead
422 * of making a shared geometry buffer - a new Embree buffer will be created and data
423 * will be copied. */
424 /* As float3 is packed on GPU side, we map it to packed_float3. */
425 /* There is no need for additional padding in rtcSetNewGeometryBuffer since Embree 3.6:
426 * "Fixed automatic vertex buffer padding when using rtcSetNewGeometry API function". */
427 packed_float3 *verts_buffer = nullptr;
428# if RTC_VERSION >= 40400
429 rtcSetNewGeometryBufferHostDevice(
430# else
431 verts_buffer = (packed_float3 *)rtcSetNewGeometryBuffer(
432# endif
433 geom_id,
434 RTC_BUFFER_TYPE_VERTEX,
435 t,
436 RTC_FORMAT_FLOAT3,
437 sizeof(packed_float3),
438 num_verts
439# if RTC_VERSION >= 40400
440 ,
441 (void **)(&verts_buffer),
442 nullptr
443# endif
444 );
445 assert(verts_buffer);
446 if (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;
451 }
452 }
453 }
454 }
455 }
456}
457
461template<typename T>
462void pack_motion_verts(const size_t num_curves,
463 const Hair *hair,
464 const T *verts,
465 const float *curve_radius,
466 float4 *rtc_verts,
467 CurveShapeType curve_shape)
468{
469 for (size_t j = 0; j < num_curves; ++j) {
470 const Hair::Curve c = hair->get_curve(j);
471 int fk = c.first_key;
472
473 if (curve_shape == CURVE_THICK_LINEAR) {
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];
479 }
480 rtc_verts += c.num_keys;
481 }
482 else {
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];
488 }
489 /* Duplicate Embree's Catmull-Rom spline CVs at the start and end of each curve. */
490 rtc_verts[0] = rtc_verts[1];
491 rtc_verts[c.num_keys + 1] = rtc_verts[c.num_keys];
492 rtc_verts += c.num_keys + 2;
493 }
494 }
495}
496
497void BVHEmbree::set_curve_vertex_buffer(RTCGeometry geom_id, const Hair *hair, const bool update)
498{
499 const Attribute *attr_P = hair->attributes.find(ATTR_STD_POSITION);
500 const Attribute *attr_R = hair->attributes.find(ATTR_STD_RADIUS);
501 size_t num_motion_steps = 1;
502 if (hair->has_motion_blur() && attr_P->has_motion()) {
503 num_motion_steps = hair->get_motion_steps();
504 }
505
506 const size_t num_curves = hair->num_curves();
507 size_t num_keys = 0;
508 for (size_t j = 0; j < num_curves; ++j) {
509 const Hair::Curve c = hair->get_curve(j);
510 num_keys += c.num_keys;
511 }
512
513 /* Catmull-Rom splines need extra CVs at the beginning and end of each curve. */
514 size_t num_keys_embree = num_keys;
515 num_keys_embree += num_curves * 2;
516
517 /* Copy the CV data to Embree */
518 for (int t = 0; t < num_motion_steps; ++t) {
519 float4 *rtc_verts = nullptr;
520 if (update) {
521 rtc_verts = (float4 *)rtcGetGeometryBufferData(geom_id, RTC_BUFFER_TYPE_VERTEX, t);
522 }
523 else {
524# if RTC_VERSION >= 40400
525 rtcSetNewGeometryBufferHostDevice(
526# else
527 rtc_verts = (float4 *)rtcSetNewGeometryBuffer(
528# endif
529 geom_id,
530 RTC_BUFFER_TYPE_VERTEX,
531 t,
532 RTC_FORMAT_FLOAT4,
533 sizeof(float) * 4,
534 num_keys_embree
535# if RTC_VERSION >= 40400
536 ,
537 (void **)(&rtc_verts),
538 nullptr
539# endif
540 );
541 }
542
543 assert(rtc_verts);
544 if (rtc_verts) {
545 const size_t num_curves = hair->num_curves();
546 pack_motion_verts<packed_float3>(
547 num_curves,
548 hair,
549 attr_P->data_at_time_step<packed_float3>(t, num_motion_steps),
550 attr_R->data_at_time_step<float>(t, num_motion_steps),
551 rtc_verts,
552 hair->curve_shape);
553 }
554
555 if (update) {
556 rtcUpdateGeometryBuffer(geom_id, RTC_BUFFER_TYPE_VERTEX, t);
557 }
558 }
559}
560
561void BVHEmbree::set_point_vertex_buffer(RTCGeometry geom_id,
562 const PointCloud *pointcloud,
563 const bool update)
564{
565 const Attribute *attr_P = pointcloud->attributes.find(ATTR_STD_POSITION);
566 const Attribute *attr_R = pointcloud->attributes.find(ATTR_STD_RADIUS);
567 size_t num_motion_steps = 1;
568 if (pointcloud->has_motion_blur() && attr_P->has_motion()) {
569 num_motion_steps = pointcloud->get_motion_steps();
570 }
571
572 const size_t num_points = pointcloud->num_points();
573
574 /* Copy the point data to Embree. */
575 for (int t = 0; t < num_motion_steps; ++t) {
576
577 float4 *rtc_verts = nullptr;
578 if (update) {
579 rtc_verts = (float4 *)rtcGetGeometryBufferData(geom_id, RTC_BUFFER_TYPE_VERTEX, t);
580 }
581 else {
582# if RTC_VERSION >= 40400
583 rtcSetNewGeometryBufferHostDevice(
584# else
585 rtc_verts = (float4 *)rtcSetNewGeometryBuffer(
586# endif
587 geom_id,
588 RTC_BUFFER_TYPE_VERTEX,
589 t,
590 RTC_FORMAT_FLOAT4,
591 sizeof(float) * 4,
592 num_points
593# if RTC_VERSION >= 40400
594 ,
595 (void **)(&rtc_verts),
596 nullptr
597# endif
598 );
599 }
600
601 assert(rtc_verts);
602 if (rtc_verts) {
603 const packed_float3 *verts = attr_P->data_at_time_step<packed_float3>(t, num_motion_steps);
604 const float *radius = attr_R->data_at_time_step<float>(t, num_motion_steps);
605 for (size_t j = 0; j < num_points; ++j) {
606 rtc_verts[j] = make_float4(float3(verts[j]), radius[j]);
607 }
608 }
609
610 if (update) {
611 rtcUpdateGeometryBuffer(geom_id, RTC_BUFFER_TYPE_VERTEX, t);
612 }
613 }
614}
615
616void BVHEmbree::add_points(const Object *ob, const PointCloud *pointcloud, const int i)
617{
618 assert(pointcloud->primitive_type() & PRIMITIVE_POINT);
619
620 size_t num_motion_steps = 1;
621 if (pointcloud->has_motion_blur()) {
622 const Attribute *attr_P = pointcloud->attributes.find(ATTR_STD_POSITION);
623 if (attr_P->has_motion()) {
624 num_motion_steps = pointcloud->get_motion_steps();
625 }
626 }
627
628 const enum RTCGeometryType type = RTC_GEOMETRY_TYPE_SPHERE_POINT;
629
630 RTCGeometry geom_id = rtcNewGeometry(rtc_device, type);
631
632 rtcSetGeometryBuildQuality(geom_id, build_quality);
633 rtcSetGeometryTimeStepCount(geom_id, num_motion_steps);
634
635 set_point_vertex_buffer(geom_id, pointcloud, false);
636
637 rtcSetGeometryMask(geom_id, ob->visibility_for_tracing());
638 rtcSetGeometryEnableFilterFunctionFromArguments(geom_id, true);
639
640 rtcCommitGeometry(geom_id);
641 rtcAttachGeometryByID(scene, geom_id, i * 2);
642 rtcReleaseGeometry(geom_id);
643}
644
645static void gsplat_bounds_func(const RTCBoundsFunctionArguments *args)
646{
647 const auto *pointcloud = reinterpret_cast<const PointCloud *>(args->geometryUserPtr);
648
649 /* TODO(sergey): Somehow avoid attribute lookup on every gsplat. */
650 const Attribute *attr_P = pointcloud->attributes.find(ATTR_STD_POSITION);
651 const Attribute *attr_R = pointcloud->attributes.find(ATTR_STD_RADIUS);
652
653 size_t num_motion_steps = 1;
654 if (pointcloud->has_motion_blur()) {
655 if (attr_P->has_motion()) {
656 num_motion_steps = pointcloud->get_motion_steps();
657 }
658 }
659
660 const packed_float3 point = attr_P->data_at_time_step<packed_float3>(
661 args->timeStep, num_motion_steps)[args->primID];
662 const float radius = attr_R->data_at_time_step<float>(args->timeStep,
663 num_motion_steps)[args->primID];
664
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;
672}
673
674void BVHEmbree::add_gsplats(const Object *ob, const PointCloud *pointcloud, const int i)
675{
676 assert(pointcloud->primitive_type() & PRIMITIVE_GSPLAT);
677
678 size_t num_motion_steps = 1;
679 if (pointcloud->has_motion_blur()) {
680 const Attribute *attr_P = pointcloud->attributes.find(ATTR_STD_POSITION);
681 if (attr_P->has_motion()) {
682 num_motion_steps = pointcloud->get_motion_steps();
683 }
684 }
685
686 RTCGeometry geom_id = rtcNewGeometry(rtc_device, RTC_GEOMETRY_TYPE_USER);
687
688 rtcSetGeometryBuildQuality(geom_id, build_quality);
689
690 rtcSetGeometryUserData(geom_id, const_cast<PointCloud *>(pointcloud));
691 rtcSetGeometryMask(geom_id, ob->visibility_for_tracing());
692
693 rtcSetGeometryUserPrimitiveCount(geom_id, pointcloud->num_points());
694 rtcSetGeometryTimeStepCount(geom_id, num_motion_steps);
695 rtcSetGeometryBoundsFunction(geom_id, gsplat_bounds_func, nullptr);
696
697 rtcCommitGeometry(geom_id);
698 rtcAttachGeometryByID(scene, geom_id, i * 2);
699 rtcReleaseGeometry(geom_id);
700}
701
702void BVHEmbree::add_curves(const Object *ob, const Hair *hair, const int i)
703{
704 const Attribute *attr_P = hair->attributes.find(ATTR_STD_POSITION);
705 size_t num_motion_steps = 1;
706 if (hair->has_motion_blur() && attr_P->has_motion()) {
707 num_motion_steps = hair->get_motion_steps();
708 }
709
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);
712
713 const size_t num_curves = hair->num_curves();
714 size_t num_segments = 0;
715 for (size_t j = 0; j < num_curves; ++j) {
716 const Hair::Curve c = hair->get_curve(j);
717 assert(c.num_segments() > 0);
718 num_segments += c.num_segments();
719 }
720
721 const enum RTCGeometryType type = (hair->curve_shape == CURVE_THICK_LINEAR ?
722 RTC_GEOMETRY_TYPE_ROUND_LINEAR_CURVE :
723 hair->curve_shape == CURVE_RIBBON ?
724 RTC_GEOMETRY_TYPE_FLAT_CATMULL_ROM_CURVE :
725 RTC_GEOMETRY_TYPE_ROUND_CATMULL_ROM_CURVE);
726
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(
732# else
733 rtc_indices = (unsigned *)rtcSetNewGeometryBuffer(
734# endif
735 geom_id,
736 RTC_BUFFER_TYPE_INDEX,
737 0,
738 RTC_FORMAT_UINT,
739 sizeof(int),
740 num_segments
741# if RTC_VERSION >= 40400
742 ,
743 (void **)(&rtc_indices),
744 nullptr
745# endif
746 );
747
748 size_t rtc_index = 0;
749 for (size_t j = 0; j < num_curves; ++j) {
750 const Hair::Curve c = hair->get_curve(j);
751 for (size_t k = 0; k < c.num_segments(); ++k) {
752 rtc_indices[rtc_index] = c.first_key + k;
753 if (hair->curve_shape != CURVE_THICK_LINEAR) {
754 /* Room for extra CVs at Catmull-Rom splines. */
755 rtc_indices[rtc_index] += j * 2;
756 }
757
758 ++rtc_index;
759 }
760 }
761
762 rtcSetGeometryBuildQuality(geom_id, build_quality);
763 rtcSetGeometryTimeStepCount(geom_id, num_motion_steps);
764
765 set_curve_vertex_buffer(geom_id, hair, false);
766
767 rtcSetGeometryMask(geom_id, ob->visibility_for_tracing());
768 rtcSetGeometryEnableFilterFunctionFromArguments(geom_id, true);
769
770 rtcCommitGeometry(geom_id);
771 rtcAttachGeometryByID(scene, geom_id, i * 2 + 1);
772 rtcReleaseGeometry(geom_id);
773}
774
775void BVHEmbree::refit(Progress &progress)
776{
777 progress.set_substatus("Refitting BVH nodes");
778
779 /* Update all vertex buffers, then tell Embree to rebuild/-fit the BVHs. */
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();
784
785 if (geom->is_mesh() || geom->is_volume()) {
786 Mesh *mesh = static_cast<Mesh *>(geom);
787 if (mesh->num_triangles() > 0) {
788 RTCGeometry geom = rtcGetGeometry(scene, geom_id);
789 set_tri_vertex_buffer(geom, mesh, true);
790 rtcCommitGeometry(geom);
791 }
792 }
793 else if (geom->is_hair()) {
794 Hair *hair = static_cast<Hair *>(geom);
795 if (hair->is_traceable()) {
796 RTCGeometry geom = rtcGetGeometry(scene, geom_id + 1);
797 set_curve_vertex_buffer(geom, hair, true);
798 rtcCommitGeometry(geom);
799 }
800 }
801 else if (geom->is_pointcloud()) {
802 const PointCloud *pointcloud = static_cast<const PointCloud *>(geom);
803 if (pointcloud->num_points() > 0) {
804 RTCGeometry geom = rtcGetGeometry(scene, geom_id);
805 if (pointcloud->primitive_type() & PRIMITIVE_POINT) {
806 set_point_vertex_buffer(geom, pointcloud, true);
807 }
808 else {
809 assert(pointcloud->primitive_type() & PRIMITIVE_GSPLAT);
810 rtcSetGeometryUserData(geom, const_cast<PointCloud *>(pointcloud));
811 rtcSetGeometryUserPrimitiveCount(geom, pointcloud->num_points());
812 rtcSetGeometryBoundsFunction(geom, gsplat_bounds_func, nullptr);
813 }
814 rtcCommitGeometry(geom);
815 }
816 }
817 }
818 geom_id += 2;
819 }
820
821 rtcCommitScene(scene);
822}
823
825
826#endif /* WITH_EMBREE */
#define SIMD_SET_FLUSH_TO_ZERO
Definition BLI_simd.hh:91
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.
Definition btQuadWord.h:117
Attribute * find(ustring name) const
bool has_motion() const
const T * data_at_time_step(const int step, const int num_time_steps) const
Definition bvh/bvh.h:67
static const uint MAX_MOTION_STEPS
bool is_volume() const
bool is_pointcloud() const
bool is_hair() const
virtual bool has_motion_blur() const
AttributeSet attributes
bool is_mesh() const
Definition hair.h:13
bool is_traceable() const
Definition hair.h:119
Curve get_curve(const size_t i) const
Definition hair.h:94
size_t num_curves() const
Definition hair.h:109
CurveShapeType curve_shape
Definition hair.h:77
NODE_DECLARE size_t num_verts() const
bool has_motion_blur() const override
size_t num_triangles() const
Definition scene/mesh.h:73
bool use_motion() 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_)
Definition progress.h:259
bool get_cancel() const
Definition progress.h:77
void mem_alloc(const size_t size)
Definition util/stats.h:18
void mem_free(const size_t size)
Definition util/stats.h:24
#define CCL_NAMESPACE_END
#define str(s)
#define assert(assertion)
VecBase< float, D > step(VecOp< float, D >, VecOp< float, D >) RET
@ PRIMITIVE_GSPLAT
@ PRIMITIVE_POINT
CurveShapeType
@ CURVE_RIBBON
@ CURVE_THICK_LINEAR
@ ATTR_STD_POSITION
@ ATTR_STD_RADIUS
#define LOG_WARNING
Definition log.h:103
#define LOG_INFO
Definition log.h:106
#define T
void add_curves(bke::CurvesGeometry &curves, const Span< int > new_sizes)
unsigned int uint
Definition BKE_text.h:55
@ BVH_TYPE_DYNAMIC
Definition params.h:33
#define make_float4
#define min(a, b)
Definition sort.cc:38
CCL_NAMESPACE_BEGIN string string_printf(const char *format,...)
Definition string.cpp:25
int first_key
Definition hair.h:19
int num_segments() const
Definition hair.h:22
int num_keys
Definition hair.h:20
float y
Definition sky_math.h:225
float z
Definition sky_math.h:225
float x
Definition sky_math.h:225
float w
Definition sky_math.h:225
CCL_NAMESPACE_BEGIN double time_dt()
Definition time.cpp:31
void transform_motion_decompose(DecomposedTransform *decomp, const Transform *motion, const size_t size)