Skip to content
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

Fire ‘shader:generate’ when a shader code gets generated #5524

Merged
merged 2 commits into from
Jul 31, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/scene/materials/basic-material.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ class BasicMaterial extends Material {
const library = getProgramLibrary(device);
library.register('basic', basic);

return library.getProgram('basic', options, processingOptions);
return library.getProgram('basic', options, processingOptions, this.userId);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/scene/materials/lit-material.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ class LitMaterial extends Material {
const processingOptions = new ShaderProcessorOptions(viewUniformFormat, viewBindGroupFormat, vertexFormat);
const library = getProgramLibrary(device);
library.register('lit', lit);
const shader = library.getProgram('lit', options, processingOptions);
const shader = library.getProgram('lit', options, processingOptions, this.userId);
return shader;
}
}
Expand Down
9 changes: 9 additions & 0 deletions src/scene/materials/material.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,15 @@ class Material {
*/
name = 'Untitled';

/**
* A unique id the user can assign to the material. The engine internally does not use this for
* anything, and the user can assign a value to this id for any purpose they like. Defaults to
* an empty string.
*
* @type {string}
*/
userId = '';
slimbuck marked this conversation as resolved.
Show resolved Hide resolved

id = id++;

variants = {};
Expand Down
2 changes: 1 addition & 1 deletion src/scene/materials/standard-material.js
Original file line number Diff line number Diff line change
Expand Up @@ -865,7 +865,7 @@ class StandardMaterial extends Material {

const library = getProgramLibrary(device);
library.register('standard', standard);
const shader = library.getProgram('standard', options, processingOptions);
const shader = library.getProgram('standard', options, processingOptions, this.userId);

this._dirtyShader = false;
return shader;
Expand Down
13 changes: 11 additions & 2 deletions src/scene/shader-lib/program-library.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ class ProgramLibrary {
this.processedCache.set(key, shader);
}

getProgram(name, options, processingOptions) {
getProgram(name, options, processingOptions, userMaterialId) {
const generator = this._generators[name];
if (!generator) {
Debug.warn(`ProgramLibrary#getProgram: No program library functions registered for: ${name}`);
Expand All @@ -133,11 +133,20 @@ class ProgramLibrary {

// use shader pass name if known
let passName = '';
let shaderPassInfo;
if (options.pass !== undefined) {
const shaderPassInfo = ShaderPass.get(this._device).getByIndex(options.pass);
shaderPassInfo = ShaderPass.get(this._device).getByIndex(options.pass);
passName = `-${shaderPassInfo.name}`;
}

// fire an event to allow the shader to be modified by the user. Note that any modifications are applied
// to all materials using the same generated shader, as the cache key is not modified.
this._device.fire('shader:generate', {
userMaterialId,
shaderPassInfo,
definition: generatedShaderDef
});
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: it would be great if this had a type so consumers could at least typecast the event as pc.ShaderGenerateEvent or similar


// create a shader definition for the shader that will include the processingOptions
const shaderDefinition = {
name: `${generatedShaderDef.name}${passName}-proc`,
Expand Down