Skip to content

Commit 3f19a05

Browse files
WebGPURenderer: Support KTX Compressed texture in the WebGL Backend (#27463)
* wip * cleanup webgl backend * cleanup and add silentError * cleanup WebGLTextureUtils * move textures to textureutils * remove handling error in hasFeatre
1 parent 1d3895d commit 3f19a05

File tree

6 files changed

+250
-165
lines changed

6 files changed

+250
-165
lines changed

examples/jsm/loaders/KTX2Loader.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -126,11 +126,11 @@ class KTX2Loader extends Loader {
126126

127127
this.workerConfig = {
128128
astcSupported: renderer.hasFeature( 'texture-compression-astc' ),
129-
etc1Supported: false,
129+
etc1Supported: renderer.hasFeature( 'texture-compression-etc1' ),
130130
etc2Supported: renderer.hasFeature( 'texture-compression-etc2' ),
131131
dxtSupported: renderer.hasFeature( 'texture-compression-bc' ),
132-
bptcSupported: false,
133-
pvrtcSupported: false
132+
bptcSupported: renderer.hasFeature( 'texture-compression-bptc' ),
133+
pvrtcSupported: renderer.hasFeature( 'texture-compression-pvrtc' )
134134
};
135135

136136
} else {

examples/jsm/renderers/webgl/WebGLBackend.js

Lines changed: 31 additions & 143 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import WebGLUtils from './utils/WebGLUtils.js';
99
import WebGLTextureUtils from './utils/WebGLTextureUtils.js';
1010
import WebGLExtensions from './utils/WebGLExtensions.js';
1111
import WebGLCapabilities from './utils/WebGLCapabilities.js';
12-
12+
import { GLFeatureName } from './utils/WebGLConstants.js';
1313
//
1414

1515
class WebGLBackend extends Backend {
@@ -22,9 +22,9 @@ class WebGLBackend extends Backend {
2222

2323
}
2424

25-
async init( renderer ) {
25+
init( renderer ) {
2626

27-
await super.init( renderer );
27+
super.init( renderer );
2828

2929
//
3030

@@ -40,7 +40,6 @@ class WebGLBackend extends Backend {
4040
this.textureUtils = new WebGLTextureUtils( this );
4141
this.state = new WebGLState( this );
4242
this.utils = new WebGLUtils( this );
43-
this.defaultTextures = {};
4443

4544
this.extensions.get( 'EXT_color_buffer_float' );
4645
this._currentContext = null;
@@ -445,7 +444,7 @@ class WebGLBackend extends Backend {
445444

446445
}
447446

448-
needsRenderUpdate( renderObject ) {
447+
needsRenderUpdate( /*renderObject*/ ) {
449448

450449
return false;
451450

@@ -459,173 +458,50 @@ class WebGLBackend extends Backend {
459458

460459
// textures
461460

462-
createSampler( /*texture*/ ) {
463-
464-
//console.warn( 'Abstract class.' );
465-
466-
}
467-
468461
createDefaultTexture( texture ) {
469462

470-
const { gl, textureUtils, defaultTextures } = this;
471-
472-
const glTextureType = textureUtils.getGLTextureType( texture );
473-
474-
let textureGPU = defaultTextures[ glTextureType ];
475-
476-
if ( textureGPU === undefined ) {
477-
478-
textureGPU = gl.createTexture();
479-
480-
gl.bindTexture( glTextureType, textureGPU );
481-
gl.texParameteri( glTextureType, gl.TEXTURE_MIN_FILTER, gl.NEAREST );
482-
gl.texParameteri( glTextureType, gl.TEXTURE_MAG_FILTER, gl.NEAREST );
483-
484-
//gl.texImage2D( target + i, 0, gl.RGBA, 1, 1, 0, gl.RGBA, gl.UNSIGNED_BYTE, data );
485-
486-
defaultTextures[ glTextureType ] = textureGPU;
487-
488-
}
489-
490-
this.set( texture, {
491-
textureGPU,
492-
glTextureType,
493-
isDefault: true
494-
} );
463+
this.textureUtils.createDefaultTexture( texture );
495464

496465
}
497466

498467
createTexture( texture, options ) {
499468

500-
const { gl, utils, textureUtils } = this;
501-
const { levels, width, height, depth } = options;
502-
503-
const glFormat = utils.convert( texture.format, texture.colorSpace );
504-
const glType = utils.convert( texture.type );
505-
const glInternalFormat = textureUtils.getInternalFormat( texture.internalFormat, glFormat, glType, texture.colorSpace, texture.isVideoTexture );
506-
507-
const textureGPU = gl.createTexture();
508-
const glTextureType = textureUtils.getGLTextureType( texture );
509-
510-
gl.bindTexture( glTextureType, textureGPU );
511-
512-
gl.pixelStorei( gl.UNPACK_FLIP_Y_WEBGL, texture.flipY );
513-
gl.pixelStorei( gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL, texture.premultiplyAlpha );
514-
gl.pixelStorei( gl.UNPACK_ALIGNMENT, texture.unpackAlignment );
515-
gl.pixelStorei( gl.UNPACK_COLORSPACE_CONVERSION_WEBGL, gl.NONE );
516-
517-
textureUtils.setTextureParameters( glTextureType, texture );
518-
519-
gl.bindTexture( glTextureType, textureGPU );
520-
521-
if ( texture.isDataArrayTexture ) {
522-
523-
gl.texStorage3D( gl.TEXTURE_2D_ARRAY, levels, glInternalFormat, width, height, depth );
524-
525-
} else if ( ! texture.isVideoTexture ) {
526-
527-
gl.texStorage2D( glTextureType, levels, glInternalFormat, width, height );
528-
529-
}
530-
531-
this.set( texture, {
532-
textureGPU,
533-
glTextureType,
534-
glFormat,
535-
glType,
536-
glInternalFormat
537-
} );
469+
this.textureUtils.createTexture( texture, options );
538470

539471
}
540472

541473
updateTexture( texture, options ) {
542474

543-
const { gl } = this;
544-
const { width, height } = options;
545-
const { textureGPU, glTextureType, glFormat, glType, glInternalFormat } = this.get( texture );
546-
547-
const getImage = ( source ) => {
548-
549-
if ( source.isDataTexture ) {
550-
551-
return source.image.data;
552-
553-
} else if ( source instanceof ImageBitmap || source instanceof OffscreenCanvas || source instanceof HTMLImageElement || source instanceof HTMLCanvasElement ) {
554-
555-
return source;
556-
557-
}
558-
559-
return source.data;
560-
561-
};
562-
563-
gl.bindTexture( glTextureType, textureGPU );
564-
565-
if ( texture.isCubeTexture ) {
566-
567-
const images = options.images;
568-
569-
for ( let i = 0; i < 6; i ++ ) {
570-
571-
const image = getImage( images[ i ] );
572-
573-
gl.texSubImage2D( gl.TEXTURE_CUBE_MAP_POSITIVE_X + i, 0, 0, 0, width, height, glFormat, glType, image );
574-
575-
}
576-
577-
} else if ( texture.isDataArrayTexture ) {
578-
579-
const image = options.image;
580-
581-
gl.texSubImage3D( gl.TEXTURE_2D_ARRAY, 0, 0, 0, 0, image.width, image.height, image.depth, glFormat, glType, image.data );
582-
583-
} else if ( texture.isVideoTexture ) {
584-
585-
texture.update();
586-
587-
gl.texImage2D( glTextureType, 0, glInternalFormat, glFormat, glType, options.image );
588-
589-
590-
} else {
591-
592-
const image = getImage( options.image );
593-
594-
gl.texSubImage2D( glTextureType, 0, 0, 0, width, height, glFormat, glType, image );
595-
596-
}
475+
this.textureUtils.updateTexture( texture, options );
597476

598477
}
599478

600479
generateMipmaps( texture ) {
601480

602-
const { gl } = this;
603-
const { textureGPU, glTextureType } = this.get( texture );
604-
605-
gl.bindTexture( glTextureType, textureGPU );
606-
gl.generateMipmap( glTextureType );
481+
this.textureUtils.generateMipmaps( texture );
607482

608483
}
609484

610485
destroyTexture( texture ) {
611486

612-
const { gl } = this;
613-
const { textureGPU } = this.get( texture );
614-
615-
gl.deleteTexture( textureGPU );
616-
617-
this.delete( texture );
487+
this.textureUtils.destroyTexture( texture );
618488

619489
}
620490

621-
destroySampler() {}
622-
623491
copyTextureToBuffer( texture, x, y, width, height ) {
624492

625493
return this.textureUtils.copyTextureToBuffer( texture, x, y, width, height );
626494

627495
}
628496

497+
createSampler( /*texture*/ ) {
498+
499+
//console.warn( 'Abstract class.' );
500+
501+
}
502+
503+
destroySampler() {}
504+
629505
// node builder
630506

631507
createNodeBuilder( object, renderer, scene = null ) {
@@ -892,12 +768,24 @@ class WebGLBackend extends Backend {
892768

893769
}
894770

895-
hasFeature( /*name*/ ) {
771+
hasFeature( name ) {
772+
773+
const keysMatching = Object.keys( GLFeatureName ).filter( key => GLFeatureName[ key ] === name );
774+
775+
const extensions = this.extensions;
776+
777+
for ( let i = 0; i < keysMatching.length; i ++ ) {
778+
896779

897-
return true;
780+
if ( extensions.has( keysMatching[ i ] ) ) return true;
781+
782+
}
783+
784+
return false;
898785

899786
}
900787

788+
901789
getMaxAnisotropy() {
902790

903791
return this.capabilities.getMaxAnisotropy();
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
export const GLFeatureName = {
2+
3+
'WEBGL_compressed_texture_astc': 'texture-compression-astc',
4+
'WEBGL_compressed_texture_etc': 'texture-compression-etc2',
5+
'WEBGL_compressed_texture_etc1': 'texture-compression-etc1',
6+
'WEBGL_compressed_texture_pvrtc': 'texture-compression-pvrtc',
7+
'WEBKIT_WEBGL_compressed_texture_pvrtc': 'texture-compression-pvrtc',
8+
'WEBGL_compressed_texture_s3tc': 'texture-compression-bc',
9+
'EXT_texture_compression_bptc': 'texture-compression-bptc',
10+
11+
};

0 commit comments

Comments
 (0)