Main Page | Modules | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

Cc.hpp

00001 // Sh: A GPU metaprogramming language.
00002 //
00003 // Copyright 2003-2005 Serious Hack Inc.
00004 // 
00005 // This software is provided 'as-is', without any express or implied
00006 // warranty. In no event will the authors be held liable for any damages
00007 // arising from the use of this software.
00008 // 
00009 // Permission is granted to anyone to use this software for any purpose,
00010 // including commercial applications, and to alter it and redistribute it
00011 // freely, subject to the following restrictions:
00012 // 
00013 // 1. The origin of this software must not be misrepresented; you must
00014 // not claim that you wrote the original software. If you use this
00015 // software in a product, an acknowledgment in the product documentation
00016 // would be appreciated but is not required.
00017 // 
00018 // 2. Altered source versions must be plainly marked as such, and must
00019 // not be misrepresented as being the original software.
00020 // 
00021 // 3. This notice may not be removed or altered from any source
00022 // distribution.
00024 #ifndef SHCC_HPP
00025 #define SHCC_HPP
00026 
00027 #ifdef WIN32
00028 #include <windows.h>
00029 #endif
00030 
00031 #include <map>
00032 #include <string>
00033 #include <sstream>
00034 
00035 #include "ShVariant.hpp"
00036 #include "ShBackend.hpp"
00037 #include "ShTransformer.hpp"
00038 
00039 // #define SH_CC_DEBUG 1
00040 
00041 // function representing a single kernel
00042 // Each of the parameters is an array of single storage-typed arrays
00043 // (The size and type are known during code emission)
00044 // @todo type - inputs should be allowed to change?
00045 extern "C" typedef void (*CcShaderFunc)(void** inputs, 
00046                                                           void** params,
00047                                                           void** channels,
00048                                                           void** textures,
00049                                                           void** outputs);
00050 
00051 namespace ShCc {
00052 
00053 struct CcVariable
00054 {
00055   CcVariable(void);
00056   CcVariable(int num, const std::string& name, int size, SH::ShValueType valueType);
00057 
00058   int m_num;
00059   std::string m_name;
00060   int m_size;
00061   SH::ShValueType m_valueType;
00062 };
00063 
00064 class CcBackendCode: public SH::ShBackendCode
00065 {
00066 public:
00067   CcBackendCode(const SH::ShProgramNodeCPtr& program);
00068   ~CcBackendCode(void);
00069 
00070   bool allocateRegister(const SH::ShVariableNodePtr& var);
00071   void freeRegister(const SH::ShVariableNodePtr& var);
00072     
00073   void upload(void);
00074   void bind(void);
00075   void unbind(void);
00076   void update(void);
00077     
00078   void updateUniform(const SH::ShVariableNodePtr& uniform);
00079     
00080   std::ostream& print(std::ostream& out);
00081     
00082   std::ostream& describe_interface(std::ostream& out);
00083 
00084 protected:
00085   friend class CcBackend;
00086   bool generate(void);
00087   bool execute(SH::ShStream& dest);
00088 
00089 private:
00090 
00095   template<typename T>
00096   void allocate_varlist(const std::list<T> &varList, const char* varPrefix, const char* arrayName, const char* typePrefix=""); 
00097 
00098   // Allocates a variable names for different kinds of data 
00099   // and initializes the variable to index into the appropriate
00100   // element of an array passed into the CcShaderFunc.
00101   //
00102   // Spits out these variable declarations & assignments to m_code
00103   //
00104   // void* type of the array gets cast to the variable type
00105   // @{
00106   void allocate_consts(void);
00107   void allocate_inputs(void);
00108   void allocate_outputs(void);
00109   void allocate_channels(void);
00110   void allocate_textures(void);
00111   void allocate_uniforms(void);
00112   void allocate_temps(void);
00113   // @}
00114 
00115   std::string resolve(const SH::ShVariable& v);
00116   std::string resolve(const SH::ShVariable& v, int idx);
00117   const char* ctype(SH::ShValueType valueType);
00118 
00119   class LabelFunctor
00120   {
00121   public:
00122     LabelFunctor(std::map<SH::ShCtrlGraphNodePtr, int>& label_map);
00123         
00124     void operator()(SH::ShCtrlGraphNode* node);
00125         
00126   public:
00127     int m_cur_label;
00128     std::map<SH::ShCtrlGraphNodePtr, int>& m_label_map;
00129   };
00130         
00131   class EmitFunctor
00132   {
00133   public:
00134     EmitFunctor(CcBackendCode* bec);
00135 
00136     void operator()(SH::ShCtrlGraphNode* node);
00137         
00138   public:
00139     CcBackendCode* m_bec;
00140   };
00141         
00142   void emit(const SH::ShStatement& stmt);
00143   void emitTexLookup(const SH::ShStatement &stmt, const char* texfunc);
00144   void emit(SH::ShBasicBlockPtr block);
00145   void emit(SH::ShCtrlGraphNodePtr node);
00146       
00147 private:
00148   const SH::ShProgramNodeCPtr& m_original_program;
00149   SH::ShProgramNodePtr m_program;
00150 
00151   std::map<SH::ShCtrlGraphNodePtr, int> m_label_map;
00152   std::map<SH::ShVariableNodePtr, CcVariable> m_varmap;
00153 
00155   // floating point types
00156   //
00157   // @todo may want more intelligent conversion if hardware 
00158   SH::ShTransformer::ValueTypeMap m_convertMap;
00159 
00160   std::stringstream m_code;
00161 
00162 #ifdef WIN32
00163   HMODULE m_hmodule;
00164 #else
00165   void* m_handle;
00166 #endif /* WIN32 */
00167 
00168   CcShaderFunc m_shader_func;
00169 
00170   int m_cur_temp;
00171 
00172   void** m_params;
00173   std::vector<SH::ShVariantPtr> m_paramVariants;
00174 
00175   //std::vector<SH::ShChannelNodePtr> m_channels;
00176   //std::vector<CcVariable> m_temps;
00177   //std::vector<SH::ShTextureNodePtr> m_textures;
00178 };
00179   
00180 class CcBackend: public SH::ShBackend
00181 {
00182 public:
00183   CcBackend(void);
00184   ~CcBackend(void);
00185   
00186   std::string name(void) const { return "cc"; }
00187   std::string version(void) const { return "1.0"; }
00188   
00189   SH::ShBackendCodePtr generate_code(const std::string& target,
00190                                      const SH::ShProgramNodeCPtr& program);
00191   
00192   void execute(const SH::ShProgramNodeCPtr& program, SH::ShStream& dest);
00193 };
00194 
00195 
00196 typedef SH::ShPointer<CcBackendCode> CcBackendCodePtr;
00197 typedef SH::ShPointer<CcBackend> CcBackendPtr;
00198 
00199 }
00200 
00201 #endif

Generated on Wed Jun 15 18:12:37 2005 for Sh by  doxygen 1.4.3-20050530