Pārlūkot izejas kodu

WebGPURenderer: More efficient texture updates. (#22869)

Michael Herzog 3 gadi atpakaļ
vecāks
revīzija
f72449c7d9

+ 3 - 3
examples/jsm/renderers/webgpu/WebGPUBindings.js

@@ -78,7 +78,7 @@ class WebGPUBindings {
 
 	}
 
-	update( object, camera ) {
+	update( object ) {
 
 		const textures = this.textures;
 
@@ -141,10 +141,10 @@ class WebGPUBindings {
 
 				const texture = binding.getTexture();
 
-				const forceUpdate = textures.updateTexture( texture );
+				const needsTextureRefresh = textures.updateTexture( texture );
 				const textureGPU = textures.getTextureGPU( texture );
 
-				if ( textureGPU !== undefined && binding.textureGPU !== textureGPU || forceUpdate === true ) {
+				if ( textureGPU !== undefined && binding.textureGPU !== textureGPU || needsTextureRefresh === true ) {
 
 					binding.textureGPU = textureGPU;
 					needsBindGroupRefresh = true;

+ 2 - 2
examples/jsm/renderers/webgpu/WebGPURenderer.js

@@ -773,7 +773,7 @@ class WebGPURenderer {
 						passEncoder.setViewport( vp.x, vp.y, vp.width, vp.height, minDepth, maxDepth );
 
 						this._nodes.update( object, camera2 );
-						this._bindings.update( object, camera2 );
+						this._bindings.update( object );
 						this._renderObject( object, passEncoder );
 
 					}
@@ -783,7 +783,7 @@ class WebGPURenderer {
 			} else {
 
 				this._nodes.update( object, camera );
-				this._bindings.update( object, camera );
+				this._bindings.update( object );
 				this._renderObject( object, passEncoder );
 
 			}

+ 32 - 24
examples/jsm/renderers/webgpu/WebGPUTextures.js

@@ -41,7 +41,9 @@ class WebGPUTextures {
 			texture.minFilter = NearestFilter;
 			texture.magFilter = NearestFilter;
 
-			this.defaultTexture = this._createTexture( texture );
+			this._uploadTexture( texture );
+
+			this.defaultTexture = this.getTextureGPU( texture );
 
 		}
 
@@ -57,7 +59,9 @@ class WebGPUTextures {
 			texture.minFilter = NearestFilter;
 			texture.magFilter = NearestFilter;
 
-			this.defaultCubeTexture = this._createTexture( texture );
+			this._uploadTexture( texture );
+
+			this.defaultCubeTexture = this.getTextureGPU( texture );
 
 		}
 
@@ -83,7 +87,7 @@ class WebGPUTextures {
 
 	updateTexture( texture ) {
 
-		let forceUpdate = false;
+		let needsUpdate = false;
 
 		const textureProperties = this.properties.get( texture );
 
@@ -116,21 +120,9 @@ class WebGPUTextures {
 
 				}
 
-				// texture creation
-
-				if ( textureProperties.textureGPU !== undefined ) {
+				//
 
-					// @TODO: Avoid calling of destroy() in certain scenarios. When only the contents of a texture
-					// are updated, a buffer upload should be sufficient. However, if the user changes
-					// the dimensions of the texture, format or usage, a new instance of GPUTexture is required.
-
-					textureProperties.textureGPU.destroy();
-
-				}
-
-				textureProperties.textureGPU = this._createTexture( texture );
-				textureProperties.version = texture.version;
-				forceUpdate = true;
+				needsUpdate = this._uploadTexture( texture );
 
 			}
 
@@ -142,11 +134,11 @@ class WebGPUTextures {
 		if ( textureProperties.initializedRTT === false ) {
 
 			textureProperties.initializedRTT = true;
-			forceUpdate = true;
+			needsUpdate = true;
 
 		}
 
-		return forceUpdate;
+		return needsUpdate;
 
 	}
 
@@ -304,11 +296,15 @@ class WebGPUTextures {
 
 	}
 
-	_createTexture( texture ) {
+	_uploadTexture( texture ) {
+
+		let needsUpdate = false;
 
 		const device = this.device;
 		const image = texture.image;
 
+		const textureProperties = this.properties.get( texture );
+
 		const { width, height, depth } = this._getSize( texture );
 		const needsMipmaps = this._needsMipmaps( texture );
 		const dimension = this._getDimension( texture );
@@ -325,8 +321,6 @@ class WebGPUTextures {
 
 		}
 
-		// texture creation
-
 		const textureGPUDescriptor = {
 			size: {
 				width: width,
@@ -339,7 +333,19 @@ class WebGPUTextures {
 			format: format,
 			usage: usage
 		};
-		const textureGPU = device.createTexture( textureGPUDescriptor );
+
+		// texture creation
+
+		let textureGPU = textureProperties.textureGPU;
+
+		if ( textureGPU === undefined ) {
+
+			textureGPU = device.createTexture( textureGPUDescriptor );
+			textureProperties.textureGPU = textureGPU;
+
+			needsUpdate = true;
+
+		}
 
 		// transfer texture data
 
@@ -375,7 +381,9 @@ class WebGPUTextures {
 
 		}
 
-		return textureGPU;
+		textureProperties.version = texture.version;
+
+		return needsUpdate;
 
 	}