Commit b3da45c1 by Jamie Madill

Revert "Add constant folding support for geometric built-ins"

This is failing gpu_unittests on Mac: ShaderTranslatorTest.BuiltInFunctionEmulation: ../../gpu/command_buffer/service/shader_translator_unittest.cc:314: Failure Value of: strstr(translated_source.c_str(), "webgl_dot_emu") != NULL Actual: false Expected: true Reverting until we can fix the overly-narrow test. BUG=angleproject:913 This reverts commit b50788d1. Change-Id: I5bd9df83704e771e419339745eceaa43a20bd1e6 Reviewed-on: https://chromium-review.googlesource.com/275320Reviewed-by: 's avatarJamie Madill <jmadill@chromium.org> Tested-by: 's avatarJamie Madill <jmadill@chromium.org>
parent aebd002d
...@@ -168,25 +168,6 @@ void UndefinedConstantFoldingError(const TSourceLoc &loc, TOperator op, TBasicTy ...@@ -168,25 +168,6 @@ void UndefinedConstantFoldingError(const TSourceLoc &loc, TOperator op, TBasicTy
} }
} }
float VectorLength(TConstantUnion *paramArray, size_t paramArraySize)
{
float result = 0.0f;
for (size_t i = 0; i < paramArraySize; i++)
{
float f = paramArray[i].getFConst();
result += f * f;
}
return sqrtf(result);
}
float VectorDotProduct(TConstantUnion *paramArray1, TConstantUnion *paramArray2, size_t paramArraySize)
{
float result = 0.0f;
for (size_t i = 0; i < paramArraySize; i++)
result += paramArray1[i].getFConst() * paramArray2[i].getFConst();
return result;
}
} // namespace anonymous } // namespace anonymous
...@@ -1198,15 +1179,14 @@ TIntermTyped *TIntermConstantUnion::fold( ...@@ -1198,15 +1179,14 @@ TIntermTyped *TIntermConstantUnion::fold(
return tempNode; return tempNode;
} }
else if (op == EOpAny || op == EOpAll || op == EOpLength) else if (op == EOpAny || op == EOpAll)
{ {
// Do operations where the return type is different from the operand type. // Do operations where the return type is different from the operand type.
TType returnType; TType returnType(EbtBool, EbpUndefined, EvqConst);
TConstantUnion *tempConstArray = nullptr; TConstantUnion *tempConstArray = nullptr;
switch (op) if (op == EOpAny)
{ {
case EOpAny:
if (getType().getBasicType() == EbtBool) if (getType().getBasicType() == EbtBool)
{ {
tempConstArray = new TConstantUnion(); tempConstArray = new TConstantUnion();
...@@ -1219,16 +1199,17 @@ TIntermTyped *TIntermConstantUnion::fold( ...@@ -1219,16 +1199,17 @@ TIntermTyped *TIntermConstantUnion::fold(
break; break;
} }
} }
returnType = TType(EbtBool, EbpUndefined, EvqConst);
break;
} }
else else
{ {
infoSink.info.message(EPrefixInternalError, getLine(), "Unary operation not folded into constant"); infoSink.info.message(
EPrefixInternalError, getLine(),
"Unary operation not folded into constant");
return nullptr; return nullptr;
} }
}
case EOpAll: else if (op == EOpAll)
{
if (getType().getBasicType() == EbtBool) if (getType().getBasicType() == EbtBool)
{ {
tempConstArray = new TConstantUnion(); tempConstArray = new TConstantUnion();
...@@ -1241,33 +1222,15 @@ TIntermTyped *TIntermConstantUnion::fold( ...@@ -1241,33 +1222,15 @@ TIntermTyped *TIntermConstantUnion::fold(
break; break;
} }
} }
returnType = TType(EbtBool, EbpUndefined, EvqConst);
break;
} }
else else
{ {
infoSink.info.message(EPrefixInternalError, getLine(), "Unary operation not folded into constant"); infoSink.info.message(
return nullptr; EPrefixInternalError, getLine(),
} "Unary operation not folded into constant");
case EOpLength:
if (getType().getBasicType() == EbtFloat)
{
tempConstArray = new TConstantUnion();
tempConstArray->setFConst(VectorLength(unionArray, objectSize));
returnType = TType(EbtFloat, getType().getPrecision(), EvqConst);
break;
}
else
{
infoSink.info.message(EPrefixInternalError, getLine(), "Unary operation not folded into constant");
return nullptr; return nullptr;
} }
default:
break;
} }
TIntermConstantUnion *tempNode = new TIntermConstantUnion(tempConstArray, returnType); TIntermConstantUnion *tempNode = new TIntermConstantUnion(tempConstArray, returnType);
tempNode->setLine(getLine()); tempNode->setLine(getLine());
return tempNode; return tempNode;
...@@ -1612,20 +1575,6 @@ TIntermTyped *TIntermConstantUnion::fold( ...@@ -1612,20 +1575,6 @@ TIntermTyped *TIntermConstantUnion::fold(
"Unary operation not folded into constant"); "Unary operation not folded into constant");
return nullptr; return nullptr;
case EOpNormalize:
if (getType().getBasicType() == EbtFloat)
{
float x = unionArray[i].getFConst();
float length = VectorLength(unionArray, objectSize);
if (length)
tempConstArray[i].setFConst(x / length);
else
UndefinedConstantFoldingError(getLine(), op, getType().getBasicType(), infoSink, &tempConstArray[i]);
break;
}
infoSink.info.message(EPrefixInternalError, getLine(), "Unary operation not folded into constant");
return nullptr;
default: default:
return nullptr; return nullptr;
} }
...@@ -1969,70 +1918,6 @@ TIntermTyped *TIntermConstantUnion::FoldAggregateBuiltIn(TOperator op, TIntermAg ...@@ -1969,70 +1918,6 @@ TIntermTyped *TIntermConstantUnion::FoldAggregateBuiltIn(TOperator op, TIntermAg
} }
break; break;
case EOpDistance:
if (basicType == EbtFloat)
{
TConstantUnion *distanceArray = new TConstantUnion[maxObjectSize];
tempConstArray = new TConstantUnion();
for (size_t i = 0; i < maxObjectSize; i++)
{
float x = unionArrays[0][i].getFConst();
float y = unionArrays[1][i].getFConst();
distanceArray[i].setFConst(x - y);
}
tempConstArray->setFConst(VectorLength(distanceArray, maxObjectSize));
}
else
UNREACHABLE();
break;
case EOpDot:
if (basicType == EbtFloat)
{
tempConstArray = new TConstantUnion();
tempConstArray->setFConst(VectorDotProduct(unionArrays[0], unionArrays[1], maxObjectSize));
}
else
UNREACHABLE();
break;
case EOpCross:
if (basicType == EbtFloat && maxObjectSize == 3)
{
tempConstArray = new TConstantUnion[maxObjectSize];
float x0 = unionArrays[0][0].getFConst();
float x1 = unionArrays[0][1].getFConst();
float x2 = unionArrays[0][2].getFConst();
float y0 = unionArrays[1][0].getFConst();
float y1 = unionArrays[1][1].getFConst();
float y2 = unionArrays[1][2].getFConst();
tempConstArray[0].setFConst(x1 * y2 - y1 * x2);
tempConstArray[1].setFConst(x2 * y0 - y2 * x0);
tempConstArray[2].setFConst(x0 * y1 - y0 * x1);
}
else
UNREACHABLE();
break;
case EOpReflect:
if (basicType == EbtFloat)
{
// genType reflect (genType I, genType N) :
// For the incident vector I and surface orientation N, returns the reflection direction:
// I - 2 * dot(N, I) * N.
tempConstArray = new TConstantUnion[maxObjectSize];
float dotProduct = VectorDotProduct(unionArrays[1], unionArrays[0], maxObjectSize);
for (size_t i = 0; i < maxObjectSize; i++)
{
float result = unionArrays[0][i].getFConst() -
2.0f * dotProduct * unionArrays[1][i].getFConst();
tempConstArray[i].setFConst(result);
}
}
else
UNREACHABLE();
break;
default: default:
UNREACHABLE(); UNREACHABLE();
// TODO: Add constant folding support for other built-in operations that take 2 parameters and not handled above. // TODO: Add constant folding support for other built-in operations that take 2 parameters and not handled above.
...@@ -2158,53 +2043,6 @@ TIntermTyped *TIntermConstantUnion::FoldAggregateBuiltIn(TOperator op, TIntermAg ...@@ -2158,53 +2043,6 @@ TIntermTyped *TIntermConstantUnion::FoldAggregateBuiltIn(TOperator op, TIntermAg
} }
break; break;
case EOpFaceForward:
if (basicType == EbtFloat)
{
// genType faceforward(genType N, genType I, genType Nref) :
// If dot(Nref, I) < 0 return N, otherwise return -N.
tempConstArray = new TConstantUnion[maxObjectSize];
float dotProduct = VectorDotProduct(unionArrays[2], unionArrays[1], maxObjectSize);
for (size_t i = 0; i < maxObjectSize; i++)
{
if (dotProduct < 0)
tempConstArray[i].setFConst(unionArrays[0][i].getFConst());
else
tempConstArray[i].setFConst(-unionArrays[0][i].getFConst());
}
}
else
UNREACHABLE();
break;
case EOpRefract:
if (basicType == EbtFloat)
{
// genType refract(genType I, genType N, float eta) :
// For the incident vector I and surface normal N, and the ratio of indices of refraction eta,
// return the refraction vector. The result is computed by
// k = 1.0 - eta * eta * (1.0 - dot(N, I) * dot(N, I))
// if (k < 0.0)
// return genType(0.0)
// else
// return eta * I - (eta * dot(N, I) + sqrt(k)) * N
tempConstArray = new TConstantUnion[maxObjectSize];
float dotProduct = VectorDotProduct(unionArrays[1], unionArrays[0], maxObjectSize);
for (size_t i = 0; i < maxObjectSize; i++)
{
float eta = unionArrays[2][i].getFConst();
float k = 1.0f - eta * eta * (1.0f - dotProduct * dotProduct);
if (k < 0.0f)
tempConstArray[i].setFConst(0.0f);
else
tempConstArray[i].setFConst(eta * unionArrays[0][i].getFConst() -
(eta * dotProduct + sqrtf(k)) * unionArrays[1][i].getFConst());
}
}
else
UNREACHABLE();
break;
default: default:
UNREACHABLE(); UNREACHABLE();
// TODO: Add constant folding support for other built-in operations that take 3 parameters and not handled above. // TODO: Add constant folding support for other built-in operations that take 3 parameters and not handled above.
......
...@@ -477,12 +477,6 @@ TIntermTyped *TIntermediate::foldAggregateBuiltIn(TOperator op, TIntermAggregate ...@@ -477,12 +477,6 @@ TIntermTyped *TIntermediate::foldAggregateBuiltIn(TOperator op, TIntermAggregate
case EOpGreaterThanEqual: case EOpGreaterThanEqual:
case EOpVectorEqual: case EOpVectorEqual:
case EOpVectorNotEqual: case EOpVectorNotEqual:
case EOpDistance:
case EOpDot:
case EOpCross:
case EOpFaceForward:
case EOpReflect:
case EOpRefract:
return TIntermConstantUnion::FoldAggregateBuiltIn(op, aggregate, mInfoSink); return TIntermConstantUnion::FoldAggregateBuiltIn(op, aggregate, mInfoSink);
default: default:
// Constant folding not supported for the built-in. // Constant folding not supported for the built-in.
......
...@@ -7,53 +7,32 @@ ...@@ -7,53 +7,32 @@
// Tests for constant folding // Tests for constant folding
// //
#include <vector>
#include "angle_gl.h" #include "angle_gl.h"
#include "gtest/gtest.h" #include "gtest/gtest.h"
#include "GLSLANG/ShaderLang.h" #include "GLSLANG/ShaderLang.h"
#include "compiler/translator/PoolAlloc.h" #include "compiler/translator/PoolAlloc.h"
#include "compiler/translator/TranslatorESSL.h" #include "compiler/translator/TranslatorESSL.h"
template <typename T>
class ConstantFinder : public TIntermTraverser class ConstantFinder : public TIntermTraverser
{ {
public: public:
ConstantFinder(const std::vector<T> &constantVector) ConstantFinder(TConstantUnion constToFind)
: mConstantVector(constantVector), : mConstToFind(constToFind),
mFound(false) mFound(false)
{} {}
ConstantFinder(const T &value) virtual void visitConstantUnion(TIntermConstantUnion *node)
: mFound(false)
{
mConstantVector.push_back(value);
}
void visitConstantUnion(TIntermConstantUnion *node)
{ {
if (node->getType().getObjectSize() == mConstantVector.size()) if (node->getUnionArrayPointer()[0] == mConstToFind)
{ {
bool found = true; mFound = true;
for (size_t i = 0; i < mConstantVector.size(); i++)
{
if (node->getUnionArrayPointer()[i] != mConstantVector[i])
{
found = false;
break;
}
}
if (found)
{
mFound = found;
}
} }
} }
bool found() const { return mFound; } bool found() const { return mFound; }
private: private:
std::vector<T> mConstantVector; TConstantUnion mConstToFind;
bool mFound; bool mFound;
}; };
...@@ -93,20 +72,18 @@ class ConstantFoldingTest : public testing::Test ...@@ -93,20 +72,18 @@ class ConstantFoldingTest : public testing::Test
} }
} }
template <typename T> bool constantFoundInAST(TConstantUnion c)
bool constantFoundInAST(T constant)
{ {
ConstantFinder<T> finder(constant); ConstantFinder finder(c);
mASTRoot->traverse(&finder); mASTRoot->traverse(&finder);
return finder.found(); return finder.found();
} }
template <typename T> bool constantFoundInAST(int i)
bool constantVectorFoundInAST(const std::vector<T> &constantVector)
{ {
ConstantFinder<T> finder(constantVector); TConstantUnion c;
mASTRoot->traverse(&finder); c.setIConst(i);
return finder.found(); return constantFoundInAST(c);
} }
private: private:
...@@ -196,28 +173,3 @@ TEST_F(ConstantFoldingTest, FoldIntegerModulus) ...@@ -196,28 +173,3 @@ TEST_F(ConstantFoldingTest, FoldIntegerModulus)
ASSERT_FALSE(constantFoundInAST(5)); ASSERT_FALSE(constantFoundInAST(5));
ASSERT_TRUE(constantFoundInAST(4)); ASSERT_TRUE(constantFoundInAST(4));
} }
TEST_F(ConstantFoldingTest, FoldVectorCrossProduct)
{
const std::string &shaderString =
"#version 300 es\n"
"precision mediump float;\n"
"out vec3 my_Vec3;"
"void main() {\n"
" const vec3 v3 = cross(vec3(1.0f, 1.0f, 1.0f), vec3(1.0f, -1.0f, 1.0f));\n"
" my_Vec3 = v3;\n"
"}\n";
compile(shaderString);
std::vector<float> input1(3, 1.0f);
ASSERT_FALSE(constantVectorFoundInAST(input1));
std::vector<float> input2;
input2.push_back(1.0f);
input2.push_back(-1.0f);
input2.push_back(1.0f);
ASSERT_FALSE(constantVectorFoundInAST(input2));
std::vector<float> result;
result.push_back(2.0f);
result.push_back(0.0f);
result.push_back(-2.0f);
ASSERT_TRUE(constantVectorFoundInAST(result));
}
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