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

WebGLRenderer: Stable reversed Z buffer implementation. #29579

Merged
merged 3 commits into from
Oct 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
14 changes: 10 additions & 4 deletions src/renderers/WebGLRenderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ class WebGLRenderer {
preserveDrawingBuffer = false,
powerPreference = 'default',
failIfMajorPerformanceCaveat = false,
reverseDepthBuffer = false,
} = parameters;

this.isWebGLRenderer = true;
Expand Down Expand Up @@ -288,9 +289,13 @@ class WebGLRenderer {

capabilities = new WebGLCapabilities( _gl, extensions, parameters, utils );

state = new WebGLState( _gl );
state = new WebGLState( _gl, extensions );

if ( capabilities.reverseDepthBuffer ) state.buffers.depth.setReversed( true );
if ( capabilities.reverseDepthBuffer && reverseDepthBuffer ) {

state.buffers.depth.setReversed( true );

}

info = new WebGLInfo( _gl );
properties = new WebGLProperties();
Expand Down Expand Up @@ -594,7 +599,6 @@ class WebGLRenderer {
if ( depth ) {

bits |= _gl.DEPTH_BUFFER_BIT;
_gl.clearDepth( this.capabilities.reverseDepthBuffer ? 0 : 1 );

}

Expand Down Expand Up @@ -1978,7 +1982,9 @@ class WebGLRenderer {

// common camera uniforms

if ( capabilities.reverseDepthBuffer ) {
const reverseDepthBuffer = state.buffers.depth.getReversed();

if ( reverseDepthBuffer ) {

_currentProjectionMatrix.copy( camera.projectionMatrix );

Expand Down
7 changes: 0 additions & 7 deletions src/renderers/webgl/WebGLCapabilities.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,13 +94,6 @@ function WebGLCapabilities( gl, extensions, parameters, utils ) {
const logarithmicDepthBuffer = parameters.logarithmicDepthBuffer === true;
const reverseDepthBuffer = parameters.reverseDepthBuffer === true && extensions.has( 'EXT_clip_control' );
Mugen87 marked this conversation as resolved.
Show resolved Hide resolved

if ( reverseDepthBuffer === true ) {

const ext = extensions.get( 'EXT_clip_control' );
ext.clipControlEXT( ext.LOWER_LEFT_EXT, ext.ZERO_TO_ONE_EXT );

}

const maxTextures = gl.getParameter( gl.MAX_TEXTURE_IMAGE_UNITS );
const maxVertexTextures = gl.getParameter( gl.MAX_VERTEX_TEXTURE_IMAGE_UNITS );
const maxTextureSize = gl.getParameter( gl.MAX_TEXTURE_SIZE );
Expand Down
2 changes: 1 addition & 1 deletion src/renderers/webgl/WebGLPrograms.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ function WebGLPrograms( renderer, cubemaps, cubeuvmaps, extensions, capabilities
const programs = [];

const logarithmicDepthBuffer = capabilities.logarithmicDepthBuffer;
const reverseDepthBuffer = capabilities.reverseDepthBuffer;
const SUPPORTS_VERTEX_TEXTURES = capabilities.vertexTextures;

let precision = capabilities.precision;
Expand Down Expand Up @@ -109,6 +108,7 @@ function WebGLPrograms( renderer, cubemaps, cubeuvmaps, extensions, capabilities
}

const currentRenderTarget = renderer.getRenderTarget();
const reverseDepthBuffer = renderer.state.buffers.depth.getReversed();

const IS_INSTANCEDMESH = object.isInstancedMesh === true;
const IS_BATCHEDMESH = object.isBatchedMesh === true;
Expand Down
38 changes: 37 additions & 1 deletion src/renderers/webgl/WebGLState.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const reversedFuncs = {
[ GreaterEqualDepth ]: LessEqualDepth,
};

function WebGLState( gl ) {
function WebGLState( gl, extensions ) {

function ColorBuffer() {

Expand Down Expand Up @@ -88,10 +88,36 @@ function WebGLState( gl ) {

setReversed: function ( value ) {

if ( reversed !== value ) {

const ext = extensions.get( 'EXT_clip_control' );

if ( reversed ) {

ext.clipControlEXT( ext.LOWER_LEFT_EXT, ext.ZERO_TO_ONE_EXT );

} else {

ext.clipControlEXT( ext.LOWER_LEFT_EXT, ext.NEGATIVE_ONE_TO_ONE_EXT );

}

const oldDepth = currentDepthClear;
currentDepthClear = null;
this.setClear( oldDepth );
Comment on lines +105 to +107
Copy link
Contributor

Choose a reason for hiding this comment

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

@eXponenta Additionally, maybe we should also do:

const oldFunc = currentDepthFunc;
currentDepthFunc = null;
this.setFunc( oldFunc );

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oops. Yep, this one.
Because we also should upgrade depth func to inverter version.


}

reversed = value;

},

getReversed: function () {

return reversed;

},

setTest: function ( depthTest ) {

if ( depthTest ) {
Expand Down Expand Up @@ -187,6 +213,12 @@ function WebGLState( gl ) {

if ( currentDepthClear !== depth ) {

if ( reversed ) {

depth = 1 - depth;

}

gl.clearDepth( depth );
currentDepthClear = depth;

Expand All @@ -201,6 +233,7 @@ function WebGLState( gl ) {
currentDepthMask = null;
currentDepthFunc = null;
currentDepthClear = null;
reversed = false;

}

Expand Down Expand Up @@ -1171,6 +1204,9 @@ function WebGLState( gl ) {

gl.depthMask( true );
gl.depthFunc( gl.LESS );

depthBuffer.setReversed( false );

gl.clearDepth( 1 );

gl.stencilMask( 0xffffffff );
Expand Down