Skip to content

WebGLRenderer: Allow for binding and rendering into a 2d render target mipmap #29844

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

Merged
merged 2 commits into from
Dec 18, 2024
Merged
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
22 changes: 19 additions & 3 deletions src/renderers/WebGLRenderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -1268,7 +1268,7 @@ class WebGLRenderer {

//

if ( _currentRenderTarget !== null ) {
if ( _currentRenderTarget !== null && _currentActiveMipmapLevel === 0 ) {

// resolve multisample renderbuffers to a single-sample texture if necessary

Expand Down Expand Up @@ -2263,6 +2263,7 @@ class WebGLRenderer {

};

const _scratchFrameBuffer = _gl.createFramebuffer();
this.setRenderTarget = function ( renderTarget, activeCubeFace = 0, activeMipmapLevel = 0 ) {

_currentRenderTarget = renderTarget;
Expand Down Expand Up @@ -2371,6 +2372,14 @@ class WebGLRenderer {

}

// Use a scratch frame buffer if rendering to a mip level to avoid depth buffers
// being bound that are different sizes.
if ( activeMipmapLevel !== 0 ) {

framebuffer = _scratchFrameBuffer;

}

const framebufferBound = state.bindFramebuffer( _gl.FRAMEBUFFER, framebuffer );

if ( framebufferBound && useDefaultFramebuffer ) {
Expand All @@ -2391,8 +2400,15 @@ class WebGLRenderer {
} else if ( isRenderTarget3D ) {

const textureProperties = properties.get( renderTarget.texture );
const layer = activeCubeFace || 0;
_gl.framebufferTextureLayer( _gl.FRAMEBUFFER, _gl.COLOR_ATTACHMENT0, textureProperties.__webglTexture, activeMipmapLevel || 0, layer );
const layer = activeCubeFace;
_gl.framebufferTextureLayer( _gl.FRAMEBUFFER, _gl.COLOR_ATTACHMENT0, textureProperties.__webglTexture, activeMipmapLevel, layer );

} else if ( renderTarget !== null && activeMipmapLevel !== 0 ) {

// Only bind the frame buffer if we are using a scratch frame buffer to render to a mipmap.
// If we rebind the texture when using a multi sample buffer then an error about inconsistent samples will be thrown.
const textureProperties = properties.get( renderTarget.texture );
_gl.framebufferTexture2D( _gl.FRAMEBUFFER, _gl.COLOR_ATTACHMENT0, _gl.TEXTURE_2D, textureProperties.__webglTexture, activeMipmapLevel );

}

Expand Down