19# include "device/metal/bvh.h"
24# define BVH_status(...) \
26 string str = string_printf(__VA_ARGS__); \
27 progress.set_substatus(str); \
28 metal_printf("%s", str.c_str()); \
32# ifdef BVH_THROTTLE_DIAGNOSTICS
33# define bvh_throttle_printf(...) printf("BVHMetalBuildThrottler::" __VA_ARGS__)
35# define bvh_throttle_printf(...)
41# ifndef MAC_OS_VERSION_26_0
42# define MTLAccelerationStructureUsagePreferFastIntersection \
43 MTLAccelerationStructureUsage(1 << 4)
48struct BVHMetalBuildThrottler {
50 size_t wired_memory = 0;
51 size_t safe_wired_limit = 0;
52 int requests_in_flight = 0;
54 BVHMetalBuildThrottler()
58 id<MTLDevice> mtlDevice = MTLCreateSystemDefaultDevice();
61 safe_wired_limit = [mtlDevice recommendedMaxWorkingSetSize] / 4;
62 bvh_throttle_printf(
"safe_wired_limit = %zu\n", safe_wired_limit);
66 void acquire(
const size_t bytes_to_be_wired)
68 bool throttled =
false;
75 if (wired_memory == 0 || wired_memory + bytes_to_be_wired <= safe_wired_limit) {
76 wired_memory += bytes_to_be_wired;
77 requests_in_flight += 1;
78 bvh_throttle_printf(
"acquire -- success (requests_in_flight = %d, wired_memory = %zu)\n",
86 "acquire -- throttling (requests_in_flight = %d, wired_memory = %zu, "
87 "bytes_to_be_wired = %zu)\n",
95 std::this_thread::sleep_for(std::chrono::milliseconds(10));
100 void release(
const size_t bytes_just_unwired)
103 wired_memory -= bytes_just_unwired;
104 requests_in_flight -= 1;
105 bvh_throttle_printf(
"release (requests_in_flight = %d, wired_memory = %zu)\n",
116 if (wired_memory == 0) {
120 std::this_thread::sleep_for(std::chrono::milliseconds(10));
123} g_bvh_build_throttler;
130static bool support_refit_blas()
132 if (@available(macos 15.4, *)) {
135 if (@available(macos 15.2, *)) {
141BVHMetal::BVHMetal(
const BVHParams ¶ms_,
145 :
BVH(params_, geometry_, objects_), device(device)
151 set_accel_struct(nil);
157API_AVAILABLE(macos(11.0))
158void BVHMetal::set_accel_struct(
id<MTLAccelerationStructure> new_accel_struct)
161 [accel_struct release];
165 if (new_accel_struct) {
166 accel_struct = new_accel_struct;
170bool BVHMetal::build_BLAS_mesh(
Progress &progress,
171 id<MTLDevice> mtl_device,
172 id<MTLCommandQueue> queue,
177 Mesh *
const mesh =
static_cast<Mesh *const
>(geom);
182 const bool use_fast_trace_bvh = (params.bvh_type ==
BVH_TYPE_STATIC) || !support_refit_blas();
185 const array<int> &tris = mesh->get_triangles();
186 const size_t num_verts = mesh->
num_verts();
187 const size_t num_indices = tris.
size();
189 size_t num_motion_steps = 1;
191 if (motion_blur && mesh->get_use_motion_blur() && attr_P->
has_motion()) {
192 num_motion_steps = mesh->get_motion_steps();
196 id<MTLBuffer> posBuf = nil;
197 id<MTLBuffer> indexBuf = [mtl_device newBufferWithBytes:tris.
data()
199 options:MTLResourceStorageModeShared];
201 if (num_motion_steps == 1) {
202 posBuf = [mtl_device newBufferWithBytes:verts
203 length:num_verts *
sizeof(verts[0])
204 options:MTLResourceStorageModeShared];
207 posBuf = [mtl_device newBufferWithLength:num_verts * num_motion_steps *
sizeof(verts[0])
208 options:MTLResourceStorageModeShared];
210 for (
size_t step = 0;
step < num_motion_steps; ++
step) {
213 std::copy_n(verts, num_verts, dest_data + num_verts *
step);
218 MTLAccelerationStructureGeometryDescriptor *geomDesc;
219 if (num_motion_steps > 1) {
220 std::vector<MTLMotionKeyframeData *> vertex_ptrs;
221 vertex_ptrs.reserve(num_motion_steps);
222 for (
size_t step = 0;
step < num_motion_steps; ++
step) {
223 MTLMotionKeyframeData *k = [MTLMotionKeyframeData
data];
226 vertex_ptrs.push_back(k);
229 MTLAccelerationStructureMotionTriangleGeometryDescriptor *geomDescMotion =
230 [MTLAccelerationStructureMotionTriangleGeometryDescriptor
descriptor];
231 geomDescMotion.vertexBuffers = [NSArray arrayWithObjects:vertex_ptrs.data()
232 count:vertex_ptrs.size()];
233 geomDescMotion.vertexStride =
sizeof(verts[0]);
234 geomDescMotion.indexBuffer = indexBuf;
235 geomDescMotion.indexBufferOffset = 0;
236 geomDescMotion.indexType = MTLIndexTypeUInt32;
237 geomDescMotion.triangleCount = num_indices / 3;
238 geomDescMotion.intersectionFunctionTableOffset = 0;
239 geomDescMotion.opaque =
true;
241 geomDesc = geomDescMotion;
243 BVH_status(
"Building motion mesh BLAS | %7d tris | %s | %7d motion keyframes",
246 (
int)num_motion_steps);
249 MTLAccelerationStructureTriangleGeometryDescriptor *geomDescNoMotion =
250 [MTLAccelerationStructureTriangleGeometryDescriptor
descriptor];
251 geomDescNoMotion.vertexBuffer = posBuf;
252 geomDescNoMotion.vertexBufferOffset = 0;
253 geomDescNoMotion.vertexStride =
sizeof(verts[0]);
254 geomDescNoMotion.indexBuffer = indexBuf;
255 geomDescNoMotion.indexBufferOffset = 0;
256 geomDescNoMotion.indexType = MTLIndexTypeUInt32;
257 geomDescNoMotion.triangleCount = num_indices / 3;
258 geomDescNoMotion.intersectionFunctionTableOffset = 0;
259 geomDescNoMotion.opaque =
true;
261 geomDesc = geomDescNoMotion;
264 "Building mesh BLAS | %7d tris | %s", (
int)mesh->
num_triangles(), geom->
name.c_str());
270 geomDesc.allowDuplicateIntersectionFunctionInvocation =
false;
272 MTLPrimitiveAccelerationStructureDescriptor *accelDesc =
273 [MTLPrimitiveAccelerationStructureDescriptor
descriptor];
274 accelDesc.geometryDescriptors = @[ geomDesc ];
275 if (num_motion_steps > 1) {
276 accelDesc.motionStartTime = 0.0f;
277 accelDesc.motionEndTime = 1.0f;
278 accelDesc.motionStartBorderMode = MTLMotionBorderModeClamp;
279 accelDesc.motionEndBorderMode = MTLMotionBorderModeClamp;
280 accelDesc.motionKeyframeCount = num_motion_steps;
282 if (extended_limits) {
283 accelDesc.usage |= MTLAccelerationStructureUsageExtendedLimits;
286 if (!use_fast_trace_bvh) {
287 accelDesc.usage |= (MTLAccelerationStructureUsageRefit |
288 MTLAccelerationStructureUsagePreferFastBuild);
290 else if (@available(macos 26.0, *)) {
291 accelDesc.usage |= MTLAccelerationStructureUsagePreferFastIntersection;
294 MTLAccelerationStructureSizes accelSizes = [mtl_device
295 accelerationStructureSizesWithDescriptor:accelDesc];
296 id<MTLAccelerationStructure> accel_uncompressed = [mtl_device
297 newAccelerationStructureWithSize:accelSizes.accelerationStructureSize];
298 id<MTLBuffer> scratchBuf = [mtl_device newBufferWithLength:accelSizes.buildScratchBufferSize
299 options:MTLResourceStorageModePrivate];
300 id<MTLBuffer> sizeBuf = [mtl_device newBufferWithLength:8
options:MTLResourceStorageModeShared];
301 id<MTLCommandBuffer> accelCommands = [queue commandBuffer];
302 id<MTLAccelerationStructureCommandEncoder> accelEnc =
303 [accelCommands accelerationStructureCommandEncoder];
305 [accelEnc refitAccelerationStructure:accel_struct
307 destination:accel_uncompressed
308 scratchBuffer:scratchBuf
309 scratchBufferOffset:0];
312 [accelEnc buildAccelerationStructure:accel_uncompressed
314 scratchBuffer:scratchBuf
315 scratchBufferOffset:0];
317 if (use_fast_trace_bvh) {
318 [accelEnc writeCompactedAccelerationStructureSize:accel_uncompressed
321 sizeDataType:MTLDataTypeULong];
323 [accelEnc endEncoding];
327 size_t wired_size = posBuf.allocatedSize + indexBuf.allocatedSize + scratchBuf.allocatedSize +
328 accel_uncompressed.allocatedSize * 2;
330 [accelCommands addCompletedHandler:^(id<MTLCommandBuffer> ) {
332 [scratchBuf release];
336 if (use_fast_trace_bvh) {
340 dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
341 id<MTLCommandBuffer> accelCommands = [queue commandBuffer];
342 id<MTLAccelerationStructureCommandEncoder> accelEnc =
343 [accelCommands accelerationStructureCommandEncoder];
344 id<MTLAccelerationStructure> accel = [mtl_device
345 newAccelerationStructureWithSize:compressed_size];
346 [accelEnc copyAndCompactAccelerationStructure:accel_uncompressed
347 toAccelerationStructure:accel];
348 [accelEnc endEncoding];
349 [accelCommands addCompletedHandler:^(id<MTLCommandBuffer> ) {
350 set_accel_struct(accel);
351 [accel_uncompressed release];
354 g_bvh_build_throttler.release(wired_size);
356 [accelCommands commit];
361 set_accel_struct(accel_uncompressed);
364 g_bvh_build_throttler.release(wired_size);
371 g_bvh_build_throttler.acquire(wired_size);
372 [accelCommands commit];
377bool BVHMetal::build_BLAS_hair(
Progress &progress,
378 id<MTLDevice> mtl_device,
379 id<MTLCommandQueue> queue,
383# if defined(MAC_OS_VERSION_14_0)
384 if (@available(macos 14.0, *)) {
386 Hair *hair =
static_cast<Hair *
>(geom);
391 const bool use_fast_trace_bvh = (params.bvh_type ==
BVH_TYPE_STATIC) || !support_refit_blas();
393 size_t num_motion_steps = 1;
396 if (motion_blur && hair->get_use_motion_blur() && attr_P->
has_motion()) {
397 num_motion_steps = hair->get_motion_steps();
400 id<MTLBuffer> cpBuffer = nil;
401 id<MTLBuffer> radiusBuffer = nil;
402 id<MTLBuffer> idxBuffer = nil;
404 MTLAccelerationStructureGeometryDescriptor *geomDesc;
405 if (num_motion_steps > 1) {
406 MTLAccelerationStructureMotionCurveGeometryDescriptor *geomDescCrv =
407 [MTLAccelerationStructureMotionCurveGeometryDescriptor
descriptor];
413 std::vector<packed_float3> cpData;
414 std::vector<int> idxData;
415 std::vector<float> radiusData;
416 cpData.reserve(numKeys);
417 radiusData.reserve(numKeys);
419 std::vector<int> step_offsets;
420 for (
size_t step = 0;
step < num_motion_steps; ++
step) {
425 step_offsets.push_back(cpData.size());
427 for (
int c = 0; c < numCurves; ++c) {
433 cpData.push_back(keys[firstKey]);
434 radiusData.push_back(radii[firstKey]);
436 for (
int s = 0; s < segCount; ++s) {
438 idxData.push_back(idxBase + s);
440 cpData.push_back(keys[firstKey + s]);
441 radiusData.push_back(radii[firstKey + s]);
443 cpData.push_back(keys[firstKey + curve.
num_keys - 1]);
444 radiusData.push_back(radii[firstKey + curve.
num_keys - 1]);
446 cpData.push_back(keys[firstKey + curve.
num_keys - 1]);
447 radiusData.push_back(radii[firstKey + curve.
num_keys - 1]);
453 idxBuffer = [mtl_device newBufferWithBytes:idxData.data()
454 length:idxData.size() *
sizeof(int)
455 options:MTLResourceStorageModeShared];
457 cpBuffer = [mtl_device newBufferWithBytes:cpData.data()
459 options:MTLResourceStorageModeShared];
461 radiusBuffer = [mtl_device newBufferWithBytes:radiusData.data()
463 options:MTLResourceStorageModeShared];
465 std::vector<MTLMotionKeyframeData *> cp_ptrs;
466 std::vector<MTLMotionKeyframeData *> radius_ptrs;
467 cp_ptrs.reserve(num_motion_steps);
468 radius_ptrs.reserve(num_motion_steps);
470 for (
size_t step = 0;
step < num_motion_steps; ++
step) {
471 MTLMotionKeyframeData *k = [MTLMotionKeyframeData
data];
474 cp_ptrs.push_back(k);
476 k = [MTLMotionKeyframeData
data];
477 k.buffer = radiusBuffer;
478 k.offset = step_offsets[
step] *
sizeof(
float);
479 radius_ptrs.push_back(k);
482 geomDescCrv.controlPointBuffers = [NSArray arrayWithObjects:cp_ptrs.data()
483 count:cp_ptrs.size()];
484 geomDescCrv.radiusBuffers = [NSArray arrayWithObjects:radius_ptrs.data()
485 count:radius_ptrs.size()];
488 geomDescCrv.controlPointCount = cpData.size() / num_motion_steps;
490 geomDescCrv.controlPointFormat = MTLAttributeFormatFloat3;
491 geomDescCrv.radiusStride =
sizeof(
float);
492 geomDescCrv.radiusFormat = MTLAttributeFormatFloat;
493 geomDescCrv.segmentCount = idxData.size();
498 geomDescCrv.curveBasis = MTLCurveBasisLinear;
499 geomDescCrv.curveEndCaps = MTLCurveEndCapsSphere;
502 geomDescCrv.curveBasis = MTLCurveBasisCatmullRom;
503 geomDescCrv.curveEndCaps = MTLCurveEndCapsDisk;
505 geomDescCrv.indexType = MTLIndexTypeUInt32;
506 geomDescCrv.indexBuffer = idxBuffer;
507 geomDescCrv.intersectionFunctionTableOffset = 1;
512 geomDescCrv.allowDuplicateIntersectionFunctionInvocation =
false;
513 geomDescCrv.opaque =
true;
514 geomDesc = geomDescCrv;
517 MTLAccelerationStructureCurveGeometryDescriptor *geomDescCrv =
518 [MTLAccelerationStructureCurveGeometryDescriptor
descriptor];
525 std::vector<packed_float3> cpData;
526 std::vector<int> idxData;
527 std::vector<float> radiusData;
528 cpData.reserve(numKeys);
529 radiusData.reserve(numKeys);
531 for (
int c = 0; c < numCurves; ++c) {
537 cpData.push_back(keys[firstKey]);
538 radiusData.push_back(radiuses[firstKey]);
540 for (
int s = 0; s < segCount; ++s) {
541 idxData.push_back(idxBase + s);
542 cpData.push_back(keys[firstKey + s]);
543 radiusData.push_back(radiuses[firstKey + s]);
545 cpData.push_back(keys[firstKey + curve.
num_keys - 1]);
546 radiusData.push_back(radiuses[firstKey + curve.
num_keys - 1]);
548 cpData.push_back(keys[firstKey + curve.
num_keys - 1]);
549 radiusData.push_back(radiuses[firstKey + curve.
num_keys - 1]);
554 idxBuffer = [mtl_device newBufferWithBytes:idxData.data()
555 length:idxData.size() *
sizeof(int)
556 options:MTLResourceStorageModeShared];
558 cpBuffer = [mtl_device newBufferWithBytes:cpData.data()
560 options:MTLResourceStorageModeShared];
562 radiusBuffer = [mtl_device newBufferWithBytes:radiusData.data()
564 options:MTLResourceStorageModeShared];
566 geomDescCrv.controlPointBuffer = cpBuffer;
567 geomDescCrv.radiusBuffer = radiusBuffer;
568 geomDescCrv.controlPointCount = cpData.size();
570 geomDescCrv.controlPointFormat = MTLAttributeFormatFloat3;
571 geomDescCrv.controlPointBufferOffset = 0;
572 geomDescCrv.segmentCount = idxData.size();
577 geomDescCrv.curveBasis = MTLCurveBasisLinear;
578 geomDescCrv.curveEndCaps = MTLCurveEndCapsSphere;
581 geomDescCrv.curveBasis = MTLCurveBasisCatmullRom;
582 geomDescCrv.curveEndCaps = MTLCurveEndCapsDisk;
584 geomDescCrv.indexType = MTLIndexTypeUInt32;
585 geomDescCrv.indexBuffer = idxBuffer;
586 geomDescCrv.intersectionFunctionTableOffset = 1;
591 geomDescCrv.allowDuplicateIntersectionFunctionInvocation =
false;
592 geomDescCrv.opaque =
true;
593 geomDesc = geomDescCrv;
596 MTLPrimitiveAccelerationStructureDescriptor *accelDesc =
597 [MTLPrimitiveAccelerationStructureDescriptor
descriptor];
598 accelDesc.geometryDescriptors = @[ geomDesc ];
600 if (num_motion_steps > 1) {
601 accelDesc.motionStartTime = 0.0f;
602 accelDesc.motionEndTime = 1.0f;
603 accelDesc.motionStartBorderMode = MTLMotionBorderModeVanish;
604 accelDesc.motionEndBorderMode = MTLMotionBorderModeVanish;
605 accelDesc.motionKeyframeCount = num_motion_steps;
607 BVH_status(
"Building motion hair BLAS | %7d curves | %s | %7d motion keyframes",
610 (
int)num_motion_steps);
614 "Building hair BLAS | %7d curves | %s", (
int)hair->
num_curves(), geom->
name.c_str());
617 if (extended_limits) {
618 accelDesc.usage |= MTLAccelerationStructureUsageExtendedLimits;
621 if (!use_fast_trace_bvh) {
622 accelDesc.usage |= (MTLAccelerationStructureUsageRefit |
623 MTLAccelerationStructureUsagePreferFastBuild);
625 else if (@available(macos 26.0, *)) {
626 accelDesc.usage |= MTLAccelerationStructureUsagePreferFastIntersection;
629 MTLAccelerationStructureSizes accelSizes = [mtl_device
630 accelerationStructureSizesWithDescriptor:accelDesc];
631 id<MTLAccelerationStructure> accel_uncompressed = [mtl_device
632 newAccelerationStructureWithSize:accelSizes.accelerationStructureSize];
633 id<MTLBuffer> scratchBuf = [mtl_device newBufferWithLength:accelSizes.buildScratchBufferSize
634 options:MTLResourceStorageModePrivate];
635 id<MTLBuffer> sizeBuf = [mtl_device newBufferWithLength:8
636 options:MTLResourceStorageModeShared];
637 id<MTLCommandBuffer> accelCommands = [queue commandBuffer];
638 id<MTLAccelerationStructureCommandEncoder> accelEnc =
639 [accelCommands accelerationStructureCommandEncoder];
641 [accelEnc refitAccelerationStructure:accel_struct
643 destination:accel_uncompressed
644 scratchBuffer:scratchBuf
645 scratchBufferOffset:0];
648 [accelEnc buildAccelerationStructure:accel_uncompressed
650 scratchBuffer:scratchBuf
651 scratchBufferOffset:0];
653 if (use_fast_trace_bvh) {
654 [accelEnc writeCompactedAccelerationStructureSize:accel_uncompressed
657 sizeDataType:MTLDataTypeULong];
659 [accelEnc endEncoding];
663 size_t wired_size = cpBuffer.allocatedSize + radiusBuffer.allocatedSize +
664 idxBuffer.allocatedSize + scratchBuf.allocatedSize +
665 accel_uncompressed.allocatedSize * 2;
667 [accelCommands addCompletedHandler:^(id<MTLCommandBuffer> ) {
669 [scratchBuf release];
671 [radiusBuffer release];
674 if (use_fast_trace_bvh) {
677 dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
678 id<MTLCommandBuffer> accelCommands = [queue commandBuffer];
679 id<MTLAccelerationStructureCommandEncoder> accelEnc =
680 [accelCommands accelerationStructureCommandEncoder];
681 id<MTLAccelerationStructure> accel = [mtl_device
682 newAccelerationStructureWithSize:compressed_size];
683 [accelEnc copyAndCompactAccelerationStructure:accel_uncompressed
684 toAccelerationStructure:accel];
685 [accelEnc endEncoding];
686 [accelCommands addCompletedHandler:^(id<MTLCommandBuffer> ) {
687 set_accel_struct(accel);
688 [accel_uncompressed release];
691 g_bvh_build_throttler.release(wired_size);
693 [accelCommands commit];
698 set_accel_struct(accel_uncompressed);
701 g_bvh_build_throttler.release(wired_size);
708 g_bvh_build_throttler.acquire(wired_size);
709 [accelCommands commit];
723bool BVHMetal::build_BLAS_pointcloud(
Progress &progress,
724 id<MTLDevice> mtl_device,
725 id<MTLCommandQueue> queue,
735 const size_t num_points = pointcloud->
num_points();
737 const bool use_fast_trace_bvh = (params.bvh_type ==
BVH_TYPE_STATIC) || !support_refit_blas();
739 size_t num_motion_steps = 1;
742 if (motion_blur && pointcloud->get_use_motion_blur() && attr_P->
has_motion()) {
743 num_motion_steps = pointcloud->get_motion_steps();
746 const size_t num_aabbs = num_motion_steps * num_points;
749 id<MTLBuffer> aabbBuf = [mtl_device
750 newBufferWithLength:num_aabbs *
sizeof(MTLAxisAlignedBoundingBox)
751 options:MTLResourceStorageModeShared];
752 MTLAxisAlignedBoundingBox *aabb_data = (MTLAxisAlignedBoundingBox *)[aabbBuf contents];
755 for (
size_t step = 0;
step < num_motion_steps; ++
step) {
760 for (
size_t j = 0; j < num_points; ++j) {
765 const size_t index =
step * num_points + j;
771 MTLAccelerationStructureGeometryDescriptor *geomDesc;
772 if (num_motion_steps > 1) {
773 std::vector<MTLMotionKeyframeData *> aabb_ptrs;
774 aabb_ptrs.reserve(num_motion_steps);
775 for (
size_t step = 0;
step < num_motion_steps; ++
step) {
776 MTLMotionKeyframeData *k = [MTLMotionKeyframeData
data];
778 k.offset =
step * num_points *
sizeof(MTLAxisAlignedBoundingBox);
779 aabb_ptrs.push_back(k);
782 MTLAccelerationStructureMotionBoundingBoxGeometryDescriptor *geomDescMotion =
783 [MTLAccelerationStructureMotionBoundingBoxGeometryDescriptor
descriptor];
784 geomDescMotion.boundingBoxBuffers = [NSArray arrayWithObjects:aabb_ptrs.data()
785 count:aabb_ptrs.size()];
786 geomDescMotion.boundingBoxCount = num_points;
787 geomDescMotion.boundingBoxStride =
sizeof(aabb_data[0]);
788 geomDescMotion.intersectionFunctionTableOffset = 2;
793 geomDescMotion.allowDuplicateIntersectionFunctionInvocation =
false;
794 geomDescMotion.opaque =
true;
795 geomDesc = geomDescMotion;
798 MTLAccelerationStructureBoundingBoxGeometryDescriptor *geomDescNoMotion =
799 [MTLAccelerationStructureBoundingBoxGeometryDescriptor
descriptor];
800 geomDescNoMotion.boundingBoxBuffer = aabbBuf;
801 geomDescNoMotion.boundingBoxBufferOffset = 0;
802 geomDescNoMotion.boundingBoxCount = int(num_aabbs);
803 geomDescNoMotion.boundingBoxStride =
sizeof(aabb_data[0]);
804 geomDescNoMotion.intersectionFunctionTableOffset = 2;
809 geomDescNoMotion.allowDuplicateIntersectionFunctionInvocation =
false;
810 geomDescNoMotion.opaque =
true;
811 geomDesc = geomDescNoMotion;
814 MTLPrimitiveAccelerationStructureDescriptor *accelDesc =
815 [MTLPrimitiveAccelerationStructureDescriptor
descriptor];
816 accelDesc.geometryDescriptors = @[ geomDesc ];
818 if (num_motion_steps > 1) {
819 accelDesc.motionStartTime = 0.0f;
820 accelDesc.motionEndTime = 1.0f;
823 accelDesc.motionKeyframeCount = num_motion_steps;
825 BVH_status(
"Building motion pointcloud BLAS | %7d points | %s | %7d motion keyframes",
828 (
int)num_motion_steps);
831 BVH_status(
"Building pointcloud BLAS | %7d points | %s",
835 if (extended_limits) {
836 accelDesc.usage |= MTLAccelerationStructureUsageExtendedLimits;
839 if (!use_fast_trace_bvh) {
840 accelDesc.usage |= (MTLAccelerationStructureUsageRefit |
841 MTLAccelerationStructureUsagePreferFastBuild);
843 else if (@available(macos 26.0, *)) {
844 accelDesc.usage |= MTLAccelerationStructureUsagePreferFastIntersection;
847 MTLAccelerationStructureSizes accelSizes = [mtl_device
848 accelerationStructureSizesWithDescriptor:accelDesc];
849 id<MTLAccelerationStructure> accel_uncompressed = [mtl_device
850 newAccelerationStructureWithSize:accelSizes.accelerationStructureSize];
851 id<MTLBuffer> scratchBuf = [mtl_device newBufferWithLength:accelSizes.buildScratchBufferSize
852 options:MTLResourceStorageModePrivate];
853 id<MTLBuffer> sizeBuf = [mtl_device newBufferWithLength:8
options:MTLResourceStorageModeShared];
854 id<MTLCommandBuffer> accelCommands = [queue commandBuffer];
855 id<MTLAccelerationStructureCommandEncoder> accelEnc =
856 [accelCommands accelerationStructureCommandEncoder];
858 [accelEnc refitAccelerationStructure:accel_struct
860 destination:accel_uncompressed
861 scratchBuffer:scratchBuf
862 scratchBufferOffset:0];
865 [accelEnc buildAccelerationStructure:accel_uncompressed
867 scratchBuffer:scratchBuf
868 scratchBufferOffset:0];
870 if (use_fast_trace_bvh) {
871 [accelEnc writeCompactedAccelerationStructureSize:accel_uncompressed
874 sizeDataType:MTLDataTypeULong];
876 [accelEnc endEncoding];
880 size_t wired_size = aabbBuf.allocatedSize + scratchBuf.allocatedSize +
881 accel_uncompressed.allocatedSize * 2;
883 [accelCommands addCompletedHandler:^(id<MTLCommandBuffer> ) {
885 [scratchBuf release];
888 if (use_fast_trace_bvh) {
892 dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
893 id<MTLCommandBuffer> accelCommands = [queue commandBuffer];
894 id<MTLAccelerationStructureCommandEncoder> accelEnc =
895 [accelCommands accelerationStructureCommandEncoder];
896 id<MTLAccelerationStructure> accel = [mtl_device
897 newAccelerationStructureWithSize:compressed_size];
898 [accelEnc copyAndCompactAccelerationStructure:accel_uncompressed
899 toAccelerationStructure:accel];
900 [accelEnc endEncoding];
901 [accelCommands addCompletedHandler:^(id<MTLCommandBuffer> ) {
902 set_accel_struct(accel);
903 [accel_uncompressed release];
906 g_bvh_build_throttler.release(wired_size);
908 [accelCommands commit];
913 set_accel_struct(accel_uncompressed);
916 g_bvh_build_throttler.release(wired_size);
923 g_bvh_build_throttler.acquire(wired_size);
924 [accelCommands commit];
928bool BVHMetal::build_BLAS(
Progress &progress,
929 id<MTLDevice> mtl_device,
930 id<MTLCommandQueue> queue,
933 assert(objects.size() == 1 && geometry.size() == 1);
940 return build_BLAS_mesh(progress, mtl_device, queue, geom,
refit);
942 return build_BLAS_hair(progress, mtl_device, queue, geom,
refit);
944 return build_BLAS_pointcloud(progress, mtl_device, queue, geom,
refit);
951# if defined(MAC_OS_VERSION_15_0)
954static MTLComponentTransform decomposed_to_component_transform(
const DecomposedTransform &src)
956 MTLComponentTransform tfm;
957 tfm.scale = MTLPackedFloat3Make(src.
y.
w, src.
z.
w, src.
w.
w);
958 tfm.shear = MTLPackedFloat3Make(src.
z.
x, src.
z.
y, src.
w.
x);
959 tfm.pivot = MTLPackedFloat3Make(0.0f, 0.0f, 0.0f);
960 tfm.rotation = MTLPackedFloatQuaternionMake(src.
x.
x, src.
x.
y, src.
x.
z, src.
x.
w);
961 tfm.translation = MTLPackedFloat3Make(src.
y.
x, src.
y.
y, src.
y.
z);
966static MTLComponentTransform component_transform_make_unit()
968 MTLComponentTransform tfm;
969 tfm.scale = MTLPackedFloat3Make(1.0f, 1.0f, 1.0f);
970 tfm.shear = MTLPackedFloat3Make(0.0f, 0.0f, 0.0f);
971 tfm.pivot = MTLPackedFloat3Make(0.0f, 0.0f, 0.0f);
972 tfm.rotation = MTLPackedFloatQuaternionMake(0.0f, 0.0f, 0.0f, 1.0f);
973 tfm.translation = MTLPackedFloat3Make(0.0f, 0.0f, 0.0f);
979bool BVHMetal::build_TLAS(
Progress &progress,
980 id<MTLDevice> mtl_device,
981 id<MTLCommandQueue> queue,
985 g_bvh_build_throttler.wait_for_all();
987 auto make_null_BLAS = [
this](id<MTLDevice> mtl_device,
988 id<MTLCommandQueue> queue) -> id<MTLAccelerationStructure> {
989 id<MTLBuffer> nullBuf = [mtl_device newBufferWithLength:
sizeof(
packed_float3)
990 options:MTLResourceStorageModeShared];
993 MTLAccelerationStructureTriangleGeometryDescriptor *geomDesc =
994 [MTLAccelerationStructureTriangleGeometryDescriptor
descriptor];
995 geomDesc.vertexBuffer = nullBuf;
996 geomDesc.vertexBufferOffset = 0;
998 geomDesc.indexBuffer = nullBuf;
999 geomDesc.indexBufferOffset = 0;
1000 geomDesc.indexType = MTLIndexTypeUInt32;
1001 geomDesc.triangleCount = 0;
1002 geomDesc.intersectionFunctionTableOffset = 0;
1003 geomDesc.opaque =
true;
1004 geomDesc.allowDuplicateIntersectionFunctionInvocation =
false;
1006 MTLPrimitiveAccelerationStructureDescriptor *accelDesc =
1007 [MTLPrimitiveAccelerationStructureDescriptor
descriptor];
1008 accelDesc.geometryDescriptors = @[ geomDesc ];
1009 if (extended_limits) {
1010 accelDesc.usage |= MTLAccelerationStructureUsageExtendedLimits;
1013 MTLAccelerationStructureSizes accelSizes = [mtl_device
1014 accelerationStructureSizesWithDescriptor:accelDesc];
1015 id<MTLAccelerationStructure> accel_struct = [mtl_device
1016 newAccelerationStructureWithSize:accelSizes.accelerationStructureSize];
1017 id<MTLBuffer> scratchBuf = [mtl_device newBufferWithLength:accelSizes.buildScratchBufferSize
1018 options:MTLResourceStorageModePrivate];
1019 id<MTLBuffer> sizeBuf = [mtl_device newBufferWithLength:8
1020 options:MTLResourceStorageModeShared];
1021 id<MTLCommandBuffer> accelCommands = [queue commandBuffer];
1022 id<MTLAccelerationStructureCommandEncoder> accelEnc =
1023 [accelCommands accelerationStructureCommandEncoder];
1024 [accelEnc buildAccelerationStructure:accel_struct
1026 scratchBuffer:scratchBuf
1027 scratchBufferOffset:0];
1028 [accelEnc endEncoding];
1029 [accelCommands commit];
1030 [accelCommands waitUntilCompleted];
1033 [scratchBuf release];
1037 return accel_struct;
1041 uint32_t num_motion_transforms = 0;
1043 for (
Object *ob : objects) {
1046 if (ob->use_motion()) {
1047 num_motion_transforms +=
max((
size_t)1, ob->get_motion().size());
1048 num_motion_instances++;
1051 num_motion_transforms++;
1055 const bool use_instance_motion = motion_blur && num_motion_instances;
1056 const bool use_fast_trace_bvh = (params.bvh_type ==
BVH_TYPE_STATIC) || !support_refit_blas();
1058 NSMutableArray *all_blas = [NSMutableArray
array];
1059 unordered_map<const BVHMetal *, int> instance_mapping;
1062 auto get_blas_index = [&](
const BVHMetal *blas) {
1063 auto it = instance_mapping.find(blas);
1064 if (it != instance_mapping.end()) {
1067 int blas_index = (int)[all_blas count];
1068 instance_mapping[blas] = blas_index;
1069 [all_blas addObject:(blas ? blas->accel_struct : null_BLAS)];
1073 size_t instance_size;
1074 if (use_instance_motion) {
1075 instance_size =
sizeof(MTLAccelerationStructureMotionInstanceDescriptor);
1078 instance_size =
sizeof(MTLAccelerationStructureUserIDInstanceDescriptor);
1082 id<MTLBuffer> instanceBuf = [mtl_device newBufferWithLength:num_instances * instance_size
1083 options:MTLResourceStorageModeShared];
1084 id<MTLBuffer> motion_transforms_buf = nil;
1085 MTLPackedFloat4x3 *matrix_motion_transforms =
nullptr;
1086# if defined(MAC_OS_VERSION_15_0)
1087 MTLComponentTransform *decomposed_motion_transforms =
nullptr;
1089 if (use_instance_motion && num_motion_transforms) {
1090# if defined(MAC_OS_VERSION_15_0)
1092 if (@available(macos 15.0, *)) {
1093 motion_transforms_buf = [mtl_device
1094 newBufferWithLength:num_motion_transforms *
sizeof(MTLComponentTransform)
1095 options:MTLResourceStorageModeShared];
1096 decomposed_motion_transforms = (MTLComponentTransform *)motion_transforms_buf.contents;
1102 motion_transforms_buf = [mtl_device
1103 newBufferWithLength:num_motion_transforms *
sizeof(MTLPackedFloat4x3)
1104 options:MTLResourceStorageModeShared];
1105 matrix_motion_transforms = (MTLPackedFloat4x3 *)motion_transforms_buf.contents;
1110 uint32_t motion_transform_index = 0;
1113 blas_array.reserve(num_instances);
1115 for (
Object *ob : objects) {
1117 const Geometry *geom = ob->get_geometry();
1118 const BVHMetal *blas =
static_cast<const BVHMetal *
>(geom->
bvh.get());
1119 if (!blas || !blas->accel_struct || !ob->is_traceable()) {
1127 null_BLAS = make_null_BLAS(mtl_device, queue);
1129 blas_array.push_back(null_BLAS);
1132 blas_array.push_back(blas->accel_struct);
1135 uint32_t accel_struct_index = get_blas_index(blas);
1166 if (use_instance_motion) {
1167 MTLAccelerationStructureMotionInstanceDescriptor *instances =
1168 (MTLAccelerationStructureMotionInstanceDescriptor *)[instanceBuf contents];
1169 MTLAccelerationStructureMotionInstanceDescriptor &desc = instances[currIndex];
1171 desc.accelerationStructureIndex = accel_struct_index;
1172 desc.userID = primitive_offset;
1174 desc.motionStartTime = 0.0f;
1175 desc.motionEndTime = 1.0f;
1176 desc.motionTransformsStartIndex = motion_transform_index;
1177 desc.motionStartBorderMode = MTLMotionBorderModeVanish;
1178 desc.motionEndBorderMode = MTLMotionBorderModeVanish;
1179 desc.intersectionFunctionTableOffset = 0;
1184 int key_count = ob->get_motion().size();
1186 desc.motionTransformsCount = key_count;
1188# if defined(MAC_OS_VERSION_15_0)
1190 for (
int i = 0; i < key_count; i++) {
1191 decomposed_motion_transforms[motion_transform_index++] =
1192 decomposed_to_component_transform(decomp[i]);
1198 Transform *keys = ob->get_motion().data();
1199 for (
int i = 0; i < key_count; i++) {
1200 float *t = (
float *)&matrix_motion_transforms[motion_transform_index++];
1202 const auto *src = (
const float *)&keys[i];
1203 for (
int i = 0; i < 12; i++) {
1204 t[i] = src[(i / 3) + 4 * (i % 3)];
1210 desc.motionTransformsCount = 1;
1212# if defined(MAC_OS_VERSION_15_0)
1214 if (ob->get_geometry()->is_instanced()) {
1217 decomposed_motion_transforms[motion_transform_index++] =
1218 decomposed_to_component_transform(decomp);
1221 decomposed_motion_transforms[motion_transform_index++] =
1222 component_transform_make_unit();
1228 float *t = (
float *)&matrix_motion_transforms[motion_transform_index++];
1229 if (ob->get_geometry()->is_instanced()) {
1231 const auto *src = (
const float *)&ob->get_tfm();
1232 for (
int i = 0; i < 12; i++) {
1233 t[i] = src[(i / 3) + 4 * (i % 3)];
1238 t[0] = t[4] = t[8] = 1.0f;
1244 MTLAccelerationStructureUserIDInstanceDescriptor *instances =
1245 (MTLAccelerationStructureUserIDInstanceDescriptor *)[instanceBuf contents];
1246 MTLAccelerationStructureUserIDInstanceDescriptor &desc = instances[currIndex];
1248 desc.accelerationStructureIndex = accel_struct_index;
1249 desc.userID = primitive_offset;
1251 desc.intersectionFunctionTableOffset = 0;
1252 desc.options = MTLAccelerationStructureInstanceOptionOpaque;
1254 float *t = (
float *)&desc.transformationMatrix;
1255 if (ob->get_geometry()->is_instanced()) {
1257 const auto *src = (
const float *)&ob->get_tfm();
1258 for (
int i = 0; i < 12; i++) {
1259 t[i] = src[(i / 3) + 4 * (i % 3)];
1264 t[0] = t[4] = t[8] = 1.0f;
1269 if (use_instance_motion) {
1271 "Building motion TLAS | %7d instances | %7d motion instances | %7d motion "
1274 (
int)num_motion_instances,
1275 (
int)num_motion_transforms);
1278 BVH_status(
"Building TLAS | %7d instances", (
int)num_instances);
1281 MTLInstanceAccelerationStructureDescriptor *accelDesc =
1282 [MTLInstanceAccelerationStructureDescriptor
descriptor];
1283 accelDesc.instanceCount = num_instances;
1284 accelDesc.instanceDescriptorType = MTLAccelerationStructureInstanceDescriptorTypeUserID;
1285 accelDesc.instanceDescriptorBuffer = instanceBuf;
1286 accelDesc.instanceDescriptorBufferOffset = 0;
1287 accelDesc.instanceDescriptorStride = instance_size;
1288 accelDesc.instancedAccelerationStructures = all_blas;
1290 if (use_instance_motion) {
1291 accelDesc.instanceDescriptorType = MTLAccelerationStructureInstanceDescriptorTypeMotion;
1292 accelDesc.motionTransformBuffer = motion_transforms_buf;
1293 accelDesc.motionTransformCount = num_motion_transforms;
1294# if defined(MAC_OS_VERSION_15_0)
1295 if (@available(macos 15.0, *)) {
1296 accelDesc.motionTransformStride = 0;
1297 accelDesc.motionTransformType = use_pcmi ? MTLTransformTypeComponent :
1298 MTLTransformTypePackedFloat4x3;
1303 if (extended_limits) {
1304 accelDesc.usage |= MTLAccelerationStructureUsageExtendedLimits;
1306 if (!use_fast_trace_bvh) {
1307 accelDesc.usage |= (MTLAccelerationStructureUsageRefit |
1308 MTLAccelerationStructureUsagePreferFastBuild);
1310 else if (@available(macos 26.0, *)) {
1311 accelDesc.usage |= MTLAccelerationStructureUsagePreferFastIntersection;
1314 MTLAccelerationStructureSizes accelSizes = [mtl_device
1315 accelerationStructureSizesWithDescriptor:accelDesc];
1316 id<MTLAccelerationStructure> accel = [mtl_device
1317 newAccelerationStructureWithSize:accelSizes.accelerationStructureSize];
1318 id<MTLBuffer> scratchBuf = [mtl_device newBufferWithLength:accelSizes.buildScratchBufferSize
1319 options:MTLResourceStorageModePrivate];
1320 id<MTLCommandBuffer> accelCommands = [queue commandBuffer];
1321 id<MTLAccelerationStructureCommandEncoder> accelEnc =
1322 [accelCommands accelerationStructureCommandEncoder];
1324 [accelEnc refitAccelerationStructure:accel_struct
1327 scratchBuffer:scratchBuf
1328 scratchBufferOffset:0];
1331 [accelEnc buildAccelerationStructure:accel
1333 scratchBuffer:scratchBuf
1334 scratchBufferOffset:0];
1336 [accelEnc endEncoding];
1337 [accelCommands commit];
1338 [accelCommands waitUntilCompleted];
1340 if (motion_transforms_buf) {
1341 [motion_transforms_buf release];
1343 [instanceBuf release];
1344 [scratchBuf release];
1347 set_accel_struct(accel);
1349 unique_blas_array.clear();
1350 unique_blas_array.reserve(all_blas.count);
1351 [all_blas enumerateObjectsUsingBlock:^(id<MTLAccelerationStructure> blas, NSUInteger, BOOL *) {
1352 unique_blas_array.push_back(blas);
1358bool BVHMetal::build(
Progress &progress,
1359 id<MTLDevice> mtl_device,
1360 id<MTLCommandQueue> queue,
1367 assert(!
"Can't refit static Metal BVH");
1370 else if (!accel_struct) {
1378 set_accel_struct(nil);
1381 if (!support_refit_blas()) {
1386 if (!params.top_level) {
1387 return build_BLAS(progress, mtl_device, queue,
refit);
1389 return build_TLAS(progress, mtl_device, queue,
refit);
unsigned long long int uint64_t
static btDbvtVolume bounds(btDbvtNode **leaves, int count)
void refit(btStridingMeshInterface *triangles, const btVector3 &aabbMin, const btVector3 &aabbMax)
Attribute * find(ustring name) const
const T * data_at_time_step(const int step, const int num_time_steps) const
bool is_pointcloud() const
const packed_float3 * get_position() const
const float * get_radius() const
Curve get_curve(const size_t i) const
size_t curve_segment_offset
size_t num_curves() const
CurveShapeType curve_shape
NODE_DECLARE size_t num_verts() const
size_t num_triangles() const
Point get_point(const int i) const
size_t num_points() const
CCL_NAMESPACE_BEGIN struct Options options
#define CCL_NAMESPACE_END
#define assert(assertion)
VecBase< float, D > step(VecOp< float, D >, VecOp< float, D >) RET
float length(VecOp< float, D >) RET
ccl_device_inline float2 mask(const MaskType mask, const float2 a)
BMesh const char void * data
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
void bounds_grow(const packed_float3 *points, const float *radius, BoundBox &bounds) const
std::unique_lock< std::mutex > thread_scoped_lock