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

WebGPUTextures: Add support for Texture.dispose().

Mugen87 4 лет назад
Родитель
Сommit
2aca7c6525

+ 2 - 1
examples/jsm/renderers/webgpu/WebGPUInfo.js

@@ -13,7 +13,8 @@ class WebGPUInfo {
 		};
 
 		this.memory = {
-			geometries: 0
+			geometries: 0,
+			textures: 0
 		};
 
 	}

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

@@ -638,7 +638,7 @@ async function initWebGPU( scope ) {
 	scope._properties = new WebGPUProperties();
 	scope._attributes = new WebGPUAttributes( device );
 	scope._geometries = new WebGPUGeometries( scope._attributes, scope._info );
-	scope._textures = new WebGPUTextures( device, scope._properties );
+	scope._textures = new WebGPUTextures( device, scope._properties, scope._info );
 	scope._bindings = new WebGPUBindings( device, scope._info, scope._properties, scope._textures );
 	scope._objects = new WebGPUObjects( scope._geometries, scope._info );
 	scope._renderPipelines = new WebGPURenderPipelines( device, compiler, scope._bindings );

+ 34 - 3
examples/jsm/renderers/webgpu/WebGPUTextures.js

@@ -3,12 +3,11 @@ import { Texture, NearestFilter, NearestMipmapNearestFilter, NearestMipmapLinear
 
 class WebGPUTextures {
 
-	constructor( device, properties ) {
+	constructor( device, properties, info ) {
 
 		this.device = device;
 		this.properties = properties;
-
-		this.textures = new WeakMap();
+		this.info = info;
 
 		this.defaultTexture = null;
 		this.defaultSampler = null;
@@ -77,6 +76,23 @@ class WebGPUTextures {
 
 			} else {
 
+				// texture init
+
+				if ( textureProperties.initialized === undefined ) {
+
+					textureProperties.initialized = true;
+
+					const disposeCallback = onTextureDispose.bind( this );
+					textureProperties.disposeCallback = disposeCallback;
+
+					texture.addEventListener( 'dispose', disposeCallback );
+
+					this.info.memory.textures ++;
+
+				}
+
+				// texture creation
+
 				if ( textureProperties.textureGPU !== undefined ) {
 
 					// TODO: Avoid calling of destroy() in certain scenarios. When only the contents of a texture
@@ -316,4 +332,19 @@ class WebGPUTextures {
 
 }
 
+function onTextureDispose( event ) {
+
+	const texture = event.target;
+
+	const textureProperties = this.properties.get( texture );
+	textureProperties.textureGPU.destroy();
+
+	texture.removeEventListener( 'dispose', textureProperties.disposeCallback );
+
+	this.properties.remove( texture );
+
+	this.info.memory.textures --;
+
+}
+
 export default WebGPUTextures;