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

[glsl-in][wgsl-out] Nested loop miscompilation #1935

Closed
Uriopass opened this issue May 21, 2022 · 0 comments · Fixed by #2029
Closed

[glsl-in][wgsl-out] Nested loop miscompilation #1935

Uriopass opened this issue May 21, 2022 · 0 comments · Fixed by #2029
Labels
area: front-end Input formats for conversion kind: bug Something isn't working lang: GLSL OpenGL Shading Language

Comments

@Uriopass
Copy link
Contributor

The following glsl code with a simple nested loop:

main.frag
void main() {
    float total = 0.0;

    for (int y = -1 ; y <= 1 ; y++) {
        for (int x = -1; x <= 1; x++) {
            total += x + y;
        }
    }
}

when translated to wgsl gives

main.wgsl
fn main_1() {
    var total: f32 = 0.0;
    var y: i32 = -1;
    var x: i32 = -1;

    loop {
        let _e5 = y;
        if (!((_e5 <= 1))) {
            break;
        }
        {
            loop {
                let _e15 = x;
                if (!((_e15 <= 1))) {
                    break;
                }
                {
                    let _e22 = total;
                    let _e23 = x;
                    let _e24 = y;
                    total = (_e22 + f32((_e23 + _e24)));
                }
                continuing {
                    let _e19 = x;
                    x = (_e19 + 1);
                }
            }
        }
        continuing {
            let _e9 = y;
            y = (_e9 + 1);
        }
    }
    return;
}

[[stage(fragment)]]
fn main() {
    main_1();
    return;
}

Which is obviously wrong as x is not declared within the scope of the first nested loop, thus reusing it and summing total to -3 instead of 0.

I got this bug while implementing PCF, nested loops do exist in shaders! :D

Note that trying to declare the x out of the for like so doesn't work:

main.frag
void main() {
    float total = 0.0;

    for (int y = -1 ; y <= 1 ; y++) {
        int x = -1;
        for (; x <= 1; x++) {
            total += x + y;
        }
    }
}

Workaround

Declare main.frag like so:

main.frag
void main() {
    float total = 0.0;
    int x;
    for (int y = -1 ; y <= 1 ; y++) {
        x = -1;
        for (; x <= 1; x++) {
            total += x + y;
        }
    }
}

Tested with naga-cli 8.0. I've searched through naga's issue but didn't find anything like it.

@teoxoy teoxoy added kind: bug Something isn't working area: front-end Input formats for conversion lang: GLSL OpenGL Shading Language labels May 23, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: front-end Input formats for conversion kind: bug Something isn't working lang: GLSL OpenGL Shading Language
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants