Skip to content

WebGPURenderer: Reduce bindingGroup creation for data texture content updates. #1204

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 5 commits into from
Aug 26, 2024
Merged
Show file tree
Hide file tree
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
109 changes: 58 additions & 51 deletions src-testing/changes.patch
Original file line number Diff line number Diff line change
Expand Up @@ -2995,7 +2995,7 @@ index ad7de840..8cca219d 100644
}

diff --git a/src-testing/src/renderers/common/Backend.ts b/src-testing/src/renderers/common/Backend.ts
index 3dac46fe..e68939b8 100644
index edd4fcf9..28f354e9 100644
--- a/src-testing/src/renderers/common/Backend.ts
+++ b/src-testing/src/renderers/common/Backend.ts
@@ -1,25 +1,71 @@
Expand Down Expand Up @@ -3231,7 +3231,7 @@ index a12f3563..e7ae8d1c 100644
}

diff --git a/src-testing/src/renderers/common/Bindings.ts b/src-testing/src/renderers/common/Bindings.ts
index 51aa31f0..a300e5f6 100644
index 38dc9811..d6820e2f 100644
--- a/src-testing/src/renderers/common/Bindings.ts
+++ b/src-testing/src/renderers/common/Bindings.ts
@@ -1,8 +1,42 @@
Expand Down Expand Up @@ -3303,18 +3303,18 @@ index 51aa31f0..a300e5f6 100644

- updateForCompute(computeNode) {
+ updateForCompute(computeNode: ComputeNode) {
this._updateBindings(computeNode, this.getForCompute(computeNode));
this._updateBindings(this.getForCompute(computeNode));
}

- updateForRender(renderObject) {
+ updateForRender(renderObject: RenderObject) {
this._updateBindings(renderObject, this.getForRender(renderObject));
this._updateBindings(this.getForRender(renderObject));
}

- _updateBindings(object, bindings) {
+ _updateBindings(object: ComputeNode | RenderObject, bindings: BindGroup[]) {
- _updateBindings(bindings) {
+ _updateBindings(bindings: BindGroup[]) {
for (const bindGroup of bindings) {
this._update(object, bindGroup, bindings);
this._update(bindGroup, bindings);
}
}

Expand All @@ -3335,12 +3335,12 @@ index 51aa31f0..a300e5f6 100644
}
}

- _update(object, bindGroup, bindings) {
+ _update(object: ComputeNode | RenderObject, bindGroup: BindGroup, bindings: BindGroup[]) {
- _update(bindGroup, bindings) {
+ _update(bindGroup: BindGroup, bindings: BindGroup[]) {
const { backend } = this;

let needsBindingsUpdate = false;
@@ -87,32 +121,32 @@ class Bindings extends DataMap {
@@ -87,27 +121,31 @@ class Bindings extends DataMap {
// iterate over all bindings and check if buffer updates or a new binding group is required

for (const binding of bindGroup.bindings) {
Expand All @@ -3364,43 +3364,39 @@ index 51aa31f0..a300e5f6 100644
- } else if (binding.isSampler) {
- binding.update();
- } else if (binding.isSampledTexture) {
- const texture = binding.texture;
- if (binding.needsBindingsUpdate(this.textures.get(binding.texture).generation))
+ } else if ((binding as Sampler).isSampler) {
+ (binding as Sampler).update();
+ } else if ((binding as SampledTexture).isSampledTexture) {
+ const texture = (binding as SampledTexture).texture;

- if (binding.needsBindingsUpdate) needsBindingsUpdate = true;
+ if ((binding as SampledTexture).needsBindingsUpdate) needsBindingsUpdate = true;
+ if (
+ (binding as SampledTexture).needsBindingsUpdate(
+ this.textures.get((binding as SampledTexture).texture).generation,
+ )
+ )
needsBindingsUpdate = true;

- const updated = binding.update();
+ const updated = (binding as SampledTexture).update();

if (updated) {
- this.textures.updateTexture(binding.texture);
+ this.textures.updateTexture((binding as SampledTexture).texture);
}

- const textureData = backend.get(binding.texture);
+ const textureData = backend.get((binding as SampledTexture).texture);
- const texture = binding.texture;
+ const texture = (binding as SampledTexture).texture;

if (
backend.isWebGPUBackend === true &&
@@ -124,18 +158,18 @@ class Bindings extends DataMap {
if (updated) {
this.textures.updateTexture(texture);
@@ -123,10 +161,10 @@ class Bindings extends DataMap {
// TODO: Remove this once we found why updated === false isn't bound to a texture in the WebGPU backend
console.error(
'Bindings._update: binding should be available:',
binding,
- binding,
+ binding as SampledTexture,
updated,
- binding.texture,
texture,
- binding.textureNode.value,
+ (binding as SampledTexture).texture,
+ (binding as SampledTexture).textureNode.value,
needsBindingsUpdate,
);

- this.textures.updateTexture(binding.texture);
+ this.textures.updateTexture((binding as SampledTexture).texture);
needsBindingsUpdate = true;
}

@@ -137,7 +175,7 @@ class Bindings extends DataMap {
if (texture.isStorageTexture === true) {
const textureData = this.get(texture);

Expand Down Expand Up @@ -5961,10 +5957,10 @@ index be803038..a4f4f40f 100644

get compute() {
diff --git a/src-testing/src/renderers/common/SampledTexture.ts b/src-testing/src/renderers/common/SampledTexture.ts
index c671cb09..83243c0b 100644
index 841e6a85..542d5dd8 100644
--- a/src-testing/src/renderers/common/SampledTexture.ts
+++ b/src-testing/src/renderers/common/SampledTexture.ts
@@ -1,9 +1,19 @@
@@ -1,9 +1,20 @@
import Binding from './Binding.js';
+import { Texture } from '../../textures/Texture.js';
+import { VideoTexture } from '../../textures/VideoTexture.js';
Expand All @@ -5978,23 +5974,33 @@ index c671cb09..83243c0b 100644
+ texture: Texture;
+ version: number;
+ store: boolean;
+ generation: number | null;
+
+ readonly isSampledTexture: true;
+
+ constructor(name: string, texture: Texture) {
super(name);

this.id = _id++;
@@ -18,7 +28,7 @@ class SampledTexture extends Binding {
get needsBindingsUpdate() {
const { texture, version } = this;
@@ -16,7 +27,7 @@ class SampledTexture extends Binding {
this.isSampledTexture = true;
}

- needsBindingsUpdate(generation) {
+ needsBindingsUpdate(generation: number) {
const { texture } = this;

if (generation !== this.generation) {
@@ -25,7 +36,7 @@ class SampledTexture extends Binding {
return true;
}

- return texture.isVideoTexture ? true : version !== texture.version; // @TODO: version === 0 && texture.version > 0 ( add it just to External Textures like PNG,JPG )
+ return (texture as VideoTexture).isVideoTexture ? true : version !== texture.version; // @TODO: version === 0 && texture.version > 0 ( add it just to External Textures like PNG,JPG )
- return texture.isVideoTexture;
+ return (texture as VideoTexture).isVideoTexture;
}

update() {
@@ -35,7 +45,9 @@ class SampledTexture extends Binding {
@@ -42,7 +53,9 @@ class SampledTexture extends Binding {
}

class SampledArrayTexture extends SampledTexture {
Expand All @@ -6005,7 +6011,7 @@ index c671cb09..83243c0b 100644
super(name, texture);

this.isSampledArrayTexture = true;
@@ -43,7 +55,9 @@ class SampledArrayTexture extends SampledTexture {
@@ -50,7 +63,9 @@ class SampledArrayTexture extends SampledTexture {
}

class Sampled3DTexture extends SampledTexture {
Expand All @@ -6016,7 +6022,7 @@ index c671cb09..83243c0b 100644
super(name, texture);

this.isSampled3DTexture = true;
@@ -51,7 +65,9 @@ class Sampled3DTexture extends SampledTexture {
@@ -58,7 +73,9 @@ class Sampled3DTexture extends SampledTexture {
}

class SampledCubeTexture extends SampledTexture {
Expand Down Expand Up @@ -6065,10 +6071,10 @@ index ef5d3e46..fb6acc45 100644

this.attribute = attribute;
diff --git a/src-testing/src/renderers/common/Textures.ts b/src-testing/src/renderers/common/Textures.ts
index 036705e0..3ed51134 100644
index cbec331f..b494171a 100644
--- a/src-testing/src/renderers/common/Textures.ts
+++ b/src-testing/src/renderers/common/Textures.ts
@@ -15,11 +15,55 @@ import {
@@ -15,11 +15,56 @@ import {
CubeRefractionMapping,
UnsignedByteType,
} from '../../constants.js';
Expand Down Expand Up @@ -6102,6 +6108,7 @@ index 036705e0..3ed51134 100644
+ initialized?: boolean;
+ version?: number;
+ isDefaultTexture?: boolean;
+ generation?: number;
+}
+
+interface TextureOptions {
Expand All @@ -6127,7 +6134,7 @@ index 036705e0..3ed51134 100644
super();

this.renderer = renderer;
@@ -27,7 +71,7 @@ class Textures extends DataMap {
@@ -27,7 +72,7 @@ class Textures extends DataMap {
this.info = info;
}

Expand All @@ -6136,7 +6143,7 @@ index 036705e0..3ed51134 100644
const renderTargetData = this.get(renderTarget);

const sampleCount = renderTarget.samples === 0 ? 1 : renderTarget.samples;
@@ -113,7 +157,7 @@ class Textures extends DataMap {
@@ -113,7 +158,7 @@ class Textures extends DataMap {
}
}

Expand All @@ -6145,7 +6152,7 @@ index 036705e0..3ed51134 100644
const textureData = this.get(texture);
if (textureData.initialized === true && textureData.version === texture.version) return;

@@ -226,7 +270,7 @@ class Textures extends DataMap {
@@ -231,7 +276,7 @@ class Textures extends DataMap {
textureData.version = texture.version;
}

Expand All @@ -6154,7 +6161,7 @@ index 036705e0..3ed51134 100644
let image = texture.images ? texture.images[0] : texture.image;

if (image) {
@@ -234,18 +278,18 @@ class Textures extends DataMap {
@@ -239,18 +284,18 @@ class Textures extends DataMap {

target.width = image.width;
target.height = image.height;
Expand All @@ -6177,7 +6184,7 @@ index 036705e0..3ed51134 100644
mipLevelCount = texture.mipmaps.length;
} else {
mipLevelCount = Math.floor(Math.log2(Math.max(width, height))) + 1;
@@ -254,16 +298,16 @@ class Textures extends DataMap {
@@ -259,16 +304,16 @@ class Textures extends DataMap {
return mipLevelCount;
}

Expand All @@ -6197,7 +6204,7 @@ index 036705e0..3ed51134 100644
const mapping = texture.mapping;

return (
@@ -274,7 +318,7 @@ class Textures extends DataMap {
@@ -279,7 +324,7 @@ class Textures extends DataMap {
);
}

Expand Down
4 changes: 2 additions & 2 deletions types/three/src/renderers/common/Bindings.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ declare class Bindings extends DataMap<{
getForCompute(computeNode: ComputeNode): BindGroup[];
updateForCompute(computeNode: ComputeNode): void;
updateForRender(renderObject: RenderObject): void;
_updateBindings(object: ComputeNode | RenderObject, bindings: BindGroup[]): void;
_updateBindings(bindings: BindGroup[]): void;
_init(bindGroup: BindGroup): void;
_update(object: ComputeNode | RenderObject, bindGroup: BindGroup, bindings: BindGroup[]): void;
_update(bindGroup: BindGroup, bindings: BindGroup[]): void;
}
export default Bindings;
8 changes: 7 additions & 1 deletion types/three/src/renderers/common/SampledTexture.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,18 @@ import Binding from "./Binding.js";

declare class SampledTexture extends Binding {
id: number;

texture: Texture | null;
version: number;
store: boolean;
generation: number | null;

readonly isSampledTexture: true;

constructor(name: string, texture: Texture | null);
get needsBindingsUpdate(): boolean;

needsBindingsUpdate(generation: number): boolean;

update(): boolean;
}

Expand Down
1 change: 1 addition & 0 deletions types/three/src/renderers/common/Textures.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ interface TextureData {
initialized?: boolean;
version?: number;
isDefaultTexture?: boolean;
generation?: number;
}
interface TextureOptions {
width?: number;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ type GPUStorageTextureAccess = "read-only" | "read-write" | "write-only";
declare class NodeSampledTexture extends SampledTexture {
textureNode: TextureNode | undefined;
groupNode: UniformGroupNode;

access: "read-write" | "read-only" | "write-only";

constructor(
name: string,
textureNode: TextureNode | undefined,
groupNode: UniformGroupNode,
access: GPUStorageTextureAccess | null,
access?: GPUStorageTextureAccess | null,
);
get needsBindingsUpdate(): boolean;
update(): boolean;
}

declare class NodeSampledCubeTexture extends NodeSampledTexture {
Expand Down
Loading