gpucalc::GLGLSLShader Class Reference

#include <gl_glsl_shader.h>

Inheritance diagram for gpucalc::GLGLSLShader:

Inheritance graph

List of all members.

Public Member Functions

void addUniform (Uniform *uniform)
void bind ()
 Bind our object.
void compile (const std::string &Parameters=std::string())
 Compiles current shader.
const std::string & getClassName () const
 Returns name of class of our object.
std::string getLanguageID () const
const std::string & getObjectName () const
 Returns name of our object.
std::string getSource () const
 Returns current source of shader.
void getSpecificParameter (const std::string &ParameterName, void *ParameterValue)
 Method for obtaining some specific parameters, such as texture specific params, and so on.
 GLGLSLShader (const std::string &ShaderName, ShaderSystem *system)
bool isActive () const
bool isCompiled () const
 Simple method for checking compilation status.
bool isDebugEnabled () const
 Check for debugging our shader.
bool isLocked () const
 returns state of object.
void preCompute ()
 Precompute shader.
void removeUniform (Uniform *uniform)
 Remove uniform variable from shader.
void setSource (const std::string &ShaderSource)
 Performs basic initialization of shader and sets up shader source.
void unbind ()
 Unbind our object.
void validate ()
 Validate Shader.
 ~GLGLSLShader ()

Protected Member Functions

void addSpecificParameter (const std::string &ParameterName, void *ParameterValue, size_t Size)
void lock ()
void unlock ()

Private Types

typedef std::list< Uniform * > UniformVector

Private Member Functions

void compileStatus ()
GLint getUniformLocation (const std::string &Name)
void linkStatus ()
void ProgramInfoLog ()
void setUniformSampler (Uniform *uniform)
void setUniformVariable (Uniform *uniform)
void ShaderInfoLog ()

Private Attributes

GLhandleARB mGLSLFragmentShaderID
GLhandleARB mGLSLProgramID
bool mIsCompiled
bool mIsDebug
GLint mMultiTextureNumber
std::string mShaderCompilerParams
std::string mShaderSource
UniformVector mUniformVector

Friends

class GLGLSLShaderSystem


Detailed Description

Definition at line 45 of file gl_glsl_shader.h.


Member Typedef Documentation

typedef std::list<Uniform *> gpucalc::GLGLSLShader::UniformVector [private]

Definition at line 116 of file gl_glsl_shader.h.


Constructor & Destructor Documentation

gpucalc::GLGLSLShader::GLGLSLShader ( const std::string &  ShaderName,
ShaderSystem system 
)

Definition at line 46 of file gl_glsl_shader.cpp.

00046                                                                               :
00047   Object(_GLGLSLShaderClassName, ShaderName), Shader(_GLGLSLShaderClassName, ShaderName, system),
00048   mGLSLProgramID(0), mGLSLFragmentShaderID(0), mMultiTextureNumber(0), mUniformVector(),
00049   mIsCompiled(false), mIsDebug(false), mShaderSource(), mShaderCompilerParams()
00050  {
00051   mGLSLProgramID = glCreateProgramObjectARB();
00052   LogManager::getSingleton().logMessage(this, "GLSL program created with OpenGL ID: " +
00053    auxillary::StringUtil::toString(mGLSLProgramID), LML_Trivial);
00054 
00055   mGLSLFragmentShaderID = glCreateShaderObjectARB(GL_FRAGMENT_SHADER_ARB);
00056   LogManager::getSingleton().logMessage(this, "GLSL fragment shader created with OpenGL ID: " +
00057    auxillary::StringUtil::toString(mGLSLFragmentShaderID), LML_Trivial);
00058   
00059   glAttachObjectARB(mGLSLProgramID, mGLSLFragmentShaderID);
00060  }

gpucalc::GLGLSLShader::~GLGLSLShader (  ) 

Definition at line 63 of file gl_glsl_shader.cpp.

00064  {
00065   glDetachShader(mGLSLProgramID, mGLSLFragmentShaderID);
00066   glDeleteShader(mGLSLFragmentShaderID);
00067   glDeleteProgram(mGLSLProgramID);
00068   LogManager::getSingleton().logMessage(this, "deleted.", LML_Trivial);
00069  }


Member Function Documentation

void gpucalc::Object::addSpecificParameter ( const std::string &  ParameterName,
void *  ParameterValue,
size_t  Size 
) [inline, protected, inherited]

Definition at line 254 of file object.h.

00255    {
00256     SpecificParametersMap::iterator i = mSpecificParameters.find(ParameterName);
00257     if (i != mSpecificParameters.end())
00258     {
00259      mSpecificParameters.erase(i);
00260     }
00261     mSpecificParameters[ParameterName] = Any(ParameterValue, Size);
00262    }

void gpucalc::GLGLSLShader::addUniform ( Uniform uniform  )  [virtual]

Set uniform variable, declared in our shader.

Parameters:
uniform - pointer to class Uniform, that contain all necessary members for correct setting uniform variable.

Implements gpucalc::Shader.

Definition at line 79 of file gl_glsl_shader.cpp.

00080  {
00081   if (addToContainer(uniform, mUniformVector))
00082   {
00083    uniform->uploadToGPU();
00084   }
00085  }

void gpucalc::Shader::bind (  )  [inline, virtual, inherited]

Bind our object.

For example, set in some active state (Texture, Shader and so on)

Implements gpucalc::Bindable.

Definition at line 140 of file shader.h.

00141    {
00142     mDefaultShaderSystem->bind(this);
00143    }

void gpucalc::GLGLSLShader::compile ( const std::string &  Parameters = std::string()  )  [virtual]

Compiles current shader.

Implements gpucalc::Shader.

Definition at line 178 of file gl_glsl_shader.cpp.

00179  {
00180   // needs for recompilation shader
00181   mShaderCompilerParams = Parameters;
00182 
00183   LogManager::getSingleton().logMessage(this, "This type of shader cannot use compiler parameters. Following parameters will be ignored: \"" +
00184    Parameters + "\".", LML_Critical);
00185 
00186   const char * src = mShaderSource.c_str();
00187 
00188   glShaderSource(mGLSLFragmentShaderID, 1, &src, NULL);
00189   glCompileShader(mGLSLFragmentShaderID);
00190   compileStatus();
00191   LogManager::getSingleton().logMessage(this, "Shader compiled.", LML_Trivial);
00192 
00193   glLinkProgram(mGLSLProgramID);
00194   linkStatus();
00195   LogManager::getSingleton().logMessage(this, "Program linked.", LML_Trivial);
00196 
00197 
00198   if (LogManager::getSingleton().getLogDetail() == LL_Verbose)
00199   {
00200    ShaderInfoLog();
00201    ProgramInfoLog();
00202   }
00203   mIsCompiled = true;
00204  }

void gpucalc::GLGLSLShader::compileStatus (  )  [private]

Definition at line 250 of file gl_glsl_shader.cpp.

00251  {
00252   GLint compile_status = 0;
00253   glGetShaderiv(mGLSLFragmentShaderID, GL_COMPILE_STATUS, &compile_status);
00254   if (compile_status != GL_TRUE)
00255   {
00256    ShaderInfoLog();
00257    Except<ERR_SOURCE_COMPILATION_ERROR>(this, "Cannot compile!", "GLGLSLShader::compileStatus()", __FILE__, __LINE__);
00258   }
00259  }

const std::string& gpucalc::Object::getClassName (  )  const [inline, inherited]

Returns name of class of our object.

Definition at line 92 of file object.h.

00093    {
00094     return mClassName;
00095    }

std::string gpucalc::GLGLSLShader::getLanguageID (  )  const [inline, virtual]

Implements gpucalc::Shader.

Definition at line 100 of file gl_glsl_shader.h.

00101    {
00102     return "glsl";
00103    }

const std::string& gpucalc::Object::getObjectName (  )  const [inline, inherited]

Returns name of our object.

Definition at line 101 of file object.h.

00102    {
00103     return mObjectName;
00104    }

std::string gpucalc::GLGLSLShader::getSource (  )  const [inline, virtual]

Returns current source of shader.

Implements gpucalc::Shader.

Definition at line 64 of file gl_glsl_shader.h.

00065    {
00066     return mShaderSource;
00067    }

void gpucalc::Object::getSpecificParameter ( const std::string &  ParameterName,
void *  ParameterValue 
) [inline, inherited]

Method for obtaining some specific parameters, such as texture specific params, and so on.

Parameters:
ParameterName - name of parameter, usually it similar to specific method of some object (for example, GLTextureID)
ParameterValue - contain value of named parameter, it must be allocated and have sufficient size.
Todo:
Think for better realization.

Definition at line 128 of file object.h.

00129    {
00130     SpecificParametersMap::iterator i = mSpecificParameters.find(ParameterName);
00131     if (i != mSpecificParameters.end())
00132     {
00133      i->second.copyValue(ParameterValue);
00134     }
00135     else
00136     {
00137      Except<ERR_ITEM_NOT_FOUND>(this, "Parameter \"" + ParameterName + "\" does not exists.", "Object::getSpecificParameter()", __FILE__, __LINE__);
00138     }
00139    }

GLint gpucalc::GLGLSLShader::getUniformLocation ( const std::string &  Name  )  [private]

Definition at line 309 of file gl_glsl_shader.cpp.

00310  {
00311   GLint Result = glGetUniformLocation(mGLSLProgramID, Name.c_str());
00312   if (Result == -1)
00313   {
00314    Warning<ERR_ITEM_NOT_FOUND>(this, "Uniform variable \"" + Name + "\" cannot be found in shader", "GLGLSLShader::getUniformLocation", __FILE__, __LINE__);
00315   }
00316   return Result;
00317  }

bool gpucalc::Shader::isActive (  )  const [inline, virtual, inherited]

Implements gpucalc::Bindable.

Definition at line 134 of file shader.h.

00135    {
00136     return (this == mDefaultShaderSystem->getActiveShader());
00137    }

bool gpucalc::GLGLSLShader::isCompiled (  )  const [inline, virtual]

Simple method for checking compilation status.

Implements gpucalc::Shader.

Definition at line 82 of file gl_glsl_shader.h.

00083    {
00084     return mIsCompiled;
00085    }

bool gpucalc::GLGLSLShader::isDebugEnabled (  )  const [inline, virtual]

Check for debugging our shader.

Implements gpucalc::Shader.

Definition at line 94 of file gl_glsl_shader.h.

00095    {
00096     return mIsDebug;
00097    }

bool gpucalc::Object::isLocked (  )  const [inline, inherited]

returns state of object.

If object is locked then we cannot change some state of it, because object is used in computation. If object is unlocked then we can change some state of it, object is not in computation.

Definition at line 113 of file object.h.

00114    {
00115     return mIsLocked;
00116    }

void gpucalc::GLGLSLShader::linkStatus (  )  [private]

Definition at line 280 of file gl_glsl_shader.cpp.

00281  {
00282   GLint link_status;
00283   glGetProgramiv(mGLSLProgramID, GL_LINK_STATUS, &link_status);
00284   if (link_status != GL_TRUE)
00285   {
00286    ProgramInfoLog();
00287    Except<ERR_SOURCE_COMPILATION_ERROR>(this, "Cannot link!", "GLGLSLShader::linkStatus()", __FILE__, __LINE__);
00288   }
00289  }

void gpucalc::Object::lock (  )  [inline, protected, inherited]

Definition at line 265 of file object.h.

00266    {
00267     mIsLocked = true;
00268    }

void gpucalc::GLGLSLShader::preCompute (  )  [virtual]

Precompute shader.

Implements gpucalc::Shader.

Definition at line 207 of file gl_glsl_shader.cpp.

00208  {
00209   if (!mIsCompiled)
00210   {
00211    compile(mShaderCompilerParams);
00212   }
00213 
00214   mMultiTextureNumber = 0;
00215   bind();
00216   for (UniformVector::iterator i = mUniformVector.begin(); i != mUniformVector.end(); ++i)
00217   {
00218    Uniform * uniform = (*i);
00219    Uniform::UniformType uniform_type = uniform->getUniformType();
00220    if ((uniform_type == Uniform::UT_TEX_1) || (uniform_type == Uniform::UT_TEX_2))
00221    {
00222     setUniformSampler(uniform);
00223    }
00224    else
00225    {
00226     setUniformVariable(uniform);
00227    }
00228   }
00229  }

void gpucalc::GLGLSLShader::ProgramInfoLog (  )  [private]

Definition at line 292 of file gl_glsl_shader.cpp.

00293  {
00294   GLint infologLength = 0;
00295   GLint charsWritten  = 0;
00296   char * infoLog;
00297   glGetProgramiv(mGLSLProgramID, GL_INFO_LOG_LENGTH, &infologLength);
00298   if (infologLength > 1)
00299   {
00300    infoLog = new char [infologLength];
00301    glGetProgramInfoLog(mGLSLProgramID, infologLength, &charsWritten, infoLog);
00302    LogManager::getSingleton().logMessage(this, "Log of program compilation:", LML_Critical);
00303    LogManager::getSingleton().logMessage(this, infoLog, LML_Critical);
00304    delete [] infoLog;
00305   }
00306  }

void gpucalc::GLGLSLShader::removeUniform ( Uniform uniform  )  [virtual]

Remove uniform variable from shader.

Implements gpucalc::Shader.

Definition at line 88 of file gl_glsl_shader.cpp.

00089  {
00090   removeFromContainer(uniform, mUniformVector);
00091  }

void gpucalc::GLGLSLShader::setSource ( const std::string &  ShaderSource  )  [virtual]

Performs basic initialization of shader and sets up shader source.

Depending on shader type this method can compile shader or not.

Implements gpucalc::Shader.

Definition at line 72 of file gl_glsl_shader.cpp.

00073  {
00074   mShaderSource = ShaderSource;
00075   mIsCompiled = false;
00076  }

void gpucalc::GLGLSLShader::setUniformSampler ( Uniform uniform  )  [private]

Definition at line 157 of file gl_glsl_shader.cpp.

00158  {
00159   Texture * Sampler = uniform->getSampler();
00160   if (!Sampler->isUploadedToGPU())
00161   {
00162    Sampler->uploadToGPU();
00163   }
00164 
00165   GLint Location = getUniformLocation(Sampler->getObjectName());
00166   if (Location != -1)
00167   {
00168    glActiveTextureARB(GL_TEXTURE0_ARB + mMultiTextureNumber);
00169    Sampler->bind();
00170 
00171    glUniform1i(Location, mMultiTextureNumber);
00172    ++mMultiTextureNumber;
00173    LogManager::getSingleton().logMessage(this, "Uniform sampler \"" + uniform->getObjectName() + "\" successfully set.", LML_Trivial);
00174   }
00175  }

void gpucalc::GLGLSLShader::setUniformVariable ( Uniform uniform  )  [private]

Definition at line 94 of file gl_glsl_shader.cpp.

00095  {
00096   GLint Location = getUniformLocation(uniform->getObjectName());
00097   if (Location != -1)
00098   {
00099    switch (uniform->getUniformType())
00100    {
00101     case Uniform::UT_INT_1:
00102      glUniform1iv(Location, uniform->getActualLengthGPU(), static_cast<const int *>(uniform->pointer()));
00103      break;
00104 
00105     case Uniform::UT_INT_2:
00106      glUniform2iv(Location, uniform->getActualLengthGPU(), static_cast<const int *>(uniform->pointer()));
00107      break;
00108 
00109     case Uniform::UT_INT_3:
00110      glUniform3iv(Location, uniform->getActualLengthGPU(), static_cast<const int *>(uniform->pointer()));
00111      break;
00112 
00113     case Uniform::UT_INT_4:
00114      glUniform4iv(Location, uniform->getActualLengthGPU(), static_cast<const int *>(uniform->pointer()));
00115     break;
00116 
00117     case Uniform::UT_FLOAT_1:
00118      glUniform1fv(Location, uniform->getActualLengthGPU(), static_cast<const float *>(uniform->pointer()));
00119      break;
00120 
00121 
00122     case Uniform::UT_FLOAT_2:
00123      glUniform2fv(Location, uniform->getActualLengthGPU(), static_cast<const float *>(uniform->pointer()));
00124      break;
00125 
00126     case Uniform::UT_FLOAT_3:
00127      glUniform3fv(Location, uniform->getActualLengthGPU(), static_cast<const float *>(uniform->pointer()));
00128      break;
00129    
00130    case Uniform::UT_FLOAT_4:
00131     glUniform4fv(Location, uniform->getActualLengthGPU(), static_cast<const float *>(uniform->pointer()));
00132     break;
00133 
00134 
00135     case Uniform::UT_MAT_2X2:
00136      glUniformMatrix2fv(Location, uniform->getActualLengthGPU(), false, static_cast<const float *>(uniform->pointer()));
00137      break;
00138 
00139     case Uniform::UT_MAT_3X3:
00140      glUniformMatrix3fv(Location, uniform->getActualLengthGPU(), false, static_cast<const float *>(uniform->pointer()));
00141      break;
00142 
00143     case Uniform::UT_MAT_4X4:
00144      glUniformMatrix4fv(Location, uniform->getActualLengthGPU(), false, static_cast<const float *>(uniform->pointer()));
00145      break;
00146 
00147     default:
00148      Except<ERR_NOT_IMPLEMENTED>(this, "Other uniform types not implemented.", "GLGLSLShader::setUniformVariable", __FILE__, __LINE__);
00149      break;
00150    }
00151 
00152    LogManager::getSingleton().logMessage(this, "Uniform variable \"" + uniform->getObjectName() + "\" successfuly set.", LML_Trivial);
00153   }
00154  }

void gpucalc::GLGLSLShader::ShaderInfoLog (  )  [private]

Definition at line 262 of file gl_glsl_shader.cpp.

00263  {
00264   GLint infologLength = 0;
00265   GLint charsWritten  = 0;
00266   char * infoLog;
00267 
00268   glGetShaderiv(mGLSLFragmentShaderID, GL_INFO_LOG_LENGTH, &infologLength);
00269   if (infologLength > 1)
00270   {
00271    infoLog = new char [infologLength];
00272    glGetShaderInfoLog(mGLSLFragmentShaderID, infologLength, &charsWritten, infoLog);
00273    LogManager::getSingleton().logMessage(this, "Log of shader compilation:", LML_Critical);
00274    LogManager::getSingleton().logMessage(this, infoLog, LML_Critical);
00275    delete [] infoLog;
00276   }
00277  }

void gpucalc::Shader::unbind (  )  [inline, virtual, inherited]

Unbind our object.

For example, set in some inactive state (Texture, Shader and so on)

Implements gpucalc::Bindable.

Definition at line 146 of file shader.h.

00147    {
00148     mDefaultShaderSystem->unbind(this);
00149    }

void gpucalc::Object::unlock (  )  [inline, protected, inherited]

Definition at line 271 of file object.h.

00272    {
00273     mIsLocked = false;
00274    }

void gpucalc::GLGLSLShader::validate (  )  [virtual]

Validate Shader.

This method checks that all uniform variables are set. Also it checks that this program is ready for use.

Implements gpucalc::Shader.

Definition at line 232 of file gl_glsl_shader.cpp.

00233  {
00234   GLint validate_status = 0;
00235   glValidateProgram(mGLSLProgramID);
00236   glGetProgramiv(mGLSLProgramID, GL_VALIDATE_STATUS, &validate_status);
00237   if (validate_status != GL_TRUE)
00238   {
00239    Warning<ERR_SOURCE_COMPILATION_ERROR>(this, "Does not check validation!", "GLGLSLShader::validateShader", __FILE__, __LINE__);
00240    ProgramInfoLog();
00241   }
00242   else
00243   {
00244    LogManager::getSingleton().logMessage(this, "Validated, shader is ready for computation.", LML_Trivial);
00245   }
00247  }


Friends And Related Function Documentation

friend class GLGLSLShaderSystem [friend]

Definition at line 51 of file gl_glsl_shader.h.


Member Data Documentation

Definition at line 110 of file gl_glsl_shader.h.

Definition at line 107 of file gl_glsl_shader.h.

Definition at line 120 of file gl_glsl_shader.h.

Definition at line 123 of file gl_glsl_shader.h.

Definition at line 113 of file gl_glsl_shader.h.

Definition at line 129 of file gl_glsl_shader.h.

std::string gpucalc::GLGLSLShader::mShaderSource [private]

Definition at line 126 of file gl_glsl_shader.h.

Definition at line 117 of file gl_glsl_shader.h.


The documentation for this class was generated from the following files:

Generated on Thu Mar 5 22:36:43 2009 for gpucalc by  doxygen 1.5.6