00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00027
#ifndef SHGCC_HPP
00028
#define SHGCC_HPP
00029
00030
#include <map>
00031
#include <string>
00032
#include <sstream>
00033
00034
#include "ShBackend.hpp"
00035
00036
typedef void (*GccFunc)(
float** inputs,
00037
float** params,
00038
float** streams,
00039
void** textures,
00040
float** outputs);
00041
00042
namespace ShGcc {
00043
00044
class GccVariable
00045 {
00046
public:
00047 GccVariable(
void);
00048 GccVariable(
int num,
const std::string& name);
00049 GccVariable(
int num,
const std::string& name,
int size);
00050
00051
public:
00052
int m_num;
00053 std::string m_name;
00054
int m_size;
00055 };
00056
00057
class GccBackendCode:
public SH::ShBackendCode
00058 {
00059
public:
00060 GccBackendCode(
const SH::ShProgramNodeCPtr& program);
00061 ~GccBackendCode(
void);
00062
00063
bool allocateRegister(
const SH::ShVariableNodePtr& var);
00064
void freeRegister(
const SH::ShVariableNodePtr& var);
00065
00066
void upload(
void);
00067
void bind(
void);
00068
00069
void updateUniform(
const SH::ShVariableNodePtr& uniform);
00070
00071 std::ostream& print(std::ostream& out);
00072
00073 std::ostream& printInputOutputFormat(std::ostream& out);
00074
00075
protected:
00076
friend class GccBackend;
00077
bool generate(
void);
00078
bool execute(
SH::ShStream& dest);
00079
00080
private:
00081
void allocate_consts(
void);
00082
void allocate_inputs(
void);
00083
void allocate_outputs(
void);
00084
void allocate_streams(
void);
00085
void allocate_textures(
void);
00086
void allocate_uniforms(
void);
00087
00088 std::string resolve(
const SH::ShVariable& v);
00089 std::string resolve(
const SH::ShVariable& v,
int idx);
00090
00091
class LabelFunctor
00092 {
00093
public:
00094 LabelFunctor(std::map<SH::ShCtrlGraphNodePtr, int>& label_map);
00095
00096
void operator()(
SH::ShCtrlGraphNode* node);
00097
00098
public:
00099
int m_cur_label;
00100 std::map<SH::ShCtrlGraphNodePtr, int>& m_label_map;
00101 };
00102
00103
class EmitFunctor
00104 {
00105
public:
00106 EmitFunctor(GccBackendCode* bec);
00107
00108
void operator()(
SH::ShCtrlGraphNode* node);
00109
00110
public:
00111 GccBackendCode* m_bec;
00112 };
00113
00114
void emit(
const SH::ShStatement& stmt);
00115
void emit(
SH::ShBasicBlockPtr block);
00116
void emit(
SH::ShCtrlGraphNodePtr node);
00117
00118
private:
00119
const SH::ShProgramNodeCPtr& m_program;
00120
00121 std::map<SH::ShCtrlGraphNodePtr, int> m_label_map;
00122 std::map<SH::ShVariableNodePtr, GccVariable> m_varmap;
00123
00124 std::stringstream m_code;
00125
00126
void* m_handle;
00127 GccFunc m_func;
00128
00129
int m_cur_temp;
00130
00131
float** m_params;
00132 std::vector<SH::ShChannelNodePtr> m_streams;
00133 std::vector<GccVariable> m_temps;
00134 std::vector<SH::ShTextureNodePtr> m_textures;
00135 };
00136
00137
class GccBackend:
public SH::ShBackend
00138 {
00139
public:
00140 GccBackend(
void);
00141 ~GccBackend(
void);
00142
00143 std::string name(
void) const;
00144
00145 SH::ShBackendCodePtr generateCode(const std::string& target,
00146 const SH::ShProgramNodeCPtr& program);
00147
00148
void execute(const SH::ShProgramNodeCPtr& program, SH::ShStream& dest);
00149 };
00150
00151
00152 typedef SH::ShPointer<GccBackendCode> GccBackendCodePtr;
00153 typedef SH::ShPointer<GccBackend> GccBackendPtr;
00154 }
00155
00156 #endif