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

ShSmBackend.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 SHSM_BACKEND_HPP
00028 #define SHSM_BACKEND_HPP
00029 
00030 #include <string>
00031 #include <map>
00032 #include <iosfwd>
00033 #include <list>
00034 #include <sm/sm.hpp>
00035 #include "ShBackend.hpp"
00036 #include "ShVariableNode.hpp"
00037 #include "ShBasicBlock.hpp"
00038 #include "ShRefCount.hpp"
00039 #include "ShTextureNode.hpp"
00040 #include "ShCtrlGraph.hpp"
00041 #include "ShTransformer.hpp"
00042 
00043 namespace ShSm {
00044 
00045 struct SmInstruction {
00046   SmInstruction(const Operation& op, const SH::ShVariable& dest)
00047     : op(op), dest(dest), src1(0), src2(0), src3(0)
00048   {}
00049   SmInstruction(const Operation& op, const SH::ShVariable& dest, const SH::ShVariable& src1)
00050     : op(op), dest(dest), src1(src1), src2(0), src3(0)
00051   {}
00052   SmInstruction(const Operation& op, const SH::ShVariable& dest, const SH::ShVariable& src1,
00053                 const SH::ShVariable& src2)
00054     : op(op), dest(dest), src1(src1), src2(src2), src3(0)
00055   {}
00056   SmInstruction(const Operation& op, const SH::ShVariable& dest, const SH::ShVariable& src1,
00057                 const SH::ShVariable& src2, const SH::ShVariable& src3)
00058     : op(op), dest(dest), src1(src1), src2(src2), src3(src3)
00059   {}
00060   Operation op;
00061   SH::ShVariable dest;
00062   SH::ShVariable src1, src2, src3;
00063 };
00064 
00065 enum SmRegType {
00066   SHSM_REG_INPUT = 0,
00067   SHSM_REG_OUTPUT = 1,
00068   SHSM_REG_TEMP = 2,
00069   SHSM_REG_CONST = 3,
00070   SHSM_REG_TEXTURE = 4,
00071 };
00072 
00073 struct SmRegister {
00074   SmRegister() {}
00075   SmRegister(SmRegType type, int index, std::string name = "")
00076     : type(type), index(index)
00077   {
00078   }
00079   SmRegType type;
00080   int index;
00081 
00082   std::string print() const;
00083 };
00084 
00085 class Backend;
00086 
00087 class BackendCode : public SH::ShBackendCode {
00088 public:
00089   BackendCode(SH::ShPointer<Backend> backend, const SH::ShProgram& shader,
00090               const std::string& target);
00091   virtual ~BackendCode();
00092 
00093   virtual bool allocateRegister(const SH::ShVariableNodePtr& var);
00094   virtual void freeRegister(const SH::ShVariableNodePtr& var);
00095   
00096   virtual void upload();
00097   virtual void bind();
00098   virtual void updateUniform(const SH::ShVariableNodePtr& uniform);
00099 
00101   void generate();
00102   
00103   std::ostream& print(std::ostream& out);
00104 
00105   int label(const SH::ShBasicBlockPtr& block);
00106   void addBasicBlock(const SH::ShBasicBlockPtr& block);
00107 
00108   void allocRegs();
00109 
00110   const std::string& target() const { return m_target; }
00111 
00112 private:
00113   SH::ShPointer<Backend> m_backend;
00114   SH::ShProgram m_shader;
00115   SH::ShProgram m_originalShader;
00116   SMshader m_smShader;
00117   std::string m_target;
00118 
00119   typedef std::vector<SmInstruction> SmInstList;
00120   SmInstList m_instructions; 
00121 
00123   // where the scalar must be promoted to a vector by duplicating 
00124   // components.
00125   void genScalarVectorInst( SH::ShVariable dest, SH::ShVariable op1, 
00126     SH::ShVariable op2, Operation opcode ); 
00127 
00129   bool haveReg(const SH::ShVariableNodePtr& var);
00130 
00132   SmRegister getReg(const SH::ShVariableNodePtr& var);
00133   
00135   SMreg getSmReg(const SH::ShVariable& var);
00136 
00138   std::list<int> m_tempRegs;
00139   
00140   int m_maxCR;
00141   int m_maxTR;
00142   int m_maxIR;
00143   int m_maxOR;
00144   int m_maxTex;
00145 
00146   typedef std::map<SH::ShVariableNodePtr, SmRegister> RegMap;
00147   RegMap m_registers;
00148 
00149   SMreg* m_cR;
00150   SMreg* m_tR;
00151   SMreg* m_iR;
00152   SMreg* m_oR;
00153 
00154   typedef std::map<SH::ShTextureNodePtr, SMtexture> TextureNodeMap;
00155 
00156   TextureNodeMap m_textureMap;
00157 
00158   SH::ShTransformer::VarSplitMap m_splits;
00159 
00160   std::string printVar(const SH::ShVariable& var);
00161 };
00162 
00163 typedef SH::ShPointer<BackendCode> BackendCodePtr;
00164 
00165 class Backend : public SH::ShBackend {
00166 public:
00167   Backend();
00168   ~Backend();
00169   std::string name() const;
00170   SH::ShBackendCodePtr generateCode(const std::string& target, const SH::ShProgram& shader);
00171 
00172   void execute(const SH::ShProgram& program, SH::ShStream& dest);
00173 
00174 private:
00175   void generateNode(BackendCodePtr& code, const SH::ShCtrlGraphNodePtr& node);
00176 };
00177 
00178 typedef SH::ShPointer<Backend> BackendPtr;
00179 
00180 }
00181 #endif

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