From dc3b68e923127b96a26d7d2a9e6f6f6ac56501b6 Mon Sep 17 00:00:00 2001 From: Renaud Rohlinger Date: Wed, 15 Jan 2025 18:00:32 +0900 Subject: [PATCH] WebGPURenderer: Fix clear alpha in WebGLBackend --- src/renderers/common/Renderer.js | 2 + src/renderers/webgl-fallback/WebGLBackend.js | 24 +++---- .../webgl-fallback/utils/WebGLState.js | 69 +++++++++++++++++++ 3 files changed, 79 insertions(+), 16 deletions(-) diff --git a/src/renderers/common/Renderer.js b/src/renderers/common/Renderer.js index 3291b833ac9063..02b1ff7f1a1b41 100644 --- a/src/renderers/common/Renderer.js +++ b/src/renderers/common/Renderer.js @@ -1855,6 +1855,8 @@ class Renderer { } + renderContext.clearColorValue = this._clearColor; + this.backend.clear( color, depth, stencil, renderContext ); if ( renderTarget !== null && this._renderTarget === null ) { diff --git a/src/renderers/webgl-fallback/WebGLBackend.js b/src/renderers/webgl-fallback/WebGLBackend.js index 53924bc581f7e2..65a0379377c17f 100644 --- a/src/renderers/webgl-fallback/WebGLBackend.js +++ b/src/renderers/webgl-fallback/WebGLBackend.js @@ -410,7 +410,7 @@ class WebGLBackend extends Backend { */ beginRender( renderContext ) { - const { gl } = this; + const { state, gl } = this; const renderContextData = this.get( renderContext ); // @@ -433,7 +433,7 @@ class WebGLBackend extends Backend { } else { - gl.viewport( 0, 0, gl.drawingBufferWidth, gl.drawingBufferHeight ); + state.viewport( 0, 0, gl.drawingBufferWidth, gl.drawingBufferHeight ); } @@ -441,7 +441,7 @@ class WebGLBackend extends Backend { const { x, y, width, height } = renderContext.scissorValue; - gl.scissor( x, renderContext.height - height - y, width, height ); + state.scissor( x, renderContext.height - height - y, width, height ); } @@ -565,7 +565,7 @@ class WebGLBackend extends Backend { } else { - gl.viewport( 0, 0, gl.drawingBufferWidth, gl.drawingBufferHeight ); + state.viewport( 0, 0, gl.drawingBufferWidth, gl.drawingBufferHeight ); } @@ -663,10 +663,10 @@ class WebGLBackend extends Backend { */ updateViewport( renderContext ) { - const gl = this.gl; + const { state } = this; const { x, y, width, height } = renderContext.viewportValue; - gl.viewport( x, renderContext.height - height - y, width, height ); + state.viewport( x, renderContext.height - height - y, width, height ); } @@ -677,17 +677,9 @@ class WebGLBackend extends Backend { */ setScissorTest( boolean ) { - const gl = this.gl; - - if ( boolean ) { + const state = this.state; - gl.enable( gl.SCISSOR_TEST ); - - } else { - - gl.disable( gl.SCISSOR_TEST ); - - } + state.setScissorTest( boolean ); } diff --git a/src/renderers/webgl-fallback/utils/WebGLState.js b/src/renderers/webgl-fallback/utils/WebGLState.js index c623c82c072e04..e6b9533301de79 100644 --- a/src/renderers/webgl-fallback/utils/WebGLState.js +++ b/src/renderers/webgl-fallback/utils/WebGLState.js @@ -6,6 +6,7 @@ import { OneMinusSrcColorFactor, OneMinusSrcAlphaFactor, OneMinusDstColorFactor, OneMinusDstAlphaFactor, NeverDepth, AlwaysDepth, LessDepth, LessEqualDepth, EqualDepth, GreaterEqualDepth, GreaterDepth, NotEqualDepth } from '../../../constants.js'; +import { Vector4 } from '../../../math/Vector4.js'; let initialized = false, equationToGL, factorToGL; @@ -121,6 +122,14 @@ class WebGLState { [ OneMinusDstAlphaFactor ]: gl.ONE_MINUS_DST_ALPHA }; + const scissorParam = gl.getParameter( gl.SCISSOR_BOX ); + const viewportParam = gl.getParameter( gl.VIEWPORT ); + + this.currentScissor = new Vector4().fromArray( scissorParam ); + this.currentViewport = new Vector4().fromArray( viewportParam ); + this._tempScissor = new Vector4(); + this._tempViewport = new Vector4(); + } /** @@ -543,6 +552,66 @@ class WebGLState { } + scissor( x, y, z, w ) { + + const scissor = this._tempScissor.set( x, y, z, w ); + + if ( this.currentScissor.equals( scissor ) === false ) { + + const { gl } = this; + + gl.scissor( scissor.x, scissor.y, scissor.z, scissor.w ); + this.currentScissor.copy( scissor ); + + } + + } + + /** + * Specifies the viewport. + * + * @param {Number} x - The x-coordinate of the lower left corner of the viewport. + * @param {Number} y - The y-coordinate of the lower left corner of the viewport. + * @param {Number} width - The width of the viewport. + * @param {Number} height - The height of the viewport. + * + */ + viewport( x, y, width, height ) { + + const viewport = this._tempScissor.set( x, y, width, height ); + + if ( this.currentViewport.equals( viewport ) === false ) { + + const { gl } = this; + + gl.viewport( viewport.x, viewport.y, viewport.z, viewport.w ); + this.currentViewport.copy( viewport ); + + } + + } + + /** + * Defines the scissor test. + * + * @param {Boolean} boolean - Whether the scissor test should be enabled or not. + */ + setScissorTest( boolean ) { + + const gl = this.gl; + + if ( boolean ) { + + gl.enable( gl.SCISSOR_TEST ); + + } else { + + gl.disable( gl.SCISSOR_TEST ); + + } + + } + /** * Specifies whether the stencil test is enabled or not. *