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

Generated on Mon Jan 24 18:36:29 2005 for Sh by  doxygen 1.4.1