Blender V5.0
metal/device_impl.h
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2021-2022 Blender Foundation
2 *
3 * SPDX-License-Identifier: Apache-2.0 */
4
5#pragma once
6
7#ifdef WITH_METAL
8
9# include "bvh/bvh.h"
10# include "device/device.h"
11# include "device/metal/bvh.h"
12# include "device/metal/device.h"
13# include "device/metal/kernel.h"
14# include "device/metal/queue.h"
15# include "device/metal/util.h"
16
17# include <Metal/Metal.h>
18
20
21class DeviceQueue;
22
23class MetalDevice : public Device {
24 public:
25 id<MTLDevice> mtlDevice = nil;
26 id<MTLLibrary> mtlLibrary[PSO_NUM] = {nil};
27 id<MTLCommandQueue> mtlComputeCommandQueue = nil;
28 id<MTLCommandQueue> mtlGeneralCommandQueue = nil;
29 id<MTLCounterSampleBuffer> mtlCounterSampleBuffer = nil;
30 string source[PSO_NUM];
31 string kernels_md5[PSO_NUM];
32 string global_defines_md5[PSO_NUM];
33
34 bool capture_enabled = false;
35
36 /* Argument buffer for static data. */
37 id<MTLBuffer> launch_params_buffer = nil;
38 KernelParamsMetal *launch_params = nullptr;
39
40 /* MetalRT members ----------------------------------*/
41 bool use_metalrt = false;
42 bool use_metalrt_extended_limits = false;
43 bool motion_blur = false;
44 bool use_pcmi = false;
45
46 id<MTLBuffer> blas_buffer = nil;
47
48 API_AVAILABLE(macos(11.0))
49 vector<id<MTLAccelerationStructure>> unique_blas_array;
50
51 API_AVAILABLE(macos(11.0))
52 vector<id<MTLAccelerationStructure>> blas_array;
53
54 API_AVAILABLE(macos(11.0))
55 id<MTLAccelerationStructure> accel_struct = nil;
56 /*---------------------------------------------------*/
57
58 uint kernel_features = 0;
59 bool using_nanovdb = false;
60 int max_threads_per_threadgroup;
61
62 int mtlDevId = 0;
63 bool has_error = false;
64
65 struct MetalMem {
66 device_memory *mem = nullptr;
67 int pointer_index = -1;
68 id<MTLBuffer> mtlBuffer = nil;
69 id<MTLTexture> mtlTexture = nil;
70 uint64_t offset = 0;
71 uint64_t size = 0;
72 void *hostPtr = nullptr;
73 };
74 using MetalMemMap = map<device_memory *, unique_ptr<MetalMem>>;
75 MetalMemMap metal_mem_map;
76 std::vector<id<MTLResource>> delayed_free_list;
77 std::recursive_mutex metal_mem_map_mutex;
78
79 /* Bindless Textures */
80 bool is_texture(const TextureInfo &tex);
81 device_vector<TextureInfo> texture_info;
82 id<MTLBuffer> texture_bindings = nil;
83 std::vector<id<MTLResource>> texture_slot_map;
84
85 MetalPipelineType kernel_specialization_level = PSO_GENERIC;
86
87 int device_id = 0;
88
89 static thread_mutex existing_devices_mutex;
90 static std::map<int, MetalDevice *> active_device_ids;
91
92 static bool is_device_cancelled(const int device_id);
93
94 static MetalDevice *get_device_by_ID(const int device_idID,
95 thread_scoped_lock &existing_devices_mutex_lock);
96
97 bool is_ready(string &status) const override;
98
99 void cancel() override;
100
101 BVHLayoutMask get_bvh_layout_mask(uint /*kernel_features*/) const override;
102
103 void set_error(const string &error) override;
104
105 MetalDevice(const DeviceInfo &info, Stats &stats, Profiler &profiler, bool headless);
106
107 ~MetalDevice() override;
108
109 bool support_device(const uint /*kernel_features*/);
110
111 bool check_peer_access(Device *peer_device) override;
112
113 bool use_adaptive_compilation();
114
115 bool use_local_atomic_sort() const;
116
117 string preprocess_source(MetalPipelineType pso_type,
118 const uint kernel_features,
119 string *source = nullptr);
120
121 void refresh_source_and_kernels_md5(MetalPipelineType pso_type);
122
123 void make_source(MetalPipelineType pso_type, const uint kernel_features);
124
125 bool load_kernels(const uint kernel_features) override;
126
127 void load_texture_info();
128
129 void erase_allocation(device_memory &mem);
130
131 bool should_use_graphics_interop(const GraphicsInteropDevice &interop_device,
132 const bool log) override;
133
134 void *get_native_buffer(device_ptr ptr) override;
135
136 unique_ptr<DeviceQueue> gpu_queue_create() override;
137
138 void build_bvh(BVH *bvh, Progress &progress, bool refit) override;
139
140 bool set_bvh_limits(size_t instance_count, size_t max_prim_count) override;
141
142 void optimize_for_scene(Scene *scene) override;
143
144 static void compile_and_load(const int device_id, MetalPipelineType pso_type);
145
146 /* ------------------------------------------------------------------ */
147 /* low-level memory management */
148
149 bool max_working_set_exceeded(const size_t safety_margin = 8 * 1024 * 1024) const;
150
151 MetalMem *generic_alloc(device_memory &mem);
152
153 void generic_copy_to(device_memory &mem);
154
155 void generic_free(device_memory &mem);
156
157 void mem_alloc(device_memory &mem) override;
158
159 void mem_copy_to(device_memory &mem) override;
160
161 void mem_move_to_host(device_memory &mem) override;
162
163 void mem_copy_from(device_memory &mem)
164 {
165 mem_copy_from(mem, -1, -1, -1, -1);
166 }
167 void mem_copy_from(
168 device_memory &mem, const size_t y, size_t w, const size_t h, size_t elem) override;
169
170 void mem_zero(device_memory &mem) override;
171
172 void mem_free(device_memory &mem) override;
173
174 device_ptr mem_alloc_sub_ptr(device_memory &mem, const size_t offset, size_t /*size*/) override;
175
176 void const_copy_to(const char *name, void *host, const size_t size) override;
177
178 void global_alloc(device_memory &mem);
179 void global_free(device_memory &mem);
180
181 void tex_alloc(device_texture &mem);
182 void tex_alloc_as_buffer(device_texture &mem);
183 void tex_copy_to(device_texture &mem);
184 void tex_free(device_texture &mem);
185
186 void flush_delayed_free_list();
187
188 void free_bvh();
189
190 void update_bvh(BVHMetal *bvh_metal);
191};
192
194
195#endif
unsigned int uint
struct Scene Scene
unsigned long long int uint64_t
static DBVT_INLINE btScalar size(const btDbvtVolume &a)
Definition btDbvt.cpp:52
void refit(btStridingMeshInterface *triangles, const btVector3 &aabbMin, const btVector3 &aabbMax)
SIMD_FORCE_INLINE const btScalar & w() const
Return the w value.
Definition btQuadWord.h:119
#define CCL_NAMESPACE_END
#define log
ccl_device bool BVH_FUNCTION_FULL_NAME BVH(KernelGlobals kg, const ccl_private Ray *ray, ccl_private LocalIntersection *local_isect, const int local_object, ccl_private uint *lcg_state, const int max_hits)
Definition local.h:28
static void error(const char *str)
int BVHLayoutMask
Definition params.h:50
const char * name
const int status
std::mutex thread_mutex
Definition thread.h:27
std::unique_lock< std::mutex > thread_scoped_lock
Definition thread.h:28
uint64_t device_ptr
Definition types_base.h:44
PointerRNA * ptr
Definition wm_files.cc:4238