ShBackend.hpp

00001 // Sh: A GPU metaprogramming language.
00002 //
00003 // Copyright 2003-2006 Serious Hack Inc.
00004 // 
00005 // This library is free software; you can redistribute it and/or
00006 // modify it under the terms of the GNU Lesser General Public
00007 // License as published by the Free Software Foundation; either
00008 // version 2.1 of the License, or (at your option) any later version.
00009 //
00010 // This library is distributed in the hope that it will be useful,
00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013 // Lesser General Public License for more details.
00014 //
00015 // You should have received a copy of the GNU Lesser General Public
00016 // License along with this library; if not, write to the Free Software
00017 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 
00018 // MA  02110-1301, USA
00020 #ifndef SHBACKEND_HPP
00021 #define SHBACKEND_HPP
00022 
00023 #include <vector>
00024 #include <iosfwd>
00025 #include <string>
00026 #include "ShDllExport.hpp"
00027 #include "ShRefCount.hpp"
00028 #include "ShProgram.hpp"
00029 #include "ShProgramSet.hpp"
00030 #include "ShVariableNode.hpp"
00031 
00032 #if defined(_WIN32)
00033 #elif defined(__APPLE__) && !defined(AUTOTOOLS)
00034 #include <CoreFoundation/CoreFoundation.h>
00035 #else
00036   struct lt_dlhandle_struct;
00037 #endif
00038 
00039 namespace SH  {
00040 
00041 class ShStream;
00042 
00043 class
00044 SH_DLLEXPORT ShBackendCode : public ShRefCountable {
00045 public:
00046   virtual ~ShBackendCode();
00047 
00050   virtual bool allocateRegister(const ShVariableNodePtr& var) = 0;
00051 
00055   virtual void freeRegister(const ShVariableNodePtr& var) = 0;
00056   
00058   virtual void upload() = 0;
00059 
00061   virtual void bind() = 0;
00062 
00064   virtual void unbind() = 0;
00065 
00067   virtual void update() = 0;
00068 
00070   virtual void updateUniform(const ShVariableNodePtr& uniform) = 0;
00071 
00072   virtual std::ostream& print(std::ostream& out) = 0;
00073 
00075   // (Useful for how to format long tuple input on targets 
00076   // that only support limited tuple lengths) 
00077   virtual std::ostream& describe_interface(std::ostream& out) = 0;
00078 
00080   virtual std::ostream& describe_bindings(std::ostream& out) = 0;
00081 };
00082 
00083 typedef ShPointer<ShBackendCode> ShBackendCodePtr;
00084 typedef ShPointer<const ShBackendCode> ShBackendCodeCPtr;
00085 
00086 // A backend-specific set of programs, to be linked together/bound at once
00087 class
00088 SH_DLLEXPORT ShBackendSet : public ShRefCountable {
00089 public:
00090   virtual ~ShBackendSet();
00091 
00092   virtual void link() = 0;
00093 
00094   virtual void bind() = 0;
00095   virtual void unbind() = 0;
00096 };
00097 
00098 typedef ShPointer<ShBackendSet> ShBackendSetPtr;
00099 typedef ShPointer<const ShBackendSet> ShBackendSetCPtr;
00100 
00101 class ShTransformer;
00102 class
00103 SH_DLLEXPORT ShBackend : public ShRefCountable {
00104 public:
00105   virtual ~ShBackend();
00106 
00108   std::string name() const { return m_name;}
00109 
00111   std::string version() const { return m_version; }
00112 
00114   virtual ShBackendCodePtr generate_code(const std::string& target,
00115                                          const ShProgramNodeCPtr& shader) = 0;
00116 
00117   virtual ShBackendSetPtr generate_set(const ShProgramSet& s);
00118   
00120   virtual void execute(const ShProgramNodeCPtr& program, ShStream& dest) = 0;
00121   
00123   virtual void unbind_all_programs();
00124 
00126   static void unbind_all_backends();
00127 
00129   static bool use_backend(const std::string& backend_name);
00130 
00132   static bool have_backend(const std::string& backend_name);
00133 
00135   static void clear_backends();
00136 
00138   static std::string target_handler(const std::string& target, bool restrict_to_selected);
00139 
00141   static ShPointer<ShBackend> get_backend(const std::string& target);
00142 
00146   static std::list<std::string> derived_targets(const std::string& target);
00147 
00148   typedef ShBackend* InstantiateEntryPoint();
00149   typedef int TargetCostEntryPoint(const std::string &);
00150 
00152   static void register_backend(const std::string& backend_name, InstantiateEntryPoint *instantiate, TargetCostEntryPoint *target_cost);
00153   
00154 #if defined(_WIN32)
00155   typedef void* LibraryHandle;
00156 #elif defined(__APPLE__) && !defined(AUTOTOOLS)
00157   typedef CFBundleRef LibraryHandle;
00158 #else
00159   typedef lt_dlhandle_struct* LibraryHandle;
00160 #endif
00161   
00162   class LibraryInformation : public ShRefCountable {
00163   public:
00164     LibraryInformation(LibraryHandle handle, InstantiateEntryPoint* instantiate_function, TargetCostEntryPoint* target_cost_function) :
00165       m_handle(handle), m_instantiate_function(instantiate_function), m_target_cost_function(target_cost_function)
00166     {}
00167     
00168     LibraryHandle handle() { return m_handle; }
00169     InstantiateEntryPoint* instantiate_function() { return m_instantiate_function; }
00170     TargetCostEntryPoint* target_cost_function() { return m_target_cost_function; }
00171   private: 
00172     LibraryHandle m_handle;
00173     InstantiateEntryPoint* m_instantiate_function;
00174     TargetCostEntryPoint* m_target_cost_function;
00175   };
00176 
00177   typedef std::map<std::string, ShPointer<LibraryInformation> > LibraryMap;
00178   typedef std::map<std::string, ShPointer<ShBackend> > BackendMap;
00179   typedef std::set<std::string> BackendSet;
00180   
00181 protected:
00182   ShBackend(const std::string& name, const std::string& version);
00183   
00184 private:
00185   static BackendMap* m_instantiated_backends;
00186   static BackendSet* m_selected_backends;
00187   static LibraryMap* m_loaded_libraries;
00188   static bool m_done_init;
00189   static bool m_all_backends_loaded;
00190 
00191   std::string m_name;
00192   std::string m_version;
00193 
00195   static void init();
00196 
00199   static std::string lookup_filename(const std::string& backend_name);
00200 
00202   static bool is_valid_name(const std::string& backend_name);
00203 
00205   static void load_all_backends();
00206 
00209   static bool load_library(const std::string& filename);
00210 
00212   static void load_libraries(const std::string& directory);
00213 
00216   static int target_cost(const std::string& backend_name, const std::string& target);
00217   
00220   static void check_target(const std::string& target, const std::string& generic_target, 
00221                            std::list<std::string>& derived_targets);
00222 
00224   static ShPointer<ShBackend> instantiate_backend(const std::string& backend_name);
00225 };
00226 
00227 typedef ShPointer<ShBackend> ShBackendPtr;
00228 typedef ShPointer<const ShBackend> ShBackendCPtr;
00229 
00230 }
00231 
00232 #endif

Generated on Thu Feb 16 14:51:31 2006 for Sh by  doxygen 1.4.6