Skip to content

[Fix] Fix frequent shader variant invalidation #7294

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 20, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading