From f9109568b81c5c9e8071b2e8c07f482460950417 Mon Sep 17 00:00:00 2001 From: rogerman Date: Sun, 3 Dec 2017 00:18:30 -0800 Subject: [PATCH] Cocoa Port: Improve stability of Metal display views when running CPU-based pixel scalers. - Also fix a bug where restoring multiple display windows on startup would only have the last display window shown to work properly. --- .../userinterface/EmuControllerDelegate.mm | 1 + .../userinterface/MacMetalDisplayView.mm | 32 +++++++------------ 2 files changed, 13 insertions(+), 20 deletions(-) diff --git a/desmume/src/frontend/cocoa/userinterface/EmuControllerDelegate.mm b/desmume/src/frontend/cocoa/userinterface/EmuControllerDelegate.mm index 28b40eb2c..bd2b262b6 100644 --- a/desmume/src/frontend/cocoa/userinterface/EmuControllerDelegate.mm +++ b/desmume/src/frontend/cocoa/userinterface/EmuControllerDelegate.mm @@ -2410,6 +2410,7 @@ else { [[windowController window] orderFront:self]; + [[windowController window] makeMainWindow]; } // If this window is set to full screen mode, its associated screen index must diff --git a/desmume/src/frontend/cocoa/userinterface/MacMetalDisplayView.mm b/desmume/src/frontend/cocoa/userinterface/MacMetalDisplayView.mm index c3a1b6316..fdb7707a5 100644 --- a/desmume/src/frontend/cocoa/userinterface/MacMetalDisplayView.mm +++ b/desmume/src/frontend/cocoa/userinterface/MacMetalDisplayView.mm @@ -1143,16 +1143,8 @@ _texDisplaySrcDeposterize[NDSDisplayID_Main][1] = [[sharedData device] newTextureWithDescriptor:texDisplaySrcDesc]; _texDisplaySrcDeposterize[NDSDisplayID_Touch][1] = [[sharedData device] newTextureWithDescriptor:texDisplaySrcDesc]; - MTLTextureDescriptor *texDisplayPixelScaleDesc = [MTLTextureDescriptor texture2DDescriptorWithPixelFormat:MTLPixelFormatRGBA8Unorm - width:GPU_FRAMEBUFFER_NATIVE_WIDTH*2 - height:GPU_FRAMEBUFFER_NATIVE_HEIGHT*2 - mipmapped:NO]; - [texDisplayPixelScaleDesc setResourceOptions:MTLResourceStorageModePrivate]; - [texDisplayPixelScaleDesc setStorageMode:MTLStorageModePrivate]; - [texDisplayPixelScaleDesc setUsage:MTLTextureUsageShaderRead | MTLTextureUsageShaderWrite]; - - [self setTexDisplayPixelScaleMain:[[[sharedData device] newTextureWithDescriptor:texDisplayPixelScaleDesc] autorelease]]; - [self setTexDisplayPixelScaleTouch:[[[sharedData device] newTextureWithDescriptor:texDisplayPixelScaleDesc] autorelease]]; + [self setTexDisplayPixelScaleMain:nil]; + [self setTexDisplayPixelScaleTouch:nil]; _processedFrameInfo.tex[NDSDisplayID_Main] = [[sharedData texFetchMain] retain]; _processedFrameInfo.tex[NDSDisplayID_Touch] = [[sharedData texFetchTouch] retain]; @@ -1348,7 +1340,7 @@ { [cce setComputePipelineState:[sharedData deposterizePipeline]]; - if ((texMain != nil) && shouldProcessDisplay[NDSDisplayID_Main]) + if (shouldProcessDisplay[NDSDisplayID_Main] && (texMain != nil)) { [cce setTexture:texMain atIndex:0]; [cce setTexture:_texDisplaySrcDeposterize[NDSDisplayID_Main][0] atIndex:1]; @@ -1370,7 +1362,7 @@ } } - if ((texTouch != nil) && shouldProcessDisplay[NDSDisplayID_Touch]) + if (shouldProcessDisplay[NDSDisplayID_Touch] && (texTouch != nil)) { [cce setTexture:texTouch atIndex:0]; [cce setTexture:_texDisplaySrcDeposterize[NDSDisplayID_Touch][0] atIndex:1]; @@ -1416,11 +1408,13 @@ } // Run the pixel scalers. First attempt on the GPU. + pthread_mutex_lock(((MacMetalDisplayPresenter *)cdp)->GetMutexProcessPtr()); + if ( (cdp->GetPixelScaler() != VideoFilterTypeID_None) && willFilterOnGPU ) { [cce setComputePipelineState:[self pixelScalePipeline]]; - if ((texMain != nil) && shouldProcessDisplay[NDSDisplayID_Main]) + if (shouldProcessDisplay[NDSDisplayID_Main] && (texMain != nil) && ([self texDisplayPixelScaleMain] != nil)) { [cce setTexture:texMain atIndex:0]; [cce setTexture:[self texDisplayPixelScaleMain] atIndex:1]; @@ -1441,7 +1435,7 @@ } } - if ((texTouch != nil) && shouldProcessDisplay[NDSDisplayID_Touch]) + if (shouldProcessDisplay[NDSDisplayID_Touch] && (texTouch != nil) && ([self texDisplayPixelScaleTouch] != nil)) { [cce setTexture:texTouch atIndex:0]; [cce setTexture:[self texDisplayPixelScaleTouch] atIndex:1]; @@ -1533,9 +1527,7 @@ } } - pthread_mutex_lock(((MacMetalDisplayPresenter *)cdp)->GetMutexProcessPtr()); - - if (shouldProcessDisplay[NDSDisplayID_Main]) + if (shouldProcessDisplay[NDSDisplayID_Main] && ([self texDisplayPixelScaleMain] != nil)) { pthread_rwlock_rdlock(((MacMetalDisplayPresenter *)cdp)->GetCPUFilterRWLock(NDSDisplayID_Main, bufferIndex)); needsCPUFilterUnlockMain = true; @@ -1572,7 +1564,7 @@ } } - if (shouldProcessDisplay[NDSDisplayID_Touch]) + if (shouldProcessDisplay[NDSDisplayID_Touch] && ([self texDisplayPixelScaleTouch] != nil)) { pthread_rwlock_rdlock(((MacMetalDisplayPresenter *)cdp)->GetCPUFilterRWLock(NDSDisplayID_Touch, bufferIndex)); needsCPUFilterUnlockTouch = true; @@ -1601,11 +1593,11 @@ isDisplayProcessedTouch = true; } - pthread_mutex_unlock(((MacMetalDisplayPresenter *)cdp)->GetMutexProcessPtr()); - [bce endEncoding]; } + pthread_mutex_unlock(((MacMetalDisplayPresenter *)cdp)->GetMutexProcessPtr()); + if (needsCPUFilterUnlockMain) { if (needsCPUFilterUnlockTouch)