Skip to content

WebGPURenderer: Persistent video texture approach #31416

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 1 commit into from
Jul 15, 2025
Merged

Conversation

sunag
Copy link
Collaborator

@sunag sunag commented Jul 15, 2025

Closes #31330, #31397

Description

Replaces texture_external with a persistent texture using copyExternalImageToTexture() updating by requestVideoFrameCallback of VideoTexture, reusing all of the core's texture management, including sharing between materials, and adds support for vertex stage usage too.

@Mugen87 I think this is the best way, in addition to the benefits, it reduces the total code.

Copy link

📦 Bundle size

Full ESM build, minified and gzipped.

Before After Diff
WebGL 338.28
78.93
338.28
78.93
+0 B
+0 B
WebGPU 558.52
154.58
557.63
154.34
-893 B
-248 B
WebGPU Nodes 557.45
154.37
556.55
154.12
-893 B
-249 B

🌳 Bundle size after tree-shaking

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

Before After Diff
WebGL 469.62
113.66
469.62
113.66
+0 B
+0 B
WebGPU 634.18
171.62
633.28
171.4
-892 B
-220 B
WebGPU Nodes 589.31
160.95
588.42
160.71
-892 B
-239 B

@mrdoob mrdoob added this to the r179 milestone Jul 15, 2025
@Mugen87
Copy link
Collaborator

Mugen87 commented Jul 15, 2025

Yes, that sounds like a good solution 👍 .

Are parts of the PR missing though? I don't see any changes to VideoTexture or usage of copyExternalImageToTexture()?

@sunag sunag marked this pull request as ready for review July 15, 2025 14:40
@sunag
Copy link
Collaborator Author

sunag commented Jul 15, 2025

Great, right? The current texture system was reused, and most things worked auto-magically.

@sunag sunag merged commit b38ac17 into mrdoob:dev Jul 15, 2025
13 of 14 checks passed
@sunag sunag deleted the dev-video branch July 15, 2025 14:56
@Mugen87
Copy link
Collaborator

Mugen87 commented Jul 15, 2025

Ah I see, the code goes now in the WebGPUTextureUtils._copyImageToTexture() branch. Neat!

@Mugen87
Copy link
Collaborator

Mugen87 commented Jul 15, 2025

It seems this PR broke VideoFrameTexture in webgpu_video_frame when using WebGPU. The WebGL backend works fine.

@ycw
Copy link
Contributor

ycw commented Jul 17, 2025

This introduced copy overheads that would be hamstringed by LPDDR bandwidth in mobiles.

@Mugen87
Copy link
Collaborator

Mugen87 commented Jul 17, 2025

This introduced copy overheads that would be hamstringed by LPDDR bandwidth in mobiles.

@greggman We were hoping to simplify our code base by not using texture_external and importExternalTexture() for video textures in WebGPURenderer. Instead the idea is to copy the video frame via copyExternalImageToTexture() and then treat it as a normal texture.

Does this approach via copyExternalImageToTexture() indeed introduce a performance issue?

@greggman
Copy link
Contributor

Does this approach via copyExternalImageToTexture() indeed introduce a performance issue?

I'm not really sure how to define "performance issue" 😛

copying is slower than not copying. importExternalTexture is designed to allow no copy. Conceptually you're directly using some external texture. That said, it's up to the implementation whether or not it lets you directly use some external texture or makes a temporary copy.

In Chrome, if you enable webgpu-developer-features, then GPUExternalTexture has a isZeroCopy boolean you can use to see if it's using the external texture directly or making a temporary copy. This is affected by platform/OS/gpu/video format.

copyExternalImageToTexture on the other hand, definitely always makes a copy. This is the same as texImage2D(,,,, someElement) which also always makes a copy. Whether either is fast (GPU based copy) or slow (CPU based copy) up to the implementation. That said, making a copy was your only option in WebGL and people have been using video there so it's not bad/wrong to use this path. And, as you discovered, it's easier and more flexible since you don't have to change any shaders.

@Mugen87
Copy link
Collaborator

Mugen87 commented Jul 17, 2025

Then I would say we keep the status quo and see how it performs in production. Maintaining a separate code path for video textures in the WebGPU backend was a bit of a pain and the new approach is so much more elegant.

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.

ProjectorLight : VideoTexture projected on multiple meshes fails with multiple "Destroyed texture" warnings
5 participants