Skip to content

Commit

Permalink
WebGPUTextures: Refactor update methods.
Browse files Browse the repository at this point in the history
  • Loading branch information
Mugen87 committed Sep 6, 2020
1 parent b166f40 commit 91e38e7
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 26 deletions.
15 changes: 9 additions & 6 deletions examples/jsm/renderers/webgpu/WebGPUBindings.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ class WebGPUBindings {

update( object, camera ) {

const textures = this.textures;

const data = this.get( object );
const bindings = data.bindings;

Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand Down
1 change: 1 addition & 0 deletions examples/jsm/renderers/webgpu/WebGPURenderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,7 @@ class WebGPURenderer {
this._bindings.dispose();
this._info.dispose();
this._renderLists.dispose();
this._textures.dispose();

}

Expand Down
39 changes: 19 additions & 20 deletions examples/jsm/renderers/webgpu/WebGPUTextures.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class WebGPUTextures {
this.defaultTexture = null;
this.defaultSampler = null;

this.samplerCache = new WeakMap();
this.samplerCache = new Map();

}

Expand Down Expand Up @@ -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 );

Expand Down Expand Up @@ -105,7 +105,7 @@ class WebGPUTextures {

textureProperties.textureGPU = this._createTexture( texture );
textureProperties.version = texture.version;
updated = true;
forceUpdate = true;

}

Expand All @@ -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 );
Expand All @@ -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 ),
Expand All @@ -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;

}

Expand Down Expand Up @@ -230,6 +223,12 @@ class WebGPUTextures {

}

dispose() {

this.samplerCache.clear();

}

_convertAddressMode( value ) {

let addressMode = GPUAddressMode.ClampToEdge;
Expand Down

0 comments on commit 91e38e7

Please sign in to comment.