ShBackend.hpp

00001 // Sh: A GPU metaprogramming language.
00002 //
00003 // Copyright 2003-2005 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   virtual std::string name() const { return "";}
00109 
00111   virtual std::string version() const = 0;
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 #if defined(WIN32)
00149   typedef void* LibraryHandle;
00150 #elif defined(__APPLE__) && !defined(AUTOTOOLS)
00151   typedef CFBundleRef LibraryHandle;
00152 #else
00153   typedef lt_dlhandle_struct* LibraryHandle;
00154 #endif
00155   typedef std::map<std::string, LibraryHandle> LibraryMap;
00156   typedef std::map<std::string, ShPointer<ShBackend> > BackendMap;
00157   typedef std::set<std::string> BackendSet;
00158 
00159 protected:
00160   ShBackend();
00161   
00162 private:
00163   static BackendMap* m_instantiated_backends;
00164   static BackendSet* m_selected_backends;
00165   static LibraryMap* m_loaded_libraries;
00166   static bool m_done_init;
00167   static bool m_all_backends_loaded;
00168 
00170   static void init();
00171 
00174   static std::string lookup_filename(const std::string& backend_name);
00175 
00177   static bool is_valid_name(const std::string& backend_name);
00178 
00180   static void load_all_backends();
00181 
00184   static bool load_library(const std::string& filename);
00185 
00187   static void load_libraries(const std::string& directory);
00188 
00191   static int target_cost(const std::string& backend_name, const std::string& target);
00192   
00195   static void check_target(const std::string& target, const std::string& generic_target, 
00196                            std::list<std::string>& derived_targets);
00197 
00199   static ShPointer<ShBackend> instantiate_backend(const std::string& backend_name);
00200 };
00201 
00202 typedef ShPointer<ShBackend> ShBackendPtr;
00203 typedef ShPointer<const ShBackend> ShBackendCPtr;
00204 
00205 }
00206 
00207 #endif

Generated on Wed Nov 9 15:29:29 2005 for Sh by  doxygen 1.4.5