Restrict GLSL globals to be initialized with constant expressions.
This follows the GLSL ES 1.0 and 3.0 specs more strictly.
Change-Id: I323e90ef0a1588109e2cb7988136a9520e501a6d
Reviewed-on: https://swiftshader-review.googlesource.com/5065
Tested-by: Nicolas Capens <nicolascapens@google.com>
Reviewed-by: Alexis Hétu <sugoi@google.com>
Reviewed-by: Nicolas Capens <nicolascapens@google.com>
diff --git a/src/OpenGL/compiler/Android.mk b/src/OpenGL/compiler/Android.mk
index 5bca1fe..da35aa0 100644
--- a/src/OpenGL/compiler/Android.mk
+++ b/src/OpenGL/compiler/Android.mk
@@ -69,7 +69,6 @@
SymbolTable.cpp \
TranslatorASM.cpp \
util.cpp \
- ValidateGlobalInitializer.cpp \
ValidateLimitations.cpp \
ValidateSwitch.cpp \
diff --git a/src/OpenGL/compiler/BUILD.gn b/src/OpenGL/compiler/BUILD.gn
index 3341e1e..3759f1c 100644
--- a/src/OpenGL/compiler/BUILD.gn
+++ b/src/OpenGL/compiler/BUILD.gn
@@ -57,7 +57,6 @@
"PoolAlloc.cpp",
"SymbolTable.cpp",
"TranslatorASM.cpp",
- "ValidateGlobalInitializer.cpp",
"ValidateLimitations.cpp",
"ValidateSwitch.cpp",
"debug.cpp",
diff --git a/src/OpenGL/compiler/Compiler.vcxproj b/src/OpenGL/compiler/Compiler.vcxproj
index 58a0e5a..c4f56bf 100644
--- a/src/OpenGL/compiler/Compiler.vcxproj
+++ b/src/OpenGL/compiler/Compiler.vcxproj
@@ -229,7 +229,6 @@
<ClCompile Include="SymbolTable.cpp" />
<ClCompile Include="TranslatorASM.cpp" />
<ClCompile Include="util.cpp" />
- <ClCompile Include="ValidateGlobalInitializer.cpp" />
<ClCompile Include="ValidateLimitations.cpp" />
<ClCompile Include="glslang_lex.cpp" />
<ClCompile Include="glslang_tab.cpp" />
@@ -334,7 +333,6 @@
<ClInclude Include="TranslatorASM.h" />
<ClInclude Include="Types.h" />
<ClInclude Include="util.h" />
- <ClInclude Include="ValidateGlobalInitializer.h" />
<ClInclude Include="ValidateLimitations.h" />
<ClInclude Include="glslang_tab.h" />
<ClInclude Include="ValidateSwitch.h" />
diff --git a/src/OpenGL/compiler/Compiler.vcxproj.filters b/src/OpenGL/compiler/Compiler.vcxproj.filters
index a4435bc..91d0749 100644
--- a/src/OpenGL/compiler/Compiler.vcxproj.filters
+++ b/src/OpenGL/compiler/Compiler.vcxproj.filters
@@ -86,9 +86,6 @@
<ClCompile Include="ValidateSwitch.cpp">
<Filter>Source Files</Filter>
</ClCompile>
- <ClCompile Include="ValidateGlobalInitializer.cpp">
- <Filter>Source Files</Filter>
- </ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="BaseTypes.h">
@@ -178,9 +175,6 @@
<ClInclude Include="ValidateSwitch.h">
<Filter>Header Files</Filter>
</ClInclude>
- <ClInclude Include="ValidateGlobalInitializer.h">
- <Filter>Header Files</Filter>
- </ClInclude>
</ItemGroup>
<ItemGroup>
<CustomBuild Include="glslang.l">
diff --git a/src/OpenGL/compiler/ParseHelper.cpp b/src/OpenGL/compiler/ParseHelper.cpp
index 7cca42c..3fea084 100644
--- a/src/OpenGL/compiler/ParseHelper.cpp
+++ b/src/OpenGL/compiler/ParseHelper.cpp
@@ -19,7 +19,6 @@
#include "glslang.h"
#include "preprocessor/SourceLocation.h"
-#include "ValidateGlobalInitializer.h"
#include "ValidateSwitch.h"
///////////////////////////////////////////////////////////////////////
@@ -1300,19 +1299,11 @@
return true;
}
- bool globalInitWarning = false;
- if(symbolTable.atGlobalLevel() && !ValidateGlobalInitializer(initializer, this, &globalInitWarning))
+ if(symbolTable.atGlobalLevel() && initializer->getQualifier() != EvqConstExpr)
{
- // Error message does not completely match behavior with ESSL 1.00, but
- // we want to steer developers towards only using constant expressions.
error(line, "global variable initializers must be constant expressions", "=");
return true;
}
- if(globalInitWarning)
- {
- warning(line, "global variable initializers should be constant expressions "
- "(uniforms and globals are allowed in global initializers for legacy compatibility)", "=");
- }
//
// identifier must be of type constant, a global, or a temporary
diff --git a/src/OpenGL/compiler/ValidateGlobalInitializer.cpp b/src/OpenGL/compiler/ValidateGlobalInitializer.cpp
deleted file mode 100644
index 6c5fa86..0000000
--- a/src/OpenGL/compiler/ValidateGlobalInitializer.cpp
+++ /dev/null
@@ -1,88 +0,0 @@
-// Copyright 2016 The SwiftShader Authors. All Rights Reserved.
-//
-// Licensed under the Apache License, Version 2.0 (the "License");
-// you may not use this file except in compliance with the License.
-// You may obtain a copy of the License at
-//
-// http://www.apache.org/licenses/LICENSE-2.0
-//
-// Unless required by applicable law or agreed to in writing, software
-// distributed under the License is distributed on an "AS IS" BASIS,
-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-// See the License for the specific language governing permissions and
-// limitations under the License.
-
-#include "ValidateGlobalInitializer.h"
-
-#include "ParseHelper.h"
-
-namespace
-{
-
-class ValidateGlobalInitializerTraverser : public TIntermTraverser
-{
-public:
- ValidateGlobalInitializerTraverser(const TParseContext *context);
-
- void visitSymbol(TIntermSymbol *node) override;
-
- bool isValid() const { return mIsValid; }
- bool issueWarning() const { return mIssueWarning; }
-
-private:
- const TParseContext *mContext;
- bool mIsValid;
- bool mIssueWarning;
-};
-
-void ValidateGlobalInitializerTraverser::visitSymbol(TIntermSymbol *node)
-{
- const TSymbol *sym = mContext->symbolTable.find(node->getSymbol(), mContext->getShaderVersion());
- if (sym->isVariable())
- {
- // ESSL 1.00 section 4.3 (or ESSL 3.00 section 4.3):
- // Global initializers must be constant expressions.
- const TVariable *var = static_cast<const TVariable *>(sym);
- switch (var->getType().getQualifier())
- {
- case EvqConstExpr:
- break;
- case EvqGlobal:
- case EvqTemporary:
- case EvqUniform:
- // We allow these cases to be compatible with legacy ESSL 1.00 content.
- // Implement stricter rules for ESSL 3.00 since there's no legacy content to deal with.
- if (mContext->getShaderVersion() >= 300)
- {
- mIsValid = false;
- }
- else
- {
- mIssueWarning = true;
- }
- break;
- default:
- mIsValid = false;
- }
- }
-}
-
-ValidateGlobalInitializerTraverser::ValidateGlobalInitializerTraverser(const TParseContext *context)
- : TIntermTraverser(true, false, false),
- mContext(context),
- mIsValid(true),
- mIssueWarning(false)
-{
-}
-
-} // namespace
-
-bool ValidateGlobalInitializer(TIntermTyped *initializer, const TParseContext *context, bool *warning)
-{
- ValidateGlobalInitializerTraverser validate(context);
- initializer->traverse(&validate);
- ASSERT(warning != nullptr);
- *warning = validate.issueWarning();
- return validate.isValid();
-}
-
diff --git a/src/OpenGL/compiler/ValidateGlobalInitializer.h b/src/OpenGL/compiler/ValidateGlobalInitializer.h
deleted file mode 100644
index 28c1293..0000000
--- a/src/OpenGL/compiler/ValidateGlobalInitializer.h
+++ /dev/null
@@ -1,24 +0,0 @@
-// Copyright 2016 The SwiftShader Authors. All Rights Reserved.
-//
-// Licensed under the Apache License, Version 2.0 (the "License");
-// you may not use this file except in compliance with the License.
-// You may obtain a copy of the License at
-//
-// http://www.apache.org/licenses/LICENSE-2.0
-//
-// Unless required by applicable law or agreed to in writing, software
-// distributed under the License is distributed on an "AS IS" BASIS,
-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-// See the License for the specific language governing permissions and
-// limitations under the License.
-
-#ifndef COMPILER_TRANSLATOR_VALIDATEGLOBALINITIALIZER_H_
-#define COMPILER_TRANSLATOR_VALIDATEGLOBALINITIALIZER_H_
-
-class TIntermTyped;
-class TParseContext;
-
-// Returns true if the initializer is valid.
-bool ValidateGlobalInitializer(TIntermTyped *initializer, const TParseContext *context, bool *warning);
-
-#endif // COMPILER_TRANSLATOR_VALIDATEGLOBALINITIALIZER_H_