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

Fix binding __webglFramebuffer when mipmaps are used #30746

Draft
wants to merge 3 commits into
base: dev
Choose a base branch
from

Conversation

haxiomic
Copy link
Contributor

@haxiomic haxiomic commented Mar 17, 2025

Closes #30745

Related issue: #29779

Description

It's possible render into specific mipmap levels when we set the target.texture.mipmaps array. This works great for color-only render targets, however if we give this target a depth buffer (either through depthBuffer: true or an explicit depthTexture), rendering will fail with a crash when rendering

The crash is caused by three.js assuming .__webglFramebuffer is a single framebuffer, however, when .mipmaps is set it becomes an array:

if (texture.mipmaps && texture.mipmaps.length > 0) {
  for (let level = 0; level < texture.mipmaps.length; level++) {
    setupFrameBufferTexture(renderTargetProperties.__webglFramebuffer[level], renderTarget, texture, _gl.COLOR_ATTACHMENT0, glTextureType, level);
  }
} else {
  setupFrameBufferTexture(renderTargetProperties.__webglFramebuffer, renderTarget, texture, _gl.COLOR_ATTACHMENT0, glTextureType, 0);
}

This causes a crash on this line

state.bindFramebuffer( _gl.FRAMEBUFFER, renderTargetProperties.__webglFramebuffer );

And

state.bindFramebuffer( _gl.FRAMEBUFFER, framebuffer );

Maybe other places too

This PR resolves this by instead setting the depth attachment for only mipmap 0.

I will also think about setting a depth attachment for each mip level

I've tried to use idomatic three.js style which comes out quite verbose but let me know if we want something tidier!

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
Closes mrdoob#30745
@haxiomic
Copy link
Contributor Author

haxiomic commented Mar 17, 2025

Draft for now to get the ball rolling – I want to complete a working demo before I finish

Copy link

github-actions bot commented Mar 17, 2025

📦 Bundle size

Full ESM build, minified and gzipped.

Before After Diff
WebGL 336.08
78.29
336.36
78.34
+277 B
+53 B
WebGPU 525.17
146.28
525.17
146.28
+0 B
+0 B
WebGPU Nodes 524.63
146.18
524.63
146.18
+0 B
+0 B

🌳 Bundle size after tree-shaking

Minimal build including a renderer, camera, empty scene, and dependencies.

Before After Diff
WebGL 465.15
112.18
465.44
112.23
+287 B
+52 B
WebGPU 597.97
162.56
597.97
162.56
+0 B
+0 B
WebGPU Nodes 553.1
152.04
553.1
152.04
+0 B
+0 B

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Rendering into a framebuffer with .mipmaps set crashes if depth is used
1 participant