Просмотр исходного кода

WebGPURenderer: Improve and fix texture bindings manager (#31497)

* improve texture bindings

* cleanup

* Update webgpu_volume_lighting.jpg

* cleanup
sunag 8 месяцев назад
Родитель
Сommit
3752bd2796

BIN
examples/screenshots/webgpu_volume_lighting.jpg


+ 15 - 14
src/renderers/common/Bindings.js

@@ -246,18 +246,29 @@ class Bindings extends DataMap {
 
 			} else if ( binding.isSampledTexture ) {
 
-				const texturesTextureData = this.textures.get( binding.texture );
-
-				if ( binding.needsBindingsUpdate( texturesTextureData.generation ) ) needsBindingsUpdate = true;
-
 				const updated = binding.update();
 
+				// get the texture data after the update, to sync the texture reference from node
+
 				const texture = binding.texture;
+				const texturesTextureData = this.textures.get( texture );
 
 				if ( updated ) {
 
+					// version: update the texture data or create a new one
+
 					this.textures.updateTexture( texture );
 
+					// generation: update the bindings if a new texture has been created
+
+					if ( binding.generation !== texturesTextureData.generation ) {
+
+						binding.generation = texturesTextureData.generation;
+
+						needsBindingsUpdate = true;
+
+					}
+
 				}
 
 				const textureData = backend.get( texture );
@@ -273,16 +284,6 @@ class Bindings extends DataMap {
 
 				}
 
-				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
-					console.error( 'Bindings._update: binding should be available:', binding, updated, texture, binding.textureNode.value, needsBindingsUpdate );
-
-					this.textures.updateTexture( texture );
-					needsBindingsUpdate = true;
-
-				}
-
 				if ( texture.isStorageTexture === true ) {
 
 					const textureData = this.get( texture );

+ 0 - 30
src/renderers/common/SampledTexture.js

@@ -35,15 +35,6 @@ class SampledTexture extends Sampler {
 		 */
 		this.store = false;
 
-		/**
-		 * The binding's generation which is an additional version
-		 * qualifier.
-		 *
-		 * @type {?number}
-		 * @default null
-		 */
-		this.generation = null;
-
 		/**
 		 * This flag can be used for type testing.
 		 *
@@ -55,27 +46,6 @@ class SampledTexture extends Sampler {
 
 	}
 
-	/**
-	 * Returns `true` whether this binding requires an update for the
-	 * given generation.
-	 *
-	 * @param {number} generation - The generation.
-	 * @return {boolean} Whether an update is required or not.
-	 */
-	needsBindingsUpdate( generation ) {
-
-		if ( generation !== this.generation ) {
-
-			this.generation = generation;
-
-			return true;
-
-		}
-
-		return false;
-
-	}
-
 }
 
 /**

+ 57 - 0
src/renderers/common/Sampler.js

@@ -18,6 +18,17 @@ class Sampler extends Binding {
 
 		super( name );
 
+		/**
+		 * This function is called when the texture is disposed.
+		 * @type {function}
+		 * @private
+		 */
+		this._onDisposeTexture = () => {
+
+			this.texture = null;
+
+		};
+
 		/**
 		 * The texture the sampler is referring to.
 		 *
@@ -32,6 +43,15 @@ class Sampler extends Binding {
 		 */
 		this.version = texture ? texture.version : 0;
 
+		/**
+		 * The binding's generation which is an additional version
+		 * qualifier.
+		 *
+		 * @type {?number}
+		 * @default null
+		 */
+		this.generation = null;
+
 		/**
 		 * This flag can be used for type testing.
 		 *
@@ -43,6 +63,43 @@ class Sampler extends Binding {
 
 	}
 
+	/**
+	 * Sets the texture of this sampler.
+	 * @param {?Texture} value - The texture to set.
+	 */
+	set texture( value ) {
+
+		if ( this._texture === value ) return;
+
+		if ( this._texture ) {
+
+			this._texture.removeEventListener( 'dispose', this._onDisposeTexture );
+
+		}
+
+		this._texture = value;
+
+		this.generation = null;
+		this.version = 0;
+
+		if ( this._texture ) {
+
+			this._texture.addEventListener( 'dispose', this._onDisposeTexture );
+
+		}
+
+	}
+
+	/**
+	 * Gets the texture of this sampler.
+	 * @return {?Texture} The texture.
+	 */
+	get texture() {
+
+		return this._texture;
+
+	}
+
 	/**
 	 * Updates the binding.
 	 *

+ 0 - 12
src/renderers/common/nodes/NodeSampledTexture.js

@@ -45,18 +45,6 @@ class NodeSampledTexture extends SampledTexture {
 
 	}
 
-	/**
-	 * Overwrites the default to additionally check if the node value has changed.
-	 *
-	 * @param {number} generation - The generation.
-	 * @return {boolean} Whether an update is required or not.
-	 */
-	needsBindingsUpdate( generation ) {
-
-		return this.textureNode.value !== this.texture || super.needsBindingsUpdate( generation );
-
-	}
-
 	/**
 	 * Updates the binding.
 	 *

粤ICP备19079148号