Front-ends: Non-functional: Rationalize vector and matrix swizzles.
This reduces code duplication in a few ways, and better encapsulates vector swizzle representation.
This commit is contained in:
@@ -974,20 +974,20 @@ TIntermTyped* TIntermediate::foldDereference(TIntermTyped* node, int index, cons
|
||||
// Make a constant vector node or constant scalar node, representing a given
|
||||
// constant vector and constant swizzle into it.
|
||||
//
|
||||
TIntermTyped* TIntermediate::foldSwizzle(TIntermTyped* node, TVectorFields& fields, const TSourceLoc& loc)
|
||||
TIntermTyped* TIntermediate::foldSwizzle(TIntermTyped* node, TSwizzleSelectors<TVectorSelector>& selectors, const TSourceLoc& loc)
|
||||
{
|
||||
const TConstUnionArray& unionArray = node->getAsConstantUnion()->getConstArray();
|
||||
TConstUnionArray constArray(fields.num);
|
||||
TConstUnionArray constArray(selectors.size());
|
||||
|
||||
for (int i = 0; i < fields.num; i++)
|
||||
constArray[i] = unionArray[fields.offsets[i]];
|
||||
for (int i = 0; i < selectors.size(); i++)
|
||||
constArray[i] = unionArray[selectors[i]];
|
||||
|
||||
TIntermTyped* result = addConstantUnion(constArray, node->getType(), loc);
|
||||
|
||||
if (result == 0)
|
||||
result = node;
|
||||
else
|
||||
result->setType(TType(node->getBasicType(), EvqConst, fields.num));
|
||||
result->setType(TType(node->getBasicType(), EvqConst, selectors.size()));
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
@@ -1392,38 +1392,35 @@ TIntermConstantUnion* TIntermediate::addConstantUnion(double d, TBasicType baseT
|
||||
return addConstantUnion(unionArray, TType(baseType, EvqConst), loc, literal);
|
||||
}
|
||||
|
||||
TIntermTyped* TIntermediate::addSwizzle(TVectorFields& fields, const TSourceLoc& loc)
|
||||
// Put vector swizzle selectors onto the given sequence
|
||||
void TIntermediate::pushSelector(TIntermSequence& sequence, const TVectorSelector& selector, const TSourceLoc& loc)
|
||||
{
|
||||
TIntermAggregate* node = new TIntermAggregate(EOpSequence);
|
||||
|
||||
node->setLoc(loc);
|
||||
TIntermConstantUnion* constIntNode;
|
||||
TIntermSequence &sequenceVector = node->getSequence();
|
||||
|
||||
for (int i = 0; i < fields.num; i++) {
|
||||
constIntNode = addConstantUnion(fields.offsets[i], loc);
|
||||
sequenceVector.push_back(constIntNode);
|
||||
}
|
||||
|
||||
return node;
|
||||
TIntermConstantUnion* constIntNode = addConstantUnion(selector, loc);
|
||||
sequence.push_back(constIntNode);
|
||||
}
|
||||
|
||||
// A matrix swizzle is a sequence of nodes, 2N long, where N is the
|
||||
// number of components in the swizzle, alternating col,row, col,row, ...
|
||||
TIntermTyped* TIntermediate::addSwizzle(TMatrixComponents& comps, const TSourceLoc& loc)
|
||||
// Put matrix swizzle selectors onto the given sequence
|
||||
void TIntermediate::pushSelector(TIntermSequence& sequence, const TMatrixSelector& selector, const TSourceLoc& loc)
|
||||
{
|
||||
TIntermConstantUnion* constIntNode = addConstantUnion(selector.coord1, loc);
|
||||
sequence.push_back(constIntNode);
|
||||
constIntNode = addConstantUnion(selector.coord2, loc);
|
||||
sequence.push_back(constIntNode);
|
||||
}
|
||||
|
||||
// Make an aggregate node that has a sequence of all selectors.
|
||||
template TIntermTyped* TIntermediate::addSwizzle<TVectorSelector>(TSwizzleSelectors<TVectorSelector>& selector, const TSourceLoc& loc);
|
||||
template TIntermTyped* TIntermediate::addSwizzle<TMatrixSelector>(TSwizzleSelectors<TMatrixSelector>& selector, const TSourceLoc& loc);
|
||||
template<typename selectorType>
|
||||
TIntermTyped* TIntermediate::addSwizzle(TSwizzleSelectors<selectorType>& selector, const TSourceLoc& loc)
|
||||
{
|
||||
TIntermAggregate* node = new TIntermAggregate(EOpSequence);
|
||||
|
||||
node->setLoc(loc);
|
||||
TIntermConstantUnion* constIntNode;
|
||||
TIntermSequence &sequenceVector = node->getSequence();
|
||||
|
||||
for (int i = 0; i < comps.size(); i++) {
|
||||
constIntNode = addConstantUnion(comps.get(i).coord1, loc);
|
||||
sequenceVector.push_back(constIntNode);
|
||||
constIntNode = addConstantUnion(comps.get(i).coord2, loc);
|
||||
sequenceVector.push_back(constIntNode);
|
||||
}
|
||||
for (int i = 0; i < selector.size(); i++)
|
||||
pushSelector(sequenceVector, selector[i], loc);
|
||||
|
||||
return node;
|
||||
}
|
||||
|
||||
@@ -433,6 +433,108 @@ const TFunction* TParseContextBase::selectFunction(
|
||||
return incumbent;
|
||||
}
|
||||
|
||||
//
|
||||
// Look at a '.' field selector string and change it into numerical selectors
|
||||
// for a vector or scalar.
|
||||
//
|
||||
// Always return some form of swizzle, so the result is always usable.
|
||||
//
|
||||
void TParseContextBase::parseSwizzleSelector(const TSourceLoc& loc, const TString& compString, int vecSize,
|
||||
TSwizzleSelectors<TVectorSelector>& selector)
|
||||
{
|
||||
// Too long?
|
||||
if (compString.size() > TSwizzleSelectors<TVectorSelector>::maxSelectors)
|
||||
error(loc, "vector swizzle too long", compString.c_str(), "");
|
||||
|
||||
// Use this to test that all swizzle characters are from the same swizzle-namespace-set
|
||||
enum {
|
||||
exyzw,
|
||||
ergba,
|
||||
estpq,
|
||||
} fieldSet[TSwizzleSelectors<TVectorSelector>::maxSelectors];
|
||||
|
||||
// Decode the swizzle string.
|
||||
int size = std::min(TSwizzleSelectors<TVectorSelector>::maxSelectors, (int)compString.size());
|
||||
for (int i = 0; i < size; ++i) {
|
||||
switch (compString[i]) {
|
||||
case 'x':
|
||||
selector.push_back(0);
|
||||
fieldSet[i] = exyzw;
|
||||
break;
|
||||
case 'r':
|
||||
selector.push_back(0);
|
||||
fieldSet[i] = ergba;
|
||||
break;
|
||||
case 's':
|
||||
selector.push_back(0);
|
||||
fieldSet[i] = estpq;
|
||||
break;
|
||||
|
||||
case 'y':
|
||||
selector.push_back(1);
|
||||
fieldSet[i] = exyzw;
|
||||
break;
|
||||
case 'g':
|
||||
selector.push_back(1);
|
||||
fieldSet[i] = ergba;
|
||||
break;
|
||||
case 't':
|
||||
selector.push_back(1);
|
||||
fieldSet[i] = estpq;
|
||||
break;
|
||||
|
||||
case 'z':
|
||||
selector.push_back(2);
|
||||
fieldSet[i] = exyzw;
|
||||
break;
|
||||
case 'b':
|
||||
selector.push_back(2);
|
||||
fieldSet[i] = ergba;
|
||||
break;
|
||||
case 'p':
|
||||
selector.push_back(2);
|
||||
fieldSet[i] = estpq;
|
||||
break;
|
||||
|
||||
case 'w':
|
||||
selector.push_back(3);
|
||||
fieldSet[i] = exyzw;
|
||||
break;
|
||||
case 'a':
|
||||
selector.push_back(3);
|
||||
fieldSet[i] = ergba;
|
||||
break;
|
||||
case 'q':
|
||||
selector.push_back(3);
|
||||
fieldSet[i] = estpq;
|
||||
break;
|
||||
|
||||
default:
|
||||
error(loc, "unknown swizzle selection", compString.c_str(), "");
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// Additional error checking.
|
||||
for (int i = 0; i < selector.size(); ++i) {
|
||||
if (selector[i] >= vecSize) {
|
||||
error(loc, "vector swizzle selection out of range", compString.c_str(), "");
|
||||
selector.resize(i);
|
||||
break;
|
||||
}
|
||||
|
||||
if (i > 0 && fieldSet[i] != fieldSet[i-1]) {
|
||||
error(loc, "vector swizzle selectors not from the same set", compString.c_str(), "");
|
||||
selector.resize(i);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// Ensure it is valid.
|
||||
if (selector.size() == 0)
|
||||
selector.push_back(0);
|
||||
}
|
||||
|
||||
//
|
||||
// Make the passed-in variable information become a member of the
|
||||
// global uniform block. If this doesn't exist yet, make it.
|
||||
|
||||
@@ -258,106 +258,6 @@ void TParseContext::handlePragma(const TSourceLoc& loc, const TVector<TString>&
|
||||
}
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// Sub- vector and matrix fields
|
||||
//
|
||||
////////////////////////////////////////////////////////////////////////
|
||||
|
||||
//
|
||||
// Look at a '.' field selector string and change it into offsets
|
||||
// for a vector or scalar
|
||||
//
|
||||
// Returns true if there is no error.
|
||||
//
|
||||
bool TParseContext::parseVectorFields(const TSourceLoc& loc, const TString& compString, int vecSize, TVectorFields& fields)
|
||||
{
|
||||
fields.num = (int)compString.size();
|
||||
if (fields.num > 4) {
|
||||
error(loc, "illegal vector field selection", compString.c_str(), "");
|
||||
return false;
|
||||
}
|
||||
|
||||
enum {
|
||||
exyzw,
|
||||
ergba,
|
||||
estpq,
|
||||
} fieldSet[4];
|
||||
|
||||
for (int i = 0; i < fields.num; ++i) {
|
||||
switch (compString[i]) {
|
||||
case 'x':
|
||||
fields.offsets[i] = 0;
|
||||
fieldSet[i] = exyzw;
|
||||
break;
|
||||
case 'r':
|
||||
fields.offsets[i] = 0;
|
||||
fieldSet[i] = ergba;
|
||||
break;
|
||||
case 's':
|
||||
fields.offsets[i] = 0;
|
||||
fieldSet[i] = estpq;
|
||||
break;
|
||||
case 'y':
|
||||
fields.offsets[i] = 1;
|
||||
fieldSet[i] = exyzw;
|
||||
break;
|
||||
case 'g':
|
||||
fields.offsets[i] = 1;
|
||||
fieldSet[i] = ergba;
|
||||
break;
|
||||
case 't':
|
||||
fields.offsets[i] = 1;
|
||||
fieldSet[i] = estpq;
|
||||
break;
|
||||
case 'z':
|
||||
fields.offsets[i] = 2;
|
||||
fieldSet[i] = exyzw;
|
||||
break;
|
||||
case 'b':
|
||||
fields.offsets[i] = 2;
|
||||
fieldSet[i] = ergba;
|
||||
break;
|
||||
case 'p':
|
||||
fields.offsets[i] = 2;
|
||||
fieldSet[i] = estpq;
|
||||
break;
|
||||
|
||||
case 'w':
|
||||
fields.offsets[i] = 3;
|
||||
fieldSet[i] = exyzw;
|
||||
break;
|
||||
case 'a':
|
||||
fields.offsets[i] = 3;
|
||||
fieldSet[i] = ergba;
|
||||
break;
|
||||
case 'q':
|
||||
fields.offsets[i] = 3;
|
||||
fieldSet[i] = estpq;
|
||||
break;
|
||||
default:
|
||||
error(loc, "illegal vector field selection", compString.c_str(), "");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
for (int i = 0; i < fields.num; ++i) {
|
||||
if (fields.offsets[i] >= vecSize) {
|
||||
error(loc, "vector field selection out of range", compString.c_str(), "");
|
||||
return false;
|
||||
}
|
||||
|
||||
if (i > 0) {
|
||||
if (fieldSet[i] != fieldSet[i-1]) {
|
||||
error(loc, "illegal - vector component fields not from the same set", compString.c_str(), "");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
//
|
||||
// Handle seeing a variable identifier in the grammar.
|
||||
//
|
||||
@@ -781,17 +681,14 @@ TIntermTyped* TParseContext::handleDotDereference(const TSourceLoc& loc, TInterm
|
||||
profileRequires(loc, ~EEsProfile, 420, E_GL_ARB_shading_language_420pack, dotFeature);
|
||||
}
|
||||
|
||||
TVectorFields fields;
|
||||
if (! parseVectorFields(loc, field, base->getVectorSize(), fields)) {
|
||||
fields.num = 1;
|
||||
fields.offsets[0] = 0;
|
||||
}
|
||||
TSwizzleSelectors<TVectorSelector> selectors;
|
||||
parseSwizzleSelector(loc, field, base->getVectorSize(), selectors);
|
||||
|
||||
if (base->isScalar()) {
|
||||
if (fields.num == 1)
|
||||
if (selectors.size() == 1)
|
||||
return result;
|
||||
else {
|
||||
TType type(base->getBasicType(), EvqTemporary, fields.num);
|
||||
TType type(base->getBasicType(), EvqTemporary, selectors.size());
|
||||
// Swizzle operations propagate specialization-constantness
|
||||
if (base->getQualifier().isSpecConstant())
|
||||
type.getQualifier().makeSpecConstant();
|
||||
@@ -800,17 +697,16 @@ TIntermTyped* TParseContext::handleDotDereference(const TSourceLoc& loc, TInterm
|
||||
}
|
||||
|
||||
if (base->getType().getQualifier().isFrontEndConstant())
|
||||
result = intermediate.foldSwizzle(base, fields, loc);
|
||||
result = intermediate.foldSwizzle(base, selectors, loc);
|
||||
else {
|
||||
if (fields.num == 1) {
|
||||
TIntermTyped* index = intermediate.addConstantUnion(fields.offsets[0], loc);
|
||||
if (selectors.size() == 1) {
|
||||
TIntermTyped* index = intermediate.addConstantUnion(selectors[0], loc);
|
||||
result = intermediate.addIndex(EOpIndexDirect, base, index, loc);
|
||||
result->setType(TType(base->getBasicType(), EvqTemporary, base->getType().getQualifier().precision));
|
||||
} else {
|
||||
TString vectorString = field;
|
||||
TIntermTyped* index = intermediate.addSwizzle(fields, loc);
|
||||
TIntermTyped* index = intermediate.addSwizzle(selectors, loc);
|
||||
result = intermediate.addIndex(EOpVectorSwizzle, base, index, loc);
|
||||
result->setType(TType(base->getBasicType(), EvqTemporary, base->getType().getQualifier().precision, (int)vectorString.size()));
|
||||
result->setType(TType(base->getBasicType(), EvqTemporary, base->getType().getQualifier().precision, selectors.size()));
|
||||
}
|
||||
// Swizzle operations propagate specialization-constantness
|
||||
if (base->getType().getQualifier().isSpecConstant())
|
||||
|
||||
@@ -170,6 +170,9 @@ protected:
|
||||
std::function<bool(const TType&, const TType&, const TType&)>,
|
||||
/* output */ bool& tie);
|
||||
|
||||
virtual void parseSwizzleSelector(const TSourceLoc&, const TString&, int size,
|
||||
TSwizzleSelectors<TVectorSelector>&);
|
||||
|
||||
// Manage the global uniform block (default uniforms in GLSL, $Global in HLSL)
|
||||
TVariable* globalUniformBlock; // the actual block, inserted into the symbol table
|
||||
int firstNewMember; // the index of the first member not yet inserted into the symbol table
|
||||
@@ -284,7 +287,6 @@ public:
|
||||
void handlePrecisionQualifier(const TSourceLoc&, TQualifier&, TPrecisionQualifier);
|
||||
void checkPrecisionQualifier(const TSourceLoc&, TPrecisionQualifier);
|
||||
|
||||
bool parseVectorFields(const TSourceLoc&, const TString&, int vecSize, TVectorFields&);
|
||||
void assignError(const TSourceLoc&, const char* op, TString left, TString right);
|
||||
void unaryOpError(const TSourceLoc&, const char* op, TString operand);
|
||||
void binaryOpError(const TSourceLoc&, const char* op, TString left, TString right);
|
||||
|
||||
@@ -47,45 +47,39 @@ class TInfoSink;
|
||||
|
||||
namespace glslang {
|
||||
|
||||
struct TVectorFields {
|
||||
TVectorFields() { }
|
||||
|
||||
TVectorFields(int c0, int c1, int c2, int c3) : num(4)
|
||||
{
|
||||
offsets[0] = c0;
|
||||
offsets[1] = c1;
|
||||
offsets[2] = c2;
|
||||
offsets[3] = c3;
|
||||
}
|
||||
|
||||
int offsets[4];
|
||||
int num;
|
||||
struct TMatrixSelector {
|
||||
int coord1; // stay agnostic about column/row; this is parse order
|
||||
int coord2;
|
||||
};
|
||||
|
||||
class TMatrixComponents {
|
||||
public:
|
||||
static const int maxMatrixComponents = 4;
|
||||
struct tMatrixComponent {
|
||||
int coord1; // stay agnostic about column/row; this is parse order
|
||||
int coord2;
|
||||
};
|
||||
typedef int TVectorSelector;
|
||||
|
||||
TMatrixComponents() : size_(0) { }
|
||||
void push_back(tMatrixComponent comp)
|
||||
template<typename selectorType>
|
||||
class TSwizzleSelectors {
|
||||
public:
|
||||
static const int maxSelectors = 4;
|
||||
TSwizzleSelectors() : size_(0) { }
|
||||
|
||||
void push_back(selectorType comp)
|
||||
{
|
||||
if (size_ < maxMatrixComponents)
|
||||
if (size_ < maxSelectors)
|
||||
components[size_++] = comp;
|
||||
}
|
||||
int size() const { return size_; }
|
||||
tMatrixComponent get(int i) const
|
||||
void resize(int s)
|
||||
{
|
||||
assert(i < maxMatrixComponents);
|
||||
assert(s <= size_);
|
||||
size_ = s;
|
||||
}
|
||||
int size() const { return size_; }
|
||||
selectorType operator[](int i) const
|
||||
{
|
||||
assert(i < maxSelectors);
|
||||
return components[i];
|
||||
}
|
||||
|
||||
|
||||
private:
|
||||
int size_;
|
||||
tMatrixComponent components[4];
|
||||
selectorType components[maxSelectors];
|
||||
};
|
||||
|
||||
//
|
||||
@@ -274,8 +268,7 @@ public:
|
||||
TIntermAggregate* addForLoop(TIntermNode*, TIntermNode*, TIntermTyped*, TIntermTyped*, bool testFirst, const TSourceLoc&);
|
||||
TIntermBranch* addBranch(TOperator, const TSourceLoc&);
|
||||
TIntermBranch* addBranch(TOperator, TIntermTyped*, const TSourceLoc&);
|
||||
TIntermTyped* addSwizzle(TVectorFields&, const TSourceLoc&);
|
||||
TIntermTyped* addSwizzle(TMatrixComponents&, const TSourceLoc&);
|
||||
template<typename selectorType> TIntermTyped* addSwizzle(TSwizzleSelectors<selectorType>&, const TSourceLoc&);
|
||||
|
||||
// Low level functions to add nodes (no conversions or other higher level transformations)
|
||||
// If a type is provided, the node's type will be set to it.
|
||||
@@ -291,7 +284,7 @@ public:
|
||||
TIntermTyped* fold(TIntermAggregate* aggrNode);
|
||||
TIntermTyped* foldConstructor(TIntermAggregate* aggrNode);
|
||||
TIntermTyped* foldDereference(TIntermTyped* node, int index, const TSourceLoc&);
|
||||
TIntermTyped* foldSwizzle(TIntermTyped* node, TVectorFields& fields, const TSourceLoc&);
|
||||
TIntermTyped* foldSwizzle(TIntermTyped* node, TSwizzleSelectors<TVectorSelector>& fields, const TSourceLoc&);
|
||||
|
||||
// Tree ops
|
||||
static const TIntermTyped* findLValueBase(const TIntermTyped*, bool swizzleOkay);
|
||||
@@ -444,6 +437,8 @@ protected:
|
||||
bool promoteBinary(TIntermBinary&);
|
||||
void addSymbolLinkageNode(TIntermAggregate*& linkage, TSymbolTable&, const TString&);
|
||||
bool promoteAggregate(TIntermAggregate&);
|
||||
void pushSelector(TIntermSequence&, const TVectorSelector&, const TSourceLoc&);
|
||||
void pushSelector(TIntermSequence&, const TMatrixSelector&, const TSourceLoc&);
|
||||
|
||||
const EShLanguage language; // stage, known at construction time
|
||||
EShSource source; // source language, known a bit later
|
||||
|
||||
Reference in New Issue
Block a user