From 82d6baf86fc89f21370f145dbd2332cea69330fb Mon Sep 17 00:00:00 2001 From: John Kessenich Date: Fri, 29 Jul 2016 13:03:05 -0600 Subject: [PATCH] HLSL: Implement packoffset production. --- Test/baseResults/hlsl.buffer.frag.out | 128 ++++++++++++++------------ Test/baseResults/hlsl.float4.frag.out | 15 +-- Test/baseResults/hlsl.struct.frag.out | 12 +-- Test/hlsl.buffer.frag | 8 +- glslang/Include/revision.h | 2 +- hlsl/hlslGrammar.cpp | 18 +++- hlsl/hlslParseHelper.cpp | 40 ++++++++ hlsl/hlslParseHelper.h | 1 + 8 files changed, 147 insertions(+), 77 deletions(-) diff --git a/Test/baseResults/hlsl.buffer.frag.out b/Test/baseResults/hlsl.buffer.frag.out index d03629cc..a51be517 100755 --- a/Test/baseResults/hlsl.buffer.frag.out +++ b/Test/baseResults/hlsl.buffer.frag.out @@ -2,37 +2,37 @@ hlsl.buffer.frag Shader version: 450 gl_FragCoord origin is upper left 0:? Sequence -0:23 Function Definition: PixelShaderFunction(vf4; (global 4-component vector of float) -0:20 Function Parameters: -0:20 'input' (in 4-component vector of float) +0:29 Function Definition: PixelShaderFunction(vf4; (global 4-component vector of float) +0:26 Function Parameters: +0:26 'input' (in 4-component vector of float) 0:? Sequence -0:21 Branch: Return with expression -0:21 add (temp 4-component vector of float) -0:21 add (temp 4-component vector of float) -0:21 add (temp 4-component vector of float) -0:21 add (temp 4-component vector of float) -0:21 'input' (in 4-component vector of float) -0:21 v1: direct index for structure (layout(column_major std140 ) uniform 4-component vector of float) -0:21 'anon@0' (layout(column_major std140 ) uniform block{layout(column_major std140 ) uniform 4-component vector of float v1}) -0:21 Constant: -0:21 0 (const uint) -0:21 v2: direct index for structure (layout(column_major std430 ) buffer 4-component vector of float) -0:21 'anon@1' (layout(column_major std430 ) buffer block{layout(column_major std430 ) buffer 4-component vector of float v2}) -0:21 Constant: -0:21 0 (const uint) -0:21 v3: direct index for structure (layout(column_major std140 ) uniform 4-component vector of float) -0:21 'anon@2' (layout(column_major std140 ) uniform block{layout(column_major std140 ) uniform 4-component vector of float v3, layout(column_major std140 ) uniform int i3}) -0:21 Constant: -0:21 0 (const uint) -0:21 v4: direct index for structure (layout(column_major std430 ) buffer 4-component vector of float) -0:21 'anon@3' (layout(column_major std430 ) buffer block{layout(column_major std430 ) buffer 4-component vector of float v4, layout(column_major std430 ) buffer int i4}) -0:21 Constant: -0:21 0 (const uint) +0:27 Branch: Return with expression +0:27 add (temp 4-component vector of float) +0:27 add (temp 4-component vector of float) +0:27 add (temp 4-component vector of float) +0:27 add (temp 4-component vector of float) +0:27 'input' (in 4-component vector of float) +0:27 v1: direct index for structure (layout(column_major std140 ) uniform 4-component vector of float) +0:27 'anon@0' (layout(column_major std140 ) uniform block{layout(column_major std140 ) uniform 4-component vector of float v1}) +0:27 Constant: +0:27 0 (const uint) +0:27 v2: direct index for structure (layout(column_major std430 ) buffer 4-component vector of float) +0:27 'anon@1' (layout(column_major std430 ) buffer block{layout(column_major std430 ) buffer 4-component vector of float v2}) +0:27 Constant: +0:27 0 (const uint) +0:27 v3: direct index for structure (layout(column_major std140 ) uniform 4-component vector of float) +0:27 'anon@2' (layout(column_major std140 ) uniform block{layout(column_major std140 ) uniform 4-component vector of float v3, layout(column_major std140 offset=20 ) uniform int i3}) +0:27 Constant: +0:27 0 (const uint) +0:27 v4: direct index for structure (layout(column_major std430 offset=16 ) buffer 4-component vector of float) +0:27 'anon@3' (layout(column_major std430 ) buffer block{layout(column_major std430 offset=16 ) buffer 4-component vector of float v4, layout(column_major std430 offset=48 ) buffer int i4, layout(column_major std430 offset=60 ) buffer float f1, layout(column_major std430 offset=64 ) buffer float f3, layout(column_major std430 offset=68 ) buffer float f4, layout(column_major std430 offset=72 ) buffer float f5, layout(column_major std430 ) buffer float f6, layout(column_major std430 ) buffer float f7}) +0:27 Constant: +0:27 0 (const uint) 0:? Linker Objects 0:? 'anon@0' (layout(column_major std140 ) uniform block{layout(column_major std140 ) uniform 4-component vector of float v1}) 0:? 'anon@1' (layout(column_major std430 ) buffer block{layout(column_major std430 ) buffer 4-component vector of float v2}) -0:? 'anon@2' (layout(column_major std140 ) uniform block{layout(column_major std140 ) uniform 4-component vector of float v3, layout(column_major std140 ) uniform int i3}) -0:? 'anon@3' (layout(column_major std430 ) buffer block{layout(column_major std430 ) buffer 4-component vector of float v4, layout(column_major std430 ) buffer int i4}) +0:? 'anon@2' (layout(column_major std140 ) uniform block{layout(column_major std140 ) uniform 4-component vector of float v3, layout(column_major std140 offset=20 ) uniform int i3}) +0:? 'anon@3' (layout(column_major std430 ) buffer block{layout(column_major std430 offset=16 ) buffer 4-component vector of float v4, layout(column_major std430 offset=48 ) buffer int i4, layout(column_major std430 offset=60 ) buffer float f1, layout(column_major std430 offset=64 ) buffer float f3, layout(column_major std430 offset=68 ) buffer float f4, layout(column_major std430 offset=72 ) buffer float f5, layout(column_major std430 ) buffer float f6, layout(column_major std430 ) buffer float f7}) Linked fragment stage: @@ -41,37 +41,37 @@ Linked fragment stage: Shader version: 450 gl_FragCoord origin is upper left 0:? Sequence -0:23 Function Definition: PixelShaderFunction(vf4; (global 4-component vector of float) -0:20 Function Parameters: -0:20 'input' (in 4-component vector of float) +0:29 Function Definition: PixelShaderFunction(vf4; (global 4-component vector of float) +0:26 Function Parameters: +0:26 'input' (in 4-component vector of float) 0:? Sequence -0:21 Branch: Return with expression -0:21 add (temp 4-component vector of float) -0:21 add (temp 4-component vector of float) -0:21 add (temp 4-component vector of float) -0:21 add (temp 4-component vector of float) -0:21 'input' (in 4-component vector of float) -0:21 v1: direct index for structure (layout(column_major std140 ) uniform 4-component vector of float) -0:21 'anon@0' (layout(column_major std140 ) uniform block{layout(column_major std140 ) uniform 4-component vector of float v1}) -0:21 Constant: -0:21 0 (const uint) -0:21 v2: direct index for structure (layout(column_major std430 ) buffer 4-component vector of float) -0:21 'anon@1' (layout(column_major std430 ) buffer block{layout(column_major std430 ) buffer 4-component vector of float v2}) -0:21 Constant: -0:21 0 (const uint) -0:21 v3: direct index for structure (layout(column_major std140 ) uniform 4-component vector of float) -0:21 'anon@2' (layout(column_major std140 ) uniform block{layout(column_major std140 ) uniform 4-component vector of float v3, layout(column_major std140 ) uniform int i3}) -0:21 Constant: -0:21 0 (const uint) -0:21 v4: direct index for structure (layout(column_major std430 ) buffer 4-component vector of float) -0:21 'anon@3' (layout(column_major std430 ) buffer block{layout(column_major std430 ) buffer 4-component vector of float v4, layout(column_major std430 ) buffer int i4}) -0:21 Constant: -0:21 0 (const uint) +0:27 Branch: Return with expression +0:27 add (temp 4-component vector of float) +0:27 add (temp 4-component vector of float) +0:27 add (temp 4-component vector of float) +0:27 add (temp 4-component vector of float) +0:27 'input' (in 4-component vector of float) +0:27 v1: direct index for structure (layout(column_major std140 ) uniform 4-component vector of float) +0:27 'anon@0' (layout(column_major std140 ) uniform block{layout(column_major std140 ) uniform 4-component vector of float v1}) +0:27 Constant: +0:27 0 (const uint) +0:27 v2: direct index for structure (layout(column_major std430 ) buffer 4-component vector of float) +0:27 'anon@1' (layout(column_major std430 ) buffer block{layout(column_major std430 ) buffer 4-component vector of float v2}) +0:27 Constant: +0:27 0 (const uint) +0:27 v3: direct index for structure (layout(column_major std140 ) uniform 4-component vector of float) +0:27 'anon@2' (layout(column_major std140 ) uniform block{layout(column_major std140 ) uniform 4-component vector of float v3, layout(column_major std140 offset=20 ) uniform int i3}) +0:27 Constant: +0:27 0 (const uint) +0:27 v4: direct index for structure (layout(column_major std430 offset=16 ) buffer 4-component vector of float) +0:27 'anon@3' (layout(column_major std430 ) buffer block{layout(column_major std430 offset=16 ) buffer 4-component vector of float v4, layout(column_major std430 offset=48 ) buffer int i4, layout(column_major std430 offset=60 ) buffer float f1, layout(column_major std430 offset=64 ) buffer float f3, layout(column_major std430 offset=68 ) buffer float f4, layout(column_major std430 offset=72 ) buffer float f5, layout(column_major std430 ) buffer float f6, layout(column_major std430 ) buffer float f7}) +0:27 Constant: +0:27 0 (const uint) 0:? Linker Objects 0:? 'anon@0' (layout(column_major std140 ) uniform block{layout(column_major std140 ) uniform 4-component vector of float v1}) 0:? 'anon@1' (layout(column_major std430 ) buffer block{layout(column_major std430 ) buffer 4-component vector of float v2}) -0:? 'anon@2' (layout(column_major std140 ) uniform block{layout(column_major std140 ) uniform 4-component vector of float v3, layout(column_major std140 ) uniform int i3}) -0:? 'anon@3' (layout(column_major std430 ) buffer block{layout(column_major std430 ) buffer 4-component vector of float v4, layout(column_major std430 ) buffer int i4}) +0:? 'anon@2' (layout(column_major std140 ) uniform block{layout(column_major std140 ) uniform 4-component vector of float v3, layout(column_major std140 offset=20 ) uniform int i3}) +0:? 'anon@3' (layout(column_major std430 ) buffer block{layout(column_major std430 offset=16 ) buffer 4-component vector of float v4, layout(column_major std430 offset=48 ) buffer int i4, layout(column_major std430 offset=60 ) buffer float f1, layout(column_major std430 offset=64 ) buffer float f3, layout(column_major std430 offset=68 ) buffer float f4, layout(column_major std430 offset=72 ) buffer float f5, layout(column_major std430 ) buffer float f6, layout(column_major std430 ) buffer float f7}) // Module Version 10000 // Generated by (magic number): 80001 @@ -98,6 +98,12 @@ gl_FragCoord origin is upper left Name 32 "" MemberName 32 0 "v4" MemberName 32 1 "i4" + MemberName 32 2 "f1" + MemberName 32 3 "f3" + MemberName 32 4 "f4" + MemberName 32 5 "f5" + MemberName 32 6 "f6" + MemberName 32 7 "f7" Name 34 "" MemberDecorate 11 0 Offset 0 Decorate 11 Block @@ -106,11 +112,17 @@ gl_FragCoord origin is upper left Decorate 20 BufferBlock Decorate 22 DescriptorSet 0 MemberDecorate 26 0 Offset 0 - MemberDecorate 26 1 Offset 16 + MemberDecorate 26 1 Offset 20 Decorate 26 Block Decorate 28 DescriptorSet 0 - MemberDecorate 32 0 Offset 0 - MemberDecorate 32 1 Offset 16 + MemberDecorate 32 0 Offset 16 + MemberDecorate 32 1 Offset 48 + MemberDecorate 32 2 Offset 60 + MemberDecorate 32 3 Offset 64 + MemberDecorate 32 4 Offset 68 + MemberDecorate 32 5 Offset 72 + MemberDecorate 32 6 Offset 76 + MemberDecorate 32 7 Offset 80 Decorate 32 BufferBlock Decorate 34 DescriptorSet 0 2: TypeVoid @@ -131,7 +143,7 @@ gl_FragCoord origin is upper left 26: TypeStruct 7(fvec4) 14(int) 27: TypePointer Uniform 26(struct) 28: 27(ptr) Variable Uniform - 32: TypeStruct 7(fvec4) 14(int) + 32: TypeStruct 7(fvec4) 14(int) 6(float) 6(float) 6(float) 6(float) 6(float) 6(float) 33: TypePointer Uniform 32(struct) 34: 33(ptr) Variable Uniform 4(PixelShaderFunction): 2 Function None 3 diff --git a/Test/baseResults/hlsl.float4.frag.out b/Test/baseResults/hlsl.float4.frag.out index a086d95e..481c9018 100755 --- a/Test/baseResults/hlsl.float4.frag.out +++ b/Test/baseResults/hlsl.float4.frag.out @@ -21,9 +21,9 @@ gl_FragCoord origin is upper left 0:? Linker Objects 0:? 'AmbientColor' (global 4-component vector of float) 0:? 'ff1' (global bool Face) -0:? 'ff2' (global 4-component vector of float) -0:? 'ff3' (global 4-component vector of float) -0:? 'ff4' (global 4-component vector of float FragCoord) +0:? 'ff2' (layout(offset=4 ) global 4-component vector of float) +0:? 'ff3' (layout(offset=4 ) global 4-component vector of float) +0:? 'ff4' (layout(offset=4 ) global 4-component vector of float FragCoord) Linked fragment stage: @@ -51,9 +51,9 @@ gl_FragCoord origin is upper left 0:? Linker Objects 0:? 'AmbientColor' (global 4-component vector of float) 0:? 'ff1' (global bool Face) -0:? 'ff2' (global 4-component vector of float) -0:? 'ff3' (global 4-component vector of float) -0:? 'ff4' (global 4-component vector of float FragCoord) +0:? 'ff2' (layout(offset=4 ) global 4-component vector of float) +0:? 'ff3' (layout(offset=4 ) global 4-component vector of float) +0:? 'ff4' (layout(offset=4 ) global 4-component vector of float FragCoord) // Module Version 10000 // Generated by (magic number): 80001 @@ -74,6 +74,9 @@ gl_FragCoord origin is upper left Name 28 "ff3" Name 29 "ff4" Decorate 26(ff1) BuiltIn FrontFacing + Decorate 27(ff2) Offset 4 + Decorate 28(ff3) Offset 4 + Decorate 29(ff4) Offset 4 Decorate 29(ff4) BuiltIn FragCoord 2: TypeVoid 3: TypeFunction 2 diff --git a/Test/baseResults/hlsl.struct.frag.out b/Test/baseResults/hlsl.struct.frag.out index a70bba60..684d4c88 100755 --- a/Test/baseResults/hlsl.struct.frag.out +++ b/Test/baseResults/hlsl.struct.frag.out @@ -14,8 +14,8 @@ gl_FragCoord origin is upper left 0:37 's2' (global structure{temp 4-component vector of float i}) 0:37 Constant: 0:37 0 (const int) -0:37 ff4: direct index for structure (temp 4-component vector of float FragCoord) -0:37 's4' (global structure{smooth in 4-component vector of float a, flat temp bool b, centroid noperspective temp 1-component vector of float c, centroid sample temp 2-component vector of float d, temp bool Face ff1, temp bool ff2, temp bool ff3, temp 4-component vector of float FragCoord ff4}) +0:37 ff4: direct index for structure (layout(offset=4 ) temp 4-component vector of float FragCoord) +0:37 's4' (global structure{smooth in 4-component vector of float a, flat temp bool b, centroid noperspective temp 1-component vector of float c, centroid sample temp 2-component vector of float d, temp bool Face ff1, layout(offset=4 ) temp bool ff2, layout(offset=4 ) temp bool ff3, layout(offset=4 ) temp 4-component vector of float FragCoord ff4}) 0:37 Constant: 0:37 7 (const int) 0:39 Branch: Return with expression @@ -23,7 +23,7 @@ gl_FragCoord origin is upper left 0:? Linker Objects 0:? 's1' (global structure{temp bool b, temp bool c, temp 4-component vector of float a, temp 4-component vector of float d}) 0:? 's2' (global structure{temp 4-component vector of float i}) -0:? 's4' (global structure{smooth in 4-component vector of float a, flat temp bool b, centroid noperspective temp 1-component vector of float c, centroid sample temp 2-component vector of float d, temp bool Face ff1, temp bool ff2, temp bool ff3, temp 4-component vector of float FragCoord ff4}) +0:? 's4' (global structure{smooth in 4-component vector of float a, flat temp bool b, centroid noperspective temp 1-component vector of float c, centroid sample temp 2-component vector of float d, temp bool Face ff1, layout(offset=4 ) temp bool ff2, layout(offset=4 ) temp bool ff3, layout(offset=4 ) temp 4-component vector of float FragCoord ff4}) Linked fragment stage: @@ -44,8 +44,8 @@ gl_FragCoord origin is upper left 0:37 's2' (global structure{temp 4-component vector of float i}) 0:37 Constant: 0:37 0 (const int) -0:37 ff4: direct index for structure (temp 4-component vector of float FragCoord) -0:37 's4' (global structure{smooth in 4-component vector of float a, flat temp bool b, centroid noperspective temp 1-component vector of float c, centroid sample temp 2-component vector of float d, temp bool Face ff1, temp bool ff2, temp bool ff3, temp 4-component vector of float FragCoord ff4}) +0:37 ff4: direct index for structure (layout(offset=4 ) temp 4-component vector of float FragCoord) +0:37 's4' (global structure{smooth in 4-component vector of float a, flat temp bool b, centroid noperspective temp 1-component vector of float c, centroid sample temp 2-component vector of float d, temp bool Face ff1, layout(offset=4 ) temp bool ff2, layout(offset=4 ) temp bool ff3, layout(offset=4 ) temp 4-component vector of float FragCoord ff4}) 0:37 Constant: 0:37 7 (const int) 0:39 Branch: Return with expression @@ -53,7 +53,7 @@ gl_FragCoord origin is upper left 0:? Linker Objects 0:? 's1' (global structure{temp bool b, temp bool c, temp 4-component vector of float a, temp 4-component vector of float d}) 0:? 's2' (global structure{temp 4-component vector of float i}) -0:? 's4' (global structure{smooth in 4-component vector of float a, flat temp bool b, centroid noperspective temp 1-component vector of float c, centroid sample temp 2-component vector of float d, temp bool Face ff1, temp bool ff2, temp bool ff3, temp 4-component vector of float FragCoord ff4}) +0:? 's4' (global structure{smooth in 4-component vector of float a, flat temp bool b, centroid noperspective temp 1-component vector of float c, centroid sample temp 2-component vector of float d, temp bool Face ff1, layout(offset=4 ) temp bool ff2, layout(offset=4 ) temp bool ff3, layout(offset=4 ) temp 4-component vector of float FragCoord ff4}) // Module Version 10000 // Generated by (magic number): 80001 diff --git a/Test/hlsl.buffer.frag b/Test/hlsl.buffer.frag index fbfdc316..e01e4fcb 100644 --- a/Test/hlsl.buffer.frag +++ b/Test/hlsl.buffer.frag @@ -13,7 +13,13 @@ cbuffer cbufName : register(b2) { tbuffer tbufName : register(b8) { float4 v4 : packoffset(c1); - int i4 : packoffset(c3); + int i4 : packoffset(c3); + float f1 : packoffset(c3.w); + float f3 : packoffset(c4.x); + float f4 : packoffset(c4.y); + float f5 : packoffset(c4.z); + float f6 : packoffset(c); + float f7; }; float4 PixelShaderFunction(float4 input) : COLOR0 diff --git a/glslang/Include/revision.h b/glslang/Include/revision.h index e77aff4c..fcaa435a 100644 --- a/glslang/Include/revision.h +++ b/glslang/Include/revision.h @@ -3,4 +3,4 @@ // For the date, it uses the current date (when then script is run). #define GLSLANG_REVISION "SPIRV99.1362" -#define GLSLANG_DATE "28-Jul-2016" +#define GLSLANG_DATE "29-Jul-2016" diff --git a/hlsl/hlslGrammar.cpp b/hlsl/hlslGrammar.cpp index c3825f83..a6c7ac96 100755 --- a/hlsl/hlslGrammar.cpp +++ b/hlsl/hlslGrammar.cpp @@ -2537,15 +2537,23 @@ void HlslGrammar::acceptPostDecls(TType& type) expected("("); return; } - acceptTokenClass(EHTokIdentifier); - acceptTokenClass(EHTokDot); - acceptTokenClass(EHTokIdentifier); + HlslToken locationToken; + if (! acceptIdentifier(locationToken)) { + expected("c[subcomponent][.component]"); + return; + } + HlslToken componentToken; + if (acceptTokenClass(EHTokDot)) { + if (! acceptIdentifier(componentToken)) { + expected("component"); + return; + } + } if (! acceptTokenClass(EHTokRightParen)) { expected(")"); break; } - // TODO: process the packoffset information - // c1.y means component y of location slot 1 + parseContext.handlePackOffset(locationToken.loc, type, *locationToken.string, componentToken.string); } else if (! acceptIdentifier(idToken)) { expected("semantic or packoffset or register"); return; diff --git a/hlsl/hlslParseHelper.cpp b/hlsl/hlslParseHelper.cpp index 2f728172..6022e7d9 100755 --- a/hlsl/hlslParseHelper.cpp +++ b/hlsl/hlslParseHelper.cpp @@ -2134,6 +2134,46 @@ void HlslParseContext::handleSemantic(TType& type, const TString& semantic) type.getQualifier().builtIn = EbvViewportIndex; } +// +// Handle seeing something like ": packoffset( c[Subcomponent][.component] )" +// +// 'location' has the "c[Subcomponent]" part. +// 'component' points to the "component" part, or nullptr if not present. +// +void HlslParseContext::handlePackOffset(const TSourceLoc& loc, TType& type, const glslang::TString& location, + const glslang::TString* component) +{ + if (location.size() == 0 || location[0] != 'c') { + error(loc, "expected 'c'", "packoffset", ""); + return; + } + if (location.size() == 1) + return; + if (! isdigit(location[1])) { + error(loc, "expected number after 'c'", "packoffset", ""); + return; + } + + type.getQualifier().layoutOffset = 16 * atoi(location.substr(1, location.size()).c_str()); + if (component) { + int componentOffset = 0; + switch ((*component)[0]) { + case 'x': componentOffset = 0; break; + case 'y': componentOffset = 4; break; + case 'z': componentOffset = 8; break; + case 'w': componentOffset = 12; break; + default: + componentOffset = -1; + break; + } + if (componentOffset < 0 || component->size() > 1) { + error(loc, "expected {x, y, z, w} for component", "packoffset", ""); + return; + } + type.getQualifier().layoutOffset += componentOffset; + } +} + // // Same error message for all places assignments don't work. // diff --git a/hlsl/hlslParseHelper.h b/hlsl/hlslParseHelper.h index 979e4a48..7a02ebb4 100755 --- a/hlsl/hlslParseHelper.h +++ b/hlsl/hlslParseHelper.h @@ -95,6 +95,7 @@ public: void builtInOpCheck(const TSourceLoc&, const TFunction&, TIntermOperator&); TFunction* handleConstructorCall(const TSourceLoc&, const TType&); void handleSemantic(TType& type, const TString& semantic); + void handlePackOffset(const TSourceLoc&, TType& type, const glslang::TString& location, const glslang::TString* component); TIntermAggregate* handleSamplerTextureCombine(const TSourceLoc& loc, TIntermTyped* argTex, TIntermTyped* argSampler);