mirror of
https://github.com/godotengine/godot.git
synced 2024-11-28 15:12:33 +00:00
Merge pull request #96089 from stuartcarnie/sgc/metal_tidy_up
Metal: Minor improvements to shader cache
This commit is contained in:
commit
850067d686
@ -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;
|
||||
@ -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<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) :
|
||||
|
@ -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<MTLLibrary> _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<MTLLibrary> _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<MTLDevice>)device
|
||||
@ -1447,10 +1446,6 @@ void ShaderCacheEntry::notify_free() const {
|
||||
}
|
||||
}
|
||||
|
||||
- (ShaderCacheEntry *)entry {
|
||||
return _entry;
|
||||
}
|
||||
|
||||
- (id<MTLLibrary>)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");
|
||||
|
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user