Blender V4.3
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 string source[PSO_NUM];
34 string kernels_md5[PSO_NUM];
35 string global_defines_md5[PSO_NUM];
36
37 bool capture_enabled = false;
38
39 KernelParamsMetal launch_params = {0};
40
41 /* MetalRT members ----------------------------------*/
42 bool use_metalrt = false;
43 bool motion_blur = false;
44 id<MTLArgumentEncoder> mtlASArgEncoder =
45 nil; /* encoder used for fetching device pointers from MTLAccelerationStructure */
46
47 id<MTLArgumentEncoder> mtlBlasArgEncoder = nil;
48 id<MTLBuffer> blas_buffer = nil;
49
50 API_AVAILABLE(macos(11.0))
51 vector<id<MTLAccelerationStructure>> unique_blas_array;
52
53 API_AVAILABLE(macos(11.0))
54 id<MTLAccelerationStructure> accel_struct = nil;
55 /*---------------------------------------------------*/
56
57 uint kernel_features = 0;
58 bool using_nanovdb = false;
59 MTLResourceOptions default_storage_mode;
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 bool use_UMA = false; /* If true, UMA memory in shared_pointer is being used. */
74 };
75 typedef map<device_memory *, unique_ptr<MetalMem>> MetalMemMap;
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(int device_id);
99
100 static MetalDevice *get_device_by_ID(int device_idID,
101 thread_scoped_lock &existing_devices_mutex_lock);
102
103 virtual bool is_ready(string &status) const override;
104
105 virtual void cancel() override;
106
107 virtual 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 virtual ~MetalDevice();
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 virtual bool load_kernels(const uint kernel_features) override;
132
133 void load_texture_info();
134
135 void erase_allocation(device_memory &mem);
136
137 virtual bool should_use_graphics_interop() override;
138
139 virtual void *get_native_buffer(device_ptr ptr) override;
140
141 virtual unique_ptr<DeviceQueue> gpu_queue_create() override;
142
143 virtual void build_bvh(BVH *bvh, Progress &progress, bool refit) override;
144
145 virtual void optimize_for_scene(Scene *scene) override;
146
147 static void compile_and_load(int device_id, MetalPipelineType pso_type);
148
149 /* ------------------------------------------------------------------ */
150 /* low-level memory management */
151
152 bool max_working_set_exceeded(size_t safety_margin = 8 * 1024 * 1024) const;
153
154 MetalMem *generic_alloc(device_memory &mem);
155
156 void generic_copy_to(device_memory &mem);
157
158 void generic_free(device_memory &mem);
159
160 void mem_alloc(device_memory &mem) override;
161
162 void mem_copy_to(device_memory &mem) override;
163
164 void mem_copy_from(device_memory &mem)
165 {
166 mem_copy_from(mem, -1, -1, -1, -1);
167 }
168 void mem_copy_from(device_memory &mem, size_t y, size_t w, 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, size_t offset, size_t /*size*/) override;
175
176 virtual void const_copy_to(const char *name, void *host, size_t size) override;
177
178 void global_alloc(device_memory &mem);
179
180 void global_free(device_memory &mem);
181
182 void tex_alloc(device_texture &mem);
183
184 void tex_alloc_as_buffer(device_texture &mem);
185
186 void tex_free(device_texture &mem);
187
188 void flush_delayed_free_list();
189
190 void free_bvh();
191
192 void update_bvh(BVHMetal *bvh_metal);
193};
194
196
197#endif
unsigned int uint
struct Scene Scene
in reality light always falls off quadratically Particle Retrieve the data of the particle that spawned the object for example to give variation to multiple instances of an object Point Retrieve information about points in a point cloud Retrieve the edges of an object as it appears to Cycles topology will always appear triangulated Convert a blackbody temperature to an RGB value Normal Generate a perturbed normal from an RGB normal map image Typically used for faking highly detailed surfaces Generate an OSL shader from a file or text data block Image Sample an image file as a texture Gabor Generate Gabor noise Gradient Generate interpolated color and intensity values based on the input vector Magic Generate a psychedelic color texture Voronoi Generate Worley noise based on the distance to random points Typically used to generate textures such as or biological cells Brick Generate a procedural texture producing bricks Texture Retrieve multiple types of texture coordinates nTypically used as inputs for texture nodes Vector Convert a vector
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
CCL_NAMESPACE_BEGIN struct KernelParamsMetal KernelParamsMetal
ccl_device bool BVH_FUNCTION_FULL_NAME BVH(KernelGlobals kg, ccl_private const Ray *ray, ccl_private LocalIntersection *local_isect, int local_object, ccl_private uint *lcg_state, int max_hits)
Definition local.h:28
static void error(const char *str)
int BVHLayoutMask
Definition params.h:51
unsigned __int64 uint64_t
Definition stdint.h:90
std::unique_lock< std::mutex > thread_scoped_lock
Definition thread.h:30
CCL_NAMESPACE_BEGIN typedef std::mutex thread_mutex
Definition thread.h:29
struct TextureInfo TextureInfo
uint64_t device_ptr
Definition util/types.h:45
PointerRNA * ptr
Definition wm_files.cc:4126