Skip to content

Commit

Permalink
[Fix] Fix frequent shader variant invalidation
Browse files Browse the repository at this point in the history
  • Loading branch information
Martin Valigursky committed Jan 17, 2025
1 parent 64fafcd commit e0eeadd
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 15 deletions.
9 changes: 7 additions & 2 deletions src/scene/materials/material.js
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,8 @@ class Material {
*/
_chunks = { };

_dirtyShader = true;

/** @protected */
constructor() {
if (new.target === Material) {
Expand All @@ -192,7 +194,7 @@ class Material {
* @type {Object<string, string>}
*/
set chunks(value) {
this.clearVariants();
this._dirtyShader = true;
this._chunks = value;
}

Expand All @@ -202,7 +204,7 @@ class Material {
* @type {Object<string, string>}
*/
get chunks() {
this.clearVariants();
this._dirtyShader = true;
return this._chunks;
}

Expand Down Expand Up @@ -589,6 +591,9 @@ class Material {
}

updateUniforms(device, scene) {
if (this._dirtyShader) {
this.clearVariants();
}
}

/**
Expand Down
6 changes: 1 addition & 5 deletions src/scene/materials/standard-material.js
Original file line number Diff line number Diff line change
Expand Up @@ -569,8 +569,6 @@ class StandardMaterial extends Material {
constructor() {
super();

this._dirtyShader = true;

// storage for texture and cubemap asset references
this._assetReferences = {};

Expand Down Expand Up @@ -800,9 +798,7 @@ class StandardMaterial extends Material {
// remove unused params
this._processParameters('_activeParams');

if (this._dirtyShader) {
this.clearVariants();
}
super.updateUniforms(device, scene);
}

updateEnvUniforms(device, scene) {
Expand Down
13 changes: 5 additions & 8 deletions src/scene/renderer/shadow-renderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -300,16 +300,13 @@ class ShadowRenderer {
material.dirty = false;
}

if (material.chunks) {
renderer.setupCullMode(true, 1, meshInstance);

renderer.setupCullMode(true, 1, meshInstance);
// Uniforms I (shadow): material
material.setParameters(device);

// Uniforms I (shadow): material
material.setParameters(device);

// Uniforms II (shadow): meshInstance overrides
meshInstance.setParameters(device, passFlags);
}
// Uniforms II (shadow): meshInstance overrides
meshInstance.setParameters(device, passFlags);

const shaderInstance = meshInstance.getShaderInstance(shadowPass, 0, scene, cameraShaderParams, this.viewUniformFormat, this.viewBindGroupFormat);
const shadowShader = shaderInstance.shader;
Expand Down

0 comments on commit e0eeadd

Please sign in to comment.