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 ARBCODE_HPP
00028 #define ARBCODE_HPP
00029
00030 #include "GlBackend.hpp"
00031 #include "ShTransformer.hpp"
00032 #include "ShVariableNode.hpp"
00033 #include "ShVariable.hpp"
00034 #include "ShCtrlGraph.hpp"
00035 #include "ShTextureNode.hpp"
00036 #include "ShProgram.hpp"
00037 #include "ShSwizzle.hpp"
00038 #include "ShRefCount.hpp"
00039 #include "ShStructural.hpp"
00040 #include "ArbLimits.hpp"
00041 #include "ArbReg.hpp"
00042 #include "ArbInst.hpp"
00043
00044 namespace shgl {
00045
00046 class ArbCode;
00047 class ArbBindingSpecs;
00048 class ArbMapping;
00049
00050
00051 const unsigned int SH_ARB_ANY = 0x0000;
00052 const unsigned int SH_ARB_FP = 0x0001;
00053 const unsigned int SH_ARB_VP = 0x0002;
00054 const unsigned int SH_ARB_NVFP = 0x0004;
00055 const unsigned int SH_ARB_NVFP2 = 0x0008;
00056 const unsigned int SH_ARB_ATIDB = 0x0010;
00057 const unsigned int SH_ARB_NVVP2 = 0x0020;
00058 const unsigned int SH_ARB_NVVP3 = 0x0040;
00059 const unsigned int SH_ARB_VEC1 = 0x0080;
00060 const unsigned int SH_ARB_VEC2 = 0x0100;
00061 const unsigned int SH_ARB_VEC3 = 0x0200;
00062 const unsigned int SH_ARB_VEC4 = 0x0400;
00063 const unsigned int SH_ARB_END = 0x1000;
00064
00065
00066 class ArbCode : public SH::ShBackendCode {
00067 public:
00068 ArbCode(const SH::ShProgramNodeCPtr& program, const std::string& target,
00069 TextureStrategy* texture);
00070 virtual ~ArbCode();
00071
00072 virtual bool allocateRegister(const SH::ShVariableNodePtr& var);
00073 virtual void freeRegister(const SH::ShVariableNodePtr& var);
00074
00075 virtual void upload();
00076 virtual void bind();
00077 virtual void update();
00078 virtual void updateUniform(const SH::ShVariableNodePtr& uniform);
00079
00080 std::ostream& print(std::ostream& out);
00081 std::ostream& describe_interface(std::ostream& out);
00082
00084 void generate();
00085
00086 private:
00087
00089 void genNode(SH::ShCtrlGraphNodePtr node);
00090
00094 void genStructNode(const SH::ShStructuralNodePtr& node);
00095
00097 void emit(const SH::ShStatement& stmt);
00098
00100 void emit_div(const SH::ShStatement& stmt);
00101 void emit_sqrt(const SH::ShStatement& stmt);
00102 void emit_lerp(const SH::ShStatement& stmt);
00103 void emit_dot2(const SH::ShStatement& stmt);
00104 void emit_eq(const SH::ShStatement& stmt);
00105 void emit_ceil(const SH::ShStatement& stmt);
00106 void emit_mod(const SH::ShStatement& stmt);
00107 void emit_trig(const SH::ShStatement& stmt);
00108 void emit_invtrig(const SH::ShStatement& stmt);
00109 void emit_tan(const SH::ShStatement& stmt);
00110 void emit_exp(const SH::ShStatement& stmt);
00111 void emit_log(const SH::ShStatement& stmt);
00112 void emit_norm(const SH::ShStatement& stmt);
00113 void emit_sgn(const SH::ShStatement& stmt);
00114 void emit_tex(const SH::ShStatement& stmt);
00115 void emit_nvcond(const SH::ShStatement& stmt);
00116 void emit_cmul(const SH::ShStatement& stmt);
00117 void emit_csum(const SH::ShStatement& stmt);
00118 void emit_kil(const SH::ShStatement& stmt);
00119 void emit_pal(const SH::ShStatement& stmt);
00120
00122 void allocRegs();
00123
00125 void allocInputs(const ArbLimits& limits);
00126
00128 void allocOutputs(const ArbLimits& limits);
00129
00131 void allocParam(const ArbLimits& limits, const SH::ShVariableNodePtr& node);
00132
00134 void allocPalette(const ArbLimits& limits, const SH::ShPaletteNodePtr& node);
00135
00137 void allocConsts(const ArbLimits& limits);
00138
00142 void allocTemps(const ArbLimits& limits, bool half);
00143
00145 void allocTextures(const ArbLimits& limits);
00146
00148 void bindTextures();
00149
00151 void bindTexture(const SH::ShTextureNodePtr& node);
00152
00153 void bindSpecial(const SH::ShProgramNode::VarList::const_iterator& begin,
00154 const SH::ShProgramNode::VarList::const_iterator& end,
00155 const ArbBindingSpecs& specs,
00156 std::vector<int>& bindings,
00157 ArbRegType type, int& num);
00158
00160 std::ostream& printVar(std::ostream& out, bool dest, const SH::ShVariable& var,
00161 bool collectingOp,
00162 const SH::ShSwizzle& destSwiz,
00163 bool do_swiz) const;
00164
00167 bool printSamplingInstruction(std::ostream& out, const ArbInst& inst) const;
00168
00169 int getLabel(SH::ShCtrlGraphNodePtr node);
00170
00171 TextureStrategy* m_texture;
00172
00173
00174
00175 SH::ShProgramNode* m_shader;
00176 SH::ShProgramNode* m_originalShader;
00177 std::string m_unit;
00178
00179 typedef std::vector<ArbInst> ArbInstList;
00180 ArbInstList m_instructions;
00181
00183 std::list<int> m_tempRegs;
00184
00186 int m_numTemps;
00187
00189 int m_numHalfTemps;
00190
00192 int m_numInputs;
00193
00195 int m_numOutputs;
00196
00198 int m_numParams;
00199
00202 int m_numParamBindings;
00203
00205 int m_numConsts;
00206
00208 int m_numTextures;
00209
00210 typedef std::map<SH::ShVariableNodePtr,
00211 SH::ShPointer<ArbReg> > RegMap;
00212 RegMap m_registers;
00213
00214 typedef std::list< SH::ShPointer<ArbReg> > RegList;
00215 RegList m_reglist;
00216
00217 std::vector<int> m_outputBindings;
00218 std::vector<int> m_inputBindings;
00219
00220
00221
00222
00224 SH::ShTransformer::VarSplitMap m_splits;
00225
00227
00228
00229
00230 SH::ShTransformer::ValueTypeMap m_convertMap;
00231
00233 unsigned int m_programId;
00234
00235 static ArbMapping table[];
00236
00237
00238 unsigned int m_environment;
00239
00240 typedef std::map<SH::ShCtrlGraphNodePtr, int> LabelMap;
00241 LabelMap m_label_map;
00242 int m_max_label;
00243
00244
00245 SH::ShVariable m_address_register;
00246 };
00247
00248 typedef SH::ShPointer<ArbCode> ArbCodePtr;
00249
00250
00251 }
00252
00253 #endif