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

Post process: Add support for uniform buffers #14568

Merged
merged 1 commit into from Nov 30, 2023
Merged
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
146 changes: 141 additions & 5 deletions packages/dev/core/src/PostProcesses/postProcess.ts
Expand Up @@ -50,9 +50,86 @@ export type PostProcessCustomShaderCodeProcessing = {
};

/**
* Size options for a post process
* Options for the PostProcess constructor
*/
export type PostProcessOptions = { width: number; height: number };
export type PostProcessOptions = {
/**
* The width of the texture created for this post process.
* This parameter (and height) is only used when passing a value for the 5th parameter (options) to the PostProcess constructor function.
* If you use a PostProcessOptions for the 3rd parameter of the constructor, size is used instead of width and height.
*/
width?: number;
/**
* The height of the texture created for this post process.
* This parameter (and width) is only used when passing a value for the 5th parameter (options) to the PostProcess constructor function.
* If you use a PostProcessOptions for the 3rd parameter of the constructor, size is used instead of width and height.
*/
height?: number;

/**
* The list of uniforms used in the shader (if any)
*/
uniforms?: Nullable<string[]>;
/**
* The list of samplers used in the shader (if any)
*/
samplers?: Nullable<string[]>;
/**
* The list of uniform buffers used in the shader (if any)
*/
uniformBuffers?: Nullable<string[]>;
/**
* String of defines that will be set when running the fragment shader. (default: null)
*/
defines?: Nullable<string>;
/**
* The size of the post process texture.
* It is either a ratio to downscale or upscale the texture create for this post process, or an object containing width and height values.
* Default: 1
*/
size?: number | { width: number; height: number };
/**
* The camera that the post process will be attached to (default: null)
*/
camera?: Nullable<Camera>;
/**
* The sampling mode to be used by the shader (default: Constants.TEXTURE_NEAREST_SAMPLINGMODE)
*/
samplingMode?: number;
/**
* The engine to be used to render the post process (default: engine from scene)
*/
engine?: Engine;
/**
* If the post process can be reused on the same frame. (default: false)
*/
reusable?: boolean;
/**
* Type of the texture created for this post process (default: Constants.TEXTURETYPE_UNSIGNED_INT)
*/
textureType?: number;
/**
* The url of the vertex shader to be used. (default: "postprocess")
*/
vertexUrl?: string;
/**
* The index parameters to be used for babylons include syntax "#include<kernelBlurVaryingDeclaration>[0..varyingCount]". (default: undefined)
* See usage in babylon.blurPostProcess.ts and kernelBlur.vertex.fx
*/
indexParameters?: any;
/**
* If the shader should not be compiled immediately. (default: false)
*/
blockCompilation?: boolean;
/**
* Format of the texture created for this post process (default: TEXTUREFORMAT_RGBA)
*/
textureFormat?: number;
/**
* The shader language of the shader. (default: GLSL)
*/
shaderLanguage?: ShaderLanguage;
};

type TextureCache = { texture: RenderTargetWrapper; postProcessChannel: number; lastUsedRenderId: number };

Expand Down Expand Up @@ -220,7 +297,7 @@ export class PostProcess {
protected _scene: Scene;
private _engine: Engine;

private _options: number | PostProcessOptions;
private _options: number | { width: number; height: number };
private _reusable = false;
private _renderId = 0;
private _textureType: number;
Expand Down Expand Up @@ -254,6 +331,7 @@ export class PostProcess {
private _fragmentUrl: string;
private _vertexUrl: string;
private _parameters: string[];
private _uniformBuffers: string[];
protected _postProcessDefines: Nullable<string>;
private _scaleRatio = new Vector2(1, 1);
protected _indexParameters: any;
Expand Down Expand Up @@ -408,6 +486,14 @@ export class PostProcess {
return this._texelSize;
}

/**
* Creates a new instance PostProcess
* @param name The name of the PostProcess.
* @param fragmentUrl The url of the fragment shader to be used.
* @param options The options to be used when constructing the post process.
*/
constructor(name: string, fragmentUrl: string, options?: PostProcessOptions);

/**
* Creates a new instance PostProcess
* @param name The name of the PostProcess.
Expand All @@ -425,6 +511,7 @@ export class PostProcess {
* @param indexParameters The index parameters to be used for babylons include syntax "#include<kernelBlurVaryingDeclaration>[0..varyingCount]". (default: undefined) See usage in babylon.blurPostProcess.ts and kernelBlur.vertex.fx
* @param blockCompilation If the shader should not be compiled immediatly. (default: false)
* @param textureFormat Format of textures used when performing the post process. (default: TEXTUREFORMAT_RGBA)
* @param shaderLanguage The shader language of the shader. (default: GLSL)
*/
constructor(
name: string,
Expand All @@ -433,6 +520,26 @@ export class PostProcess {
samplers: Nullable<string[]>,
options: number | PostProcessOptions,
camera: Nullable<Camera>,
samplingMode?: number,
engine?: Engine,
reusable?: boolean,
defines?: Nullable<string>,
textureType?: number,
vertexUrl?: string,
indexParameters?: any,
blockCompilation?: boolean,
textureFormat?: number,
shaderLanguage?: ShaderLanguage
);

/** @internal */
constructor(
name: string,
fragmentUrl: string,
parameters?: Nullable<string[]> | PostProcessOptions,
samplers?: Nullable<string[]>,
_size?: number | PostProcessOptions,
camera?: Nullable<Camera>,
samplingMode: number = Constants.TEXTURE_NEAREST_SAMPLINGMODE,
engine?: Engine,
reusable?: boolean,
Expand All @@ -445,6 +552,33 @@ export class PostProcess {
shaderLanguage = ShaderLanguage.GLSL
) {
this.name = name;
let size: number | { width: number; height: number } = 1;
let uniformBuffers: Nullable<string[]> = null;
if (parameters && !Array.isArray(parameters)) {
const options = parameters;
parameters = options.uniforms ?? null;
samplers = options.samplers ?? null;
size = options.size ?? 1;
camera = options.camera ?? null;
samplingMode = options.samplingMode ?? Constants.TEXTURE_NEAREST_SAMPLINGMODE;
engine = options.engine;
reusable = options.reusable;
defines = options.defines ?? null;
textureType = options.textureType ?? Constants.TEXTURETYPE_UNSIGNED_INT;
vertexUrl = options.vertexUrl ?? "postprocess";
indexParameters = options.indexParameters;
blockCompilation = options.blockCompilation ?? false;
textureFormat = options.textureFormat ?? Constants.TEXTUREFORMAT_RGBA;
shaderLanguage = options.shaderLanguage ?? ShaderLanguage.GLSL;
uniformBuffers = options.uniformBuffers ?? null;
} else if (_size) {
if (typeof _size === "number") {
size = _size;
} else {
size = { width: _size.width!, height: _size.height! };
}
}

if (camera != null) {
this._camera = camera;
this._scene = camera.getScene();
Expand All @@ -457,7 +591,8 @@ export class PostProcess {
this._engine = engine;
this._engine.postProcesses.push(this);
}
this._options = options;

this._options = size;
this.renderTargetSamplingMode = samplingMode ? samplingMode : Constants.TEXTURE_NEAREST_SAMPLINGMODE;
this._reusable = reusable || false;
this._textureType = textureType;
Expand All @@ -472,6 +607,7 @@ export class PostProcess {
this._parameters = parameters || [];

this._parameters.push("scale");
this._uniformBuffers = uniformBuffers || [];

this._indexParameters = indexParameters;
this._drawWrapper = new DrawWrapper(this._engine);
Expand Down Expand Up @@ -569,7 +705,7 @@ export class PostProcess {
{
attributes: ["position"],
uniformsNames: uniforms || this._parameters,
uniformBuffersNames: [],
uniformBuffersNames: this._uniformBuffers,
samplers: samplers || this._samplers,
defines: defines !== null ? defines : "",
fallbacks: null,
Expand Down