Add constant casting support. BUG=15415045 Change-Id: I35b27fec33aede0fedde892cd9379ca19fdb6f08 Reviewed-on: https://swiftshader-review.googlesource.com/1112 Reviewed-by: Nicolas Capens <nicolascapens@google.com>
diff --git a/src/GLES2/compiler/ConstantUnion.h b/src/GLES2/compiler/ConstantUnion.h index 50af5e8..6b04310 100644 --- a/src/GLES2/compiler/ConstantUnion.h +++ b/src/GLES2/compiler/ConstantUnion.h
@@ -1,5 +1,5 @@ // -// Copyright (c) 2002-2013 The ANGLE Project Authors. All rights reserved. +// Copyright (c) 2002-2014 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. // @@ -17,14 +17,57 @@ type = EbtVoid; } - POOL_ALLOCATOR_NEW_DELETE(GlobalPoolAllocator) + bool cast(TBasicType newType, const ConstantUnion &constant) + { + switch (newType) + { + case EbtFloat: + switch (constant.type) + { + case EbtInt: setFConst(static_cast<float>(constant.getIConst())); break; + case EbtBool: setFConst(static_cast<float>(constant.getBConst())); break; + case EbtFloat: setFConst(static_cast<float>(constant.getFConst())); break; + default: return false; + } + break; + case EbtInt: + switch (constant.type) + { + case EbtInt: setIConst(static_cast<int>(constant.getIConst())); break; + case EbtBool: setIConst(static_cast<int>(constant.getBConst())); break; + case EbtFloat: setIConst(static_cast<int>(constant.getFConst())); break; + default: return false; + } + break; + case EbtBool: + switch (constant.type) + { + case EbtInt: setBConst(constant.getIConst() != 0); break; + case EbtBool: setBConst(constant.getBConst()); break; + case EbtFloat: setBConst(constant.getFConst() != 0.0f); break; + default: return false; + } + break; + case EbtStruct: // Struct fields don't get cast + switch (constant.type) + { + case EbtInt: setIConst(constant.getIConst()); break; + case EbtBool: setBConst(constant.getBConst()); break; + case EbtFloat: setFConst(constant.getFConst()); break; + default: return false; + } + break; + default: + return false; + } + + return true; + } + void setIConst(int i) {iConst = i; type = EbtInt; } void setFConst(float f) {fConst = f; type = EbtFloat; } void setBConst(bool b) {bConst = b; type = EbtBool; } - int getIConst() { return iConst; } - float getFConst() { return fConst; } - bool getBConst() { return bConst; } int getIConst() const { return iConst; } float getFConst() const { return fConst; } bool getBConst() const { return bConst; }
diff --git a/src/GLES2/compiler/parseConst.cpp b/src/GLES2/compiler/parseConst.cpp index f8fb6be..c864836 100644 --- a/src/GLES2/compiler/parseConst.cpp +++ b/src/GLES2/compiler/parseConst.cpp
@@ -61,24 +61,23 @@ { infoSink.info.message(EPrefixInternalError, "Symbol Node found in constant constructor", node->getLine()); return; - } bool TConstTraverser::visitBinary(Visit visit, TIntermBinary* node) { TQualifier qualifier = node->getType().getQualifier(); - + if (qualifier != EvqConst) { TString buf; buf.append("'constructor' : assigning non-constant to "); buf.append(type.getCompleteString()); infoSink.info.message(EPrefixError, buf.c_str(), node->getLine()); error = true; - return false; + return false; } - infoSink.info.message(EPrefixInternalError, "Binary Node found in constant constructor", node->getLine()); - + infoSink.info.message(EPrefixInternalError, "Binary Node found in constant constructor", node->getLine()); + return false; } @@ -89,7 +88,7 @@ buf.append(type.getCompleteString()); infoSink.info.message(EPrefixError, buf.c_str(), node->getLine()); error = true; - return false; + return false; } bool TConstTraverser::visitAggregate(Visit visit, TIntermAggregate* node) @@ -100,7 +99,7 @@ buf.append(type.getCompleteString()); infoSink.info.message(EPrefixError, buf.c_str(), node->getLine()); error = true; - return false; + return false; } if (node->getSequence().size() == 0) { @@ -109,9 +108,9 @@ } bool flag = node->getSequence().size() == 1 && node->getSequence()[0]->getAsTyped()->getAsConstantUnion(); - if (flag) + if (flag) { - singleConstantParam = true; + singleConstantParam = true; constructorType = node->getOp(); size = node->getType().getObjectSize(); @@ -119,19 +118,19 @@ isMatrix = true; matrixSize = node->getType().getNominalSize(); } - } + } - for (TIntermSequence::iterator p = node->getSequence().begin(); + for (TIntermSequence::iterator p = node->getSequence().begin(); p != node->getSequence().end(); p++) { if (node->getOp() == EOpComma) - index = 0; + index = 0; (*p)->traverse(this); - } - if (flag) + } + if (flag) { - singleConstantParam = false; + singleConstantParam = false; constructorType = EOpNull; size = 0; isMatrix = false; @@ -151,18 +150,19 @@ { ConstantUnion* leftUnionArray = unionArray; int instanceSize = type.getObjectSize(); + TBasicType basicType = type.getBasicType(); if (index >= instanceSize) return; if (!singleConstantParam) { int size = node->getType().getObjectSize(); - + ConstantUnion *rightUnionArray = node->getUnionArrayPointer(); for (int i=0; i < size; i++) { if (index >= instanceSize) return; - leftUnionArray[index] = rightUnionArray[i]; + leftUnionArray[index].cast(basicType, rightUnionArray[i]); (index)++; } @@ -175,10 +175,10 @@ if (i >= instanceSize) return; - leftUnionArray[i] = rightUnionArray[count]; + leftUnionArray[i].cast(basicType, rightUnionArray[count]); (index)++; - + if (node->getType().getObjectSize() > 1) count++; } @@ -189,14 +189,14 @@ if (i >= instanceSize) return; if (element - i == 0 || (i - element) % (matrixSize + 1) == 0 ) - leftUnionArray[i] = rightUnionArray[count]; - else + leftUnionArray[i].cast(basicType, rightUnionArray[0]); + else leftUnionArray[i].setFConst(0.0f); (index)++; if (node->getType().getObjectSize() > 1) - count++; + count++; } } }