gl_glsl_shader_system.cpp

Go to the documentation of this file.
00001 /***************************************************************************
00002  *   Copyright (C) 2008 by Kutumov Alexey                                  *
00003  *   ru.pixel@gmail.com                                                    *
00004  *                                                                         *
00005  *   This program is free software; you can redistribute it and/or modify  *
00006  *   it under the terms of the GNU General Public License as published by  *
00007  *   the Free Software Foundation; either version 2 of the License, or     *
00008  *   (at your option) any later version.                                   *
00009  *                                                                         *
00010  *   This program 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         *
00013  *   GNU General Public License for more details.                          *
00014  *                                                                         *
00015  *   You should have received a copy of the GNU General Public License     *
00016  *   along with this program; if not, write to the                         *
00017  *   Free Software Foundation, Inc.,                                       *
00018  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
00019  ***************************************************************************/
00020 
00033 #include "gl_glsl_shader_system/gl_glsl_shader_system.h"
00034 #include "gl_glsl_shader_system/gl_glsl_code_instructor.h"
00035 #include "gl_glsl_shader_system/gl_glsl_shader.h"
00036 
00037 #include "gpucalc/log_manager.h"
00038 #include "gpucalc/text_file_loader.h"
00039 #include "gpucalc/data.h"
00040 #include "gpucalc/texture.h"
00041 #include "gpucalc/string_util.h"
00042 #include "gpucalc/debugger/debugger.h"
00043 
00044 
00045 
00046 namespace gpucalc
00047 {
00048 
00049 
00050  GLGLSLShaderSystem::GLGLSLShaderSystem(): ShaderSystem(_GLGLSLShaderSystemObjectName), mShaderMap(), mUniformMap(), mAcceptableGraphicCoreIDset(),
00051   mIsStarted(false), mActiveShader(0), mCodeInstructor(0)
00052  {
00053        // mAcceptableGraphicCoreIDset.insert("GLGraphicCore"); // added standart gl graphic core
00054  }
00055 
00056 
00057  GLGLSLShaderSystem::~GLGLSLShaderSystem()
00058  {
00059   if (mIsStarted)
00060   {
00061    stop();
00062   }
00063 
00064   LogManager::getSingleton().logMessage(this, "Successfully destroyed.", LML_Normal);
00065  }
00066 
00067 
00068  Shader * GLGLSLShaderSystem::create(const std::string & ShaderName)
00069  {
00070   assert(mIsStarted && "GLGLSLShaderSystem is not started!!!");
00071   ShaderMap::iterator i = mShaderMap.find(ShaderName);
00072   if (i != mShaderMap.end())
00073   {
00074    Except<ERR_DUPLICATE_ITEM>(this, "Shader with name \"" + ShaderName + "\" already exists.", "GLGLSLShaderSystem::create()", __FILE__, __LINE__);
00075    return 0;
00076   }
00077   else
00078   {
00079    GLGLSLShader * Result = new GLGLSLShader(ShaderName, this);
00080    mShaderMap.insert(std::make_pair(ShaderName, Result));
00081    LogManager::getSingleton().logMessage(this, "Shader \"" + ShaderName + "\" succesfully created.", LML_Trivial);
00082    return Result;
00083   }
00084  }
00085 
00086 
00087  Shader * GLGLSLShaderSystem::getShaderByName(const std::string & ShaderName) const
00088  {
00089   assert(mIsStarted && "GLGLSLShaderSystem is not started!!!");
00090   ShaderMap::const_iterator i = mShaderMap.find(ShaderName);
00091   if (i != mShaderMap.end())
00092   {
00093    return i->second;
00094   }
00095   else
00096   {
00097    Except<ERR_ITEM_NOT_FOUND>(this, "Shader with name \"" + ShaderName + "\" cannot be found.", "GLGLSLShaderSystem::getShaderByName()", __FILE__, __LINE__);
00098   }
00099  }
00100 
00101 
00102  bool GLGLSLShaderSystem::isShaderCreated(const std::string & ShaderName) const
00103  {
00104   return (mShaderMap.find(ShaderName) != mShaderMap.end());
00105  }
00106 
00107 
00108  void GLGLSLShaderSystem::destroy(Shader * shader)
00109  {
00110   assert(mIsStarted && "GLGLSLShaderSystem is not started!!!");
00111   std::string ShaderName = shader->getObjectName();
00112   if (!removeFromContainer(ShaderName, mShaderMap))
00113   {
00114    Warning<ERR_INVALIDPARAMS>(this, "Shader object \"" + ShaderName + "\" was created outside GLGLSLShaderSystem!!! Destroying anyway.",
00115             "GLGraphicCore::deleteFrameBuffer", __FILE__, __LINE__);
00116   }
00117   delete shader;
00118   LogManager::getSingleton().logMessage(this, ShaderName + " successfully deleted.", LML_Trivial);
00119  }
00120 
00121 
00122  void GLGLSLShaderSystem::bind(Shader * shader)
00123  {
00124   assert(shader && "Shader does not created!!!");
00125   if (shader != mActiveShader)
00126   {
00127    GLGLSLShader * GLSLShader = static_cast<GLGLSLShader *>(shader);
00128    glUseProgramObjectARB(GLSLShader->mGLSLProgramID);
00129    mActiveShader = shader;
00130   }
00131  }
00132 
00133 
00134  void GLGLSLShaderSystem::unbind(Shader * shader)
00135  {
00136   assert(shader && "Shader does not created!!!");
00137   if (shader == mActiveShader)
00138   {
00139    glUseProgramObjectARB(0);
00140    mActiveShader = 0;
00141   }
00142  }
00143 
00144 
00145  void GLGLSLShaderSystem::start()
00146  {
00147         if (mIsStarted) return;
00148   LogManager::getSingleton().logMessage(this, "---===========================================---");
00149   LogManager::getSingleton().logMessage(this, "---===    GL GLSL Shader system started    ===---");
00150   LogManager::getSingleton().logMessage(this, "---===========================================---");
00151   mIsStarted = true;
00152   this->addAcceptableGraphicCoreID("GLGraphicCore");
00153 
00154   if (debugger::Debugger::getSingletonPtr() != 0)
00155   {
00156    mCodeInstructor = new debugger::GLGLSLCodeInstructor();
00157   }
00158  }
00159 
00160 
00161  void GLGLSLShaderSystem::stop()
00162  {
00163   assert(mIsStarted && "GLGLSLShaderSystem is not started!!!");
00164   clearContainer(mShaderMap);
00165   clearContainer(mUniformMap);
00166 
00167   LogManager::getSingleton().logMessage(this, "---===========================================---");
00168   LogManager::getSingleton().logMessage(this, "---===    GL GLSL Shader system stopped    ===---");
00169   LogManager::getSingleton().logMessage(this, "---===========================================---");
00170   mIsStarted = false;
00171 
00172   if (debugger::Debugger::getSingletonPtr() != 0)
00173   {
00174    delete mCodeInstructor;
00175   }
00176  }
00177 
00178 
00179  Uniform * GLGLSLShaderSystem::create(const Data & data)
00180  {
00181   assert(mIsStarted && "GLGLSLShaderSystem is not started!!!");
00182   UniformMap::iterator i = mUniformMap.find(data.getObjectName());
00183   if (i != mUniformMap.end())
00184   {
00185    Except<ERR_DUPLICATE_ITEM>(this, "Uniform variable with name \"" + data.getObjectName() + "\" already exists!",
00186     "GLGLSLShaderSystem::createUniform()", __FILE__, __LINE__);
00187   }
00188   else
00189   {
00190    GLGLSLUniform * result = new GLGLSLUniform(data.getObjectName());
00191    result->mPointer = data.pointer();
00192    result->mActualLengthGPU = data.getActualLengthCPU() / data.getNumberOfComponents();
00193    result->mNumberOfComponents = data.getNumberOfComponents();
00194    result->mTypeSize = data.getTypeSize();
00195 
00196    if (!data.isArray())
00197    {
00198     switch (data.getValueType())
00199     {
00200      case FloatType:
00201       result->mUniformType = static_cast<Uniform::UniformType>(
00202         Uniform::UT_FLOAT_1 + result->getNumberOfComponents() - 1);
00203       break;
00204 
00205      case IntType:
00206      case UnsignedIntType:
00207       result->mUniformType = static_cast<Uniform::UniformType>(
00208         Uniform::UT_INT_1 + result->getNumberOfComponents() - 1);
00209       break;
00210 
00211      default:
00212       Except<ERR_NOT_IMPLEMENTED>(this, "other ValueTypes is not implemented!",
00213        "GLGLSLShaderSystem::createUniform()", __FILE__, __LINE__);
00214       break;
00215     }
00216    }
00217    else
00218    {
00219     Except<ERR_INVALIDPARAMS>(this, "this method can work only with uniform data.", "GLGLSLShaderSystem::create()", __FILE__, __LINE__);
00220    }
00221 
00222    mUniformMap.insert(std::make_pair(data.getObjectName(), result));
00223    LogManager::getSingleton().logMessage(this, "Uniform variable created from data. Name is: " + data.getObjectName(), LML_Trivial);
00224    return result;
00225   }
00226   return 0;
00227  }
00228 
00229 
00230  Uniform * GLGLSLShaderSystem::create(Texture * texture)
00231  {
00232   assert(mIsStarted && "GLGLSLShaderSystem is not started!!!");
00233   UniformMap::iterator i = mUniformMap.find(texture->getObjectName());
00234   if (i != mUniformMap.end())
00235   {
00236    Except<ERR_DUPLICATE_ITEM>(this, "Uniform variable with name \"" + texture->getObjectName() + "\" already exists!",
00237     "GLGLSLShaderSystem::create()", __FILE__, __LINE__);
00238    return 0;
00239   }
00240   else
00241   {
00242    GLGLSLUniform * result = new GLGLSLUniform(texture->getObjectName());
00243    result->mSampler = texture;
00244    result->mUniformType = Uniform::UT_TEX_2;
00245    mUniformMap.insert(std::make_pair(texture->getObjectName(), result));
00246    LogManager::getSingleton().logMessage(this, "Uniform variable created from texture. Name is: " + texture->getObjectName(), LML_Trivial);
00247    return result;
00248   }
00249  }
00250 
00251 
00252  Uniform * GLGLSLShaderSystem::getUniformByName(const std::string & UniformName) const
00253  {
00254   assert(mIsStarted && "GLGLSLShaderSystem is not started!!!");
00255   UniformMap::const_iterator i = mUniformMap.find(UniformName);
00256   if (i != mUniformMap.end())
00257   {
00258    return i->second;
00259   }
00260   else
00261   {
00262    Except<ERR_ITEM_NOT_FOUND>(this, "Uniform variable with name \"" + UniformName + "\" cannot be found!",
00263     "GLGLSLShaderSystem::getUniformByName()", __FILE__, __LINE__);
00264   }
00265  }
00266 
00267 
00268  bool GLGLSLShaderSystem::isUniformCreated(const std::string & UniformName) const
00269  {
00270   assert(mIsStarted && "GLGLSLShaderSystem is not started!!!");
00271   return (mUniformMap.find(UniformName) != mUniformMap.end());
00272  }
00273 
00274 
00275  void GLGLSLShaderSystem::destroy(Uniform * uniform)
00276  {
00277   assert(mIsStarted && "GLGLSLShaderSystem is not started!!!");
00278   std::string UniformName = uniform->getObjectName();
00279   if (!removeFromContainer(UniformName, mUniformMap))
00280   {
00281    Warning<ERR_INVALIDPARAMS>(this, "Uniform object \"" + UniformName + "\" was created outside GLGLSLShaderSystem!!! Destroying anyway.",
00282     "GLGLSLShaderSystem::destroy", __FILE__, __LINE__);
00283   }
00284   delete uniform;
00285   LogManager::getSingleton().logMessage(this, "Uniform object \"" + UniformName + "\" deleted", LML_Trivial);
00286  }
00287 
00288 
00289  bool GLGLSLShaderSystem::isAcceptCore(const GraphicCore * Core) const
00290  {
00291   return (mAcceptableGraphicCoreIDset.find(Core->getObjectName()) != mAcceptableGraphicCoreIDset.end());
00292  }
00293 
00294 
00295  bool GLGLSLShaderSystem::isAcceptFileExtension(const std::string & FileExtension) const
00296  {
00298   return auxillary::StringUtil::toLower(FileExtension) == std::string("fglsl");
00299  }
00300 
00301 
00302  void GLGLSLShaderSystem::addAcceptableGraphicCoreID(const std::string & GraphicCoreID)
00303  {
00304   mAcceptableGraphicCoreIDset.insert(GraphicCoreID);
00305  }
00306 
00307 
00308  debugger::CodeInstructor * GLGLSLShaderSystem::getCodeInstructor() const
00309  {
00310   return mCodeInstructor;
00311  }
00312 }

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