Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WebGPURenderer: WebGL State improvements #29132

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 35 additions & 14 deletions src/renderers/webgl-fallback/WebGLBackend.js
Original file line number Diff line number Diff line change
Expand Up @@ -490,16 +490,16 @@ class WebGLBackend extends Backend {

beginCompute( computeGroup ) {

const gl = this.gl;
const { state, gl } = this;

gl.bindFramebuffer( gl.FRAMEBUFFER, null );
state.bindFramebuffer( gl.FRAMEBUFFER, null );
this.initTimestampQuery( computeGroup );

}

compute( computeGroup, computeNode, bindings, pipeline ) {

const gl = this.gl;
const { state, gl } = this;

if ( ! this.discard ) {

Expand All @@ -525,7 +525,7 @@ class WebGLBackend extends Backend {

}

gl.useProgram( programGPU );
state.useProgram( programGPU );

this._bindUniforms( bindings );

Expand Down Expand Up @@ -595,7 +595,7 @@ class WebGLBackend extends Backend {

state.setMaterial( material, frontFaceCW );

gl.useProgram( programGPU );
state.useProgram( programGPU );

//

Expand Down Expand Up @@ -974,7 +974,7 @@ class WebGLBackend extends Backend {

_completeCompile( renderObject, pipeline ) {

const gl = this.gl;
const { state, gl } = this;
const pipelineData = this.get( pipeline );
const { programGPU, fragmentShader, vertexShader } = pipelineData;

Expand All @@ -984,7 +984,7 @@ class WebGLBackend extends Backend {

}

gl.useProgram( programGPU );
state.useProgram( programGPU );

// Bindings

Expand All @@ -1002,7 +1002,7 @@ class WebGLBackend extends Backend {

createComputePipeline( computePipeline, bindings ) {

const gl = this.gl;
const { state, gl } = this;

// Program

Expand Down Expand Up @@ -1052,7 +1052,7 @@ class WebGLBackend extends Backend {

}

gl.useProgram( programGPU );
state.useProgram( programGPU );

// Bindings

Expand Down Expand Up @@ -1104,7 +1104,7 @@ class WebGLBackend extends Backend {

updateBindings( bindGroup, bindings ) {

const { gl } = this;
const { state, gl } = this;

let groupIndex = 0;
let textureIndex = 0;
Expand All @@ -1120,7 +1120,7 @@ class WebGLBackend extends Backend {

gl.bindBuffer( gl.UNIFORM_BUFFER, bufferGPU );
gl.bufferData( gl.UNIFORM_BUFFER, data, gl.DYNAMIC_DRAW );
gl.bindBufferBase( gl.UNIFORM_BUFFER, groupIndex, bufferGPU );
state.bindBufferBase( gl.UNIFORM_BUFFER, groupIndex, bufferGPU );

this.set( binding, {
index: groupIndex ++,
Expand Down Expand Up @@ -1519,7 +1519,7 @@ class WebGLBackend extends Backend {

}

const gl = this.gl;
const { state, gl } = this;

transformFeedbackGPU = gl.createTransformFeedback();

Expand All @@ -1529,7 +1529,7 @@ class WebGLBackend extends Backend {

const attributeData = transformBuffers[ i ];

gl.bindBufferBase( gl.TRANSFORM_FEEDBACK_BUFFER, i, attributeData.transformBuffer );
state.bindBufferBase( gl.TRANSFORM_FEEDBACK_BUFFER, i, attributeData.transformBuffer );

}

Expand Down Expand Up @@ -1571,6 +1571,26 @@ class WebGLBackend extends Backend {

}


_bindBufferRange( binding, offset, size ) {

const { gl } = this;
const bindingData = this.get( binding );
const index = bindingData.index;

if ( binding.isUniformsGroup || binding.isUniformBuffer ) {

gl.bindBufferRange( gl.UNIFORM_BUFFER, index, bindingData.bufferGPU, offset, size );

} else {

console.warn( 'bindBufferRange is only applicable for uniform buffers' );
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be good to always add the component to the message that logs it. E.g.:

console.warn( 'THREE.WebGLBackend: bindBufferRange() is only applicable for uniform buffers.' );

Copy link
Collaborator Author

@RenaudRohlinger RenaudRohlinger Aug 14, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops, actually _bindBufferRange was a method I forgot to cleanup that I will work on later on, also I don't think internal warning is useful. Thanks for the heads up!


}

}


_bindUniforms( bindings ) {

const { gl, state } = this;
Expand All @@ -1584,7 +1604,8 @@ class WebGLBackend extends Backend {

if ( binding.isUniformsGroup || binding.isUniformBuffer ) {

gl.bindBufferBase( gl.UNIFORM_BUFFER, index, bindingData.bufferGPU );
// TODO USE bindBufferRange to group multiple uniform buffers
state.bindBufferBase( gl.UNIFORM_BUFFER, index, bindingData.bufferGPU );

} else if ( binding.isSampledTexture ) {

Expand Down
21 changes: 21 additions & 0 deletions src/renderers/webgl-fallback/utils/WebGLState.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ class WebGLState {
this.maxTextures = this.gl.getParameter( this.gl.MAX_TEXTURE_IMAGE_UNITS );
this.currentTextureSlot = null;
this.currentBoundTextures = {};
this.currentBoundBufferBases = {};

if ( initialized === false ) {

Expand Down Expand Up @@ -716,6 +717,26 @@ class WebGLState {

}

bindBufferBase( target, index, buffer ) {

const { gl } = this;

const key = `${target}-${index}`;

if ( this.currentBoundBufferBases[ key ] !== buffer ) {

gl.bindBufferBase( target, index, buffer );
this.currentBoundBufferBases[ key ] = buffer;

return true;

}

return false;

}


unbindTexture() {

const { gl, currentTextureSlot, currentBoundTextures } = this;
Expand Down
Loading