XShaderCompiler icon indicating copy to clipboard operation
XShaderCompiler copied to clipboard

Interpolation modifier being recognized as keyword

Open Xottab-DUTY opened this issue 4 years ago • 3 comments

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)
      ^~~~~~

Xottab-DUTY avatar Dec 11 '20 17:12 Xottab-DUTY

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.

LukasBanana avatar Dec 11 '20 17:12 LukasBanana

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 😃

Xottab-DUTY avatar Dec 11 '20 20:12 Xottab-DUTY

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.

LukasBanana avatar Dec 11 '20 22:12 LukasBanana