Hash :
02ae054c
Author :
Date :
2025-04-07T13:48:29
Translator: Fix init of inactive output variables Bug: chromium:398401939 Change-Id: I0df494b945b8d0e805a62cf7645d06bf233f36ca Reviewed-on: https://chromium-review.googlesource.com/c/angle/angle/+/6438495 Commit-Queue: Shahbaz Youssefi <syoussefi@chromium.org> Reviewed-by: Geoff Lang <geofflang@chromium.org>
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 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492
//
// Copyright 2017 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.
//
// InitOutputVariables_test.cpp: Tests correctness of the AST pass enabled through
// SH_INIT_OUTPUT_VARIABLES.
//
#include "common/angleutils.h"
#include "compiler/translator/SymbolTable.h"
#include "compiler/translator/tree_util/FindMain.h"
#include "compiler/translator/tree_util/IntermNode_util.h"
#include "compiler/translator/tree_util/IntermTraverse.h"
#include "tests/test_utils/ShaderCompileTreeTest.h"
#include <algorithm>
namespace sh
{
namespace
{
typedef std::vector<TIntermTyped *> ExpectedLValues;
bool AreSymbolsTheSame(const TIntermSymbol *expected, const TIntermSymbol *candidate)
{
if (expected == nullptr || candidate == nullptr)
{
return false;
}
const TType &expectedType = expected->getType();
const TType &candidateType = candidate->getType();
const bool sameTypes = expectedType == candidateType &&
expectedType.getPrecision() == candidateType.getPrecision() &&
expectedType.getQualifier() == candidateType.getQualifier();
const bool sameSymbols = (expected->variable().symbolType() == SymbolType::Empty &&
candidate->variable().symbolType() == SymbolType::Empty) ||
expected->getName() == candidate->getName();
return sameSymbols && sameTypes;
}
bool AreLValuesTheSame(TIntermTyped *expected, TIntermTyped *candidate)
{
const TIntermBinary *expectedBinary = expected->getAsBinaryNode();
if (expectedBinary)
{
ASSERT(expectedBinary->getOp() == EOpIndexDirect);
const TIntermBinary *candidateBinary = candidate->getAsBinaryNode();
if (candidateBinary == nullptr || candidateBinary->getOp() != EOpIndexDirect)
{
return false;
}
if (expectedBinary->getRight()->getAsConstantUnion()->getIConst(0) !=
candidateBinary->getRight()->getAsConstantUnion()->getIConst(0))
{
return false;
}
return AreSymbolsTheSame(expectedBinary->getLeft()->getAsSymbolNode(),
candidateBinary->getLeft()->getAsSymbolNode());
}
return AreSymbolsTheSame(expected->getAsSymbolNode(), candidate->getAsSymbolNode());
}
TIntermTyped *CreateLValueNode(const ImmutableString &lValueName, const TType &type)
{
// We're using a mock symbol table here, don't need to assign proper symbol ids to these nodes.
TSymbolTable symbolTable;
TVariable *variable =
new TVariable(&symbolTable, lValueName, new TType(type), SymbolType::UserDefined);
return new TIntermSymbol(variable);
}
ExpectedLValues CreateIndexedLValueNodeList(const ImmutableString &lValueName,
const TType &elementType,
unsigned arraySize)
{
ASSERT(elementType.isArray() == false);
TType *arrayType = new TType(elementType);
arrayType->makeArray(arraySize);
// We're using a mock symbol table here, don't need to assign proper symbol ids to these nodes.
TSymbolTable symbolTable;
TVariable *variable =
new TVariable(&symbolTable, lValueName, arrayType, SymbolType::UserDefined);
TIntermSymbol *arraySymbol = new TIntermSymbol(variable);
ExpectedLValues expected(arraySize);
for (unsigned index = 0u; index < arraySize; ++index)
{
expected[index] = new TIntermBinary(EOpIndexDirect, arraySymbol->deepCopy(),
CreateIndexNode(static_cast<int>(index)));
}
return expected;
}
// VerifyOutputVariableInitializers traverses the subtree covering main and collects the lvalues in
// assignments for which the rvalue is an expression containing only zero constants.
class VerifyOutputVariableInitializers final : public TIntermTraverser
{
public:
VerifyOutputVariableInitializers(TIntermBlock *root) : TIntermTraverser(true, false, false)
{
ASSERT(root != nullptr);
// The traversal starts in the body of main because this is where the varyings and output
// variables are initialized.
sh::TIntermFunctionDefinition *main = FindMain(root);
ASSERT(main != nullptr);
main->traverse(this);
}
bool visitBinary(Visit visit, TIntermBinary *node) override
{
if (node->getOp() == EOpAssign && IsZero(node->getRight()))
{
mCandidateLValues.push_back(node->getLeft());
return false;
}
return true;
}
// The collected lvalues are considered valid if every expected lvalue in expectedLValues is
// matched by name and type with any lvalue in mCandidateLValues.
bool areAllExpectedLValuesFound(const ExpectedLValues &expectedLValues) const
{
for (size_t i = 0u; i < expectedLValues.size(); ++i)
{
if (!isExpectedLValueFound(expectedLValues[i]))
{
return false;
}
}
return true;
}
bool isExpectedLValueFound(TIntermTyped *expectedLValue) const
{
bool isFound = false;
for (size_t j = 0; j < mCandidateLValues.size() && !isFound; ++j)
{
isFound = AreLValuesTheSame(expectedLValue, mCandidateLValues[j]);
}
return isFound;
}
const ExpectedLValues &getCandidates() const { return mCandidateLValues; }
private:
ExpectedLValues mCandidateLValues;
};
// Traverses the AST and records a pointer to a structure with a given name.
class FindStructByName final : public TIntermTraverser
{
public:
FindStructByName(const ImmutableString &structName)
: TIntermTraverser(true, false, false), mStructName(structName), mStructure(nullptr)
{}
void visitSymbol(TIntermSymbol *symbol) override
{
if (isStructureFound())
{
return;
}
const TStructure *structure = symbol->getType().getStruct();
if (structure != nullptr && structure->symbolType() != SymbolType::Empty &&
structure->name() == mStructName)
{
mStructure = structure;
}
}
bool isStructureFound() const { return mStructure != nullptr; }
const TStructure *getStructure() const { return mStructure; }
private:
ImmutableString mStructName;
const TStructure *mStructure;
};
} // namespace
class InitOutputVariablesWebGL2Test : public ShaderCompileTreeTest
{
public:
void SetUp() override
{
mCompileOptions.initOutputVariables = true;
if (getShaderType() == GL_VERTEX_SHADER)
{
mCompileOptions.initGLPosition = true;
}
ShaderCompileTreeTest::SetUp();
}
protected:
ShShaderSpec getShaderSpec() const override { return SH_WEBGL2_SPEC; }
};
class InitOutputVariablesWebGL2VertexShaderTest : public InitOutputVariablesWebGL2Test
{
protected:
::GLenum getShaderType() const override { return GL_VERTEX_SHADER; }
};
class InitOutputVariablesWebGL2FragmentShaderTest : public InitOutputVariablesWebGL2Test
{
protected:
::GLenum getShaderType() const override { return GL_FRAGMENT_SHADER; }
void initResources(ShBuiltInResources *resources) override
{
resources->EXT_draw_buffers = 1;
resources->MaxDrawBuffers = 2;
}
};
class InitOutputVariablesWebGL1FragmentShaderTest : public ShaderCompileTreeTest
{
public:
InitOutputVariablesWebGL1FragmentShaderTest() { mCompileOptions.initOutputVariables = true; }
protected:
::GLenum getShaderType() const override { return GL_FRAGMENT_SHADER; }
ShShaderSpec getShaderSpec() const override { return SH_WEBGL_SPEC; }
void initResources(ShBuiltInResources *resources) override
{
resources->EXT_draw_buffers = 1;
resources->MaxDrawBuffers = 2;
}
};
class InitOutputVariablesVertexShaderClipDistanceTest : public ShaderCompileTreeTest
{
public:
InitOutputVariablesVertexShaderClipDistanceTest()
{
mCompileOptions.initOutputVariables = true;
mCompileOptions.validateAST = true;
}
protected:
::GLenum getShaderType() const override { return GL_VERTEX_SHADER; }
ShShaderSpec getShaderSpec() const override { return SH_GLES2_SPEC; }
void initResources(ShBuiltInResources *resources) override
{
resources->APPLE_clip_distance = 1;
resources->MaxClipDistances = 8;
}
};
// Test the initialization of output variables with various qualifiers in a vertex shader.
TEST_F(InitOutputVariablesWebGL2VertexShaderTest, OutputAllQualifiers)
{
const std::string &shaderString =
"#version 300 es\n"
"precision mediump float;\n"
"precision lowp int;\n"
"out vec4 out1;\n"
"flat out int out2;\n"
"centroid out float out3;\n"
"smooth out float out4;\n"
"void main() {\n"
" out1.x += 0.0001;\n"
" out2 += 1;\n"
" out3 += 0.0001;\n"
" out4 += 0.0001;\n"
"}\n";
compileAssumeSuccess(shaderString);
VerifyOutputVariableInitializers verifier(mASTRoot);
ExpectedLValues expectedLValues = {
CreateLValueNode(ImmutableString("out1"), TType(EbtFloat, EbpMedium, EvqVertexOut, 4)),
CreateLValueNode(ImmutableString("out2"), TType(EbtInt, EbpLow, EvqFlatOut)),
CreateLValueNode(ImmutableString("out3"), TType(EbtFloat, EbpMedium, EvqCentroidOut)),
CreateLValueNode(ImmutableString("out4"), TType(EbtFloat, EbpMedium, EvqSmoothOut))};
EXPECT_TRUE(verifier.areAllExpectedLValuesFound(expectedLValues));
}
// Test the initialization of an output array in a vertex shader.
TEST_F(InitOutputVariablesWebGL2VertexShaderTest, OutputArray)
{
const std::string &shaderString =
"#version 300 es\n"
"precision mediump float;\n"
"out float out1[2];\n"
"void main() {\n"
" out1[0] += 0.0001;\n"
"}\n";
compileAssumeSuccess(shaderString);
VerifyOutputVariableInitializers verifier(mASTRoot);
ExpectedLValues expectedLValues = CreateIndexedLValueNodeList(
ImmutableString("out1"), TType(EbtFloat, EbpMedium, EvqVertexOut), 2);
EXPECT_TRUE(verifier.areAllExpectedLValuesFound(expectedLValues));
}
// Test the initialization of a struct output variable in a vertex shader.
TEST_F(InitOutputVariablesWebGL2VertexShaderTest, OutputStruct)
{
const std::string &shaderString =
"#version 300 es\n"
"precision mediump float;\n"
"struct MyS{\n"
" float a;\n"
" float b;\n"
"};\n"
"out MyS out1;\n"
"void main() {\n"
" out1.a += 0.0001;\n"
"}\n";
compileAssumeSuccess(shaderString);
VerifyOutputVariableInitializers verifier(mASTRoot);
FindStructByName findStruct(ImmutableString("MyS"));
mASTRoot->traverse(&findStruct);
ASSERT(findStruct.isStructureFound());
TType type(findStruct.getStructure(), false);
type.setQualifier(EvqVertexOut);
TIntermTyped *expectedLValue = CreateLValueNode(ImmutableString("out1"), type);
EXPECT_TRUE(verifier.isExpectedLValueFound(expectedLValue));
delete expectedLValue;
}
// Test the initialization of a varying variable in an ESSL1 vertex shader.
TEST_F(InitOutputVariablesWebGL2VertexShaderTest, OutputFromESSL1Shader)
{
const std::string &shaderString =
"precision mediump float;\n"
"varying vec4 out1;\n"
"void main() {\n"
" out1.x += 0.0001;\n"
"}\n";
compileAssumeSuccess(shaderString);
VerifyOutputVariableInitializers verifier(mASTRoot);
TIntermTyped *expectedLValue =
CreateLValueNode(ImmutableString("out1"), TType(EbtFloat, EbpMedium, EvqVaryingOut, 4));
EXPECT_TRUE(verifier.isExpectedLValueFound(expectedLValue));
delete expectedLValue;
}
// Test the initialization of output variables in a fragment shader.
TEST_F(InitOutputVariablesWebGL2FragmentShaderTest, Output)
{
const std::string &shaderString =
"#version 300 es\n"
"precision mediump float;\n"
"out vec4 out1;\n"
"void main() {\n"
" out1.x += 0.0001;\n"
"}\n";
compileAssumeSuccess(shaderString);
VerifyOutputVariableInitializers verifier(mASTRoot);
TIntermTyped *expectedLValue =
CreateLValueNode(ImmutableString("out1"), TType(EbtFloat, EbpMedium, EvqFragmentOut, 4));
EXPECT_TRUE(verifier.isExpectedLValueFound(expectedLValue));
delete expectedLValue;
}
// Test the initialization of gl_FragData in a WebGL2 ESSL1 fragment shader. Only writes to
// gl_FragData[0] should be found.
TEST_F(InitOutputVariablesWebGL2FragmentShaderTest, FragData)
{
const std::string &shaderString =
"precision mediump float;\n"
"void main() {\n"
" gl_FragData[0] = vec4(1.);\n"
"}\n";
compileAssumeSuccess(shaderString);
VerifyOutputVariableInitializers verifier(mASTRoot);
ExpectedLValues expectedLValues = CreateIndexedLValueNodeList(
ImmutableString("gl_FragData"), TType(EbtFloat, EbpMedium, EvqFragData, 4), 1);
EXPECT_TRUE(verifier.isExpectedLValueFound(expectedLValues[0]));
EXPECT_EQ(1u, verifier.getCandidates().size());
}
// Test the initialization of gl_FragData in a WebGL1 ESSL1 fragment shader. Only writes to
// gl_FragData[0] should be found.
TEST_F(InitOutputVariablesWebGL1FragmentShaderTest, FragData)
{
const std::string &shaderString =
"precision mediump float;\n"
"void main() {\n"
" gl_FragData[0] = vec4(1.);\n"
"}\n";
compileAssumeSuccess(shaderString);
VerifyOutputVariableInitializers verifier(mASTRoot);
// In the symbol table, gl_FragData array has 2 elements. However, only the 1st one should be
// initialized.
ExpectedLValues expectedLValues = CreateIndexedLValueNodeList(
ImmutableString("gl_FragData"), TType(EbtFloat, EbpMedium, EvqFragData, 4), 2);
EXPECT_TRUE(verifier.isExpectedLValueFound(expectedLValues[0]));
EXPECT_EQ(1u, verifier.getCandidates().size());
}
// Test the initialization of gl_FragData in a WebGL1 ESSL1 fragment shader with GL_EXT_draw_buffers
// enabled. All attachment slots should be initialized.
TEST_F(InitOutputVariablesWebGL1FragmentShaderTest, FragDataWithDrawBuffersExtEnabled)
{
const std::string &shaderString =
"#extension GL_EXT_draw_buffers : enable\n"
"precision mediump float;\n"
"void main() {\n"
" gl_FragData[0] = vec4(1.);\n"
"}\n";
compileAssumeSuccess(shaderString);
VerifyOutputVariableInitializers verifier(mASTRoot);
ExpectedLValues expectedLValues = CreateIndexedLValueNodeList(
ImmutableString("gl_FragData"), TType(EbtFloat, EbpMedium, EvqFragData, 4), 2);
EXPECT_TRUE(verifier.isExpectedLValueFound(expectedLValues[0]));
EXPECT_TRUE(verifier.isExpectedLValueFound(expectedLValues[1]));
EXPECT_EQ(2u, verifier.getCandidates().size());
}
// Test that gl_Position is initialized once in case it is not statically used and both
// SH_INIT_OUTPUT_VARIABLES and SH_INIT_GL_POSITION flags are set.
TEST_F(InitOutputVariablesWebGL2VertexShaderTest, InitGLPositionWhenNotStaticallyUsed)
{
const std::string &shaderString =
"#version 300 es\n"
"precision highp float;\n"
"void main() {\n"
"}\n";
compileAssumeSuccess(shaderString);
VerifyOutputVariableInitializers verifier(mASTRoot);
TIntermTyped *glPosition =
CreateLValueNode(ImmutableString("gl_Position"), TType(EbtFloat, EbpHigh, EvqPosition, 4));
EXPECT_TRUE(verifier.isExpectedLValueFound(glPosition));
EXPECT_EQ(1u, verifier.getCandidates().size());
}
// Test that gl_Position is initialized once in case it is statically used and both
// SH_INIT_OUTPUT_VARIABLES and SH_INIT_GL_POSITION flags are set.
TEST_F(InitOutputVariablesWebGL2VertexShaderTest, InitGLPositionOnceWhenStaticallyUsed)
{
const std::string &shaderString =
"#version 300 es\n"
"precision highp float;\n"
"void main() {\n"
" gl_Position = vec4(1.0);\n"
"}\n";
compileAssumeSuccess(shaderString);
VerifyOutputVariableInitializers verifier(mASTRoot);
TIntermTyped *glPosition =
CreateLValueNode(ImmutableString("gl_Position"), TType(EbtFloat, EbpHigh, EvqPosition, 4));
EXPECT_TRUE(verifier.isExpectedLValueFound(glPosition));
EXPECT_EQ(1u, verifier.getCandidates().size());
}
// Mirrors ClipDistanceTest.ThreeClipDistancesRedeclared
TEST_F(InitOutputVariablesVertexShaderClipDistanceTest, RedeclareClipDistance)
{
constexpr char shaderString[] = R"(
#extension GL_APPLE_clip_distance : require
varying highp float gl_ClipDistance[3];
void computeClipDistances(in vec4 position, in vec4 plane[3])
{
gl_ClipDistance[0] = dot(position, plane[0]);
gl_ClipDistance[1] = dot(position, plane[1]);
gl_ClipDistance[2] = dot(position, plane[2]);
}
uniform vec4 u_plane[3];
attribute vec2 a_position;
void main()
{
gl_Position = vec4(a_position, 0.0, 1.0);
computeClipDistances(gl_Position, u_plane);
})";
compileAssumeSuccess(shaderString);
}
} // namespace sh