Skip to content

Commit

Permalink
first pass minimise binds
Browse files Browse the repository at this point in the history
  • Loading branch information
aardgoose committed Oct 23, 2023
1 parent 9d26154 commit 1a05aa9
Showing 1 changed file with 66 additions and 28 deletions.
94 changes: 66 additions & 28 deletions examples/jsm/renderers/webgl/WebGLBackend.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ class WebGLBackend extends Backend {

this.extensions.get( 'EXT_color_buffer_float' );

this.maxTexture = this.gl.getParameter( this.gl.MAX_TEXTURE_IMAGE_UNITS );
this.activeTextureUnit = null;
this.textureBindings = Array( this.maxTexture );

}

get coordinateSystem() {
Expand Down Expand Up @@ -279,8 +283,7 @@ class WebGLBackend extends Backend {

} else if ( binding.isSampledTexture ) {

gl.activeTexture( gl.TEXTURE0 + index );
gl.bindTexture( bindingData.glTextureType, bindingData.textureGPU );
this._bindTexture( binding.texture, index );

}

Expand Down Expand Up @@ -422,12 +425,26 @@ class WebGLBackend extends Backend {
const glTextureType = textureUtils.getGLTextureType( texture );

let textureGPU = defaultTextures[ glTextureType ];
let firstUse = false;

if ( textureGPU === undefined ) {

textureGPU = gl.createTexture();
firstUse = true;

}

this.set( texture, {
textureGPU,
glTextureType,
isDefault: true,
unit: null
} );

if ( firstUse ) {

this._bindTexture( texture );

gl.bindTexture( glTextureType, textureGPU );
gl.texParameteri( glTextureType, gl.TEXTURE_MIN_FILTER, gl.NEAREST );
gl.texParameteri( glTextureType, gl.TEXTURE_MAG_FILTER, gl.NEAREST );

Expand All @@ -437,12 +454,6 @@ class WebGLBackend extends Backend {

}

this.set( texture, {
textureGPU,
glTextureType,
isDefault: true
} );

}

createTexture( texture, options ) {
Expand All @@ -457,7 +468,16 @@ class WebGLBackend extends Backend {
const textureGPU = gl.createTexture();
const glTextureType = textureUtils.getGLTextureType( texture );

gl.bindTexture( glTextureType, textureGPU );
this.set( texture, {
textureGPU,
glTextureType,
glFormat,
glType,
glInternalFormat,
unit: null
} );

this._bindTexture( texture );

gl.pixelStorei( gl.UNPACK_FLIP_Y_WEBGL, texture.flipY );
gl.pixelStorei( gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL, texture.premultiplyAlpha );
Expand All @@ -466,22 +486,12 @@ class WebGLBackend extends Backend {

textureUtils.setTextureParameters( glTextureType, texture );

gl.bindTexture( glTextureType, textureGPU );

if ( ! texture.isVideoTexture ) {

gl.texStorage2D( glTextureType, levels, glInternalFormat, width, height );

}

this.set( texture, {
textureGPU,
glTextureType,
glFormat,
glType,
glInternalFormat
} );

}

updateTexture( texture, options ) {
Expand All @@ -506,7 +516,7 @@ class WebGLBackend extends Backend {

};

gl.bindTexture( glTextureType, textureGPU );
this._bindTexture( texture );

if ( texture.isCubeTexture ) {

Expand Down Expand Up @@ -540,9 +550,10 @@ class WebGLBackend extends Backend {
generateMipmaps( texture ) {

const { gl } = this;
const { textureGPU, glTextureType } = this.get( texture );
const { glTextureType } = this.get( texture );

this._bindTexture( texture );

gl.bindTexture( glTextureType, textureGPU );
gl.generateMipmap( glTextureType );

}
Expand Down Expand Up @@ -834,17 +845,14 @@ class WebGLBackend extends Backend {

const { gl } = this;

const { textureGPU } = this.get( texture );

gl.bindFramebuffer( gl.FRAMEBUFFER, null );
gl.bindTexture( gl.TEXTURE_2D, textureGPU );

this._bindTexture( texture );

gl.copyTexSubImage2D( gl.TEXTURE_2D, 0, 0, 0, 0, 0, texture.image.width, texture.image.height );

if ( texture.generateMipmaps ) gl.generateMipmap( gl.TEXTURE_2D );

gl.bindTexture( gl.TEXTURE_2D, null );

}

_setFramebuffer( renderContext ) {
Expand Down Expand Up @@ -906,6 +914,36 @@ class WebGLBackend extends Backend {

}

_bindTexture( texture, unit ) {

const textureData = this.get( texture );

const { gl, textureBindings, maxTexture } = this;
const { glTextureType, textureGPU } = textureData;

if ( unit === undefined ) unit = textureData.unit === null ? maxTexture - 1 : textureData.unit;

if ( this.activeTextureUnit !== unit ) {

gl.activeTexture( gl.TEXTURE0 + unit );
this.activeTextureUnit = unit;

}

if ( textureBindings[ unit ] !== textureGPU ) {

const oldBinding = textureBindings[ unit ];
if ( oldBinding !== undefined ) oldBinding.unit = null;

gl.bindTexture( glTextureType, textureGPU );

textureBindings[ unit ] = textureGPU;
textureData.unit = unit;

}

}

}

export default WebGLBackend;

0 comments on commit 1a05aa9

Please sign in to comment.