Back to index

nux  3.0.0
IOpenGLGLSLShader.h
Go to the documentation of this file.
00001 /*
00002  * Copyright 2010 Inalogic® Inc.
00003  *
00004  * This program is free software: you can redistribute it and/or modify it
00005  * under the terms of the GNU Lesser General Public License, as
00006  * published by the  Free Software Foundation; either version 2.1 or 3.0
00007  * of the License.
00008  *
00009  * This program is distributed in the hope that it will be useful, but
00010  * WITHOUT ANY WARRANTY; without even the implied warranties of
00011  * MERCHANTABILITY, SATISFACTORY QUALITY or FITNESS FOR A PARTICULAR
00012  * PURPOSE.  See the applicable version of the GNU Lesser General Public
00013  * License for more details.
00014  *
00015  * You should have received a copy of both the GNU Lesser General Public
00016  * License along with this program. If not, see <http://www.gnu.org/licenses/>
00017  *
00018  * Authored by: Jay Taoko <jaytaoko@inalogic.com>
00019  *
00020  */
00021 
00022 
00023 #ifndef IOPENGLSHADER_H
00024 #define IOPENGLSHADER_H
00025 
00026 namespace nux
00027 {
00028 
00029   class IOpenGLResource;
00030 
00031   bool ExtractShaderString3(const NString &ShaderToken, const NString &ShaderSource, NString &RetSource, NString ShaderPreprocessorDefines = NString(""));
00032 
00033   class IOpenGLShader:  public IOpenGLResource
00034   {
00035     NUX_DECLARE_OBJECT_TYPE(IOpenGLShader, IOpenGLResource);
00036     IOpenGLShader(NString ShaderName, OpenGLResourceType ResourceType);
00037     virtual ~IOpenGLShader();
00038   public:
00039     virtual void SetShaderCode(const ANSICHAR *ShaderCode, const char *ShaderPreprocessorDefines = "") = 0;
00040     virtual bool Compile() = 0;
00041     virtual bool IsValid() = 0;
00042     NString _ShaderName;
00043     NString _ShaderCode;
00044   };
00045 
00046   class IOpenGLVertexShader:  public IOpenGLShader
00047   {
00048     NUX_DECLARE_OBJECT_TYPE(IOpenGLVertexShader, IOpenGLShader);
00049   public:
00050     virtual ~IOpenGLVertexShader();
00051     virtual void SetShaderCode(const ANSICHAR *ShaderCode, const char *VtxShaderPreprocessorDefines = "");
00052     virtual bool Compile();
00053     virtual bool IsValid();
00054   private:
00055     IOpenGLVertexShader(NString ShaderName = NString("Vertex Shader"));
00056     int m_CompiledAndReady;
00057     friend class GpuDevice;
00058   };
00059 
00060   class IOpenGLPixelShader:  public IOpenGLShader
00061   {
00062     NUX_DECLARE_OBJECT_TYPE(IOpenGLPixelShader, IOpenGLShader);
00063   public:
00064     virtual ~IOpenGLPixelShader();
00065     virtual void SetShaderCode(const ANSICHAR *ShaderCode, const char *FrgShaderPreprocessorDefines = "");
00066     virtual bool Compile();
00067     virtual bool IsValid();
00068   private:
00069     IOpenGLPixelShader(NString ShaderName = NString("Fragment Shader"));
00070     int m_CompiledAndReady;
00071     friend class GpuDevice;
00072   };
00073 
00074 #if 0
00075   class IOpenGLGeometryShader: public IOpenGLShader
00076   {
00077     NUX_DECLARE_OBJECT_TYPE(IOpenGLGeometryShader, IOpenGLShader);
00078   public:
00079     virtual ~IOpenGLGeometryShader();
00080     virtual void SetShaderCode(const ANSICHAR *ShaderCode, const char *GeometryShaderPreprocessorDefines = "");
00081     virtual bool Compile();
00082     virtual bool IsValid();
00083 
00084     void SetInputPrimitiveType(GLenum type);
00085     void SetOutputPrimitiveType(GLenum type);
00086     void SetMaxVertexOutput(int max_vertex_output);
00087 
00088   private:
00089     IOpenGLGeometryShader(NString ShaderName = NString("Geometry Shader"));
00090     int m_CompiledAndReady;
00091     friend class GpuDevice;
00092   };
00093 #endif
00094 
00095   class IOpenGLShaderProgram:  public IOpenGLResource
00096   {
00097     NUX_DECLARE_OBJECT_TYPE(IOpenGLShaderProgram, IOpenGLResource);
00098   public:
00099     virtual ~IOpenGLShaderProgram();
00100 
00101     // Load an inalogic shader.
00102     void LoadVertexShader(const char *glslshader, const char *VtxShaderPreprocessorDefines = "");
00103     void LoadPixelShader(const char *glslshader, const char *FrgShaderPreprocessorDefines = "");
00104     void LoadIShaderFile(const char *ShaderFileName, const char *VtxShaderPreprocessorDefines = "", const char *FrgShaderPreprocessorDefines = "");
00105     void LoadIShader(const char *ShaderCode, const char *VtxShaderPreprocessorDefines = "", const char *FrgShaderPreprocessorDefines = "");
00106     void AddShaderParameter(GLShaderParameter *Parameter);
00107     void AddShaderObject(ObjectPtr<IOpenGLShader> ShaderObject);
00108     void RemoveShaderObject(ObjectPtr<IOpenGLShader> ShaderObject);
00109     void ClearShaderObjects();
00110     bool Link();
00111 
00112     void Begin();
00113     void End();
00114 
00115     static void SetShaderTracking(bool enabled);
00116 
00117   public:
00118 
00119     bool SetUniform1f(char *varname, GLfloat v0);
00120     bool SetUniform2f(char *varname, GLfloat v0, GLfloat v1);
00121     bool SetUniform3f(char *varname, GLfloat v0, GLfloat v1, GLfloat v2);
00122     bool SetUniform4f(char *varname, GLfloat v0, GLfloat v1, GLfloat v2, GLfloat v3);
00123 
00124     bool SetUniform1i(char *varname, GLint v0);
00125     bool SetUniform2i(char *varname, GLint v0, GLint v1);
00126     bool SetUniform3i(char *varname, GLint v0, GLint v1, GLint v2);
00127     bool SetUniform4i(char *varname, GLint v0, GLint v1, GLint v2, GLint v3);
00128 
00129     bool SetUniform1fv(char *varname, GLsizei count, GLfloat *value);
00130     bool SetUniform2fv(char *varname, GLsizei count, GLfloat *value);
00131     bool SetUniform3fv(char *varname, GLsizei count, GLfloat *value);
00132     bool SetUniform4fv(char *varname, GLsizei count, GLfloat *value);
00133 
00134     bool SetUniform1iv(char *varname, GLsizei count, GLint *value);
00135     bool SetUniform2iv(char *varname, GLsizei count, GLint *value);
00136     bool SetUniform3iv(char *varname, GLsizei count, GLint *value);
00137     bool SetUniform4iv(char *varname, GLsizei count, GLint *value);
00138 
00139     bool SetUniformMatrix2fv(char *varname, GLsizei count, GLboolean transpose, GLfloat *value);
00140     bool SetUniformMatrix3fv(char *varname, GLsizei count, GLboolean transpose, GLfloat *value);
00141     bool SetUniformMatrix4fv(char *varname, GLsizei count, GLboolean transpose, GLfloat *value);
00142 
00143 
00144     bool SetUniform1f(GLint loc, GLfloat v0);
00145     bool SetUniform2f(GLint loc, GLfloat v0, GLfloat v1);
00146     bool SetUniform3f(GLint loc, GLfloat v0, GLfloat v1, GLfloat v2);
00147     bool SetUniform4f(GLint loc, GLfloat v0, GLfloat v1, GLfloat v2, GLfloat v3);
00148 
00149     bool SetUniform1i(GLint loc, GLint v0);
00150     bool SetUniform2i(GLint loc, GLint v0, GLint v1);
00151     bool SetUniform3i(GLint loc, GLint v0, GLint v1, GLint v2);
00152     bool SetUniform4i(GLint loc, GLint v0, GLint v1, GLint v2, GLint v3);
00153 
00154     bool SetUniform1fv(GLint loc, GLsizei count, GLfloat *value);
00155     bool SetUniform2fv(GLint loc, GLsizei count, GLfloat *value);
00156     bool SetUniform3fv(GLint loc, GLsizei count, GLfloat *value);
00157     bool SetUniform4fv(GLint loc, GLsizei count, GLfloat *value);
00158 
00159     bool SetUniform1iv(GLint loc, GLsizei count, GLint *value);
00160     bool SetUniform2iv(GLint loc, GLsizei count, GLint *value);
00161     bool SetUniform3iv(GLint loc, GLsizei count, GLint *value);
00162     bool SetUniform4iv(GLint loc, GLsizei count, GLint *value);
00163 
00164     bool SetUniformLocMatrix2fv(GLint loc, GLsizei count, GLboolean transpose, GLfloat *value);
00165     bool SetUniformLocMatrix3fv(GLint loc, GLsizei count, GLboolean transpose, GLfloat *value);
00166     bool SetUniformLocMatrix4fv(GLint loc, GLsizei count, GLboolean transpose, GLfloat *value);
00167 
00168     void GetUniformfv(char *name, GLfloat *values);
00169     void GetUniformiv(char *name, GLint *values);
00170     int GetUniformLocationARB(const GLchar *name);
00171     void GetActiveUniformARB(GLuint index,
00172                               GLsizei maxLength,
00173                               GLsizei *length,
00174                               GLint *size,
00175                               GLenum *type,
00176                               GLchar *name);
00177 
00178     //    glGetObjectParameterARBfv Parameters
00179     //        object
00180     //        Specifies the handle of the object to be queried
00181     //
00182     //        pname
00183     //        Specifies the an object parameter. Accepted symbolic names are
00184     //        GL_OBJECT_TYPE_ARB ,
00185     //        GL_OBJECT_SUBTYPE_ARB,
00186     //        GL_OBJECT_DELETE_STATUS_ARB,
00187     //        GL_OBJECT_LINK_STATUS_ARB,
00188     //        GL_OBJECT_VALIDATE_STATUS_ARB,
00189     //        GL_OBJECT_COMPILE_STATUS_ARB,
00190     //        GL_OBJECT_INFO_LOG_LENGTH_ARB,
00191     //        GL_OBJECT_ATTACHED_OBJECTS_ARB,
00192     //        GL_OBJECT_ACTIVE_ATTRIBUTES_ARB,
00193     //        GL_OBJECT_ACTIVE_ATTRIBUTE_MAX_LENGTH_ARB,
00194     //        GL_OBJECT_ACTIVE_UNIFORMS_ARB,
00195     //        GL_OBJECT_ACTIVE_UNIFORM_MAX_LENGTH_ARB,
00196     //        GL_OBJECT_SHADER_SOURCE_LENGTH_ARB
00197     //
00198     //        params
00199     //        Returns the requested object parameter
00200     //
00201     void GetObjectParameterfvARB(GLenum pname, GLfloat *params);
00202     bool SetSampler(char *name, int exture_unit);
00203 
00204     GLShaderParameter *_FirstParameter;
00205     void CheckAttributeLocation();
00206     void CheckUniformLocation();
00207     int GetAttributeLocation(const char *AttributeName);
00208 
00209   private:
00210     IOpenGLShaderProgram(NString ShaderProgramName = NString("ShaderProgram"));
00211     ShaderAttributeDefinition m_ProgramAttributeDefinition[16/*NUM_VERTEX_SHADER_INPUT_ATTRIBUTE*/];
00212     std::vector<ObjectPtr<IOpenGLShader> > ShaderObjectList;
00213     bool m_CompiledAndReady;
00214     NString _ShaderProgramName;
00215     friend class GpuDevice;
00216   };
00217 
00218 }
00219 
00220 #endif // IOPENGLSHADER_H