Skip to content

Commit

Permalink
WebGPUTextures: Add support for Texture.dispose().
Browse files Browse the repository at this point in the history
  • Loading branch information
Mugen87 committed Sep 3, 2020
1 parent da64654 commit 2aca7c6
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 5 deletions.
3 changes: 2 additions & 1 deletion examples/jsm/renderers/webgpu/WebGPUInfo.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ class WebGPUInfo {
};

this.memory = {
geometries: 0
geometries: 0,
textures: 0
};

}
Expand Down
2 changes: 1 addition & 1 deletion examples/jsm/renderers/webgpu/WebGPURenderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
Expand Down
37 changes: 34 additions & 3 deletions examples/jsm/renderers/webgpu/WebGPUTextures.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;

0 comments on commit 2aca7c6

Please sign in to comment.