00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
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
00036
00037
00038
00039
00040
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
00095
00096
00097
00098
00099
00100
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
00152
00153
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
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
00178
00179
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