From 818afcb327f92a3599e8336fbcdcd52a04ba78f0 Mon Sep 17 00:00:00 2001 From: Stuart Carnie Date: Sun, 23 Feb 2025 05:59:41 +1100 Subject: [PATCH] Metal: Compile `MTLLibrary` on demand when pipeline is created This changes the default shader loading strategy, implemented in the Metal driver, to compile the `MTLLibrary` on demand when the pipeline is created, which reduces cold startup time on IPHONE target OSs. Normally, the `MTLLibrary` is compiled from Metal source asynchronously when Godot calls `RenderingDeviceDriverMetal::shader_create_from_bytecode`; however, this changes this behaviour on mobile platforms to do it on demand when the pipeline is created, as noted in #96052, Godot will ask to create many more shaders from bytecode than are initially required. Mobile OSs like iOS are limited to compiling to shader libraries concurrently, which results in a significant bottleneck. This is not the default for macOS, as it can concurrently compile many shaders at once, resulting in faster startup times for the Godot editor. --- drivers/metal/rendering_device_driver_metal.mm | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/drivers/metal/rendering_device_driver_metal.mm b/drivers/metal/rendering_device_driver_metal.mm index b6f606bfeb9..0a49dc1cb12 100644 --- a/drivers/metal/rendering_device_driver_metal.mm +++ b/drivers/metal/rendering_device_driver_metal.mm @@ -4082,9 +4082,14 @@ RenderingDeviceDriverMetal::RenderingDeviceDriverMetal(RenderingContextDriverMet context_driver(p_context_driver) { DEV_ASSERT(p_context_driver != nullptr); +#if TARGET_OS_OSX if (String res = OS::get_singleton()->get_environment("GODOT_MTL_SHADER_LOAD_STRATEGY"); res == U"lazy") { _shader_load_strategy = ShaderLoadStrategy::LAZY; } +#else + // Always use the lazy strategy on other OSs like iOS, tvOS, or visionOS. + _shader_load_strategy = ShaderLoadStrategy::LAZY; +#endif } RenderingDeviceDriverMetal::~RenderingDeviceDriverMetal() {