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

WebGPURenderer: Added VideoTexture support #25530

Merged
merged 9 commits into from
Mar 27, 2023
Merged
Show file tree
Hide file tree
Changes from 4 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
1 change: 1 addition & 0 deletions examples/files.json
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,7 @@
"webgpu_lights_selective",
"webgpu_loader_gltf",
"webgpu_materials",
"webgpu_materials_video",
"webgpu_nodes_playground",
"webgpu_particles",
"webgpu_rtt",
Expand Down
6 changes: 5 additions & 1 deletion examples/jsm/nodes/accessors/TextureNode.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,11 @@ class TextureNode extends UniformNode {

let snippet = null;

if ( levelNode && levelNode.isNode === true ) {
if ( texture.isVideoTexture === true ) {

snippet = builder.getVideoTexture( textureProperty, uvSnippet );

} else if ( levelNode && levelNode.isNode === true ) {

const levelSnippet = levelNode.build( builder, 'float' );

Expand Down
2 changes: 1 addition & 1 deletion examples/jsm/renderers/webgpu/WebGPUBindings.js
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ class WebGPUBindings {

}

entries.push( { binding: bindingPoint, resource: binding.textureGPU.createView( { dimension: binding.dimension } ) } );
entries.push( { binding: bindingPoint, resource: binding.textureGPU } );

}

Expand Down
59 changes: 47 additions & 12 deletions examples/jsm/renderers/webgpu/WebGPUTextures.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,15 @@ class WebGPUTextures {

//

needsUpdate = this._uploadTexture( texture );
if ( texture.isVideoTexture ) {

needsUpdate = this._uploadVideoTexture( texture );

} else {

needsUpdate = this._uploadTexture( texture );

}

}

Expand Down Expand Up @@ -296,6 +304,35 @@ class WebGPUTextures {

}

_uploadVideoTexture( texture ) {

let needsUpdate = false;

const device = this.device;

const textureProperties = this.properties.get( texture );

let textureGPU = textureProperties.textureGPU;

if ( textureGPU === undefined || textureGPU.expired ) {

textureGPU = device.importExternalTexture( {
source: texture.source.data
} );

needsUpdate = true;

}

//

textureProperties.textureGPU = textureGPU;
textureProperties.version = texture.version;

return needsUpdate;

}

_uploadTexture( texture ) {

let needsUpdate = false;
Expand Down Expand Up @@ -341,7 +378,6 @@ class WebGPUTextures {
if ( textureGPU === undefined ) {

textureGPU = device.createTexture( textureGPUDescriptor );
textureProperties.textureGPU = textureGPU;

needsUpdate = true;

Expand All @@ -367,24 +403,23 @@ class WebGPUTextures {

}

} else {
} else if ( image !== null ) {

if ( image !== null ) {
// assume HTMLImageElement, HTMLCanvasElement or ImageBitmap

// assume HTMLImageElement, HTMLCanvasElement or ImageBitmap
this._getImageBitmap( image, texture ).then( imageBitmap => {

this._getImageBitmap( image, texture ).then( imageBitmap => {
this._copyExternalImageToTexture( imageBitmap, textureGPU );

this._copyExternalImageToTexture( imageBitmap, textureGPU );
if ( needsMipmaps === true ) this._generateMipmaps( textureGPU, textureGPUDescriptor );

if ( needsMipmaps === true ) this._generateMipmaps( textureGPU, textureGPUDescriptor );

} );

}
} );

}

//

textureProperties.textureGPU = textureGPU.createView( { dimension } );
textureProperties.version = texture.version;

return needsUpdate;
Expand Down
26 changes: 25 additions & 1 deletion examples/jsm/renderers/webgpu/nodes/WebGPUNodeBuilder.js
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,20 @@ class WebGPUNodeBuilder extends NodeBuilder {

}

getVideoTexture( textureProperty, uvSnippet, shaderStage = this.shaderStage ) {

if ( shaderStage === 'fragment' ) {

return `textureSampleBaseClampToEdge( ${textureProperty}, ${textureProperty}_sampler, vec2<f32>( ${uvSnippet}.x, 1.0 - ${uvSnippet}.y ) )`;

} else {

console.error( 'WebGPURenderer: THREE.VideoTexture does not support vertex shader.' );
Copy link
Contributor

Choose a reason for hiding this comment

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

Does it support compute shader?


}

}

getPropertyName( node, shaderStage = this.shaderStage ) {

if ( node.isNodeVarying === true && node.needsInterpolation === true ) {
Expand Down Expand Up @@ -537,7 +551,17 @@ class WebGPUNodeBuilder extends NodeBuilder {

}

bindingSnippets.push( `@group( 0 ) @binding( ${index ++} ) var ${uniform.name} : texture_2d<f32>;` );
const texture = uniform.node.value;

if ( texture.isVideoTexture === true ) {

bindingSnippets.push( `@group( 0 ) @binding( ${index ++} ) var ${uniform.name} : texture_external;` );

} else {

bindingSnippets.push( `@group( 0 ) @binding( ${index ++} ) var ${uniform.name} : texture_2d<f32>;` );

}

} else if ( uniform.type === 'cubeTexture' ) {

Expand Down
Binary file added examples/screenshots/webgpu_materials_video.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading