Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members   Related Pages  

OgreGpuProgram.cpp

Go to the documentation of this file.
00001 /*
00002 -----------------------------------------------------------------------------
00003 This source file is part of OGRE
00004     (Object-oriented Graphics Rendering Engine)
00005 For the latest info, see http://ogre.sourceforge.net/
00006 
00007 Copyright © 2000-2003 The OGRE Team
00008 Also see acknowledgements in Readme.html
00009 
00010 This program is free software you can redistribute it and/or modify it under
00011 the terms of the GNU Lesser General Public License as published by the Free Software
00012 Foundation either version 2 of the License, or (at your option) any later
00013 version.
00014 
00015 This program is distributed in the hope that it will be useful, but WITHOUT
00016 ANY WARRANTY without even the implied warranty of MERCHANTABILITY or FITNESS
00017 FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
00018 
00019 You should have received a copy of the GNU Lesser General Public License along with
00020 this program if not, write to the Free Software Foundation, Inc., 59 Temple
00021 Place - Suite 330, Boston, MA 02111-1307, USA, or go to
00022 http://www.gnu.org/copyleft/lesser.txt.
00023 -----------------------------------------------------------------------------
00024 */
00025 #include "OgreStableHeaders.h"
00026 #include "OgreGpuProgram.h"
00027 #include "OgreGpuProgramManager.h"
00028 #include "OgreSDDataChunk.h"
00029 #include "OgreVector3.h"
00030 #include "OgreVector4.h"
00031 #include "OgreAutoParamDataSource.h"
00032 #include "OgreLight.h"
00033 #include "OgreControllerManager.h"
00034 #include "OgreRoot.h"
00035 #include "OgreRenderSystem.h"
00036 #include "OgreRenderSystemCapabilities.h"
00037 
00038 namespace Ogre
00039 {
00040     //-----------------------------------------------------------------------------
00041     GpuProgram::GpuProgram(const String& name, GpuProgramType gptype, const String& syntaxCode) 
00042         : mType(gptype), mLoadFromFile(true), mSyntaxCode(syntaxCode), mSkeletalAnimation(false)
00043     {
00044         mName = name;
00045     }
00046     //-----------------------------------------------------------------------------
00047     void GpuProgram::setSourceFile(const String& filename)
00048     {
00049         mFilename = filename;
00050         mSource = "";
00051         mLoadFromFile = true;
00052     }
00053     //-----------------------------------------------------------------------------
00054     void GpuProgram::setSource(const String& source)
00055     {
00056         mSource = source;
00057         mFilename = "";
00058         mLoadFromFile = false;
00059     }
00060 
00061     //-----------------------------------------------------------------------------
00062     void GpuProgram::load(void)
00063     {
00064         if (mIsLoaded)
00065         {
00066             unload();
00067         }
00068         if (mLoadFromFile)
00069         {
00070             // find & load source code
00071             SDDataChunk chunk;
00072             GpuProgramManager::getSingleton()._findResourceData(mFilename, chunk);
00073             mSource = chunk.getAsString();
00074         }
00075 
00076         // Call polymorphic load
00077         loadFromSource();
00078 
00079         mIsLoaded = true;
00080     }
00081     //-----------------------------------------------------------------------------
00082     bool GpuProgram::isSupported(void) const
00083     {
00084         // If skeletal animation is being done, we need support for UBYTE4
00085         if (isSkeletalAnimationIncluded() && 
00086             !Root::getSingleton().getRenderSystem()->getCapabilities()
00087                 ->hasCapability(RSC_VERTEX_FORMAT_UBYTE4))
00088         {
00089             return false;
00090         }
00091         return GpuProgramManager::getSingleton().isSyntaxSupported(mSyntaxCode);
00092     }
00093     //-----------------------------------------------------------------------------
00094     GpuProgramParametersSharedPtr GpuProgram::createParameters(void)
00095     {
00096         // Default implementation simply returns standard parameters.
00097         return GpuProgramManager::getSingleton().createParameters();
00098     }
00099     //-----------------------------------------------------------------------------
00100     GpuProgramParameters::GpuProgramParameters()
00101         : mTransposeMatrices(false)
00102     {
00103     }
00104     //-----------------------------------------------------------------------------
00105     void GpuProgramParameters::setConstant(size_t index, const Vector4& vec)
00106     {
00107         setConstant(index, vec.val, 1);
00108     }
00109     //-----------------------------------------------------------------------------
00110     void GpuProgramParameters::setConstant(size_t index, const Vector3& vec)
00111     {
00112         setConstant(index, Vector4(vec.x, vec.y, vec.z, 1.0f));
00113     }
00114     //-----------------------------------------------------------------------------
00115     void GpuProgramParameters::setConstant(size_t index, const Matrix4& m)
00116     {
00117         // set as 4x 4-element floats
00118         if (mTransposeMatrices)
00119         {
00120             Matrix4 t = m.transpose();
00121             GpuProgramParameters::setConstant(index++, t[0], 1);
00122             GpuProgramParameters::setConstant(index++, t[1], 1);
00123             GpuProgramParameters::setConstant(index++, t[2], 1);
00124             GpuProgramParameters::setConstant(index, t[3], 1);
00125         }
00126         else
00127         {
00128             GpuProgramParameters::setConstant(index++, m[0], 1);
00129             GpuProgramParameters::setConstant(index++, m[1], 1);
00130             GpuProgramParameters::setConstant(index++, m[2], 1);
00131             GpuProgramParameters::setConstant(index, m[3], 1);
00132         }
00133     }
00134     //-----------------------------------------------------------------------------
00135     void GpuProgramParameters::setConstant(size_t index, const Matrix4* pMatrix, 
00136         size_t numEntries)
00137     {
00138         for (size_t i = 0; i < numEntries; ++i)
00139         {
00140             const Matrix4& m = pMatrix[i];
00141 
00142             if (mTransposeMatrices)
00143             {
00144                 Matrix4 t = m.transpose();
00145                 GpuProgramParameters::setConstant(index++, t[0], 1);
00146                 GpuProgramParameters::setConstant(index++, t[1], 1);
00147                 GpuProgramParameters::setConstant(index++, t[2], 1);
00148                 GpuProgramParameters::setConstant(index++, t[3], 1);
00149             }
00150             else
00151             {
00152                 GpuProgramParameters::setConstant(index++, m[0], 1);
00153                 GpuProgramParameters::setConstant(index++, m[1], 1);
00154                 GpuProgramParameters::setConstant(index++, m[2], 1);
00155                 GpuProgramParameters::setConstant(index++, m[3], 1);
00156             }
00157         }
00158     }
00159     //-----------------------------------------------------------------------------
00160     void GpuProgramParameters::setConstant(size_t index, const ColourValue& colour)
00161     {
00162         setConstant(index, colour.val, 1);
00163     }
00164     //-----------------------------------------------------------------------------
00165     void GpuProgramParameters::setConstant(size_t index, const Real *val, size_t count)
00166     {
00167         // Expand if required
00168         if (mRealConstants.size() < index + count)
00169             mRealConstants.resize(index + count);
00170 
00171         // Copy in chunks of 4
00172         while (count--)
00173         {
00174             RealConstantEntry* e = &(mRealConstants[index++]);
00175             e->isSet = true;
00176             memcpy(e->val, val, sizeof(Real) * 4);
00177             val += 4;
00178         }
00179 
00180     }
00181     //-----------------------------------------------------------------------------
00182     void GpuProgramParameters::setConstant(size_t index, const int *val, size_t count)
00183     {
00184         // Expand if required
00185         if (mIntConstants.size() < index + count)
00186             mIntConstants.resize(index + count);
00187 
00188         // Copy in chunks of 4
00189         while (count--)
00190         {
00191             IntConstantEntry* e = &(mIntConstants[index++]);
00192             e->isSet = true;
00193             memcpy(e->val, val, sizeof(int) * 4);
00194             val += 4;
00195         }
00196     }
00197     //-----------------------------------------------------------------------------
00198     void GpuProgramParameters::setAutoConstant(size_t index, AutoConstantType acType, size_t extraInfo)
00199     {
00200         mAutoConstants.push_back(AutoConstantEntry(acType, index, extraInfo));
00201     }
00202     //-----------------------------------------------------------------------------
00203     void GpuProgramParameters::clearAutoConstants(void)
00204     {
00205         mAutoConstants.clear();
00206     }
00207     //-----------------------------------------------------------------------------
00208     GpuProgramParameters::AutoConstantIterator GpuProgramParameters::getAutoConstantIterator(void)
00209     {
00210         return AutoConstantIterator(mAutoConstants.begin(), mAutoConstants.end());
00211     }
00212     //-----------------------------------------------------------------------------
00213     void GpuProgramParameters::_updateAutoParamsNoLights(const AutoParamDataSource& source)
00214     {
00215         if (!hasAutoConstants()) return; // abort early if no autos
00216         Vector3 vec3;
00217         Vector4 vec4;
00218         size_t index;
00219         size_t numMatrices;
00220         const Matrix4* pMatrix;
00221         size_t m;
00222 
00223         AutoConstantList::const_iterator i, iend;
00224         iend = mAutoConstants.end();
00225         for (i = mAutoConstants.begin(); i != iend; ++i)
00226         {
00227             switch(i->paramType)
00228             {
00229             case ACT_WORLD_MATRIX:
00230                 setConstant(i->index, source.getWorldMatrix());
00231                 break;
00232             case ACT_WORLD_MATRIX_ARRAY:
00233                 setConstant(i->index, source.getWorldMatrixArray(), 
00234                     source.getWorldMatrixCount());
00235                 break;
00236             case ACT_WORLD_MATRIX_ARRAY_3x4:
00237                 // Loop over matrices
00238                 pMatrix = source.getWorldMatrixArray();
00239                 numMatrices = source.getWorldMatrixCount();
00240                 index = i->index;
00241                 for (m = 0; m < numMatrices; ++m)
00242                 {
00243                     GpuProgramParameters::setConstant(index++, (*pMatrix)[0], 1);
00244                     GpuProgramParameters::setConstant(index++, (*pMatrix)[1], 1);
00245                     GpuProgramParameters::setConstant(index++, (*pMatrix)[2], 1);
00246                     ++pMatrix;
00247                 }
00248                 
00249                 break;
00250             case ACT_VIEW_MATRIX:
00251                 setConstant(i->index, source.getViewMatrix());
00252                 break;
00253             case ACT_PROJECTION_MATRIX:
00254                 setConstant(i->index, source.getProjectionMatrix());
00255                 break;
00256             case ACT_WORLDVIEW_MATRIX:
00257                 setConstant(i->index, source.getWorldViewMatrix());
00258                 break;
00259             case ACT_VIEWPROJ_MATRIX:
00260                 setConstant(i->index, source.getViewProjectionMatrix());
00261                 break;
00262             case ACT_WORLDVIEWPROJ_MATRIX:
00263                 setConstant(i->index, source.getWorldViewProjMatrix());
00264                 break;
00265             case ACT_INVERSE_WORLD_MATRIX:
00266                 setConstant(i->index, source.getInverseWorldMatrix());
00267                 break;
00268             case ACT_INVERSE_WORLDVIEW_MATRIX:
00269                 setConstant(i->index, source.getInverseWorldViewMatrix());
00270                 break;
00271             case ACT_CAMERA_POSITION_OBJECT_SPACE:
00272                 setConstant(i->index, source.getCameraPositionObjectSpace());
00273                 break;
00274             // NB ambient light still here because it's not related to a specific light
00275             case ACT_AMBIENT_LIGHT_COLOUR: 
00276                 setConstant(i->index, source.getAmbientLightColour());
00277                 break;
00278             case ACT_TEXTURE_VIEWPROJ_MATRIX:
00279                 setConstant(i->index, source.getTextureViewProjMatrix());
00280                 break;
00281             default:
00282                 break;
00283             }
00284         }
00285     }
00286     //-----------------------------------------------------------------------------
00287     void GpuProgramParameters::_updateAutoParamsLightsOnly(const AutoParamDataSource& source)
00288     {
00289         if (!hasAutoConstants()) return; // abort early if no autos
00290         Vector3 vec3;
00291         Vector4 vec4;
00292 
00293         AutoConstantList::const_iterator i, iend;
00294         iend = mAutoConstants.end();
00295         for (i = mAutoConstants.begin(); i != iend; ++i)
00296         {
00297             switch(i->paramType)
00298             {
00299             case ACT_LIGHT_DIFFUSE_COLOUR:
00300                 setConstant(i->index, source.getLight(i->data).getDiffuseColour());
00301                 break;
00302             case ACT_LIGHT_SPECULAR_COLOUR:
00303                 setConstant(i->index, source.getLight(i->data).getSpecularColour());
00304                 break;
00305             case ACT_LIGHT_POSITION:
00306                 // Get as 4D vector, works for directional lights too
00307                 setConstant(i->index, 
00308                     source.getLight(i->data).getAs4DVector());
00309                 break;
00310             case ACT_LIGHT_DIRECTION:
00311                 vec3 = source.getLight(i->data).getDerivedDirection();
00312                 // Set as 4D vector for compatibility
00313                 setConstant(i->index, Vector4(vec3.x, vec3.y, vec3.z, 1.0f));
00314                 break;
00315             case ACT_LIGHT_POSITION_OBJECT_SPACE:
00316                 setConstant(i->index, 
00317                     source.getInverseWorldMatrix() * source.getLight(i->data).getAs4DVector());
00318                 break;
00319             case ACT_LIGHT_DIRECTION_OBJECT_SPACE:
00320                 vec3 = source.getInverseWorldMatrix() * 
00321                     source.getLight(i->data).getDerivedDirection();
00322                 vec3.normalise();
00323                 // Set as 4D vector for compatibility
00324                 setConstant(i->index, Vector4(vec3.x, vec3.y, vec3.z, 1.0f));
00325                 break;
00326             case ACT_LIGHT_DISTANCE_OBJECT_SPACE:
00327                 vec3 = source.getInverseWorldMatrix() * source.getLight(i->data).getDerivedPosition();
00328                 setConstant(i->index, vec3.length());
00329                 break;
00330             case ACT_SHADOW_EXTRUSION_DISTANCE:
00331                 setConstant(i->index, source.getShadowExtrusionDistance());
00332                 break;
00333             case ACT_LIGHT_ATTENUATION:
00334                 // range, const, linear, quad
00335                 const Light& l = source.getLight(i->data);
00336                 vec4.x = l.getAttenuationRange();
00337                 vec4.y = l.getAttenuationConstant();
00338                 vec4.z = l.getAttenuationLinear();
00339                 vec4.w = l.getAttenuationQuadric();
00340                 setConstant(i->index, vec4);
00341                 break;
00342             }
00343         }
00344     }
00345     //---------------------------------------------------------------------------
00346     void GpuProgramParameters::_mapParameterNameToIndex(const String& name, 
00347         size_t index)
00348     {
00349         mParamNameMap[name] = index;
00350     }
00351     //---------------------------------------------------------------------------
00352     size_t GpuProgramParameters::getParamIndex(const String& name) const
00353     {
00354         ParamNameMap::const_iterator i = mParamNameMap.find(name);
00355         if (i == mParamNameMap.end())
00356         {
00357             Except(Exception::ERR_ITEM_NOT_FOUND, "Cannot find a parameter named " + name,
00358                 "GpuProgramParameters::getParamIndex");
00359         }
00360         return i->second;
00361     }
00362     //---------------------------------------------------------------------------
00363     void GpuProgramParameters::setNamedConstant(const String& name, Real val)
00364     {
00365         setConstant(getParamIndex(name), val);
00366     }
00367     //---------------------------------------------------------------------------
00368     void GpuProgramParameters::setNamedConstant(const String& name, int val)
00369     {
00370         setConstant(getParamIndex(name), val);
00371     }
00372     //---------------------------------------------------------------------------
00373     void GpuProgramParameters::setNamedConstant(const String& name, const Vector4& vec)
00374     {
00375         setConstant(getParamIndex(name), vec);
00376     }
00377     //---------------------------------------------------------------------------
00378     void GpuProgramParameters::setNamedConstant(const String& name, const Vector3& vec)
00379     {
00380         setConstant(getParamIndex(name), vec);
00381     }
00382     //---------------------------------------------------------------------------
00383     void GpuProgramParameters::setNamedConstant(const String& name, const Matrix4& m)
00384     {
00385         setConstant(getParamIndex(name), m);
00386     }
00387     //---------------------------------------------------------------------------
00388     void GpuProgramParameters::setNamedConstant(const String& name, const Matrix4* m, 
00389         size_t numEntries)
00390     {
00391         setConstant(getParamIndex(name), m, numEntries);
00392     }
00393     //---------------------------------------------------------------------------
00394     void GpuProgramParameters::setNamedConstant(const String& name, const Real *val, size_t count)
00395     {
00396         setConstant(getParamIndex(name), val, count);
00397     }
00398     //---------------------------------------------------------------------------
00399     void GpuProgramParameters::setNamedConstant(const String& name, const ColourValue& colour)
00400     {
00401         setConstant(getParamIndex(name), colour);
00402     }
00403     //---------------------------------------------------------------------------
00404     void GpuProgramParameters::setNamedConstant(const String& name, const int *val, size_t count)
00405     {
00406         setConstant(getParamIndex(name), val, count);
00407     }
00408     //---------------------------------------------------------------------------
00409     void GpuProgramParameters::setNamedAutoConstant(const String& name, AutoConstantType acType, size_t extraInfo)
00410     {
00411         setAutoConstant(getParamIndex(name), acType, extraInfo);
00412     }
00413     //---------------------------------------------------------------------------
00414     void GpuProgramParameters::setConstantFromTime(size_t index, Real factor)
00415     {
00416         // Create controller
00417         ControllerManager::getSingleton().createGpuProgramTimerParam(this, index, factor);
00418 
00419     }
00420     //---------------------------------------------------------------------------
00421     void GpuProgramParameters::setNamedConstantFromTime(const String& name, Real factor)
00422     {
00423         setConstantFromTime(getParamIndex(name), factor);
00424     }
00425     //---------------------------------------------------------------------------
00426     GpuProgramParameters::RealConstantIterator GpuProgramParameters::getRealConstantIterator(void)
00427     {
00428         return RealConstantIterator(mRealConstants.begin(), mRealConstants.end());
00429     }
00430     //---------------------------------------------------------------------------
00431     GpuProgramParameters::IntConstantIterator GpuProgramParameters::getIntConstantIterator(void)
00432     {
00433         return IntConstantIterator(mIntConstants.begin(), mIntConstants.end());
00434     }
00435 
00436 }

Copyright © 2002-2003 by The OGRE Team
Last modified Fri May 14 23:22:14 2004