Skip to content

Commit

Permalink
Merge pull request #14924 from RaananW/allowCastingToIComputerShaderPath
Browse files Browse the repository at this point in the history
allow simple casting to IComputeShaderPath
  • Loading branch information
sebavan authored Mar 28, 2024
2 parents fa67ca4 + 6b2def1 commit 1c53a27
Show file tree
Hide file tree
Showing 8 changed files with 65 additions and 67 deletions.
38 changes: 18 additions & 20 deletions packages/dev/core/src/Compute/computeEffect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,21 @@ import type { Engine } from "../Engines/engine";
* * object: `{ compute: "custom" }`, used with `Effect.ShadersStore["customVertexShader"]` and `Effect.ShadersStore["customFragmentShader"]`
* * string: `"./COMMON_NAME"`, used with external files COMMON_NAME.vertex.fx and COMMON_NAME.fragment.fx in index.html folder.
*/
export type IComputeShaderPath =
| {
/**
* Directly pass the shader code
*/
computeSource?: string;
/**
* Used with Effect.ShadersStore. If the `vertex` is set to `"custom`, then
* Babylon.js will read from Effect.ShadersStore["customVertexShader"]
*/
compute?: string;
/**
* Used with shader code in script tags
*/
computeElement?: string;
}
| string;
export type IComputeShaderPath = {
/**
* Directly pass the shader code
*/
computeSource?: string;
/**
* Used with Effect.ShadersStore. If the `vertex` is set to `"custom`, then
* Babylon.js will read from Effect.ShadersStore["customVertexShader"]
*/
compute?: string;
/**
* Used with shader code in script tags
*/
computeElement?: string;
};

/**
* Options to be used when creating a compute effect.
Expand Down Expand Up @@ -74,7 +72,7 @@ export class ComputeEffect {
/**
* Name of the effect.
*/
public name: IComputeShaderPath;
public name: IComputeShaderPath | string;
/**
* String container all the define statements that should be set on the shader.
*/
Expand Down Expand Up @@ -135,7 +133,7 @@ export class ComputeEffect {
* @param engine The engine the effect is created for
* @param key Effect Key identifying uniquely compiled shader variants
*/
constructor(baseName: IComputeShaderPath, options: IComputeEffectCreationOptions, engine: Engine, key = "") {
constructor(baseName: IComputeShaderPath | string, options: IComputeEffectCreationOptions, engine: Engine, key = "") {
this.name = baseName;
this._key = key;

Expand All @@ -151,7 +149,7 @@ export class ComputeEffect {
this._shaderRepository = ShaderStore.GetShadersRepository(this._shaderLanguage);
this._includeShaderStore = ShaderStore.GetIncludesShadersStore(this._shaderLanguage);

let computeSource: IComputeShaderPath | HTMLElement;
let computeSource: IComputeShaderPath | HTMLElement | string;

const hostDocument = IsWindowObjectExist() ? this._engine.getHostDocument() : null;

Expand Down
4 changes: 2 additions & 2 deletions packages/dev/core/src/Compute/computeShader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ type ComputeBindingListInternal = { [key: string]: { type: ComputeBindingType; o
*/
export class ComputeShader {
private _engine: ThinEngine;
private _shaderPath: IComputeShaderPath;
private _shaderPath: IComputeShaderPath | string;
private _options: IComputeShaderOptions;
private _effect: ComputeEffect;
private _cachedDefines: string;
Expand Down Expand Up @@ -123,7 +123,7 @@ export class ComputeShader {
* * string: try first to find the code in ShaderStore.ShadersStoreWGSL[shaderPath + "ComputeShader"]. If not, assumes it is a file with name shaderPath.compute.fx in index.html folder.
* @param options Define the options used to create the shader
*/
constructor(name: string, engine: ThinEngine, shaderPath: IComputeShaderPath, options: Partial<IComputeShaderOptions> = {}) {
constructor(name: string, engine: ThinEngine, shaderPath: IComputeShaderPath | string, options: Partial<IComputeShaderOptions> = {}) {
this.name = name;
this._engine = engine;
this.uniqueId = UniqueIdGenerator.UniqueId;
Expand Down
14 changes: 8 additions & 6 deletions packages/dev/core/src/Engines/Extensions/engine.computeShader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,14 @@ declare module "../../Engines/thinEngine" {
* @returns The new compute effect
*/
createComputeEffect(
baseName: IComputeShaderPath & {
/**
* @internal
*/
computeToken?: string;
},
baseName:
| string
| (IComputeShaderPath & {
/**
* @internal
*/
computeToken?: string;
}),
options: IComputeEffectCreationOptions
): ComputeEffect;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ WebGPUEngine.prototype.createComputeContext = function (): IComputeContext | und
return new WebGPUComputeContext(this._device, this._cacheSampler);
};

WebGPUEngine.prototype.createComputeEffect = function (baseName: IComputeShaderPath & { computeToken?: string }, options: IComputeEffectCreationOptions): ComputeEffect {
WebGPUEngine.prototype.createComputeEffect = function (baseName: string | (IComputeShaderPath & { computeToken?: string }), options: IComputeEffectCreationOptions): ComputeEffect {
const compute = typeof baseName === "string" ? baseName : baseName.computeToken || baseName.computeSource || baseName.computeElement || baseName.compute;

const name = compute + "@" + options.defines;
Expand Down
2 changes: 1 addition & 1 deletion packages/dev/core/src/Engines/thinEngine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2924,7 +2924,7 @@ export class ThinEngine {
* @returns the new Effect
*/
public createEffect(
baseName: IShaderPath & { vertexToken?: string; fragmentToken?: string },
baseName: string | (IShaderPath & { vertexToken?: string; fragmentToken?: string }),
attributesNamesOrOptions: string[] | IEffectCreationOptions,
uniformsNamesOrEngine: string[] | ThinEngine,
samplers?: string[],
Expand Down
2 changes: 1 addition & 1 deletion packages/dev/core/src/Engines/webgpuEngine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1766,7 +1766,7 @@ export class WebGPUEngine extends Engine {
* @returns the new Effect
*/
public createEffect(
baseName: IShaderPath & { vertexToken?: string; fragmentToken?: string },
baseName: string | (IShaderPath & { vertexToken?: string; fragmentToken?: string }),
attributesNamesOrOptions: string[] | IEffectCreationOptions,
uniformsNamesOrEngine: string[] | Engine,
samplers?: string[],
Expand Down
62 changes: 30 additions & 32 deletions packages/dev/core/src/Materials/effect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,36 +28,34 @@ import type { PostProcess } from "../PostProcesses/postProcess";
* * object: `{ vertex: "custom", fragment: "custom" }`, used with `Effect.ShadersStore["customVertexShader"]` and `Effect.ShadersStore["customFragmentShader"]`
* * string: `"./COMMON_NAME"`, used with external files COMMON_NAME.vertex.fx and COMMON_NAME.fragment.fx in index.html folder.
*/
export type IShaderPath =
| {
/**
* Directly pass the shader code
*/
vertexSource?: string;
/**
* Directly pass the shader code
*/
fragmentSource?: string;
/**
* Used with Effect.ShadersStore. If the `vertex` is set to `"custom`, then
* Babylon.js will read from Effect.ShadersStore["customVertexShader"]
*/
vertex?: string;
/**
* Used with Effect.ShadersStore. If the `fragment` is set to `"custom`, then
* Babylon.js will read from Effect.ShadersStore["customFragmentShader"]
*/
fragment?: string;
/**
* Used with shader code in script tags
*/
vertexElement?: string;
/**
* Used with shader code in script tags
*/
fragmentElement?: string;
}
| string;
export type IShaderPath = {
/**
* Directly pass the shader code
*/
vertexSource?: string;
/**
* Directly pass the shader code
*/
fragmentSource?: string;
/**
* Used with Effect.ShadersStore. If the `vertex` is set to `"custom`, then
* Babylon.js will read from Effect.ShadersStore["customVertexShader"]
*/
vertex?: string;
/**
* Used with Effect.ShadersStore. If the `fragment` is set to `"custom`, then
* Babylon.js will read from Effect.ShadersStore["customFragmentShader"]
*/
fragment?: string;
/**
* Used with shader code in script tags
*/
vertexElement?: string;
/**
* Used with shader code in script tags
*/
fragmentElement?: string;
};

/**
* Options to be used when creating an effect.
Expand Down Expand Up @@ -145,7 +143,7 @@ export class Effect implements IDisposable {
/**
* Name of the effect.
*/
public name: IShaderPath;
public name: IShaderPath | string;
/**
* String container all the define statements that should be set on the shader.
*/
Expand Down Expand Up @@ -275,7 +273,7 @@ export class Effect implements IDisposable {
* @param shaderLanguage the language the shader is written in (default: GLSL)
*/
constructor(
baseName: IShaderPath,
baseName: IShaderPath | string,
attributesNamesOrOptions: string[] | IEffectCreationOptions,
uniformsNamesOrEngine: string[] | ThinEngine,
samplers: Nullable<string[]> = null,
Expand Down
8 changes: 4 additions & 4 deletions packages/dev/core/src/Materials/shaderMaterial.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ export interface IShaderMaterialOptions {
* @see https://doc.babylonjs.com/features/featuresDeepDive/materials/shaders/shaderMaterial
*/
export class ShaderMaterial extends PushMaterial {
private _shaderPath: IShaderPath;
private _shaderPath: IShaderPath | string;
private _options: IShaderMaterialOptions;
private _textures: { [name: string]: BaseTexture } = {};
private _textureArrays: { [name: string]: BaseTexture[] } = {};
Expand Down Expand Up @@ -162,7 +162,7 @@ export class ShaderMaterial extends PushMaterial {
* @param options Define the options used to create the shader
* @param storeEffectOnSubMeshes true to store effect on submeshes, false to store the effect directly in the material class.
*/
constructor(name: string, scene: Scene, shaderPath: IShaderPath, options: Partial<IShaderMaterialOptions> = {}, storeEffectOnSubMeshes = true) {
constructor(name: string, scene: Scene, shaderPath: IShaderPath | string, options: Partial<IShaderMaterialOptions> = {}, storeEffectOnSubMeshes = true) {
super(name, scene, storeEffectOnSubMeshes);
this._shaderPath = shaderPath;

Expand All @@ -186,15 +186,15 @@ export class ShaderMaterial extends PushMaterial {
* Gets the shader path used to define the shader code
* It can be modified to trigger a new compilation
*/
public get shaderPath(): IShaderPath {
public get shaderPath() {
return this._shaderPath;
}

/**
* Sets the shader path used to define the shader code
* It can be modified to trigger a new compilation
*/
public set shaderPath(shaderPath: IShaderPath) {
public set shaderPath(shaderPath: IShaderPath | string) {
this._shaderPath = shaderPath;
}

Expand Down

0 comments on commit 1c53a27

Please sign in to comment.