Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
10 changes: 4 additions & 6 deletions examples/jsm/tsl/display/GaussianBlurNode.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ import { TempNode, nodeObject, Fn, If, float, NodeUpdateType, uv, uniform, conve
// WebGPU: The use of a single QuadMesh for both gaussian blur passes results in a single RenderObject with a SampledTexture binding that
// alternates between source textures and triggers creation of new BindGroups and BindGroupLayouts every frame.

const _quadMesh1 = /*@__PURE__*/ new QuadMesh();
const _quadMesh2 = /*@__PURE__*/ new QuadMesh();
const _quadMesh = /*@__PURE__*/ new QuadMesh();

let _rendererState;

Expand Down Expand Up @@ -108,8 +107,7 @@ class GaussianBlurNode extends TempNode {

const currentTexture = textureNode.value;

_quadMesh1.material = this._material;
_quadMesh2.material = this._material;
_quadMesh.material = this._material;

this.setSize( map.image.width, map.image.height );

Expand All @@ -124,7 +122,7 @@ class GaussianBlurNode extends TempNode {

this._passDirection.value.set( 1, 0 );

_quadMesh1.render( renderer );
_quadMesh.render( renderer );

// vertical

Expand All @@ -133,7 +131,7 @@ class GaussianBlurNode extends TempNode {

this._passDirection.value.set( 0, 1 );

_quadMesh2.render( renderer );
_quadMesh.render( renderer );

// restore

Expand Down
22 changes: 18 additions & 4 deletions src/renderers/common/Bindings.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class Bindings extends DataMap {

this._init( bindGroup );

this.backend.createBindings( bindGroup, bindings );
this.backend.createBindings( bindGroup, bindings, 0 );

groupData.bindGroup = bindGroup;

Expand All @@ -56,7 +56,7 @@ class Bindings extends DataMap {

this._init( bindGroup );

this.backend.createBindings( bindGroup, bindings );
this.backend.createBindings( bindGroup, bindings, 0 );

groupData.bindGroup = bindGroup;

Expand Down Expand Up @@ -116,6 +116,8 @@ class Bindings extends DataMap {
const { backend } = this;

let needsBindingsUpdate = false;
let cacheBindings = true;
let cacheIndex = 0;

// iterate over all bindings and check if buffer updates or a new binding group is required

Expand Down Expand Up @@ -145,7 +147,9 @@ class Bindings extends DataMap {

} else if ( binding.isSampledTexture ) {

if ( binding.needsBindingsUpdate( this.textures.get( binding.texture ).generation ) ) needsBindingsUpdate = true;
const texturesTextureData = this.textures.get( binding.texture );

if ( binding.needsBindingsUpdate( texturesTextureData.generation ) ) needsBindingsUpdate = true;

const updated = binding.update();

Expand All @@ -159,6 +163,16 @@ class Bindings extends DataMap {

const textureData = backend.get( texture );

if ( textureData.externalTexture !== undefined || texturesTextureData.isDefaultTexture ) {

cacheBindings = false;

} else {

cacheIndex = cacheIndex * 10 + texture.id * 10 + texture.version;
Copy link
Collaborator

Choose a reason for hiding this comment

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

What is the purpose of * 10?

Copy link
Collaborator

@RenaudRohlinger RenaudRohlinger Nov 8, 2024

Choose a reason for hiding this comment

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

Maybe some hash test that could use the NodeUtils hashArray() or hash()?

Copy link
Contributor Author

@aardgoose aardgoose Nov 8, 2024

Choose a reason for hiding this comment

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

?10 just to stop obvious collisions as a proof of concept. I am trying to avoid more string creation on a fairly hot path. A cheap hash would be better.


}

if ( backend.isWebGPUBackend === true && textureData.texture === undefined && textureData.externalTexture === undefined ) {

// TODO: Remove this once we found why updated === false isn't bound to a texture in the WebGPU backend
Expand Down Expand Up @@ -193,7 +207,7 @@ class Bindings extends DataMap {

if ( needsBindingsUpdate === true ) {

this.backend.updateBindings( bindGroup, bindings );
this.backend.updateBindings( bindGroup, bindings, cacheBindings ? cacheIndex : 0 );

}

Expand Down
8 changes: 4 additions & 4 deletions src/renderers/webgpu/WebGPUBackend.js
Original file line number Diff line number Diff line change
Expand Up @@ -1341,15 +1341,15 @@ class WebGPUBackend extends Backend {

// bindings

createBindings( bindGroup ) {
createBindings( bindGroup, bindings, cacheIndex ) {

this.bindingUtils.createBindings( bindGroup );
this.bindingUtils.createBindings( bindGroup, bindings, cacheIndex );

}

updateBindings( bindGroup ) {
updateBindings( bindGroup, bindings, cacheIndex ) {

this.bindingUtils.createBindings( bindGroup );
this.bindingUtils.createBindings( bindGroup, bindings, cacheIndex );

}

Expand Down
21 changes: 18 additions & 3 deletions src/renderers/webgpu/utils/WebGPUBindingUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ class WebGPUBindingUtils {

}

createBindings( bindGroup ) {
createBindings( bindGroup, bindings, cacheIndex ) {

const { backend, bindGroupLayoutCache } = this;
const bindingsData = backend.get( bindGroup );
Expand All @@ -156,10 +156,25 @@ class WebGPUBindingUtils {

}

const bindGroupGPU = this.createBindGroup( bindGroup, bindLayoutGPU );
let bindGroupGPU;

if ( cacheIndex > 0 ) {

if ( bindingsData.groups === undefined ) bindingsData.groups = [];

bindGroupGPU = bindingsData.groups[ cacheIndex ];

}

if ( bindGroupGPU === undefined ) {

bindGroupGPU = this.createBindGroup( bindGroup, bindLayoutGPU );
if ( cacheIndex > 0 ) bindingsData.groups[ cacheIndex ] = bindGroupGPU;

}

bindingsData.layout = bindLayoutGPU;
bindingsData.group = bindGroupGPU;
bindingsData.layout = bindLayoutGPU;

}

Expand Down