Skip to content

Commit 1075735

Browse files
eXponentaMugen87
andauthored
WebGLRenderer: Stable reversed Z buffer implementation. (#29579)
* fix: stable reversed Z buffer implementation Fix: reset clip state when reset is called Fix: valid depth clear value when reversed is enabled Feat: non-persistent reversedZ state ( can be controlled via renderer.state.buffers.depth.setReversed( ))) * fix: lint + misstakes fix: program parameters * Update WebGLCapabilities.js --------- Co-authored-by: Michael Herzog <[email protected]>
1 parent 167c022 commit 1075735

File tree

4 files changed

+48
-13
lines changed

4 files changed

+48
-13
lines changed

src/renderers/WebGLRenderer.js

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ class WebGLRenderer {
7070
preserveDrawingBuffer = false,
7171
powerPreference = 'default',
7272
failIfMajorPerformanceCaveat = false,
73+
reverseDepthBuffer = false,
7374
} = parameters;
7475

7576
this.isWebGLRenderer = true;
@@ -288,9 +289,13 @@ class WebGLRenderer {
288289

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

291-
state = new WebGLState( _gl );
292+
state = new WebGLState( _gl, extensions );
292293

293-
if ( capabilities.reverseDepthBuffer ) state.buffers.depth.setReversed( true );
294+
if ( capabilities.reverseDepthBuffer && reverseDepthBuffer ) {
295+
296+
state.buffers.depth.setReversed( true );
297+
298+
}
294299

295300
info = new WebGLInfo( _gl );
296301
properties = new WebGLProperties();
@@ -594,7 +599,6 @@ class WebGLRenderer {
594599
if ( depth ) {
595600

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

599603
}
600604

@@ -1978,7 +1982,9 @@ class WebGLRenderer {
19781982

19791983
// common camera uniforms
19801984

1981-
if ( capabilities.reverseDepthBuffer ) {
1985+
const reverseDepthBuffer = state.buffers.depth.getReversed();
1986+
1987+
if ( reverseDepthBuffer ) {
19821988

19831989
_currentProjectionMatrix.copy( camera.projectionMatrix );
19841990

src/renderers/webgl/WebGLCapabilities.js

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -94,13 +94,6 @@ function WebGLCapabilities( gl, extensions, parameters, utils ) {
9494
const logarithmicDepthBuffer = parameters.logarithmicDepthBuffer === true;
9595
const reverseDepthBuffer = parameters.reverseDepthBuffer === true && extensions.has( 'EXT_clip_control' );
9696

97-
if ( reverseDepthBuffer === true ) {
98-
99-
const ext = extensions.get( 'EXT_clip_control' );
100-
ext.clipControlEXT( ext.LOWER_LEFT_EXT, ext.ZERO_TO_ONE_EXT );
101-
102-
}
103-
10497
const maxTextures = gl.getParameter( gl.MAX_TEXTURE_IMAGE_UNITS );
10598
const maxVertexTextures = gl.getParameter( gl.MAX_VERTEX_TEXTURE_IMAGE_UNITS );
10699
const maxTextureSize = gl.getParameter( gl.MAX_TEXTURE_SIZE );

src/renderers/webgl/WebGLPrograms.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ function WebGLPrograms( renderer, cubemaps, cubeuvmaps, extensions, capabilities
1414
const programs = [];
1515

1616
const logarithmicDepthBuffer = capabilities.logarithmicDepthBuffer;
17-
const reverseDepthBuffer = capabilities.reverseDepthBuffer;
1817
const SUPPORTS_VERTEX_TEXTURES = capabilities.vertexTextures;
1918

2019
let precision = capabilities.precision;
@@ -109,6 +108,7 @@ function WebGLPrograms( renderer, cubemaps, cubeuvmaps, extensions, capabilities
109108
}
110109

111110
const currentRenderTarget = renderer.getRenderTarget();
111+
const reverseDepthBuffer = renderer.state.buffers.depth.getReversed();
112112

113113
const IS_INSTANCEDMESH = object.isInstancedMesh === true;
114114
const IS_BATCHEDMESH = object.isBatchedMesh === true;

src/renderers/webgl/WebGLState.js

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ const reversedFuncs = {
1414
[ GreaterEqualDepth ]: LessEqualDepth,
1515
};
1616

17-
function WebGLState( gl ) {
17+
function WebGLState( gl, extensions ) {
1818

1919
function ColorBuffer() {
2020

@@ -88,10 +88,36 @@ function WebGLState( gl ) {
8888

8989
setReversed: function ( value ) {
9090

91+
if ( reversed !== value ) {
92+
93+
const ext = extensions.get( 'EXT_clip_control' );
94+
95+
if ( reversed ) {
96+
97+
ext.clipControlEXT( ext.LOWER_LEFT_EXT, ext.ZERO_TO_ONE_EXT );
98+
99+
} else {
100+
101+
ext.clipControlEXT( ext.LOWER_LEFT_EXT, ext.NEGATIVE_ONE_TO_ONE_EXT );
102+
103+
}
104+
105+
const oldDepth = currentDepthClear;
106+
currentDepthClear = null;
107+
this.setClear( oldDepth );
108+
109+
}
110+
91111
reversed = value;
92112

93113
},
94114

115+
getReversed: function () {
116+
117+
return reversed;
118+
119+
},
120+
95121
setTest: function ( depthTest ) {
96122

97123
if ( depthTest ) {
@@ -187,6 +213,12 @@ function WebGLState( gl ) {
187213

188214
if ( currentDepthClear !== depth ) {
189215

216+
if ( reversed ) {
217+
218+
depth = 1 - depth;
219+
220+
}
221+
190222
gl.clearDepth( depth );
191223
currentDepthClear = depth;
192224

@@ -201,6 +233,7 @@ function WebGLState( gl ) {
201233
currentDepthMask = null;
202234
currentDepthFunc = null;
203235
currentDepthClear = null;
236+
reversed = false;
204237

205238
}
206239

@@ -1171,6 +1204,9 @@ function WebGLState( gl ) {
11711204

11721205
gl.depthMask( true );
11731206
gl.depthFunc( gl.LESS );
1207+
1208+
depthBuffer.setReversed( false );
1209+
11741210
gl.clearDepth( 1 );
11751211

11761212
gl.stencilMask( 0xffffffff );

0 commit comments

Comments
 (0)