1 #ifndef _RSGVARIABLEMANAGER_HPP
2 #define _RSGVARIABLEMANAGER_HPP
3 /*-------------------------------------------------------------------------
4 * drawElements Quality Program Random Shader Generator
5 * ----------------------------------------------------
7 * Copyright 2014 The Android Open Source Project
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
23 * \brief Variable manager.
26 * Variable manager owns variable objects until they are either explictly
27 * removed or moved to currently active scope. After that the ownership
28 * is transferred to Scope or the object that called removeEntry().
29 *//*--------------------------------------------------------------------*/
31 #include "rsgDefs.hpp"
32 #include "rsgVariable.hpp"
33 #include "rsgVariableValue.hpp"
34 #include "rsgNameAllocator.hpp"
46 ValueEntry (const Variable* variable);
49 const Variable* getVariable (void) const { return m_variable; }
51 ConstValueRangeAccess getValueRange (void) const { return m_valueRange.asAccess(); }
52 ValueRangeAccess getValueRange (void) { return m_valueRange.asAccess(); }
55 const Variable* m_variable;
56 ValueRange m_valueRange;
59 // Variable scope manages variable allocation.
64 ~VariableScope (void);
66 Variable* allocate (const VariableType& type, Variable::Storage storage, const char* name);
67 void declare (Variable* variable); //!< Move from live set to declared set
68 void removeLive (const Variable* variable); //!< Just remove from live set (when migrating to parent).
70 const std::vector<Variable*>& getDeclaredVariables (void) const { return m_declaredVariables; }
72 std::vector<Variable*>& getLiveVariables (void) { return m_liveVariables; }
73 const std::vector<Variable*>& getLiveVariables (void) const { return m_liveVariables; }
76 VariableScope (const VariableScope& other);
77 VariableScope& operator= (const VariableScope& other);
79 std::vector<Variable*> m_declaredVariables; //!< Variables declared in this scope. Not available for expressions.
80 std::vector<Variable*> m_liveVariables; //!< Live variables (available for expression) that can be declared in this scope.
89 ValueEntry* allocate (const Variable* variable);
90 ValueEntry* findEntry (const Variable* variable) const;
91 void setValue (const Variable* variable, ConstValueRangeAccess value);
92 void removeValue (const Variable* variable);
94 std::vector<ValueEntry*>& getValues (void) { return m_entries; }
95 const std::vector<ValueEntry*>& getValues (void) const { return m_entries; }
100 ValueScope (const ValueScope& other);
101 ValueScope& operator= (const ValueScope& other);
103 std::vector<ValueEntry*> m_entries;
106 class ReservedScalars
111 ReservedScalars (void)
117 // \todo [2011-05-26 pyry] Clean up this a bit, separate const variant.
118 template <typename Item, typename Iterator, class Filter>
119 class FilteredIterator : public std::iterator<std::input_iterator_tag, Item>
122 FilteredIterator (Iterator iter, Iterator end, Filter filter)
129 FilteredIterator operator+ (ptrdiff_t offset) const
131 Iterator nextEntry = m_iter;
133 nextEntry = findNext(m_filter, nextEntry, m_end);
134 return FilteredIterator(nextEntry, m_end, m_filter);
137 FilteredIterator& operator++ ()
140 m_iter = findNext(m_filter, m_iter, m_end);
144 FilteredIterator operator++ (int)
147 FilteredIterator copy = *this;
148 m_iter = findNext(m_filter, m_iter, m_end);
152 bool operator== (const FilteredIterator& other) const
154 return m_iter == other.m_iter;
157 bool operator!= (const FilteredIterator& other) const
159 return m_iter != other.m_iter;
162 const Item& operator* (void)
164 DE_ASSERT(m_iter != m_end);
165 DE_ASSERT(m_filter(*m_iter));
170 static Iterator findNext (Filter filter, Iterator iter, Iterator end)
174 while (iter != end && !filter(*iter));
183 template <class Filter>
184 class ValueEntryIterator : public FilteredIterator<const ValueEntry*, std::vector<const ValueEntry*>::const_iterator, Filter>
187 ValueEntryIterator (std::vector<const ValueEntry*>::const_iterator begin, std::vector<const ValueEntry*>::const_iterator end, Filter filter)
188 : FilteredIterator<const ValueEntry*, std::vector<const ValueEntry*>::const_iterator, Filter>(begin, end, filter)
193 class VariableManager
196 VariableManager (NameAllocator& nameAllocator);
197 ~VariableManager (void);
199 int getNumAllocatedScalars (void) const { return m_numAllocatedScalars; }
200 int getNumAllocatedShaderInScalars (void) const { return m_numAllocatedShaderInScalars; }
201 int getNumAllocatedShaderInVariables(void) const { return m_numAllocatedShaderInVariables; }
202 int getNumAllocatedUniformScalars (void) const { return m_numAllocatedUniformScalars; }
204 void reserve (ReservedScalars& store, int numScalars);
205 void release (ReservedScalars& store);
207 Variable* allocate (const VariableType& type);
208 Variable* allocate (const VariableType& type, Variable::Storage storage, const char* name);
210 void setStorage (Variable* variable, Variable::Storage storage);
212 void setValue (const Variable* variable, ConstValueRangeAccess value);
213 const ValueEntry* getValue (const Variable* variable) const;
214 const ValueEntry* getParentValue (const Variable* variable) const;
216 void removeValueFromCurrentScope (const Variable* variable);
218 void declareVariable (Variable* variable);
219 bool canDeclareInCurrentScope (const Variable* variable) const;
220 const std::vector<Variable*>& getLiveVariables (void) const;
222 void pushVariableScope (VariableScope& scope);
223 void popVariableScope (void);
225 void pushValueScope (ValueScope& scope);
226 void popValueScope (void);
228 template <class Filter>
229 ValueEntryIterator<Filter> getBegin (Filter filter = Filter()) const;
231 template <class Filter>
232 ValueEntryIterator<Filter> getEnd (Filter filter = Filter()) const;
234 template <class Filter>
235 bool hasEntry (Filter filter = Filter()) const;
238 VariableManager (const VariableManager& other);
239 VariableManager& operator= (const VariableManager& other);
241 VariableScope& getCurVariableScope (void) { return *m_variableScopeStack.back(); }
242 const VariableScope& getCurVariableScope (void) const { return *m_variableScopeStack.back(); }
244 ValueScope& getCurValueScope (void) { return *m_valueScopeStack.back(); }
245 const ValueScope& getCurValueScope (void) const { return *m_valueScopeStack.back(); }
247 std::vector<VariableScope*> m_variableScopeStack;
248 std::vector<ValueScope*> m_valueScopeStack;
250 std::vector<const ValueEntry*> m_entryCache; //!< For faster value entry access.
252 int m_numAllocatedScalars;
253 int m_numAllocatedShaderInScalars;
254 int m_numAllocatedShaderInVariables;
255 int m_numAllocatedUniformScalars;
256 NameAllocator& m_nameAllocator;
259 template <class Filter>
260 ValueEntryIterator<Filter> VariableManager::getBegin (Filter filter) const
262 std::vector<const ValueEntry*>::const_iterator first = m_entryCache.begin();
263 while (first != m_entryCache.end() && !filter(*first))
265 return ValueEntryIterator<Filter>(first, m_entryCache.end(), filter);
268 template <class Filter>
269 ValueEntryIterator<Filter> VariableManager::getEnd (Filter filter) const
271 return ValueEntryIterator<Filter>(m_entryCache.end(), m_entryCache.end(), filter);
274 template <class Filter>
275 bool VariableManager::hasEntry (Filter filter) const
277 for (std::vector<const ValueEntry*>::const_iterator i = m_entryCache.begin(); i != m_entryCache.end(); i++)
290 typedef ValueEntryIterator<AnyEntry> Iterator;
292 bool operator() (const ValueEntry* entry) const
299 class IsWritableEntry
302 bool operator() (const ValueEntry* entry) const
304 switch (entry->getVariable()->getStorage())
306 case Variable::STORAGE_LOCAL:
307 case Variable::STORAGE_SHADER_OUT:
308 case Variable::STORAGE_PARAMETER_IN:
309 case Variable::STORAGE_PARAMETER_OUT:
310 case Variable::STORAGE_PARAMETER_INOUT:
319 template <Variable::Storage Storage>
320 class EntryStorageFilter
323 typedef ValueEntryIterator<EntryStorageFilter<Storage> > Iterator;
325 bool operator() (const ValueEntry* entry) const
327 return entry->getVariable()->getStorage() == Storage;
331 typedef EntryStorageFilter<Variable::STORAGE_LOCAL> LocalEntryFilter;
332 typedef EntryStorageFilter<Variable::STORAGE_SHADER_IN> ShaderInEntryFilter;
333 typedef EntryStorageFilter<Variable::STORAGE_SHADER_OUT> ShaderOutEntryFilter;
337 #endif // _RSGVARIABLEMANAGER_HPP