Skip to content

Commit

Permalink
fix: shader pass compilation cache
Browse files Browse the repository at this point in the history
  • Loading branch information
Sway007 committed Jul 3, 2024
1 parent cc1c0fd commit ad9059e
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 32 deletions.
9 changes: 3 additions & 6 deletions packages/core/src/shader/ShaderPass.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import { ShaderPart } from "./ShaderPart";
import { ShaderProgram } from "./ShaderProgram";
import { ShaderProgramPool } from "./ShaderProgramPool";
import { ShaderProperty } from "./ShaderProperty";
import { RenderStateElementKey } from "./enums/RenderStateElementKey";
import { ShaderType } from "./enums/ShaderType";
import { RenderState } from "./state/RenderState";

Expand Down Expand Up @@ -81,6 +80,7 @@ export class ShaderPass extends ShaderPart {
) {
super();
this._shaderPassId = ShaderPass._shaderPassCounter++;
this._type = ShaderType.Canonical;

if (typeof fragmentSourceOrTags === "string") {
this._name = nameOrVertexSourceOrShaderLabSource;
Expand All @@ -103,6 +103,7 @@ export class ShaderPass extends ShaderPart {
tags = fragmentSourceOrTags ?? {
pipelineStage: PipelineStage.Forward
};
this._type = ShaderType.ShaderLab;
}
for (const key in tags) {
this.setTag(key, tags[key]);
Expand Down Expand Up @@ -130,7 +131,7 @@ export class ShaderPass extends ShaderPart {
return this._getCanonicalShaderProgram(engine, macroCollection);
}

if (!this._isCompiled()) this._compile(engine, macroCollection, this._vertexEntry, this._fragmentEntry);
this._compile(engine, macroCollection, this._vertexEntry, this._fragmentEntry);
shaderProgram = new ShaderProgram(engine, this._vertexSource, this._fragmentSource);

shaderProgramPool.cache(shaderProgram);
Expand All @@ -148,10 +149,6 @@ export class ShaderPass extends ShaderPart {
shaderProgramPools.length = 0;
}

private _isCompiled() {
return this._fragmentSource != undefined;
}

/**
* Shader Lab compilation
*/
Expand Down
48 changes: 24 additions & 24 deletions packages/shader-lab/src/Utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import { EKeyword, ETokenType } from "./common";
import { TreeNode } from "./parser/AST";
import { GalaceanDataType } from "./parser/types";
// #if _DEVELOPMENT
import { createWriteStream } from "fs";
import State from "./lalr/State";
// import { createWriteStream } from "fs";
// import State from "./lalr/State";
// #endif

export class ParserUtils {
Expand Down Expand Up @@ -44,29 +44,29 @@ export class ParserUtils {
* @internal
*/
// #if _DEVELOPMENT
static printStatePool(logPath: string) {
const logStream = createWriteStream(logPath);
// static printStatePool(logPath: string) {
// const logStream = createWriteStream(logPath);

console.log("========== Parser Pool ==========");
// console.log("========== Parser Pool ==========");

let count = 0;
for (const state of State.pool.values()) {
count++;
let tmp = "";
tmp += `${state.id}: \n`.padEnd(4);
for (const psItem of state.items) {
tmp += " " + psItem.toString() + "\n";
}
logStream.write(tmp);
}
logStream.end();
logStream.close();
console.log("state count:", count);
return new Promise((res) => {
logStream.on("finish", () => {
res("");
});
});
}
// let count = 0;
// for (const state of State.pool.values()) {
// count++;
// let tmp = "";
// tmp += `${state.id}: \n`.padEnd(4);
// for (const psItem of state.items) {
// tmp += " " + psItem.toString() + "\n";
// }
// logStream.write(tmp);
// }
// logStream.end();
// logStream.close();
// console.log("state count:", count);
// return new Promise((res) => {
// logStream.on("finish", () => {
// res("");
// });
// });
// }
// #endif
}
4 changes: 2 additions & 2 deletions packages/shader-lab/src/parser/Parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,8 @@ export default class Parser {
this.printStack(nextToken.value);
// #endif
} else if (actionInfo?.action === EAction.Accept) {
this.logger.debug(`Accept! State automata run ${loopCount} times! cost time ${performance.now() - start}ms`);
// console.log(`Accept! State automata run ${loopCount} times! cost time ${performance.now() - start}ms`);
// this.logger.debug(`Accept! State automata run ${loopCount} times! cost time ${performance.now() - start}ms`);
console.log(`Accept! State automata run ${loopCount} times! cost time ${performance.now() - start}ms`);
sematicAnalyzer.acceptRule?.(sematicAnalyzer);
return sematicAnalyzer.semanticStack.pop() as ASTNode.GLShaderProgram;
} else if (actionInfo?.action === EAction.Reduce) {
Expand Down

0 comments on commit ad9059e

Please sign in to comment.