From 6fb9107c913c74616a7ca39956155d5a79d542ab Mon Sep 17 00:00:00 2001 From: wangjin Date: Fri, 29 Dec 2023 00:33:49 +0800 Subject: [PATCH] Add option to toggle GLSL preprocess --- package.json | 3 ++ src/embeddedFrontend/resultView/resultView.ts | 5 +++ .../sourceCode/sourceCodeComponent.ts | 32 ++++++++++++++++--- 3 files changed, 35 insertions(+), 5 deletions(-) diff --git a/package.json b/package.json index 0b76b55..7e2a427 100644 --- a/package.json +++ b/package.json @@ -33,6 +33,9 @@ "build": "run-s build:tslint build:bundle build:copybuild:copy:bundle build:concatBundleFunc -n", "test": "echo \"Error: no test specified\" && exit 1" }, + "dependencies": { + "@shaderfrog/glsl-parser": "^2.0.1" + }, "devDependencies": { "@types/webxr": "^0.5.1", "concat-cli": "^4.0.0", diff --git a/src/embeddedFrontend/resultView/resultView.ts b/src/embeddedFrontend/resultView/resultView.ts index afe84ea..ffa2474 100644 --- a/src/embeddedFrontend/resultView/resultView.ts +++ b/src/embeddedFrontend/resultView/resultView.ts @@ -186,6 +186,11 @@ export class ResultView { state.beautify = (sourceCodeState.sender as HTMLInputElement).checked; this.mvx.updateState(this.sourceCodeComponentStateId, state); }); + this.sourceCodeComponent.onPreprocessChanged.add((sourceCodeState) => { + const state = this.mvx.getGenericState(this.sourceCodeComponentStateId); + state.preprocessed = (sourceCodeState.sender as HTMLInputElement).checked; + this.mvx.updateState(this.sourceCodeComponentStateId, state); + }); this.updateViewState(); diff --git a/src/embeddedFrontend/resultView/sourceCode/sourceCodeComponent.ts b/src/embeddedFrontend/resultView/sourceCode/sourceCodeComponent.ts index 716fc6b..935d808 100644 --- a/src/embeddedFrontend/resultView/sourceCode/sourceCodeComponent.ts +++ b/src/embeddedFrontend/resultView/sourceCode/sourceCodeComponent.ts @@ -1,5 +1,7 @@ +import preprocess from "@shaderfrog/glsl-parser/preprocessor"; // tslint:disable-line:no-submodule-imports import { BaseComponent, IStateEvent } from "../../mvx/baseComponent"; import { ISourceCodeChangeEvent } from "../resultView"; +import { Logger } from "../../../shared/utils/logger"; export interface ISourceCodeState extends ISourceCodeChangeEvent { nameVertex: string; @@ -8,6 +10,7 @@ export interface ISourceCodeState extends ISourceCodeChangeEvent { translated: boolean; editable: boolean; beautify: boolean; + preprocessed: boolean; } // Declare Ace types here. @@ -43,6 +46,7 @@ export class SourceCodeComponent extends BaseComponent { public onSourceCodeCloseClicked: IStateEvent; public onSourceCodeChanged: IStateEvent; public onBeautifyChanged: IStateEvent; + public onPreprocessChanged: IStateEvent; private editor: IAceEditor; @@ -55,6 +59,7 @@ export class SourceCodeComponent extends BaseComponent { this.onSourceCodeCloseClicked = this.createEvent("onSourceCodeCloseClicked"); this.onSourceCodeChanged = this.createEvent("onSourceCodeChanged"); this.onBeautifyChanged = this.createEvent("onBeautifyChanged"); + this.onPreprocessChanged = this.createEvent("onPreprocessChanged"); } public showError(errorMessage: string) { @@ -86,33 +91,50 @@ export class SourceCodeComponent extends BaseComponent { public render(state: ISourceCodeState, stateId: number): Element { const source = state.fragment ? state.sourceFragment : state.sourceVertex; let originalShader: string; + let preprocessed = state.preprocessed; + // tslint:disable-next-line:prefer-conditional-expression if (state.translated) { originalShader = state.fragment ? state.translatedSourceFragment : state.translatedSourceVertex; + preprocessed = false; } else { originalShader = source ?? ""; } - const displayedShader = state.beautify ? this._indentIfdef(this._beautify(originalShader)) : originalShader; + let displayedShader = originalShader; + if (preprocessed) { + try { + displayedShader = preprocess(displayedShader, { + preserveComments: false, + stopOnError: true + }); + } catch (e) { + Logger.error("shader preprocess failed", e); + } + } + + if (state.beautify) { + displayedShader = this._indentIfdef(this._beautify(displayedShader)); + } const htmlString = this.htmlTemplate`
- $${ - this.htmlTemplate`
${displayedShader}
` + $${this.htmlTemplate`
${displayedShader}
` }

+

`;