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

ShaderData add get shader properties ability #1030

Merged
merged 14 commits into from
Sep 14, 2022
3 changes: 3 additions & 0 deletions packages/core/src/shader/Shader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import { ShaderProperty } from "./ShaderProperty";
export class Shader {
/** @internal */
static readonly _compileMacros: ShaderMacroCollection = new ShaderMacroCollection();
/** @internal */
static _propertyIdMap: Record<number, ShaderProperty> = Object.create(null);

private static _shaderCounter: number = 0;
private static _shaderMap: Record<string, Shader> = Object.create(null);
Expand Down Expand Up @@ -92,6 +94,7 @@ export class Shader {
} else {
const property = new ShaderProperty(name);
propertyNameMap[name] = property;
Shader._propertyIdMap[property._uniqueId] = property;
return property;
}
}
Expand Down
97 changes: 62 additions & 35 deletions packages/core/src/shader/ShaderData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { IRefObject } from "../asset/IRefObject";
import { CloneManager } from "../clone/CloneManager";
import { Texture } from "../texture/Texture";
import { ShaderDataGroup } from "./enums/ShaderDataGroup";
import { ShaderPropertyType } from "./enums/ShaderPropertyType";
import { Shader } from "./Shader";
import { ShaderMacro } from "./ShaderMacro";
import { ShaderMacroCollection } from "./ShaderMacroCollection";
Expand Down Expand Up @@ -51,7 +52,7 @@ export class ShaderData implements IRefObject, IClone {

/**
* Get float by shader property.
* @param property - Shader property
* @param property - Shader property, use `Shader.getPropertyByName` to get
* @returns Float
*/
getFloat(property: ShaderProperty): number;
Expand All @@ -71,13 +72,13 @@ export class ShaderData implements IRefObject, IClone {
/**
* Set float by shader property.
* @remarks Corresponding float shader property type.
* @param property - Shader property
* @param property - Shader property, use `Shader.getPropertyByName` to get
* @param value - Float
*/
setFloat(property: ShaderProperty, value: number): void;

setFloat(property: string | ShaderProperty, value: number): void {
this._setData(property, value);
this._setData(property, ShaderPropertyType.Float, value);
}

/**
Expand All @@ -89,7 +90,7 @@ export class ShaderData implements IRefObject, IClone {

/**
* Get int by shader property.
* @param property - Shader property
* @param property - Shader property, use `Shader.getPropertyByName` to get
* @returns Int
*/
getInt(property: ShaderProperty): number;
Expand All @@ -109,13 +110,13 @@ export class ShaderData implements IRefObject, IClone {
/**
* Set int by shader property.
* @remarks Correspondence includes int and bool shader property type.
* @param property - Shader property
* @param property - Shader property, use `Shader.getPropertyByName` to get
* @param value - Int
*/
setInt(property: ShaderProperty, value: number): void;

setInt(property: string | ShaderProperty, value: number): void {
this._setData(property, value);
this._setData(property, ShaderPropertyType.Int, value);
}

/**
Expand All @@ -127,7 +128,7 @@ export class ShaderData implements IRefObject, IClone {

/**
* Get float array by shader property.
* @param property - Shader property
* @param property - Shader property, use `Shader.getPropertyByName` to get
* @returns Float array
*/
getFloatArray(property: ShaderProperty): Float32Array;
Expand All @@ -147,13 +148,13 @@ export class ShaderData implements IRefObject, IClone {
/**
* Set float array by shader property.
* @remarks Correspondence includes float array、vec2 array、vec3 array、vec4 array and matrix array shader property type.
* @param property - Shader property
* @param property - Shader property, use `Shader.getPropertyByName` to get
* @param value - Float array
*/
setFloatArray(property: ShaderProperty, value: Float32Array): void;

setFloatArray(property: string | ShaderProperty, value: Float32Array): void {
this._setData(property, value);
this._setData(property, ShaderPropertyType.FloatArray, value);
}

/**
Expand All @@ -165,7 +166,7 @@ export class ShaderData implements IRefObject, IClone {

/**
* Get int array by shader property.
* @param property - Shader property
* @param property - Shader property, use `Shader.getPropertyByName` to get
* @returns Int Array
*/
getIntArray(property: ShaderProperty): Int32Array;
Expand All @@ -185,13 +186,13 @@ export class ShaderData implements IRefObject, IClone {
/**
* Set int array by shader property.
* @remarks Correspondence includes bool array、int array、bvec2 array、bvec3 array、bvec4 array、ivec2 array、ivec3 array and ivec4 array shader property type.
* @param property - Shader property
* @param property - Shader property, use `Shader.getPropertyByName` to get
* @param value - Int Array
*/
setIntArray(property: ShaderProperty, value: Int32Array): void;

setIntArray(property: string | ShaderProperty, value: Int32Array): void {
this._setData(property, value);
this._setData(property, ShaderPropertyType.IntArray, value);
}

/**
Expand All @@ -203,7 +204,7 @@ export class ShaderData implements IRefObject, IClone {

/**
* Get two-dimensional from shader property.
* @param property - Shader property
* @param property - Shader property, use `Shader.getPropertyByName` to get
* @returns Two-dimensional vector
*/
getVector2(property: ShaderProperty): Vector2;
Expand All @@ -223,13 +224,13 @@ export class ShaderData implements IRefObject, IClone {
/**
* Set two-dimensional vector from shader property.
* @remarks Correspondence includes vec2、ivec2 and bvec2 shader property type.
* @param property - Shader property
* @param property - Shader property, use `Shader.getPropertyByName` to get
* @param value - Two-dimensional vector
*/
setVector2(property: ShaderProperty, value: Vector2): void;

setVector2(property: string | ShaderProperty, value: Vector2): void {
this._setData(property, value);
this._setData(property, ShaderPropertyType.Vector2, value);
}

/**
Expand All @@ -241,7 +242,7 @@ export class ShaderData implements IRefObject, IClone {

/**
* Get vector3 by shader property.
* @param property - Shader property
* @param property - Shader property, use `Shader.getPropertyByName` to get
* @returns Three-dimensional vector
*/
getVector3(property: ShaderProperty): Vector3;
Expand All @@ -261,13 +262,13 @@ export class ShaderData implements IRefObject, IClone {
/**
* Set three dimensional vector by shader property.
* @remarks Correspondence includes vec3、ivec3 and bvec3 shader property type.
* @param property - Shader property
* @param property - Shader property, use `Shader.getPropertyByName` to get
* @param value - Three-dimensional vector
*/
setVector3(property: ShaderProperty, value: Vector3): void;

setVector3(property: string | ShaderProperty, value: Vector3): void {
this._setData(property, value);
this._setData(property, ShaderPropertyType.Vector3, value);
}

/**
Expand All @@ -279,7 +280,7 @@ export class ShaderData implements IRefObject, IClone {

/**
* Get vector4 by shader property.
* @param property - Shader property
* @param property - Shader property, use `Shader.getPropertyByName` to get
* @returns Four-dimensional vector
*/
getVector4(property: ShaderProperty): Vector4;
Expand All @@ -299,13 +300,13 @@ export class ShaderData implements IRefObject, IClone {
/**
* Set four-dimensional vector by shader property.
* @remarks Correspondence includes vec4、ivec4 and bvec4 shader property type.
* @param property - Shader property
* @param property - Shader property, use `Shader.getPropertyByName` to get
* @param value - Four-dimensional vector
*/
setVector4(property: ShaderProperty, value: Vector4): void;

setVector4(property: string | ShaderProperty, value: Vector4): void {
this._setData(property, value);
this._setData(property, ShaderPropertyType.Vector4, value);
}

/**
Expand All @@ -317,7 +318,7 @@ export class ShaderData implements IRefObject, IClone {

/**
* Get matrix by shader property.
* @param property - Shader property
* @param property - Shader property, use `Shader.getPropertyByName` to get
* @returns Matrix
*/
getMatrix(property: ShaderProperty): Matrix;
Expand All @@ -337,13 +338,13 @@ export class ShaderData implements IRefObject, IClone {
/**
* Set matrix by shader property.
* @remarks Correspondence includes matrix shader property type.
* @param property - Shader property
* @param property - Shader property, use `Shader.getPropertyByName` to get
* @param value - Matrix
*/
setMatrix(property: ShaderProperty, value: Matrix);

setMatrix(property: string | ShaderProperty, value: Matrix): void {
this._setData(property, value);
this._setData(property, ShaderPropertyType.Matrix, value);
}

/**
Expand All @@ -355,7 +356,7 @@ export class ShaderData implements IRefObject, IClone {

/**
* Get color by shader property.
* @param property - Shader property
* @param property - Shader property, use `Shader.getPropertyByName` to get
* @returns Color
*/
getColor(property: ShaderProperty): Color;
Expand All @@ -375,13 +376,13 @@ export class ShaderData implements IRefObject, IClone {
/**
* Set color by shader property.
* @remarks Correspondence includes vec4 shader property type.
* @param property - Shader property
* @param property - Shader property, use `Shader.getPropertyByName` to get
* @param value - Color
*/
setColor(property: ShaderProperty, value: Color): void;

setColor(property: string | ShaderProperty, value: Color): void {
this._setData(property, value);
this._setData(property, ShaderPropertyType.Color, value);
}

/**
Expand All @@ -393,7 +394,7 @@ export class ShaderData implements IRefObject, IClone {

/**
* Get texture by shader property.
* @param property - Shader property
* @param property - Shader property, use `Shader.getPropertyByName` to get
* @returns Texture
*/
getTexture(property: ShaderProperty): Texture;
Expand All @@ -411,7 +412,7 @@ export class ShaderData implements IRefObject, IClone {

/**
* Set texture by shader property.
* @param property - Shader property
* @param property - Shader property, use `Shader.getPropertyByName` to get
* @param value - Texture
*/
setTexture(property: ShaderProperty, value: Texture): void;
Expand All @@ -422,7 +423,7 @@ export class ShaderData implements IRefObject, IClone {
lastValue && lastValue._addRefCount(-1);
value && value._addRefCount(1);
}
this._setData(property, value);
this._setData(property, ShaderPropertyType.Texture, value);
}

/**
Expand All @@ -434,7 +435,7 @@ export class ShaderData implements IRefObject, IClone {

/**
* Get texture array by shader property.
* @param property - Shader property
* @param property - Shader property, use `Shader.getPropertyByName` to get
* @returns Texture array
*/
getTextureArray(property: ShaderProperty): Texture[];
Expand All @@ -452,7 +453,7 @@ export class ShaderData implements IRefObject, IClone {

/**
* Set texture array by shader property.
* @param property - Shader property
* @param property - Shader property, use `Shader.getPropertyByName` to get
* @param value - Texture array
*/
setTextureArray(property: ShaderProperty, value: Texture[]): void;
Expand All @@ -471,7 +472,7 @@ export class ShaderData implements IRefObject, IClone {
}
}
}
this._setData(property, value);
this._setData(property, ShaderPropertyType.TextureArray, value);
}

/**
Expand Down Expand Up @@ -560,6 +561,20 @@ export class ShaderData implements IRefObject, IClone {
}
}

/**
* Get all shader properties that have been set on this shaderData
* @param out - All shader properties
*/
getShaderProperties(out: ShaderProperty[]): void {
GuoLei1990 marked this conversation as resolved.
Show resolved Hide resolved
out.length = 0;

const properties = this._properties;
const propertyIdMap = Shader._propertyIdMap;
for (let key in properties) {
out.push(propertyIdMap[key]);
}
}

clone(): ShaderData {
const shaderData = new ShaderData(this._group);
this.cloneTo(shaderData);
Expand Down Expand Up @@ -610,7 +625,11 @@ export class ShaderData implements IRefObject, IClone {
/**
* @internal
*/
_setData<T extends ShaderPropertyValueType>(property: string | ShaderProperty, value: T): void {
_setData<T extends ShaderPropertyValueType>(
property: string | ShaderProperty,
type: ShaderPropertyType,
value: T
): void {
if (typeof property === "string") {
property = Shader.getPropertyByName(property);
}
Expand All @@ -619,7 +638,15 @@ export class ShaderData implements IRefObject, IClone {
if (property._group === undefined) {
property._group = this._group;
} else {
throw `Shader property ${property.name} has been used as ${ShaderDataGroup[property._group]} property.`;
throw `Shader property ${property.name} has been used as ${ShaderDataGroup[property._group]} group.`;
}
}

if (property._type !== type) {
if (property._type === undefined) {
property._type = type;
} else {
throw `Shader property ${property.name} has been used as ${ShaderPropertyType[property._type]} type.`;
}
}

Expand Down
7 changes: 4 additions & 3 deletions packages/core/src/shader/ShaderProgram.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ export class ShaderProgram {
/**
* Grouping other data.
*/
groupingOtherUniformBlock() {
groupingOtherUniformBlock(): void {
const { constUniforms, textureUniforms } = this.otherUniformBlock;
constUniforms.length > 0 && this._groupingSubOtherUniforms(constUniforms, false);
textureUniforms.length > 0 && this._groupingSubOtherUniforms(textureUniforms, true);
Expand Down Expand Up @@ -309,7 +309,7 @@ export class ShaderProgram {
/**
* record the location of uniform/attribute.
*/
private _recordLocation() {
private _recordLocation(): void {
const gl = this._gl;
const program = this._glProgram;
const uniformInfos = this._getUniformInfos();
Expand All @@ -325,7 +325,6 @@ export class ShaderProgram {
isArray = true;
}

const group = Shader._getShaderPropertyGroup(name);
const location = gl.getUniformLocation(program, name);
shaderUniform.name = name;
shaderUniform.propertyId = Shader.getPropertyByName(name)._uniqueId;
Expand Down Expand Up @@ -449,6 +448,8 @@ export class ShaderProgram {
}
break;
}

const group = Shader._getShaderPropertyGroup(name);
this._groupingUniform(shaderUniform, group, isTexture);
});

Expand Down
Loading