gl_Cg_shader.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_Cg_shader_system/gl_Cg_shader.h"
00034 #include "gl_Cg_shader_system/gl_Cg_uniform.h"
00035 
00036 #include "gpucalc/text_file_loader.h"
00037 #include "gpucalc/log_manager.h"
00038 #include "gpucalc/texture.h"
00039 
00040 
00041 namespace gpucalc
00042 {
00043  GLCgShader::GLCgShader(const std::string & ShaderName, CGcontext CgContext, CGprofile CgProfile, ShaderSystem * system):
00044   Object(_GLCgShaderClassName, ShaderName), Shader(_GLCgShaderClassName, ShaderName, system), mCgContextID(CgContext),
00045   mCgFragmentProfileID(CgProfile),mIsCompiled(false), mIsDebug(false), mShaderSource(), mShaderCompilerParams(), mUniformVector()
00046  {
00047  }
00048 
00049 
00050  GLCgShader::~GLCgShader()
00051  {
00052   cgDestroyProgram(mCgFragmentProgramID);
00053  }
00054 
00055 
00056  void GLCgShader::setSource(const std::string & ShaderSource)
00057  {
00058   mShaderSource = ShaderSource;
00059   mIsCompiled = false;
00060  }
00061 
00062 
00063  void GLCgShader::addUniform(Uniform * uniform)
00064  {
00065   if (addToContainer(uniform, mUniformVector))
00066   {
00067    uniform->uploadToGPU();
00068   }
00069  }
00070  
00071  
00072  void GLCgShader::removeUniform(Uniform * uniform)
00073  {
00074   removeFromContainer(uniform, mUniformVector);
00075  }
00076 
00077 
00078  void GLCgShader::compile(const std::string & Parameters)
00079  {
00080   //remember compiler params for recompilation
00081   mShaderCompilerParams = Parameters;
00082 
00083   if (mIsCompiled)
00084   {
00085    cgDestroyProgram(mCgFragmentProgramID);
00086   }
00087 
00088   LogManager::getSingleton().logMessage(this, "Using following compiler parameters: \"" + Parameters + "\".", LML_Critical);
00089   const char ** Params = new const char * [2];
00090   Params[0] = Parameters.c_str();
00091   Params[1] = 0;
00092   mCgFragmentProgramID = cgCreateProgram(mCgContextID, CG_SOURCE, mShaderSource.c_str(),
00093    mCgFragmentProfileID, "main", Params);
00094 
00095   delete [] Params;
00096 
00097   cgGLLoadProgram(mCgFragmentProgramID);
00098   mIsCompiled = true;
00099   mIsDebug = (Parameters.find("-debug") != std::string::npos);
00100  }
00101 
00102 
00103  void GLCgShader::validate()
00104  {
00106  }
00107 
00108 
00109  void GLCgShader::preCompute()
00110  {
00111   if (!mIsCompiled)
00112   {
00113    compile(mShaderCompilerParams);
00114   }
00115 
00116   bind();
00117   cgGLEnableProfile(mCgFragmentProfileID);
00118   for (UniformVector::iterator i = mUniformVector.begin(); i != mUniformVector.end(); ++i)
00119   {
00120    Uniform * uniform = (*i);
00121    Uniform::UniformType uniform_type = uniform->getUniformType();
00122    if ((uniform_type == Uniform::UT_TEX_1) || (uniform_type == Uniform::UT_TEX_2))
00123    {
00124     setUniformSampler(uniform);
00125    }
00126    else
00127    {
00128     setUniformVariable(uniform);
00129    }
00130   }
00131  }
00132 
00133 
00134  void GLCgShader::setUniformVariable(Uniform * uniform)
00135  {
00136   CGparameter VariableParameter = cgGetNamedParameter(mCgFragmentProgramID, uniform->getObjectName().c_str());
00137   switch (uniform->getUniformType())
00138   {
00139    case Uniform::UT_INT_1:
00140     cgSetParameter1iv(VariableParameter, static_cast<const int *>(uniform->pointer()));
00141     break;
00142 
00143    case Uniform::UT_INT_2:
00144     cgSetParameter2iv(VariableParameter, static_cast<const int *>(uniform->pointer()));
00145     break;
00146 
00147    case Uniform::UT_INT_3:
00148     cgSetParameter3iv(VariableParameter, static_cast<const int *>(uniform->pointer()));
00149     break;
00150 
00151    case Uniform::UT_INT_4:
00152     cgSetParameter4iv(VariableParameter, static_cast<const int *>(uniform->pointer()));
00153     break;
00154 
00155    case Uniform::UT_FLOAT_1:
00156     cgSetParameter1fv(VariableParameter, static_cast<const float *>(uniform->pointer()));
00157     break;
00158 
00159 
00160    case Uniform::UT_FLOAT_2:
00161     cgSetParameter2fv(VariableParameter, static_cast<const float *>(uniform->pointer()));
00162     break;
00163 
00164    case Uniform::UT_FLOAT_3:
00165     cgSetParameter3fv(VariableParameter, static_cast<const float *>(uniform->pointer()));
00166     break;
00167    
00168    case Uniform::UT_FLOAT_4:
00169     cgSetParameter4fv(VariableParameter, static_cast<const float *>(uniform->pointer()));
00170     break;
00171 
00172 
00173    /*case Uniform::UT_MAT_2X2:
00174     cgSetParameter1iv(VariableParameter, false, static_cast<const float *>(uniform->pointer()));
00175     break;
00176 
00177    case Uniform::UT_MAT_3X3:
00178     cgSetParameter1iv(VariableParameter, false, static_cast<const float *>(uniform->pointer()));
00179     break;
00180 
00181    case Uniform::UT_MAT_4X4:
00182     cgSetParameter1iv(VariableParameter, false, static_cast<const float *>(uniform->pointer()));
00183     break;
00184 */
00185    default:
00186     Except<ERR_NOT_IMPLEMENTED>(this, "Other uniform types not implemented.", "GLCgShader::setUniformVariable", __FILE__, __LINE__);
00187     break;
00188   }
00189  }
00190 
00191 
00192  void GLCgShader::setUniformSampler(Uniform * uniform)
00193  {
00194   CGparameter SamplerParameter = cgGetNamedParameter(mCgFragmentProgramID, uniform->getObjectName().c_str());
00195 
00196   GLuint GLTextureID = 0;
00197   uniform->getSampler()->getSpecificParameter("GLTextureID", &GLTextureID);
00198   cgGLSetTextureParameter(SamplerParameter, GLTextureID);
00199   cgGLEnableTextureParameter(SamplerParameter);
00200  }
00201 }

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