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

ShBackend.hpp

00001 // Sh: A GPU metaprogramming language.
00002 //
00003 // Copyright 2003-2005 Serious Hack Inc.
00004 // 
00005 // This software is provided 'as-is', without any express or implied
00006 // warranty. In no event will the authors be held liable for any damages
00007 // arising from the use of this software.
00008 // 
00009 // Permission is granted to anyone to use this software for any purpose,
00010 // including commercial applications, and to alter it and redistribute it
00011 // freely, subject to the following restrictions:
00012 // 
00013 // 1. The origin of this software must not be misrepresented; you must
00014 // not claim that you wrote the original software. If you use this
00015 // software in a product, an acknowledgment in the product documentation
00016 // would be appreciated but is not required.
00017 // 
00018 // 2. Altered source versions must be plainly marked as such, and must
00019 // not be misrepresented as being the original software.
00020 // 
00021 // 3. This notice may not be removed or altered from any source
00022 // distribution.
00024 #ifndef SHBACKEND_HPP
00025 #define SHBACKEND_HPP
00026 
00027 #include <vector>
00028 #include <iosfwd>
00029 #include <string>
00030 #include "ShDllExport.hpp"
00031 #include "ShRefCount.hpp"
00032 #include "ShProgram.hpp"
00033 #include "ShProgramSet.hpp"
00034 #include "ShVariableNode.hpp"
00035 
00036 #ifndef WIN32
00037   struct lt_dlhandle_struct;
00038 #endif
00039 
00040 namespace SH  {
00041 
00042 class ShStream;
00043 
00044 class
00045 SH_DLLEXPORT ShBackendCode : public ShRefCountable {
00046 public:
00047   virtual ~ShBackendCode();
00048 
00051   virtual bool allocateRegister(const ShVariableNodePtr& var) = 0;
00052 
00056   virtual void freeRegister(const ShVariableNodePtr& var) = 0;
00057   
00059   virtual void upload() = 0;
00060 
00062   virtual void bind() = 0;
00063 
00065   virtual void unbind() = 0;
00066 
00068   virtual void update() = 0;
00069 
00071   virtual void updateUniform(const ShVariableNodePtr& uniform) = 0;
00072 
00073   virtual std::ostream& print(std::ostream& out) = 0;
00074 
00076   // (Useful for how to format long tuple input on targets 
00077   // that only support limited tuple lengths) 
00078   virtual std::ostream& describe_interface(std::ostream& out) = 0;
00079 };
00080 
00081 typedef ShPointer<ShBackendCode> ShBackendCodePtr;
00082 typedef ShPointer<const ShBackendCode> ShBackendCodeCPtr;
00083 
00084 // A backend-specific set of programs, to be linked together/bound at once
00085 class
00086 SH_DLLEXPORT ShBackendSet : public ShRefCountable {
00087 public:
00088   virtual ~ShBackendSet();
00089 
00090   virtual void link() = 0;
00091 
00092   virtual void bind() = 0;
00093   virtual void unbind() = 0;
00094 };
00095 
00096 typedef ShPointer<ShBackendSet> ShBackendSetPtr;
00097 typedef ShPointer<const ShBackendSet> ShBackendSetCPtr;
00098 
00099 class ShTransformer;
00100 class
00101 SH_DLLEXPORT ShBackend : public ShRefCountable {
00102 public:
00103   virtual ~ShBackend();
00104 
00106   virtual std::string name() const { return "";}
00107 
00109   virtual std::string version() const = 0;
00110 
00112   virtual ShBackendCodePtr generate_code(const std::string& target,
00113                                          const ShProgramNodeCPtr& shader) = 0;
00114 
00115   virtual ShBackendSetPtr generate_set(const ShProgramSet& s);
00116   
00118   virtual void execute(const ShProgramNodeCPtr& program, ShStream& dest) = 0;
00119   
00121   virtual void unbind_all_programs();
00122 
00124   static void unbind_all_backends();
00125 
00127   static bool use_backend(const std::string& backend_name);
00128 
00130   static bool have_backend(const std::string& backend_name);
00131 
00133   static void clear_backends();
00134 
00136   static std::string target_handler(const std::string& target, bool restrict_to_selected);
00137 
00139   static ShPointer<ShBackend> get_backend(const std::string& target);
00140 
00144   static std::list<std::string> derived_targets(const std::string& target);
00145 
00146 #ifndef WIN32
00147   typedef lt_dlhandle_struct* LibraryHandle;
00148 #else
00149   typedef void* LibraryHandle;
00150 #endif
00151   typedef std::map<std::string, LibraryHandle> LibraryMap;
00152   typedef std::map<std::string, ShPointer<ShBackend> > BackendMap;
00153   typedef std::set<std::string> BackendSet;
00154 
00155 protected:
00156   ShBackend();
00157   
00158 private:
00159   static BackendMap* m_instantiated_backends;
00160   static BackendSet* m_selected_backends;
00161   static LibraryMap* m_loaded_libraries;
00162   static bool m_done_init;
00163   static bool m_all_backends_loaded;
00164 
00166   static void init();
00167 
00170   static std::string lookup_filename(const std::string& backend_name);
00171 
00173   static bool is_valid_name(const std::string& backend_name);
00174 
00177   static bool load_library(const std::string& filename);
00178 
00180   static void load_libraries(const std::string& directory);
00181 
00184   static int target_cost(const std::string& backend_name, const std::string& target);
00185   
00187   static ShPointer<ShBackend> instantiate_backend(const std::string& backend_name);
00188 };
00189 
00190 typedef ShPointer<ShBackend> ShBackendPtr;
00191 typedef ShPointer<const ShBackend> ShBackendCPtr;
00192 
00193 }
00194 
00195 #endif

Generated on Wed Jun 15 18:12:38 2005 for Sh by  doxygen 1.4.3-20050530