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

GlBackend.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 GLBACKEND_HPP
00025 #define GLBACKEND_HPP
00026 
00027 #include "ShVariableType.hpp"
00028 #include "ShProgram.hpp"
00029 #include "ShStream.hpp"
00030 #include "ShTextureNode.hpp"
00031 
00032 #ifdef WIN32
00033 
00034 #include <windows.h>
00035 
00036 #include <GL/gl.h>
00037 #include <GL/glext.h>
00038 #include <GL/wglext.h>
00039 
00040 extern PFNGLPROGRAMSTRINGARBPROC glProgramStringARB;
00041 extern PFNGLBINDPROGRAMARBPROC glBindProgramARB;
00042 extern PFNGLGENPROGRAMSARBPROC glGenProgramsARB;
00043 extern PFNGLPROGRAMENVPARAMETER4FVARBPROC glProgramEnvParameter4fvARB;
00044 extern PFNGLPROGRAMLOCALPARAMETER4FVARBPROC glProgramLocalParameter4fvARB;
00045 extern PFNGLGETPROGRAMIVARBPROC glGetProgramivARB;
00046 extern PFNGLACTIVETEXTUREARBPROC glActiveTextureARB;
00047 
00048 extern PFNGLTEXIMAGE3DPROC glTexImage3D;
00049 extern PFNGLTEXSUBIMAGE3DPROC glTexSubImage3D;
00050 
00051 // WGL_ARB_pixel_format
00052 extern PFNWGLGETPIXELFORMATATTRIBIVARBPROC wglGetPixelFormatAttribivARB;
00053 extern PFNWGLGETPIXELFORMATATTRIBFVARBPROC wglGetPixelFormatAttribfvARB;
00054 extern PFNWGLCHOOSEPIXELFORMATARBPROC wglChoosePixelFormatARB;
00055 
00056 // WGL_ARB_pbuffer
00057 extern PFNWGLCREATEPBUFFERARBPROC wglCreatePbufferARB;
00058 extern PFNWGLGETPBUFFERDCARBPROC wglGetPbufferDCARB;
00059 extern PFNWGLRELEASEPBUFFERDCARBPROC wglReleasePbufferDCARB;
00060 extern PFNWGLDESTROYPBUFFERARBPROC wglDestroyPbufferARB;
00061 extern PFNWGLQUERYPBUFFERARBPROC wglQueryPbufferARB;
00062 
00063 extern PFNGLGETOBJECTPARAMETERIVARBPROC glGetObjectParameterivARB;
00064 extern PFNGLGETINFOLOGARBPROC glGetInfoLogARB;
00065 extern PFNGLGETSHADERSOURCEARBPROC glGetShaderSourceARB;
00066 extern PFNGLDELETEOBJECTARBPROC glDeleteObjectARB;
00067 extern PFNGLCREATESHADEROBJECTARBPROC glCreateShaderObjectARB;
00068 extern PFNGLSHADERSOURCEARBPROC glShaderSourceARB;
00069 extern PFNGLUNIFORM1FARBPROC glUniform1fARB;
00070 extern PFNGLUNIFORM2FARBPROC glUniform2fARB;
00071 extern PFNGLUNIFORM3FARBPROC glUniform3fARB;
00072 extern PFNGLUNIFORM4FARBPROC glUniform4fARB;
00073 extern PFNGLUNIFORM1IARBPROC glUniform1iARB;
00074 extern PFNGLUNIFORM2IARBPROC glUniform2iARB;
00075 extern PFNGLUNIFORM3IARBPROC glUniform3iARB;
00076 extern PFNGLUNIFORM4IARBPROC glUniform4iARB;
00077 extern PFNGLGETUNIFORMLOCATIONARBPROC glGetUniformLocationARB;
00078 extern PFNGLCOMPILESHADERARBPROC glCompileShaderARB;
00079 extern PFNGLCREATEPROGRAMOBJECTARBPROC glCreateProgramObjectARB;
00080 extern PFNGLDETACHOBJECTARBPROC glDetachObjectARB;
00081 extern PFNGLATTACHOBJECTARBPROC glAttachObjectARB;
00082 extern PFNGLLINKPROGRAMARBPROC glLinkProgramARB;
00083 extern PFNGLUSEPROGRAMOBJECTARBPROC glUseProgramObjectARB;
00084 extern PFNGLVALIDATEPROGRAMARBPROC glValidateProgramARB;
00085 
00086 // extern PFN ARBPROC ARB;
00087 
00088 #else
00089 
00090 #define GL_GLEXT_VERBOSE 1
00091 #define GL_GLEXT_PROTOTYPES 1
00092 
00093 #ifdef __APPLE__
00094 #include <OpenGL/OpenGL.h>
00095 #include <OpenGL/gl.h>
00096 #include <OpenGL/glext.h>
00097 #else
00098 #include <GL/gl.h>
00099 #include <GL/glext.h>
00100 #include <GL/glx.h>
00101 #endif /* __APPLE */
00102 
00103 #endif /* WIN32 */
00104 
00105 namespace shgl {
00106 
00107 struct TextureStrategy {
00108   virtual ~TextureStrategy() {}
00109   virtual TextureStrategy* create() = 0;
00110   virtual void bindTexture(const SH::ShTextureNodePtr& texture,
00111                            GLenum target) = 0;
00112 };
00113 
00114 struct StreamStrategy {
00115   virtual ~StreamStrategy() {}
00116   virtual StreamStrategy* create() = 0;
00117   virtual void execute(const SH::ShProgramNodeCPtr& program, SH::ShStream& dest) = 0;
00118 };
00119 
00120 struct CodeStrategy {
00121   virtual ~CodeStrategy() {}
00122   virtual CodeStrategy* create() = 0;
00123   virtual SH::ShBackendCodePtr generate(const std::string& target,
00124                                         const SH::ShProgramNodeCPtr& shader,
00125                                         TextureStrategy* texture) = 0;
00126 
00127   // If you want to use a special set generation function, override
00128   // generate_set, and override use_default_set() to return false.
00129   virtual SH::ShBackendSetPtr generate_set(const SH::ShProgramSet& s);
00130   virtual bool use_default_set() const;
00131 
00132   virtual void unbind_all_programs();
00133   virtual bool use_default_unbind_all() const;
00134 };
00135 
00136 class GlBackend : public SH::ShBackend {
00137 public:
00138   virtual SH::ShBackendCodePtr generate_code(const std::string& target,
00139                                              const SH::ShProgramNodeCPtr& shader);
00140   virtual SH::ShBackendSetPtr generate_set(const SH::ShProgramSet& s);
00141   virtual void unbind_all_programs();
00142 
00143   // execute a stream program, if supported
00144   virtual void execute(const SH::ShProgramNodeCPtr& program, SH::ShStream& dest);
00145 
00146 protected:
00147   GlBackend(CodeStrategy* code, TextureStrategy* texture, StreamStrategy* stream);
00148   
00149 private:
00150   CodeStrategy* m_code;
00151   TextureStrategy* m_texture;
00152   StreamStrategy* m_stream;
00153 
00154   // NOT IMPLEMENTED
00155   GlBackend(const GlBackend& other);
00156   GlBackend& operator=(const GlBackend& other);
00157 };
00158 
00159 void shGlCheckError(const char* desc, const char* file, int line);
00160 
00161 /* Returns glReadPixels/glTexImage type for a given value type 
00162  * and returns a value type for the temporary buffer
00163  * (or SH_VALUETYPE_END if we can use the original buffer directly) 
00164  */
00165 GLenum shGlType(SH::ShValueType valueType, SH::ShValueType &convertedType); 
00166 
00167 }
00168 
00169 #define SH_GL_CHECK_ERROR(op) \
00170   op;shGlCheckError( # op, (char*) __FILE__, (int) __LINE__);
00171 
00172 
00173 
00174 #endif

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