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 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__)
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 };
00079 
00080 typedef ShPointer<ShBackendCode> ShBackendCodePtr;
00081 typedef ShPointer<const ShBackendCode> ShBackendCodeCPtr;
00082 
00083 // A backend-specific set of programs, to be linked together/bound at once
00084 class
00085 SH_DLLEXPORT ShBackendSet : public ShRefCountable {
00086 public:
00087   virtual ~ShBackendSet();
00088 
00089   virtual void link() = 0;
00090 
00091   virtual void bind() = 0;
00092   virtual void unbind() = 0;
00093 };
00094 
00095 typedef ShPointer<ShBackendSet> ShBackendSetPtr;
00096 typedef ShPointer<const ShBackendSet> ShBackendSetCPtr;
00097 
00098 class ShTransformer;
00099 class
00100 SH_DLLEXPORT ShBackend : public ShRefCountable {
00101 public:
00102   virtual ~ShBackend();
00103 
00105   virtual std::string name() const { return "";}
00106 
00108   virtual std::string version() const = 0;
00109 
00111   virtual ShBackendCodePtr generate_code(const std::string& target,
00112                                          const ShProgramNodeCPtr& shader) = 0;
00113 
00114   virtual ShBackendSetPtr generate_set(const ShProgramSet& s);
00115   
00117   virtual void execute(const ShProgramNodeCPtr& program, ShStream& dest) = 0;
00118   
00120   virtual void unbind_all_programs();
00121 
00123   static void unbind_all_backends();
00124 
00126   static bool use_backend(const std::string& backend_name);
00127 
00129   static bool have_backend(const std::string& backend_name);
00130 
00132   static void clear_backends();
00133 
00135   static std::string target_handler(const std::string& target, bool restrict_to_selected);
00136 
00138   static ShPointer<ShBackend> get_backend(const std::string& target);
00139 
00143   static std::list<std::string> derived_targets(const std::string& target);
00144 
00145 #if defined(WIN32)
00146   typedef void* LibraryHandle;
00147 #elif defined(__APPLE__)
00148   typedef CFBundleRef LibraryHandle;
00149 #else
00150   typedef lt_dlhandle_struct* LibraryHandle;
00151 #endif
00152   typedef std::map<std::string, LibraryHandle> LibraryMap;
00153   typedef std::map<std::string, ShPointer<ShBackend> > BackendMap;
00154   typedef std::set<std::string> BackendSet;
00155 
00156 protected:
00157   ShBackend();
00158   
00159 private:
00160   static BackendMap* m_instantiated_backends;
00161   static BackendSet* m_selected_backends;
00162   static LibraryMap* m_loaded_libraries;
00163   static bool m_done_init;
00164   static bool m_all_backends_loaded;
00165 
00167   static void init();
00168 
00171   static std::string lookup_filename(const std::string& backend_name);
00172 
00174   static bool is_valid_name(const std::string& backend_name);
00175 
00178   static bool load_library(const std::string& filename);
00179 
00181   static void load_libraries(const std::string& directory);
00182 
00185   static int target_cost(const std::string& backend_name, const std::string& target);
00186   
00188   static ShPointer<ShBackend> instantiate_backend(const std::string& backend_name);
00189 };
00190 
00191 typedef ShPointer<ShBackend> ShBackendPtr;
00192 typedef ShPointer<const ShBackend> ShBackendCPtr;
00193 
00194 }
00195 
00196 #endif

Generated on Thu Jul 28 17:33:01 2005 for Sh by  doxygen 1.4.3-20050530