Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Metal: Minor improvements to shader cache #96089

Merged
merged 1 commit into from
Aug 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 13 additions & 13 deletions drivers/metal/metal_objects.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<MTLLibrary>)library;
- (NSError *)error;
- (void)setLabel:(NSString *)label;
Expand All @@ -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 <>
Expand All @@ -545,22 +549,18 @@ struct HashMapComparatorDefault<SHA256Digest> {
}
};

/**
* 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) :
Expand Down
21 changes: 7 additions & 14 deletions drivers/metal/metal_objects.mm
Original file line number Diff line number Diff line change
Expand Up @@ -1396,9 +1396,9 @@ fragment ClearColorsOut fragClear(VaryingsPos varyings [[stage_in]], constant Cl

@interface MDLibrary ()
- (instancetype)initWithCacheEntry:(ShaderCacheEntry *)entry;
- (ShaderCacheEntry *)entry;
@end

/// Loads the MTLLibrary when the library is first accessed.
@interface MDLazyLibrary : MDLibrary {
id<MTLLibrary> _library;
NSError *_error;
Expand All @@ -1414,6 +1414,7 @@ - (instancetype)initWithCacheEntry:(ShaderCacheEntry *)entry
options:(MTLCompileOptions *)options;
@end

/// Loads the MTLLibrary immediately on initialization, using an asynchronous API.
@interface MDImmediateLibrary : MDLibrary {
id<MTLLibrary> _library;
NSError *_error;
Expand All @@ -1428,9 +1429,7 @@ - (instancetype)initWithCacheEntry:(ShaderCacheEntry *)entry
options:(MTLCompileOptions *)options;
@end

@implementation MDLibrary {
ShaderCacheEntry *_entry;
}
@implementation MDLibrary

+ (instancetype)newLibraryWithCacheEntry:(ShaderCacheEntry *)entry
device:(id<MTLDevice>)device
Expand All @@ -1447,10 +1446,6 @@ + (instancetype)newLibraryWithCacheEntry:(ShaderCacheEntry *)entry
}
}

- (ShaderCacheEntry *)entry {
return _entry;
}

- (id<MTLLibrary>)library {
CRASH_NOW_MSG("Not implemented");
return nil;
Expand Down Expand Up @@ -1489,8 +1484,8 @@ - (instancetype)initWithCacheEntry:(ShaderCacheEntry *)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());

[device newLibraryWithSource:source
options:options
Expand Down Expand Up @@ -1556,12 +1551,10 @@ - (void)load {
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");
Expand Down
6 changes: 2 additions & 4 deletions drivers/metal/rendering_device_driver_metal.mm
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@
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");
}

/*****************/
Expand Down Expand Up @@ -2323,8 +2323,6 @@ void deserialize(BufReader &p_reader) {

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
Expand Down
Loading