Constant fold array indexing and comparison
A virtual function to get the constant value of an AST node is added
to TIntermTyped. This way a constant value can be retrieved
conveniently from multiple different types of nodes. TIntermSymbol
nodes pointing to a const variable can return the value associated
with the variable, constructor nodes can build a constant value from
their arguments, and indexing nodes can index into a constant array.
This enables constant folding operations on constant arrays, while
making sure that large amounts of data are not duplicated in the
output shader. When folding an operation makes sense, the values of
the arguments can be retrieved by using the new
TIntermTyped::getConstantValue(). When folding an operation would
result in duplicating data, the AST can just be left to be written out
as is.
For example, if the code contains a constant array of arrays, indexing
into individual elements of the inner arrays can be folded, but
indexing the top level array is left in place and not replaced with
duplicated array literals.
Constant folding is supported for indexing and comparisons of arrays.
In case constant arrays are only referenced through foldable
operations, the variable declarations will be pruned from the AST by
the RemoveUnreferencedVariables step.
BUG=angleproject:2298
TEST=angle_unittests
Change-Id: I5b3be237b7e9fdba56aa9bf0a41b691f4d8f01eb
Reviewed-on: https://chromium-review.googlesource.com/850973
Reviewed-by: Geoff Lang <geofflang@chromium.org>
Commit-Queue: Olli Etuaho <oetuaho@nvidia.com>
diff --git a/src/compiler/translator/OutputGLSLBase.cpp b/src/compiler/translator/OutputGLSLBase.cpp
index 72d5e39..ec4a65a 100644
--- a/src/compiler/translator/OutputGLSLBase.cpp
+++ b/src/compiler/translator/OutputGLSLBase.cpp
@@ -441,7 +441,7 @@
void TOutputGLSLBase::visitConstantUnion(TIntermConstantUnion *node)
{
- writeConstantUnion(node->getType(), node->getUnionArrayPointer());
+ writeConstantUnion(node->getType(), node->getConstantValue());
}
bool TOutputGLSLBase::visitSwizzle(Visit visit, TIntermSwizzle *node)