1 // 2 //Copyright (C) 2013 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 41 #include <list> 42 #include <set> 43 44 // 45 // A reflection database and its interface, consistent with the OpenGL API reflection queries. 46 // 47 48 namespace glslang { 49 50 class TIntermediate; 51 class TIntermAggregate; 52 class TLiveTraverser; 53 54 // Data needed for just a single object at the granularity exchanged by the reflection API 55 class TObjectReflection { 56 public: 57 TObjectReflection(const TString& pName, int pOffset, int pGLDefineType, int pSize, int pIndex) : 58 name(pName), offset(pOffset), glDefineType(pGLDefineType), size(pSize), index(pIndex) { } 59 void dump() const { printf("%s: offset %d, type %x, size %d, index %d\n", name.c_str(), offset, glDefineType, size, index); } 60 TString name; 61 int offset; 62 int glDefineType; 63 int size; // data size in bytes for a block, array size for a (non-block) object that's an array 64 int index; 65 }; 66 67 // The full reflection database 68 class TReflection { 69 public: 70 TReflection() : badReflection("__bad__", -1, -1, -1, -1) {} 71 virtual ~TReflection() {} 72 73 // grow the reflection stage by stage 74 bool addStage(EShLanguage, const TIntermediate&); 75 76 // for mapping a uniform index to a uniform object's description 77 int getNumUniforms() { return (int)indexToUniform.size(); } 78 const TObjectReflection& getUniform(int i) const 79 { 80 if (i >= 0 && i < (int)indexToUniform.size()) 81 return indexToUniform[i]; 82 else 83 return badReflection; 84 } 85 86 // for mapping a block index to the block's description 87 int getNumUniformBlocks() const { return (int)indexToUniformBlock.size(); } 88 const TObjectReflection& getUniformBlock(int i) const 89 { 90 if (i >= 0 && i < (int)indexToUniformBlock.size()) 91 return indexToUniformBlock[i]; 92 else 93 return badReflection; 94 } 95 96 // for mapping an attribute index to the attribute's description 97 int getNumAttributes() { return (int)indexToAttribute.size(); } 98 const TObjectReflection& getAttribute(int i) const 99 { 100 if (i >= 0 && i < (int)indexToAttribute.size()) 101 return indexToAttribute[i]; 102 else 103 return badReflection; 104 } 105 106 // for mapping any name to its index (block names, uniform names and attribute names) 107 int getIndex(const char* name) const 108 { 109 TNameToIndex::const_iterator it = nameToIndex.find(name); 110 if (it == nameToIndex.end()) 111 return -1; 112 else 113 return it->second; 114 } 115 116 void dump(); 117 118 protected: 119 friend class glslang::TLiveTraverser; 120 121 // Need a TString hash: typedef std::unordered_map<TString, int> TNameToIndex; 122 typedef std::map<TString, int> TNameToIndex; 123 typedef std::vector<TObjectReflection> TMapIndexToReflection; 124 125 TObjectReflection badReflection; // return for queries of -1 or generally out of range; has expected descriptions with in it for this 126 TNameToIndex nameToIndex; // maps names to indexes; can hold all types of data: uniform/buffer and which function names have been processed 127 TMapIndexToReflection indexToUniform; 128 TMapIndexToReflection indexToUniformBlock; 129 TMapIndexToReflection indexToAttribute; 130 }; 131 132 } // end namespace glslang 133 134 #endif // _REFLECTION_INCLUDED 135