Free fork of NotProton: Windows Steam games in Mac Steam, on CodeWeavers' Wine and Apple's Game Porting Toolkit
Swift apple-silicond3dmetaldxmtdxvkgame-porting-toolkitgptkmac-gamingmacosmetalfxrosetta-2steamsteam-playwinewinecx
Fix overlay pipeline ownership
NotProtonNotf5a871a329bb
diff --git a/Makefile b/Makefileindex 4b9259b..5d74238 100644--- a/Makefile+++ b/Makefile@@ -423,10 +423,10 @@ OVERLAY_SHIM := $(OUT_DIR)/overlay-shim.dylibOVERLAY_SHIM_FLAGS := -mmacosx-version-min=$(MIN_VER) $(AVAIL) -dynamiclib -O2 -Wall -Wextra \-install_name @rpath/overlay-shim.dylib \-framework Metal -framework QuartzCore \- -framework CoreGraphics -framework CoreFoundation+ -framework CoreGraphics -framework CoreFoundation -framework FoundationOVERLAY_SHIM_TESTS := overlay-shim/testsOVERLAY_SHIM_TEST_FLAGS := -mmacosx-version-min=$(MIN_VER) -O2 -Wall -Wextra \- -framework Metal -framework Foundation -framework QuartzCore+ -framework Metal -framework Foundation -framework QuartzCore -framework CoreGraphicsoverlay-shim: $(OVERLAY_SHIM)@@ -453,7 +453,7 @@ overlay-shim-tests: $(OVERLAY_SHIM)$(call SKIP,overlay-shim-tests,$(OVERLAY_SHIM_TESTS)); exit 0; fi; \mkdir -p $(OUT_DIR); \fail=0; ran=0; skipped=0; \- for t in device bridge delivery; do \+ for t in device bridge delivery ownership; do \for a in arm64 x86_64; do \$(CC) -arch $$a $(OVERLAY_SHIM_TEST_FLAGS) -o $(OUT_DIR)/$$t.$$a \$(OVERLAY_SHIM_TESTS)/$$t.m || exit 1; \diff --git a/overlay-shim/overlay_shim.m b/overlay-shim/overlay_shim.mindex 2d2678b..0c62301 100644--- a/overlay-shim/overlay_shim.m+++ b/overlay-shim/overlay_shim.m@@ -40,8 +40,6 @@ enum {format_rgba16_float = 115,};-// Make room for the overlay to rebuild its pipeline set several times per session.-#define max_pipelines 32#define max_variants 4struct variant {@@ -51,7 +49,6 @@ struct variant {};struct pipeline_entry {- id state;MTLRenderPipelineDescriptor *descriptor;id<MTLDevice> device;NSUInteger base_format;@@ -60,13 +57,32 @@ struct pipeline_entry {int unmarked_logged;};-static struct pipeline_entry pipelines[max_pipelines];-static int pipeline_count;+static const char pipeline_cache_key;+static const char command_pipelines_key;++// the cache dies with original pipeline+@interface NPOverlayPipelineCache : NSObject {+@public+ struct pipeline_entry entry;+}+@end++@implementation NPOverlayPipelineCache+- (void)dealloc+{+ for (int i = 0; i < entry.variant_count; i++)+ [entry.variants[i].state release];+ [entry.descriptor release];+ [entry.device release];+ [super dealloc];+}+@end#define max_encoders 32struct encoder_slot {id encoder;+ id<MTLCommandBuffer> buffer;NSUInteger format;};@@ -242,7 +258,7 @@ static int stage_claimed(Class seen, Class *held, const char *stage)return 1;}-static void encoder_mark(id encoder, NSUInteger format)+static void encoder_mark(id encoder, NSUInteger format, id<MTLCommandBuffer> buffer){os_unfair_lock_lock(&encoder_lock);for (int i = 0; i < max_encoders; i++) {@@ -250,6 +266,7 @@ static void encoder_mark(id encoder, NSUInteger format)continue;overlay_encoders[i].encoder = encoder;+ overlay_encoders[i].buffer = buffer;overlay_encoders[i].format = format;os_unfair_lock_unlock(&encoder_lock);return;@@ -268,13 +285,14 @@ static void encoder_clear(id encoder)for (int i = 0; i < max_encoders; i++) {if (overlay_encoders[i].encoder == encoder) {overlay_encoders[i].encoder = nil;+ overlay_encoders[i].buffer = nil;break;}}os_unfair_lock_unlock(&encoder_lock);}-static int encoder_format(id encoder, NSUInteger *format)+static int encoder_format(id encoder, NSUInteger *format, id<MTLCommandBuffer> *buffer){int found = 0;@@ -282,6 +300,7 @@ static int encoder_format(id encoder, NSUInteger *format)for (int i = 0; i < max_encoders; i++) {if (overlay_encoders[i].encoder == encoder) {*format = overlay_encoders[i].format;+ *buffer = overlay_encoders[i].buffer;found = 1;break;}@@ -317,7 +336,7 @@ static struct variant *variant_for(struct pipeline_entry *entry, NSUInteger formstruct variant *slot = &entry->variants[entry->variant_count];slot->format = format;- slot->state = [state retain];+ slot->state = state;slot->logged = 0;entry->variant_count++;@@ -329,28 +348,46 @@ static struct variant *variant_for(struct pipeline_entry *entry, NSUInteger formstatic void record_overlay_pipeline(id<MTLDevice> device, MTLRenderPipelineDescriptor *descriptor,id state){- for (int i = 0; i < pipeline_count; i++) {- if (pipelines[i].state == state)- return;- }-- if (pipeline_count == max_pipelines) {- shim_log("overlay_shim record_overlay_pipeline: table full, %s not tracked\n",- format_name(descriptor.colorAttachments[0].pixelFormat));+ if (objc_getAssociatedObject(state, &pipeline_cache_key) != nil)return;- }- struct pipeline_entry *entry = &pipelines[pipeline_count];- entry->state = [state retain];+ NPOverlayPipelineCache *cache = [[NPOverlayPipelineCache alloc] init];+ struct pipeline_entry *entry = &cache->entry;entry->descriptor = [descriptor copy];entry->device = [device retain];entry->base_format = descriptor.colorAttachments[0].pixelFormat;entry->variant_count = 0;entry->unmarked_logged = 0;- pipeline_count++;- shim_log("overlay_shim record_overlay_pipeline: overlay pipeline %d format %s\n",- pipeline_count - 1, format_name(entry->base_format));+ shim_log("overlay_shim record_overlay_pipeline: overlay pipeline %p format %s\n",+ (void *)state, format_name(entry->base_format));+ objc_setAssociatedObject(state, &pipeline_cache_key, cache, OBJC_ASSOCIATION_RETAIN);+ [cache release];+}++// Metal won't retain these for an unretained command buffer.+static void hold_pipeline(id<MTLCommandBuffer> buffer, id state)+{+ if (buffer == nil || buffer.retainedReferences)+ return;++ @synchronized(buffer) {+ NSMutableSet *states = objc_getAssociatedObject(buffer, &command_pipelines_key);+ if (states == nil) {+ states = [[NSMutableSet alloc] init];+ objc_setAssociatedObject(buffer, &command_pipelines_key, states, OBJC_ASSOCIATION_RETAIN);+ [buffer addCompletedHandler:^(id<MTLCommandBuffer> completed) {+ (void)completed;+ @synchronized(states) {+ [states removeAllObjects];+ }+ }];+ [states release];+ }+ @synchronized(states) {+ [states addObject:state];+ }+ }}static void ensure_encoder_hooks(id encoder);@@ -446,7 +483,6 @@ static id hook_command_buffer_unretained(id self, SEL selector)static id hook_render_encoder(id self, SEL selector, MTLRenderPassDescriptor *descriptor){- int overlay_pass = from_overlay(__builtin_return_address(0));id<MTLTexture> attachment = descriptor != nil ? descriptor.colorAttachments[0].texture : nil;id encoder = next_render_encoder(self, selector, descriptor);@@ -460,49 +496,41 @@ static id hook_render_encoder(id self, SEL selector, MTLRenderPassDescriptor *deNSUInteger format = attachment.pixelFormat;- encoder_mark(encoder, format);-- if (!overlay_pass)- return encoder;- for (int i = 0; i < pipeline_count; i++) {- if (pipelines[i].base_format != format)- variant_for(&pipelines[i], format);- }+ encoder_mark(encoder, format, self);return encoder;}static void hook_set_pipeline_state(id self, SEL selector, id state){- for (int i = 0; i < pipeline_count; i++) {- if (pipelines[i].state != state)- continue;-- NSUInteger format = 0;- if (!encoder_format(self, &format)) {- if (!pipelines[i].unmarked_logged) {- pipelines[i].unmarked_logged = 1;- shim_log(- "overlay_shim hook_set_pipeline_state: pipeline %d %s bound on an "- "unmarked pass, left alone\n",- i, format_name(pipelines[i].base_format));- }- break;- }-- if (format != pipelines[i].base_format) {- struct variant *slot = variant_for(&pipelines[i], format);- if (slot != NULL) {- if (!slot->logged) {- slot->logged = 1;+ NPOverlayPipelineCache *cache = objc_getAssociatedObject(state, &pipeline_cache_key);+ if (cache != nil) {+ @synchronized(cache) {+ struct pipeline_entry *entry = &cache->entry;+ NSUInteger format = 0;+ id<MTLCommandBuffer> buffer = nil;+ if (!encoder_format(self, &format, &buffer)) {+ if (!entry->unmarked_logged) {+ entry->unmarked_logged = 1;shim_log(- "overlay_shim hook_set_pipeline_state: pipeline %d %s bound to %s "- "attachment, substituted\n",- i, format_name(pipelines[i].base_format), format_name(format));+ "overlay_shim hook_set_pipeline_state: pipeline %p %s bound on an "+ "unmarked pass, left alone\n",+ (void *)state, format_name(entry->base_format));+ }+ } else if (format != entry->base_format) {+ struct variant *slot = variant_for(entry, format);+ if (slot != NULL) {+ if (!slot->logged) {+ slot->logged = 1;+ shim_log(+ "overlay_shim hook_set_pipeline_state: pipeline %p %s bound to %s "+ "attachment, substituted\n",+ (void *)state, format_name(entry->base_format), format_name(format));+ }+ state = slot->state;+ hold_pipeline(buffer, state);}- state = slot->state;}}- break;}next_set_pipeline_state(self, selector, state);diff --git a/overlay-shim/tests/ownership.m b/overlay-shim/tests/ownership.mnew file mode 100644index 0000000..327dfd5--- /dev/null+++ b/overlay-shim/tests/ownership.m@@ -0,0 +1,236 @@+// No GPU needed to check who owns the pipelines.+#import "../overlay_shim.m"+#import "check.h"++static int live_pipelines;+static int live_devices;++@interface TestPipeline : NSObject+@property(nonatomic) NSUInteger format;+@end++@implementation TestPipeline+- (instancetype)init {+ self = [super init];+ if (self) live_pipelines++;+ return self;+}+- (void)dealloc {+ live_pipelines--;+ [super dealloc];+}+@end++@interface TestDevice : NSObject+@property(nonatomic) int builds;+@property(nonatomic) BOOL fail;+@end++@implementation TestDevice+- (instancetype)init {+ self = [super init];+ if (self) live_devices++;+ return self;+}+- (id)newRenderPipelineStateWithDescriptor:(MTLRenderPipelineDescriptor *)descriptor+ error:(NSError **)error {+ (void)error;+ self.builds++;+ if (self.fail) return nil;+ TestPipeline *state = [[TestPipeline alloc] init];+ state.format = descriptor.colorAttachments[0].pixelFormat;+ return state;+}+- (void)dealloc {+ live_devices--;+ [super dealloc];+}+@end++@class TestBuffer;+@interface TestEncoder : NSObject+@property(nonatomic, assign) TestBuffer *buffer;+@property(nonatomic, assign) TestPipeline *bound;+- (void)setRenderPipelineState:(TestPipeline *)state;+- (void)endEncoding;+@end++@interface TestBuffer : NSObject {+ NSMutableArray *handlers;+ NSMutableSet *metal_states;+}+@property(nonatomic) BOOL retainedReferences;+@property(nonatomic, readonly) TestEncoder *encoder;+@property(nonatomic, readonly) int handler_count;+- (void)addCompletedHandler:(void (^)(id<MTLCommandBuffer>))handler;+- (void)usePipeline:(id)state;+- (void)complete;+@end++@implementation TestEncoder+- (void)setRenderPipelineState:(TestPipeline *)state {+ self.bound = state;+ [self.buffer usePipeline:state];+}+- (void)endEncoding {}+@end++@implementation TestBuffer+- (instancetype)init {+ self = [super init];+ if (self) {+ handlers = [[NSMutableArray alloc] init];+ metal_states = [[NSMutableSet alloc] init];+ _encoder = [[TestEncoder alloc] init];+ _encoder.buffer = self;+ }+ return self;+}+- (void)addCompletedHandler:(void (^)(id<MTLCommandBuffer>))handler {+ id copied = [handler copy];+ [handlers addObject:copied];+ [copied release];+ _handler_count++;+}+- (void)usePipeline:(id)state {+ if (self.retainedReferences) [metal_states addObject:state];+}+- (void)complete {+ for (void (^handler)(id<MTLCommandBuffer>) in handlers)+ handler((id<MTLCommandBuffer>)self);+ [handlers removeAllObjects];+ [metal_states removeAllObjects];+}+- (void)dealloc {+ [handlers release];+ [metal_states release];+ [_encoder release];+ [super dealloc];+}+@end++@interface TestTexture : NSObject+@property(nonatomic) MTLPixelFormat pixelFormat;+@end+@implementation TestTexture+@end++static id test_render_encoder(id self, SEL selector, MTLRenderPassDescriptor *descriptor)+{+ (void)selector; (void)descriptor;+ return [(TestBuffer *)self encoder];+}++static void begin_pass(TestBuffer *buffer, MTLPixelFormat format)+{+ TestTexture *texture = [[TestTexture alloc] init];+ texture.pixelFormat = format;+ MTLRenderPassDescriptor *pass = [MTLRenderPassDescriptor renderPassDescriptor];+ pass.colorAttachments[0].texture = (id<MTLTexture>)texture;+ next_render_encoder = test_render_encoder;+ hook_render_encoder(buffer, @selector(renderCommandEncoderWithDescriptor:), pass);+ [texture release];+}++static TestPipeline *new_base(TestDevice *device)+{+ MTLRenderPipelineDescriptor *descriptor = [[MTLRenderPipelineDescriptor alloc] init];+ descriptor.colorAttachments[0].pixelFormat = MTLPixelFormatBGRA8Unorm;+ TestPipeline *state = [[TestPipeline alloc] init];+ state.format = MTLPixelFormatBGRA8Unorm;+ record_overlay_pipeline((id<MTLDevice>)device, descriptor, state);+ record_overlay_pipeline((id<MTLDevice>)device, descriptor, state);+ [descriptor release];+ return state;+}++static int variant_ownership(void)+{+ TestDevice *device = [[TestDevice alloc] init];+ MTLRenderPipelineDescriptor *descriptor = [[MTLRenderPipelineDescriptor alloc] init];+ descriptor.colorAttachments[0].pixelFormat = MTLPixelFormatBGRA8Unorm;+ struct pipeline_entry entry = {+ .descriptor = descriptor, .device = (id<MTLDevice>)device,+ .base_format = MTLPixelFormatBGRA8Unorm+ };+ struct variant *variant = variant_for(&entry, MTLPixelFormatRGB10A2Unorm);+ CHECK(variant != NULL, "a variant is created");+ CHECK(variant_for(&entry, MTLPixelFormatRGB10A2Unorm) == variant && device.builds == 1,+ "the same format reuses its variant");+ CHECK(descriptor.colorAttachments[0].pixelFormat == MTLPixelFormatBGRA8Unorm,+ "building a variant restores the descriptor's format");+ [variant->state release];+ CHECK(live_pipelines == 0, "one release must destroy a new variant, %d still live", live_pipelines);+ [descriptor release];+ [device release];+ return 0;+}++static int buffer_lifetime(BOOL retained, BOOL discard)+{+ TestBuffer *buffer = [[TestBuffer alloc] init];+ buffer.retainedReferences = retained;+ @autoreleasepool {+ TestDevice *device = [[TestDevice alloc] init];+ TestPipeline *base = new_base(device);+ begin_pass(buffer, MTLPixelFormatRGB10A2Unorm);+ [buffer.encoder setRenderPipelineState:base];+ CHECK(buffer.encoder.bound != base && buffer.encoder.bound.format == MTLPixelFormatRGB10A2Unorm,+ "a mismatched pipeline is substituted");+ [buffer.encoder setRenderPipelineState:base];+ CHECK(device.builds == 1, "repeated binds reuse the variant");+ [buffer.encoder endEncoding];+ [base release];+ [device release];+ }+ CHECK(live_devices == 0, "the cache releases its device after the base pipeline dies");+ CHECK(live_pipelines == 1, "only the in-flight variant remains, got %d", live_pipelines);+ CHECK(buffer.encoder.bound.format == MTLPixelFormatRGB10A2Unorm, "the GPU's variant remains valid");+ CHECK(buffer.handler_count == (retained ? 0 : 1), "unretained buffers need one completion handler");+ if (!discard) {+ [buffer complete];+ CHECK(live_pipelines == 0, "completion releases the variant even while the buffer lives");+ }+ [buffer release];+ CHECK(live_pipelines == 0, "discarding a buffer also releases its pipelines");+ return 0;+}++static int rebuilds(void)+{+ for (int i = 0; i < 64; i++) {+ @autoreleasepool {+ TestDevice *device = [[TestDevice alloc] init];+ TestPipeline *base = new_base(device);+ TestBuffer *buffer = [[TestBuffer alloc] init];+ begin_pass(buffer, MTLPixelFormatRGB10A2Unorm);+ device.fail = YES;+ [buffer.encoder setRenderPipelineState:base];+ CHECK(buffer.encoder.bound == base, "a failed compile keeps the original pipeline");+ device.fail = NO;+ [buffer.encoder setRenderPipelineState:base];+ CHECK(buffer.encoder.bound != base, "rebuild %d still gets a corrected pipeline", i);+ [buffer.encoder endEncoding];+ [buffer complete];+ [buffer release];+ [base release];+ [device release];+ }+ CHECK(live_pipelines == 0 && live_devices == 0, "rebuild %d leaves no cached objects", i);+ }+ return 0;+}++int main(void)+{+ @autoreleasepool {+ CHECK(variant_ownership() == 0, "variant ownership");+ CHECK(buffer_lifetime(NO, NO) == 0, "unretained buffer completion");+ CHECK(buffer_lifetime(NO, YES) == 0, "unretained buffer discard");+ CHECK(buffer_lifetime(YES, NO) == 0, "retained buffer completion");+ CHECK(buffer_lifetime(YES, YES) == 0, "retained buffer discard");+ CHECK(rebuilds() == 0, "pipeline rebuilds");+ }+ printf("pipeline ownership, completion, discard and rebuilds passed\n");+ return 0;+}