Hash :
1ecd14b8
Author :
Date :
2017-01-26T13:54:15
Fold user-definedness of function nodes into TOperator Whether a function call is user-defined is not orthogonal to TOperator associated with the call node - other ops than function calls can't be user-defined. Because of this it makes sense to store the user- definedness by having different TOperator enums for different types of calls. This patch also tags internal helper functions that have a raw definition outside the AST with a separate TOperator enum. This way they can be handled with logic that is easy to understand. Before this, function calls like this left the user-defined bit unset, despite not really being built-ins either. The EmulatePrecision traverser uses this. This is also something that could be used to clean up built-in emulation in the future. BUG=angleproject:1490 TEST=angle_unittests Change-Id: I597fcd9789d0cc22b689ef3ce5a0cc3f621d4859 Reviewed-on: https://chromium-review.googlesource.com/433443 Reviewed-by: Corentin Wallez <cwallez@chromium.org> Commit-Queue: Olli Etuaho <oetuaho@nvidia.com>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413
//
// Copyright (c) 2002-2016 The ANGLE Project Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
//
// ValidateMultiviewWebGL.cpp:
// Validate the AST according to rules in the WEBGL_multiview extension spec. Only covers those
// rules not already covered in ParseContext.
//
#include "compiler/translator/ValidateMultiviewWebGL.h"
#include <array>
#include "angle_gl.h"
#include "compiler/translator/Diagnostics.h"
#include "compiler/translator/FindSymbolNode.h"
#include "compiler/translator/IntermNode.h"
#include "compiler/translator/SymbolTable.h"
namespace sh
{
namespace
{
class ValidateMultiviewTraverser : public TIntermTraverser
{
public:
// Check for errors and write error messages to diagnostics. Returns true if there are no
// errors.
static bool validate(TIntermBlock *root,
GLenum shaderType,
const TSymbolTable &symbolTable,
int shaderVersion,
bool multiview2,
TDiagnostics *diagnostics);
bool isValid() { return mValid; }
protected:
void visitSymbol(TIntermSymbol *node) override;
bool visitBinary(Visit visit, TIntermBinary *node) override;
bool visitUnary(Visit visit, TIntermUnary *node) override;
bool visitIfElse(Visit visit, TIntermIfElse *node) override;
bool visitAggregate(Visit visit, TIntermAggregate *node) override;
private:
ValidateMultiviewTraverser(GLenum shaderType,
const TSymbolTable &symbolTable,
int shaderVersion,
bool multiview2,
TDiagnostics *diagnostics);
static bool IsGLPosition(TIntermNode *node);
static bool IsGLViewIDOVR(TIntermNode *node);
static bool IsSimpleAssignmentToGLPositionX(TIntermBinary *node);
static bool IsSimpleAssignmentToGLPosition(TIntermBinary *node);
void validateAndTraverseViewIDConditionalBlock(TIntermBlock *block, const char *token);
bool mValid;
bool mMultiview2;
GLenum mShaderType;
const TSymbolTable &mSymbolTable;
const int mShaderVersion;
bool mInsideViewIDConditional; // Only set if mMultiview2 is false.
bool mInsideRestrictedAssignment;
bool mGLPositionAllowed;
bool mViewIDOVRAllowed;
TDiagnostics *mDiagnostics;
};
bool ValidateMultiviewTraverser::validate(TIntermBlock *root,
GLenum shaderType,
const TSymbolTable &symbolTable,
int shaderVersion,
bool multiview2,
TDiagnostics *diagnostics)
{
ValidateMultiviewTraverser validate(shaderType, symbolTable, shaderVersion, multiview2,
diagnostics);
ASSERT(root);
root->traverse(&validate);
return validate.isValid();
}
ValidateMultiviewTraverser::ValidateMultiviewTraverser(GLenum shaderType,
const TSymbolTable &symbolTable,
int shaderVersion,
bool multiview2,
TDiagnostics *diagnostics)
: TIntermTraverser(true, true, true),
mValid(true),
mMultiview2(multiview2),
mShaderType(shaderType),
mSymbolTable(symbolTable),
mShaderVersion(shaderVersion),
mInsideViewIDConditional(false),
mInsideRestrictedAssignment(false),
mGLPositionAllowed(multiview2),
mViewIDOVRAllowed(multiview2),
mDiagnostics(diagnostics)
{
}
bool ValidateMultiviewTraverser::IsGLPosition(TIntermNode *node)
{
TIntermSymbol *symbolNode = node->getAsSymbolNode();
return (symbolNode && symbolNode->getName().getString() == "gl_Position");
}
bool ValidateMultiviewTraverser::IsGLViewIDOVR(TIntermNode *node)
{
TIntermSymbol *symbolNode = node->getAsSymbolNode();
return (symbolNode && symbolNode->getName().getString() == "gl_ViewID_OVR");
}
bool ValidateMultiviewTraverser::IsSimpleAssignmentToGLPositionX(TIntermBinary *node)
{
if (node->getOp() == EOpAssign)
{
TIntermSwizzle *leftAsSwizzle = node->getLeft()->getAsSwizzleNode();
if (leftAsSwizzle && IsGLPosition(leftAsSwizzle->getOperand()) &&
leftAsSwizzle->offsetsMatch(0))
{
return true;
}
TIntermBinary *leftAsBinary = node->getLeft()->getAsBinaryNode();
if (leftAsBinary && leftAsBinary->getOp() == EOpIndexDirect &&
IsGLPosition(leftAsBinary->getLeft()) &&
leftAsBinary->getRight()->getAsConstantUnion()->getIConst(0) == 0)
{
return true;
}
}
return false;
}
bool ValidateMultiviewTraverser::IsSimpleAssignmentToGLPosition(TIntermBinary *node)
{
if (node->getOp() == EOpAssign)
{
if (IsGLPosition(node->getLeft()))
{
return true;
}
TIntermSwizzle *leftAsSwizzle = node->getLeft()->getAsSwizzleNode();
if (leftAsSwizzle && IsGLPosition(leftAsSwizzle->getOperand()))
{
return true;
}
TIntermBinary *leftAsBinary = node->getLeft()->getAsBinaryNode();
if (leftAsBinary && leftAsBinary->getOp() == EOpIndexDirect &&
IsGLPosition(leftAsBinary->getLeft()))
{
return true;
}
}
return false;
}
void ValidateMultiviewTraverser::visitSymbol(TIntermSymbol *node)
{
if (IsGLPosition(node) && !mGLPositionAllowed)
{
ASSERT(!mMultiview2);
mDiagnostics->error(node->getLine(),
"Disallowed use of gl_Position when using OVR_multiview",
"gl_Position");
mValid = false;
}
else if (IsGLViewIDOVR(node) && !mViewIDOVRAllowed)
{
ASSERT(!mMultiview2);
mDiagnostics->error(node->getLine(),
"Disallowed use of gl_ViewID_OVR when using OVR_multiview",
"gl_ViewID_OVR");
mValid = false;
}
else if (!mMultiview2 && mShaderType == GL_FRAGMENT_SHADER)
{
std::array<const char *, 3> disallowedFragmentShaderSymbols{
{"gl_FragCoord", "gl_PointCoord", "gl_FrontFacing"}};
for (auto disallowedSymbol : disallowedFragmentShaderSymbols)
{
if (node->getSymbol() == disallowedSymbol)
{
mDiagnostics->error(
node->getLine(),
"Disallowed use of a built-in variable when using OVR_multiview",
disallowedSymbol);
mValid = false;
}
}
}
}
bool ValidateMultiviewTraverser::visitBinary(Visit visit, TIntermBinary *node)
{
if (!mMultiview2 && mShaderType == GL_VERTEX_SHADER)
{
if (visit == PreVisit)
{
ASSERT(!mInsideViewIDConditional || mInsideRestrictedAssignment);
if (node->isAssignment())
{
if (mInsideRestrictedAssignment)
{
mDiagnostics->error(node->getLine(),
"Disallowed assignment inside assignment to gl_Position.x "
"when using OVR_multiview",
GetOperatorString(node->getOp()));
mValid = false;
}
else if (IsSimpleAssignmentToGLPositionX(node) &&
FindSymbolNode(node->getRight(), "gl_ViewID_OVR", EbtUInt))
{
if (!getParentNode()->getAsBlock())
{
mDiagnostics->error(node->getLine(),
"Disallowed use of assignment to gl_Position.x "
"when using OVR_multiview",
"=");
mValid = false;
}
mInsideRestrictedAssignment = true;
mViewIDOVRAllowed = true;
node->getRight()->traverse(this);
mInsideRestrictedAssignment = false;
mViewIDOVRAllowed = false;
return false;
}
else if (IsSimpleAssignmentToGLPosition(node))
{
node->getRight()->traverse(this);
return false;
}
}
}
}
return true;
}
bool ValidateMultiviewTraverser::visitUnary(Visit visit, TIntermUnary *node)
{
if (visit == PreVisit && !mMultiview2 && mInsideRestrictedAssignment)
{
if (node->isAssignment())
{
mDiagnostics->error(node->getLine(),
"Disallowed unary operator inside assignment to gl_Position.x when "
"using OVR_multiview",
GetOperatorString(node->getOp()));
mValid = false;
}
}
return true;
}
void ValidateMultiviewTraverser::validateAndTraverseViewIDConditionalBlock(TIntermBlock *block,
const char *token)
{
if (block->getSequence()->size() > 1)
{
mDiagnostics->error(block->getLine(),
"Only one assignment to gl_Position allowed inside if block dependent "
"on gl_ViewID_OVR when using OVR_multiview",
token);
mValid = false;
}
else if (block->getSequence()->size() == 1)
{
TIntermBinary *assignment = block->getSequence()->at(0)->getAsBinaryNode();
if (assignment && IsSimpleAssignmentToGLPositionX(assignment))
{
mInsideRestrictedAssignment = true;
assignment->getRight()->traverse(this);
mInsideRestrictedAssignment = false;
}
else
{
mDiagnostics->error(block->getLine(),
"Only one assignment to gl_Position.x allowed inside if block "
"dependent on gl_ViewID_OVR when using OVR_multiview",
token);
mValid = false;
}
}
}
bool ValidateMultiviewTraverser::visitIfElse(Visit visit, TIntermIfElse *node)
{
if (!mMultiview2 && mShaderType == GL_VERTEX_SHADER)
{
ASSERT(visit == PreVisit);
// Check if the if statement refers to gl_ViewID_OVR and if it does so correctly
TIntermBinary *binaryCondition = node->getCondition()->getAsBinaryNode();
bool conditionIsAllowedComparisonWithViewID = false;
if (binaryCondition && binaryCondition->getOp() == EOpEqual)
{
conditionIsAllowedComparisonWithViewID =
IsGLViewIDOVR(binaryCondition->getLeft()) &&
binaryCondition->getRight()->getAsConstantUnion() &&
binaryCondition->getRight()->getQualifier() == EvqConst;
if (!conditionIsAllowedComparisonWithViewID)
{
conditionIsAllowedComparisonWithViewID =
IsGLViewIDOVR(binaryCondition->getRight()) &&
binaryCondition->getLeft()->getAsConstantUnion() &&
binaryCondition->getLeft()->getQualifier() == EvqConst;
}
}
if (conditionIsAllowedComparisonWithViewID)
{
mInsideViewIDConditional = true;
if (node->getTrueBlock())
{
validateAndTraverseViewIDConditionalBlock(node->getTrueBlock(), "if");
}
else
{
mDiagnostics->error(node->getLine(), "Expected assignment to gl_Position.x", "if");
}
if (node->getFalseBlock())
{
validateAndTraverseViewIDConditionalBlock(node->getFalseBlock(), "else");
}
mInsideViewIDConditional = false;
}
else
{
node->getCondition()->traverse(this);
if (node->getTrueBlock())
{
node->getTrueBlock()->traverse(this);
}
if (node->getFalseBlock())
{
node->getFalseBlock()->traverse(this);
}
}
return false;
}
return true;
}
bool ValidateMultiviewTraverser::visitAggregate(Visit visit, TIntermAggregate *node)
{
if (visit == PreVisit && !mMultiview2 && mInsideRestrictedAssignment)
{
// Check if the node is an user-defined function call or if an l-value is required, or if
// there are possible visible side effects, such as image writes.
if (node->getOp() == EOpCallFunctionInAST)
{
mDiagnostics->error(node->getLine(),
"Disallowed user defined function call inside assignment to "
"gl_Position.x when using OVR_multiview",
GetOperatorString(node->getOp()));
mValid = false;
}
else if (node->getOp() == EOpCallBuiltInFunction &&
TFunction::unmangleName(node->getFunctionSymbolInfo()->getName()) == "imageStore")
{
// TODO(oetuaho@nvidia.com): Record which built-in functions have side effects in
// the symbol info instead.
mDiagnostics->error(node->getLine(),
"Disallowed function call with side effects inside assignment "
"to gl_Position.x when using OVR_multiview",
GetOperatorString(node->getOp()));
mValid = false;
}
else if (!node->isConstructor())
{
TFunction *builtInFunc = mSymbolTable.findBuiltInOp(node, mShaderVersion);
for (size_t paramIndex = 0u; paramIndex < builtInFunc->getParamCount(); ++paramIndex)
{
TQualifier qualifier = builtInFunc->getParam(paramIndex).type->getQualifier();
if (qualifier == EvqOut || qualifier == EvqInOut)
{
mDiagnostics->error(node->getLine(),
"Disallowed use of a function with an out parameter inside "
"assignment to gl_Position.x when using OVR_multiview",
GetOperatorString(node->getOp()));
mValid = false;
}
}
}
}
return true;
}
} // anonymous namespace
bool ValidateMultiviewWebGL(TIntermBlock *root,
GLenum shaderType,
const TSymbolTable &symbolTable,
int shaderVersion,
bool multiview2,
TDiagnostics *diagnostics)
{
if (shaderType == GL_VERTEX_SHADER && !FindSymbolNode(root, "gl_ViewID_OVR", EbtUInt))
{
return true;
}
return ValidateMultiviewTraverser::validate(root, shaderType, symbolTable, shaderVersion,
multiview2, diagnostics);
}
} // namespace sh