From 91e38e703b1bbdd45bf20ec71781ca62718c82c1 Mon Sep 17 00:00:00 2001 From: Mugen87 Date: Sun, 6 Sep 2020 12:16:24 +0200 Subject: [PATCH] WebGPUTextures: Refactor update methods. --- .../jsm/renderers/webgpu/WebGPUBindings.js | 15 ++++--- .../jsm/renderers/webgpu/WebGPURenderer.js | 1 + .../jsm/renderers/webgpu/WebGPUTextures.js | 39 +++++++++---------- 3 files changed, 29 insertions(+), 26 deletions(-) diff --git a/examples/jsm/renderers/webgpu/WebGPUBindings.js b/examples/jsm/renderers/webgpu/WebGPUBindings.js index 3c202c08e7ab11..047a5c13c048f6 100644 --- a/examples/jsm/renderers/webgpu/WebGPUBindings.js +++ b/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; diff --git a/examples/jsm/renderers/webgpu/WebGPURenderer.js b/examples/jsm/renderers/webgpu/WebGPURenderer.js index 8e58997bbe81bb..1e997786b2c474 100644 --- a/examples/jsm/renderers/webgpu/WebGPURenderer.js +++ b/examples/jsm/renderers/webgpu/WebGPURenderer.js @@ -383,6 +383,7 @@ class WebGPURenderer { this._bindings.dispose(); this._info.dispose(); this._renderLists.dispose(); + this._textures.dispose(); } diff --git a/examples/jsm/renderers/webgpu/WebGPUTextures.js b/examples/jsm/renderers/webgpu/WebGPUTextures.js index 2d5067ca0978df..25c99b9436d36a 100644 --- a/examples/jsm/renderers/webgpu/WebGPUTextures.js +++ b/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;