Main Page | Modules | Namespace List | Class Hierarchy | Alphabetical List | Class List | 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 #include <map> 00031 #include <string> 00032 #include <sstream> 00033 00034 #include "ShBackend.hpp" 00035 00036 extern "C" typedef void (*CcLookupFunc)(void* t, 00037 float* src, 00038 float* dst); 00039 00040 extern "C" typedef void (*CcInitFunc)(CcLookupFunc, 00041 CcLookupFunc); 00042 00043 extern "C" typedef void (*CcShaderFunc)(float** inputs, 00044 float** params, 00045 float** streams, 00046 void** textures, 00047 float** outputs); 00048 00049 namespace ShCc { 00050 00051 class CcVariable 00052 { 00053 public: 00054 CcVariable(void); 00055 CcVariable(int num, const std::string& name); 00056 CcVariable(int num, const std::string& name, int size); 00057 00058 public: 00059 int m_num; 00060 std::string m_name; 00061 int m_size; 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 00076 void updateUniform(const SH::ShVariableNodePtr& uniform); 00077 00078 std::ostream& print(std::ostream& out); 00079 00080 std::ostream& printInputOutputFormat(std::ostream& out); 00081 00082 protected: 00083 friend class CcBackend; 00084 bool generate(void); 00085 bool execute(SH::ShStream& dest); 00086 00087 private: 00088 void allocate_consts(void); 00089 void allocate_inputs(void); 00090 void allocate_outputs(void); 00091 void allocate_streams(void); 00092 void allocate_textures(void); 00093 void allocate_uniforms(void); 00094 00095 std::string resolve(const SH::ShVariable& v); 00096 std::string resolve(const SH::ShVariable& v, int idx); 00097 00098 class LabelFunctor 00099 { 00100 public: 00101 LabelFunctor(std::map<SH::ShCtrlGraphNodePtr, int>& label_map); 00102 00103 void operator()(SH::ShCtrlGraphNode* node); 00104 00105 public: 00106 int m_cur_label; 00107 std::map<SH::ShCtrlGraphNodePtr, int>& m_label_map; 00108 }; 00109 00110 class EmitFunctor 00111 { 00112 public: 00113 EmitFunctor(CcBackendCode* bec); 00114 00115 void operator()(SH::ShCtrlGraphNode* node); 00116 00117 public: 00118 CcBackendCode* m_bec; 00119 }; 00120 00121 void emit(const SH::ShStatement& stmt); 00122 void emit(SH::ShBasicBlockPtr block); 00123 void emit(SH::ShCtrlGraphNodePtr node); 00124 00125 private: 00126 const SH::ShProgramNodeCPtr& m_program; 00127 00128 std::map<SH::ShCtrlGraphNodePtr, int> m_label_map; 00129 std::map<SH::ShVariableNodePtr, CcVariable> m_varmap; 00130 00131 std::stringstream m_code; 00132 00133 #ifdef WIN32 00134 HMODULE m_hmodule; 00135 #else 00136 void* m_handle; 00137 #endif /* WIN32 */ 00138 00139 CcInitFunc m_init_func; 00140 CcShaderFunc m_shader_func; 00141 00142 int m_cur_temp; 00143 00144 float** m_params; 00145 std::vector<SH::ShChannelNodePtr> m_streams; 00146 std::vector<CcVariable> m_temps; 00147 std::vector<SH::ShTextureNodePtr> m_textures; 00148 }; 00149 00150 class CcBackend: public SH::ShBackend 00151 { 00152 public: 00153 CcBackend(void); 00154 ~CcBackend(void); 00155 00156 std::string name(void) const; 00157 00158 SH::ShBackendCodePtr generateCode(const std::string& target, 00159 const SH::ShProgramNodeCPtr& program); 00160 00161 void execute(const SH::ShProgramNodeCPtr& program, SH::ShStream& dest); 00162 }; 00163 00164 00165 typedef SH::ShPointer<CcBackendCode> CcBackendCodePtr; 00166 typedef SH::ShPointer<CcBackend> CcBackendPtr; 00167 } 00168 00169 #endif

Generated on Fri Nov 5 16:51:19 2004 for Sh by doxygen 1.3.7