Sfoglia il codice sorgente

KTX2Loader: Support transcoding UASTC HDR to BC6H and RGBA16 (#29730)

* KTX2Loader: Support transcoding UASTC HDR to BC6H and RGBA16
* GLTFLoader: Fix incorrect texture.generateMipmaps setting
Don McCurdy 1 anno fa
parent
commit
35adad2d28

+ 1 - 1
examples/jsm/loaders/GLTFLoader.js

@@ -3230,7 +3230,7 @@ class GLTFParser {
 			texture.minFilter = WEBGL_FILTERS[ sampler.minFilter ] || LinearMipmapLinearFilter;
 			texture.wrapS = WEBGL_WRAPPINGS[ sampler.wrapS ] || RepeatWrapping;
 			texture.wrapT = WEBGL_WRAPPINGS[ sampler.wrapT ] || RepeatWrapping;
-			texture.generateMipmaps = texture.minFilter !== NearestFilter && texture.minFilter !== LinearFilter;
+			texture.generateMipmaps = ! texture.isCompressedTexture && texture.minFilter !== NearestFilter && texture.minFilter !== LinearFilter;
 
 			parser.associations.set( texture, { textures: textureIndex } );
 

+ 139 - 47
examples/jsm/loaders/KTX2Loader.js

@@ -27,6 +27,7 @@ import {
 	LinearSRGBColorSpace,
 	Loader,
 	RedFormat,
+	RGB_BPTC_UNSIGNED_Format,
 	RGB_ETC1_Format,
 	RGB_ETC2_Format,
 	RGB_PVRTC_4BPPV1_Format,
@@ -125,6 +126,7 @@ class KTX2Loader extends Loader {
 
 		this.workerConfig = {
 			astcSupported: await renderer.hasFeatureAsync( 'texture-compression-astc' ),
+			astcHDRSupported: false, // https://github.com/gpuweb/gpuweb/issues/3856
 			etc1Supported: await renderer.hasFeatureAsync( 'texture-compression-etc1' ),
 			etc2Supported: await renderer.hasFeatureAsync( 'texture-compression-etc2' ),
 			dxtSupported: await renderer.hasFeatureAsync( 'texture-compression-bc' ),
@@ -142,6 +144,7 @@ class KTX2Loader extends Loader {
 
 			this.workerConfig = {
 				astcSupported: renderer.hasFeature( 'texture-compression-astc' ),
+				astcHDRSupported: false, // https://github.com/gpuweb/gpuweb/issues/3856
 				etc1Supported: renderer.hasFeature( 'texture-compression-etc1' ),
 				etc2Supported: renderer.hasFeature( 'texture-compression-etc2' ),
 				dxtSupported: renderer.hasFeature( 'texture-compression-bc' ),
@@ -153,6 +156,8 @@ class KTX2Loader extends Loader {
 
 			this.workerConfig = {
 				astcSupported: renderer.extensions.has( 'WEBGL_compressed_texture_astc' ),
+				astcHDRSupported: renderer.extensions.has( 'WEBGL_compressed_texture_astc' )
+					&& renderer.extensions.get( 'WEBGL_compressed_texture_astc' ).getSupportedProfiles().includes( 'hdr' ),
 				etc1Supported: renderer.extensions.has( 'WEBGL_compressed_texture_etc1' ),
 				etc2Supported: renderer.extensions.has( 'WEBGL_compressed_texture_etc' ),
 				dxtSupported: renderer.extensions.has( 'WEBGL_compressed_texture_s3tc' ),
@@ -192,6 +197,7 @@ class KTX2Loader extends Loader {
 					const body = [
 						'/* constants */',
 						'let _EngineFormat = ' + JSON.stringify( KTX2Loader.EngineFormat ),
+						'let _EngineType = ' + JSON.stringify( KTX2Loader.EngineType ),
 						'let _TranscoderFormat = ' + JSON.stringify( KTX2Loader.TranscoderFormat ),
 						'let _BasisFormat = ' + JSON.stringify( KTX2Loader.BasisFormat ),
 						'/* basis_transcoder.js */',
@@ -284,23 +290,23 @@ class KTX2Loader extends Loader {
 
 	_createTextureFrom( transcodeResult, container ) {
 
-		const { faces, width, height, format, type, error, dfdFlags } = transcodeResult;
+		const { type: messageType, error, data: { faces, width, height, format, type, dfdFlags } } = transcodeResult;
 
-		if ( type === 'error' ) return Promise.reject( error );
+		if ( messageType === 'error' ) return Promise.reject( error );
 
 		let texture;
 
 		if ( container.faceCount === 6 ) {
 
-			texture = new CompressedCubeTexture( faces, format, UnsignedByteType );
+			texture = new CompressedCubeTexture( faces, format, type );
 
 		} else {
 
 			const mipmaps = faces[ 0 ].mipmaps;
 
 			texture = container.layerCount > 1
-				? new CompressedArrayTexture( mipmaps, width, height, container.layerCount, format, UnsignedByteType )
-				: new CompressedTexture( mipmaps, width, height, format, UnsignedByteType );
+				? new CompressedArrayTexture( mipmaps, width, height, container.layerCount, format, type )
+				: new CompressedTexture( mipmaps, width, height, format, type );
 
 		}
 
@@ -325,7 +331,19 @@ class KTX2Loader extends Loader {
 
 		const container = read( new Uint8Array( buffer ) );
 
-		if ( container.vkFormat !== VK_FORMAT_UNDEFINED ) {
+		// Basis UASTC HDR is a subset of ASTC, which can be transcoded efficiently
+		// to BC6H. To detect whether a KTX2 file uses Basis UASTC HDR, or default
+		// ASTC, inspect the DFD color model.
+		//
+		// Source: https://github.com/BinomialLLC/basis_universal/issues/381
+		const isBasisHDR = container.vkFormat === VK_FORMAT_ASTC_4x4_SFLOAT_BLOCK_EXT
+			&& container.dataFormatDescriptor[ 0 ].colorModel === 0xA7;
+
+		// If the device supports ASTC, Basis UASTC HDR requires no transcoder.
+		const needsTranscoder = container.vkFormat === VK_FORMAT_UNDEFINED
+			|| isBasisHDR && ! this.workerConfig.astcHDRSupported;
+
+		if ( ! needsTranscoder ) {
 
 			return createRawTexture( container );
 
@@ -364,9 +382,11 @@ class KTX2Loader extends Loader {
 
 KTX2Loader.BasisFormat = {
 	ETC1S: 0,
-	UASTC_4x4: 1,
+	UASTC: 1,
+	UASTC_HDR: 2,
 };
 
+// Source: https://github.com/BinomialLLC/basis_universal/blob/master/webgl/texture_test/index.html
 KTX2Loader.TranscoderFormat = {
 	ETC1: 0,
 	ETC2: 1,
@@ -385,11 +405,15 @@ KTX2Loader.TranscoderFormat = {
 	RGB565: 14,
 	BGR565: 15,
 	RGBA4444: 16,
+	BC6H: 22,
+	RGB_HALF: 24,
+	RGBA_HALF: 25,
 };
 
 KTX2Loader.EngineFormat = {
 	RGBAFormat: RGBAFormat,
 	RGBA_ASTC_4x4_Format: RGBA_ASTC_4x4_Format,
+	RGB_BPTC_UNSIGNED_Format: RGB_BPTC_UNSIGNED_Format,
 	RGBA_BPTC_Format: RGBA_BPTC_Format,
 	RGBA_ETC2_EAC_Format: RGBA_ETC2_EAC_Format,
 	RGBA_PVRTC_4BPPV1_Format: RGBA_PVRTC_4BPPV1_Format,
@@ -400,6 +424,11 @@ KTX2Loader.EngineFormat = {
 	RGBA_S3TC_DXT1_Format: RGBA_S3TC_DXT1_Format,
 };
 
+KTX2Loader.EngineType = {
+	UnsignedByteType: UnsignedByteType,
+	HalfFloatType: HalfFloatType,
+	FloatType: FloatType,
+};
 
 /* WEB WORKER */
 
@@ -410,6 +439,7 @@ KTX2Loader.BasisWorker = function () {
 	let BasisModule;
 
 	const EngineFormat = _EngineFormat; // eslint-disable-line no-undef
+	const EngineType = _EngineType; // eslint-disable-line no-undef
 	const TranscoderFormat = _TranscoderFormat; // eslint-disable-line no-undef
 	const BasisFormat = _BasisFormat; // eslint-disable-line no-undef
 
@@ -429,9 +459,9 @@ KTX2Loader.BasisWorker = function () {
 
 					try {
 
-						const { faces, buffers, width, height, hasAlpha, format, dfdFlags } = transcode( message.buffer );
+						const { faces, buffers, width, height, hasAlpha, format, type, dfdFlags } = transcode( message.buffer );
 
-						self.postMessage( { type: 'transcode', id: message.id, faces, width, height, hasAlpha, format, dfdFlags }, buffers );
+						self.postMessage( { type: 'transcode', id: message.id, data: { faces, width, height, hasAlpha, format, type, dfdFlags } }, buffers );
 
 					} catch ( error ) {
 
@@ -487,7 +517,26 @@ KTX2Loader.BasisWorker = function () {
 
 		}
 
-		const basisFormat = ktx2File.isUASTC() ? BasisFormat.UASTC_4x4 : BasisFormat.ETC1S;
+		let basisFormat;
+
+		if ( ktx2File.isUASTC() ) {
+
+			basisFormat = BasisFormat.UASTC;
+
+		} else if ( ktx2File.isETC1S() ) {
+
+			basisFormat = BasisFormat.ETC1S;
+
+		} else if ( ktx2File.isHDR() ) {
+
+			basisFormat = BasisFormat.UASTC_HDR;
+
+		} else {
+
+			throw new Error( 'THREE.KTX2Loader: Unknown Basis encoding' );
+
+		}
+
 		const width = ktx2File.getWidth();
 		const height = ktx2File.getHeight();
 		const layerCount = ktx2File.getLayers() || 1;
@@ -496,7 +545,7 @@ KTX2Loader.BasisWorker = function () {
 		const hasAlpha = ktx2File.getHasAlpha();
 		const dfdFlags = ktx2File.getDFDFlags();
 
-		const { transcoderFormat, engineFormat } = getTranscoderFormat( basisFormat, width, height, hasAlpha );
+		const { transcoderFormat, engineFormat, engineType } = getTranscoderFormat( basisFormat, width, height, hasAlpha );
 
 		if ( ! width || ! height || ! levelCount ) {
 
@@ -550,9 +599,15 @@ KTX2Loader.BasisWorker = function () {
 
 					}
 
-					const dst = new Uint8Array( ktx2File.getImageTranscodedSizeInBytes( mip, layer, 0, transcoderFormat ) );
+					let dst = new Uint8Array( ktx2File.getImageTranscodedSizeInBytes( mip, layer, 0, transcoderFormat ) );
 					const status = ktx2File.transcodeImage( dst, mip, layer, face, transcoderFormat, 0, - 1, - 1 );
 
+					if ( engineType === EngineType.HalfFloatType ) {
+
+						dst = new Uint16Array( dst.buffer, dst.byteOffset, dst.byteLength / Uint16Array.BYTES_PER_ELEMENT );
+
+					}
+
 					if ( ! status ) {
 
 						cleanup();
@@ -571,122 +626,159 @@ KTX2Loader.BasisWorker = function () {
 
 			}
 
-			faces.push( { mipmaps, width, height, format: engineFormat } );
+			faces.push( { mipmaps, width, height, format: engineFormat, type: engineType } );
 
 		}
 
 		cleanup();
 
-		return { faces, buffers, width, height, hasAlpha, format: engineFormat, dfdFlags };
+		return { faces, buffers, width, height, hasAlpha, dfdFlags, format: engineFormat, type: engineType };
 
 	}
 
 	//
 
-	// Optimal choice of a transcoder target format depends on the Basis format (ETC1S or UASTC),
-	// device capabilities, and texture dimensions. The list below ranks the formats separately
-	// for ETC1S and UASTC.
+	// Optimal choice of a transcoder target format depends on the Basis format (ETC1S, UASTC, or
+	// UASTC HDR), device capabilities, and texture dimensions. The list below ranks the formats
+	// separately for each format. Currently, priority is assigned based on:
 	//
-	// In some cases, transcoding UASTC to RGBA32 might be preferred for higher quality (at
-	// significant memory cost) compared to ETC1/2, BC1/3, and PVRTC. The transcoder currently
-	// chooses RGBA32 only as a last resort and does not expose that option to the caller.
+	//   high quality > low quality > uncompressed
+	//
+	// Prioritization may be revisited, or exposed for configuration, in the future.
+	//
+	// Reference: https://github.com/KhronosGroup/3D-Formats-Guidelines/blob/main/KTXDeveloperGuide.md
 	const FORMAT_OPTIONS = [
 		{
 			if: 'astcSupported',
-			basisFormat: [ BasisFormat.UASTC_4x4 ],
+			basisFormat: [ BasisFormat.UASTC ],
 			transcoderFormat: [ TranscoderFormat.ASTC_4x4, TranscoderFormat.ASTC_4x4 ],
 			engineFormat: [ EngineFormat.RGBA_ASTC_4x4_Format, EngineFormat.RGBA_ASTC_4x4_Format ],
+			engineType: [ EngineType.UnsignedByteType ],
 			priorityETC1S: Infinity,
 			priorityUASTC: 1,
 			needsPowerOfTwo: false,
 		},
 		{
 			if: 'bptcSupported',
-			basisFormat: [ BasisFormat.ETC1S, BasisFormat.UASTC_4x4 ],
+			basisFormat: [ BasisFormat.ETC1S, BasisFormat.UASTC ],
 			transcoderFormat: [ TranscoderFormat.BC7_M5, TranscoderFormat.BC7_M5 ],
 			engineFormat: [ EngineFormat.RGBA_BPTC_Format, EngineFormat.RGBA_BPTC_Format ],
+			engineType: [ EngineType.UnsignedByteType ],
 			priorityETC1S: 3,
 			priorityUASTC: 2,
 			needsPowerOfTwo: false,
 		},
 		{
 			if: 'dxtSupported',
-			basisFormat: [ BasisFormat.ETC1S, BasisFormat.UASTC_4x4 ],
+			basisFormat: [ BasisFormat.ETC1S, BasisFormat.UASTC ],
 			transcoderFormat: [ TranscoderFormat.BC1, TranscoderFormat.BC3 ],
 			engineFormat: [ EngineFormat.RGBA_S3TC_DXT1_Format, EngineFormat.RGBA_S3TC_DXT5_Format ],
+			engineType: [ EngineType.UnsignedByteType ],
 			priorityETC1S: 4,
 			priorityUASTC: 5,
 			needsPowerOfTwo: false,
 		},
 		{
 			if: 'etc2Supported',
-			basisFormat: [ BasisFormat.ETC1S, BasisFormat.UASTC_4x4 ],
+			basisFormat: [ BasisFormat.ETC1S, BasisFormat.UASTC ],
 			transcoderFormat: [ TranscoderFormat.ETC1, TranscoderFormat.ETC2 ],
 			engineFormat: [ EngineFormat.RGB_ETC2_Format, EngineFormat.RGBA_ETC2_EAC_Format ],
+			engineType: [ EngineType.UnsignedByteType ],
 			priorityETC1S: 1,
 			priorityUASTC: 3,
 			needsPowerOfTwo: false,
 		},
 		{
 			if: 'etc1Supported',
-			basisFormat: [ BasisFormat.ETC1S, BasisFormat.UASTC_4x4 ],
+			basisFormat: [ BasisFormat.ETC1S, BasisFormat.UASTC ],
 			transcoderFormat: [ TranscoderFormat.ETC1 ],
 			engineFormat: [ EngineFormat.RGB_ETC1_Format ],
+			engineType: [ EngineType.UnsignedByteType ],
 			priorityETC1S: 2,
 			priorityUASTC: 4,
 			needsPowerOfTwo: false,
 		},
 		{
 			if: 'pvrtcSupported',
-			basisFormat: [ BasisFormat.ETC1S, BasisFormat.UASTC_4x4 ],
+			basisFormat: [ BasisFormat.ETC1S, BasisFormat.UASTC ],
 			transcoderFormat: [ TranscoderFormat.PVRTC1_4_RGB, TranscoderFormat.PVRTC1_4_RGBA ],
 			engineFormat: [ EngineFormat.RGB_PVRTC_4BPPV1_Format, EngineFormat.RGBA_PVRTC_4BPPV1_Format ],
+			engineType: [ EngineType.UnsignedByteType ],
 			priorityETC1S: 5,
 			priorityUASTC: 6,
 			needsPowerOfTwo: true,
 		},
-	];
+		{
+			if: 'bptcSupported',
+			basisFormat: [ BasisFormat.UASTC_HDR ],
+			transcoderFormat: [ TranscoderFormat.BC6H ],
+			engineFormat: [ EngineFormat.RGB_BPTC_UNSIGNED_Format ],
+			engineType: [ EngineType.HalfFloatType ],
+			priorityHDR: 1,
+			needsPowerOfTwo: false,
+		},
 
-	const ETC1S_OPTIONS = FORMAT_OPTIONS.sort( function ( a, b ) {
+		// Uncompressed fallbacks.
 
-		return a.priorityETC1S - b.priorityETC1S;
+		{
+			basisFormat: [ BasisFormat.ETC1S, BasisFormat.UASTC ],
+			transcoderFormat: [ TranscoderFormat.RGBA32, TranscoderFormat.RGBA32 ],
+			engineFormat: [ EngineFormat.RGBAFormat, EngineFormat.RGBAFormat ],
+			engineType: [ EngineType.UnsignedByteType, EngineType.UnsignedByteType ],
+			priorityETC1S: 100,
+			priorityUASTC: 100,
+			needsPowerOfTwo: false,
+		},
+		{
+			basisFormat: [ BasisFormat.UASTC_HDR ],
+			transcoderFormat: [ TranscoderFormat.RGBA_HALF ],
+			engineFormat: [ EngineFormat.RGBAFormat ],
+			engineType: [ EngineType.HalfFloatType ],
+			priorityHDR: 100,
+			needsPowerOfTwo: false,
+		}
+	];
 
-	} );
-	const UASTC_OPTIONS = FORMAT_OPTIONS.sort( function ( a, b ) {
+	const OPTIONS = {
+		// TODO: For ETC1S we intentionally sort by _UASTC_ priority, preserving
+		// a historical accident shown to avoid performance pitfalls for Linux with
+		// Firefox & AMD GPU (RadeonSI). Further work needed.
+		// See https://github.com/mrdoob/three.js/pull/29730.
+		[ BasisFormat.ETC1S ]: FORMAT_OPTIONS
+			.filter( ( opt ) => opt.basisFormat.includes( BasisFormat.ETC1S ) )
+			.sort( ( a, b ) => a.priorityUASTC - b.priorityUASTC ),
 
-		return a.priorityUASTC - b.priorityUASTC;
+		[ BasisFormat.UASTC ]: FORMAT_OPTIONS
+			.filter( ( opt ) => opt.basisFormat.includes( BasisFormat.UASTC ) )
+			.sort( ( a, b ) => a.priorityUASTC - b.priorityUASTC ),
 
-	} );
+		[ BasisFormat.UASTC_HDR ]: FORMAT_OPTIONS
+			.filter( ( opt ) => opt.basisFormat.includes( BasisFormat.UASTC_HDR ) )
+			.sort( ( a, b ) => a.priorityHDR - b.priorityHDR ),
+	};
 
 	function getTranscoderFormat( basisFormat, width, height, hasAlpha ) {
 
-		let transcoderFormat;
-		let engineFormat;
-
-		const options = basisFormat === BasisFormat.ETC1S ? ETC1S_OPTIONS : UASTC_OPTIONS;
+		const options = OPTIONS[ basisFormat ];
 
 		for ( let i = 0; i < options.length; i ++ ) {
 
 			const opt = options[ i ];
 
-			if ( ! config[ opt.if ] ) continue;
+			if ( opt.if && ! config[ opt.if ] ) continue;
 			if ( ! opt.basisFormat.includes( basisFormat ) ) continue;
 			if ( hasAlpha && opt.transcoderFormat.length < 2 ) continue;
 			if ( opt.needsPowerOfTwo && ! ( isPowerOfTwo( width ) && isPowerOfTwo( height ) ) ) continue;
 
-			transcoderFormat = opt.transcoderFormat[ hasAlpha ? 1 : 0 ];
-			engineFormat = opt.engineFormat[ hasAlpha ? 1 : 0 ];
+			const transcoderFormat = opt.transcoderFormat[ hasAlpha ? 1 : 0 ];
+			const engineFormat = opt.engineFormat[ hasAlpha ? 1 : 0 ];
+			const engineType = opt.engineType[ 0 ];
 
-			return { transcoderFormat, engineFormat };
+			return { transcoderFormat, engineFormat, engineType };
 
 		}
 
-		console.warn( 'THREE.KTX2Loader: No suitable compressed texture format found. Decoding to RGBA32.' );
-
-		transcoderFormat = TranscoderFormat.RGBA32;
-		engineFormat = EngineFormat.RGBAFormat;
-
-		return { transcoderFormat, engineFormat };
+		throw new Error( 'THREE.KTX2Loader: Failed to identify transcoding target.' );
 
 	}
 

BIN
examples/textures/compressed/2d_rgba16_uastc_hdr_linear.ktx2


+ 3 - 0
examples/webgl_loader_texture_ktx2.html

@@ -40,6 +40,7 @@
 				'RGBA8 Linear': '2d_rgba8_linear.ktx2',
 				// 'RGBA8 Display P3': '2d_rgba8_displayp3.ktx2',
 				'RGBA16 Linear': '2d_rgba16_linear.ktx2',
+				'RGBA16 Linear (UASTC HDR)': '2d_rgba16_uastc_hdr_linear.ktx2',
 				'RGBA32 Linear': '2d_rgba32_linear.ktx2',
 				'ASTC 6x6 (mobile)': '2d_astc_6x6.ktx2',
 			};
@@ -47,7 +48,9 @@
 			const FORMAT_LABELS = {
 				[ THREE.RGBAFormat ]: 'RGBA',
 				[ THREE.RGBA_BPTC_Format ]: 'RGBA_BPTC',
+				[ THREE.RGB_BPTC_UNSIGNED_Format ]: 'RGB_BPTC_UNSIGNED',
 				[ THREE.RGBA_ASTC_4x4_Format ]: 'RGBA_ASTC_4x4',
+				[ THREE.RGBA_ASTC_6x6_Format ]: 'RGBA_ASTC_6x6',
 				[ THREE.RGB_S3TC_DXT1_Format ]: 'RGB_S3TC_DXT1',
 				[ THREE.RGBA_S3TC_DXT5_Format ]: 'RGBA_S3TC_DXT5',
 				[ THREE.RGB_PVRTC_4BPPV1_Format ]: 'RGB_PVRTC_4BPPV1',

粤ICP备19079148号