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

Refactoring the ShaderLab Compiler Module using the LALR Algorithm for Enhanced GLSL Compatibility and Reduced Build Size #2113

Merged
merged 111 commits into from
Jul 15, 2024

Conversation

Sway007
Copy link
Member

@Sway007 Sway007 commented Jun 13, 2024

Please check if the PR fulfills these requirements

  • The commit message follows our guidelines
  • Tests for the changes have been added (for bug fixes / features)
  • Docs have been added / updated (for bug fixes / features)

What kind of change does this PR introduce? (Bug fix, feature, docs update, ...)

In this update, we made a significant overhaul of the ShaderLab module, employing the LALR (Look-Ahead Left-to-Right) parsing algorithm to achieve several substantial improvements over the previous version. This refactoring effort aligns more closely with the GLES syntax standard, providing improved compatibility with the GLSL language. Below are the key highlights of this update:

  1. Completely Rewrite using the LALR Algorithm
    The compiler has been rebuilt from the ground up utilizing the LALR algorithm. This strategic choice not only enhances the parsing efficiency but also ensures that our compiler adheres closely to the GLES syntax standard. Through this approach, we have achieved a more robust compatibility with GLSL, catering to a wider range of shader programming needs.

  2. Elimination of Third-party Dependency
    A significant advancement in this version is the removal of third-party package chevrotain dependency. This decision drastically reduces the overall package size, leading to leaner build outputs. This reduction in dependencies not only simplifies the build process but also minimizes potential conflicts and enhances the portability of the compiler.

  3. User-friendly Programming with Less Syntax Restriction
    Compared to version 1.2, the current iteration of the compiler introduces fewer syntax restrictions, offering a more user-friendly programming experience. We've also simplified syntax extension, making it easier for developers to introduce new features and customizations. This flexibility encourages experimentation and innovation within shader programming.

Does this PR introduce a breaking change? (What changes might users need to make in their application due to this PR?)

  1. Struct declaration mandates a semicolon at the end of a struct declaration

    struct Light {
        vec4 position;
        vec4 color;
    }

    After update

    struct Light {
        vec4 position;
        vec4 color;
    };
  2. Declare shader main entry function (vertex and fragment shader main) assignment after main function definition

    Pass "default" {
        ...
        VertexShader = vert;
        FragmentShader = frag;
        
        v2f vert(a2v o) {
            ...
        }
    
        void frag(v2f i) {
           ...
        }
        ...
    }

    After update

    Pass "default" {
        ...
        v2f vert(a2v o) {
            ...
        }
    
        void frag(v2f i) {
           ...
        }
    
        VertexShader = vert;
        FragmentShader = frag;
        ...
    }

@Sway007 Sway007 added enhancement New feature or request shader Shader related functions high priority High priority issue labels Jun 13, 2024
@Sway007 Sway007 added this to the 1.3 milestone Jun 13, 2024
@Sway007 Sway007 requested a review from GuoLei1990 June 13, 2024 08:38
@Sway007 Sway007 self-assigned this Jun 13, 2024
@Sway007 Sway007 requested a review from zhuxudong June 13, 2024 08:39
@Sway007 Sway007 linked an issue Jun 17, 2024 that may be closed by this pull request
16 tasks
@zhuxudong
Copy link
Member

VertexShader = *** can before function definition in Unity

const ast = this._visitor.visit(cst);
const shaderInfo = this._context.parse(ast);
return shaderInfo;
parseShader(source: string, includeMap: Record<string, string> = {}, backend = EBackend.GLES300) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

includeMap it's not exported in Shader.create(***)

@GuoLei1990
Copy link
Member

Merge logger to engine logger

}
for (let i = 0; i < platformMacros.length; i++) {
Preprocessor.addPredefinedMacro(platformMacros[0]);
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • platformMacros[0]???
  • Why not cache n?

packages/shader-lab/src/lalr/State.ts Show resolved Hide resolved
@GuoLei1990 GuoLei1990 merged commit 1a18708 into galacean:dev/1.3 Jul 15, 2024
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request high priority High priority issue shader Shader related functions
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Refactor ShaderLab with compiler design principles
3 participants