1 // 2 // Copyright (C) 2013-2016 LunarG, Inc. 3 // 4 // All rights reserved. 5 // 6 // Redistribution and use in source and binary forms, with or without 7 // modification, are permitted provided that the following conditions 8 // are met: 9 // 10 // Redistributions of source code must retain the above copyright 11 // notice, this list of conditions and the following disclaimer. 12 // 13 // Redistributions in binary form must reproduce the above 14 // copyright notice, this list of conditions and the following 15 // disclaimer in the documentation and/or other materials provided 16 // with the distribution. 17 // 18 // Neither the name of 3Dlabs Inc. Ltd. nor the names of its 19 // contributors may be used to endorse or promote products derived 20 // from this software without specific prior written permission. 21 // 22 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 23 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 24 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 25 // FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 26 // COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 27 // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 28 // BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 29 // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 30 // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 31 // LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 32 // ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 33 // POSSIBILITY OF SUCH DAMAGE. 34 // 35 36 #ifndef _REFLECTION_INCLUDED 37 #define _REFLECTION_INCLUDED 38 39 #include "../Public/ShaderLang.h" 40 #include "../Include/Types.h" 41 42 #include <list> 43 #include <set> 44 45 // 46 // A reflection database and its interface, consistent with the OpenGL API reflection queries. 47 // 48 49 namespace glslang { 50 51 class TIntermediate; 52 class TIntermAggregate; 53 class TReflectionTraverser; 54 55 // Data needed for just a single object at the granularity exchanged by the reflection API 56 class TObjectReflection { 57 public: 58 TObjectReflection(const TString& pName, const TType& pType, int pOffset, int pGLDefineType, int pSize, int pIndex) : 59 name(pName), offset(pOffset), 60 glDefineType(pGLDefineType), size(pSize), index(pIndex), counterIndex(-1), type(pType.clone()) { } 61 62 const TType* const getType() const { return type; } 63 int getBinding() const 64 { 65 if (type == nullptr || !type->getQualifier().hasBinding()) 66 return -1; 67 return type->getQualifier().layoutBinding; 68 } 69 void dump() const 70 { 71 printf("%s: offset %d, type %x, size %d, index %d, binding %d", 72 name.c_str(), offset, glDefineType, size, index, getBinding() ); 73 74 if (counterIndex != -1) 75 printf(", counter %d", counterIndex); 76 77 printf("\n"); 78 } 79 static TObjectReflection badReflection() { return TObjectReflection(); } 80 81 TString name; 82 int offset; 83 int glDefineType; 84 int size; // data size in bytes for a block, array size for a (non-block) object that's an array 85 int index; 86 int counterIndex; 87 88 protected: 89 TObjectReflection() : offset(-1), glDefineType(-1), size(-1), index(-1), type(nullptr) { } 90 91 const TType* type; 92 }; 93 94 // The full reflection database 95 class TReflection { 96 public: 97 TReflection() : badReflection(TObjectReflection::badReflection()) 98 { 99 for (int dim=0; dim<3; ++dim) 100 localSize[dim] = 0; 101 } 102 103 virtual ~TReflection() {} 104 105 // grow the reflection stage by stage 106 bool addStage(EShLanguage, const TIntermediate&); 107 108 // for mapping a uniform index to a uniform object's description 109 int getNumUniforms() { return (int)indexToUniform.size(); } 110 const TObjectReflection& getUniform(int i) const 111 { 112 if (i >= 0 && i < (int)indexToUniform.size()) 113 return indexToUniform[i]; 114 else 115 return badReflection; 116 } 117 118 // for mapping a block index to the block's description 119 int getNumUniformBlocks() const { return (int)indexToUniformBlock.size(); } 120 const TObjectReflection& getUniformBlock(int i) const 121 { 122 if (i >= 0 && i < (int)indexToUniformBlock.size()) 123 return indexToUniformBlock[i]; 124 else 125 return badReflection; 126 } 127 128 // for mapping an attribute index to the attribute's description 129 int getNumAttributes() { return (int)indexToAttribute.size(); } 130 const TObjectReflection& getAttribute(int i) const 131 { 132 if (i >= 0 && i < (int)indexToAttribute.size()) 133 return indexToAttribute[i]; 134 else 135 return badReflection; 136 } 137 138 // for mapping any name to its index (block names, uniform names and attribute names) 139 int getIndex(const char* name) const 140 { 141 TNameToIndex::const_iterator it = nameToIndex.find(name); 142 if (it == nameToIndex.end()) 143 return -1; 144 else 145 return it->second; 146 } 147 148 // see getIndex(const char*) 149 int getIndex(const TString& name) const { return getIndex(name.c_str()); } 150 151 // Thread local size 152 unsigned getLocalSize(int dim) const { return dim <= 2 ? localSize[dim] : 0; } 153 154 void dump(); 155 156 protected: 157 friend class glslang::TReflectionTraverser; 158 159 void buildCounterIndices(); 160 void buildAttributeReflection(EShLanguage, const TIntermediate&); 161 162 // Need a TString hash: typedef std::unordered_map<TString, int> TNameToIndex; 163 typedef std::map<TString, int> TNameToIndex; 164 typedef std::vector<TObjectReflection> TMapIndexToReflection; 165 166 TObjectReflection badReflection; // return for queries of -1 or generally out of range; has expected descriptions with in it for this 167 TNameToIndex nameToIndex; // maps names to indexes; can hold all types of data: uniform/buffer and which function names have been processed 168 TMapIndexToReflection indexToUniform; 169 TMapIndexToReflection indexToUniformBlock; 170 TMapIndexToReflection indexToAttribute; 171 172 unsigned int localSize[3]; 173 }; 174 175 } // end namespace glslang 176 177 #endif // _REFLECTION_INCLUDED 178