Commit 08d4aa93 by Olli Etuaho Committed by Commit Bot

Refactor constant folding tests

The constant folding test classes are moved into a separate file in test_utils. This will enable adding multiple test files that use constant folding test classes, so that constant folding tests can be organized better. TEST=angle_unittests BUG=chromium:661857 Change-Id: I00bf25a4b941bdc1364ff5aa9bee2d571e4b0ea0 Reviewed-on: https://chromium-review.googlesource.com/414910Reviewed-by: 's avatarCorentin Wallez <cwallez@chromium.org> Reviewed-by: 's avatarJamie Madill <jmadill@chromium.org> Commit-Queue: Olli Etuaho <oetuaho@nvidia.com>
parent ced53ae2
...@@ -91,6 +91,8 @@ ...@@ -91,6 +91,8 @@
'<(angle_path)/src/tests/preprocessor_tests/version_test.cpp', '<(angle_path)/src/tests/preprocessor_tests/version_test.cpp',
'<(angle_path)/src/tests/test_utils/compiler_test.cpp', '<(angle_path)/src/tests/test_utils/compiler_test.cpp',
'<(angle_path)/src/tests/test_utils/compiler_test.h', '<(angle_path)/src/tests/test_utils/compiler_test.h',
'<(angle_path)/src/tests/test_utils/ConstantFoldingTest.h',
'<(angle_path)/src/tests/test_utils/ConstantFoldingTest.cpp',
], ],
# TODO(jmadill): should probably call this windows sources # TODO(jmadill): should probably call this windows sources
'angle_unittests_hlsl_sources': 'angle_unittests_hlsl_sources':
......
...@@ -7,214 +7,10 @@ ...@@ -7,214 +7,10 @@
// Tests for constant folding // Tests for constant folding
// //
#include <vector> #include "tests/test_utils/ConstantFoldingTest.h"
#include "angle_gl.h"
#include "gtest/gtest.h"
#include "GLSLANG/ShaderLang.h"
#include "common/mathutil.h"
#include "compiler/translator/PoolAlloc.h"
#include "compiler/translator/TranslatorESSL.h"
using namespace sh; using namespace sh;
template <typename T>
class ConstantFinder : public TIntermTraverser
{
public:
ConstantFinder(const std::vector<T> &constantVector)
: TIntermTraverser(true, false, false),
mConstantVector(constantVector),
mFaultTolerance(T()),
mFound(false)
{}
ConstantFinder(const std::vector<T> &constantVector, const T &faultTolerance)
: TIntermTraverser(true, false, false),
mConstantVector(constantVector),
mFaultTolerance(faultTolerance),
mFound(false)
{}
ConstantFinder(const T &value)
: TIntermTraverser(true, false, false),
mFaultTolerance(T()),
mFound(false)
{
mConstantVector.push_back(value);
}
void visitConstantUnion(TIntermConstantUnion *node)
{
if (node->getType().getObjectSize() == mConstantVector.size())
{
bool found = true;
for (size_t i = 0; i < mConstantVector.size(); i++)
{
if (!isEqual(node->getUnionArrayPointer()[i], mConstantVector[i]))
{
found = false;
break;
}
}
if (found)
{
mFound = found;
}
}
}
bool found() const { return mFound; }
private:
bool isEqual(const TConstantUnion &node, const float &value) const
{
if (node.getType() != EbtFloat)
{
return false;
}
if (value == std::numeric_limits<float>::infinity())
{
return gl::isInf(node.getFConst()) && node.getFConst() > 0;
}
else if (value == -std::numeric_limits<float>::infinity())
{
return gl::isInf(node.getFConst()) && node.getFConst() < 0;
}
return mFaultTolerance >= fabsf(node.getFConst() - value);
}
bool isEqual(const TConstantUnion &node, const int &value) const
{
if (node.getType() != EbtInt)
{
return false;
}
ASSERT(mFaultTolerance < std::numeric_limits<int>::max());
// abs() returns 0 at least on some platforms when the minimum int value is passed in (it
// doesn't have a positive counterpart).
return mFaultTolerance >= abs(node.getIConst() - value) &&
(node.getIConst() - value) != std::numeric_limits<int>::min();
}
bool isEqual(const TConstantUnion &node, const unsigned int &value) const
{
if (node.getType() != EbtUInt)
{
return false;
}
ASSERT(mFaultTolerance < static_cast<unsigned int>(std::numeric_limits<int>::max()));
return static_cast<int>(mFaultTolerance) >=
abs(static_cast<int>(node.getUConst() - value)) &&
static_cast<int>(node.getUConst() - value) != std::numeric_limits<int>::min();
}
bool isEqual(const TConstantUnion &node, const bool &value) const
{
if (node.getType() != EbtBool)
{
return false;
}
return node.getBConst() == value;
}
std::vector<T> mConstantVector;
T mFaultTolerance;
bool mFound;
};
class ConstantFoldingTest : public testing::Test
{
public:
ConstantFoldingTest() {}
protected:
virtual void SetUp()
{
allocator.push();
SetGlobalPoolAllocator(&allocator);
ShBuiltInResources resources;
InitBuiltInResources(&resources);
mTranslatorESSL = new TranslatorESSL(GL_FRAGMENT_SHADER, SH_GLES3_SPEC);
ASSERT_TRUE(mTranslatorESSL->Init(resources));
}
virtual void TearDown()
{
delete mTranslatorESSL;
SetGlobalPoolAllocator(NULL);
allocator.pop();
}
void compile(const std::string& shaderString)
{
const char *shaderStrings[] = { shaderString.c_str() };
mASTRoot = mTranslatorESSL->compileTreeForTesting(shaderStrings, 1, SH_OBJECT_CODE);
if (!mASTRoot)
{
TInfoSink &infoSink = mTranslatorESSL->getInfoSink();
FAIL() << "Shader compilation into ESSL failed " << infoSink.info.c_str();
}
}
template <typename T>
bool constantFoundInAST(T constant)
{
ConstantFinder<T> finder(constant);
mASTRoot->traverse(&finder);
return finder.found();
}
template <typename T>
bool constantVectorFoundInAST(const std::vector<T> &constantVector)
{
ConstantFinder<T> finder(constantVector);
mASTRoot->traverse(&finder);
return finder.found();
}
template <typename T>
bool constantColumnMajorMatrixFoundInAST(const std::vector<T> &constantMatrix)
{
return constantVectorFoundInAST(constantMatrix);
}
template <typename T>
bool constantVectorNearFoundInAST(const std::vector<T> &constantVector, const T &faultTolerance)
{
ConstantFinder<T> finder(constantVector, faultTolerance);
mASTRoot->traverse(&finder);
return finder.found();
}
private:
TranslatorESSL *mTranslatorESSL;
TIntermNode *mASTRoot;
TPoolAllocator allocator;
};
class ConstantFoldingExpressionTest : public ConstantFoldingTest
{
public:
ConstantFoldingExpressionTest() {}
void evaluateFloat(const std::string &floatExpression)
{
std::stringstream shaderStream;
shaderStream << "#version 300 es\n"
"precision mediump float;\n"
"out float my_FragColor;\n"
"void main()\n"
"{\n"
<< " my_FragColor = " << floatExpression << ";\n"
<< "}\n";
compile(shaderStream.str());
}
};
// Test that zero, true or false are not found in AST when they are not expected. This is to make // Test that zero, true or false are not found in AST when they are not expected. This is to make
// sure that the subsequent tests run correctly. // sure that the subsequent tests run correctly.
TEST_F(ConstantFoldingExpressionTest, FoldFloatTestSanityCheck) TEST_F(ConstantFoldingExpressionTest, FoldFloatTestSanityCheck)
......
//
// Copyright (c) 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.
//
// ConstantFoldingTest.cpp:
// Utilities for constant folding tests.
//
#include "tests/test_utils/ConstantFoldingTest.h"
#include "angle_gl.h"
#include "compiler/translator/TranslatorESSL.h"
#include "GLSLANG/ShaderLang.h"
using namespace sh;
void ConstantFoldingTest::SetUp()
{
allocator.push();
SetGlobalPoolAllocator(&allocator);
ShBuiltInResources resources;
InitBuiltInResources(&resources);
mTranslatorESSL = new TranslatorESSL(GL_FRAGMENT_SHADER, SH_GLES3_SPEC);
ASSERT_TRUE(mTranslatorESSL->Init(resources));
}
void ConstantFoldingTest::TearDown()
{
delete mTranslatorESSL;
SetGlobalPoolAllocator(NULL);
allocator.pop();
}
void ConstantFoldingTest::compile(const std::string &shaderString)
{
const char *shaderStrings[] = {shaderString.c_str()};
mASTRoot = mTranslatorESSL->compileTreeForTesting(shaderStrings, 1, SH_OBJECT_CODE);
if (!mASTRoot)
{
TInfoSink &infoSink = mTranslatorESSL->getInfoSink();
FAIL() << "Shader compilation into ESSL failed " << infoSink.info.c_str();
}
}
void ConstantFoldingExpressionTest::evaluateFloat(const std::string &floatExpression)
{
std::stringstream shaderStream;
shaderStream << "#version 300 es\n"
"precision mediump float;\n"
"out float my_FragColor;\n"
"void main()\n"
"{\n"
<< " my_FragColor = " << floatExpression << ";\n"
<< "}\n";
compile(shaderStream.str());
}
//
// Copyright (c) 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.
//
// ConstantFoldingTest.h:
// Utilities for constant folding tests.
//
#ifndef TESTS_TEST_UTILS_CONSTANTFOLDINGTEST_H_
#define TESTS_TEST_UTILS_CONSTANTFOLDINGTEST_H_
#include <vector>
#include "common/mathutil.h"
#include "compiler/translator/IntermNode.h"
#include "compiler/translator/PoolAlloc.h"
#include "gtest/gtest.h"
namespace sh
{
class TranslatorESSL;
template <typename T>
class ConstantFinder : public TIntermTraverser
{
public:
ConstantFinder(const std::vector<T> &constantVector)
: TIntermTraverser(true, false, false),
mConstantVector(constantVector),
mFaultTolerance(T()),
mFound(false)
{
}
ConstantFinder(const std::vector<T> &constantVector, const T &faultTolerance)
: TIntermTraverser(true, false, false),
mConstantVector(constantVector),
mFaultTolerance(faultTolerance),
mFound(false)
{
}
ConstantFinder(const T &value)
: TIntermTraverser(true, false, false), mFaultTolerance(T()), mFound(false)
{
mConstantVector.push_back(value);
}
void visitConstantUnion(TIntermConstantUnion *node)
{
if (node->getType().getObjectSize() == mConstantVector.size())
{
bool found = true;
for (size_t i = 0; i < mConstantVector.size(); i++)
{
if (!isEqual(node->getUnionArrayPointer()[i], mConstantVector[i]))
{
found = false;
break;
}
}
if (found)
{
mFound = found;
}
}
}
bool found() const { return mFound; }
private:
bool isEqual(const TConstantUnion &node, const float &value) const
{
if (node.getType() != EbtFloat)
{
return false;
}
if (value == std::numeric_limits<float>::infinity())
{
return gl::isInf(node.getFConst()) && node.getFConst() > 0;
}
else if (value == -std::numeric_limits<float>::infinity())
{
return gl::isInf(node.getFConst()) && node.getFConst() < 0;
}
return mFaultTolerance >= fabsf(node.getFConst() - value);
}
bool isEqual(const TConstantUnion &node, const int &value) const
{
if (node.getType() != EbtInt)
{
return false;
}
ASSERT(mFaultTolerance < std::numeric_limits<int>::max());
// abs() returns 0 at least on some platforms when the minimum int value is passed in (it
// doesn't have a positive counterpart).
return mFaultTolerance >= abs(node.getIConst() - value) &&
(node.getIConst() - value) != std::numeric_limits<int>::min();
}
bool isEqual(const TConstantUnion &node, const unsigned int &value) const
{
if (node.getType() != EbtUInt)
{
return false;
}
ASSERT(mFaultTolerance < static_cast<unsigned int>(std::numeric_limits<int>::max()));
return static_cast<int>(mFaultTolerance) >=
abs(static_cast<int>(node.getUConst() - value)) &&
static_cast<int>(node.getUConst() - value) != std::numeric_limits<int>::min();
}
bool isEqual(const TConstantUnion &node, const bool &value) const
{
if (node.getType() != EbtBool)
{
return false;
}
return node.getBConst() == value;
}
std::vector<T> mConstantVector;
T mFaultTolerance;
bool mFound;
};
class ConstantFoldingTest : public testing::Test
{
public:
ConstantFoldingTest() {}
protected:
void SetUp() override;
void TearDown() override;
void compile(const std::string &shaderString);
template <typename T>
bool constantFoundInAST(T constant)
{
ConstantFinder<T> finder(constant);
mASTRoot->traverse(&finder);
return finder.found();
}
template <typename T>
bool constantVectorFoundInAST(const std::vector<T> &constantVector)
{
ConstantFinder<T> finder(constantVector);
mASTRoot->traverse(&finder);
return finder.found();
}
template <typename T>
bool constantColumnMajorMatrixFoundInAST(const std::vector<T> &constantMatrix)
{
return constantVectorFoundInAST(constantMatrix);
}
template <typename T>
bool constantVectorNearFoundInAST(const std::vector<T> &constantVector, const T &faultTolerance)
{
ConstantFinder<T> finder(constantVector, faultTolerance);
mASTRoot->traverse(&finder);
return finder.found();
}
private:
TranslatorESSL *mTranslatorESSL;
TIntermNode *mASTRoot;
TPoolAllocator allocator;
};
class ConstantFoldingExpressionTest : public ConstantFoldingTest
{
public:
ConstantFoldingExpressionTest() {}
void evaluateFloat(const std::string &floatExpression);
};
} // namespace sh
#endif // TESTS_TEST_UTILS_CONSTANTFOLDINGTEST_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