Skip to content

Commit 1fbce38

Browse files
HLSL2GLSL Converter: handle preprocessor-defined shader function input types
1 parent 4ae4313 commit 1fbce38

3 files changed

Lines changed: 48 additions & 33 deletions

File tree

Graphics/HLSL2GLSLConverterLib/include/HLSL2GLSLConverterImpl.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -442,6 +442,8 @@ class HLSL2GLSLConverterImpl
442442
void RemoveSemanticsFromBlock(TokenListType::iterator& Token, TokenType OpenBracketType, TokenType ClosingBracketType);
443443
void RemoveSamplerRegister(TokenListType::iterator& Token);
444444

445+
TokenListType::iterator FindMacroDefinition(const std::string& MacroName);
446+
445447
// IteratorType may be String::iterator or String::const_iterator.
446448
// While iterator is convertible to const_iterator,
447449
// iterator& cannot be converted to const_iterator& (Microsoft compiler allows

Graphics/HLSL2GLSLConverterLib/src/HLSL2GLSLConverterImpl.cpp

Lines changed: 44 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -903,6 +903,34 @@ void HLSL2GLSLConverterImpl::ConversionStream::ParseGlobalPreprocessorDefines()
903903
}
904904
}
905905

906+
HLSL2GLSLConverterImpl::TokenListType::iterator HLSL2GLSLConverterImpl::ConversionStream::FindMacroDefinition(const std::string& MacroName)
907+
{
908+
auto define_it = m_PreprocessorDefinitions.find(MacroName.c_str());
909+
if (define_it == m_PreprocessorDefinitions.end())
910+
return m_Tokens.end();
911+
912+
auto Token = define_it->second;
913+
// #define PS_OUTPUT PSOutput
914+
// ^
915+
916+
++Token;
917+
// #define PS_OUTPUT PSOutput
918+
// ^
919+
if (Token == m_Tokens.end() || Token->Literal != MacroName)
920+
return m_Tokens.end();
921+
922+
++Token;
923+
// #define PS_OUTPUT PSOutput
924+
// ^
925+
926+
// Check that the definition is on the same line (we don't handle multiline definitions)
927+
if (Token == m_Tokens.end() || Token->Delimiter.find_first_of("\r\n") != std::string::npos)
928+
return m_Tokens.end();
929+
930+
return (Token->IsBuiltInType() || Token->Type == TokenType::Identifier) ? Token : m_Tokens.end();
931+
}
932+
933+
906934
// The function replaces cbuffer with uniform and adds semicolon if it is missing after the closing brace:
907935
// cbuffer
908936
// {
@@ -2403,6 +2431,14 @@ void HLSL2GLSLConverterImpl::ConversionStream::ParseShaderParameter(TokenListTyp
24032431

24042432
if (!TypeToken->IsBuiltInType())
24052433
{
2434+
{
2435+
auto DefinedTypeToken = FindMacroDefinition(TypeToken->Literal);
2436+
// Check that the define directive is before the type token
2437+
if (DefinedTypeToken != m_Tokens.end() && DefinedTypeToken->Idx < TypeToken->Idx)
2438+
{
2439+
TypeToken = DefinedTypeToken;
2440+
}
2441+
}
24062442
const auto& StructName = TypeToken->Literal;
24072443
auto it = m_StructDefinitions.find(StructName.c_str());
24082444
if (it == m_StructDefinitions.end())
@@ -2459,38 +2495,15 @@ void HLSL2GLSLConverterImpl::ConversionStream::ProcessFunctionParameters(TokenLi
24592495

24602496
auto ActualTypeToken = TypeToken;
24612497
{
2462-
// PS_OUTPUT TestPS ( in VSOutput In,
2463-
2464-
auto define_it = m_PreprocessorDefinitions.find(ActualTypeToken->Literal.c_str());
2465-
if (define_it != m_PreprocessorDefinitions.end())
2498+
auto DefinedTypeToken = FindMacroDefinition(TypeToken->Literal);
2499+
// Check that the define directive is before the type token
2500+
if (DefinedTypeToken != m_Tokens.end() && DefinedTypeToken->Idx < TypeToken->Idx)
24662501
{
2467-
auto DefinedTypeToken = define_it->second;
2468-
// #define PS_OUTPUT PSOutput
2502+
// PS_OUTPUT TestPS ( in VSOutput In,
24692503
// ^
2470-
// DefinedTypeToken
2471-
2472-
// Check that the define directive is before the type token
2473-
if (DefinedTypeToken->Idx < TypeToken->Idx)
2474-
{
2475-
++DefinedTypeToken;
2476-
// #define PS_OUTPUT PSOutput
2477-
// ^
2478-
// DefinedTypeToken
2479-
if (DefinedTypeToken != m_Tokens.end() && DefinedTypeToken->Literal == TypeToken->Literal)
2480-
{
2481-
++DefinedTypeToken;
2482-
// #define PS_OUTPUT PSOutput
2483-
// ^
2484-
// DefinedTypeToken
2485-
if (DefinedTypeToken != m_Tokens.end() &&
2486-
// Check that the definition is on the same line (we don't handle backslash at the moment)
2487-
DefinedTypeToken->Delimiter.find_first_of("\r\n") == std::string::npos &&
2488-
(DefinedTypeToken->IsBuiltInType() || DefinedTypeToken->Type == TokenType::Identifier))
2489-
{
2490-
ActualTypeToken = DefinedTypeToken;
2491-
}
2492-
}
2493-
}
2504+
ActualTypeToken = DefinedTypeToken;
2505+
// #define PS_OUTPUT PSOutput
2506+
// ^
24942507
}
24952508
}
24962509

@@ -2755,7 +2768,7 @@ void HLSL2GLSLConverterImpl::ConversionStream::ProcessFunctionParameters(TokenLi
27552768
else
27562769
{
27572770
// VSOut TestVS ()
2758-
auto TmpTypeToken = ActualTypeToken;
2771+
auto TmpTypeToken = TypeToken;
27592772
ParseShaderParameter(TmpTypeToken, RetParam);
27602773
}
27612774
TypeToken->Type = TokenType::Identifier;

Tests/DiligentCoreAPITest/assets/shaders/HLSL2GLSLConverter/PreprocessorTest.hlsl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ struct PSInput
1717
# define PS_OUTPUT xyz
1818
#endif
1919

20-
PS_OUTPUT main1(in PSInput PSIn)
20+
PS_OUTPUT main1(in PS_INPUT PSIn)
2121
{
2222
PS_OUTPUT PSOut;
2323
PSOut.Color = float4(PSIn.Pos.xy, 0.0, 0.0);
@@ -27,7 +27,7 @@ PS_OUTPUT main1(in PSInput PSIn)
2727
#define FLOAT4 float4
2828
#define FLOAT3 float3
2929

30-
FLOAT4 main2(in PSInput PSIn) : SV_Target
30+
FLOAT4 main2(in PS_INPUT PSIn) : SV_Target
3131
{
3232
return FLOAT4(PSIn.Pos.xy, 0.0, 0.0);
3333
}

0 commit comments

Comments
 (0)