11# include "device/metal/bvh.h"
17# include <Metal/Metal.h>
23class MetalDevice :
public Device {
25 id<MTLDevice> mtlDevice = nil;
26 id<MTLLibrary> mtlLibrary[PSO_NUM] = {nil};
27 id<MTLArgumentEncoder> mtlBufferKernelParamsEncoder =
29 id<MTLCommandQueue> mtlComputeCommandQueue = nil;
30 id<MTLCommandQueue> mtlGeneralCommandQueue = nil;
31 id<MTLArgumentEncoder> mtlAncillaryArgEncoder =
33 string source[PSO_NUM];
34 string kernels_md5[PSO_NUM];
35 string global_defines_md5[PSO_NUM];
37 bool capture_enabled =
false;
42 bool use_metalrt =
false;
43 bool motion_blur =
false;
44 id<MTLArgumentEncoder> mtlASArgEncoder =
47 id<MTLArgumentEncoder> mtlBlasArgEncoder = nil;
48 id<MTLBuffer> blas_buffer = nil;
50 API_AVAILABLE(macos(11.0))
51 vector<
id<MTLAccelerationStructure>> unique_blas_array;
53 API_AVAILABLE(macos(11.0))
54 id<MTLAccelerationStructure> accel_struct = nil;
57 uint kernel_features = 0;
58 bool using_nanovdb =
false;
59 MTLResourceOptions default_storage_mode;
60 int max_threads_per_threadgroup;
63 bool has_error =
false;
66 device_memory *mem =
nullptr;
67 int pointer_index = -1;
68 id<MTLBuffer> mtlBuffer = nil;
69 id<MTLTexture> mtlTexture = nil;
72 void *hostPtr =
nullptr;
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;
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;
91 MetalPipelineType kernel_specialization_level = PSO_GENERIC;
96 static std::map<int, MetalDevice *> active_device_ids;
98 static bool is_device_cancelled(
int device_id);
100 static MetalDevice *get_device_by_ID(
int device_idID,
103 virtual bool is_ready(
string &status)
const override;
105 virtual void cancel()
override;
109 void set_error(
const string &
error)
override;
111 MetalDevice(
const DeviceInfo &info, Stats &stats, Profiler &profiler,
bool headless);
113 virtual ~MetalDevice();
115 bool support_device(
const uint );
117 bool check_peer_access(Device *peer_device)
override;
119 bool use_adaptive_compilation();
121 bool use_local_atomic_sort()
const;
123 string preprocess_source(MetalPipelineType pso_type,
124 const uint kernel_features,
125 string *source =
nullptr);
127 void refresh_source_and_kernels_md5(MetalPipelineType pso_type);
129 void make_source(MetalPipelineType pso_type,
const uint kernel_features);
131 virtual bool load_kernels(
const uint kernel_features)
override;
133 void load_texture_info();
135 void erase_allocation(device_memory &mem);
137 virtual bool should_use_graphics_interop()
override;
141 virtual unique_ptr<DeviceQueue> gpu_queue_create()
override;
143 virtual void build_bvh(
BVH *bvh, Progress &progress,
bool refit)
override;
145 virtual void optimize_for_scene(
Scene *scene)
override;
147 static void compile_and_load(
int device_id, MetalPipelineType pso_type);
152 bool max_working_set_exceeded(
size_t safety_margin = 8 * 1024 * 1024)
const;
154 MetalMem *generic_alloc(device_memory &mem);
156 void generic_copy_to(device_memory &mem);
158 void generic_free(device_memory &mem);
160 void mem_alloc(device_memory &mem)
override;
162 void mem_copy_to(device_memory &mem)
override;
164 void mem_copy_from(device_memory &mem)
166 mem_copy_from(mem, -1, -1, -1, -1);
168 void mem_copy_from(device_memory &mem,
size_t y,
size_t w,
size_t h,
size_t elem)
override;
170 void mem_zero(device_memory &mem)
override;
172 void mem_free(device_memory &mem)
override;
174 device_ptr mem_alloc_sub_ptr(device_memory &mem,
size_t offset,
size_t )
override;
176 virtual void const_copy_to(
const char *name,
void *host,
size_t size)
override;
178 void global_alloc(device_memory &mem);
180 void global_free(device_memory &mem);
182 void tex_alloc(device_texture &mem);
184 void tex_alloc_as_buffer(device_texture &mem);
186 void tex_free(device_texture &mem);
188 void flush_delayed_free_list();
192 void update_bvh(BVHMetal *bvh_metal);
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)
void refit(btStridingMeshInterface *triangles, const btVector3 &aabbMin, const btVector3 &aabbMax)
SIMD_FORCE_INLINE const btScalar & w() const
Return the w value.
#define CCL_NAMESPACE_END
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)
static void error(const char *str)
unsigned __int64 uint64_t
std::unique_lock< std::mutex > thread_scoped_lock
CCL_NAMESPACE_BEGIN typedef std::mutex thread_mutex
struct TextureInfo TextureInfo