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

Return in a preprocessor #ifdef directive marks the rest of the shader as unreachable code #118

Open
Nehon opened this issue Mar 12, 2016 · 2 comments
Labels

Comments

@Nehon
Copy link

Nehon commented Mar 12, 2016

Hello, I've just noticed an issue with this code :

void main(){    
    #ifdef TEXTURE
          gl_FragColor = texture2D(m_Texture, texCoord);
          return;
    #endif

    gl_FragColor = vec4(1.0);
}

The last line gl_FragColor = vec4(1.0); is marked as unreachable code.

@Darkyenus
Copy link
Owner

Defines are very problematic, because they don't create lexical scopes like normal blocks do and can begin in different block to where they end. So to handle them correctly, we would probably have to create alternate versions of the parse tree for each #ifdef block and parse them as separate alternate universes files. I don't have enough time to investigate other ways to do it or to implement this bruteforce one, so it is unlikely that this issue will ever be fixed (and many many other corner cases which exist because of preprocessor).

That said, in this exact case, the unreachable code inspection is actually semi-right, it will be unreachable if the TEXTURE is false :)

As a workaround, the code can be rewritten as:

#define TEXTURE true
// or
#define TEXTURE false

void main(){    
    if(TEXTURE){
          gl_FragColor = texture2D(m_Texture, texCoord);
          return;
    }

    gl_FragColor = vec4(1.0);
}

This should not trigger unreachable code marking and the compiler should produce identical machine code.

@Nehon
Copy link
Author

Nehon commented Mar 12, 2016

I'm not sure how the compiler will treat that but if that end up in a dynamic branching it's not good.
Anyway, no big deal, I just thought I'd mention it. I understand the issue is not simple to tackle.
Also it's a pretty rare corner case, so it's not really a bother.

Thank you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants