Cc.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 SHCC_HPP
00021 #define SHCC_HPP
00022 
00023 #ifdef _WIN32
00024 #include <windows.h>
00025 #endif
00026 
00027 #include <map>
00028 #include <string>
00029 #include <sstream>
00030 
00031 #include "ShVariant.hpp"
00032 #include "ShBackend.hpp"
00033 #include "ShTransformer.hpp"
00034 
00035 // #define SH_CC_DEBUG 1
00036 
00037 // function representing a single kernel
00038 // Each of the parameters is an array of single storage-typed arrays
00039 // (The size and type are known during code emission)
00040 // @todo type - inputs should be allowed to change?
00041 extern "C" typedef void (*CcShaderFunc)(void** inputs, 
00042                                                           void** params,
00043                                                           void** channels,
00044                                                           void** textures,
00045                                                           void** outputs);
00046 
00047 namespace ShCc {
00048 
00049 struct CcVariable
00050 {
00051   CcVariable(void);
00052   CcVariable(int num, const std::string& name, int size, SH::ShValueType valueType);
00053 
00054   int m_num;
00055   std::string m_name;
00056   int m_size;
00057   SH::ShValueType m_valueType;
00058 };
00059 
00060 class CcBackendCode: public SH::ShBackendCode
00061 {
00062 public:
00063   CcBackendCode(const SH::ShProgramNodeCPtr& program);
00064   ~CcBackendCode(void);
00065 
00066   bool allocateRegister(const SH::ShVariableNodePtr& var);
00067   void freeRegister(const SH::ShVariableNodePtr& var);
00068     
00069   void upload(void);
00070   void bind(void);
00071   void unbind(void);
00072   void update(void);
00073     
00074   void updateUniform(const SH::ShVariableNodePtr& uniform);
00075     
00076   std::ostream& print(std::ostream& out);
00077   std::ostream& describe_interface(std::ostream& out);
00078   std::ostream& describe_bindings(std::ostream& out);
00079 
00080 protected:
00081   friend class CcBackend;
00082   bool generate(void);
00083   bool execute(SH::ShStream& dest);
00084 
00085 private:
00086 
00091   template<typename T>
00092   void allocate_varlist(const std::list<T> &varList, const char* varPrefix, const char* arrayName, const char* typePrefix=""); 
00093 
00094   // Allocates a variable names for different kinds of data 
00095   // and initializes the variable to index into the appropriate
00096   // element of an array passed into the CcShaderFunc.
00097   //
00098   // Spits out these variable declarations & assignments to m_code
00099   //
00100   // void* type of the array gets cast to the variable type
00101   // @{
00102   void allocate_consts(void);
00103   void allocate_inputs(void);
00104   void allocate_outputs(void);
00105   void allocate_channels(void);
00106   void allocate_textures(void);
00107   void allocate_uniforms(void);
00108   void allocate_temps(void);
00109   // @}
00110 
00111   std::string resolve(const SH::ShVariable& v);
00112   std::string resolve(const SH::ShVariable& v, int idx);
00113   const char* ctype(SH::ShValueType valueType);
00114 
00115   class LabelFunctor
00116   {
00117   public:
00118     LabelFunctor(std::map<SH::ShCtrlGraphNodePtr, int>& label_map);
00119         
00120     void operator()(SH::ShCtrlGraphNode* node);
00121         
00122   public:
00123     int m_cur_label;
00124     std::map<SH::ShCtrlGraphNodePtr, int>& m_label_map;
00125   };
00126         
00127   class EmitFunctor
00128   {
00129   public:
00130     EmitFunctor(CcBackendCode* bec);
00131 
00132     void operator()(SH::ShCtrlGraphNode* node);
00133         
00134   public:
00135     CcBackendCode* m_bec;
00136   };
00137         
00138   void emit(const SH::ShStatement& stmt);
00139   void emitTexLookup(const SH::ShStatement &stmt, const char* texfunc);
00140   void emit(const SH::ShBasicBlockPtr& block);
00141   void emit(const SH::ShCtrlGraphNodePtr& node);
00142       
00143 private:
00144   const SH::ShProgramNodeCPtr& m_original_program;
00145   SH::ShProgramNodePtr m_program;
00146 
00147   std::map<SH::ShCtrlGraphNodePtr, int> m_label_map;
00148   std::map<SH::ShVariableNodePtr, CcVariable> m_varmap;
00149 
00151   // floating point types
00152   //
00153   // @todo may want more intelligent conversion if hardware 
00154   SH::ShTransformer::ValueTypeMap m_convertMap;
00155 
00156   std::stringstream m_code;
00157 
00158 #ifdef _WIN32
00159   HMODULE m_hmodule;
00160 #else
00161   void* m_handle;
00162 #endif /* _WIN32 */
00163 
00164   CcShaderFunc m_shader_func;
00165 
00166   std::string m_code_filename;
00167   std::string m_lib_filename;
00168 
00169   void delete_temporary_files();
00170   bool load_shader_func(const std::stringstream& prologue, const std::stringstream& epilogue);
00171 
00172   int m_cur_temp;
00173 
00174   void** m_params;
00175   std::vector<SH::ShVariantPtr> m_paramVariants;
00176 
00177   //std::vector<SH::ShChannelNodePtr> m_channels;
00178   //std::vector<CcVariable> m_temps;
00179   //std::vector<SH::ShTextureNodePtr> m_textures;
00180 };
00181   
00182 class CcBackend: public SH::ShBackend
00183 {
00184 public:
00185   CcBackend(void);
00186   ~CcBackend(void);
00187   
00188   SH::ShBackendCodePtr generate_code(const std::string& target,
00189                                      const SH::ShProgramNodeCPtr& program);
00190   
00191   void execute(const SH::ShProgramNodeCPtr& program, SH::ShStream& dest);
00192 };
00193 
00194 
00195 typedef SH::ShPointer<CcBackendCode> CcBackendCodePtr;
00196 typedef SH::ShPointer<CcBackend> CcBackendPtr;
00197 
00198 }
00199 
00200 #endif

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