Commit c3bfb403 by Nicolas Capens Committed by Nicolas Capens

Add constant casting support.

BUG=15415045 Change-Id: I35b27fec33aede0fedde892cd9379ca19fdb6f08 Reviewed-on: https://swiftshader-review.googlesource.com/1112Reviewed-by: 's avatarNicolas Capens <nicolascapens@google.com>
parent 2060866e
// //
// 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 // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. // found in the LICENSE file.
// //
...@@ -17,14 +17,57 @@ public: ...@@ -17,14 +17,57 @@ public:
type = EbtVoid; 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 setIConst(int i) {iConst = i; type = EbtInt; }
void setFConst(float f) {fConst = f; type = EbtFloat; } void setFConst(float f) {fConst = f; type = EbtFloat; }
void setBConst(bool b) {bConst = b; type = EbtBool; } 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; } int getIConst() const { return iConst; }
float getFConst() const { return fConst; } float getFConst() const { return fConst; }
bool getBConst() const { return bConst; } bool getBConst() const { return bConst; }
......
...@@ -61,24 +61,23 @@ void TConstTraverser::visitSymbol(TIntermSymbol* node) ...@@ -61,24 +61,23 @@ void TConstTraverser::visitSymbol(TIntermSymbol* node)
{ {
infoSink.info.message(EPrefixInternalError, "Symbol Node found in constant constructor", node->getLine()); infoSink.info.message(EPrefixInternalError, "Symbol Node found in constant constructor", node->getLine());
return; return;
} }
bool TConstTraverser::visitBinary(Visit visit, TIntermBinary* node) bool TConstTraverser::visitBinary(Visit visit, TIntermBinary* node)
{ {
TQualifier qualifier = node->getType().getQualifier(); TQualifier qualifier = node->getType().getQualifier();
if (qualifier != EvqConst) { if (qualifier != EvqConst) {
TString buf; TString buf;
buf.append("'constructor' : assigning non-constant to "); buf.append("'constructor' : assigning non-constant to ");
buf.append(type.getCompleteString()); buf.append(type.getCompleteString());
infoSink.info.message(EPrefixError, buf.c_str(), node->getLine()); infoSink.info.message(EPrefixError, buf.c_str(), node->getLine());
error = true; 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; return false;
} }
...@@ -89,7 +88,7 @@ bool TConstTraverser::visitUnary(Visit visit, TIntermUnary* node) ...@@ -89,7 +88,7 @@ bool TConstTraverser::visitUnary(Visit visit, TIntermUnary* node)
buf.append(type.getCompleteString()); buf.append(type.getCompleteString());
infoSink.info.message(EPrefixError, buf.c_str(), node->getLine()); infoSink.info.message(EPrefixError, buf.c_str(), node->getLine());
error = true; error = true;
return false; return false;
} }
bool TConstTraverser::visitAggregate(Visit visit, TIntermAggregate* node) bool TConstTraverser::visitAggregate(Visit visit, TIntermAggregate* node)
...@@ -100,7 +99,7 @@ bool TConstTraverser::visitAggregate(Visit visit, TIntermAggregate* node) ...@@ -100,7 +99,7 @@ bool TConstTraverser::visitAggregate(Visit visit, TIntermAggregate* node)
buf.append(type.getCompleteString()); buf.append(type.getCompleteString());
infoSink.info.message(EPrefixError, buf.c_str(), node->getLine()); infoSink.info.message(EPrefixError, buf.c_str(), node->getLine());
error = true; error = true;
return false; return false;
} }
if (node->getSequence().size() == 0) { if (node->getSequence().size() == 0) {
...@@ -109,9 +108,9 @@ bool TConstTraverser::visitAggregate(Visit visit, TIntermAggregate* node) ...@@ -109,9 +108,9 @@ bool TConstTraverser::visitAggregate(Visit visit, TIntermAggregate* node)
} }
bool flag = node->getSequence().size() == 1 && node->getSequence()[0]->getAsTyped()->getAsConstantUnion(); bool flag = node->getSequence().size() == 1 && node->getSequence()[0]->getAsTyped()->getAsConstantUnion();
if (flag) if (flag)
{ {
singleConstantParam = true; singleConstantParam = true;
constructorType = node->getOp(); constructorType = node->getOp();
size = node->getType().getObjectSize(); size = node->getType().getObjectSize();
...@@ -119,19 +118,19 @@ bool TConstTraverser::visitAggregate(Visit visit, TIntermAggregate* node) ...@@ -119,19 +118,19 @@ bool TConstTraverser::visitAggregate(Visit visit, TIntermAggregate* node)
isMatrix = true; isMatrix = true;
matrixSize = node->getType().getNominalSize(); matrixSize = node->getType().getNominalSize();
} }
} }
for (TIntermSequence::iterator p = node->getSequence().begin(); for (TIntermSequence::iterator p = node->getSequence().begin();
p != node->getSequence().end(); p++) { p != node->getSequence().end(); p++) {
if (node->getOp() == EOpComma) if (node->getOp() == EOpComma)
index = 0; index = 0;
(*p)->traverse(this); (*p)->traverse(this);
} }
if (flag) if (flag)
{ {
singleConstantParam = false; singleConstantParam = false;
constructorType = EOpNull; constructorType = EOpNull;
size = 0; size = 0;
isMatrix = false; isMatrix = false;
...@@ -151,18 +150,19 @@ void TConstTraverser::visitConstantUnion(TIntermConstantUnion* node) ...@@ -151,18 +150,19 @@ void TConstTraverser::visitConstantUnion(TIntermConstantUnion* node)
{ {
ConstantUnion* leftUnionArray = unionArray; ConstantUnion* leftUnionArray = unionArray;
int instanceSize = type.getObjectSize(); int instanceSize = type.getObjectSize();
TBasicType basicType = type.getBasicType();
if (index >= instanceSize) if (index >= instanceSize)
return; return;
if (!singleConstantParam) { if (!singleConstantParam) {
int size = node->getType().getObjectSize(); int size = node->getType().getObjectSize();
ConstantUnion *rightUnionArray = node->getUnionArrayPointer(); ConstantUnion *rightUnionArray = node->getUnionArrayPointer();
for (int i=0; i < size; i++) { for (int i=0; i < size; i++) {
if (index >= instanceSize) if (index >= instanceSize)
return; return;
leftUnionArray[index] = rightUnionArray[i]; leftUnionArray[index].cast(basicType, rightUnionArray[i]);
(index)++; (index)++;
} }
...@@ -175,10 +175,10 @@ void TConstTraverser::visitConstantUnion(TIntermConstantUnion* node) ...@@ -175,10 +175,10 @@ void TConstTraverser::visitConstantUnion(TIntermConstantUnion* node)
if (i >= instanceSize) if (i >= instanceSize)
return; return;
leftUnionArray[i] = rightUnionArray[count]; leftUnionArray[i].cast(basicType, rightUnionArray[count]);
(index)++; (index)++;
if (node->getType().getObjectSize() > 1) if (node->getType().getObjectSize() > 1)
count++; count++;
} }
...@@ -189,14 +189,14 @@ void TConstTraverser::visitConstantUnion(TIntermConstantUnion* node) ...@@ -189,14 +189,14 @@ void TConstTraverser::visitConstantUnion(TIntermConstantUnion* node)
if (i >= instanceSize) if (i >= instanceSize)
return; return;
if (element - i == 0 || (i - element) % (matrixSize + 1) == 0 ) if (element - i == 0 || (i - element) % (matrixSize + 1) == 0 )
leftUnionArray[i] = rightUnionArray[count]; leftUnionArray[i].cast(basicType, rightUnionArray[0]);
else else
leftUnionArray[i].setFConst(0.0f); leftUnionArray[i].setFConst(0.0f);
(index)++; (index)++;
if (node->getType().getObjectSize() > 1) if (node->getType().getObjectSize() > 1)
count++; count++;
} }
} }
} }
......
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