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,10 +25,11 @@ gl_FragCoord origin is upper left
0:9 2 (const int)
0:10 Test condition and select ( temp void)
0:10 Condition
0:10 direct index ( temp float)
0:10 'input' ( in 4-component vector of float)
0:10 Constant:
0:10 0 (const int)
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:
0:10 0 (const int)
0:10 true case
0:11 Branch: Kill
0:12 Sequence
......@@ -80,10 +81,11 @@ gl_FragCoord origin is upper left
0:9 2 (const int)
0:10 Test condition and select ( temp void)
0:10 Condition
0:10 direct index ( temp float)
0:10 'input' ( in 4-component vector of float)
0:10 Constant:
0:10 0 (const int)
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:
0:10 0 (const int)
0:10 true case
0:11 Branch: Kill
0:12 Sequence
......@@ -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,12 +69,13 @@ 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 move second child to first child ( temp float)
0:30 'ii' ( temp float)
0:30 direct index ( temp float)
0:30 'input' ( in 4-component vector of float)
0:30 Constant:
0:30 2 (const int)
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)
0:30 'input' ( in 4-component vector of float)
0:30 Constant:
0:30 2 (const int)
0:30 true case
0:31 Pre-Increment ( temp float)
0:31 'ii' ( temp float)
......@@ -178,12 +179,13 @@ 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 move second child to first child ( temp float)
0:30 'ii' ( temp float)
0:30 direct index ( temp float)
0:30 'input' ( in 4-component vector of float)
0:30 Constant:
0:30 2 (const int)
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)
0:30 'input' ( in 4-component vector of float)
0:30 Constant:
0:30 2 (const int)
0:30 true case
0:31 Pre-Increment ( temp float)
0:31 'ii' ( 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
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