CrossGL / crosstl

Translates native shader languages into CrossGL universal shader language and vice versa.
https://crossgl.net
Apache License 2.0
36 stars 39 forks source link

Add else if to translator #32 #104

Closed AxelB1011 closed 1 week ago

AxelB1011 commented 2 weeks ago

PR Description

Modified the following files in the crosstl/src/translator directory:

parser.py: Updated the the parse_if_statement() function. directx_codegen.py: Implemented else if for HLSL output. metal_codegen.py: Implemented else if for Metal output. opengl_codegen.py: Implemented else if for GLSL output. test_lexer.py, test_parser.py, test_directx_codegen.py, test_metal_codegen.py, test_opengl_codegen.py: Added tests for multiple else if conditionals in test_translator and test_translator/test_codegen.

Related Issue

Closed #32

shader Sample

shader PerlinNoise {
    vertex {
        input vec3 position;
        output vec2 vUV;

        void main() {
            vUV = position.xy * 10.0;
            if (vUV.x < 0.5) {
                vUV.x = 0.25;
            }
            if (vUV.x < 0.25) {
                vUV.x = 0.0;
            } else if (vUV.x < 0.75) {
                vUV.x = 0.5;
            } else if (vUV.x < 1.0) {
                vUV.x = 0.75;
            } else {
                vUV.x = 0.0;
            }
            gl_Position = vec4(position, 1.0);
        }
    }

    // Fragment Shader
    fragment {
        input vec2 vUV;
        output vec4 fragColor;

        void main() {
            if (vUV.x > 0.75) {
                fragColor = vec4(1.0, 1.0, 1.0, 1.0);
            } else if (vUV.x > 0.5) {
                fragColor = vec4(0.5, 0.5, 0.5, 1.0);
            } else if (vUV.x > 0.25) {
                fragColor = vec4(0.25, 0.25, 0.25, 1.0);
            } else {
                fragColor = vec4(0.0, 0.0, 0.0, 1.0);
            }
            fragColor = vec4(color, 1.0);
            }
        }
    }

Checklist

AxelB1011 commented 2 weeks ago

@samthakur587 I have created a new PR as per your advice. Please let me know if this is alright. It might be better to squash all the commits that I've made into one or just cherry pick the latest one but I'm not sure if that is okay

samthakur587 commented 2 weeks ago

hii @AxelB1011 great work so far just one last thing to fix . let's assume you have two continues if statements how do you handle this . the parser not parse this second if state.

can you try this example code for test your code .

shader PerlinNoise {
    vertex {
        input vec3 position;
        output vec2 vUV;

        void main() {
            vUV = position.xy * 10.0;
            if (vUV.x < 0.5) {
                vUV.x = 0.25;
            }
            if (vUV.x < 0.25) {
                vUV.x = 0.0;
            }

            } else if (vUV.x < 0.75) {
                vUV.x = 0.5;
            } else {
                vUV.x = 0.0;
            }
            gl_Position = vec4(position, 1.0);
        }
    }

    // Fragment Shader
    fragment {
        input vec2 vUV;
        output vec4 fragColor;

        void main() {
            if (vUV.x > 0.75) {
                fragColor = vec4(1.0, 1.0, 1.0, 1.0);
            } else if (vUV.x > 0.5) {
                fragColor = vec4(0.5, 0.5, 0.5, 1.0);
            } else {
                fragColor = vec4(0.0, 0.0, 0.0, 1.0);
            }
            fragColor = vec4(color, 1.0);
            }
        }
    }
samthakur587 commented 2 weeks ago

you can take reference from this PR . i fixed this issue here . #39

AxelB1011 commented 2 weeks ago

Thank you for the feedback @samthakur587! I have used your code for reference and updated the tests. The parser seems to be working fine, unless I am missing something..?

hii @AxelB1011 great work so far just one last thing to fix . let's assume you have two continues if statements how do you handle this . the parser not parse this second if state.

can you try this example code for test your code .

shader PerlinNoise {
    vertex {
        input vec3 position;
        output vec2 vUV;

        void main() {
            vUV = position.xy * 10.0;
            if (vUV.x < 0.5) {
                vUV.x = 0.25;
            }
            if (vUV.x < 0.25) {
                vUV.x = 0.0;
            } else if (vUV.x < 0.75) {
                vUV.x = 0.5;
            } else {
                vUV.x = 0.0;
            }
            gl_Position = vec4(position, 1.0);
        }
    }

    // Fragment Shader
    fragment {
        input vec2 vUV;
        output vec4 fragColor;

        void main() {
            if (vUV.x > 0.75) {
                fragColor = vec4(1.0, 1.0, 1.0, 1.0);
            } else if (vUV.x > 0.5) {
                fragColor = vec4(0.5, 0.5, 0.5, 1.0);
            } else {
                fragColor = vec4(0.0, 0.0, 0.0, 1.0);
            }
            fragColor = vec4(color, 1.0);
            }
        }
    }