RetroArch
reflection.h
Go to the documentation of this file.
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
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  }
80 
82  int offset;
84  int size; // data size in bytes for a block, array size for a (non-block) object that's an array
85  int index;
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:
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:
158 
159  void buildCounterIndices(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
171 
172  unsigned int localSize[3];
173 };
174 
175 } // end namespace glslang
176 
177 #endif // _REFLECTION_INCLUDED
Definition: reflection.h:56
TMapIndexToReflection indexToAttribute
Definition: reflection.h:170
GLuint const GLchar * name
Definition: glext.h:6671
const TObjectReflection & getAttribute(int i) const
Definition: reflection.h:130
EShLanguage
Definition: ShaderLang.h:90
unsigned int localSize[3]
Definition: reflection.h:172
int getIndex(const TString &name) const
Definition: reflection.h:149
const TType *const getType() const
Definition: reflection.h:62
int getNumUniformBlocks() const
Definition: reflection.h:119
static TObjectReflection badReflection()
Definition: reflection.h:79
TMapIndexToReflection indexToUniformBlock
Definition: reflection.h:169
TString name
Definition: reflection.h:81
GLsizeiptr size
Definition: glext.h:6559
Definition: localintermediate.h:210
int counterIndex
Definition: reflection.h:86
void buildAttributeReflection(EShLanguage, const TIntermediate &)
Definition: reflection.cpp:759
TNameToIndex nameToIndex
Definition: reflection.h:167
Definition: reflection.cpp:78
GLenum type
Definition: glext.h:6233
Definition: Types.h:1169
Definition: reflection.h:95
int getNumUniforms()
Definition: reflection.h:109
TObjectReflection()
Definition: reflection.h:89
int index
Definition: reflection.h:85
TMapIndexToReflection indexToUniform
Definition: reflection.h:168
const TObjectReflection & getUniform(int i) const
Definition: reflection.h:110
void dump() const
Definition: reflection.h:69
int getBinding() const
Definition: reflection.h:63
TObjectReflection(const TString &pName, const TType &pType, int pOffset, int pGLDefineType, int pSize, int pIndex)
Definition: reflection.h:58
bool addStage(EShLanguage, const TIntermediate &)
Definition: reflection.cpp:784
int size
Definition: reflection.h:84
int getIndex(const char *name) const
Definition: reflection.h:139
Definition: arrays.h:46
GLuint index
Definition: glext.h:6671
TObjectReflection badReflection
Definition: reflection.h:166
int offset
Definition: reflection.h:82
int glDefineType
Definition: reflection.h:83
const TType * type
Definition: reflection.h:91
TReflection()
Definition: reflection.h:97
const TObjectReflection & getUniformBlock(int i) const
Definition: reflection.h:120
std::map< TString, int > TNameToIndex
Definition: reflection.h:163
unsigned getLocalSize(int dim) const
Definition: reflection.h:152
std::vector< TObjectReflection > TMapIndexToReflection
Definition: reflection.h:164
GLintptr offset
Definition: glext.h:6560
int getNumAttributes()
Definition: reflection.h:129
Definition: lobject.h:303
virtual ~TReflection()
Definition: reflection.h:103
void dump()
Definition: reflection.cpp:810
void buildCounterIndices(const TIntermediate &)
Definition: reflection.cpp:769