Pārlūkot izejas kodu

WebGPUTextures: Refactor update methods.

Mugen87 4 gadi atpakaļ
vecāks
revīzija
91e38e703b

+ 9 - 6
examples/jsm/renderers/webgpu/WebGPUBindings.js

@@ -72,6 +72,8 @@ class WebGPUBindings {
 
 	update( object, camera ) {
 
+		const textures = this.textures;
+
 		const data = this.get( object );
 		const bindings = data.bindings;
 
@@ -115,10 +117,11 @@ class WebGPUBindings {
 				const material = object.material;
 				const texture = material[ binding.name ];
 
-				const forceUpdate = this.textures.updateSampler( texture );
-				const samplerGPU = this.textures.getSampler( texture );
+				textures.updateSampler( texture );
+
+				const samplerGPU = textures.getSampler( texture );
 
-				if ( binding.samplerGPU !== samplerGPU || forceUpdate ) {
+				if ( binding.samplerGPU !== samplerGPU ) {
 
 					binding.samplerGPU = samplerGPU;
 					needsBindGroupRefresh = true;
@@ -130,10 +133,10 @@ class WebGPUBindings {
 				const material = object.material;
 				const texture = material[ binding.name ];
 
-				const forceUpdate = this.textures.updateTexture( texture );
-				const textureGPU = this.textures.getTextureGPU( texture );
+				const forceUpdate = textures.updateTexture( texture );
+				const textureGPU = textures.getTextureGPU( texture );
 
-				if ( binding.textureGPU !== textureGPU || forceUpdate ) {
+				if ( binding.textureGPU !== textureGPU || forceUpdate === true ) {
 
 					binding.textureGPU = textureGPU;
 					needsBindGroupRefresh = true;

+ 1 - 0
examples/jsm/renderers/webgpu/WebGPURenderer.js

@@ -383,6 +383,7 @@ class WebGPURenderer {
 		this._bindings.dispose();
 		this._info.dispose();
 		this._renderLists.dispose();
+		this._textures.dispose();
 
 	}
 

+ 19 - 20
examples/jsm/renderers/webgpu/WebGPUTextures.js

@@ -12,7 +12,7 @@ class WebGPUTextures {
 		this.defaultTexture = null;
 		this.defaultSampler = null;
 
-		this.samplerCache = new WeakMap();
+		this.samplerCache = new Map();
 
 	}
 
@@ -52,13 +52,13 @@ class WebGPUTextures {
 
 		const textureProperties = this.properties.get( texture );
 
-		return textureProperties.sampler;
+		return textureProperties.samplerGPU;
 
 	}
 
 	updateTexture( texture ) {
 
-		let updated = false;
+		let forceUpdate = false;
 
 		const textureProperties = this.properties.get( texture );
 
@@ -105,7 +105,7 @@ class WebGPUTextures {
 
 				textureProperties.textureGPU = this._createTexture( texture );
 				textureProperties.version = texture.version;
-				updated = true;
+				forceUpdate = true;
 
 			}
 
@@ -117,18 +117,16 @@ class WebGPUTextures {
 		if ( textureProperties.initializedRTT === false ) {
 
 			textureProperties.initializedRTT = true;
-			updated = true;
+			forceUpdate = true;
 
 		}
 
-		return updated;
+		return forceUpdate;
 
 	}
 
 	updateSampler( texture ) {
 
-		let updated = false;
-
 		const array = [];
 
 		array.push( texture.wrapS );
@@ -138,14 +136,12 @@ class WebGPUTextures {
 		array.push( texture.minFilter );
 		array.push( texture.anisotropy );
 
-		const newKey = array.join();
-		const key = this.samplerCache.get( texture );
-
-		if ( key !== newKey ) {
+		const key = array.join();
+		let samplerGPU = this.samplerCache.get( key );
 
-			this.samplerCache.set( texture, newKey );
+		if ( samplerGPU === undefined ) {
 
-			const sampler = this.device.createSampler( {
+			samplerGPU = this.device.createSampler( {
 				addressModeU: this._convertAddressMode( texture.wrapS ),
 				addressModeV: this._convertAddressMode( texture.wrapT ),
 				addressModeW: this._convertAddressMode( texture.wrapR ),
@@ -155,15 +151,12 @@ class WebGPUTextures {
 				maxAnisotropy: texture.anisotropy
 			} );
 
-			const textureProperties = this.properties.get( texture );
-			textureProperties.sampler = sampler;
-
-			updated = true;
+			this.samplerCache.set( key, samplerGPU );
 
 		}
 
-		return updated;
-
+		const textureProperties = this.properties.get( texture );
+		textureProperties.samplerGPU = samplerGPU;
 
 	}
 
@@ -230,6 +223,12 @@ class WebGPUTextures {
 
 	}
 
+	dispose() {
+
+		this.samplerCache.clear();
+
+	}
+
 	_convertAddressMode( value ) {
 
 		let addressMode = GPUAddressMode.ClampToEdge;