@itwin/core-frontend
Version:
iTwin.js frontend components
284 lines • 12.2 kB
TypeScript
/** @packageDocumentation
* @module WebGL
*/
import { AttributeDetails } from "./AttributeMap";
import { ShaderProgram } from "./ShaderProgram";
import { PositionType } from "./TechniqueFlags";
/** Describes the data type of a shader program variable.
* @internal
*/
export declare const enum VariableType {
Boolean = 0,// bool
Int = 1,// int
Float = 2,// float
Vec2 = 3,// vec2
Vec3 = 4,// vec3
Vec4 = 5,// vec4
Mat3 = 6,// mat3
Mat4 = 7,// mat4
Sampler2D = 8,// sampler2D
SamplerCube = 9,// samplerCube
Uint = 10,// uint
BVec2 = 11,// bvec2
COUNT = 12
}
/** Describes the qualifier associated with a shader program variable.
* @internal
*/
export declare const enum VariableScope {
Global = 0,// no qualifier
Varying = 1,// varying
Uniform = 2,// uniform
COUNT = 3
}
/** Describes the declared or undeclared precision of a shader program variable.
* @internal
*/
export declare const enum VariablePrecision {
Default = 0,// undeclared precision - variable uses the explicit or implicit precision default for its type
Low = 1,// lowp
Medium = 2,// mediump
High = 3,// highp
COUNT = 4
}
/**
* Function invoked by ShaderVariable::AddBinding() to bind the variable to the compiled program.
* The implementation should call ShaderProgram::AddShaderUniform or ShaderProgram::AddGraphicUniform/Attribute to register a function
* which can be used to bind the value of the variable when program is used.
* @internal
*/
export type AddVariableBinding = (prog: ShaderProgram) => void;
/** Represents a variable within a fragment or vertex shader.
* @internal
*/
export declare class ShaderVariable {
private readonly _addBinding?;
readonly name: string;
readonly value?: string;
readonly type: VariableType;
readonly scope: VariableScope;
readonly precision: VariablePrecision;
readonly isConst: boolean;
readonly length: number;
private constructor();
static create(name: string, type: VariableType, scope: VariableScope, addBinding?: AddVariableBinding, precision?: VariablePrecision): ShaderVariable;
static createArray(name: string, type: VariableType, length: number, scope: VariableScope, addBinding?: AddVariableBinding, precision?: VariablePrecision): ShaderVariable;
static createGlobal(name: string, type: VariableType, value?: string, isConst?: boolean): ShaderVariable;
get hasBinding(): boolean;
addBinding(prog: ShaderProgram): void;
get typeName(): string;
getScopeName(isVertexShader: boolean): string;
get precisionName(): string;
/** Constructs the single-line declaration of this variable */
buildDeclaration(isVertexShader: boolean): string;
}
/**
* Represents the set of variables defined and used within a fragment or vertex shader.
* If the same variable is used in both the fragment and vertex shader (e.g., a varying variable), it should be defined in both ShaderBuilders' ShaderVariables object.
* @internal
*/
export declare class ShaderVariables {
protected _list: ShaderVariable[];
/** Find an existing variable with the specified name */
find(name: string): ShaderVariable | undefined;
/** Add a new variable, if a variable with the same name does not already exist. return true if added */
addVariable(v: ShaderVariable): boolean;
addUniform(name: string, type: VariableType, binding: AddVariableBinding, precision?: VariablePrecision): void;
addUniformArray(name: string, type: VariableType, length: number, binding: AddVariableBinding): void;
addVarying(name: string, type: VariableType): boolean;
addGlobal(name: string, type: VariableType, value?: string, isConst?: boolean): void;
addConstant(name: string, type: VariableType, value: string): void;
addBitFlagConstant(name: string, value: number): void;
/** Constructs the lines of glsl code declaring the variables of a certain scope. */
buildScopeDeclarations(isVertexShader: boolean, scope: VariableScope, constants?: boolean): string;
/** Constructs the lines of glsl code declaring all of the variables. */
buildDeclarations(isVertexShader: boolean): string;
/**
* For every uniform and attribute variable not contained in the optional 'predefined' list, invokes the associated binding function
* to add the corresponding Uniform or Attribute object to the ShaderProgram.
*/
addBindings(prog: ShaderProgram, predefined?: ShaderVariables): void;
get length(): number;
private findSlot;
checkMaxVaryingVectors(fragSource: string): string;
computeNumVaryingVectors(fragSource: string): number;
}
/** Convenience API for assembling glsl source code.
* @internal
*/
export declare class SourceBuilder {
source: string;
add(what: string): void;
newline(): void;
addline(what: string): void;
/**
* Construct a function definition given the function signature and body. For example:
* buildFunctionDefinition("float average(float a, float b)", "\n return (a + b) / 2.0;\n");
* will produce:
* "float average(float a, float b) {
* return (a + b) / 2.0;
* }"
* For an inline function:
* buildFunctionDefinition("float average(float a, float b)", "return (a + b) / 2.0;");
* will produce:
* "float average(float a, float b) { return (a + b) / 2.0; }"
*/
static buildFunctionDefinition(declaration: string, implementation: string): string;
/** Constructs a function definition as described by buildFunctionDefinition() and appends it to the glsl source. */
addFunction(declaration: string, implementation: string): void;
/** Constructs the definition of the main() function using the supplied function body and appends it to the glsl source. */
addMain(implementation: string): void;
}
/** @internal */
export interface ShaderBuilderFlags {
/** If defined and true, the geometry is instanced. */
readonly instanced?: boolean;
/** If defined, indicates the vertex data comes from a texture and specifies whether the positions are quantized 16-bit integers or unquantized 32-bit floats. */
readonly positionType?: PositionType;
}
/**
* Represents a fragment or vertex shader under construction. The shader consists of a set of defined variables,
* plus a set of code snippets which can be concatenated together to form the shader source.
* @internal
*/
export declare class ShaderBuilder extends ShaderVariables {
protected _components: (string | undefined)[];
protected _functions: string[];
protected _extensions: string[];
protected _macros: string[];
protected _fragOutputs: string[];
protected _version: string;
headerComment: string;
protected readonly _flags: ShaderBuilderFlags;
protected _initializers: string[];
get usesInstancedGeometry(): boolean;
addInitializer(initializer: string): void;
protected constructor(maxComponents: number, flags: ShaderBuilderFlags);
protected addComponent(index: number, component: string): void;
protected removeComponent(index: number): void;
protected getComponent(index: number): string | undefined;
addFunction(declarationOrFull: string, implementation?: string): void;
replaceFunction(existing: string, replacement: string): boolean;
findFunction(func: string): string | undefined;
addExtension(extName: string): void;
addMacro(macro: string): void;
addDefine(name: string, value: string): void;
clearFragOutput(): void;
addFragOutput(name: string, value: number): void;
protected buildPreludeCommon(attrMap: Map<string, AttributeDetails> | undefined, isVertexShader: boolean): SourceBuilder;
protected copyCommon(src: ShaderBuilder): void;
}
/** Describes the optional and required components which can be assembled into complete
* @internal
*/
export declare const enum VertexShaderComponent {
ComputeQuantizedPosition = 0,
AdjustRawPosition = 1,
CheckForEarlyDiscard = 2,
ComputeFeatureOverrides = 3,
ComputeMaterial = 4,
ComputeBaseColor = 5,
ApplyMaterialColor = 6,
ApplyFeatureColor = 7,
AdjustContrast = 8,
CheckForDiscard = 9,
ComputePosition = 10,
ComputeAtmosphericScatteringVaryings = 11,
CheckForLateDiscard = 12,
FinalizePosition = 13,
COUNT = 14
}
/** Assembles the source code for a vertex shader from a set of modular components.
* @internal
*/
export declare class VertexShaderBuilder extends ShaderBuilder {
private _computedVarying;
private buildPrelude;
constructor(flags?: ShaderBuilderFlags);
get usesVertexTable(): boolean;
get positionType(): PositionType;
get(id: VertexShaderComponent): string | undefined;
set(id: VertexShaderComponent, component: string): void;
unset(id: VertexShaderComponent): void;
addComputedVarying(name: string, type: VariableType, computation: string): void;
buildSource(attrMap?: Map<string, AttributeDetails>): string;
copyFrom(src: VertexShaderBuilder): void;
}
/** Describes the optional and required components which can be assembled into complete
* @internal
*/
export declare const enum FragmentShaderComponent {
CheckForEarlyDiscard = 0,
ComputeBaseColor = 1,
ApplyMaterialOverrides = 2,
FinalizeBaseColor = 3,
CheckForDiscard = 4,
DiscardByAlpha = 5,
ApplyMonochrome = 6,
ApplyThematicDisplay = 7,
ApplyLighting = 8,
ReverseWhiteOnWhite = 9,
ApplyClipping = 10,
ApplyContours = 11,
ApplyFlash = 12,
ApplyPlanarClassifier = 13,
ApplyDraping = 14,
ApplySolarShadowMap = 15,
ApplyWiremesh = 16,
ApplyDebugColor = 17,
AssignFragData = 18,
OverrideFeatureId = 19,
FinalizeDepth = 20,
OverrideColor = 21,
OverrideRenderOrder = 22,
ComputeContourLineInfo = 23,
ApplyAtmosphericScattering = 24,
FinalizeNormal = 25,
COUNT = 26
}
/** Assembles the source code for a fragment shader from a set of modular components.
* @internal
*/
export declare class FragmentShaderBuilder extends ShaderBuilder {
requiresEarlyZWorkaround: boolean;
constructor(flags?: ShaderBuilderFlags);
get(id: FragmentShaderComponent): string | undefined;
set(id: FragmentShaderComponent, component: string): void;
unset(id: FragmentShaderComponent): void;
addDrawBuffersExtension(n: number): void;
buildSource(): string;
private buildPrelude;
copyFrom(src: FragmentShaderBuilder): void;
}
/** @internal */
export declare const enum ShaderType {
Fragment = 1,
Vertex = 2,
Both = 3
}
/**
* Assembles vertex and fragment shaders from a set of modular components to produce a compiled ShaderProgram.
* Be very careful with components which use samplers to ensure that no conflicts exist with texture units used by other components (see TextureUnit enum).
* @internal
*/
export declare class ProgramBuilder {
readonly vert: VertexShaderBuilder;
readonly frag: FragmentShaderBuilder;
private readonly _flags;
private readonly _attrMap?;
constructor(attrMap?: Map<string, AttributeDetails>, flags?: ShaderBuilderFlags);
private addVariable;
addUniform(name: string, type: VariableType, binding: AddVariableBinding, which?: ShaderType): void;
addUniformArray(name: string, type: VariableType, length: number, binding: AddVariableBinding, which?: ShaderType): void;
addVarying(name: string, type: VariableType): void;
addGlobal(name: string, type: VariableType, which?: ShaderType, value?: string, isConst?: boolean): void;
addInlineComputedVarying(name: string, type: VariableType, inlineComputation: string): void;
addFunctionComputedVarying(name: string, type: VariableType, funcName: string, funcBody: string): void;
addFunctionComputedVaryingWithArgs(name: string, type: VariableType, funcCall: string, funcDef: string): void;
/** Assembles the vertex and fragment shader code and returns a ready-to-compile shader program */
buildProgram(gl: WebGL2RenderingContext): ShaderProgram;
setDebugDescription(description: string): void;
/** Returns a deep copy of this program builder. */
clone(): ProgramBuilder;
}
//# sourceMappingURL=ShaderBuilder.d.ts.map