Commit bf1307b3 by Nicolas Capens Committed by Nicolas Capens

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/5065Tested-by: 's avatarNicolas Capens <nicolascapens@google.com> Reviewed-by: 's avatarAlexis Hétu <sugoi@google.com> Reviewed-by: 's avatarNicolas Capens <nicolascapens@google.com>
parent 7d9bdcb9
...@@ -69,7 +69,6 @@ COMMON_SRC_FILES := \ ...@@ -69,7 +69,6 @@ COMMON_SRC_FILES := \
SymbolTable.cpp \ SymbolTable.cpp \
TranslatorASM.cpp \ TranslatorASM.cpp \
util.cpp \ util.cpp \
ValidateGlobalInitializer.cpp \
ValidateLimitations.cpp \ ValidateLimitations.cpp \
ValidateSwitch.cpp \ ValidateSwitch.cpp \
......
...@@ -57,7 +57,6 @@ swiftshader_source_set("swiftshader_opengl_compiler") { ...@@ -57,7 +57,6 @@ swiftshader_source_set("swiftshader_opengl_compiler") {
"PoolAlloc.cpp", "PoolAlloc.cpp",
"SymbolTable.cpp", "SymbolTable.cpp",
"TranslatorASM.cpp", "TranslatorASM.cpp",
"ValidateGlobalInitializer.cpp",
"ValidateLimitations.cpp", "ValidateLimitations.cpp",
"ValidateSwitch.cpp", "ValidateSwitch.cpp",
"debug.cpp", "debug.cpp",
......
...@@ -229,7 +229,6 @@ ...@@ -229,7 +229,6 @@
<ClCompile Include="SymbolTable.cpp" /> <ClCompile Include="SymbolTable.cpp" />
<ClCompile Include="TranslatorASM.cpp" /> <ClCompile Include="TranslatorASM.cpp" />
<ClCompile Include="util.cpp" /> <ClCompile Include="util.cpp" />
<ClCompile Include="ValidateGlobalInitializer.cpp" />
<ClCompile Include="ValidateLimitations.cpp" /> <ClCompile Include="ValidateLimitations.cpp" />
<ClCompile Include="glslang_lex.cpp" /> <ClCompile Include="glslang_lex.cpp" />
<ClCompile Include="glslang_tab.cpp" /> <ClCompile Include="glslang_tab.cpp" />
...@@ -334,7 +333,6 @@ ...@@ -334,7 +333,6 @@
<ClInclude Include="TranslatorASM.h" /> <ClInclude Include="TranslatorASM.h" />
<ClInclude Include="Types.h" /> <ClInclude Include="Types.h" />
<ClInclude Include="util.h" /> <ClInclude Include="util.h" />
<ClInclude Include="ValidateGlobalInitializer.h" />
<ClInclude Include="ValidateLimitations.h" /> <ClInclude Include="ValidateLimitations.h" />
<ClInclude Include="glslang_tab.h" /> <ClInclude Include="glslang_tab.h" />
<ClInclude Include="ValidateSwitch.h" /> <ClInclude Include="ValidateSwitch.h" />
......
...@@ -86,9 +86,6 @@ ...@@ -86,9 +86,6 @@
<ClCompile Include="ValidateSwitch.cpp"> <ClCompile Include="ValidateSwitch.cpp">
<Filter>Source Files</Filter> <Filter>Source Files</Filter>
</ClCompile> </ClCompile>
<ClCompile Include="ValidateGlobalInitializer.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClInclude Include="BaseTypes.h"> <ClInclude Include="BaseTypes.h">
...@@ -178,9 +175,6 @@ ...@@ -178,9 +175,6 @@
<ClInclude Include="ValidateSwitch.h"> <ClInclude Include="ValidateSwitch.h">
<Filter>Header Files</Filter> <Filter>Header Files</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="ValidateGlobalInitializer.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<CustomBuild Include="glslang.l"> <CustomBuild Include="glslang.l">
......
...@@ -19,7 +19,6 @@ ...@@ -19,7 +19,6 @@
#include "glslang.h" #include "glslang.h"
#include "preprocessor/SourceLocation.h" #include "preprocessor/SourceLocation.h"
#include "ValidateGlobalInitializer.h"
#include "ValidateSwitch.h" #include "ValidateSwitch.h"
/////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////
...@@ -1300,19 +1299,11 @@ bool TParseContext::executeInitializer(const TSourceLoc& line, const TString& id ...@@ -1300,19 +1299,11 @@ bool TParseContext::executeInitializer(const TSourceLoc& line, const TString& id
return true; return true;
} }
bool globalInitWarning = false; if(symbolTable.atGlobalLevel() && initializer->getQualifier() != EvqConstExpr)
if(symbolTable.atGlobalLevel() && !ValidateGlobalInitializer(initializer, this, &globalInitWarning))
{ {
// 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", "="); error(line, "global variable initializers must be constant expressions", "=");
return true; 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 // identifier must be of type constant, a global, or a temporary
......
// 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();
}
// 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_
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