Skip to content

Commit

Permalink
Merge pull request #18123 from zeux/basisworkers
Browse files Browse the repository at this point in the history
BasisTextureLoader: Fix severe work distribution imbalance
  • Loading branch information
mrdoob authored Dec 12, 2019
2 parents 3766464 + f19d979 commit e66e869
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 18 deletions.
20 changes: 11 additions & 9 deletions examples/js/loaders/BasisTextureLoader.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,9 @@ THREE.BasisTextureLoader.prototype = Object.assign( Object.create( THREE.Loader.
var worker;
var taskID;

var texturePending = this._getWorker()
var taskCost = buffer.byteLength;

var texturePending = this._allocateWorker( taskCost )
.then( ( _worker ) => {

worker = _worker;
Expand All @@ -128,8 +130,6 @@ THREE.BasisTextureLoader.prototype = Object.assign( Object.create( THREE.Loader.
return new Promise( ( resolve, reject ) => {

worker._callbacks[ taskID ] = { resolve, reject };
worker._taskCosts[ taskID ] = buffer.byteLength;
worker._taskLoad += worker._taskCosts[ taskID ];

worker.postMessage( { type: 'transcode', id: taskID, buffer }, [ buffer ] );

Expand Down Expand Up @@ -181,9 +181,8 @@ THREE.BasisTextureLoader.prototype = Object.assign( Object.create( THREE.Loader.

if ( worker && taskID ) {

worker._taskLoad -= worker._taskCosts[ taskID ];
worker._taskLoad -= taskCost;
delete worker._callbacks[ taskID ];
delete worker._taskCosts[ taskID ];

}

Expand All @@ -195,7 +194,7 @@ THREE.BasisTextureLoader.prototype = Object.assign( Object.create( THREE.Loader.

_initTranscoder: function () {

if ( ! this.transcoderBinary ) {
if ( ! this.transcoderPending ) {

// Load transcoder wrapper.
var jsLoader = new THREE.FileLoader( this.manager );
Expand Down Expand Up @@ -239,7 +238,7 @@ THREE.BasisTextureLoader.prototype = Object.assign( Object.create( THREE.Loader.

},

_getWorker: function () {
_allocateWorker: function ( taskCost ) {

return this._initTranscoder().then( () => {

Expand All @@ -248,7 +247,6 @@ THREE.BasisTextureLoader.prototype = Object.assign( Object.create( THREE.Loader.
var worker = new Worker( this.workerSourceURL );

worker._callbacks = {};
worker._taskCosts = {};
worker._taskLoad = 0;

worker.postMessage( {
Expand Down Expand Up @@ -290,7 +288,11 @@ THREE.BasisTextureLoader.prototype = Object.assign( Object.create( THREE.Loader.

}

return this.workerPool[ this.workerPool.length - 1 ];
var worker = this.workerPool[ this.workerPool.length - 1 ];

worker._taskLoad += taskCost;

return worker;

} );

Expand Down
20 changes: 11 additions & 9 deletions examples/jsm/loaders/BasisTextureLoader.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,9 @@ BasisTextureLoader.prototype = Object.assign( Object.create( Loader.prototype ),
var worker;
var taskID;

var texturePending = this._getWorker()
var taskCost = buffer.byteLength;

var texturePending = this._allocateWorker( taskCost )
.then( ( _worker ) => {

worker = _worker;
Expand All @@ -141,8 +143,6 @@ BasisTextureLoader.prototype = Object.assign( Object.create( Loader.prototype ),
return new Promise( ( resolve, reject ) => {

worker._callbacks[ taskID ] = { resolve, reject };
worker._taskCosts[ taskID ] = buffer.byteLength;
worker._taskLoad += worker._taskCosts[ taskID ];

worker.postMessage( { type: 'transcode', id: taskID, buffer }, [ buffer ] );

Expand Down Expand Up @@ -194,9 +194,8 @@ BasisTextureLoader.prototype = Object.assign( Object.create( Loader.prototype ),

if ( worker && taskID ) {

worker._taskLoad -= worker._taskCosts[ taskID ];
worker._taskLoad -= taskCost;
delete worker._callbacks[ taskID ];
delete worker._taskCosts[ taskID ];

}

Expand All @@ -208,7 +207,7 @@ BasisTextureLoader.prototype = Object.assign( Object.create( Loader.prototype ),

_initTranscoder: function () {

if ( ! this.transcoderBinary ) {
if ( ! this.transcoderPending ) {

// Load transcoder wrapper.
var jsLoader = new FileLoader( this.manager );
Expand Down Expand Up @@ -252,7 +251,7 @@ BasisTextureLoader.prototype = Object.assign( Object.create( Loader.prototype ),

},

_getWorker: function () {
_allocateWorker: function ( taskCost ) {

return this._initTranscoder().then( () => {

Expand All @@ -261,7 +260,6 @@ BasisTextureLoader.prototype = Object.assign( Object.create( Loader.prototype ),
var worker = new Worker( this.workerSourceURL );

worker._callbacks = {};
worker._taskCosts = {};
worker._taskLoad = 0;

worker.postMessage( {
Expand Down Expand Up @@ -303,7 +301,11 @@ BasisTextureLoader.prototype = Object.assign( Object.create( Loader.prototype ),

}

return this.workerPool[ this.workerPool.length - 1 ];
var worker = this.workerPool[ this.workerPool.length - 1 ];

worker._taskLoad += taskCost;

return worker;

} );

Expand Down

0 comments on commit e66e869

Please sign in to comment.