Skip to content

Commit

Permalink
Fixed and added back frame skipping
Browse files Browse the repository at this point in the history
  • Loading branch information
WilliamLCobb committed May 8, 2016
1 parent a2462b1 commit fecadeb
Show file tree
Hide file tree
Showing 7 changed files with 110 additions and 74 deletions.
4 changes: 3 additions & 1 deletion desmume/src/NDSSystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1519,7 +1519,9 @@ static void execHardware_hblank()

//BUT! this was removed in order to make glitches in megaman zero collection (mmz 4 1st level) work.
//and, it seems that it is no longer necessary in nsmb. perhaps something else fixed it
GPU_swapScreens();
if (!frameSkipper.ShouldSkip2D()) {
GPU_swapScreens();
}
}


Expand Down
9 changes: 9 additions & 0 deletions iNDS.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -930,6 +930,7 @@
BF9D515517865E1300B5864B /* Resources */ = {
isa = PBXGroup;
children = (
CA4846611CDFDF2E005030EF /* Test */,
CABE38BF1C34B8B800BE8857 /* Icons */,
CAB2C42F1CB222AF00ED22BB /* cheats.rar */,
CAB2C42B1CB21C2000ED22BB /* cheats.7z */,
Expand Down Expand Up @@ -1382,6 +1383,14 @@
path = OptimizationProfiles;
sourceTree = "<group>";
};
CA4846611CDFDF2E005030EF /* Test */ = {
isa = PBXGroup;
children = (
);
name = Test;
path = iNDS/Test;
sourceTree = "<group>";
};
CA6882211CDC194000B0ED05 /* WCBuildStore */ = {
isa = PBXGroup;
children = (
Expand Down
11 changes: 10 additions & 1 deletion iNDS/AppDelegate.m
Original file line number Diff line number Diff line change
Expand Up @@ -502,7 +502,16 @@ - (WCEasySettingsViewController *)getSettingsViewController
optionSubtitles:nil
subtitle:@"JIT is not yet available for your device."];
}
coreSection.items = @[engineOption];

coreSection.items = @[engineOption,
[[WCEasySettingsSegment alloc] initWithIdentifier:@"frameSkip"
title:@"Frame Skip"
items:@[@"None",
@"1",
@"2",
@"3",
@"4"]]
];


// Auto Save
Expand Down
25 changes: 8 additions & 17 deletions iNDS/core/emu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -355,9 +355,9 @@ void iNDS_user()
}
}

void iNDS_throttle()
bool iNDS_frameSkip()
{

bool skipped;
//Change in skip rate
if(lastskiprate != frameskiprate)
{
Expand All @@ -373,6 +373,7 @@ void iNDS_throttle()

if (mainLoopData.framestoskip > 0)
mainLoopData.skipnextframe = 1;
skipped = false;
}
else //Skip
{
Expand All @@ -386,22 +387,11 @@ void iNDS_throttle()
mainLoopData.framesskipped++;

NDS_SkipNextFrame();
skipped = true;
}

if (autoframeskipenab && frameskiprate)
{
if(!frameAdvance && !continuousframeAdvancing)
{
AutoFrameSkip_NextFrame();
if (mainLoopData.framestoskip < 1)
mainLoopData.framestoskip += AutoFrameSkip_GetSkipAmount(0, frameskiprate);
}
}

else
{
if (mainLoopData.framestoskip < 1)
mainLoopData.framestoskip += frameskiprate;
if (mainLoopData.framestoskip < 1) {
mainLoopData.framestoskip += frameskiprate;
}

if(execute && emu_paused && !frameAdvance)
Expand All @@ -411,14 +401,15 @@ void iNDS_throttle()
}

//ServiceDisplayThreadInvocations();
return skipped;
}

int EMU_runOther()
{
if(execute)
{
iNDS_user();
iNDS_throttle();
iNDS_frameSkip();
return mainLoopData.fps > 0 ? mainLoopData.fps : 1;
}
return 1;
Expand Down
2 changes: 1 addition & 1 deletion iNDS/core/emu.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ bool EMU_loadRom(const char* path);
void EMU_change3D(int type);
void EMU_changeSound(int type);
void EMU_enableSound(bool enable);
void iNDS_throttle();
bool iNDS_frameSkip();
void EMU_setFrameSkip(int skip);
void EMU_setCPUMode(int cpuMode);
void EMU_setSynchMode(bool enabled);
Expand Down
131 changes: 78 additions & 53 deletions iNDS/iNDSEmulatorViewController.mm
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ @interface iNDSEmulatorViewController () <GLKViewDelegate, iCadeEventDelegate> {
RBVolumeButtons *volumeStealer;
SharkfoodMuteSwitchDetector *muteDetector;

CADisplayLink *coreLink;
dispatch_semaphore_t displaySemaphore;
}

Expand Down Expand Up @@ -261,6 +262,15 @@ - (void)viewDidLoad
control.delegate = self;

disableTouchScreen = [[NSUserDefaults standardUserDefaults] boolForKey:@"disableTouchScreen"];

coreLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(emulatorLoop)];
coreLink.paused = YES;
NSLog(@"Time Interval: %f", coreLink.duration);
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
[coreLink addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
[[NSRunLoop currentRunLoop] run];
});

}

- (void)viewWillAppear:(BOOL)animated {
Expand Down Expand Up @@ -433,6 +443,8 @@ - (void)screenChanged:(NSNotification*)notification

#pragma mark - Playing ROM



- (void)loadROM {
NSLog(@"Loading ROM %@", self.game.path);
EMU_setWorkingDir([[self.game.path stringByDeletingLastPathComponent] fileSystemRepresentation]);
Expand Down Expand Up @@ -488,12 +500,7 @@ - (void)initGL
self.profile.mainScreen = glkView[0];
self.profile.touchScreen = glkView[1];

//ToDo: make this better
BOOL bicubic = [[NSUserDefaults standardUserDefaults] boolForKey:@"highresGraphics"];
if (bicubic) {
kFragShader = [NSString stringWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"Shader" ofType:@"fsh"] encoding:NSUTF8StringEncoding error:nil];
} else {
kFragShader = SHADER_STRING (
kFragShader = SHADER_STRING (
uniform sampler2D inputImageTexture;
varying highp vec2 texCoord;

Expand All @@ -503,7 +510,7 @@ void main()
gl_FragColor = color;
}
);
}


self.program = [[GLProgram alloc] initWithVertexShaderString:kVertShader fragmentShaderString:kFragShader];

Expand All @@ -528,15 +535,15 @@ void main()
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, texHandle[0]);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, bicubic ? GL_NEAREST : GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);

//Window 2
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, texHandle[1]);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, bicubic ? GL_NEAREST : GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
}
Expand Down Expand Up @@ -610,53 +617,71 @@ - (void)startEmulatorLoop
{
[self.view endEditing:YES];
[self updateDisplay]; //This has to be called once before we touch or move any glk views
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
NSInteger filter = [[NSUserDefaults standardUserDefaults] integerForKey:@"videoFilter"];
displaySemaphore = dispatch_semaphore_create(1);
CGFloat framesToRender = 0;

displaySemaphore = dispatch_semaphore_create(1);

lastAutosave = CACurrentMediaTime();
[emuLoopLock lock];
[[iNDSMFIControllerSupport instance] startMonitoringGamePad];

coreFps = videoFps = 30;

coreLink.paused = NO;
}

- (void)endEmulatorLoop
{
NSLog(@"Ending loop");
[[iNDSMFIControllerSupport instance] stopMonitoringGamePad];
[emuLoopLock unlock];
coreLink.paused = YES;
}

//Relocate
CFTimeInterval coreStart, loopStart;
CGFloat framesToRender = 0;
NSInteger filter = [[NSUserDefaults standardUserDefaults] integerForKey:@"videoFilter"];

- (void)emulatorLoop
{
if (!execute) {
[self endEmulatorLoop];
}
loopStart = CACurrentMediaTime();
framesToRender += self.speed;

for (;framesToRender >= 1; framesToRender--) {
coreStart = CACurrentMediaTime();
EMU_runCore();
coreFps = coreFps * 0.99 + (1 / (CACurrentMediaTime() - coreStart)) * 0.01;
}

if (CACurrentMediaTime() - lastAutosave > 180) {
CGFloat coreTime = [[NSUserDefaults standardUserDefaults] floatForKey:@"coreTime"];
coreTime = coreTime * 0.95 + (CACurrentMediaTime() - coreStart) * 0.05;
[[NSUserDefaults standardUserDefaults] setFloat:coreTime forKey:@"coreTime"];
if ([[NSUserDefaults standardUserDefaults] boolForKey:@"periodicSave"]) {
[self saveStateWithName:[NSString stringWithFormat:@"Auto Save"]];
}
lastAutosave = CACurrentMediaTime();
[emuLoopLock lock];
[[iNDSMFIControllerSupport instance] startMonitoringGamePad];
CFTimeInterval coreStart, loopStart;
coreFps = videoFps = 30;
while (execute) {
loopStart = CACurrentMediaTime();
framesToRender += self.speed;

for (;framesToRender >= 1; framesToRender--) {
coreStart = CACurrentMediaTime();
EMU_runCore();
coreFps = coreFps * 0.95 + (1 / (CACurrentMediaTime() - coreStart)) * 0.05;
}

if (CACurrentMediaTime() - lastAutosave > 180) {
CGFloat coreTime = [[NSUserDefaults standardUserDefaults] floatForKey:@"coreTime"];
coreTime = coreTime * 0.95 + (CACurrentMediaTime() - coreStart) * 0.05;
[[NSUserDefaults standardUserDefaults] setFloat:coreTime forKey:@"coreTime"];
if ([[NSUserDefaults standardUserDefaults] boolForKey:@"periodicSave"]) {
[self saveStateWithName:[NSString stringWithFormat:@"Auto Save"]];
}
lastAutosave = CACurrentMediaTime();
filter = [[NSUserDefaults standardUserDefaults] integerForKey:@"videoFilter"];
}
if (filter == 0) {
filter = [[NSUserDefaults standardUserDefaults] integerForKey:@"videoFilter"];
}
if (!iNDS_frameSkip()) {
if (filter == -1) {
EMU_copyMasterBuffer();
[self updateDisplay];
} else {
// Run the filter on a seperate thread to increase performance
// Core will always be one frame ahead
dispatch_semaphore_wait(displaySemaphore, DISPATCH_TIME_FOREVER);
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
EMU_copyMasterBuffer();
//This will automatically throttle fps to 60
[self updateDisplay];
} else {
// Run the filter on a seperate thread to increase performance
// Core will always be one frame ahead
dispatch_semaphore_wait(displaySemaphore, DISPATCH_TIME_FOREVER);
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
EMU_copyMasterBuffer();
//This will automatically throttle fps to 60
[self updateDisplay];
dispatch_semaphore_signal(displaySemaphore);
});
}
dispatch_semaphore_signal(displaySemaphore);
});
}
[[iNDSMFIControllerSupport instance] stopMonitoringGamePad];
[emuLoopLock unlock];
});
}
}

- (void)saveStateWithName:(NSString*)saveStateName
Expand All @@ -682,7 +707,7 @@ - (void)updateDisplay
static CFTimeInterval fpsUpdateTime = 0;
if (CACurrentMediaTime() - fpsUpdateTime > 1) {
dispatch_async(dispatch_get_main_queue(), ^{
self.fpsLabel.text = [NSString stringWithFormat:@"%d FPS %d Max Core", MIN((int)videoFps + 5, 60), (int)(coreFps / self.speed)];
self.fpsLabel.text = [NSString stringWithFormat:@"%d FPS %d Max Core", MIN((int)videoFps, 60), (int)(coreFps / self.speed)];
});
fpsUpdateTime = CACurrentMediaTime();
}
Expand Down
2 changes: 1 addition & 1 deletion iNDS/support/iNDS-Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@
<key>CFBundlePackageType</key>
<string>APPL</string>
<key>CFBundleShortVersionString</key>
<string>1.5.1.2</string>
<string>1.6.0</string>
<key>CFBundleSignature</key>
<string>????</string>
<key>CFBundleURLTypes</key>
Expand Down

0 comments on commit fecadeb

Please sign in to comment.