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

WebGPURenderer: Fix texture disposal for render targets. (#33511)

Michael Herzog 1 месяц назад
Родитель
Сommit
86b2840f76
2 измененных файлов с 22 добавлено и 51 удалено
  1. 10 51
      src/renderers/common/Sampler.js
  2. 12 0
      src/renderers/common/Textures.js

+ 10 - 51
src/renderers/common/Sampler.js

@@ -24,30 +24,14 @@ class Sampler extends Binding {
 		 * @private
 		 * @type {?Texture}
 		 */
-		this._texture = null;
-
-		/**
-		 * An event listener which is added to {@link texture}'s dispose event.
-		 *
-		 * @private
-		 * @type {Function}
-		 */
-		this._onTextureDispose = () => {
-
-			this.generation = null;
-			this.version = - 1;
-
-		};
-
-		// Assignment to the texture via a setter must occur after "_onTextureDispose" is initialized.
-		this.texture = texture;
+		this._texture = texture;
 
 		/**
 		 * The binding's version.
 		 *
 		 * @type {number}
 		 */
-		this.version = texture ? texture.version : - 1;
+		this.version = - 1;
 
 		/**
 		 * The binding's generation which is an additional version
@@ -86,22 +70,9 @@ class Sampler extends Binding {
 
 		if ( this._texture === value ) return;
 
-		if ( this._texture ) {
-
-			this._texture.removeEventListener( 'dispose', this._onTextureDispose );
-
-		}
-
 		this._texture = value;
 
-		this.generation = null;
-		this.version = - 1;
-
-		if ( this._texture ) {
-
-			this._texture.addEventListener( 'dispose', this._onTextureDispose );
-
-		}
+		this.reset();
 
 	}
 
@@ -137,26 +108,14 @@ class Sampler extends Binding {
 
 	}
 
+	/**
+	 * Resets the version and generation. This is used when the texture
+	 * the binding is pointing to is disposed or exchanged.
+	 */
+	reset() {
 
-	clone() {
-
-		const clonedSampler = super.clone();
-
-		// fix dispose handler for cloned instances
-		// TODO: Find better solution, see #31747
-
-		clonedSampler._texture = null;
-
-		clonedSampler._onTextureDispose = () => {
-
-			clonedSampler.generation = null;
-			clonedSampler.version = - 1;
-
-		};
-
-		clonedSampler.texture = this.texture;
-
-		return clonedSampler;
+		this.generation = null;
+		this.version = - 1;
 
 	}
 

+ 12 - 0
src/renderers/common/Textures.js

@@ -610,6 +610,18 @@ class Textures extends DataMap {
 					bindingsData.groups = undefined;
 					bindingsData.versions = undefined;
 
+					// go through all bindings and if one points to the destroyed texture, trigger dispose as well
+
+					for ( const binding of bindGroup.bindings ) {
+
+						if ( binding.isSampler && binding.texture === texture ) {
+
+							binding.reset();
+
+						}
+
+					}
+
 				}
 
 			}

粤ICP备19079148号