Blender V5.3
mtl_ray_tracing.hh
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2026 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#pragma once
10
11#include "MEM_guardedalloc.h"
12
13#include "BLI_vector.hh"
14
15#include "GPU_ray_tracing.hh"
16
17#include <Metal/Metal.h>
18
19namespace blender::gpu {
20
23 id<MTLAccelerationStructure> acceleration_structure_ = nil;
24 NSMutableArray<MTLAccelerationStructureGeometryDescriptor *> *geometry_ = nil;
25
26 public:
27 MTLBottomLevelAS(const char *name);
28 ~MTLBottomLevelAS() override;
29
30 void add_geometry(IndexBuf &index_buffer, VertBuf &vertex_buffer) override;
31 void build() override;
32
34 {
35 return acceleration_structure_;
36 }
37
38 private:
39 MEM_CXX_CLASS_ALLOC_FUNCS("MTLBottomLevelAS");
40};
41
43class MTLTopLevelAS : public TopLevelAS {
44 struct Instance {
46 const MTLBottomLevelAS *blas;
48 };
49
50 id<MTLAccelerationStructure> acceleration_structure_ = nil;
51 Vector<Instance> instances_;
52 bool is_dirty_ = true;
53 /* Full rebuild needed (topology changed); otherwise a transform update can refit in place. */
54 bool needs_rebuild_ = true;
55
56 public:
57 MTLTopLevelAS(const char *name);
58 ~MTLTopLevelAS() override;
59
60 InstanceID add_instance(const BottomLevelAS &blas, const float4x4 &mat, uint8_t mask) override;
61 void update_instance(InstanceID instance_id, const float4x4 &mat, uint8_t mask) override;
62 void build() override;
63 void bind(int slot) override;
64
66 {
67 return acceleration_structure_;
68 }
69
70 /* Keep the structure and its referenced geometry resident on a command encoder wrapper. */
71 template<typename Encoder> void make_resident(Encoder &enc)
72 {
73 if (acceleration_structure_ != nil) {
74 enc.use_resource(acceleration_structure_, MTLResourceUsageRead);
75 }
76 for (const Instance &inst : instances_) {
77 id<MTLAccelerationStructure> blas = inst.blas->acceleration_structure();
78 if (blas != nil) {
79 enc.use_resource(blas, MTLResourceUsageRead);
80 }
81 }
82 }
83
84 private:
85 MEM_CXX_CLASS_ALLOC_FUNCS("MTLTopLevelAS");
86};
87
88} // namespace blender::gpu
Read Blender memory allocation and freeing.
BottomLevelAS(const char *name)
void add_geometry(IndexBuf &index_buffer, VertBuf &vertex_buffer) override
id< MTLAccelerationStructure > acceleration_structure() const
InstanceID add_instance(const BottomLevelAS &blas, const float4x4 &mat, uint8_t mask) override
void bind(int slot) override
void make_resident(Encoder &enc)
id< MTLAccelerationStructure > acceleration_structure() const
void update_instance(InstanceID instance_id, const float4x4 &mat, uint8_t mask) override
TopLevelAS(const char *name)
#define instance_id
MatBase< float, 4, 4 > float4x4
unsigned char uint8_t
Definition BKE_text.h:79