diff options
-rw-r--r-- | src/proxyClient.js | 11 | ||||
-rw-r--r-- | src/proxyWorker.js | 17 | ||||
-rw-r--r-- | src/webGLWorker.js | 5 |
3 files changed, 32 insertions, 1 deletions
diff --git a/src/proxyClient.js b/src/proxyClient.js index f8615bae..1c9b6548 100644 --- a/src/proxyClient.js +++ b/src/proxyClient.js @@ -24,6 +24,12 @@ window.requestAnimationFrame = window.requestAnimationFrame || window.mozRequest // end render +// Frame throttling + +var frameId = 0; + +// Worker + var worker = new Worker('{{{ filename }}}.js'); WebGLClient.prefetch(); // XXX not guaranteed to be before worker main() @@ -84,6 +90,11 @@ worker.onmessage = function worker_onmessage(event) { Module.glClient.onmessage(data); break; } + case 'tick': { + frameId = data.id; + worker.postMessage({ target: 'tock', id: frameId }); + break; + } default: throw 'what?'; } }; diff --git a/src/proxyWorker.js b/src/proxyWorker.js index dd572dad..70b55244 100644 --- a/src/proxyWorker.js +++ b/src/proxyWorker.js @@ -125,6 +125,19 @@ Browser.resizeListeners.push(function(width, height) { postMessage({ target: 'canvas', op: 'resize', width: width, height: height }); }); +// Frame throttling + +var frameId = 0; +var clientFrameId = 0; + +var postMainLoop = Module['postMainLoop']; +Module['postMainLoop'] = function() { + if (postMainLoop) postMainLoop(); + // frame complete, send a frame id + postMessage({ target: 'tick', id: frameId++ }); + commandBuffer = []; +}; + // buffer messages until the program starts to run var messageBuffer = null; @@ -171,6 +184,10 @@ onmessage = function onmessage(message) { webGLWorker.onmessage(message.data); break; } + case 'tock': { + clientFrameId = message.data.id; + break; + } default: throw 'wha? ' + message.data.target; } }; diff --git a/src/webGLWorker.js b/src/webGLWorker.js index 37b844b6..4ccc8d55 100644 --- a/src/webGLWorker.js +++ b/src/webGLWorker.js @@ -609,7 +609,10 @@ function WebGLWorker() { Module['postMainLoop'] = function() { if (postMainLoop) postMainLoop(); // frame complete, send the command buffer - postMessage({ target: 'gl', op: 'render', commandBuffer: commandBuffer }); + if (Math.abs(frameId - clientFrameId) <= 3) { + // only send if not throttling + postMessage({ target: 'gl', op: 'render', commandBuffer: commandBuffer }); + } commandBuffer = []; }; } |