Blender V5.3
bvh.mm
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2021-2022 Blender Foundation
2 *
3 * SPDX-License-Identifier: Apache-2.0 */
4
5#ifdef WITH_METAL
6
7# include <algorithm>
8# include <chrono>
9# include <thread>
10# include <vector>
11
12# include "scene/hair.h"
13# include "scene/mesh.h"
14# include "scene/object.h"
15# include "scene/pointcloud.h"
16
17# include "util/progress.h"
18
19# include "device/metal/bvh.h"
20# include "device/metal/util.h"
21
23
24# define BVH_status(...) \
25 { \
26 string str = string_printf(__VA_ARGS__); \
27 progress.set_substatus(str); \
28 metal_printf("%s", str.c_str()); \
29 }
30
31// # define BVH_THROTTLE_DIAGNOSTICS
32# ifdef BVH_THROTTLE_DIAGNOSTICS
33# define bvh_throttle_printf(...) printf("BVHMetalBuildThrottler::" __VA_ARGS__)
34# else
35# define bvh_throttle_printf(...)
36# endif
37
38/* This flag didn't exist until Xcode 26.0, so we ensure that it is defined for
39 * forward-compatibility.
40 */
41# ifndef MAC_OS_VERSION_26_0
42# define MTLAccelerationStructureUsagePreferFastIntersection \
43 MTLAccelerationStructureUsage(1 << 4)
44# endif
45
46/* Limit the number of concurrent BVH builds so that we don't approach unsafe GPU working set
47 * sizes. */
48struct BVHMetalBuildThrottler {
49 thread_mutex mutex;
50 size_t wired_memory = 0;
51 size_t safe_wired_limit = 0;
52 int requests_in_flight = 0;
53
54 BVHMetalBuildThrottler()
55 {
56 /* The default device will always be the one that supports MetalRT if the machine supports it.
57 */
58 id<MTLDevice> mtlDevice = MTLCreateSystemDefaultDevice();
59
60 /* Set a conservative limit, but which will still only throttle in extreme cases. */
61 safe_wired_limit = [mtlDevice recommendedMaxWorkingSetSize] / 4;
62 bvh_throttle_printf("safe_wired_limit = %zu\n", safe_wired_limit);
63 }
64
65 /* Block until we're safely able to wire the requested resources. */
66 void acquire(const size_t bytes_to_be_wired)
67 {
68 bool throttled = false;
69 while (true) {
70 {
72
73 /* Always allow a BVH build to proceed if no other is in flight, otherwise
74 * only proceed if we're within safe limits. */
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",
79 requests_in_flight,
80 wired_memory);
81 return;
82 }
83
84 if (!throttled) {
85 bvh_throttle_printf(
86 "acquire -- throttling (requests_in_flight = %d, wired_memory = %zu, "
87 "bytes_to_be_wired = %zu)\n",
88 requests_in_flight,
89 wired_memory,
90 bytes_to_be_wired);
91 }
92 throttled = true;
93 }
94
95 std::this_thread::sleep_for(std::chrono::milliseconds(10));
96 }
97 }
98
99 /* Notify of resources that have stopped being wired. */
100 void release(const size_t bytes_just_unwired)
101 {
103 wired_memory -= bytes_just_unwired;
104 requests_in_flight -= 1;
105 bvh_throttle_printf("release (requests_in_flight = %d, wired_memory = %zu)\n",
106 requests_in_flight,
107 wired_memory);
108 }
109
110 /* Wait for all outstanding work to finish. */
111 void wait_for_all()
112 {
113 while (true) {
114 {
116 if (wired_memory == 0) {
117 return;
118 }
119 }
120 std::this_thread::sleep_for(std::chrono::milliseconds(10));
121 }
122 }
123} g_bvh_build_throttler;
124
125/* macOS 15.2 and 15.3 has a bug in the dynamic BVH refitting which leads to missing geometry
126 * during render. The issue is fixed in the macOS 15.4, until then disable refitting even for
127 * the viewport.
128 * Note that dynamic BVH is still used on the scene level to speed up updates of instances and
129 * such. #132782. */
130static bool support_refit_blas()
131{
132 if (@available(macos 15.4, *)) {
133 return true;
134 }
135 if (@available(macos 15.2, *)) {
136 return false;
137 }
138 return true;
139}
140
141BVHMetal::BVHMetal(const BVHParams &params_,
142 const vector<Geometry *> &geometry_,
143 const vector<Object *> &objects_,
144 Device *device)
145 : BVH(params_, geometry_, objects_), device(device)
146{
147}
148
149BVHMetal::~BVHMetal()
150{
151 set_accel_struct(nil);
152 if (null_BLAS) {
153 [null_BLAS release];
154 }
155}
156
157API_AVAILABLE(macos(11.0))
158void BVHMetal::set_accel_struct(id<MTLAccelerationStructure> new_accel_struct)
159{
160 if (accel_struct) {
161 [accel_struct release];
162 accel_struct = nil;
163 }
164
165 if (new_accel_struct) {
166 accel_struct = new_accel_struct;
167 }
168}
169
170bool BVHMetal::build_BLAS_mesh(Progress &progress,
171 id<MTLDevice> mtl_device,
172 id<MTLCommandQueue> queue,
173 Geometry *const geom,
174 bool refit)
175{
176 /* Build BLAS for triangle primitives */
177 Mesh *const mesh = static_cast<Mesh *const>(geom);
178 if (mesh->num_triangles() == 0) {
179 return false;
180 }
181
182 const bool use_fast_trace_bvh = (params.bvh_type == BVH_TYPE_STATIC) || !support_refit_blas();
183
184 const packed_float3 *verts = mesh->get_position();
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();
188
189 size_t num_motion_steps = 1;
190 const Attribute *attr_P = mesh->attributes.find(ATTR_STD_POSITION);
191 if (motion_blur && mesh->get_use_motion_blur() && attr_P->has_motion()) {
192 num_motion_steps = mesh->get_motion_steps();
193 }
194
195 /* Upload the mesh data to the GPU */
196 id<MTLBuffer> posBuf = nil;
197 id<MTLBuffer> indexBuf = [mtl_device newBufferWithBytes:tris.data()
198 length:num_indices * sizeof(tris.data()[0])
199 options:MTLResourceStorageModeShared];
200
201 if (num_motion_steps == 1) {
202 posBuf = [mtl_device newBufferWithBytes:verts
203 length:num_verts * sizeof(verts[0])
204 options:MTLResourceStorageModeShared];
205 }
206 else {
207 posBuf = [mtl_device newBufferWithLength:num_verts * num_motion_steps * sizeof(verts[0])
208 options:MTLResourceStorageModeShared];
209 packed_float3 *dest_data = (packed_float3 *)[posBuf contents];
210 for (size_t step = 0; step < num_motion_steps; ++step) {
211 const packed_float3 *verts = attr_P->data_at_time_step<packed_float3>(step,
212 num_motion_steps);
213 std::copy_n(verts, num_verts, dest_data + num_verts * step);
214 }
215 }
216
217 /* Create an acceleration structure. */
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];
224 k.buffer = posBuf;
225 k.offset = num_verts * step * sizeof(packed_float3);
226 vertex_ptrs.push_back(k);
227 }
228
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;
240
241 geomDesc = geomDescMotion;
242
243 BVH_status("Building motion mesh BLAS | %7d tris | %s | %7d motion keyframes",
244 (int)mesh->num_triangles(),
245 geom->name.c_str(),
246 (int)num_motion_steps);
247 }
248 else {
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;
260
261 geomDesc = geomDescNoMotion;
262
263 BVH_status(
264 "Building mesh BLAS | %7d tris | %s", (int)mesh->num_triangles(), geom->name.c_str());
265 }
266
267 /* Force a single any-hit call, so shadow record-all behavior works correctly */
268 /* (Match optix behavior: unsigned int build_flags =
269 * OPTIX_GEOMETRY_FLAG_REQUIRE_SINGLE_ANYHIT_CALL;) */
270 geomDesc.allowDuplicateIntersectionFunctionInvocation = false;
271
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;
281 }
282 if (extended_limits) {
283 accelDesc.usage |= MTLAccelerationStructureUsageExtendedLimits;
284 }
285
286 if (!use_fast_trace_bvh) {
287 accelDesc.usage |= (MTLAccelerationStructureUsageRefit |
288 MTLAccelerationStructureUsagePreferFastBuild);
289 }
290 else if (@available(macos 26.0, *)) {
291 accelDesc.usage |= MTLAccelerationStructureUsagePreferFastIntersection;
292 }
293
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];
304 if (refit) {
305 [accelEnc refitAccelerationStructure:accel_struct
306 descriptor:accelDesc
307 destination:accel_uncompressed
308 scratchBuffer:scratchBuf
309 scratchBufferOffset:0];
310 }
311 else {
312 [accelEnc buildAccelerationStructure:accel_uncompressed
313 descriptor:accelDesc
314 scratchBuffer:scratchBuf
315 scratchBufferOffset:0];
316 }
317 if (use_fast_trace_bvh) {
318 [accelEnc writeCompactedAccelerationStructureSize:accel_uncompressed
319 toBuffer:sizeBuf
320 offset:0
321 sizeDataType:MTLDataTypeULong];
322 }
323 [accelEnc endEncoding];
324
325 /* Estimated size of resources that will be wired for the GPU accelerated build.
326 * Acceleration-struct size is doubled to account for possible compaction step. */
327 size_t wired_size = posBuf.allocatedSize + indexBuf.allocatedSize + scratchBuf.allocatedSize +
328 accel_uncompressed.allocatedSize * 2;
329
330 [accelCommands addCompletedHandler:^(id<MTLCommandBuffer> /*command_buffer*/) {
331 /* free temp resources */
332 [scratchBuf release];
333 [indexBuf release];
334 [posBuf release];
335
336 if (use_fast_trace_bvh) {
337 /* Compact the accel structure */
338 uint64_t compressed_size = *(uint64_t *)sizeBuf.contents;
339
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> /*command_buffer*/) {
350 set_accel_struct(accel);
351 [accel_uncompressed release];
352
353 /* Signal that we've finished doing GPU acceleration struct build. */
354 g_bvh_build_throttler.release(wired_size);
355 }];
356 [accelCommands commit];
357 });
358 }
359 else {
360 /* set our acceleration structure to the uncompressed structure */
361 set_accel_struct(accel_uncompressed);
362
363 /* Signal that we've finished doing GPU acceleration struct build. */
364 g_bvh_build_throttler.release(wired_size);
365 }
366
367 [sizeBuf release];
368 }];
369
370 /* Wait until it's safe to proceed with GPU acceleration struct build. */
371 g_bvh_build_throttler.acquire(wired_size);
372 [accelCommands commit];
373
374 return true;
375}
376
377bool BVHMetal::build_BLAS_hair(Progress &progress,
378 id<MTLDevice> mtl_device,
379 id<MTLCommandQueue> queue,
380 Geometry *const geom,
381 bool refit)
382{
383# if defined(MAC_OS_VERSION_14_0)
384 if (@available(macos 14.0, *)) {
385 /* Build BLAS for hair curves */
386 Hair *hair = static_cast<Hair *>(geom);
387 if (hair->num_curves() == 0) {
388 return false;
389 }
390
391 const bool use_fast_trace_bvh = (params.bvh_type == BVH_TYPE_STATIC) || !support_refit_blas();
392
393 size_t num_motion_steps = 1;
394 const Attribute *attr_P = hair->attributes.find(ATTR_STD_POSITION);
395 const Attribute *attr_R = hair->attributes.find(ATTR_STD_RADIUS);
396 if (motion_blur && hair->get_use_motion_blur() && attr_P->has_motion()) {
397 num_motion_steps = hair->get_motion_steps();
398 }
399
400 id<MTLBuffer> cpBuffer = nil;
401 id<MTLBuffer> radiusBuffer = nil;
402 id<MTLBuffer> idxBuffer = nil;
403
404 MTLAccelerationStructureGeometryDescriptor *geomDesc;
405 if (num_motion_steps > 1) {
406 MTLAccelerationStructureMotionCurveGeometryDescriptor *geomDescCrv =
407 [MTLAccelerationStructureMotionCurveGeometryDescriptor descriptor];
408
409 uint64_t numKeys = hair->num_keys();
410 uint64_t numCurves = hair->num_curves();
411
412 /* Gather the curve geometry. */
413 std::vector<packed_float3> cpData;
414 std::vector<int> idxData;
415 std::vector<float> radiusData;
416 cpData.reserve(numKeys);
417 radiusData.reserve(numKeys);
418
419 std::vector<int> step_offsets;
420 for (size_t step = 0; step < num_motion_steps; ++step) {
421 const packed_float3 *keys = attr_P->data_at_time_step<packed_float3>(step,
422 num_motion_steps);
423 const float *radii = attr_R->data_at_time_step<float>(step, num_motion_steps);
424
425 step_offsets.push_back(cpData.size());
426
427 for (int c = 0; c < numCurves; ++c) {
428 const Hair::Curve curve = hair->get_curve(c);
429 int segCount = curve.num_segments();
430 int firstKey = curve.first_key;
431 uint64_t idxBase = cpData.size();
432 if (hair->curve_shape != CURVE_THICK_LINEAR) {
433 cpData.push_back(keys[firstKey]);
434 radiusData.push_back(radii[firstKey]);
435 }
436 for (int s = 0; s < segCount; ++s) {
437 if (step == 0) {
438 idxData.push_back(idxBase + s);
439 }
440 cpData.push_back(keys[firstKey + s]);
441 radiusData.push_back(radii[firstKey + s]);
442 }
443 cpData.push_back(keys[firstKey + curve.num_keys - 1]);
444 radiusData.push_back(radii[firstKey + curve.num_keys - 1]);
445 if (hair->curve_shape != CURVE_THICK_LINEAR) {
446 cpData.push_back(keys[firstKey + curve.num_keys - 1]);
447 radiusData.push_back(radii[firstKey + curve.num_keys - 1]);
448 }
449 }
450 }
451
452 /* Allocate and populate MTLBuffers for geometry. */
453 idxBuffer = [mtl_device newBufferWithBytes:idxData.data()
454 length:idxData.size() * sizeof(int)
455 options:MTLResourceStorageModeShared];
456
457 cpBuffer = [mtl_device newBufferWithBytes:cpData.data()
458 length:cpData.size() * sizeof(packed_float3)
459 options:MTLResourceStorageModeShared];
460
461 radiusBuffer = [mtl_device newBufferWithBytes:radiusData.data()
462 length:radiusData.size() * sizeof(float)
463 options:MTLResourceStorageModeShared];
464
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);
469
470 for (size_t step = 0; step < num_motion_steps; ++step) {
471 MTLMotionKeyframeData *k = [MTLMotionKeyframeData data];
472 k.buffer = cpBuffer;
473 k.offset = step_offsets[step] * sizeof(packed_float3);
474 cp_ptrs.push_back(k);
475
476 k = [MTLMotionKeyframeData data];
477 k.buffer = radiusBuffer;
478 k.offset = step_offsets[step] * sizeof(float);
479 radius_ptrs.push_back(k);
480 }
481
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()];
486
487 /* controlPointCount should specify the *per-step* control point count. */
488 geomDescCrv.controlPointCount = cpData.size() / num_motion_steps;
489 geomDescCrv.controlPointStride = sizeof(packed_float3);
490 geomDescCrv.controlPointFormat = MTLAttributeFormatFloat3;
491 geomDescCrv.radiusStride = sizeof(float);
492 geomDescCrv.radiusFormat = MTLAttributeFormatFloat;
493 geomDescCrv.segmentCount = idxData.size();
494 geomDescCrv.segmentControlPointCount = (hair->curve_shape == CURVE_THICK_LINEAR) ? 2 : 4;
495 geomDescCrv.curveType = (hair->curve_shape == CURVE_RIBBON) ? MTLCurveTypeFlat :
496 MTLCurveTypeRound;
497 if (hair->curve_shape == CURVE_THICK_LINEAR) {
498 geomDescCrv.curveBasis = MTLCurveBasisLinear;
499 geomDescCrv.curveEndCaps = MTLCurveEndCapsSphere;
500 }
501 else {
502 geomDescCrv.curveBasis = MTLCurveBasisCatmullRom;
503 geomDescCrv.curveEndCaps = MTLCurveEndCapsDisk;
504 }
505 geomDescCrv.indexType = MTLIndexTypeUInt32;
506 geomDescCrv.indexBuffer = idxBuffer;
507 geomDescCrv.intersectionFunctionTableOffset = 1;
508
509 /* Force a single any-hit call, so shadow record-all behavior works correctly */
510 /* (Match optix behavior: unsigned int build_flags =
511 * OPTIX_GEOMETRY_FLAG_REQUIRE_SINGLE_ANYHIT_CALL;) */
512 geomDescCrv.allowDuplicateIntersectionFunctionInvocation = false;
513 geomDescCrv.opaque = true;
514 geomDesc = geomDescCrv;
515 }
516 else {
517 MTLAccelerationStructureCurveGeometryDescriptor *geomDescCrv =
518 [MTLAccelerationStructureCurveGeometryDescriptor descriptor];
519
520 uint64_t numKeys = hair->num_keys();
521 uint64_t numCurves = hair->num_curves();
522 const float *radiuses = hair->get_radius();
523
524 /* Gather the curve geometry. */
525 std::vector<packed_float3> cpData;
526 std::vector<int> idxData;
527 std::vector<float> radiusData;
528 cpData.reserve(numKeys);
529 radiusData.reserve(numKeys);
530 const packed_float3 *keys = hair->get_position();
531 for (int c = 0; c < numCurves; ++c) {
532 const Hair::Curve curve = hair->get_curve(c);
533 int segCount = curve.num_segments();
534 int firstKey = curve.first_key;
535 uint64_t idxBase = cpData.size();
536 if (hair->curve_shape != CURVE_THICK_LINEAR) {
537 cpData.push_back(keys[firstKey]);
538 radiusData.push_back(radiuses[firstKey]);
539 }
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]);
544 }
545 cpData.push_back(keys[firstKey + curve.num_keys - 1]);
546 radiusData.push_back(radiuses[firstKey + curve.num_keys - 1]);
547 if (hair->curve_shape != CURVE_THICK_LINEAR) {
548 cpData.push_back(keys[firstKey + curve.num_keys - 1]);
549 radiusData.push_back(radiuses[firstKey + curve.num_keys - 1]);
550 }
551 }
552
553 /* Allocate and populate MTLBuffers for geometry. */
554 idxBuffer = [mtl_device newBufferWithBytes:idxData.data()
555 length:idxData.size() * sizeof(int)
556 options:MTLResourceStorageModeShared];
557
558 cpBuffer = [mtl_device newBufferWithBytes:cpData.data()
559 length:cpData.size() * sizeof(packed_float3)
560 options:MTLResourceStorageModeShared];
561
562 radiusBuffer = [mtl_device newBufferWithBytes:radiusData.data()
563 length:radiusData.size() * sizeof(float)
564 options:MTLResourceStorageModeShared];
565
566 geomDescCrv.controlPointBuffer = cpBuffer;
567 geomDescCrv.radiusBuffer = radiusBuffer;
568 geomDescCrv.controlPointCount = cpData.size();
569 geomDescCrv.controlPointStride = sizeof(packed_float3);
570 geomDescCrv.controlPointFormat = MTLAttributeFormatFloat3;
571 geomDescCrv.controlPointBufferOffset = 0;
572 geomDescCrv.segmentCount = idxData.size();
573 geomDescCrv.segmentControlPointCount = (hair->curve_shape == CURVE_THICK_LINEAR) ? 2 : 4;
574 geomDescCrv.curveType = (hair->curve_shape == CURVE_RIBBON) ? MTLCurveTypeFlat :
575 MTLCurveTypeRound;
576 if (hair->curve_shape == CURVE_THICK_LINEAR) {
577 geomDescCrv.curveBasis = MTLCurveBasisLinear;
578 geomDescCrv.curveEndCaps = MTLCurveEndCapsSphere;
579 }
580 else {
581 geomDescCrv.curveBasis = MTLCurveBasisCatmullRom;
582 geomDescCrv.curveEndCaps = MTLCurveEndCapsDisk;
583 }
584 geomDescCrv.indexType = MTLIndexTypeUInt32;
585 geomDescCrv.indexBuffer = idxBuffer;
586 geomDescCrv.intersectionFunctionTableOffset = 1;
587
588 /* Force a single any-hit call, so shadow record-all behavior works correctly */
589 /* (Match optix behavior: unsigned int build_flags =
590 * OPTIX_GEOMETRY_FLAG_REQUIRE_SINGLE_ANYHIT_CALL;) */
591 geomDescCrv.allowDuplicateIntersectionFunctionInvocation = false;
592 geomDescCrv.opaque = true;
593 geomDesc = geomDescCrv;
594 }
595
596 MTLPrimitiveAccelerationStructureDescriptor *accelDesc =
597 [MTLPrimitiveAccelerationStructureDescriptor descriptor];
598 accelDesc.geometryDescriptors = @[ geomDesc ];
599
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;
606
607 BVH_status("Building motion hair BLAS | %7d curves | %s | %7d motion keyframes",
608 (int)hair->num_curves(),
609 geom->name.c_str(),
610 (int)num_motion_steps);
611 }
612 else {
613 BVH_status(
614 "Building hair BLAS | %7d curves | %s", (int)hair->num_curves(), geom->name.c_str());
615 }
616
617 if (extended_limits) {
618 accelDesc.usage |= MTLAccelerationStructureUsageExtendedLimits;
619 }
620
621 if (!use_fast_trace_bvh) {
622 accelDesc.usage |= (MTLAccelerationStructureUsageRefit |
623 MTLAccelerationStructureUsagePreferFastBuild);
624 }
625 else if (@available(macos 26.0, *)) {
626 accelDesc.usage |= MTLAccelerationStructureUsagePreferFastIntersection;
627 }
628
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];
640 if (refit) {
641 [accelEnc refitAccelerationStructure:accel_struct
642 descriptor:accelDesc
643 destination:accel_uncompressed
644 scratchBuffer:scratchBuf
645 scratchBufferOffset:0];
646 }
647 else {
648 [accelEnc buildAccelerationStructure:accel_uncompressed
649 descriptor:accelDesc
650 scratchBuffer:scratchBuf
651 scratchBufferOffset:0];
652 }
653 if (use_fast_trace_bvh) {
654 [accelEnc writeCompactedAccelerationStructureSize:accel_uncompressed
655 toBuffer:sizeBuf
656 offset:0
657 sizeDataType:MTLDataTypeULong];
658 }
659 [accelEnc endEncoding];
660
661 /* Estimated size of resources that will be wired for the GPU accelerated build.
662 * Acceleration-struct size is doubled to account for possible compaction step. */
663 size_t wired_size = cpBuffer.allocatedSize + radiusBuffer.allocatedSize +
664 idxBuffer.allocatedSize + scratchBuf.allocatedSize +
665 accel_uncompressed.allocatedSize * 2;
666
667 [accelCommands addCompletedHandler:^(id<MTLCommandBuffer> /*command_buffer*/) {
668 /* free temp resources */
669 [scratchBuf release];
670 [cpBuffer release];
671 [radiusBuffer release];
672 [idxBuffer release];
673
674 if (use_fast_trace_bvh) {
675 uint64_t compressed_size = *(uint64_t *)sizeBuf.contents;
676
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> /*command_buffer*/) {
687 set_accel_struct(accel);
688 [accel_uncompressed release];
689
690 /* Signal that we've finished doing GPU acceleration struct build. */
691 g_bvh_build_throttler.release(wired_size);
692 }];
693 [accelCommands commit];
694 });
695 }
696 else {
697 /* set our acceleration structure to the uncompressed structure */
698 set_accel_struct(accel_uncompressed);
699
700 /* Signal that we've finished doing GPU acceleration struct build. */
701 g_bvh_build_throttler.release(wired_size);
702 }
703
704 [sizeBuf release];
705 }];
706
707 /* Wait until it's safe to proceed with GPU acceleration struct build. */
708 g_bvh_build_throttler.acquire(wired_size);
709 [accelCommands commit];
710
711 return true;
712 }
713# else /* MAC_OS_VERSION_14_0 */
714 (void)progress;
715 (void)mtl_device;
716 (void)queue;
717 (void)geom;
718 (void)(refit);
719# endif /* MAC_OS_VERSION_14_0 */
720 return false;
721}
722
723bool BVHMetal::build_BLAS_pointcloud(Progress &progress,
724 id<MTLDevice> mtl_device,
725 id<MTLCommandQueue> queue,
726 Geometry *const geom,
727 bool refit)
728{
729 /* Build BLAS for point cloud */
730 PointCloud *pointcloud = static_cast<PointCloud *>(geom);
731 if (pointcloud->num_points() == 0) {
732 return false;
733 }
734
735 const size_t num_points = pointcloud->num_points();
736
737 const bool use_fast_trace_bvh = (params.bvh_type == BVH_TYPE_STATIC) || !support_refit_blas();
738
739 size_t num_motion_steps = 1;
740 Attribute *attr_P = pointcloud->attributes.find(ATTR_STD_POSITION);
741 Attribute *attr_R = pointcloud->attributes.find(ATTR_STD_RADIUS);
742 if (motion_blur && pointcloud->get_use_motion_blur() && attr_P->has_motion()) {
743 num_motion_steps = pointcloud->get_motion_steps();
744 }
745
746 const size_t num_aabbs = num_motion_steps * num_points;
747
748 /* Allocate a GPU buffer for the AABB data and populate it */
749 id<MTLBuffer> aabbBuf = [mtl_device
750 newBufferWithLength:num_aabbs * sizeof(MTLAxisAlignedBoundingBox)
751 options:MTLResourceStorageModeShared];
752 MTLAxisAlignedBoundingBox *aabb_data = (MTLAxisAlignedBoundingBox *)[aabbBuf contents];
753
754 /* Get AABBs for each motion step */
755 for (size_t step = 0; step < num_motion_steps; ++step) {
756 const packed_float3 *step_points = attr_P->data_at_time_step<packed_float3>(step,
757 num_motion_steps);
758 const float *step_radius = attr_R->data_at_time_step<float>(step, num_motion_steps);
759
760 for (size_t j = 0; j < num_points; ++j) {
761 const PointCloud::Point point = pointcloud->get_point(j);
763 point.bounds_grow(step_points, step_radius, bounds);
764
765 const size_t index = step * num_points + j;
766 aabb_data[index].min = (MTLPackedFloat3 &)bounds.min;
767 aabb_data[index].max = (MTLPackedFloat3 &)bounds.max;
768 }
769 }
770
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];
777 k.buffer = aabbBuf;
778 k.offset = step * num_points * sizeof(MTLAxisAlignedBoundingBox);
779 aabb_ptrs.push_back(k);
780 }
781
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;
789
790 /* Force a single any-hit call, so shadow record-all behavior works correctly */
791 /* (Match optix behavior: unsigned int build_flags =
792 * OPTIX_GEOMETRY_FLAG_REQUIRE_SINGLE_ANYHIT_CALL;) */
793 geomDescMotion.allowDuplicateIntersectionFunctionInvocation = false;
794 geomDescMotion.opaque = true;
795 geomDesc = geomDescMotion;
796 }
797 else {
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;
805
806 /* Force a single any-hit call, so shadow record-all behavior works correctly */
807 /* (Match optix behavior: unsigned int build_flags =
808 * OPTIX_GEOMETRY_FLAG_REQUIRE_SINGLE_ANYHIT_CALL;) */
809 geomDescNoMotion.allowDuplicateIntersectionFunctionInvocation = false;
810 geomDescNoMotion.opaque = true;
811 geomDesc = geomDescNoMotion;
812 }
813
814 MTLPrimitiveAccelerationStructureDescriptor *accelDesc =
815 [MTLPrimitiveAccelerationStructureDescriptor descriptor];
816 accelDesc.geometryDescriptors = @[ geomDesc ];
817
818 if (num_motion_steps > 1) {
819 accelDesc.motionStartTime = 0.0f;
820 accelDesc.motionEndTime = 1.0f;
821 // accelDesc.motionStartBorderMode = MTLMotionBorderModeVanish;
822 // accelDesc.motionEndBorderMode = MTLMotionBorderModeVanish;
823 accelDesc.motionKeyframeCount = num_motion_steps;
824
825 BVH_status("Building motion pointcloud BLAS | %7d points | %s | %7d motion keyframes",
826 (int)pointcloud->num_points(),
827 geom->name.c_str(),
828 (int)num_motion_steps);
829 }
830 else {
831 BVH_status("Building pointcloud BLAS | %7d points | %s",
832 (int)pointcloud->num_points(),
833 geom->name.c_str());
834 }
835 if (extended_limits) {
836 accelDesc.usage |= MTLAccelerationStructureUsageExtendedLimits;
837 }
838
839 if (!use_fast_trace_bvh) {
840 accelDesc.usage |= (MTLAccelerationStructureUsageRefit |
841 MTLAccelerationStructureUsagePreferFastBuild);
842 }
843 else if (@available(macos 26.0, *)) {
844 accelDesc.usage |= MTLAccelerationStructureUsagePreferFastIntersection;
845 }
846
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];
857 if (refit) {
858 [accelEnc refitAccelerationStructure:accel_struct
859 descriptor:accelDesc
860 destination:accel_uncompressed
861 scratchBuffer:scratchBuf
862 scratchBufferOffset:0];
863 }
864 else {
865 [accelEnc buildAccelerationStructure:accel_uncompressed
866 descriptor:accelDesc
867 scratchBuffer:scratchBuf
868 scratchBufferOffset:0];
869 }
870 if (use_fast_trace_bvh) {
871 [accelEnc writeCompactedAccelerationStructureSize:accel_uncompressed
872 toBuffer:sizeBuf
873 offset:0
874 sizeDataType:MTLDataTypeULong];
875 }
876 [accelEnc endEncoding];
877
878 /* Estimated size of resources that will be wired for the GPU accelerated build.
879 * Acceleration-struct size is doubled to account for possible compaction step. */
880 size_t wired_size = aabbBuf.allocatedSize + scratchBuf.allocatedSize +
881 accel_uncompressed.allocatedSize * 2;
882
883 [accelCommands addCompletedHandler:^(id<MTLCommandBuffer> /*command_buffer*/) {
884 /* free temp resources */
885 [scratchBuf release];
886 [aabbBuf release];
887
888 if (use_fast_trace_bvh) {
889 /* Compact the accel structure */
890 uint64_t compressed_size = *(uint64_t *)sizeBuf.contents;
891
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> /*command_buffer*/) {
902 set_accel_struct(accel);
903 [accel_uncompressed release];
904
905 /* Signal that we've finished doing GPU acceleration struct build. */
906 g_bvh_build_throttler.release(wired_size);
907 }];
908 [accelCommands commit];
909 });
910 }
911 else {
912 /* set our acceleration structure to the uncompressed structure */
913 set_accel_struct(accel_uncompressed);
914
915 /* Signal that we've finished doing GPU acceleration struct build. */
916 g_bvh_build_throttler.release(wired_size);
917 }
918
919 [sizeBuf release];
920 }];
921
922 /* Wait until it's safe to proceed with GPU acceleration struct build. */
923 g_bvh_build_throttler.acquire(wired_size);
924 [accelCommands commit];
925 return true;
926}
927
928bool BVHMetal::build_BLAS(Progress &progress,
929 id<MTLDevice> mtl_device,
930 id<MTLCommandQueue> queue,
931 bool refit)
932{
933 assert(objects.size() == 1 && geometry.size() == 1);
934
935 /* Build bottom level acceleration structures (BLAS) */
936 Geometry *const geom = geometry[0];
937 switch (geom->geometry_type) {
938 case Geometry::VOLUME:
939 case Geometry::MESH:
940 return build_BLAS_mesh(progress, mtl_device, queue, geom, refit);
941 case Geometry::HAIR:
942 return build_BLAS_hair(progress, mtl_device, queue, geom, refit);
944 return build_BLAS_pointcloud(progress, mtl_device, queue, geom, refit);
945 default:
946 return false;
947 }
948 return false;
949}
950
951# if defined(MAC_OS_VERSION_15_0)
952
953/* Return MTLComponentTransform from a DecomposedTransform. */
954static MTLComponentTransform decomposed_to_component_transform(const DecomposedTransform &src)
955{
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);
962 return tfm;
963}
964
965/* Return unit MTLComponentTransform. */
966static MTLComponentTransform component_transform_make_unit()
967{
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);
974 return tfm;
975}
976
977# endif
978
979bool BVHMetal::build_TLAS(Progress &progress,
980 id<MTLDevice> mtl_device,
981 id<MTLCommandQueue> queue,
982 bool refit)
983{
984 /* Wait for all BLAS builds to finish. */
985 g_bvh_build_throttler.wait_for_all();
986
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];
991
992 /* Create an acceleration structure. */
993 MTLAccelerationStructureTriangleGeometryDescriptor *geomDesc =
994 [MTLAccelerationStructureTriangleGeometryDescriptor descriptor];
995 geomDesc.vertexBuffer = nullBuf;
996 geomDesc.vertexBufferOffset = 0;
997 geomDesc.vertexStride = sizeof(packed_float3);
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;
1005
1006 MTLPrimitiveAccelerationStructureDescriptor *accelDesc =
1007 [MTLPrimitiveAccelerationStructureDescriptor descriptor];
1008 accelDesc.geometryDescriptors = @[ geomDesc ];
1009 if (extended_limits) {
1010 accelDesc.usage |= MTLAccelerationStructureUsageExtendedLimits;
1011 }
1012
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
1025 descriptor:accelDesc
1026 scratchBuffer:scratchBuf
1027 scratchBufferOffset:0];
1028 [accelEnc endEncoding];
1029 [accelCommands commit];
1030 [accelCommands waitUntilCompleted];
1031
1032 /* free temp resources */
1033 [scratchBuf release];
1034 [nullBuf release];
1035 [sizeBuf release];
1036
1037 return accel_struct;
1038 };
1039
1040 uint32_t num_instances = 0;
1041 uint32_t num_motion_transforms = 0;
1042 uint32_t num_motion_instances = 0;
1043 for (Object *ob : objects) {
1044 num_instances++;
1045
1046 if (ob->use_motion()) {
1047 num_motion_transforms += max((size_t)1, ob->get_motion().size());
1048 num_motion_instances++;
1049 }
1050 else {
1051 num_motion_transforms++;
1052 }
1053 }
1054
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();
1057
1058 NSMutableArray *all_blas = [NSMutableArray array];
1059 unordered_map<const BVHMetal *, int> instance_mapping;
1060
1061 /* Lambda function to build/retrieve the BLAS index mapping */
1062 auto get_blas_index = [&](const BVHMetal *blas) {
1063 auto it = instance_mapping.find(blas);
1064 if (it != instance_mapping.end()) {
1065 return it->second;
1066 }
1067 int blas_index = (int)[all_blas count];
1068 instance_mapping[blas] = blas_index;
1069 [all_blas addObject:(blas ? blas->accel_struct : null_BLAS)];
1070 return blas_index;
1071 };
1072
1073 size_t instance_size;
1074 if (use_instance_motion) {
1075 instance_size = sizeof(MTLAccelerationStructureMotionInstanceDescriptor);
1076 }
1077 else {
1078 instance_size = sizeof(MTLAccelerationStructureUserIDInstanceDescriptor);
1079 }
1080
1081 /* Allocate a GPU buffer for the instance data and populate it */
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;
1088# endif
1089 if (use_instance_motion && num_motion_transforms) {
1090# if defined(MAC_OS_VERSION_15_0)
1091 if (use_pcmi) {
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;
1097 }
1098 }
1099 else
1100# endif
1101 {
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;
1106 }
1107 }
1108
1110 uint32_t motion_transform_index = 0;
1111
1112 blas_array.clear();
1113 blas_array.reserve(num_instances);
1114
1115 for (Object *ob : objects) {
1116 /* Skip non-traceable 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()) {
1120 /* Place a degenerate instance, to ensure [[instance_id]] equals ob->get_device_index()
1121 * in our intersection functions */
1122 blas = nullptr;
1123
1124 /* Workaround for issue in macOS <= 14.1: Insert degenerate BLAS instead of zero-filling
1125 * the descriptor. */
1126 if (!null_BLAS) {
1127 null_BLAS = make_null_BLAS(mtl_device, queue);
1128 }
1129 blas_array.push_back(null_BLAS);
1130 }
1131 else {
1132 blas_array.push_back(blas->accel_struct);
1133 }
1134
1135 uint32_t accel_struct_index = get_blas_index(blas);
1136
1137 /* The MetalRT visibility mask can only contain 8 bits by default. */
1138 uint32_t mask = ob->visibility_for_tracing() & 0xFF;
1139
1140 /* Have to have at least one bit in the mask, or else instance would always be culled. */
1141 if (0 == mask) {
1142 mask = 0xFF;
1143 }
1144
1145 /* Set user instance ID to object index */
1146 uint32_t primitive_offset = 0;
1147 int currIndex = instance_index++;
1148
1149 if (geom->is_hair()) {
1150 /* Build BLAS for curve primitives. */
1151 Hair *const hair = static_cast<Hair *const>(const_cast<Geometry *>(geom));
1152 primitive_offset = uint32_t(hair->curve_segment_offset);
1153 }
1154 else if (geom->is_mesh() || geom->is_volume()) {
1155 /* Build BLAS for triangle primitives. */
1156 Mesh *const mesh = static_cast<Mesh *const>(const_cast<Geometry *>(geom));
1157 primitive_offset = uint32_t(mesh->prim_offset);
1158 }
1159 else if (geom->is_pointcloud()) {
1160 /* Build BLAS for points primitives. */
1161 PointCloud *const pointcloud = static_cast<PointCloud *const>(const_cast<Geometry *>(geom));
1162 primitive_offset = uint32_t(pointcloud->prim_offset);
1163 }
1164
1165 /* Bake into the appropriate descriptor */
1166 if (use_instance_motion) {
1167 MTLAccelerationStructureMotionInstanceDescriptor *instances =
1168 (MTLAccelerationStructureMotionInstanceDescriptor *)[instanceBuf contents];
1169 MTLAccelerationStructureMotionInstanceDescriptor &desc = instances[currIndex];
1170
1171 desc.accelerationStructureIndex = accel_struct_index;
1172 desc.userID = primitive_offset;
1173 desc.mask = mask;
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;
1180
1181 array<DecomposedTransform> decomp(ob->get_motion().size());
1182 transform_motion_decompose(decomp.data(), ob->get_motion().data(), ob->get_motion().size());
1183
1184 int key_count = ob->get_motion().size();
1185 if (key_count) {
1186 desc.motionTransformsCount = key_count;
1187
1188# if defined(MAC_OS_VERSION_15_0)
1189 if (use_pcmi) {
1190 for (int i = 0; i < key_count; i++) {
1191 decomposed_motion_transforms[motion_transform_index++] =
1192 decomposed_to_component_transform(decomp[i]);
1193 }
1194 }
1195 else
1196# endif
1197 {
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++];
1201 /* Transpose transform */
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)];
1205 }
1206 }
1207 }
1208 }
1209 else {
1210 desc.motionTransformsCount = 1;
1211
1212# if defined(MAC_OS_VERSION_15_0)
1213 if (use_pcmi) {
1214 if (ob->get_geometry()->is_instanced()) {
1215 DecomposedTransform decomp;
1216 transform_motion_decompose(&decomp, &ob->get_tfm(), 1);
1217 decomposed_motion_transforms[motion_transform_index++] =
1218 decomposed_to_component_transform(decomp);
1219 }
1220 else {
1221 decomposed_motion_transforms[motion_transform_index++] =
1222 component_transform_make_unit();
1223 }
1224 }
1225 else
1226# endif
1227 {
1228 float *t = (float *)&matrix_motion_transforms[motion_transform_index++];
1229 if (ob->get_geometry()->is_instanced()) {
1230 /* Transpose transform */
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)];
1234 }
1235 }
1236 else {
1237 /* Clear transform to identity matrix */
1238 t[0] = t[4] = t[8] = 1.0f;
1239 }
1240 }
1241 }
1242 }
1243 else {
1244 MTLAccelerationStructureUserIDInstanceDescriptor *instances =
1245 (MTLAccelerationStructureUserIDInstanceDescriptor *)[instanceBuf contents];
1246 MTLAccelerationStructureUserIDInstanceDescriptor &desc = instances[currIndex];
1247
1248 desc.accelerationStructureIndex = accel_struct_index;
1249 desc.userID = primitive_offset;
1250 desc.mask = mask;
1251 desc.intersectionFunctionTableOffset = 0;
1252 desc.options = MTLAccelerationStructureInstanceOptionOpaque;
1253
1254 float *t = (float *)&desc.transformationMatrix;
1255 if (ob->get_geometry()->is_instanced()) {
1256 /* Transpose transform */
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)];
1260 }
1261 }
1262 else {
1263 /* Clear transform to identity matrix */
1264 t[0] = t[4] = t[8] = 1.0f;
1265 }
1266 }
1267 }
1268
1269 if (use_instance_motion) {
1270 BVH_status(
1271 "Building motion TLAS | %7d instances | %7d motion instances | %7d motion "
1272 "transforms",
1273 (int)num_instances,
1274 (int)num_motion_instances,
1275 (int)num_motion_transforms);
1276 }
1277 else {
1278 BVH_status("Building TLAS | %7d instances", (int)num_instances);
1279 }
1280
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;
1289
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;
1299 }
1300# endif
1301 }
1302
1303 if (extended_limits) {
1304 accelDesc.usage |= MTLAccelerationStructureUsageExtendedLimits;
1305 }
1306 if (!use_fast_trace_bvh) {
1307 accelDesc.usage |= (MTLAccelerationStructureUsageRefit |
1308 MTLAccelerationStructureUsagePreferFastBuild);
1309 }
1310 else if (@available(macos 26.0, *)) {
1311 accelDesc.usage |= MTLAccelerationStructureUsagePreferFastIntersection;
1312 }
1313
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];
1323 if (refit) {
1324 [accelEnc refitAccelerationStructure:accel_struct
1325 descriptor:accelDesc
1326 destination:accel
1327 scratchBuffer:scratchBuf
1328 scratchBufferOffset:0];
1329 }
1330 else {
1331 [accelEnc buildAccelerationStructure:accel
1332 descriptor:accelDesc
1333 scratchBuffer:scratchBuf
1334 scratchBufferOffset:0];
1335 }
1336 [accelEnc endEncoding];
1337 [accelCommands commit];
1338 [accelCommands waitUntilCompleted];
1339
1340 if (motion_transforms_buf) {
1341 [motion_transforms_buf release];
1342 }
1343 [instanceBuf release];
1344 [scratchBuf release];
1345
1346 /* Cache top and bottom-level acceleration structs */
1347 set_accel_struct(accel);
1348
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);
1353 }];
1354
1355 return true;
1356}
1357
1358bool BVHMetal::build(Progress &progress,
1359 id<MTLDevice> mtl_device,
1360 id<MTLCommandQueue> queue,
1361 bool refit)
1362{
1363 if (refit) {
1364 /* It isn't valid to refit a non-existent BVH, or one which wasn't constructed as dynamic.
1365 * In such cases, assert in development but try to recover in the wild. */
1366 if (params.bvh_type != BVH_TYPE_DYNAMIC) {
1367 assert(!"Can't refit static Metal BVH");
1368 refit = false;
1369 }
1370 else if (!accel_struct) {
1371 /* It is possible that the acceleration structure does not exist here: for example, when
1372 * new points were added to a previously empty geometry. */
1373 refit = false;
1374 }
1375 }
1376
1377 if (!refit) {
1378 set_accel_struct(nil);
1379 }
1380
1381 if (!support_refit_blas()) {
1382 refit = false;
1383 }
1384
1385 @autoreleasepool {
1386 if (!params.top_level) {
1387 return build_BLAS(progress, mtl_device, queue, refit);
1388 }
1389 return build_TLAS(progress, mtl_device, queue, refit);
1390 }
1391}
1392
1394
1395#endif /* WITH_METAL */
volatile int lock
unsigned long long int uint64_t
static btDbvtVolume bounds(btDbvtNode **leaves, int count)
Definition btDbvt.cpp:299
btScalar max
void refit(btStridingMeshInterface *triangles, const btVector3 &aabbMin, const btVector3 &aabbMax)
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
float3 max
Definition boundbox.h:20
float3 min
Definition boundbox.h:20
Type geometry_type
bool is_volume() const
bool is_pointcloud() const
bool is_hair() const
const packed_float3 * get_position() const
size_t prim_offset
unique_ptr< BVH > bvh
const float * get_radius() const
AttributeSet attributes
bool is_mesh() const
Definition hair.h:13
Curve get_curve(const size_t i) const
Definition hair.h:94
size_t curve_segment_offset
Definition hair.h:76
size_t num_curves() const
Definition hair.h:109
CurveShapeType curve_shape
Definition hair.h:77
size_t num_keys() const
Definition hair.h:103
NODE_DECLARE size_t num_verts() const
size_t num_triangles() const
Definition scene/mesh.h:73
Point get_point(const int i) const
size_t num_points() const
size_t size() const
CCL_NAMESPACE_BEGIN struct Options options
#define CCL_NAMESPACE_END
#define assert(assertion)
#define instance_index
#define index(i)
VecBase< float, D > step(VecOp< float, D >, VecOp< float, D >) RET
float length(VecOp< float, D >) RET
if(state< num_states)
@ CURVE_RIBBON
@ CURVE_THICK_LINEAR
@ ATTR_STD_POSITION
@ ATTR_STD_RADIUS
ccl_device_inline float2 mask(const MaskType mask, const float2 a)
unsigned int uint32_t
Definition BKE_text.h:81
BMesh const char void * data
@ BVH_TYPE_DYNAMIC
Definition params.h:33
@ BVH_TYPE_STATIC
Definition params.h:40
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
int first_key
Definition hair.h:19
int num_segments() const
Definition hair.h:22
int num_keys
Definition hair.h:20
ustring name
Definition graph/node.h:178
void bounds_grow(const packed_float3 *points, const float *radius, BoundBox &bounds) const
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
std::mutex thread_mutex
Definition thread.h:27
std::unique_lock< std::mutex > thread_scoped_lock
Definition thread.h:28
void transform_motion_decompose(DecomposedTransform *decomp, const Transform *motion, const size_t size)