Blender V5.3
mtl_backend.mm
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2022-2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include <cstring>
10
11#include "BLI_threads.hh"
12
13#include "BKE_global.hh"
14
15#include "gpu_backend.hh"
16#include "mtl_backend.hh"
17#include "mtl_batch.hh"
18#include "mtl_context.hh"
19#include "mtl_debug.hh"
20#include "mtl_framebuffer.hh"
21#include "mtl_immediate.hh"
22#include "mtl_index_buffer.hh"
23#include "mtl_query.hh"
24#include "mtl_ray_tracing.hh"
25#include "mtl_shader.hh"
26#include "mtl_storage_buffer.hh"
27#include "mtl_texture_pool.hh"
28#include "mtl_uniform_buffer.hh"
29#include "mtl_vertex_buffer.hh"
30#include "mtl_work_in_flight.hh"
31
34
35#include <Cocoa/Cocoa.h>
36#include <Metal/Metal.h>
37#include <QuartzCore/QuartzCore.h>
38#include <sys/sysctl.h>
39
40namespace blender::gpu {
41
42static CLG_LogRef LOG = {"gpu.metal"};
43
44/* Global per-thread AutoReleasePool. */
45thread_local NSAutoreleasePool *g_autoreleasepool = nil;
46thread_local int g_autoreleasepool_depth = 0;
47
48/* -------------------------------------------------------------------- */
51
53{
54 compiler_ = MEM_new<MTLShaderCompiler>(__func__);
55}
56
58{
59 MEM_delete(compiler_);
60}
61
63{
64 return new MTLContext(ghost_window, ghost_context);
65};
66
68{
69 return new MTLBatch();
70};
71
73{
74 return new MTLFence();
75};
76
78{
79 return new MTLWorkInFlight(max_in_flight);
80};
81
83{
84 return new MTLFrameBuffer(MTLContext::get(), name);
85};
86
88{
89 return new MTLIndexBuf();
90};
91
93{
94 return new MTLPixelBuffer(size);
95};
96
101
103{
104 return new MTLShader(MTLContext::get(), name);
105};
106
108{
109 return new gpu::MTLTexture(name);
110}
111
113{
114 /* #162556: Temporarily disabled MTLTexturePool as metal texture views
115 * do not support `update_sub`, while other backends do. */
116 // if (GCaps.texture_pool_workaround) {
117 return new TexturePoolImpl();
118 // }
119 // return new MTLTexturePool();
120}
121
122UniformBuf *MTLBackend::uniformbuf_alloc(size_t size, const char *name)
123{
124 return new MTLUniformBuf(size, name);
125};
126
127StorageBuf *MTLBackend::storagebuf_alloc(size_t size, GPUUsageType usage, const char *name)
128{
129 return new MTLStorageBuf(size, usage, name);
130}
131
133{
134 return new MTLVertBuf();
135}
136
138{
139 return new MTLTopLevelAS(name);
140}
141
143{
144 return new MTLBottomLevelAS(name);
145}
146
148{
149 /* All Rendering must occur within a render boundary */
150 /* Track a call-count for nested calls, used to ensure we are inside an
151 * autoreleasepool from all rendering path. */
153
154 if (g_autoreleasepool == nil) {
155 g_autoreleasepool = [[NSAutoreleasePool alloc] init];
156 }
159}
160
162{
163 /* If call-count reaches zero, drain auto release pool.
164 * Ensures temporary objects are freed within a frame's lifetime. */
168
169 if (g_autoreleasepool_depth == 0) {
170 [g_autoreleasepool drain];
171 g_autoreleasepool = nil;
172
173 /* Return the buffers freed by completed GPU work to the memory pools. Render steps do this
174 * too, but not all GPU work is followed by one, for example compositing animations. */
176 if (memory_manager != nullptr) {
177 if (memory_manager->has_completed_safe_lists()) {
178 memory_manager->update_memory_pools();
179 }
180 memory_manager->begin_new_safe_list(true);
181 }
182 }
183}
184
185void MTLBackend::render_step(bool force_resource_release)
186{
187 /* NOTE(Metal): Primarily called from main thread, but below data-structures
188 * and operations are thread-safe, and GPUContext rendering coordination
189 * is also thread-safe. */
190
191 /* Flush any MTLSafeFreeLists which have previously been released by any MTLContext. */
193
194 /* End existing MTLSafeFreeList and begin new list --
195 * Buffers wont `free` until all associated in-flight command buffers have completed.
196 * Decrement final reference count for ensuring the previous list is certainly
197 * released. */
199
200 if (force_resource_release && g_autoreleasepool) {
201 [g_autoreleasepool drain];
202 g_autoreleasepool = [[NSAutoreleasePool alloc] init];
203 }
204}
205
207{
208 return (g_autoreleasepool != nil);
209}
210
212
213/* -------------------------------------------------------------------- */
216
217/* For Metal, platform_init needs to be called after MTLContext initialization. */
218void MTLBackend::platform_init(MTLContext *ctx)
219{
220 if (GPG.initialized) {
221 return;
222 }
223
228
229 BLI_assert(ctx);
230 id<MTLDevice> mtl_device = ctx->device;
231 BLI_assert(device);
232
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";
237 if (G.debug & G_DEBUG_GPU) {
238 CLOG_INFO(&LOG, "METAL API - DETECTED GPU: %s", vendor);
239 }
240
241 /* macOS is the only supported platform, but check to ensure we are not building with Metal
242 * enablement on another platform. */
243 BLI_assert_msg(os == GPU_OS_MAC, "Platform must be macOS");
244
245 /* Determine Vendor from name. */
246 if (strstr(vendor, "ATI") || strstr(vendor, "AMD")) {
247 device = GPU_DEVICE_ATI;
248 driver = GPU_DRIVER_OFFICIAL;
249 }
250 else if (strstr(vendor, "NVIDIA")) {
251 device = GPU_DEVICE_NVIDIA;
252 driver = GPU_DRIVER_OFFICIAL;
253 }
254 else if (strstr(vendor, "Intel")) {
255 device = GPU_DEVICE_INTEL;
256 driver = GPU_DRIVER_OFFICIAL;
257 support_level = GPU_SUPPORT_LEVEL_LIMITED;
258 }
259 else if (strstr(vendor, "Apple") || strstr(vendor, "APPLE")) {
260 /* Apple Silicon. */
261 device = GPU_DEVICE_APPLE;
262 driver = GPU_DRIVER_OFFICIAL;
263 }
264 else if (strstr(renderer, "Apple Software Renderer")) {
265 device = GPU_DEVICE_SOFTWARE;
266 driver = GPU_DRIVER_SOFTWARE;
267 }
268 else if (strstr(renderer, "llvmpipe") || strstr(renderer, "softpipe")) {
269 device = GPU_DEVICE_SOFTWARE;
270 driver = GPU_DRIVER_SOFTWARE;
271 }
272 else {
273 CLOG_WARN(&LOG,
274 "Could not find a matching GPU name. Things may not behave as expected. Detected "
275 "configuration: Vendor: %s, Renderer: %s",
276 vendor,
277 renderer);
278 }
279
280 GPUArchitectureType architecture_type = (mtl_device.hasUnifiedMemory &&
281 device == GPU_DEVICE_APPLE) ?
284
285 GPG.init(device,
286 os,
287 driver,
288 support_level,
290 vendor,
291 renderer,
292 version,
293 architecture_type);
294
295 GPG.devices.append(
296 {.identifier = "METAL", .index = 0, .vendor_id = 0, .device_id = 0, .name = renderer});
297
298 /* UUID is not supported on Metal. */
300
301 /* LUID is registryID on Metal, or at least this is what libraries like OIDN expects. */
302 const uint64_t luid = mtl_device.registryID;
303 GPG.device_luid.reinitialize(sizeof(luid));
304 std::memcpy(GPG.device_luid.data(), &luid, sizeof(luid));
305
306 /* Metal only has one device per LUID, so only the first bit will always be active.. */
308}
309
310void MTLBackend::platform_exit()
311{
313 GPG.clear();
314}
315
317
318/* -------------------------------------------------------------------- */
322
323static const char *mtl_extensions_get_null(int /*i*/)
324{
325 return nullptr;
326}
327
329{
330 NSString *gpu_name = [device name];
331 BLI_assert([gpu_name length]);
332 const char *vendor = [gpu_name UTF8String];
333
334 /* Verify GPU support. */
335 bool supported_gpu = [device supportsFamily:MTLGPUFamilyMac2];
336 bool should_support_barycentrics = false;
337
338 /* Known good configs. */
339 if (strstr(vendor, "AMD") || strstr(vendor, "Apple") || strstr(vendor, "APPLE")) {
340 should_support_barycentrics = true;
341 }
342
343 /* Explicit support for Intel-based platforms. */
344 if ((strstr(vendor, "Intel") || strstr(vendor, "INTEL"))) {
345 should_support_barycentrics = true;
346 }
347 return supported_gpu && should_support_barycentrics;
348}
349
351{
352 NSString *gpu_name = [device name];
353 BLI_assert([gpu_name length]);
354
355 const char *vendor = [gpu_name UTF8String];
356
357 /* Known good configs. */
358 return (strstr(vendor, "Apple") || strstr(vendor, "APPLE"));
359}
360
362{
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;
367
368 if (is_apple_sillicon(device)) {
369 /* On Apple Silicon query the number of performance cores */
370 if (sysctlbyname(
371 "hw.perflevel0.logicalcpu", &sysctl_buffer, &sysctl_buffer_length, nullptr, 0) == 0)
372 {
373 num_performance_cores = sysctl_buffer[0];
374 }
375 }
376 else {
377 /* On Intel just return the logical core count */
378 if (sysctlbyname("hw.logicalcpu", &sysctl_buffer, &sysctl_buffer_length, nullptr, 0) == 0) {
379 num_performance_cores = sysctl_buffer[0];
380 }
381 }
382 BLI_assert(num_performance_cores != -1);
383 return num_performance_cores;
384}
385
387{
388 if (is_apple_sillicon(device)) {
389 /* On Apple Silicon query the number of efficiency 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;
394 if (sysctlbyname(
395 "hw.perflevel1.logicalcpu", &sysctl_buffer, &sysctl_buffer_length, nullptr, 0) == 0)
396 {
397 num_efficiency_cores = sysctl_buffer[0];
398 }
399
400 BLI_assert(num_efficiency_cores != -1);
401 return num_efficiency_cores;
402 }
403 return 0;
404}
405
407{
408 /* Device compatibility information using Metal Feature-set tables.
409 * See: https://developer.apple.com/metal/Metal-Feature-Set-Tables.pdf */
410 id<MTLDevice> device = MTLCreateSystemDefaultDevice();
411
412 /* #163272: MTLCreateSystemDefaultDevice() may return nil in sandboxed or non-GUI contexts. */
413 if (device == nil) {
414 CLOG_WARN(&LOG, "Could not initialize Metal: no default Metal device available.");
415 return false;
416 }
417
418 /* Debug: Enable low power GPU with Environment Var: METAL_FORCE_INTEL. */
419 static const char *forceIntelStr = getenv("METAL_FORCE_INTEL");
420 bool forceIntel = forceIntelStr ? (atoi(forceIntelStr) != 0) : false;
421
422 if (forceIntel) {
423 NSArray<id<MTLDevice>> *allDevices = MTLCopyAllDevices();
424 for (id<MTLDevice> _device in allDevices) {
425 if (_device.lowPower) {
426 device = _device;
427 }
428 }
429 }
430
431 /* Metal Viewport requires argument buffer tier-2 support and Barycentric Coordinates.
432 * These are available on most hardware configurations supporting Metal 2.2. */
433 bool supports_argument_buffers_tier2 = ([device argumentBuffersSupport] ==
434 MTLArgumentBuffersTier2);
435 bool supports_barycentrics = [device supportsShaderBarycentricCoordinates] ||
437 bool supported_metal_version = [device supportsFamily:MTLGPUFamilyMac2];
438
439 bool result = supports_argument_buffers_tier2 && supports_barycentrics &&
440 supported_metal_version;
441
442 if (!supports_argument_buffers_tier2) {
443 CLOG_DEBUG(&LOG, "Device does not support argument buffers tier 2");
444 }
445 if (!supports_barycentrics) {
446 CLOG_DEBUG(&LOG, "Device does not support barycentrics coordinates");
447 }
448 if (!supported_metal_version) {
449 CLOG_DEBUG(&LOG, "Device does not support metal 2.2 or higher");
450 }
451
452 if (G.debug & G_DEBUG_GPU && result) {
453 CLOG_INFO(&LOG,
454 "Device with name %s supports metal minimum requirements",
455 [[device name] UTF8String]);
456 }
457
458 return result;
459}
460
461void MTLBackend::capabilities_init(MTLContext *ctx)
462{
463 BLI_assert(ctx);
464 id<MTLDevice> device = ctx->device;
465 BLI_assert(device);
466
467 /* Initialize Capabilities. */
468 MTLBackend::capabilities.supports_argument_buffers_tier2 = ([device argumentBuffersSupport] ==
469 MTLArgumentBuffersTier2);
470 MTLBackend::capabilities.supports_family_mac1 = [device supportsFamily:MTLGPUFamilyMac1];
471 MTLBackend::capabilities.supports_family_mac2 = [device supportsFamily:MTLGPUFamilyMac2];
472 MTLBackend::capabilities.supports_family_mac_catalyst1 = [device
473 supportsFamily:MTLGPUFamilyMacCatalyst1];
474 MTLBackend::capabilities.supports_family_mac_catalyst2 = [device
475 supportsFamily:MTLGPUFamilyMacCatalyst2];
476 /* NOTE(Metal): Texture gather is supported on AMD, but results are non consistent
477 * with Apple Silicon GPUs. Disabling for now to avoid erroneous rendering. */
478 MTLBackend::capabilities.supports_texture_gather = [device hasUnifiedMemory];
479
480 /* GPU Type. */
481 const char *gpu_name = [device.name UTF8String];
482 if (strstr(gpu_name, "M1")) {
484 }
485 else if (strstr(gpu_name, "M2")) {
487 }
488 else if (strstr(gpu_name, "M3")) {
490 }
491 else {
493 }
494
495 /* Texture atomics supported in Metal 3.1. */
497#if defined(MAC_OS_VERSION_14_0)
498 if (@available(macOS 14.0, *)) {
500 }
501#endif
502
503 /* Ray queries require macOS 13. */
504 MTLBackend::capabilities.supports_ray_tracing = [device supportsRaytracing];
506
507 /* Vertex pipeline stores and atomics support. */
509
511 const bool is_tile_based_arch = (GPU_platform_architecture() == GPU_ARCHITECTURE_TBDR);
512 if (is_tile_based_arch) {
514 }
515 else {
516 /* NOTE: If emulating tile input reads, we must ensure we also expose position data. */
518 }
519
520 /* CPU Info */
523
524 /* Common Global Capabilities. */
525 GCaps.max_texture_size = ([device supportsFamily:MTLGPUFamilyApple3] ||
527 16384 :
528 8192;
533 128 :
534 (([device supportsFamily:MTLGPUFamilyApple4]) ? 96 : 31);
535 /* Hardcoded limit due to ShaderInterface::enabled_tex_mask_. */
536 GCaps.max_textures = std::min(GCaps.max_textures, 64);
537
539
540 /* Conservative uniform data limit is 4KB per-stage -- This is the limit of setBytes.
541 * MTLBuffer path is also supported but not as efficient. */
544
545 GCaps.max_batch_indices = 1 << 31;
546 GCaps.max_batch_vertices = 1 << 31;
549
550 /* Feature support */
551 GCaps.mem_stats_support = false;
554
556
557 /* Apple GPUs can write to an sRGB texture as a storage image directly, with the hardware doing
558 * the sRGB conversion. Use this for sRGB mipmap generation instead of a temporary non-sRGB
559 * texture. */
561
562 /* Compile shaders on performance cores but leave one free so UI is still responsive.
563 * Also respect command line option to reduce number of threads. */
565 MTLBackend::capabilities.num_performance_cores - 1);
566
567 /* Maximum buffer bindings: 31. Consider required slot for uniforms/UBOs/Vertex attributes.
568 * Can use argument buffers if a higher limit is required. */
571 GCaps.max_storage_buffer_size = size_t(ctx->device.maxBufferLength);
572 GCaps.max_uniform_buffer_size = size_t(ctx->device.maxBufferLength);
573 GCaps.storage_buffer_alignment = 256; /* TODO(fclem): But also unused. */
574
575 GCaps.max_work_group_count[0] = 65535;
576 GCaps.max_work_group_count[1] = 65535;
577 GCaps.max_work_group_count[2] = 65535;
578 /* In Metal, total_thread_count is 512 or 1024, such that
579 * threadgroup `width*height*depth <= total_thread_count` */
580 uint max_threads_per_threadgroup_per_dim = ([device supportsFamily:MTLGPUFamilyApple4] ||
582 1024 :
583 512;
584 GCaps.max_work_group_size[0] = max_threads_per_threadgroup_per_dim;
585 GCaps.max_work_group_size[1] = max_threads_per_threadgroup_per_dim;
586 GCaps.max_work_group_size[2] = max_threads_per_threadgroup_per_dim;
587
589
590 /* OPENGL Related workarounds -- none needed for Metal. */
595
596 /* Metal related workarounds. */
597 /* Minimum per-vertex stride is 4 bytes in Metal.
598 * A bound vertex buffer must contribute at least 4 bytes per vertex. */
600
601 /* Force workarounds when starting blender with `--debug-gpu-force-workarounds`.
602 *
603 * Not all workarounds are listed here as some capabilities are currently assumed to be present
604 * on all devices. */
606 /* Texture gather is supported on AMD, but results are non consistent with Apple Silicon GPUs
607 * and can be disabled. */
612 }
613
616 }
617}
618
620
621/* -------------------------------------------------------------------- */
624
625void MTLBackend::compute_dispatch(int groups_x_len, int groups_y_len, int groups_z_len)
626{
627 /* Fetch Context.
628 * With Metal, workload submission and resource management occurs within the context.
629 * Call compute dispatch on valid context. */
631 BLI_assert(ctx != nullptr);
632 if (ctx) {
633 ctx->compute_dispatch(groups_x_len, groups_y_len, groups_z_len);
634 }
635}
636
638{
639 /* Fetch Context.
640 * With Metal, workload submission and resource management occurs within the context.
641 * Call compute dispatch on valid context. */
643 BLI_assert(ctx != nullptr);
644 if (ctx) {
645 ctx->compute_dispatch_indirect(indirect_buf);
646 }
647}
648
650
651} // namespace blender::gpu
#define CLOG_DEBUG(clg_ref,...)
Definition CLG_log.h:191
#define CLOG_WARN(clg_ref,...)
Definition CLG_log.h:189
#define CLOG_INFO(clg_ref,...)
Definition CLG_log.h:190
#define BLI_assert(a)
#define BLI_assert_msg(a, msg)
const T * data() const
Definition BLI_array.hh:323
void reinitialize(const int64_t new_size)
Definition BLI_array.hh:430
ShaderCompiler * compiler_
void init(GPUDeviceType gpu_device, GPUOSType os_type, GPUDriverType driver_type, GPUSupportLevel gpu_support_level, GPUBackendType backend, const char *vendor_str, const char *renderer_str, const char *version_str, GPUArchitectureType arch_type)
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
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
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)
MTLShader(MTLContext *ctx, const char *name)
Definition mtl_shader.mm:91
MTLStorageBuf(size_t size, GPUUsageType usage, const char *name)
MTLUniformBuf(size_t size, const char *name)
MTLWorkInFlight(unsigned int max_in_flight)
float length(VecOp< float, D >) RET
#define UINT_MAX
Definition hash_md5.cc:46
GPUPlatformGlobal GPG
static const char * mtl_extensions_get_null(int)
static int get_num_performance_cpu_cores(id< MTLDevice > device)
NSAutoreleasePool * g_autoreleasepool
static CLG_LogRef LOG
bool supports_barycentric_whitelist(id< MTLDevice > device)
static int get_num_efficiency_cpu_cores(id< MTLDevice > device)
GPUCapabilities GCaps
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
Definition BKE_text.h:91
unsigned int uint
Definition BKE_text.h:55
@ GPU_DRIVER_OFFICIAL
@ GPU_DRIVER_SOFTWARE
@ G_DEBUG_GPU_NO_TEXTURE_POOL
@ G_DEBUG_GPU_FORCE_WORKAROUNDS
@ GPU_SUPPORT_LEVEL_LIMITED
@ GPU_SUPPORT_LEVEL_SUPPORTED
Global G
Definition blender.cc:57
int BLI_system_thread_count()
Definition threads.cc:258
@ GPU_ARCHITECTURE_TBDR
@ GPU_ARCHITECTURE_IMR
GPUArchitectureType GPU_platform_architecture()
static CLG_LogRef LOG
Definition logging.cpp:12
@ GPU_DEVICE_SOFTWARE
@ GPU_DEVICE_UNKNOWN
@ GPU_DEVICE_INTEL
@ GPU_DEVICE_NVIDIA
@ GPU_DEVICE_APPLE