35#include <Cocoa/Cocoa.h>
36#include <Metal/Metal.h>
37#include <QuartzCore/QuartzCore.h>
38#include <sys/sysctl.h>
54 compiler_ = MEM_new<MTLShaderCompiler>(__func__);
64 return new MTLContext(ghost_window, ghost_context);
176 if (memory_manager !=
nullptr) {
218void MTLBackend::platform_init(
MTLContext *ctx)
233 NSString *gpu_name = [mtl_device name];
234 const char *vendor = [gpu_name UTF8String];
235 const char *renderer =
"Metal API";
236 const char *version =
"1.2";
238 CLOG_INFO(&
LOG,
"METAL API - DETECTED GPU: %s", vendor);
246 if (strstr(vendor,
"ATI") || strstr(vendor,
"AMD")) {
250 else if (strstr(vendor,
"NVIDIA")) {
254 else if (strstr(vendor,
"Intel")) {
259 else if (strstr(vendor,
"Apple") || strstr(vendor,
"APPLE")) {
264 else if (strstr(renderer,
"Apple Software Renderer")) {
268 else if (strstr(renderer,
"llvmpipe") || strstr(renderer,
"softpipe")) {
274 "Could not find a matching GPU name. Things may not behave as expected. Detected "
275 "configuration: Vendor: %s, Renderer: %s",
296 {.identifier =
"METAL", .index = 0, .vendor_id = 0, .device_id = 0, .name = renderer});
302 const uint64_t luid = mtl_device.registryID;
310void MTLBackend::platform_exit()
330 NSString *gpu_name = [device name];
332 const char *vendor = [gpu_name UTF8String];
335 bool supported_gpu = [device supportsFamily:MTLGPUFamilyMac2];
336 bool should_support_barycentrics =
false;
339 if (strstr(vendor,
"AMD") || strstr(vendor,
"Apple") || strstr(vendor,
"APPLE")) {
340 should_support_barycentrics =
true;
344 if ((strstr(vendor,
"Intel") || strstr(vendor,
"INTEL"))) {
345 should_support_barycentrics =
true;
347 return supported_gpu && should_support_barycentrics;
352 NSString *gpu_name = [device name];
355 const char *vendor = [gpu_name UTF8String];
358 return (strstr(vendor,
"Apple") || strstr(vendor,
"APPLE"));
363 const int SYSCTL_BUF_LENGTH = 16;
364 int num_performance_cores = -1;
365 unsigned char sysctl_buffer[SYSCTL_BUF_LENGTH];
366 size_t sysctl_buffer_length = SYSCTL_BUF_LENGTH;
371 "hw.perflevel0.logicalcpu", &sysctl_buffer, &sysctl_buffer_length,
nullptr, 0) == 0)
373 num_performance_cores = sysctl_buffer[0];
378 if (sysctlbyname(
"hw.logicalcpu", &sysctl_buffer, &sysctl_buffer_length,
nullptr, 0) == 0) {
379 num_performance_cores = sysctl_buffer[0];
383 return num_performance_cores;
390 const int SYSCTL_BUF_LENGTH = 16;
391 int num_efficiency_cores = -1;
392 unsigned char sysctl_buffer[SYSCTL_BUF_LENGTH];
393 size_t sysctl_buffer_length = SYSCTL_BUF_LENGTH;
395 "hw.perflevel1.logicalcpu", &sysctl_buffer, &sysctl_buffer_length,
nullptr, 0) == 0)
397 num_efficiency_cores = sysctl_buffer[0];
401 return num_efficiency_cores;
414 CLOG_WARN(&
LOG,
"Could not initialize Metal: no default Metal device available.");
419 static const char *forceIntelStr = getenv(
"METAL_FORCE_INTEL");
420 bool forceIntel = forceIntelStr ? (atoi(forceIntelStr) != 0) :
false;
423 NSArray<id<MTLDevice>> *allDevices = MTLCopyAllDevices();
425 if (_device.lowPower) {
433 bool supports_argument_buffers_tier2 = ([device argumentBuffersSupport] ==
434 MTLArgumentBuffersTier2);
435 bool supports_barycentrics = [device supportsShaderBarycentricCoordinates] ||
437 bool supported_metal_version = [device supportsFamily:MTLGPUFamilyMac2];
439 bool result = supports_argument_buffers_tier2 && supports_barycentrics &&
440 supported_metal_version;
442 if (!supports_argument_buffers_tier2) {
443 CLOG_DEBUG(&
LOG,
"Device does not support argument buffers tier 2");
445 if (!supports_barycentrics) {
446 CLOG_DEBUG(&
LOG,
"Device does not support barycentrics coordinates");
448 if (!supported_metal_version) {
449 CLOG_DEBUG(&
LOG,
"Device does not support metal 2.2 or higher");
454 "Device with name %s supports metal minimum requirements",
455 [[device name] UTF8String]);
461void MTLBackend::capabilities_init(
MTLContext *ctx)
469 MTLArgumentBuffersTier2);
473 supportsFamily:MTLGPUFamilyMacCatalyst1];
475 supportsFamily:MTLGPUFamilyMacCatalyst2];
481 const char *gpu_name = [device.name UTF8String];
482 if (strstr(gpu_name,
"M1")) {
485 else if (strstr(gpu_name,
"M2")) {
488 else if (strstr(gpu_name,
"M3")) {
497#if defined(MAC_OS_VERSION_14_0)
498 if (@available(macOS 14.0, *)) {
512 if (is_tile_based_arch) {
534 (([device supportsFamily:MTLGPUFamilyApple4]) ? 96 : 31);
580 uint max_threads_per_threadgroup_per_dim = ([device supportsFamily:MTLGPUFamilyApple4] ||
#define CLOG_DEBUG(clg_ref,...)
#define CLOG_WARN(clg_ref,...)
#define CLOG_INFO(clg_ref,...)
#define BLI_assert_msg(a, msg)
void reinitialize(const int64_t new_size)
ShaderCompiler * compiler_
TopLevelAS * tlas_alloc(const char *name) override
void render_begin() override
TexturePool * texturepool_alloc() override
QueryPool * querypool_alloc() override
void compute_dispatch_indirect(StorageBuf *indirect_buf) override
bool is_inside_render_boundary()
PixelBuffer * pixelbuf_alloc(size_t size) override
FrameBuffer * framebuffer_alloc(const char *name) override
static bool metal_is_supported()
IndexBuf * indexbuf_alloc() override
StorageBuf * storagebuf_alloc(size_t size, GPUUsageType usage, const char *name) override
void render_end() override
WorkInFlight * work_in_flight_alloc(unsigned int max_in_flight) override
Context * context_alloc(GHOST_IWindow *ghost_window, GHOST_IContext *ghost_context) override
Batch * batch_alloc() override
Fence * fence_alloc() override
void init_resources() override
BottomLevelAS * blas_alloc(const char *name) override
void compute_dispatch(int groups_x_len, int groups_y_len, int groups_z_len) override
static MTLCapabilities capabilities
UniformBuf * uniformbuf_alloc(size_t size, const char *name) override
VertBuf * vertbuf_alloc() override
void delete_resources() override
Texture * texture_alloc(const char *name) override
Shader * shader_alloc(const char *name) override
void render_step(bool force_resource_release=false) override
MTLBottomLevelAS(const char *name)
bool has_completed_safe_lists()
void update_memory_pools()
void begin_new_safe_list(bool only_if_should_flush=false)
static MTLContext * get()
static MTLBufferPool * global_memory_manager
void compute_dispatch_indirect(StorageBuf *indirect_buf)
void compute_dispatch(int groups_x_len, int groups_y_len, int groups_z_len)
static MTLBufferPool * get_global_memory_manager()
MTLContext(GHOST_IWindow *ghost_window, GHOST_IContext *ghost_context)
MTLFrameBuffer(MTLContext *ctx, const char *name)
MTLPixelBuffer(size_t size)
MTLShader(MTLContext *ctx, const char *name)
MTLStorageBuf(size_t size, GPUUsageType usage, const char *name)
MTLTopLevelAS(const char *name)
MTLWorkInFlight(unsigned int max_in_flight)
float length(VecOp< float, D >) RET
static const char * mtl_extensions_get_null(int)
static int get_num_performance_cpu_cores(id< MTLDevice > device)
NSAutoreleasePool * g_autoreleasepool
bool supports_barycentric_whitelist(id< MTLDevice > device)
static int get_num_efficiency_cpu_cores(id< MTLDevice > device)
int g_autoreleasepool_depth
bool is_apple_sillicon(id< MTLDevice > device)
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
unsigned __int64 uint64_t
@ G_DEBUG_GPU_NO_TEXTURE_POOL
@ G_DEBUG_GPU_FORCE_WORKAROUNDS
@ GPU_SUPPORT_LEVEL_LIMITED
@ GPU_SUPPORT_LEVEL_SUPPORTED
int BLI_system_thread_count()
GPUArchitectureType GPU_platform_architecture()
int max_shader_storage_buffer_bindings
bool multi_viewport_support
int max_parallel_compilations
bool geometry_shader_support
bool texture_pool_workaround
int max_work_group_size[3]
size_t max_uniform_buffer_size
bool use_main_context_workaround
const char *(*) extension_get(int)
bool depth_blitting_workaround
bool hdr_viewport_support
int minimum_per_vertex_stride
uint32_t max_buffer_texture_size
int max_work_group_count[3]
bool srgb_write_direct_support
int max_compute_shader_storage_blocks
size_t storage_buffer_alignment
bool vertex_pipeline_stores_and_atomics_support
bool stencil_export_support
size_t max_storage_buffer_size
bool supports_native_tile_inputs
int num_performance_cores
bool supports_ray_tracing
bool supports_texture_atomics
bool supports_texture_gather
bool supports_family_mac1