Skip to content

Commit

Permalink
feat: code opt
Browse files Browse the repository at this point in the history
  • Loading branch information
Sway007 committed Jul 9, 2024
1 parent 6e7625c commit 785c205
Show file tree
Hide file tree
Showing 21 changed files with 68 additions and 97 deletions.
2 changes: 1 addition & 1 deletion packages/core/src/shader/Shader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ export class Shader implements IReferable {
throw "ShaderLab has not been set up yet.";
}

const shaderStructInfo = Shader._shaderLab.parseShaderStruct(nameOrShaderSource);
const shaderStructInfo = Shader._shaderLab.parseShaderContent(nameOrShaderSource);
if (shaderMap[shaderStructInfo.name]) {
console.error(`Shader named "${shaderStructInfo.name}" already exists.`);
return;
Expand Down
4 changes: 2 additions & 2 deletions packages/design/src/shader-lab/IShaderLab.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { CodeGenBackEnd } from "./Backend";
import { IShaderInfo } from "./IShaderInfo";
import { ShaderStruct } from "./shaderStruct/ShaderStruct";
import { ShaderContent } from "./shaderContent/ShaderContent";

/**
* Shader lab interface.
Expand All @@ -9,7 +9,7 @@ export interface IShaderLab {
/**
* Parse shader source to get the structure of shader.
*/
parseShaderStruct(shaderSource: string): ShaderStruct;
parseShaderContent(shaderSource: string): ShaderContent;

/**
* Parse shader pass source code.
Expand Down
5 changes: 4 additions & 1 deletion packages/design/src/shader-lab/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
export type { IShaderInfo } from "./IShaderInfo";
export type { IShaderLab } from "./IShaderLab";
export type { ShaderStruct } from "./shaderStruct/ShaderStruct";
export type { ShaderContent } from "./shaderContent/ShaderContent";
export { CodeGenBackEnd } from "./Backend";
export { Statement } from "./shaderContent/Statement";
export { SubShaderContent } from "./shaderContent/SubShaderContent";
export { PassContent } from "./shaderContent/PassContent";
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { RenderStates } from "./RenderStates";

export interface PassStruct {
export interface PassContent {
name: string;
isUsePass: boolean;
// Undefined content when referenced by `UsePass`
Expand Down
10 changes: 10 additions & 0 deletions packages/design/src/shader-lab/shaderContent/ShaderContent.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { RenderStates } from "./RenderStates";
import { Statement } from "./Statement";
import { SubShaderContent } from "./SubShaderContent";

export interface ShaderContent {
name: string;
subShaders: SubShaderContent[];
globalContents: Statement[];
renderStates: RenderStates;
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Position } from "./Position";

export interface statement {
export interface Statement {
content: string;
range: { start: Position; end: Position };
}
11 changes: 11 additions & 0 deletions packages/design/src/shader-lab/shaderContent/SubShaderContent.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { PassContent } from "./PassContent";
import { RenderStates } from "./RenderStates";
import { Statement } from "./Statement";

export interface SubShaderContent {
name: string;
passes: PassContent[];
globalContents: Statement[];
tags?: Record<string, number | string | boolean>;
renderStates: RenderStates;
}
10 changes: 0 additions & 10 deletions packages/design/src/shader-lab/shaderStruct/ShaderStruct.ts

This file was deleted.

11 changes: 0 additions & 11 deletions packages/design/src/shader-lab/shaderStruct/SubShaderStruct.ts

This file was deleted.

10 changes: 5 additions & 5 deletions packages/shader-lab/src/ShaderLab.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import { Parser } from "./parser";
import Preprocessor from "./preprocessor";
import { GLES100Visitor, GLES300Visitor } from "./codeGen";
import { Logger } from "./Logger";
import { ShaderStruct, CodeGenBackEnd, IShaderLab } from "@galacean/engine-design/src/shader-lab";
import { ShaderStructParser } from "./structParser";
import { ShaderContent, CodeGenBackEnd, IShaderLab } from "@galacean/engine-design/src/shader-lab";
import { ShaderContentParser } from "./contentParser";
// @ts-ignore
import { ShaderLib } from "@galacean/engine";

Expand Down Expand Up @@ -48,8 +48,8 @@ export class ShaderLab implements IShaderLab {
return codeGen.visitShaderProgram(program, vertexEntry, fragmentEntry);
}

parseShaderStruct(shaderSource: string): ShaderStruct {
const parser = new ShaderStructParser(shaderSource);
parseShaderContent(shaderSource: string): ShaderContent {
const parser = new ShaderContentParser(shaderSource);
return parser.parse();
}

Expand All @@ -62,7 +62,7 @@ export class ShaderLab implements IShaderLab {
macros: string[] = [],
backend = CodeGenBackEnd.GLES300
): (ReturnType<ShaderLab["parseShaderPass"]> & { name: string })[] {
const structInfo = this.parseShaderStruct(shaderSource);
const structInfo = this.parseShaderContent(shaderSource);
const passResult = [] as any;
for (const subShader of structInfo.subShaders) {
for (const pass of subShader.passes) {
Expand Down
4 changes: 2 additions & 2 deletions packages/shader-lab/src/codeGen/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ShaderStruct } from "@galacean/engine-design";
import { ShaderContent } from "@galacean/engine-design";

export type IRenderState = [
/** Constant RenderState. */
Expand All @@ -7,7 +7,7 @@ export type IRenderState = [
Record<number, string>
];

export type ITag = ShaderStruct["subShaders"][number]["tags"];
export type ITag = ShaderContent["subShaders"][number]["tags"];

export interface IPassCodeGenResult {
vertexSource: string;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import BaseError from "../common/BaseError";
import { SymbolTableStack } from "../common/BaseSymbolTable";
import { BaseToken } from "../common/BaseToken";
import { EKeyword, ETokenType } from "../common";
import { IIndexRange, Position } from "../common";
import { Position } from "../common";
import { KeywordMap } from "./KeywordMap";
import Scanner from "./Scanner";
import SymbolTable, { ISymbol } from "./SymbolTable";
Expand All @@ -16,11 +16,7 @@ import {
BlendFactor,
CullMode
} from "@galacean/engine";

export interface statement {
content: string;
range: IIndexRange;
}
import { Statement, ShaderContent, SubShaderContent, PassContent } from "@galacean/engine-design";

type RenderStates = [
/** Constant RenderState. */
Expand All @@ -29,33 +25,6 @@ type RenderStates = [
Record<number, string>
];

interface ShaderStruct {
name: string;
subShaders: SubShaderStruct[];
globalContents: statement[];
renderStates: RenderStates;
}

interface SubShaderStruct {
name: string;
passes: PassStruct[];
globalContents: statement[];
tags?: Record<string, number | string | boolean>;
renderStates: RenderStates;
}

interface PassStruct {
name: string;
isUsePass: boolean;
// Undefined content when referenced by `UsePass`
globalContents?: statement[];
tags: Record<string, number | string | boolean>;
renderStates: RenderStates;
contents: string;
vertexEntry: string;
fragmentEntry: string;
}

const EngineType = [
EKeyword.GS_RenderQueueType,
EKeyword.GS_BlendFactor,
Expand All @@ -75,7 +44,7 @@ const RenderStateType = [
EKeyword.GS_StencilState
];

export default class ShaderStructParser extends BaseError {
export class ShaderContentParser extends BaseError {
static _engineType = { RenderQueueType, CompareFunction, StencilOperation, BlendOperation, BlendFactor, CullMode };
private static _isRenderStateDeclarator(token: BaseToken) {
return RenderStateType.includes(token.type);
Expand All @@ -94,8 +63,8 @@ export default class ShaderStructParser extends BaseError {
this._newScope();
}

parse(): ShaderStruct {
const ret = { subShaders: [], globalContents: [], renderStates: [{}, {}] } as ShaderStruct;
parse(): ShaderContent {
const ret = { subShaders: [], globalContents: [], renderStates: [{}, {}] } as ShaderContent;
const scanner = this._scanner;

scanner.scanText("Shader");
Expand All @@ -117,14 +86,15 @@ export default class ShaderStructParser extends BaseError {
Object.assign(pass.renderStates[0], curSubShaderRenderStates[0]);
Object.assign(pass.renderStates[1], curSubShaderRenderStates[1]);
if (pass.isUsePass) continue;
// @ts-ignore
const passGlobalStatements = curSubShaderGlobalStatements.concat(pass.globalContents);
pass.contents = passGlobalStatements.map((item) => item.content).join("\n");
}
}
return ret;
}

private _parseShaderStatements(ret: ShaderStruct) {
private _parseShaderStatements(ret: ShaderContent) {
const scanner = this._scanner;
let braceLevel = 1;
let start = scanner.curPosition;
Expand Down Expand Up @@ -164,12 +134,12 @@ export default class ShaderStructParser extends BaseError {
}

default:
if (ShaderStructParser._isRenderStateDeclarator(word)) {
if (ShaderContentParser._isRenderStateDeclarator(word)) {
this._addGlobalStatement(ret, scanner, start, word.lexeme.length);
this._parseRenderStateDeclarationOrAssignment(ret, word);
start = scanner.curPosition;
break;
} else if (ShaderStructParser._isEngineType(word)) {
} else if (ShaderContentParser._isEngineType(word)) {
this._addGlobalStatement(ret, scanner, start, word.lexeme.length);
this._parseVariableDeclaration(word.type);
start = scanner.curPosition;
Expand Down Expand Up @@ -275,7 +245,7 @@ export default class ShaderStructParser extends BaseError {
} else if (this._scanner.curChar() === ".") {
this._scanner._advance();
const engineTypeProp = this._scanner.scanToken();
value = ShaderStructParser._engineType[token.lexeme]?.[engineTypeProp.lexeme];
value = ShaderContentParser._engineType[token.lexeme]?.[engineTypeProp.lexeme];
if (value == undefined)
this.throw(this._scanner.current, "Invalid engine constant:", `${token.lexeme}.${engineTypeProp.lexeme}`);
} else {
Expand All @@ -294,15 +264,15 @@ export default class ShaderStructParser extends BaseError {
this._scanner.scanText("=");
const word = this._scanner.scanToken();
this._scanner.scanText(";");
const value = ShaderStructParser._engineType.RenderQueueType[word.lexeme];
const value = ShaderContentParser._engineType.RenderQueueType[word.lexeme];
if (value == undefined) {
this.throw(this._scanner.current, "Invalid render queue", word.lexeme);
}
const key = RenderStateDataKey.RenderQueueType;
ret.renderStates[0][key] = value;
}

private _addGlobalStatement(ret: { globalContents: statement[] }, scanner: Scanner, start: Position, offset: number) {
private _addGlobalStatement(ret: { globalContents: Statement[] }, scanner: Scanner, start: Position, offset: number) {
if (scanner.current > start.index + offset) {
ret.globalContents.push({
range: { start, end: { ...scanner.curPosition, index: scanner.current - offset - 1 } },
Expand All @@ -311,9 +281,9 @@ export default class ShaderStructParser extends BaseError {
}
}

private _parseSubShader(): SubShaderStruct {
private _parseSubShader(): SubShaderContent {
this._newScope();
const ret = { passes: [], globalContents: [], renderStates: [{}, {}], tags: {} } as SubShaderStruct;
const ret = { passes: [], globalContents: [], renderStates: [{}, {}], tags: {} } as SubShaderContent;
const scanner = this._scanner;
let braceLevel = 1;
ret.name = scanner.scanPairedText('"', '"');
Expand Down Expand Up @@ -364,12 +334,12 @@ export default class ShaderStructParser extends BaseError {
}

default:
if (ShaderStructParser._isRenderStateDeclarator(word)) {
if (ShaderContentParser._isRenderStateDeclarator(word)) {
this._addGlobalStatement(ret, scanner, start, word.lexeme.length);
this._parseRenderStateDeclarationOrAssignment(ret, word);
start = scanner.curPosition;
break;
} else if (ShaderStructParser._isEngineType(word)) {
} else if (ShaderContentParser._isEngineType(word)) {
this._addGlobalStatement(ret, scanner, start, word.lexeme.length);
this._parseVariableDeclaration(word.type);
start = scanner.curPosition;
Expand Down Expand Up @@ -397,9 +367,9 @@ export default class ShaderStructParser extends BaseError {
}
}

private _parsePass(): PassStruct {
const ret = { globalContents: [], renderStates: [{}, {}], tags: {} } as PassStruct & {
globalContents: statement[];
private _parsePass(): PassContent {
const ret = { globalContents: [], renderStates: [{}, {}], tags: {} } as PassContent & {
globalContents: Statement[];
};
const scanner = this._scanner;
ret.name = scanner.scanPairedText('"', '"');
Expand Down Expand Up @@ -452,12 +422,12 @@ export default class ShaderStructParser extends BaseError {
}

default:
if (ShaderStructParser._isRenderStateDeclarator(word)) {
if (ShaderContentParser._isRenderStateDeclarator(word)) {
this._addGlobalStatement(ret, scanner, start, word.lexeme.length);
this._parseRenderStateDeclarationOrAssignment(ret, word);
start = scanner.curPosition;
break;
} else if (ShaderStructParser._isEngineType(word)) {
} else if (ShaderContentParser._isEngineType(word)) {
this._addGlobalStatement(ret, scanner, start, word.lexeme.length);
this._parseVariableDeclaration(word.type);
start = scanner.curPosition;
Expand Down
1 change: 1 addition & 0 deletions packages/shader-lab/src/contentParser/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { ShaderContentParser } from "./ShaderContentParser";
3 changes: 0 additions & 3 deletions packages/shader-lab/src/structParser/index.ts

This file was deleted.

12 changes: 6 additions & 6 deletions tests/src/shader-lab/ShaderLab.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import chai, { expect } from "chai";
import spies from "chai-spies";
import fs from "fs";
import path from "path";
import { ShaderStruct } from "@galacean/engine-design";
import { ShaderContent } from "@galacean/engine-design";

chai.use(spies);
const demoShader = fs.readFileSync(path.join(__dirname, "shaders/demo.shader")).toString();
Expand Down Expand Up @@ -127,13 +127,13 @@ const shaderLab = new ShaderLab(
);

describe("ShaderLab", () => {
let shader: ShaderStruct;
let subShader: ShaderStruct["subShaders"][number];
let passList: ShaderStruct["subShaders"][number]["passes"];
let pass1: ShaderStruct["subShaders"][number]["passes"][number];
let shader: ShaderContent;
let subShader: ShaderContent["subShaders"][number];
let passList: ShaderContent["subShaders"][number]["passes"];
let pass1: ShaderContent["subShaders"][number]["passes"][number];

before(() => {
shader = shaderLab.parseShaderStruct(demoShader);
shader = shaderLab.parseShaderContent(demoShader);
subShader = shader.subShaders[0];
passList = subShader.passes;
expect(passList[0].isUsePass).to.be.true;
Expand Down
6 changes: 3 additions & 3 deletions tests/src/shader-lab/ShaderValidate.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { expect } from "chai";
import { ShaderLab } from "@galacean/engine-shader-lab";
import { Shader, ShaderFactory } from "@galacean/engine-core";
import { ShaderStruct, CodeGenBackEnd } from "@galacean/engine-design/src/shader-lab";
import { ShaderContent, CodeGenBackEnd } from "@galacean/engine-design/src/shader-lab";

function addLineNum(str: string) {
const lines = str.split("\n");
Expand All @@ -20,7 +20,7 @@ function addLineNum(str: string) {
}

function validateShaderPass(
pass: ShaderStruct["subShaders"][number]["passes"][number],
pass: ShaderContent["subShaders"][number]["passes"][number],
vertexSource: string,
fragmentSource: string
) {
Expand Down Expand Up @@ -73,7 +73,7 @@ export function glslValidate(shaderSource, _shaderLab?: ShaderLab, includeMap =
}

const start = performance.now();
const shader = shaderLab.parseShaderStruct(shaderSource);
const shader = shaderLab.parseShaderContent(shaderSource);
console.log("struct compilation time: ", (performance.now() - start).toFixed(2), "ms");
expect(shader).not.be.null;
shader.subShaders.forEach((subShader) => {
Expand Down

0 comments on commit 785c205

Please sign in to comment.