27 #include <unordered_set> 28 #include <unordered_map> 30 #include "HLSL2GLSLConverter.h" 31 #include "ObjectBase.h" 32 #include "HLSLKeywords.h" 34 #include "HashUtils.h" 35 #include "HLSLKeywords.h" 39 struct FunctionStubHashKey
41 FunctionStubHashKey(
const String& _Obj,
const String& _Func, Uint32 _NumArgs) :
44 NumArguments(_NumArgs)
48 FunctionStubHashKey(
const Char* _Obj,
const Char* _Func, Uint32 _NumArgs) :
51 NumArguments(_NumArgs)
55 FunctionStubHashKey( FunctionStubHashKey && Key ) :
56 Object(
std::move(Key.Object)),
57 Function(
std::move(Key.Function)),
58 NumArguments(Key.NumArguments)
62 bool operator==(
const FunctionStubHashKey& rhs)
const 64 return Object == rhs.Object &&
65 Function == rhs.Function &&
66 NumArguments == rhs.NumArguments;
69 HashMapStringKey Object;
70 HashMapStringKey Function;
77 template<>
struct hash <
Diligent::FunctionStubHashKey >
79 size_t operator()(
const Diligent::FunctionStubHashKey &Key )
const 81 return ComputeHash(Key.Object, Key.Function, Key.NumArguments);
88 class HLSL2GLSLConverterImpl
91 static const HLSL2GLSLConverterImpl& GetInstance();
92 struct ConversionAttribs
94 IShaderSourceInputStreamFactory *pSourceStreamFactory =
nullptr;
95 IHLSL2GLSLConversionStream **ppConversionStream =
nullptr;
96 const Char* HLSLSource =
nullptr;
97 size_t NumSymbols = 0;
98 const Char* EntryPoint =
nullptr;
100 bool IncludeDefinitions =
false;
101 const Char* InputFileName =
nullptr;
104 String Convert(ConversionAttribs &Attribs)
const;
105 void CreateStream(
const Char* InputFileName,
106 IShaderSourceInputStreamFactory *pSourceStreamFactory,
107 const Char* HLSLSource,
109 IHLSL2GLSLConversionStream **ppStream)
const;
112 HLSL2GLSLConverterImpl();
114 struct HLSLObjectInfo
117 Uint32 NumComponents;
121 HLSLObjectInfo(
const String& Type, Uint32 NComp ) :
123 NumComponents( NComp )
126 struct ObjectsTypeHashType
130 ObjectsTypeHashType()noexcept {}
131 ObjectsTypeHashType(ObjectsTypeHashType&& rhs)noexcept :
134 ObjectsTypeHashType& operator = (ObjectsTypeHashType&&) =
delete;
135 ObjectsTypeHashType(ObjectsTypeHashType&) =
delete;
136 ObjectsTypeHashType& operator = (ObjectsTypeHashType&) =
delete;
138 std::unordered_map<HashMapStringKey, HLSLObjectInfo> m;
145 GLSLStubInfo(
const String& _Name,
const char* _Swizzle ) :
153 std::unordered_map<FunctionStubHashKey, GLSLStubInfo> m_GLSLStubs;
158 #define ADD_KEYWORD(keyword)kw_##keyword, 159 ITERATE_KEYWORDS(ADD_KEYWORD)
161 PreprocessorDirective,
190 bool IsBuiltInType()
const 192 static_assert( static_cast<int>(TokenType::kw_bool) == 1 && static_cast<int>(TokenType::kw_void) == 191,
193 "If you updated built-in types, double check that all types are defined between bool and void");
194 return Type >= TokenType::kw_bool && Type <= TokenType::kw_void;
196 bool IsFlowControl()
const 198 static_assert( static_cast<int>(TokenType::kw_break) == 192 && static_cast<int>(TokenType::kw_while) == 202,
199 "If you updated control flow keywords, double check that all keywords are defined between break and while");
200 return Type >= TokenType::kw_break && Type <= TokenType::kw_while;
202 TokenInfo( TokenType _Type = TokenType :: Undefined,
203 const Char* _Literal =
"",
204 const Char* _Delimiter =
"" ) :
207 Delimiter(_Delimiter)
210 typedef std::list<TokenInfo> TokenListType;
213 class ConversionStream :
public ObjectBase<IHLSL2GLSLConversionStream>
216 typedef ObjectBase<IHLSL2GLSLConversionStream> TBase;
217 ConversionStream(IReferenceCounters *pRefCounters,
218 const HLSL2GLSLConverterImpl &Converter,
219 const char* InputFileName,
220 IShaderSourceInputStreamFactory* pInputStreamFactory,
221 const Char* HLSLSource,
223 bool bPreserveTokens);
225 String Convert(
const Char* EntryPoint,
SHADER_TYPE ShaderType,
bool IncludeDefintions);
226 virtual void Convert(
const Char* EntryPoint,
SHADER_TYPE ShaderType,
bool IncludeDefintions, IDataBlob **ppGLSLSource)
override;
228 IMPLEMENT_QUERY_INTERFACE_IN_PLACE( IID_HLSL2GLSLConversionStream, TBase )
230 const String& GetInputFileName()
const{
return m_InputFileName; }
232 void InsertIncludes(String &GLSLSource, IShaderSourceInputStreamFactory* pSourceStreamFactory);
233 void Tokenize(
const String &Source);
235 typedef std::unordered_map<String, bool> SamplerHashType;
237 const HLSLObjectInfo *FindHLSLObject(
const String &Name );
239 void ProcessShaderDeclaration(TokenListType::iterator EntryPointToken,
SHADER_TYPE ShaderType);
241 void ProcessObjectMethods(
const TokenListType::iterator &ScopeStart,
const TokenListType::iterator &ScopeEnd);
243 void ProcessRWTextures(
const TokenListType::iterator &ScopeStart,
const TokenListType::iterator &ScopeEnd);
245 void ProcessAtomics(
const TokenListType::iterator &ScopeStart,
246 const TokenListType::iterator &ScopeEnd);
248 void RegisterStruct(TokenListType::iterator &Token);
250 void ProcessConstantBuffer(TokenListType::iterator &Token);
251 void ProcessStructuredBuffer(TokenListType::iterator &Token);
252 void ParseSamplers(TokenListType::iterator &ScopeStart, SamplerHashType &SamplersHash);
253 void ProcessTextureDeclaration(TokenListType::iterator &Token,
const std::vector<SamplerHashType> &SamplersHash, ObjectsTypeHashType &Objects);
254 bool ProcessObjectMethod(TokenListType::iterator &Token,
const TokenListType::iterator &ScopeStart,
const TokenListType::iterator &ScopeEnd);
255 Uint32 CountFunctionArguments(TokenListType::iterator &Token,
const TokenListType::iterator &ScopeEnd);
256 bool ProcessRWTextureStore(TokenListType::iterator &Token,
const TokenListType::iterator &ScopeEnd);
257 void RemoveFlowControlAttribute(TokenListType::iterator &Token);
258 void RemoveSemantics();
259 void RemoveSpecialShaderAttributes();
260 void RemoveSemanticsFromBlock(TokenListType::iterator &Token, TokenType OpenBracketType, TokenType ClosingBracketType);
266 template<
typename IteratorType>
267 String PrintTokenContext(IteratorType &TargetToken, Int32 NumAdjacentLines);
269 struct ShaderParameterInfo
271 enum class StorageQualifier : Int8
282 enum class PrimitiveType : Int8
291 enum class StreamType : Int8
298 PrimitiveType PrimType;
301 PrimType(PrimitiveType::Undefined),
302 Stream(StreamType::Undefined)
308 enum class InOutPatchType : Int8
315 PatchType(InOutPatchType::Undefined)
323 std::vector<ShaderParameterInfo> members;
325 ShaderParameterInfo() :
326 storageQualifier(StorageQualifier::Unknown)
329 void ParseShaderParameter(TokenListType::iterator &Token, ShaderParameterInfo &ParamInfo);
330 void ProcessFunctionParameters( TokenListType::iterator &Token, std::vector<ShaderParameterInfo>& Params,
bool &bIsVoid );
331 String AddFlatQualifier(
const String& Type);
332 void ProcessFragmentShaderArguments( std::vector<ShaderParameterInfo>& Params,
333 String &GlobalVariables,
334 std::stringstream &ReturnHandlerSS,
337 String BuildParameterName(
const std::vector<const ShaderParameterInfo*>& MemberStack, Char Separator,
const Char* Prefix =
"",
const Char *SubstituteInstName =
"",
const Char *Index =
"");
339 template<
typename THandler>
340 void ProcessScope(TokenListType::iterator &Token, TokenListType::iterator ScopeEnd, TokenType OpenParenType, TokenType ClosingParenType, THandler Handler);
342 template<
typename TArgHandler>
343 void ProcessShaderArgument(
const ShaderParameterInfo &Param,
346 std::stringstream &PrologueSS,
347 TArgHandler ArgHandler);
349 void ProcessVertexShaderArguments( std::vector<ShaderParameterInfo>& Params,
351 std::stringstream &ReturnHandlerSS,
354 void ProcessGeometryShaderArguments( TokenListType::iterator &TypeToken,
355 std::vector<ShaderParameterInfo>& Params,
359 void ProcessHullShaderConstantFunction(
const Char *FuncName,
bool &bTakesInputPatch );
361 void ProcessShaderAttributes( TokenListType::iterator &TypeToken,
362 std::unordered_map<HashMapStringKey, String>& Attributes);
364 void ProcessHullShaderArguments( TokenListType::iterator &TypeToken,
365 std::vector<ShaderParameterInfo>& Params,
367 std::stringstream &ReturnHandlerSS,
369 void ProcessDomainShaderArguments( TokenListType::iterator &TypeToken,
370 std::vector<ShaderParameterInfo>& Params,
372 std::stringstream &ReturnHandlerSS,
374 void ProcessComputeShaderArguments( TokenListType::iterator &TypeToken,
375 std::vector<ShaderParameterInfo>& Params,
379 void FindClosingBracket( TokenListType::iterator &Token,
const TokenListType::iterator &ScopeEnd, TokenType OpenBracketType, TokenType ClosingBracketType );
381 void ProcessReturnStatements( TokenListType::iterator &Token,
bool IsVoid,
const Char *EntryPoint,
const Char *MacroName );
383 void ProcessGSOutStreamOperations( TokenListType::iterator &Token,
const String &OutStreamName,
const char *EntryPoint );
385 String BuildGLSLSource();
388 TokenListType m_Tokens;
391 std::unordered_map<HashMapStringKey, TokenListType::iterator> m_StructDefinitions;
399 std::vector< ObjectsTypeHashType > m_Objects;
401 const bool m_bPreserveTokens;
402 const HLSL2GLSLConverterImpl &m_Converter;
406 const String m_InputFileName;
411 std::unordered_map<HashMapStringKey, TokenInfo> m_HLSLKeywords;
414 std::unordered_set<HashMapStringKey> m_ImageTypes;
417 std::unordered_set<HashMapStringKey> m_AtomicOperations;
422 static constexpr
int InVar = 0;
423 static constexpr
int OutVar = 1;
424 std::unordered_map<HashMapStringKey, String> m_HLSLSemanticToGLSLVar[6][2];
SHADER_TYPE
Describes the shader type.
Definition: Shader.h:46
Namespace for the OpenGL implementation of the graphics engine.
Definition: BufferD3D11Impl.h:34
Definition: AdvancedMath.h:316
Unknown shader type.
Definition: Shader.h:48