Skip to content

Commit

Permalink
Use shader material written in wgsl as shadow depth wrapper (#14755)
Browse files Browse the repository at this point in the history
* use shader material written in wgsl as shadow depth wrapper

* minor change

* minor change

* fix lint

* access shader language option from effect class

* add ts doc
  • Loading branch information
shen-lin committed Feb 1, 2024
1 parent 124f73f commit 2f10ef0
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -104,10 +104,10 @@ export class WebGPUShaderProcessorWGSL extends WebGPUShaderProcessor {
}

public preProcessShaderCode(code: string): string {
return (
`struct ${WebGPUShaderProcessor.InternalsUBOName} {\n yFactor_: f32,\n textureOutputHeight_: f32,\n};\nvar<uniform> ${internalsVarName} : ${WebGPUShaderProcessor.InternalsUBOName};\n` +
RemoveComments(code)
);
// Same check as in webgpuShaderProcessorsGLSL to avoid same ubDelcaration to be injected twice.
const ubDeclaration = `struct ${WebGPUShaderProcessor.InternalsUBOName} {\n yFactor_: f32,\n textureOutputHeight_: f32,\n};\nvar<uniform> ${internalsVarName} : ${WebGPUShaderProcessor.InternalsUBOName};\n`;
const alreadyInjected = code.indexOf(ubDeclaration) !== -1;
return alreadyInjected ? code : ubDeclaration + RemoveComments(code);
}

public varyingProcessor(varying: string, isFragment: boolean, preProcessors: { [key: string]: string }) {
Expand Down
7 changes: 7 additions & 0 deletions packages/dev/core/src/Materials/effect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,13 @@ export class Effect implements IDisposable {
private _processCodeAfterIncludes: ShaderCustomProcessingFunction | undefined = undefined;
private _processFinalCode: Nullable<ShaderCustomProcessingFunction> = null;

/**
* Gets the shader language type used to write vertex and fragment source code.
*/
public get shaderLanguage(): ShaderLanguage {
return this._shaderLanguage;
}

/**
* Instantiates an effect.
* An effect can be used to create/manage/execute vertex and fragment shaders.
Expand Down
20 changes: 14 additions & 6 deletions packages/dev/core/src/Materials/shadowDepthWrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import type { ShadowGenerator } from "../Lights/Shadows/shadowGenerator";
import { RandomGUID } from "../Misc/guid";
import { DrawWrapper } from "./drawWrapper";
import { EngineStore } from "../Engines/engineStore";
import { ShaderLanguage } from "./shaderLanguage";

/**
* Options to be used when creating a shadow depth material
Expand Down Expand Up @@ -240,22 +241,28 @@ export class ShadowDepthWrapper {
fragmentCode = origEffect.fragmentSourceCodeBeforeMigration;

if (!this.doNotInjectCode) {
// vertex code
// Declare the shadow map includes
const vertexNormalBiasCode =
this._options && this._options.remappedVariables
? `#include<shadowMapVertexNormalBias>(${this._options.remappedVariables.join(",")})`
: Effect.IncludesShadersStore["shadowMapVertexNormalBias"],
: `#include<shadowMapVertexNormalBias>`,
vertexMetricCode =
this._options && this._options.remappedVariables
? `#include<shadowMapVertexMetric>(${this._options.remappedVariables.join(",")})`
: Effect.IncludesShadersStore["shadowMapVertexMetric"],
: `#include<shadowMapVertexMetric>`,
fragmentSoftTransparentShadow =
this._options && this._options.remappedVariables
? `#include<shadowMapFragmentSoftTransparentShadow>(${this._options.remappedVariables.join(",")})`
: Effect.IncludesShadersStore["shadowMapFragmentSoftTransparentShadow"],
fragmentBlockCode = Effect.IncludesShadersStore["shadowMapFragment"];
: `#include<shadowMapFragmentSoftTransparentShadow>`,
fragmentBlockCode = `#include<shadowMapFragment>`,
vertexExtraDeclartion = `#include<shadowMapVertexExtraDeclaration>`;

vertexCode = vertexCode.replace(/void\s+?main/g, Effect.IncludesShadersStore["shadowMapVertexExtraDeclaration"] + "\nvoid main");
// vertex code
if (origEffect.shaderLanguage === ShaderLanguage.GLSL) {
vertexCode = vertexCode.replace(/void\s+?main/g, `\n${vertexExtraDeclartion}\nvoid main`);
} else {
vertexCode = vertexCode.replace(/@vertex/g, `\n${vertexExtraDeclartion}\n@vertex`);
}
vertexCode = vertexCode.replace(/#define SHADOWDEPTH_NORMALBIAS|#define CUSTOM_VERTEX_UPDATE_WORLDPOS/g, vertexNormalBiasCode);

if (vertexCode.indexOf("#define SHADOWDEPTH_METRIC") !== -1) {
Expand Down Expand Up @@ -306,6 +313,7 @@ export class ShadowDepthWrapper {
samplers: origEffect.getSamplers(),
defines: join + "\n" + origEffect.defines.replace("#define SHADOWS", "").replace(/#define SHADOW\d/g, ""),
indexParameters: origEffect.getIndexParameters(),
shaderLanguage: origEffect.shaderLanguage,
},
engine
);
Expand Down

0 comments on commit 2f10ef0

Please sign in to comment.