// //Copyright (C) 2013 LunarG, Inc. // //All rights reserved. // //Redistribution and use in source and binary forms, with or without //modification, are permitted provided that the following conditions //are met: // // Redistributions of source code must retain the above copyright // notice, this list of conditions and the following disclaimer. // // Redistributions in binary form must reproduce the above // copyright notice, this list of conditions and the following // disclaimer in the documentation and/or other materials provided // with the distribution. // // Neither the name of 3Dlabs Inc. Ltd. nor the names of its // contributors may be used to endorse or promote products derived // from this software without specific prior written permission. // //THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS //"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT //LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS //FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE //COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, //INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, //BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; //LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER //CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT //LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN //ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE //POSSIBILITY OF SUCH DAMAGE. // #include "../Include/Common.h" #include "reflection.h" #include "localintermediate.h" #include "gl_types.h" // // Grow the reflection database through a friend traverser class of TReflection and a // collection of functions to do a liveness traversal that note what uniforms are used // in semantically non-dead code. // // Can be used multiple times, once per stage, to grow a program reflection. // // High-level algorithm for one stage: // // 1. Put main() on list of live functions. // // 2. Traverse any live function, while skipping if-tests with a compile-time constant // condition of false, and while adding any encountered function calls to the live // function list. // // Repeat until the live function list is empty. // // 3. Add any encountered uniform variables and blocks to the reflection database. // // Can be attempted with a failed link, but will return false if recursion had been detected, or // there wasn't exactly one main. // namespace glslang { // // The traverser: mostly pass through, except // - processing function-call nodes to push live functions onto the stack of functions to process // - processing binary nodes to see if they are dereferences of an aggregates to track // - processing symbol nodes to see if they are non-aggregate objects to track // - processing selection nodes to trim semantically dead code // // This is in the glslang namespace directly so it can be a friend of TReflection. // class TLiveTraverser : public TIntermTraverser { public: TLiveTraverser(const TIntermediate& i, TReflection& r) : intermediate(i), reflection(r) { } // Track live funtions as well as uniforms, so that we don't visit dead functions // and only visit each function once. void addFunctionCall(TIntermAggregate* call) { // just use the map to ensure we process each function at most once if (reflection.nameToIndex.find(call->getName()) == reflection.nameToIndex.end()) { reflection.nameToIndex[call->getName()] = -1; pushFunction(call->getName()); } } // Add a simple uniform variable reference to the uniform database, no dereference involved. void addUniform(const TIntermSymbol& symbol) { if (reflection.nameToIndex.find(symbol.getName()) == reflection.nameToIndex.end()) { if (isReflectionGranularity(symbol.getType())) { reflection.nameToIndex[symbol.getName()] = reflection.indexToUniform.size(); reflection.indexToUniform.push_back(TObjectReflection(symbol.getName(), -1, mapToGlType(symbol.getType()), mapToGlArraySize(symbol.getType()), -1)); } } } static const int baseAlignmentVec4Std140; // align a value: if 'value' is not aligned to 'alignment', move it up to a multiple of alignment void align(int& value, int alignment) { int error = value % alignment; if (error) value += alignment - error; } // return the size and alignment of a scalar int getBaseAlignmentScalar(const TType& type, int& size) { switch (type.getBasicType()) { case EbtDouble: size = 8; return 8; default: size = 4; return 4; } } // Implement base-alignment and size rules from section 7.6.2.2 Standard Uniform Block Layout // Operates recursively. // If std140 is true, it does the rounding up to vec4 size required by std140, // otherwise it does not, yielding std430 rules. // // Returns the size of the type. int getBaseAlignment(const TType& type, int& size, bool std140) { int alignment; // rules 4, 6, and 8 if (type.isArray()) { TType derefType(type, 0); alignment = getBaseAlignment(derefType, size, std140); if (std140) alignment = std::max(baseAlignmentVec4Std140, alignment); align(size, alignment); size *= type.getArraySize(); return alignment; } // rule 9 if (type.getBasicType() == EbtStruct) { const TTypeList& memberList = *type.getStruct(); size = 0; int maxAlignment = std140 ? baseAlignmentVec4Std140 : 0; for (size_t m = 0; m < memberList.size(); ++m) { int memberSize; int memberAlignment = getBaseAlignment(*memberList[m].type, memberSize, std140); maxAlignment = std::max(maxAlignment, memberAlignment); align(size, memberAlignment); size += memberSize; } return maxAlignment; } // rule 1 if (type.isScalar()) return getBaseAlignmentScalar(type, size); // rules 2 and 3 if (type.isVector()) { int scalarAlign = getBaseAlignmentScalar(type, size); switch (type.getVectorSize()) { case 2: size *= 2; return 2 * scalarAlign; default: size *= type.getVectorSize(); return 4 * scalarAlign; } } // rules 5 and 7 if (type.isMatrix()) { TType derefType(type, 0); // rule 5: deref to row, not to column, meaning the size of vector is num columns instead of num rows if (type.getQualifier().layoutMatrix == ElmRowMajor) derefType.setElementType(derefType.getBasicType(), type.getMatrixCols(), 0, 0, 0); alignment = getBaseAlignment(derefType, size, std140); if (std140) alignment = std::max(baseAlignmentVec4Std140, alignment); align(size, alignment); if (type.getQualifier().layoutMatrix == ElmRowMajor) size *= type.getMatrixRows(); else size *= type.getMatrixCols(); return alignment; } assert(0); // all cases should be covered above size = baseAlignmentVec4Std140; return baseAlignmentVec4Std140; } // Calculate the offset of a block member, using the recursively defined // block offset rules. int getBlockMemberOffset(const TType& blockType, int index) { // TODO: reflection performance: cache intermediate results instead of recomputing them int offset = 0; const TTypeList& memberList = *blockType.getStruct(); int memberSize; for (int m = 0; m < index; ++m) { int memberAlignment = getBaseAlignment(*memberList[m].type, memberSize, blockType.getQualifier().layoutPacking == ElpStd140); align(offset, memberAlignment); offset += memberSize; } int memberAlignment = getBaseAlignment(*memberList[index].type, memberSize, blockType.getQualifier().layoutPacking == ElpStd140); align(offset, memberAlignment); return offset; } // Calculate the block data size. // Arrayness is not taken into account, each element is backed by a separate buffer. int getBlockSize(const TType& blockType) { int size = 0; const TTypeList& memberList = *blockType.getStruct(); int memberSize; for (size_t m = 0; m < memberList.size(); ++m) { int memberAlignment = getBaseAlignment(*memberList[m].type, memberSize, blockType.getQualifier().layoutPacking == ElpStd140); align(size, memberAlignment); size += memberSize; } return size; } // Add a complex uniform reference where blocks/struct/arrays are involved in the access. // Handles the situation where the left node is at too coarse a granularity to be a single // uniform, while the result of the operation at 'node' is at the right granuarity. // // Note: Simpler things like the following are already handled elsewhere: // - a simple non-array, non-struct variable // - a variable that's an array of non-struct // // So, this code is for cases like // - a struct/block holding a member (member is array or not) // - an array of struct // - structs/arrays containing the above // void addDereferencedUniform(TIntermSymbol* base, TIntermBinary* topNode) { int offset = -1; int blockIndex = -1; bool anonymous = false; // See if we need to record the block itself bool block = base->getBasicType() == EbtBlock; if (block) { // TODO: how is an array of blocks handled differently? anonymous = base->getName().compare(0, 6, "__anon") == 0; const TString& blockName = anonymous ? base->getType().getTypeName() : base->getName(); TReflection::TNameToIndex::const_iterator it = reflection.nameToIndex.find(blockName); if (it == reflection.nameToIndex.end()) { blockIndex = reflection.indexToUniformBlock.size(); reflection.nameToIndex[blockName] = blockIndex; reflection.indexToUniformBlock.push_back(TObjectReflection(blockName, offset, -1, getBlockSize(base->getType()), -1)); } else blockIndex = it->second; } // Process the dereference chain, backward, accumulating the pieces on a stack if (block) offset = 0; std::list derefs; for (TIntermBinary* visitNode = topNode; visitNode; visitNode = visitNode->getLeft()->getAsBinaryNode()) { int index; switch (visitNode->getOp()) { case EOpIndexIndirect: // TODO handle indirect references in mid-chain: enumerate all possibilities? derefs.push_back(TString("[") + String(0) + "]"); break; case EOpIndexDirect: // TODO: reflection: track the highest used index for an array, to reduce the array's size index = visitNode->getRight()->getAsConstantUnion()->getConstArray()[0].getIConst(); derefs.push_back(TString("[") + String(index) + "]"); break; case EOpIndexDirectStruct: index = visitNode->getRight()->getAsConstantUnion()->getConstArray()[0].getIConst(); if (block) offset += getBlockMemberOffset(visitNode->getLeft()->getType(), index); derefs.push_back(TString("")); if (visitNode->getLeft()->getAsSymbolNode() != base || ! anonymous) derefs.back().append("."); derefs.back().append((*visitNode->getLeft()->getType().getStruct())[index].type->getFieldName().c_str()); break; default: break; } } // Put the dereference chain together, forward (reversing the stack) TString name; if (! anonymous) name = base->getName(); while (! derefs.empty()) { name += derefs.back(); derefs.pop_back(); } if (name.size() > 0) { if (reflection.nameToIndex.find(name) == reflection.nameToIndex.end()) { reflection.nameToIndex[name] = reflection.indexToUniform.size(); reflection.indexToUniform.push_back(TObjectReflection(name, offset, mapToGlType(topNode->getType()), mapToGlArraySize(topNode->getType()), blockIndex)); } } } // // Given a function name, find its subroot in the tree, and push it onto the stack of // functions left to process. // void pushFunction(const TString& name) { TIntermSequence& globals = intermediate.getTreeRoot()->getAsAggregate()->getSequence(); for (unsigned int f = 0; f < globals.size(); ++f) { TIntermAggregate* candidate = globals[f]->getAsAggregate(); if (candidate && candidate->getOp() == EOpFunction && candidate->getName() == name) { functions.push_back(candidate); break; } } } // Are we at a level in a dereference chain at which individual active uniform queries are made? bool isReflectionGranularity(const TType& type) { return type.getBasicType() != EbtBlock && type.getBasicType() != EbtStruct; } // For a binary operation indexing into an aggregate, chase down the base of the aggregate. // Return 0 if the topology does not fit this situation. TIntermSymbol* findBase(const TIntermBinary* node) { TIntermSymbol *symbol = node->getLeft()->getAsSymbolNode(); if (symbol) return symbol; TIntermBinary* left = node->getLeft()->getAsBinaryNode(); if (! left) return 0; return findBase(left); } // // Translate a glslang sampler type into the GL API #define number. // int mapSamplerToGlType(TSampler sampler) { if (! sampler.image) { // a sampler... switch (sampler.type) { case EbtFloat: switch (sampler.dim) { case Esd1D: switch (sampler.shadow) { case false: return sampler.arrayed ? GL_SAMPLER_1D_ARRAY : GL_SAMPLER_1D; case true: return sampler.arrayed ? GL_SAMPLER_1D_ARRAY_SHADOW : GL_SAMPLER_1D_SHADOW; } case Esd2D: switch (sampler.ms) { case false: switch (sampler.shadow) { case false: return sampler.arrayed ? GL_SAMPLER_2D_ARRAY : GL_SAMPLER_2D; case true: return sampler.arrayed ? GL_SAMPLER_2D_ARRAY_SHADOW : GL_SAMPLER_2D_SHADOW; } case true: return sampler.arrayed ? GL_SAMPLER_2D_MULTISAMPLE_ARRAY : GL_SAMPLER_2D_MULTISAMPLE; } case Esd3D: return GL_SAMPLER_3D; case EsdCube: switch (sampler.shadow) { case false: return sampler.arrayed ? GL_SAMPLER_CUBE_MAP_ARRAY : GL_SAMPLER_CUBE; case true: return sampler.arrayed ? GL_SAMPLER_CUBE_MAP_ARRAY_SHADOW : GL_SAMPLER_CUBE_SHADOW; } case EsdRect: return sampler.shadow ? GL_SAMPLER_2D_RECT_SHADOW : GL_SAMPLER_2D_RECT; case EsdBuffer: return GL_SAMPLER_BUFFER; } case EbtInt: switch (sampler.dim) { case Esd1D: return sampler.arrayed ? GL_INT_SAMPLER_1D_ARRAY : GL_INT_SAMPLER_1D; case Esd2D: switch (sampler.ms) { case false: return sampler.arrayed ? GL_INT_SAMPLER_2D_ARRAY : GL_INT_SAMPLER_2D; case true: return sampler.arrayed ? GL_INT_SAMPLER_2D_MULTISAMPLE_ARRAY : GL_INT_SAMPLER_2D_MULTISAMPLE; } case Esd3D: return GL_INT_SAMPLER_3D; case EsdCube: return sampler.arrayed ? GL_INT_SAMPLER_CUBE_MAP_ARRAY : GL_INT_SAMPLER_CUBE; case EsdRect: return GL_INT_SAMPLER_2D_RECT; case EsdBuffer: return GL_INT_SAMPLER_BUFFER; } case EbtUint: switch (sampler.dim) { case Esd1D: return sampler.arrayed ? GL_UNSIGNED_INT_SAMPLER_1D_ARRAY : GL_UNSIGNED_INT_SAMPLER_1D; case Esd2D: switch (sampler.ms) { case false: return sampler.arrayed ? GL_UNSIGNED_INT_SAMPLER_2D_ARRAY : GL_UNSIGNED_INT_SAMPLER_2D; case true: return sampler.arrayed ? GL_UNSIGNED_INT_SAMPLER_2D_MULTISAMPLE_ARRAY : GL_UNSIGNED_INT_SAMPLER_2D_MULTISAMPLE; } case Esd3D: return GL_UNSIGNED_INT_SAMPLER_3D; case EsdCube: return sampler.arrayed ? GL_UNSIGNED_INT_SAMPLER_CUBE_MAP_ARRAY : GL_UNSIGNED_INT_SAMPLER_CUBE; case EsdRect: return GL_UNSIGNED_INT_SAMPLER_2D_RECT; case EsdBuffer: return GL_UNSIGNED_INT_SAMPLER_BUFFER; } default: return 0; } } else { // an image... switch (sampler.type) { case EbtFloat: switch (sampler.dim) { case Esd1D: return sampler.arrayed ? GL_IMAGE_1D_ARRAY : GL_IMAGE_1D; case Esd2D: switch (sampler.ms) { case false: return sampler.arrayed ? GL_IMAGE_2D_ARRAY : GL_IMAGE_2D; case true: return sampler.arrayed ? GL_IMAGE_2D_MULTISAMPLE_ARRAY : GL_IMAGE_2D_MULTISAMPLE; } case Esd3D: return GL_IMAGE_3D; case EsdCube: return sampler.arrayed ? GL_IMAGE_CUBE_MAP_ARRAY : GL_IMAGE_CUBE; case EsdRect: return GL_IMAGE_2D_RECT; case EsdBuffer: return GL_IMAGE_BUFFER; } case EbtInt: switch (sampler.dim) { case Esd1D: return sampler.arrayed ? GL_INT_IMAGE_1D_ARRAY : GL_INT_IMAGE_1D; case Esd2D: switch (sampler.ms) { case false: return sampler.arrayed ? GL_INT_IMAGE_2D_ARRAY : GL_INT_IMAGE_2D; case true: return sampler.arrayed ? GL_INT_IMAGE_2D_MULTISAMPLE_ARRAY : GL_INT_IMAGE_2D_MULTISAMPLE; } case Esd3D: return GL_INT_IMAGE_3D; case EsdCube: return sampler.arrayed ? GL_INT_IMAGE_CUBE_MAP_ARRAY : GL_INT_IMAGE_CUBE; case EsdRect: return GL_INT_IMAGE_2D_RECT; case EsdBuffer: return GL_INT_IMAGE_BUFFER; } case EbtUint: switch (sampler.dim) { case Esd1D: return sampler.arrayed ? GL_UNSIGNED_INT_IMAGE_1D_ARRAY : GL_UNSIGNED_INT_IMAGE_1D; case Esd2D: switch (sampler.ms) { case false: return sampler.arrayed ? GL_UNSIGNED_INT_IMAGE_2D_ARRAY : GL_UNSIGNED_INT_IMAGE_2D; case true: return sampler.arrayed ? GL_UNSIGNED_INT_IMAGE_2D_MULTISAMPLE_ARRAY : GL_UNSIGNED_INT_IMAGE_2D_MULTISAMPLE; } case Esd3D: return GL_UNSIGNED_INT_IMAGE_3D; case EsdCube: return sampler.arrayed ? GL_UNSIGNED_INT_IMAGE_CUBE_MAP_ARRAY : GL_UNSIGNED_INT_IMAGE_CUBE; case EsdRect: return GL_UNSIGNED_INT_IMAGE_2D_RECT; case EsdBuffer: return GL_UNSIGNED_INT_IMAGE_BUFFER; } default: return 0; } } } // // Translate a glslang type into the GL API #define number. // Ignores arrayness. // int mapToGlType(const TType& type) { switch (type.getBasicType()) { case EbtSampler: return mapSamplerToGlType(type.getSampler()); case EbtStruct: case EbtBlock: case EbtVoid: return 0; default: break; } if (type.isVector()) { int offset = type.getVectorSize() - 2; switch (type.getBasicType()) { case EbtFloat: return GL_FLOAT_VEC2 + offset; case EbtDouble: return GL_DOUBLE_VEC2 + offset; case EbtInt: return GL_INT_VEC2 + offset; case EbtUint: return GL_UNSIGNED_INT_VEC2 + offset; case EbtBool: return GL_BOOL_VEC2 + offset; default: return 0; } } if (type.isMatrix()) { switch (type.getBasicType()) { case EbtFloat: switch (type.getMatrixCols()) { case 2: switch (type.getMatrixRows()) { case 2: return GL_FLOAT_MAT2; case 3: return GL_FLOAT_MAT2x3; case 4: return GL_FLOAT_MAT2x4; default: return 0; } case 3: switch (type.getMatrixRows()) { case 2: return GL_FLOAT_MAT3x2; case 3: return GL_FLOAT_MAT3; case 4: return GL_FLOAT_MAT3x4; default: return 0; } case 4: switch (type.getMatrixRows()) { case 2: return GL_FLOAT_MAT4x2; case 3: return GL_FLOAT_MAT4x3; case 4: return GL_FLOAT_MAT4; default: return 0; } } case EbtDouble: switch (type.getMatrixCols()) { case 2: switch (type.getMatrixRows()) { case 2: return GL_DOUBLE_MAT2; case 3: return GL_DOUBLE_MAT2x3; case 4: return GL_DOUBLE_MAT2x4; default: return 0; } case 3: switch (type.getMatrixRows()) { case 2: return GL_DOUBLE_MAT3x2; case 3: return GL_DOUBLE_MAT3; case 4: return GL_DOUBLE_MAT3x4; default: return 0; } case 4: switch (type.getMatrixRows()) { case 2: return GL_DOUBLE_MAT4x2; case 3: return GL_DOUBLE_MAT4x3; case 4: return GL_DOUBLE_MAT4; default: return 0; } } default: return 0; } } if (type.getVectorSize() == 1) { switch (type.getBasicType()) { case EbtFloat: return GL_FLOAT; case EbtDouble: return GL_DOUBLE; case EbtInt: return GL_INT; case EbtUint: return GL_UNSIGNED_INT; case EbtBool: return GL_BOOL; default: return 0; } } return 0; } int mapToGlArraySize(const TType& type) { return type.isArray() ? type.getArraySize() : 1; } typedef std::list TFunctionStack; TFunctionStack functions; const TIntermediate& intermediate; TReflection& reflection; }; const int TLiveTraverser::baseAlignmentVec4Std140 = 16; namespace { // // Implement the traversal functions of interest. // // To catch which function calls are not dead, and hence which functions must be visited. bool LiveAggregate(bool /* preVisit */, TIntermAggregate* node, TIntermTraverser* it) { TLiveTraverser* oit = static_cast(it); if (node->getOp() == EOpFunctionCall) oit->addFunctionCall(node); return true; // traverse this subtree } // To catch dereferenced aggregates that must be reflected. bool LiveBinary(bool /* preVisit */, TIntermBinary* node, TIntermTraverser* it) { TLiveTraverser* oit = static_cast(it); switch (node->getOp()) { case EOpIndexDirect: case EOpIndexIndirect: case EOpIndexDirectStruct: // If the left side is already small enough granularity to report, ignore // this operation, and pick it up when the left side is visited. if (! oit->isReflectionGranularity(node->getLeft()->getType()) && oit->isReflectionGranularity(node->getType())) { // right granularity; see if this really is a uniform-based dereference, // and if so, process it TIntermSymbol* base = oit->findBase(node); if (base && base->getQualifier().storage == EvqUniform) oit->addDereferencedUniform(base, node); } default: break; } return true; // still need to visit everything below } // To catch non-dereferenced objects that must be reflected. void LiveSymbol(TIntermSymbol* symbol, TIntermTraverser* it) { TLiveTraverser* oit = static_cast(it); if (symbol->getQualifier().storage == EvqUniform) oit->addUniform(*symbol); } // To prune semantically dead paths. bool LiveSelection(bool /* preVisit */, TIntermSelection* node, TIntermTraverser* it) { TLiveTraverser* oit = static_cast(it); TIntermConstantUnion* constant = node->getCondition()->getAsConstantUnion(); if (constant) { // cull the path that is dead if (constant->getConstArray()[0].getBConst() == true && node->getTrueBlock()) node->getTrueBlock()->traverse(it); if (constant->getConstArray()[0].getBConst() == false && node->getFalseBlock()) node->getFalseBlock()->traverse(it); return false; // don't traverse any more, we did it all above } else return true; // traverse the whole subtree } } // end anonymous namespace // // Implement TReflection methods. // // Merge live symbols from 'intermediate' into the existing reflection database. // // Returns false if the input is too malformed to do this. bool TReflection::addStage(EShLanguage, const TIntermediate& intermediate) { if (intermediate.getNumMains() != 1 || intermediate.isRecursive()) return false; TLiveTraverser it(intermediate, *this); it.visitSymbol = LiveSymbol; it.visitSelection = LiveSelection; it.visitBinary = LiveBinary; it.visitAggregate = LiveAggregate; // put main() on functions to process it.pushFunction("main("); // process all the functions while (! it.functions.empty()) { TIntermNode* function = it.functions.back(); it.functions.pop_back(); function->traverse(&it); } return true; } void TReflection::dump() { printf("Uniform reflection:\n"); for (size_t i = 0; i < indexToUniform.size(); ++i) indexToUniform[i].dump(); printf("\n"); printf("Uniform block reflection:\n"); for (size_t i = 0; i < indexToUniformBlock.size(); ++i) indexToUniformBlock[i].dump(); printf("\n"); //printf("Live names\n"); //for (TNameToIndex::const_iterator it = nameToIndex.begin(); it != nameToIndex.end(); ++it) // printf("%s: %d\n", it->first.c_str(), it->second); //printf("\n"); } } // end namespace glslang