Mac: Remove render lock and fix freeze/defrost screen

This commit is contained in:
Michael Buckley 2019-12-26 11:12:39 -08:00
parent 375ba2702a
commit b12ca3e9e3
3 changed files with 13 additions and 35 deletions

BIN
macosx/.DS_Store vendored

Binary file not shown.

View File

@ -219,7 +219,6 @@ bool8 pressedFunctionButtons[kNumFunctionButtons] = { 0 };
bool8 pressedRawKeyboardButtons[MAC_NUM_KEYCODES] = { 0 }; bool8 pressedRawKeyboardButtons[MAC_NUM_KEYCODES] = { 0 };
bool8 heldFunctionButtons[kNumFunctionButtons] = { 0 }; bool8 heldFunctionButtons[kNumFunctionButtons] = { 0 };
pthread_mutex_t keyLock; pthread_mutex_t keyLock;
pthread_mutex_t renderLock;
MTKView *s9xView; MTKView *s9xView;
@ -400,9 +399,7 @@ static inline void EmulationLoop (void)
if (!pauseEmulation) if (!pauseEmulation)
{ {
pthread_mutex_lock(&renderLock);
S9xMainLoop(); S9xMainLoop();
pthread_mutex_unlock(&renderLock);
} }
else else
{ {
@ -411,9 +408,7 @@ static inline void EmulationLoop (void)
macFrameSkip = 1; macFrameSkip = 1;
skipFrames = 1; skipFrames = 1;
frameAdvance = false; frameAdvance = false;
pthread_mutex_lock(&renderLock);
S9xMainLoop(); S9xMainLoop();
pthread_mutex_unlock(&renderLock);
macFrameSkip = storedMacFrameSkip; macFrameSkip = storedMacFrameSkip;
} }
@ -2808,7 +2803,6 @@ void QuitWithFatalError ( NSString *message)
+ (void)initialize + (void)initialize
{ {
keyLock = PTHREAD_MUTEX_INITIALIZER; keyLock = PTHREAD_MUTEX_INITIALIZER;
renderLock = PTHREAD_MUTEX_INITIALIZER;
} }
- (instancetype)initWithFrame:(NSRect)frameRect - (instancetype)initWithFrame:(NSRect)frameRect
@ -2939,13 +2933,10 @@ void QuitWithFatalError ( NSString *message)
- (void)drawRect:(NSRect)dirtyRect - (void)drawRect:(NSRect)dirtyRect
{ {
pthread_mutex_lock(&renderLock);
self.subviews[0].hidden = !pauseEmulation; self.subviews[0].hidden = !pauseEmulation;
CGFloat scaleFactor = MAX(self.window.backingScaleFactor, 1.0); CGFloat scaleFactor = MAX(self.window.backingScaleFactor, 1.0);
glScreenW = self.frame.size.width * scaleFactor; glScreenW = self.frame.size.width * scaleFactor;
glScreenH = self.frame.size.height * scaleFactor; glScreenH = self.frame.size.height * scaleFactor;
S9xPutImage(IPPU.RenderedScreenWidth, IPPU.RenderedScreenHeight);
pthread_mutex_unlock(&renderLock);
} }
- (void)setFrame:(NSRect)frame - (void)setFrame:(NSRect)frame
@ -3211,9 +3202,7 @@ void QuitWithFatalError ( NSString *message)
- (void)setVideoMode:(int)mode - (void)setVideoMode:(int)mode
{ {
pthread_mutex_lock(&renderLock);
videoMode = mode; videoMode = mode;
pthread_mutex_unlock(&renderLock);
} }
@dynamic inputDelegate; @dynamic inputDelegate;

View File

@ -36,7 +36,7 @@
static void S9xInitMetal (void); static void S9xInitMetal (void);
static void S9xDeinitMetal(void); static void S9xDeinitMetal(void);
static void S9xPutImageMetal (int, int, uint8 *); static void S9xPutImageMetal (int, int, uint16 *);
static uint16 *gfxScreen[2], static uint16 *gfxScreen[2],
*snesScreenA, *snesScreenA,
@ -137,21 +137,8 @@ void DeinitGraphics (void)
void DrawFreezeDefrostScreen (uint8 *draw) void DrawFreezeDefrostScreen (uint8 *draw)
{ {
const int w = SNES_WIDTH << 1, h = SNES_HEIGHT << 1; const int w = SNES_WIDTH << 1, h = SNES_HEIGHT << 1;
S9xPutImageMetal(w, h, (uint16 *)draw);
imageWidth[0] = imageHeight[0] = 0;
imageWidth[1] = imageHeight[1] = 0;
prevBlitWidth = prevBlitHeight = 0;
if (nx < 0)
{
for (int y = 0; y < h; y++)
memcpy(blitGLBuffer + y * 1024 * 2, draw + y * w * 2, w * 2);
}
else
memcpy(blitGLBuffer, draw, w * h * 2);
S9xPutImageMetal(w, h, draw);
} }
static void S9xInitMetal (void) static void S9xInitMetal (void)
@ -262,6 +249,7 @@ bool8 S9xInitUpdate (void)
bool8 S9xDeinitUpdate (int width, int height) bool8 S9xDeinitUpdate (int width, int height)
{ {
S9xPutImage(width, height);
return true; return true;
} }
@ -298,11 +286,17 @@ void S9xPutImage (int width, int height)
IPPU.DisplayedRenderedFrameCount = (Memory.ROMFramesPerSecond * 60) / frameCalc; IPPU.DisplayedRenderedFrameCount = (Memory.ROMFramesPerSecond * 60) / frameCalc;
} }
S9xPutImageMetal(width, height, GFX.Screen);
}
uint8 buffer[width * height * 4];
static void S9xPutImageMetal (int width, int height, uint16 *buffer16)
{
uint8 *buffer = (uint8 *)malloc(width * height * 4);
for (int i = 0; i < width * height; ++i) for (int i = 0; i < width * height; ++i)
{ {
uint16 pixel = GFX.Screen[i]; uint16 pixel = buffer16[i];
unsigned int red = (pixel & FIRST_COLOR_MASK_RGB555) >> 10; unsigned int red = (pixel & FIRST_COLOR_MASK_RGB555) >> 10;
unsigned int green = (pixel & SECOND_COLOR_MASK_RGB555) >> 5; unsigned int green = (pixel & SECOND_COLOR_MASK_RGB555) >> 5;
unsigned int blue = (pixel & THIRD_COLOR_MASK_RGB555); unsigned int blue = (pixel & THIRD_COLOR_MASK_RGB555);
@ -318,12 +312,6 @@ void S9xPutImage (int width, int height)
buffer[offset] = 0xFF; buffer[offset] = 0xFF;
} }
S9xPutImageMetal(width, height, buffer);
}
static void S9xPutImageMetal (int width, int height, uint8 *buffer)
{
CGSize layerSize = metalLayer.bounds.size; CGSize layerSize = metalLayer.bounds.size;
MTLTextureDescriptor *descriptor = [MTLTextureDescriptor new]; MTLTextureDescriptor *descriptor = [MTLTextureDescriptor new];
@ -334,6 +322,7 @@ static void S9xPutImageMetal (int width, int height, uint8 *buffer)
metalTexture = [metalDevice newTextureWithDescriptor:descriptor]; metalTexture = [metalDevice newTextureWithDescriptor:descriptor];
[metalTexture replaceRegion:MTLRegionMake2D(0, 0, width, height) mipmapLevel:0 withBytes:buffer bytesPerRow:width * 4]; [metalTexture replaceRegion:MTLRegionMake2D(0, 0, width, height) mipmapLevel:0 withBytes:buffer bytesPerRow:width * 4];
free(buffer);
float vWidth = layerSize.width / 2.0; float vWidth = layerSize.width / 2.0;
float vHeight = layerSize.height / 2.0; float vHeight = layerSize.height / 2.0;