Main Page | Modules | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

ArbCode.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 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 // Filters for code emission and environment setup
00051 const unsigned int SH_ARB_ANY   = 0x0000; // All targets
00052 const unsigned int SH_ARB_FP    = 0x0001; // ARB_fragment_program
00053 const unsigned int SH_ARB_VP    = 0x0002; // ARB_vertex_program
00054 const unsigned int SH_ARB_NVFP  = 0x0004; // NV_fragment_program_option
00055 const unsigned int SH_ARB_NVFP2 = 0x0008; // NV_fragment_program2
00056 const unsigned int SH_ARB_ATIDB = 0x0010; // ATI_draw_buffers
00057 const unsigned int SH_ARB_NVVP2 = 0x0020; // NV_vertex_program2_option
00058 const unsigned int SH_ARB_NVVP3 = 0x0040; // NV_vertex_program3
00059 const unsigned int SH_ARB_VEC1  = 0x0080; // Maximum source has length 1
00060 const unsigned int SH_ARB_VEC2  = 0x0100; // Maximum source has length 2
00061 const unsigned int SH_ARB_VEC3  = 0x0200; // Maximum source has length 3
00062 const unsigned int SH_ARB_VEC4  = 0x0400; // Maximum source has length 4
00063 const unsigned int SH_ARB_END   = 0x1000; // Not a filter. End of
00064                                          // table.
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   // NOTE: These two pointer are deliberately not smart pointers
00173   // so that the circular referenece between a program and
00174   // its compiled code is broken
00175   SH::ShProgramNode* m_shader; // internally visible shader ShTransformered to fit this target (ARB)
00176   SH::ShProgramNode* m_originalShader; // original shader (should alway use this for external (e.g. globals))
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   //@todo remove m_splits and m_converts with dependent uniforms 
00222   
00224   SH::ShTransformer::VarSplitMap m_splits;
00225 
00227   // floating point types
00228   //
00229   // @todo may want more intelligent conversion if hardware 
00230   SH::ShTransformer::ValueTypeMap m_convertMap;
00231 
00233   unsigned int m_programId;
00234 
00235   static ArbMapping table[];
00236 
00237   // Extensions and language alternatives available. See list above
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   // For array lookup
00245   SH::ShVariable m_address_register;
00246 };
00247 
00248 typedef SH::ShPointer<ArbCode> ArbCodePtr;
00249 
00250 
00251 }
00252 
00253 #endif

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