gl_Cg_shader.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
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
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
00174
00175
00176
00177
00178
00179
00180
00181
00182
00183
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 }