GlslVariableMap.hpp

00001 // Sh: A GPU metaprogramming language.
00002 //
00003 // Copyright 2003-2006 Serious Hack Inc.
00004 // 
00005 // This library is free software; you can redistribute it and/or
00006 // modify it under the terms of the GNU Lesser General Public
00007 // License as published by the Free Software Foundation; either
00008 // version 2.1 of the License, or (at your option) any later version.
00009 //
00010 // This library is distributed in the hope that it will be useful,
00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013 // Lesser General Public License for more details.
00014 //
00015 // You should have received a copy of the GNU Lesser General Public
00016 // License along with this library; if not, write to the Free Software
00017 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 
00018 // MA  02110-1301, USA
00020 #ifndef GLSLVARIABLEMAP_HPP
00021 #define GLSLVARIABLEMAP_HPP
00022 
00023 #include "Glsl.hpp"
00024 #include "ShVariableNode.hpp"
00025 #include "ShProgramNode.hpp"
00026 #include "ShInternals.hpp"
00027 #include "GlslVariable.hpp"
00028 #include <map>
00029 #include <string>
00030 #include <list>
00031 #include <string>
00032 
00033 namespace shgl {
00034 
00035 struct GlslBindingSpecs {
00036   GlslVarBinding binding;
00037   int max_bindings;
00038   SH::ShSemanticType semantic_type;
00039   bool allow_generic;
00040 };
00041 
00042 struct GlslVariableDeclaration {
00043   std::string declaration;
00044   std::string sh_name;
00045 };
00046 
00047 class GlslVariableMap {
00048 public:
00049   typedef std::list<GlslVariableDeclaration> DeclarationList;
00050   typedef std::list<SH::ShVariableNodePtr> NodeList;
00051 
00052 private:
00053   typedef std::map<const SH::ShVariableNodePtr, GlslVariable> VarMap;
00054 
00055   SH::ShProgramNode* m_shader;
00056   SH::ShVarTransformMap* m_original_vars;
00057   GlslProgramType m_unit;
00058 
00059   unsigned m_nb_uniform_variables;
00060   unsigned m_nb_regular_variables;  
00061   VarMap m_varmap;
00062   NodeList m_nodes;
00063   
00064   DeclarationList m_attribute_declarations;
00065   DeclarationList m_uniform_declarations;
00066   DeclarationList m_regular_declarations;
00067 
00068   std::map<GlslVarBinding, int> m_input_bindings;
00069   std::map<GlslVarBinding, int> m_output_bindings;
00070 
00071   void allocate_builtin(const SH::ShVariableNodePtr& node,
00072                         const GlslBindingSpecs* specs, std::map<GlslVarBinding, int>& bindings,
00073                         bool generic);
00074   void allocate_builtin_inputs();
00075   void allocate_builtin_outputs();
00076   void allocate_generic_vertex_input(const SH::ShVariableNodePtr& node, int index);
00077   void allocate_generic_vertex_inputs();
00078   void allocate_temp(const SH::ShVariableNodePtr& node);
00079   
00080   void map_insert(const SH::ShVariableNodePtr& node, GlslVariable var);
00081 
00082   std::string real_resolve(const SH::ShVariable& v, const std::string& array_index, int index);
00083   std::string swizzle(const SH::ShVariable& v, int var_size) const;
00084   std::string repeat_scalar(const std::string& name, SH::ShValueType type, int size) const;
00085 
00086 public:
00087   GlslVariableMap(SH::ShProgramNode* shader, SH::ShVarTransformMap* original_vars, 
00088                   GlslProgramType unit);
00089   ~GlslVariableMap();
00090 
00091   std::string resolve(const SH::ShVariable& v, int index = -1);
00092   std::string resolve(const SH::ShVariable& v, const SH::ShVariable& index);
00093 
00094   const GlslVariable& variable(const SH::ShVariableNodePtr& node)
00095   {
00096     return m_varmap[node];
00097   }
00098 
00099   bool contains(const SH::ShVariableNodePtr& node) const
00100   {
00101     return (m_varmap.find(node) != m_varmap.end());
00102   }
00103 
00109   bool contains(const SH::ShVariableNodePtr& node, const GlslVariable*& var) const
00110   {
00111     VarMap::const_iterator i = m_varmap.find(node);
00112     if (i != m_varmap.end()) {
00113       var = &i->second;
00114       return true;
00115     }
00116     else {
00117       return false;
00118     }
00119   }
00120 
00121   DeclarationList::const_iterator attribute_begin() const;
00122   DeclarationList::const_iterator attribute_end() const;
00123   DeclarationList::const_iterator uniform_begin() const;
00124   DeclarationList::const_iterator uniform_end() const;
00125   DeclarationList::const_iterator regular_begin() const;
00126   DeclarationList::const_iterator regular_end() const;
00127   
00128   NodeList::iterator node_begin() { return m_nodes.begin(); }
00129   NodeList::iterator node_end() { return m_nodes.end(); }
00130 };
00131 
00132 }
00133 
00134 #endif /* GLSLVARIABLEMAP_HPP */

Generated on Thu Feb 16 14:51:29 2006 for Sh by  doxygen 1.4.6