Blender V4.5
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<MTLArgumentEncoder> mtlBufferKernelParamsEncoder =
28 nil; /* encoder used for fetching device pointers from MTLBuffers */
29 id<MTLCommandQueue> mtlComputeCommandQueue = nil;
30 id<MTLCommandQueue> mtlGeneralCommandQueue = nil;
31 id<MTLArgumentEncoder> mtlAncillaryArgEncoder =
32 nil; /* encoder used for fetching device pointers from MTLBuffers */
33 id<MTLCounterSampleBuffer> mtlCounterSampleBuffer = nil;
34 string source[PSO_NUM];
35 string kernels_md5[PSO_NUM];
36 string global_defines_md5[PSO_NUM];
37
38 bool capture_enabled = false;
39
40 KernelParamsMetal launch_params = {nullptr};
41
42 /* MetalRT members ----------------------------------*/
43 bool use_metalrt = false;
44 bool motion_blur = false;
45 bool use_pcmi = false;
46 id<MTLArgumentEncoder> mtlASArgEncoder =
47 nil; /* encoder used for fetching device pointers from MTLAccelerationStructure */
48
49 id<MTLArgumentEncoder> mtlBlasArgEncoder = nil;
50 id<MTLBuffer> blas_buffer = nil;
51
52 API_AVAILABLE(macos(11.0))
53 vector<id<MTLAccelerationStructure>> unique_blas_array;
54
55 API_AVAILABLE(macos(11.0))
56 id<MTLAccelerationStructure> accel_struct = nil;
57 /*---------------------------------------------------*/
58
59 uint kernel_features = 0;
60 bool using_nanovdb = false;
61 int max_threads_per_threadgroup;
62
63 int mtlDevId = 0;
64 bool has_error = false;
65
66 struct MetalMem {
67 device_memory *mem = nullptr;
68 int pointer_index = -1;
69 id<MTLBuffer> mtlBuffer = nil;
70 id<MTLTexture> mtlTexture = nil;
71 uint64_t offset = 0;
72 uint64_t size = 0;
73 void *hostPtr = nullptr;
74 };
75 using MetalMemMap = map<device_memory *, unique_ptr<MetalMem>>;
76 MetalMemMap metal_mem_map;
77 std::vector<id<MTLResource>> delayed_free_list;
78 std::recursive_mutex metal_mem_map_mutex;
79
80 /* Bindless Textures */
81 bool is_texture(const TextureInfo &tex);
82 device_vector<TextureInfo> texture_info;
83 bool need_texture_info = false;
84 id<MTLArgumentEncoder> mtlTextureArgEncoder = nil;
85 id<MTLArgumentEncoder> mtlBufferArgEncoder = nil;
86 id<MTLBuffer> buffer_bindings_1d = nil;
87 id<MTLBuffer> texture_bindings_2d = nil;
88 id<MTLBuffer> texture_bindings_3d = nil;
89 std::vector<id<MTLTexture>> texture_slot_map;
90
91 MetalPipelineType kernel_specialization_level = PSO_GENERIC;
92
93 int device_id = 0;
94
95 static thread_mutex existing_devices_mutex;
96 static std::map<int, MetalDevice *> active_device_ids;
97
98 static bool is_device_cancelled(const int device_id);
99
100 static MetalDevice *get_device_by_ID(const int device_idID,
101 thread_scoped_lock &existing_devices_mutex_lock);
102
103 bool is_ready(string &status) const override;
104
105 void cancel() override;
106
107 BVHLayoutMask get_bvh_layout_mask(uint /*kernel_features*/) const override;
108
109 void set_error(const string &error) override;
110
111 MetalDevice(const DeviceInfo &info, Stats &stats, Profiler &profiler, bool headless);
112
113 ~MetalDevice() override;
114
115 bool support_device(const uint /*kernel_features*/);
116
117 bool check_peer_access(Device *peer_device) override;
118
119 bool use_adaptive_compilation();
120
121 bool use_local_atomic_sort() const;
122
123 string preprocess_source(MetalPipelineType pso_type,
124 const uint kernel_features,
125 string *source = nullptr);
126
127 void refresh_source_and_kernels_md5(MetalPipelineType pso_type);
128
129 void make_source(MetalPipelineType pso_type, const uint kernel_features);
130
131 bool load_kernels(const uint kernel_features) override;
132
133 void load_texture_info();
134
135 void erase_allocation(device_memory &mem);
136
137 bool should_use_graphics_interop(const GraphicsInteropDevice &interop_device,
138 const bool log) override;
139
140 void *get_native_buffer(device_ptr ptr) override;
141
142 unique_ptr<DeviceQueue> gpu_queue_create() override;
143
144 void build_bvh(BVH *bvh, Progress &progress, bool refit) override;
145
146 void optimize_for_scene(Scene *scene) override;
147
148 static void compile_and_load(const int device_id, MetalPipelineType pso_type);
149
150 /* ------------------------------------------------------------------ */
151 /* low-level memory management */
152
153 bool max_working_set_exceeded(const size_t safety_margin = 8 * 1024 * 1024) const;
154
155 MetalMem *generic_alloc(device_memory &mem);
156
157 void generic_copy_to(device_memory &mem);
158
159 void generic_free(device_memory &mem);
160
161 void mem_alloc(device_memory &mem) override;
162
163 void mem_copy_to(device_memory &mem) override;
164
165 void mem_move_to_host(device_memory &mem) override;
166
167 void mem_copy_from(device_memory &mem)
168 {
169 mem_copy_from(mem, -1, -1, -1, -1);
170 }
171 void mem_copy_from(
172 device_memory &mem, const size_t y, size_t w, const size_t h, size_t elem) override;
173
174 void mem_zero(device_memory &mem) override;
175
176 void mem_free(device_memory &mem) override;
177
178 device_ptr mem_alloc_sub_ptr(device_memory &mem, const size_t offset, size_t /*size*/) override;
179
180 void const_copy_to(const char *name, void *host, const size_t size) override;
181
182 void global_alloc(device_memory &mem);
183 void global_free(device_memory &mem);
184
185 void tex_alloc(device_texture &mem);
186 void tex_alloc_as_buffer(device_texture &mem);
187 void tex_copy_to(device_texture &mem);
188 void tex_free(device_texture &mem);
189
190 void flush_delayed_free_list();
191
192 void free_bvh();
193
194 void update_bvh(BVHMetal *bvh_metal);
195};
196
198
199#endif
unsigned int uint
struct Scene Scene
float progress
Definition WM_types.hh:1019
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
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:4226