From 6de70e6b8a30c3f8622b3faba4012c1aa5563457 Mon Sep 17 00:00:00 2001 From: Stuart Carnie Date: Mon, 26 Aug 2024 08:29:19 +1000 Subject: [PATCH] Metal: Minor improvements to #96052; update logger subsystem name --- drivers/metal/metal_objects.h | 26 +++++++++---------- drivers/metal/metal_objects.mm | 21 +++++---------- .../metal/rendering_device_driver_metal.mm | 6 ++--- 3 files changed, 22 insertions(+), 31 deletions(-) diff --git a/drivers/metal/metal_objects.h b/drivers/metal/metal_objects.h index 11b96f8373f..f0a3e85f88b 100644 --- a/drivers/metal/metal_objects.h +++ b/drivers/metal/metal_objects.h @@ -506,10 +506,10 @@ enum class ShaderLoadStrategy { LAZY, }; -/** - * A Metal shader library. - */ -@interface MDLibrary : NSObject +/// A Metal shader library. +@interface MDLibrary : NSObject { + ShaderCacheEntry *_entry; +}; - (id)library; - (NSError *)error; - (void)setLabel:(NSString *)label; @@ -536,6 +536,10 @@ struct SHA256Digest { SHA256Digest(const char *p_data, size_t p_length) { CC_SHA256(p_data, (CC_LONG)p_length, data); } + + _FORCE_INLINE_ uint32_t short_sha() const { + return __builtin_bswap32(*(uint32_t *)&data[0]); + } }; template <> @@ -545,22 +549,18 @@ struct HashMapComparatorDefault { } }; -/** - * A cache entry for a Metal shader library. - */ +/// A cache entry for a Metal shader library. struct ShaderCacheEntry { RenderingDeviceDriverMetal &owner; + /// A hash of the Metal shader source code. SHA256Digest key; CharString name; - CharString short_sha; RD::ShaderStage stage = RD::SHADER_STAGE_VERTEX; - /** - * This reference must be weak, to ensure that when the last strong reference to the library - * is released, the cache entry is freed. - */ + /// This reference must be weak, to ensure that when the last strong reference to the library + /// is released, the cache entry is freed. MDLibrary *__weak library = nil; - /** Notify the cache that this entry is no longer needed. */ + /// Notify the cache that this entry is no longer needed. void notify_free() const; ShaderCacheEntry(RenderingDeviceDriverMetal &p_owner, SHA256Digest p_key) : diff --git a/drivers/metal/metal_objects.mm b/drivers/metal/metal_objects.mm index 5c7036f11ed..c66b683e236 100644 --- a/drivers/metal/metal_objects.mm +++ b/drivers/metal/metal_objects.mm @@ -1396,9 +1396,9 @@ void ShaderCacheEntry::notify_free() const { @interface MDLibrary () - (instancetype)initWithCacheEntry:(ShaderCacheEntry *)entry; -- (ShaderCacheEntry *)entry; @end +/// Loads the MTLLibrary when the library is first accessed. @interface MDLazyLibrary : MDLibrary { id _library; NSError *_error; @@ -1414,6 +1414,7 @@ void ShaderCacheEntry::notify_free() const { options:(MTLCompileOptions *)options; @end +/// Loads the MTLLibrary immediately on initialization, using an asynchronous API. @interface MDImmediateLibrary : MDLibrary { id _library; NSError *_error; @@ -1428,9 +1429,7 @@ void ShaderCacheEntry::notify_free() const { options:(MTLCompileOptions *)options; @end -@implementation MDLibrary { - ShaderCacheEntry *_entry; -} +@implementation MDLibrary + (instancetype)newLibraryWithCacheEntry:(ShaderCacheEntry *)entry device:(id)device @@ -1447,10 +1446,6 @@ void ShaderCacheEntry::notify_free() const { } } -- (ShaderCacheEntry *)entry { - return _entry; -} - - (id)library { CRASH_NOW_MSG("Not implemented"); return nil; @@ -1489,8 +1484,8 @@ void ShaderCacheEntry::notify_free() const { __block os_signpost_id_t compile_id = (os_signpost_id_t)(uintptr_t)self; os_signpost_interval_begin(LOG_INTERVALS, compile_id, "shader_compile", - "shader_name=%{public}s stage=%{public}s hash=%{public}s", - entry->name.get_data(), SHADER_STAGE_NAMES[entry->stage], entry->short_sha.get_data()); + "shader_name=%{public}s stage=%{public}s hash=%X", + entry->name.get_data(), SHADER_STAGE_NAMES[entry->stage], entry->key.short_sha()); [device newLibraryWithSource:source options:options @@ -1556,12 +1551,10 @@ void ShaderCacheEntry::notify_free() const { return; } - ShaderCacheEntry *entry = [self entry]; - __block os_signpost_id_t compile_id = (os_signpost_id_t)(uintptr_t)self; os_signpost_interval_begin(LOG_INTERVALS, compile_id, "shader_compile", - "shader_name=%{public}s stage=%{public}s hash=%{public}s", - entry->name.get_data(), SHADER_STAGE_NAMES[entry->stage], entry->short_sha.get_data()); + "shader_name=%{public}s stage=%{public}s hash=%X", + _entry->name.get_data(), SHADER_STAGE_NAMES[_entry->stage], _entry->key.short_sha()); NSError *error; _library = [_device newLibraryWithSource:_source options:_options error:&error]; os_signpost_interval_end(LOG_INTERVALS, compile_id, "shader_compile"); diff --git a/drivers/metal/rendering_device_driver_metal.mm b/drivers/metal/rendering_device_driver_metal.mm index ec42472cb6e..0d88a14fbc4 100644 --- a/drivers/metal/rendering_device_driver_metal.mm +++ b/drivers/metal/rendering_device_driver_metal.mm @@ -72,8 +72,8 @@ os_log_t LOG_DRIVER; os_log_t LOG_INTERVALS; __attribute__((constructor)) static void InitializeLogging(void) { - LOG_DRIVER = os_log_create("org.stuartcarnie.godot.metal", OS_LOG_CATEGORY_POINTS_OF_INTEREST); - LOG_INTERVALS = os_log_create("org.stuartcarnie.godot.metal", "events"); + LOG_DRIVER = os_log_create("org.godotengine.godot.metal", OS_LOG_CATEGORY_POINTS_OF_INTEREST); + LOG_INTERVALS = os_log_create("org.godotengine.godot.metal", "events"); } /*****************/ @@ -2323,8 +2323,6 @@ RDD::ShaderID RenderingDeviceDriverMetal::shader_create_from_bytecode(const Vect ShaderCacheEntry *cd = memnew(ShaderCacheEntry(*this, key)); cd->name = binary_data.shader_name; - String sha_hex = String::hex_encode_buffer(key.data, CC_SHA256_DIGEST_LENGTH); - cd->short_sha = sha_hex.substr(0, 8).utf8(); cd->stage = shader_data.stage; MDLibrary *library = [MDLibrary newLibraryWithCacheEntry:cd