LukasBanana / XShaderCompiler

Shader cross compiler to translate HLSL (Shader Model 4 and 5) to GLSL
BSD 3-Clause "New" or "Revised" License
352 stars 48 forks source link

Interpolation modifier being recognized as keyword #98

Open Xottab-DUTY opened 3 years ago

Xottab-DUTY commented 3 years ago

Cannot use certain words when naming a function or variable.

XSC fails, allowed in FXC.

Example shader:

//////////////////////////////////////////////////////////////////////////////////////////
// Pixel

float sample(float2 tc)
{
    return 0.f;
}

float4 main() : SV_Target
{
    float sample = 0; // if we comment out the 'sample' function, XSC will fail on this variable..
    return  0;
}

Error log:

syntax error (test.ps:4:7) : unexpected token: interpolation modifier (expected identifier)
float sample(float2 tc)
      ^~~~~~
LukasBanana commented 3 years ago

I'm not sure anymore how XSC treats the sample keyword but since it's already used as an interpolation modifier, I suggest to avoid using this name for both functions and variables. HLSL has a weird case where sample float sample : Sample is a valid declaration but sample float sample : sample confuses DXC while FXC accepts it. This is one of many examples in which HLSL lacks a clear definition.

Xottab-DUTY commented 3 years ago

i suggest to avoid using this name for both functions and variables.

Unfortunately, it is not possible, we need to compile unmodified shaders. 🙁 It seems we will need to patch some existing cross-compiler or to write our own. 🙃

HLSL has a weird case where sample float sample : Sample is a valid declaration but sample float sample : sample confuses DXC while FXC accepts it. This is one of many examples in which HLSL lacks a clear definition.

Interesting case... Thank you 😃

LukasBanana commented 3 years ago

Unfortunately, it is not possible, we need to compile unmodified shaders.

In that case, I suggest to add another token type like InterpModifierOrIdent alongside InterpModifier just for the keyword sample and handle it at places like this one.

I see you already considered ShaderConductor/DXC for your purpose. I would recommend that compiler toolchain as its more advanced and has a strong support from Microsoft and Google. Only downside is that it's a much larger code base and not a small single library project, i.e. it takes some effort to integrate it into a graphics pipeline.