From 86f7138706e6909353ecb2f4bbe692ee3b2164e5 Mon Sep 17 00:00:00 2001 From: John Kessenich Date: Mon, 19 Sep 2016 20:23:18 -0600 Subject: [PATCH] HLSL: Add string basic type and recognize string declaration grammar. This includes the "< decl ; decl ; >" syntax which has its own namespace. This functionality is not implemented, just silently accepted. --- Test/baseResults/hlsl.string.frag.out | 64 +++++++++++++++++++++++++++ Test/hlsl.string.frag | 12 +++++ glslang/Include/BaseTypes.h | 4 ++ glslang/Include/revision.h | 2 +- gtests/Hlsl.FromFile.cpp | 1 + hlsl/hlslGrammar.cpp | 55 +++++++++++++++++++++++ hlsl/hlslGrammar.h | 1 + hlsl/hlslParseHelper.cpp | 5 +++ hlsl/hlslScanContext.cpp | 2 + hlsl/hlslTokens.h | 1 + 10 files changed, 146 insertions(+), 1 deletion(-) create mode 100755 Test/baseResults/hlsl.string.frag.out create mode 100755 Test/hlsl.string.frag diff --git a/Test/baseResults/hlsl.string.frag.out b/Test/baseResults/hlsl.string.frag.out new file mode 100755 index 00000000..6016f52e --- /dev/null +++ b/Test/baseResults/hlsl.string.frag.out @@ -0,0 +1,64 @@ +hlsl.string.frag +Shader version: 450 +gl_FragCoord origin is upper left +0:? Sequence +0:10 Function Definition: main(f1; (global float) +0:10 Function Parameters: +0:10 'f' (layout(location=0 ) in float) +0:? Sequence +0:11 Sequence +0:11 move second child to first child (temp float) +0:? '@entryPointOutput' (layout(location=0 ) out float) +0:11 'f' (layout(location=0 ) in float) +0:11 Branch: Return +0:? Linker Objects +0:? '@entryPointOutput' (layout(location=0 ) out float) +0:? 'f' (layout(location=0 ) in float) + + +Linked fragment stage: + + +Shader version: 450 +gl_FragCoord origin is upper left +0:? Sequence +0:10 Function Definition: main(f1; (global float) +0:10 Function Parameters: +0:10 'f' (layout(location=0 ) in float) +0:? Sequence +0:11 Sequence +0:11 move second child to first child (temp float) +0:? '@entryPointOutput' (layout(location=0 ) out float) +0:11 'f' (layout(location=0 ) in float) +0:11 Branch: Return +0:? Linker Objects +0:? '@entryPointOutput' (layout(location=0 ) out float) +0:? 'f' (layout(location=0 ) in float) + +// Module Version 10000 +// Generated by (magic number): 80001 +// Id's are bound by 13 + + Capability Shader + 1: ExtInstImport "GLSL.std.450" + MemoryModel Logical GLSL450 + EntryPoint Fragment 4 "main" 8 10 + ExecutionMode 4 OriginUpperLeft + Name 4 "main" + Name 8 "@entryPointOutput" + Name 10 "f" + Decorate 8(@entryPointOutput) Location 0 + Decorate 10(f) Location 0 + 2: TypeVoid + 3: TypeFunction 2 + 6: TypeFloat 32 + 7: TypePointer Output 6(float) +8(@entryPointOutput): 7(ptr) Variable Output + 9: TypePointer Input 6(float) + 10(f): 9(ptr) Variable Input + 4(main): 2 Function None 3 + 5: Label + 11: 6(float) Load 10(f) + Store 8(@entryPointOutput) 11 + Return + FunctionEnd diff --git a/Test/hlsl.string.frag b/Test/hlsl.string.frag new file mode 100755 index 00000000..09885ff9 --- /dev/null +++ b/Test/hlsl.string.frag @@ -0,0 +1,12 @@ +string s = "string1"; +string e = ""; +string bracket < string a = "nested" ; > ; +string brackets < string b = "nest1" ; string c = "nest2" ; string d = "nest3" ; > ; +string brackete1 < > ; +string brackete2 < ; > ; +string brackete3 < ; ; > ; + +float main(float f) +{ + return f; +} \ No newline at end of file diff --git a/glslang/Include/BaseTypes.h b/glslang/Include/BaseTypes.h index bc8f1103..64ef80c9 100644 --- a/glslang/Include/BaseTypes.h +++ b/glslang/Include/BaseTypes.h @@ -55,6 +55,10 @@ enum TBasicType { EbtSampler, EbtStruct, EbtBlock, + + // HLSL types that live only temporarily. + EbtString, + EbtNumTypes }; diff --git a/glslang/Include/revision.h b/glslang/Include/revision.h index a496025a..bf8f2285 100644 --- a/glslang/Include/revision.h +++ b/glslang/Include/revision.h @@ -2,5 +2,5 @@ // For the version, it uses the latest git tag followed by the number of commits. // For the date, it uses the current date (when then script is run). -#define GLSLANG_REVISION "Overload400-PrecQual.1499" +#define GLSLANG_REVISION "Overload400-PrecQual.1501" #define GLSLANG_DATE "19-Sep-2016" diff --git a/gtests/Hlsl.FromFile.cpp b/gtests/Hlsl.FromFile.cpp index 06660c73..01cc0323 100644 --- a/gtests/Hlsl.FromFile.cpp +++ b/gtests/Hlsl.FromFile.cpp @@ -160,6 +160,7 @@ INSTANTIATE_TEST_CASE_P( {"hlsl.semicolons.frag", "main"}, {"hlsl.shapeConv.frag", "main"}, {"hlsl.stringtoken.frag", "main"}, + {"hlsl.string.frag", "main"}, {"hlsl.structin.vert", "main"}, {"hlsl.intrinsics.vert", "VertexShaderFunction"}, {"hlsl.matType.frag", "PixelShaderFunction"}, diff --git a/hlsl/hlslGrammar.cpp b/hlsl/hlslGrammar.cpp index cc7e9010..07625145 100755 --- a/hlsl/hlslGrammar.cpp +++ b/hlsl/hlslGrammar.cpp @@ -721,6 +721,54 @@ bool HlslGrammar::acceptMatrixTemplateType(TType& type) return true; } +// string_template_type +// : STRING +// | STRING identifier LEFT_ANGLE declaration SEMI_COLON ... declaration SEMICOLON RIGHT_ANGLE +// +bool HlslGrammar::acceptStringTemplateType(TType& type) +{ + // STRING + if (! acceptTokenClass(EHTokString)) + return false; + + // no matter what happens next, we recognized a string type + new(&type) TType(EbtString); + + // identifier LEFT_ANGLE, or not? + if (! acceptTokenClass(EHTokIdentifier)) { + expected("identifier following 'string'"); + return false; + } + + if (! peekTokenClass(EHTokLeftAngle)) { + // then it must be the non-template version, back up and let + // normal declaration code handle it + + // recede the identifier + recedeToken(); + return true; + } + + // move past the LEFT_ANGLE + advanceToken(); + + // declaration SEMI_COLON ... declaration SEMICOLON RIGHT_ANGLE + do { + // eat any extra SEMI_COLON; don't know if the grammar calls for this or not + while (acceptTokenClass(EHTokSemicolon)) + ; + + if (acceptTokenClass(EHTokRightAngle)) + return true; + + // declaration + TIntermNode* node; + if (! acceptDeclaration(node)) { + expected("declaration in string list"); + return false; + } + } while (true); +} // sampler_type // : SAMPLER @@ -894,6 +942,10 @@ bool HlslGrammar::acceptType(TType& type) return acceptMatrixTemplateType(type); break; + case EHTokString: + return acceptStringTemplateType(type); + break; + case EHTokSampler: // fall through case EHTokSampler1d: // ... case EHTokSampler2d: // ... @@ -2062,6 +2114,9 @@ bool HlslGrammar::acceptLiteral(TIntermTyped*& node) case EHTokBoolConstant: node = intermediate.addConstantUnion(token.b, token.loc, true); break; + case EHTokStringConstant: + node = nullptr; + break; default: return false; diff --git a/hlsl/hlslGrammar.h b/hlsl/hlslGrammar.h index 4e93f09d..f5c7d4d2 100755 --- a/hlsl/hlslGrammar.h +++ b/hlsl/hlslGrammar.h @@ -73,6 +73,7 @@ namespace glslang { bool acceptTemplateType(TBasicType&); bool acceptVectorTemplateType(TType&); bool acceptMatrixTemplateType(TType&); + bool acceptStringTemplateType(TType&); bool acceptSamplerType(TType&); bool acceptTextureType(TType&); bool acceptStruct(TType&); diff --git a/hlsl/hlslParseHelper.cpp b/hlsl/hlslParseHelper.cpp index 4e43bff0..e506e297 100755 --- a/hlsl/hlslParseHelper.cpp +++ b/hlsl/hlslParseHelper.cpp @@ -4052,6 +4052,11 @@ void HlslParseContext::declareTypedef(const TSourceLoc& loc, TString& identifier // TIntermNode* HlslParseContext::declareVariable(const TSourceLoc& loc, TString& identifier, const TType& parseType, TArraySizes* arraySizes, TIntermTyped* initializer) { + // string identifiers can nest inside < ... >, apparently with their own namespace, + // which is not implemented + if (parseType.getBasicType() == EbtString) + return nullptr; + TType type; type.shallowCopy(parseType); if (type.isImplicitlySizedArray()) { diff --git a/hlsl/hlslScanContext.cpp b/hlsl/hlslScanContext.cpp index 29a22553..edeee64c 100755 --- a/hlsl/hlslScanContext.cpp +++ b/hlsl/hlslScanContext.cpp @@ -122,6 +122,7 @@ void HlslScanContext::fillInKeywordMap() (*KeywordMap)["Buffer"] = EHTokBuffer; (*KeywordMap)["vector"] = EHTokVector; (*KeywordMap)["matrix"] = EHTokMatrix; + (*KeywordMap)["string"] = EHTokString; (*KeywordMap)["void"] = EHTokVoid; (*KeywordMap)["bool"] = EHTokBool; @@ -471,6 +472,7 @@ EHlslTokenClass HlslScanContext::tokenizeIdentifier() case EHTokBuffer: case EHTokVector: case EHTokMatrix: + case EHTokString: return keyword; // scalar types diff --git a/hlsl/hlslTokens.h b/hlsl/hlslTokens.h index db3b0972..9e23cc7a 100755 --- a/hlsl/hlslTokens.h +++ b/hlsl/hlslTokens.h @@ -70,6 +70,7 @@ enum EHlslTokenClass { EHTokBuffer, EHTokVector, EHTokMatrix, + EHTokString, // scalar types EHTokVoid,