2 // Copyright (C) 2013-2016 LunarG, Inc.
4 // All rights reserved.
6 // Redistribution and use in source and binary forms, with or without
7 // modification, are permitted provided that the following conditions
10 // Redistributions of source code must retain the above copyright
11 // notice, this list of conditions and the following disclaimer.
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.
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.
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.
36 #ifndef _REFLECTION_INCLUDED
37 #define _REFLECTION_INCLUDED
39 #include "../Public/ShaderLang.h"
40 #include "../Include/Types.h"
46 // A reflection database and its interface, consistent with the OpenGL API reflection queries.
52 class TIntermAggregate;
53 class TReflectionTraverser;
55 // The full reflection database
58 TReflection(EShReflectionOptions opts, EShLanguage first, EShLanguage last)
59 : options(opts), firstStage(first), lastStage(last), badReflection(TObjectReflection::badReflection())
61 for (int dim=0; dim<3; ++dim)
65 virtual ~TReflection() {}
67 // grow the reflection stage by stage
68 bool addStage(EShLanguage, const TIntermediate&);
70 // for mapping a uniform index to a uniform object's description
71 int getNumUniforms() { return (int)indexToUniform.size(); }
72 const TObjectReflection& getUniform(int i) const
74 if (i >= 0 && i < (int)indexToUniform.size())
75 return indexToUniform[i];
80 // for mapping a block index to the block's description
81 int getNumUniformBlocks() const { return (int)indexToUniformBlock.size(); }
82 const TObjectReflection& getUniformBlock(int i) const
84 if (i >= 0 && i < (int)indexToUniformBlock.size())
85 return indexToUniformBlock[i];
90 // for mapping an pipeline input index to the input's description
91 int getNumPipeInputs() { return (int)indexToPipeInput.size(); }
92 const TObjectReflection& getPipeInput(int i) const
94 if (i >= 0 && i < (int)indexToPipeInput.size())
95 return indexToPipeInput[i];
100 // for mapping an pipeline output index to the output's description
101 int getNumPipeOutputs() { return (int)indexToPipeOutput.size(); }
102 const TObjectReflection& getPipeOutput(int i) const
104 if (i >= 0 && i < (int)indexToPipeOutput.size())
105 return indexToPipeOutput[i];
107 return badReflection;
110 // for mapping any name to its index (block names, uniform names and input/output names)
111 int getIndex(const char* name) const
113 TNameToIndex::const_iterator it = nameToIndex.find(name);
114 if (it == nameToIndex.end())
120 // see getIndex(const char*)
121 int getIndex(const TString& name) const { return getIndex(name.c_str()); }
124 unsigned getLocalSize(int dim) const { return dim <= 2 ? localSize[dim] : 0; }
129 friend class glslang::TReflectionTraverser;
131 void buildCounterIndices(const TIntermediate&);
132 void buildUniformStageMask(const TIntermediate& intermediate);
133 void buildAttributeReflection(EShLanguage, const TIntermediate&);
135 // Need a TString hash: typedef std::unordered_map<TString, int> TNameToIndex;
136 typedef std::map<std::string, int> TNameToIndex;
137 typedef std::vector<TObjectReflection> TMapIndexToReflection;
139 EShReflectionOptions options;
141 EShLanguage firstStage;
142 EShLanguage lastStage;
144 TObjectReflection badReflection; // return for queries of -1 or generally out of range; has expected descriptions with in it for this
145 TNameToIndex nameToIndex; // maps names to indexes; can hold all types of data: uniform/buffer and which function names have been processed
146 TMapIndexToReflection indexToUniform;
147 TMapIndexToReflection indexToUniformBlock;
148 TMapIndexToReflection indexToPipeInput;
149 TMapIndexToReflection indexToPipeOutput;
151 unsigned int localSize[3];
154 } // end namespace glslang
156 #endif // _REFLECTION_INCLUDED