Offthread gfx #22

Merged
merged 16 commits into from Jan 24, 2016

Infrastructure update

  • Loading branch information...
taisel committed Jan 23, 2016
commit fbece9631c830e962995429c7401fb321e5f6f8f
View
@@ -27,6 +27,9 @@ function GameBoyAdvanceEmulator() {
this.saveExportHandler = null; //Save export handler attached by GUI.
this.saveImportHandler = null; //Save import handler attached by GUI.
this.speedCallback = null; //Speed report handler attached by GUI.
+ this.startCallbacks = []; //Some jobs to run at iteration head.
+ this.endCallbacks = []; //Some jobs to run at iteration end.
+ this.terminationCallbacks = []; //Some jobs to run if the emulation core is killed.
this.timerIntervalRate = 4; //How often the emulator core is called into (in milliseconds).
this.lastTimestamp = 0; //Track the last time given in milliseconds.
this.dynamicSpeedRefresh = false; //Whether speed is allowed to be changed dynamically in the current cycle.
@@ -36,10 +39,19 @@ function GameBoyAdvanceEmulator() {
GameBoyAdvanceEmulator.prototype.generateCoreExposed = function () {
var parentObj = this;
this.coreExposed = {
- "outputAudio":function (l, r) {
+ outputAudio:function (l, r) {
parentObj.outputAudio(l, r);
},
- graphicsHandle:null
+ graphicsHandle:null,
+ appendStartIterationSync:function (callback) {
+ parentObj.startCallbacks.push(callback);
+ },
+ appendEndIterationSync:function (callback) {
+ parentObj.endCallbacks.push(callback);
+ },
+ appendTerminationSync:function (callback) {
+ parentObj.terminationCallbacks.push(callback);
+ }
}
}
GameBoyAdvanceEmulator.prototype.play = function () {
@@ -93,11 +105,41 @@ GameBoyAdvanceEmulator.prototype.iterationStartSequence = function () {
this.emulatorStatus = this.emulatorStatus | 0x2; //If the end routine doesn't unset this, then we are marked as having crashed.
this.audioUnderrunAdjustment(); //If audio is enabled, look to see how much we should overclock by to maintain the audio buffer.
this.audioPushNewState(); //Check to see if we need to update the audio core for any output changes.
+ this.runStartJobs(); //Run various callbacks assigned from internal components.
}
GameBoyAdvanceEmulator.prototype.iterationEndSequence = function () {
this.emulatorStatus = this.emulatorStatus & 0x1D; //If core did not throw while running, unset the fatal error flag.
this.clockCyclesSinceStart = ((this.clockCyclesSinceStart | 0) + (this.CPUCyclesTotal | 0)) | 0; //Accumulate tracking.
this.submitAudioBuffer(); //Flush audio buffer to output.
+ this.runEndJobs(); //Run various callbacks assigned from internal components.
+}
+GameBoyAdvanceEmulator.prototype.runStartJobs = function () {
+ var length = this.startCallbacks.length | 0;
+ //Loop through all jobs:
+ for (var index = 0; (index | 0) < (length | 0); index = ((index | 0) + 1) | 0) {
+ //Run job:
+ this.startCallbacks[index | 0]();
+ }
+}
+GameBoyAdvanceEmulator.prototype.runEndJobs = function () {
+ var length = this.endCallbacks.length | 0;
+ //Loop through all jobs:
+ for (var index = 0; (index | 0) < (length | 0); index = ((index | 0) + 1) | 0) {
+ //Run job:
+ this.endCallbacks[index | 0]();
+ }
+}
+GameBoyAdvanceEmulator.prototype.runTerminationJobs = function () {
+ var length = this.terminationCallbacks.length | 0;
+ //Loop through all jobs:
+ for (var index = 0; (index | 0) < (length | 0); index = ((index | 0) + 1) | 0) {
+ //Run job:
+ this.terminationCallbacks[index | 0]();
+ }
+ //Remove old jobs:
+ this.startCallbacks = [];
+ this.endCallbacks = [];
+ this.terminationCallbacks = [];
}
GameBoyAdvanceEmulator.prototype.attachROM = function (ROM) {
this.stop();
@@ -242,6 +284,8 @@ GameBoyAdvanceEmulator.prototype.calculateSpeedPercentage = function () {
}
}
GameBoyAdvanceEmulator.prototype.initializeCore = function () {
+ //Wrap up any old internal instance callbacks:
+ this.runTerminationJobs();
//Setup a new instance of the i/o core:
this.IOCore = new GameBoyAdvanceIO(this.settings.SKIPBoot, this.coreExposed, this.BIOS, this.ROM);
}
@@ -1,6 +1,6 @@
"use strict";
/*
- Copyright (C) 2012-2015 Grant Galitz
+ Copyright (C) 2012-2016 Grant Galitz
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
@@ -9,12 +9,12 @@
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
function getGameBoyAdvanceGraphicsRenderer(coreExposed, skippingBIOS) {
- if (!window.SharedArrayBuffer || !Atomics) {
+ //if (!window.SharedArrayBuffer || !Atomics) {
return new GameBoyAdvanceGraphicsRenderer(coreExposed, skippingBIOS);
- }
+ /*}
else {
return new GameBoyAdvanceGraphicsRendererShim(coreExposed, skippingBIOS);
- }
+ }*/
}
function GameBoyAdvanceGraphicsRendererShim(coreExposed, skippingBIOS) {
this.coreExposed = coreExposed;
@@ -31,10 +31,31 @@
//Graphics Buffers:
this.gfxCommandBuffer = getSharedInt32Array(0x80000);
this.gfxCommandCounters = getSharedInt32Array(2);
+ this.start = 0;
+ this.end = 0;
+ }
+ GameBoyAdvanceGraphicsRendererShim.prototype.appendAtomicSync = function () {
+ //Command buffer counters get synchronized with emulator runtime head/end for efficiency:
+ var parentObj = this;
+ this.coreExposed.appendStartIterationSync(function () {
+ //Load command buffer reader counter value:
+ parentObj.start = Atomics.load(parentObj.gfxCommandCounters, 0) | 0;
+ });
+ this.coreExposed.appendEndIterationSync(function () {
+ //Store command buffer writer counter value:
+ Atomics.store(parentObj.gfxCommandCounters, 1, parentObj.end | 0);
+ //Tell consumer thread to check command buffer:
+ this.worker.postMessage({messageID:0});
+ });
+ this.coreExposed.appendTerminationSync(function () {
+ //Core instance being replaced, kill the worker thread:
+ this.worker.terminate();
+ });
}
GameBoyAdvanceGraphicsRendererShim.prototype.shareBuffers = function (skippingBIOS) {
skippingBIOS = !!skippingBIOS;
this.worker.postMessage({
+ messageID:1,
skippingBIOS:!!skippingBIOS,
gfxBuffers:gfxBuffers,
gfxCounters:gfxCounters,
@@ -51,22 +72,30 @@
GameBoyAdvanceGraphicsRendererShim.prototype.pushCommand = function (command, data) {
command = command | 0;
data = data | 0;
- //Load the write counter value:
- var end = this.gfxCommandCounters[1] | 0;
//Block while full:
- Atomics.futexWait(this.gfxCommandCounters, 0, ((end | 0) - 0x80000) | 0);
+ this.blockIfCommandBufferFull();
//Get the write offset into the ring buffer:
- var endCorrected = end & 0x7FFFF;
+ var endCorrected = this.end & 0x7FFFF;
//Push command into buffer:
this.gfxCommandBuffer[endCorrected | 0] = command | 0;
//Push data into buffer:
this.gfxCommandBuffer[endCorrected | 1] = data | 0;
//Update the cross thread buffering count:
- end = ((end | 0) + 2) | 0;
- //Atomic store to commit writes to memory:
- Atomics.store(this.gfxCommandCounters, 1, end | 0);
+ this.end = ((this.end | 0) + 2) | 0;
+}
+GameBoyAdvanceGraphicsRendererShim.prototype.blockIfCommandBufferFull = function () {
+ if ((this.start | 0) == (((this.end | 0) - 0x80000) | 0)) {
+ //Wait for consumer thread:
+ Atomics.futexWait(this.gfxCommandCounters, 0, ((this.end | 0) - 0x80000) | 0);
+ //Reload reader counter value:
+ this.start = Atomics.load(this.gfxCommandCounters, 0) | 0;
+ }
}
GameBoyAdvanceGraphicsRendererShim.prototype.incrementScanLineQueue = function () {
//Increment scan line command:
this.pushCommand(0, 0);
}
+GameBoyAdvanceGraphicsRendererShim.prototype.ensureFraming = function () {
+ //Vertical blank synchronization command:
+ this.pushCommand(0, 1);
+}
@@ -141,6 +141,7 @@ self.onmessage = function (event) {
}
}
var graphicsFrameHandler = {
+ //Function only called if graphics is THIS thread:
copyBuffer:function (swizzledFrame) {
//Push a frame of graphics to the blitter handle:
//Load the counter values: