Commit 7e997e26 by John Kessenich

HLSL: Implicit bool conversions for conditional expressions and related.

Covers if(cond), while(cond), do-while(cond), for(;cond;), and (cond ? :). Fixes #778.
parent 8f9fdc98
......@@ -9,7 +9,7 @@ gl_FragCoord origin is upper left
0:11 Test condition and select ( temp void)
0:11 Condition
0:11 Constant:
0:11 0 (const int)
0:11 false (const bool)
0:11 true case is null
0:2 Function Definition: PixelShaderFunction( ( temp void)
0:2 Function Parameters:
......@@ -36,7 +36,7 @@ gl_FragCoord origin is upper left
0:11 Test condition and select ( temp void)
0:11 Condition
0:11 Constant:
0:11 0 (const int)
0:11 false (const bool)
0:11 true case is null
0:2 Function Definition: PixelShaderFunction( ( temp void)
0:2 Function Parameters:
......@@ -71,8 +71,8 @@ gl_FragCoord origin is upper left
7: TypeVector 6(float) 4
8: TypePointer Function 7(fvec4)
9: TypeFunction 2 8(ptr)
13: TypeInt 32 1
14: 13(int) Constant 0
13: TypeBool
14: 13(bool) ConstantFalse
18: TypePointer Input 7(fvec4)
19(input): 18(ptr) Variable Input
4(PixelShaderFunction): 2 Function None 3
......
......@@ -25,6 +25,7 @@ gl_FragCoord origin is upper left
0:9 2 (const int)
0:10 Test condition and select ( temp void)
0:10 Condition
0:10 Convert float to bool ( temp bool)
0:10 direct index ( temp float)
0:10 'input' ( in 4-component vector of float)
0:10 Constant:
......@@ -80,6 +81,7 @@ gl_FragCoord origin is upper left
0:9 2 (const int)
0:10 Test condition and select ( temp void)
0:10 Condition
0:10 Convert float to bool ( temp bool)
0:10 direct index ( temp float)
0:10 'input' ( in 4-component vector of float)
0:10 Constant:
......@@ -107,12 +109,12 @@ gl_FragCoord origin is upper left
// Module Version 10000
// Generated by (magic number): 80001
// Id's are bound by 48
// Id's are bound by 50
Capability Shader
1: ExtInstImport "GLSL.std.450"
MemoryModel Logical GLSL450
EntryPoint Fragment 4 "PixelShaderFunction" 43
EntryPoint Fragment 4 "PixelShaderFunction" 45
ExecutionMode 4 OriginUpperLeft
Name 4 "PixelShaderFunction"
Name 10 "foo(f1;"
......@@ -120,11 +122,11 @@ gl_FragCoord origin is upper left
Name 16 "@PixelShaderFunction(vf4;"
Name 15 "input"
Name 25 "param"
Name 37 "f"
Name 41 "input"
Name 39 "f"
Name 43 "input"
Name 45 "param"
Decorate 43(input) Location 0
Name 45 "input"
Name 47 "param"
Decorate 45(input) Location 0
2: TypeVoid
3: TypeFunction 2
6: TypeFloat 32
......@@ -138,17 +140,18 @@ gl_FragCoord origin is upper left
26: TypeInt 32 0
27: 26(int) Constant 2
31: 26(int) Constant 0
42: TypePointer Input 12(fvec4)
43(input): 42(ptr) Variable Input
34: 6(float) Constant 0
44: TypePointer Input 12(fvec4)
45(input): 44(ptr) Variable Input
4(PixelShaderFunction): 2 Function None 3
5: Label
41(input): 13(ptr) Variable Function
45(param): 13(ptr) Variable Function
44: 12(fvec4) Load 43(input)
Store 41(input) 44
46: 12(fvec4) Load 41(input)
Store 45(param) 46
47: 2 FunctionCall 16(@PixelShaderFunction(vf4;) 45(param)
43(input): 13(ptr) Variable Function
47(param): 13(ptr) Variable Function
46: 12(fvec4) Load 45(input)
Store 43(input) 46
48: 12(fvec4) Load 43(input)
Store 47(param) 48
49: 2 FunctionCall 16(@PixelShaderFunction(vf4;) 47(param)
Return
FunctionEnd
10(foo(f1;): 2 Function None 8
......@@ -167,20 +170,21 @@ gl_FragCoord origin is upper left
15(input): 13(ptr) FunctionParameter
17: Label
25(param): 7(ptr) Variable Function
37(f): 7(ptr) Variable Function
39(f): 7(ptr) Variable Function
28: 7(ptr) AccessChain 15(input) 27
29: 6(float) Load 28
Store 25(param) 29
30: 2 FunctionCall 10(foo(f1;) 25(param)
32: 7(ptr) AccessChain 15(input) 31
33: 6(float) Load 32
SelectionMerge 35 None
BranchConditional 33 34 35
34: Label
35: 20(bool) FOrdNotEqual 33 34
SelectionMerge 37 None
BranchConditional 35 36 37
36: Label
Kill
35: Label
38: 7(ptr) AccessChain 15(input) 31
39: 6(float) Load 38
Store 37(f) 39
37: Label
40: 7(ptr) AccessChain 15(input) 31
41: 6(float) Load 40
Store 39(f) 41
Kill
FunctionEnd
......@@ -69,6 +69,7 @@ gl_FragCoord origin is upper left
0:26 'input' ( in 4-component vector of float)
0:30 Test condition and select ( temp void)
0:30 Condition
0:30 Convert float to bool ( temp bool)
0:30 move second child to first child ( temp float)
0:30 'ii' ( temp float)
0:30 direct index ( temp float)
......@@ -178,6 +179,7 @@ gl_FragCoord origin is upper left
0:26 'input' ( in 4-component vector of float)
0:30 Test condition and select ( temp void)
0:30 Condition
0:30 Convert float to bool ( temp bool)
0:30 move second child to first child ( temp float)
0:30 'ii' ( temp float)
0:30 direct index ( temp float)
......@@ -215,24 +217,24 @@ gl_FragCoord origin is upper left
// Module Version 10000
// Generated by (magic number): 80001
// Id's are bound by 101
// Id's are bound by 103
Capability Shader
1: ExtInstImport "GLSL.std.450"
MemoryModel Logical GLSL450
EntryPoint Fragment 4 "PixelShaderFunction" 94 97
EntryPoint Fragment 4 "PixelShaderFunction" 96 99
ExecutionMode 4 OriginUpperLeft
Name 4 "PixelShaderFunction"
Name 11 "@PixelShaderFunction(vf4;"
Name 10 "input"
Name 68 "ii"
Name 80 "ii"
Name 92 "input"
Name 82 "ii"
Name 94 "input"
Name 97 "@entryPointOutput"
Name 98 "param"
Decorate 94(input) Location 0
Decorate 97(@entryPointOutput) Location 0
Name 96 "input"
Name 99 "@entryPointOutput"
Name 100 "param"
Decorate 96(input) Location 0
Decorate 99(@entryPointOutput) Location 0
2: TypeVoid
3: TypeFunction 2
6: TypeFloat 32
......@@ -244,31 +246,32 @@ gl_FragCoord origin is upper left
67: TypePointer Function 6(float)
69: TypeInt 32 0
70: 69(int) Constant 2
76: 6(float) Constant 1065353216
78: TypeInt 32 1
79: TypePointer Function 78(int)
82: 78(int) Constant 1
93: TypePointer Input 7(fvec4)
94(input): 93(ptr) Variable Input
96: TypePointer Output 7(fvec4)
97(@entryPointOutput): 96(ptr) Variable Output
73: 6(float) Constant 0
78: 6(float) Constant 1065353216
80: TypeInt 32 1
81: TypePointer Function 80(int)
84: 80(int) Constant 1
95: TypePointer Input 7(fvec4)
96(input): 95(ptr) Variable Input
98: TypePointer Output 7(fvec4)
99(@entryPointOutput): 98(ptr) Variable Output
4(PixelShaderFunction): 2 Function None 3
5: Label
92(input): 8(ptr) Variable Function
98(param): 8(ptr) Variable Function
95: 7(fvec4) Load 94(input)
Store 92(input) 95
99: 7(fvec4) Load 92(input)
Store 98(param) 99
100: 7(fvec4) FunctionCall 11(@PixelShaderFunction(vf4;) 98(param)
Store 97(@entryPointOutput) 100
94(input): 8(ptr) Variable Function
100(param): 8(ptr) Variable Function
97: 7(fvec4) Load 96(input)
Store 94(input) 97
101: 7(fvec4) Load 94(input)
Store 100(param) 101
102: 7(fvec4) FunctionCall 11(@PixelShaderFunction(vf4;) 100(param)
Store 99(@entryPointOutput) 102
Return
FunctionEnd
11(@PixelShaderFunction(vf4;): 7(fvec4) Function None 9
10(input): 8(ptr) FunctionParameter
12: Label
68(ii): 67(ptr) Variable Function
80(ii): 79(ptr) Variable Function
82(ii): 81(ptr) Variable Function
13: 7(fvec4) Load 10(input)
14: 7(fvec4) Load 10(input)
17: 16(bvec4) FOrdEqual 13 14
......@@ -338,28 +341,29 @@ gl_FragCoord origin is upper left
71: 67(ptr) AccessChain 10(input) 70
72: 6(float) Load 71
Store 68(ii) 72
SelectionMerge 74 None
BranchConditional 72 73 74
73: Label
75: 6(float) Load 68(ii)
77: 6(float) FAdd 75 76
Store 68(ii) 77
Branch 74
74: Label
81: 78(int) Load 80(ii)
83: 78(int) IAdd 81 82
Store 80(ii) 83
84: 78(int) Load 80(ii)
85: 6(float) ConvertSToF 84
86: 15(bool) FOrdEqual 85 76
SelectionMerge 88 None
BranchConditional 86 87 88
87: Label
89: 78(int) Load 80(ii)
90: 78(int) IAdd 89 82
Store 80(ii) 90
Branch 88
88: Label
91: 7(fvec4) Undef
ReturnValue 91
74: 15(bool) FOrdNotEqual 72 73
SelectionMerge 76 None
BranchConditional 74 75 76
75: Label
77: 6(float) Load 68(ii)
79: 6(float) FAdd 77 78
Store 68(ii) 79
Branch 76
76: Label
83: 80(int) Load 82(ii)
85: 80(int) IAdd 83 84
Store 82(ii) 85
86: 80(int) Load 82(ii)
87: 6(float) ConvertSToF 86
88: 15(bool) FOrdEqual 87 78
SelectionMerge 90 None
BranchConditional 88 89 90
89: Label
91: 80(int) Load 82(ii)
92: 80(int) IAdd 91 84
Store 82(ii) 92
Branch 90
90: Label
93: 7(fvec4) Undef
ReturnValue 93
FunctionEnd
......@@ -31,6 +31,7 @@ gl_FragCoord origin is upper left
0:17 3 (const uint)
0:19 Test condition and select ( temp void)
0:19 Condition
0:19 Convert int to bool ( temp bool)
0:19 ival: direct index for structure ( uniform int)
0:19 'anon@0' (layout( row_major std140) uniform block{ uniform int ival, uniform 4-component vector of int ival4, uniform float fval, uniform 4-component vector of float fval4})
0:19 Constant:
......@@ -38,6 +39,7 @@ gl_FragCoord origin is upper left
0:19 true case is null
0:20 Test condition and select ( temp void)
0:20 Condition
0:20 Convert float to bool ( temp bool)
0:20 fval: direct index for structure ( uniform float)
0:20 'anon@0' (layout( row_major std140) uniform block{ uniform int ival, uniform 4-component vector of int ival4, uniform float fval, uniform 4-component vector of float fval4})
0:20 Constant:
......@@ -123,6 +125,7 @@ gl_FragCoord origin is upper left
0:17 3 (const uint)
0:19 Test condition and select ( temp void)
0:19 Condition
0:19 Convert int to bool ( temp bool)
0:19 ival: direct index for structure ( uniform int)
0:19 'anon@0' (layout( row_major std140) uniform block{ uniform int ival, uniform 4-component vector of int ival4, uniform float fval, uniform 4-component vector of float fval4})
0:19 Constant:
......@@ -130,6 +133,7 @@ gl_FragCoord origin is upper left
0:19 true case is null
0:20 Test condition and select ( temp void)
0:20 Condition
0:20 Convert float to bool ( temp bool)
0:20 fval: direct index for structure ( uniform float)
0:20 'anon@0' (layout( row_major std140) uniform block{ uniform int ival, uniform 4-component vector of int ival4, uniform float fval, uniform 4-component vector of float fval4})
0:20 Constant:
......@@ -181,12 +185,12 @@ gl_FragCoord origin is upper left
// Module Version 10000
// Generated by (magic number): 80001
// Id's are bound by 82
// Id's are bound by 84
Capability Shader
1: ExtInstImport "GLSL.std.450"
MemoryModel Logical GLSL450
EntryPoint Fragment 4 "main" 79
EntryPoint Fragment 4 "main" 81
ExecutionMode 4 OriginUpperLeft
Name 4 "main"
Name 8 "PS_OUTPUT"
......@@ -198,15 +202,15 @@ gl_FragCoord origin is upper left
MemberName 14($Global) 2 "fval"
MemberName 14($Global) 3 "fval4"
Name 16 ""
Name 70 "psout"
Name 79 "Color"
Name 72 "psout"
Name 81 "Color"
MemberDecorate 14($Global) 0 Offset 0
MemberDecorate 14($Global) 1 Offset 16
MemberDecorate 14($Global) 2 Offset 32
MemberDecorate 14($Global) 3 Offset 48
Decorate 14($Global) Block
Decorate 16 DescriptorSet 0
Decorate 79(Color) Location 0
Decorate 81(Color) Location 0
2: TypeVoid
3: TypeFunction 2
6: TypeFloat 32
......@@ -234,22 +238,22 @@ gl_FragCoord origin is upper left
42: 12(int) Constant 3
43: TypePointer Uniform 7(fvec4)
46: 7(fvec4) ConstantComposite 39 39 39 39
69: TypePointer Function 8(PS_OUTPUT)
71: 6(float) Constant 1065353216
72: 7(fvec4) ConstantComposite 71 71 71 71
73: TypePointer Function 7(fvec4)
78: TypePointer Output 7(fvec4)
79(Color): 78(ptr) Variable Output
71: TypePointer Function 8(PS_OUTPUT)
73: 6(float) Constant 1065353216
74: 7(fvec4) ConstantComposite 73 73 73 73
75: TypePointer Function 7(fvec4)
80: TypePointer Output 7(fvec4)
81(Color): 80(ptr) Variable Output
4(main): 2 Function None 3
5: Label
80:8(PS_OUTPUT) FunctionCall 10(@main()
81: 7(fvec4) CompositeExtract 80 0
Store 79(Color) 81
82:8(PS_OUTPUT) FunctionCall 10(@main()
83: 7(fvec4) CompositeExtract 82 0
Store 81(Color) 83
Return
FunctionEnd
10(@main():8(PS_OUTPUT) Function None 9
11: Label
70(psout): 69(ptr) Variable Function
72(psout): 71(ptr) Variable Function
19: 18(ptr) AccessChain 16 17
20: 12(int) Load 19
24: 21(bool) INotEqual 20 23
......@@ -268,38 +272,40 @@ gl_FragCoord origin is upper left
48: 30(bvec4) LogicalNot 47
49: 18(ptr) AccessChain 16 17
50: 12(int) Load 49
SelectionMerge 52 None
BranchConditional 50 51 52
51: Label
Branch 52
51: 21(bool) INotEqual 50 23
SelectionMerge 53 None
BranchConditional 51 52 53
52: Label
53: 36(ptr) AccessChain 16 35
54: 6(float) Load 53
SelectionMerge 56 None
BranchConditional 54 55 56
55: Label
Branch 56
56: Label
57: 18(ptr) AccessChain 16 17
58: 12(int) Load 57
59: 21(bool) INotEqual 58 23
60: 21(bool) LogicalNot 59
SelectionMerge 62 None
BranchConditional 60 61 62
61: Label
Branch 62
62: Label
63: 36(ptr) AccessChain 16 35
64: 6(float) Load 63
65: 21(bool) FOrdNotEqual 64 39
66: 21(bool) LogicalNot 65
SelectionMerge 68 None
BranchConditional 66 67 68
67: Label
Branch 68
68: Label
74: 73(ptr) AccessChain 70(psout) 17
Store 74 72
75:8(PS_OUTPUT) Load 70(psout)
ReturnValue 75
Branch 53
53: Label
54: 36(ptr) AccessChain 16 35
55: 6(float) Load 54
56: 21(bool) FOrdNotEqual 55 39
SelectionMerge 58 None
BranchConditional 56 57 58
57: Label
Branch 58
58: Label
59: 18(ptr) AccessChain 16 17
60: 12(int) Load 59
61: 21(bool) INotEqual 60 23
62: 21(bool) LogicalNot 61
SelectionMerge 64 None
BranchConditional 62 63 64
63: Label
Branch 64
64: Label
65: 36(ptr) AccessChain 16 35
66: 6(float) Load 65
67: 21(bool) FOrdNotEqual 66 39
68: 21(bool) LogicalNot 67
SelectionMerge 70 None
BranchConditional 68 69 70
69: Label
Branch 70
70: Label
76: 75(ptr) AccessChain 72(psout) 17
Store 76 74
77:8(PS_OUTPUT) Load 72(psout)
ReturnValue 77
FunctionEnd
float condf;
int condi;
float1 condf1;
int1 condi1;
float4 main() : SV_Target0
{
float4 a = float4(2.0, 2.0, 2.0, 2.0);
if (condi)
return a + 1.0;
if (condf)
return a + 2.0;
if (condf1)
return a + 3.0;
if (condi1)
return a + 4.0;
if (condi && condf || condf1)
return a + 5.0;
float f = condf;
while (f) { --f; }
int i = condi;
do { --i; } while (i);
for (; i; ) { --i; }
float g = condf ? 7.0 : 8.0;
a += g;
return a - 1.0;
}
\ No newline at end of file
......@@ -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.1946"
#define GLSLANG_REVISION "Overload400-PrecQual.1947"
#define GLSLANG_DATE "30-Mar-2017"
......@@ -127,6 +127,7 @@ INSTANTIATE_TEST_CASE_P(
{"hlsl.hull.void.tesc", "main"},
{"hlsl.identifier.sample.frag", "main"},
{"hlsl.if.frag", "PixelShaderFunction"},
{"hlsl.implicitBool.frag", "main"},
{"hlsl.inoutquals.frag", "main"},
{"hlsl.init.frag", "ShaderFunction"},
{"hlsl.init2.frag", "main"},
......
......@@ -2527,6 +2527,10 @@ bool HlslGrammar::acceptConditionalExpression(TIntermTyped*& node)
if (! acceptTokenClass(EHTokQuestion))
return true;
node = parseContext.convertConditionalExpression(token.loc, node);
if (node == nullptr)
return false;
TIntermTyped* trueNode = nullptr;
if (! acceptExpression(trueNode)) {
expected("expression after ?");
......@@ -3197,6 +3201,9 @@ bool HlslGrammar::acceptSelectionStatement(TIntermNode*& statement)
TIntermTyped* condition;
if (! acceptParenExpression(condition))
return false;
condition = parseContext.convertConditionalExpression(loc, condition);
if (condition == nullptr)
return false;
// create the child statements
TIntermNodePair thenElse = { nullptr, nullptr };
......@@ -3280,6 +3287,9 @@ bool HlslGrammar::acceptIterationStatement(TIntermNode*& statement)
// LEFT_PAREN condition RIGHT_PAREN
if (! acceptParenExpression(condition))
return false;
condition = parseContext.convertConditionalExpression(loc, condition);
if (condition == nullptr)
return false;
// statement
if (! acceptScopedStatement(statement)) {
......@@ -3319,6 +3329,9 @@ bool HlslGrammar::acceptIterationStatement(TIntermNode*& statement)
TIntermTyped* condition;
if (! acceptParenExpression(condition))
return false;
condition = parseContext.convertConditionalExpression(loc, condition);
if (condition == nullptr)
return false;
if (! acceptTokenClass(EHTokSemicolon))
expected(";");
......@@ -3356,6 +3369,11 @@ bool HlslGrammar::acceptIterationStatement(TIntermNode*& statement)
acceptExpression(condition);
if (! acceptTokenClass(EHTokSemicolon))
expected(";");
if (condition != nullptr) {
condition = parseContext.convertConditionalExpression(loc, condition);
if (condition == nullptr)
return false;
}
// iterator SEMI_COLON
TIntermTyped* iterator = nullptr;
......
......@@ -4285,6 +4285,18 @@ void HlslParseContext::handleRegister(const TSourceLoc& loc, TQualifier& qualifi
}
}
// Convert to a scalar boolean, or if not allowed by HLSL semantics,
// report an error and return nullptr.
TIntermTyped* HlslParseContext::convertConditionalExpression(const TSourceLoc& loc, TIntermTyped* condition)
{
if (!condition->getType().isScalarOrVec1()) {
error(loc, "requires a scalar", "conditional expression", "");
return nullptr;
}
return intermediate.addConversion(EOpConstructBool, TType(EbtBool), condition);
}
//
// Same error message for all places assignments don't work.
//
......@@ -4607,13 +4619,6 @@ bool HlslParseContext::voidErrorCheck(const TSourceLoc& loc, const TString& iden
return false;
}
// Checks to see if the node (for the expression) contains a scalar boolean expression or not
void HlslParseContext::boolCheck(const TSourceLoc& loc, const TIntermTyped* type)
{
if (type->getBasicType() != EbtBool || type->isArray() || type->isMatrix() || type->isVector())
error(loc, "boolean expression expected", "", "");
}
//
// Fix just a full qualifier (no variables or types yet, but qualifier is complete) at global level.
//
......
......@@ -101,7 +101,7 @@ public:
const glslang::TString* component);
void handleRegister(const TSourceLoc&, TQualifier&, const glslang::TString* profile, const glslang::TString& desc,
int subComponent, const glslang::TString*);
TIntermTyped* convertConditionalExpression(const TSourceLoc&, TIntermTyped*);
TIntermAggregate* handleSamplerTextureCombine(const TSourceLoc& loc, TIntermTyped* argTex, TIntermTyped* argSampler);
bool parseMatrixSwizzleSelector(const TSourceLoc&, const TString&, int cols, int rows, TSwizzleSelectors<TMatrixSelector>&);
......@@ -120,7 +120,6 @@ public:
void structArrayCheck(const TSourceLoc&, const TType& structure);
void arrayDimMerge(TType& type, const TArraySizes* sizes);
bool voidErrorCheck(const TSourceLoc&, const TString&, TBasicType);
void boolCheck(const TSourceLoc&, const TIntermTyped*);
void globalQualifierFix(const TSourceLoc&, TQualifier&);
bool structQualifierErrorCheck(const TSourceLoc&, const TPublicType& pType);
void mergeQualifiers(TQualifier& dst, const TQualifier& src);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment