BasisTextureLoader.js 10.4 KB
Newer Older
D
Don McCurdy 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/**
 * @author Don McCurdy / https://www.donmccurdy.com
 * @author Austin Eng / https://github.com/austinEng
 * @author Shrek Shao / https://github.com/shrekshao
 */

/**
 * Loader for Basis Universal GPU Texture Codec.
 *
 * Basis Universal is a "supercompressed" GPU texture and texture video
 * compression system that outputs a highly compressed intermediate file format
 * (.basis) that can be quickly transcoded to a wide variety of GPU texture
 * compression formats.
 *
 * This loader parallelizes the transcoding process across a configurable number
 * of web workers, before transferring the transcoded compressed texture back
 * to the main thread.
 */
19
THREE.BasisTextureLoader = function ( manager ) {
D
Don McCurdy 已提交
20

21
	this.manager = manager || THREE.DefaultLoadingManager;
D
Don McCurdy 已提交
22

23
	this.crossOrigin = 'anonymous';
D
Don McCurdy 已提交
24

25 26 27
	this.transcoderPath = '';
	this.transcoderBinary = null;
	this.transcoderPending = null;
28

29 30 31 32 33 34 35 36 37 38
	this.workerLimit = 4;
	this.workerPool = [];
	this.workerNextTaskID = 1;
	this.workerSourceURL = '';
	this.workerConfig = {
		format: null,
		etcSupported: false,
		dxtSupported: false,
		pvrtcSupported: false,
	};
D
Don McCurdy 已提交
39

40
};
D
Don McCurdy 已提交
41

42 43 44
THREE.BasisTextureLoader.prototype = {

	constructor: THREE.BasisTextureLoader,
D
Don McCurdy 已提交
45

46
	setCrossOrigin: function ( crossOrigin ) {
47 48 49 50 51

		this.crossOrigin = crossOrigin;

		return this;

52
	},
53

54
	setTranscoderPath: function ( path ) {
D
Don McCurdy 已提交
55 56 57

		this.transcoderPath = path;

58 59
		return this;

60
	},
61

62
	setWorkerLimit: function ( workerLimit ) {
63 64 65 66 67

		this.workerLimit = workerLimit;

		return this;

68
	},
D
Don McCurdy 已提交
69

70
	detectSupport: function ( renderer ) {
D
Don McCurdy 已提交
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99

		var context = renderer.context;
		var config = this.workerConfig;

		config.etcSupported = !! context.getExtension('WEBGL_compressed_texture_etc1');
		config.dxtSupported = !! context.getExtension('WEBGL_compressed_texture_s3tc');
		config.pvrtcSupported = !! context.getExtension('WEBGL_compressed_texture_pvrtc')
			|| !! context.getExtension('WEBKIT_WEBGL_compressed_texture_pvrtc');

		if ( config.etcSupported ) {

			config.format = THREE.BasisTextureLoader.BASIS_FORMAT.cTFETC1;

		} else if ( config.dxtSupported ) {

			config.format = THREE.BasisTextureLoader.BASIS_FORMAT.cTFBC1;

		} else if ( config.pvrtcSupported ) {

			config.format = THREE.BasisTextureLoader.BASIS_FORMAT.cTFPVRTC1_4_OPAQUE_ONLY;

		} else {

			throw new Error( 'THREE.BasisTextureLoader: No suitable compressed texture format found.' );

		}

		return this;

100
	},
D
Don McCurdy 已提交
101

102
	load: function ( url, onLoad, onProgress, onError ) {
D
Don McCurdy 已提交
103

104 105 106 107 108 109 110 111 112 113 114
		var loader = new THREE.FileLoader( this.manager );

		loader.setResponseType( 'arraybuffer' );

		loader.load( url, ( buffer ) => {

			this._createTexture( buffer )
				.then( onLoad )
				.catch( onError );

		}, onProgress, onError );
D
Don McCurdy 已提交
115

116
	},
D
Don McCurdy 已提交
117 118 119 120 121

	/**
	 * @param  {ArrayBuffer} buffer
	 * @return {Promise<THREE.CompressedTexture>}
	 */
122
	_createTexture: function ( buffer ) {
D
Don McCurdy 已提交
123

124 125 126
		var worker;
		var taskID;

127
		var texturePending = this._getWorker()
128
			.then( ( _worker ) => {
D
Don McCurdy 已提交
129

130 131
				worker = _worker;
				taskID = this.workerNextTaskID++;
D
Don McCurdy 已提交
132

133
				return new Promise( ( resolve, reject ) => {
D
Don McCurdy 已提交
134

135
					worker._callbacks[ taskID ] = { resolve, reject };
D
Don McCurdy 已提交
136 137 138 139 140
					worker._taskCosts[ taskID ] = buffer.byteLength;
					worker._taskLoad += worker._taskCosts[ taskID ];

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

141
				} )
D
Don McCurdy 已提交
142 143 144 145 146 147

			} )
			.then( ( message ) => {

				var config = this.workerConfig;

148
				var { width, height, mipmaps } = message;
D
Don McCurdy 已提交
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178

				var texture;

				if ( config.etcSupported ) {

					texture = new THREE.CompressedTexture( mipmaps, width, height, THREE.RGB_ETC1_Format );

				} else if ( config.dxtSupported ) {

					texture = new THREE.CompressedTexture( mipmaps, width, height, THREE.BasisTextureLoader.DXT_FORMAT_MAP[ config.format ], THREE.UnsignedByteType );

				} else if ( config.pvrtcSupported ) {

					texture = new THREE.CompressedTexture( mipmaps, width, height, THREE.RGB_PVRTC_4BPPV1_Format );

				} else {

					throw new Error( 'THREE.BasisTextureLoader: No supported format available.' );

				}

				texture.minFilter = THREE.LinearMipMapLinearFilter;
				texture.magFilter = THREE.LinearFilter;
				texture.generateMipmaps = false;
				texture.needsUpdate = true;

				return texture;

			});

179 180 181 182 183 184 185 186 187 188 189 190 191 192 193
		texturePending
			.finally( () => {

				if ( worker && taskID ) {

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

				}

			} );

		return texturePending;

194
	},
D
Don McCurdy 已提交
195

196
	_initTranscoder: function () {
D
Don McCurdy 已提交
197 198 199

		if ( ! this.transcoderBinary ) {

200 201 202 203
			// Load transcoder wrapper.
			var jsLoader = new THREE.FileLoader( this.manager );
			jsLoader.setPath( this.transcoderPath );
			var jsContent = new Promise( ( resolve, reject ) => {
D
Don McCurdy 已提交
204

205 206 207 208 209 210 211 212 213 214 215 216 217
				jsLoader.load( 'basis_transcoder.js', resolve, undefined, reject );

			} );

			// Load transcoder WASM binary.
			var binaryLoader = new THREE.FileLoader( this.manager );
			binaryLoader.setPath( this.transcoderPath );
			binaryLoader.setResponseType( 'arraybuffer' );
			var binaryContent = new Promise( ( resolve, reject ) => {

				binaryLoader.load( 'basis_transcoder.wasm', resolve, undefined, reject );

			} );
D
Don McCurdy 已提交
218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244

			this.transcoderPending = Promise.all( [ jsContent, binaryContent ] )
				.then( ( [ jsContent, binaryContent ] ) => {

					var fn = THREE.BasisTextureLoader.BasisWorker.toString();

					var body = [
						'/* basis_transcoder.js */',
						'var Module;',
						'function createBasisModule () {',
						'  ' + jsContent,
						'  return Module;',
						'}',
						'',
						'/* worker */',
						fn.substring( fn.indexOf( '{' ) + 1, fn.lastIndexOf( '}' ) )
					].join( '\n' );

					this.workerSourceURL = URL.createObjectURL( new Blob( [ body ] ) );
					this.transcoderBinary = binaryContent;

				} );

		}

		return this.transcoderPending;

245
	},
D
Don McCurdy 已提交
246

247
	_getWorker: function () {
D
Don McCurdy 已提交
248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271

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

			if ( this.workerPool.length < this.workerLimit ) {

				var worker = new Worker( this.workerSourceURL );

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

				worker.postMessage( {
					type: 'init',
					config: this.workerConfig,
					transcoderBinary: this.transcoderBinary,
				} );

				worker.onmessage = function ( e ) {

					var message = e.data;

					switch ( message.type ) {

						case 'transcode':
272 273 274 275 276
							worker._callbacks[ message.id ].resolve( message );
							break;

						case 'error':
							worker._callbacks[ message.id ].reject( message );
D
Don McCurdy 已提交
277 278 279
							break;

						default:
280
							console.error( 'THREE.BasisTextureLoader: Unexpected message, "' + message.type + '"' );
D
Don McCurdy 已提交
281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297

					}

				}

				this.workerPool.push( worker );

			} else {

				this.workerPool.sort( function ( a, b ) { return a._taskLoad > b._taskLoad ? -1 : 1; } );

			}

			return this.workerPool[ this.workerPool.length - 1 ];

		} );

298
	},
D
Don McCurdy 已提交
299

300
	dispose: function () {
D
Don McCurdy 已提交
301 302 303 304 305 306 307 308 309

		for ( var i = 0; i < this.workerPool.length; i++ ) {

			this.workerPool[ i ].terminate();

		}

		this.workerPool.length = 0;

310 311
		return this;

D
Don McCurdy 已提交
312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362
	}
}

/* CONSTANTS */

THREE.BasisTextureLoader.BASIS_FORMAT = {
	cTFETC1: 0,
	cTFBC1: 1,
	cTFBC4: 2,
	cTFPVRTC1_4_OPAQUE_ONLY: 3,
	cTFBC7_M6_OPAQUE_ONLY: 4,
	cTFETC2: 5,
	cTFBC3: 6,
	cTFBC5: 7,
};

// DXT formats, from:
// http://www.khronos.org/registry/webgl/extensions/WEBGL_compressed_texture_s3tc/
THREE.BasisTextureLoader.DXT_FORMAT = {
	COMPRESSED_RGB_S3TC_DXT1_EXT: 0x83F0,
	COMPRESSED_RGBA_S3TC_DXT1_EXT: 0x83F1,
	COMPRESSED_RGBA_S3TC_DXT3_EXT: 0x83F2,
	COMPRESSED_RGBA_S3TC_DXT5_EXT: 0x83F3,
};
THREE.BasisTextureLoader.DXT_FORMAT_MAP = {};
THREE.BasisTextureLoader.DXT_FORMAT_MAP[ THREE.BasisTextureLoader.BASIS_FORMAT.cTFBC1 ] =
	THREE.BasisTextureLoader.DXT_FORMAT.COMPRESSED_RGB_S3TC_DXT1_EXT;
THREE.BasisTextureLoader.DXT_FORMAT_MAP[ THREE.BasisTextureLoader.BASIS_FORMAT.cTFBC3 ] =
	THREE.BasisTextureLoader.DXT_FORMAT.COMPRESSED_RGBA_S3TC_DXT5_EXT;

/* WEB WORKER */

THREE.BasisTextureLoader.BasisWorker = function () {
	var config;
	var transcoderPending;
	var _BasisFile;

	onmessage = function ( e ) {

		var message = e.data;

		switch ( message.type ) {

			case 'init':
				config = message.config;
				init( message.transcoderBinary );
				break;

			case 'transcode':
				transcoderPending.then( () => {

363
					try {
364

365
						var { width, height, mipmaps } = transcode( message.buffer );
366

367
						var buffers = [];
368

369
						for ( var i = 0; i < mipmaps.length; ++i ) {
370

371 372 373
							buffers.push( mipmaps[i].data.buffer );

						}
D
Don McCurdy 已提交
374

375 376 377 378 379 380 381 382 383
						self.postMessage( { type: 'transcode', id: message.id, width, height, mipmaps }, buffers );

					} catch ( error ) {

						console.error( error );

						self.postMessage( { type: 'error', id: message.id, error: error.message } );

					}
D
Don McCurdy 已提交
384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432

				} );
				break;

		}

	};

	function init ( wasmBinary ) {

		transcoderPending = new Promise( ( resolve ) => {

			// The 'Module' global is used by the Basis wrapper, which will check for
			// the 'wasmBinary' property before trying to load the file itself.

			// TODO(donmccurdy): This only works with a modified version of the
			// emscripten-generated wrapper. The default seems to have a bug making it
			// impossible to override the WASM binary.
			Module = { wasmBinary, onRuntimeInitialized: resolve };

		} ).then( () => {

			var { BasisFile, initializeBasis } = Module;

			_BasisFile = BasisFile;

			initializeBasis();

		} );

		createBasisModule();

	}

	function transcode ( buffer ) {

		var basisFile = new _BasisFile( new Uint8Array( buffer ) );

		var width = basisFile.getImageWidth( 0, 0 );
		var height = basisFile.getImageHeight( 0, 0 );
		var levels = basisFile.getNumLevels( 0 );

		function cleanup () {

			basisFile.close();
			basisFile.delete();

		}

433
		if ( ! width || ! height || ! levels ) {
D
Don McCurdy 已提交
434 435 436 437 438 439 440 441 442 443 444 445 446

			cleanup();
			throw new Error( 'THREE.BasisTextureLoader:  Invalid .basis file' );

		}

		if ( ! basisFile.startTranscoding() ) {

			cleanup();
			throw new Error( 'THREE.BasisTextureLoader: .startTranscoding failed' );

		}

447
		var mipmaps = [];
D
Don McCurdy 已提交
448

449
		for ( var mip = 0; mip < levels; mip++ ) {
D
Don McCurdy 已提交
450

451 452 453
			var mipWidth = basisFile.getImageWidth( 0, mip );
			var mipHeight = basisFile.getImageHeight( 0, mip );
			var dst = new Uint8Array( basisFile.getImageTranscodedSizeInBytes( 0, mip, config.format ) );
D
Don McCurdy 已提交
454

455 456 457 458 459 460 461 462 463 464 465 466 467
			var status = basisFile.transcodeImage(
				dst,
				0,
				mip,
				config.format,
				config.etcSupported ? 0 : ( config.dxtSupported ? 1 : 0 ),
				0
			);

			if ( ! status ) {

				cleanup();
				throw new Error( 'THREE.BasisTextureLoader: .transcodeImage failed.' );
D
Don McCurdy 已提交
468

469
			}
D
Don McCurdy 已提交
470

471
			mipmaps.push( { data: dst, width: mipWidth, height: mipHeight } );
D
Don McCurdy 已提交
472 473 474

		}

475 476 477
		cleanup();

		return { width, height, mipmaps };
D
Don McCurdy 已提交
478 479 480 481

	}

};