[657] | 1 | /*
|
---|
| 2 | -----------------------------------------------------------------------------
|
---|
| 3 | This source file is part of OGRE
|
---|
| 4 | (Object-oriented Graphics Rendering Engine)
|
---|
| 5 | For the latest info, see http://ogre.sourceforge.net/
|
---|
| 6 |
|
---|
| 7 | Copyright (c) 2000-2005 The OGRE Team
|
---|
| 8 | Also see acknowledgements in Readme.html
|
---|
| 9 |
|
---|
| 10 | This program is free software you can redistribute it and/or modify it under
|
---|
| 11 | the terms of the GNU Lesser General Public License as published by the Free Software
|
---|
| 12 | Foundation either version 2 of the License, or (at your option) any later
|
---|
| 13 | version.
|
---|
| 14 |
|
---|
| 15 | This program is distributed in the hope that it will be useful, but WITHOUT
|
---|
| 16 | ANY WARRANTY without even the implied warranty of MERCHANTABILITY or FITNESS
|
---|
| 17 | FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
|
---|
| 18 |
|
---|
| 19 | You should have received a copy of the GNU Lesser General Public License along with
|
---|
| 20 | this program if not, write to the Free Software Foundation, Inc., 59 Temple
|
---|
| 21 | Place - Suite 330, Boston, MA 02111-1307, USA, or go to
|
---|
| 22 | http://www.gnu.org/copyleft/lesser.txt.
|
---|
| 23 | -----------------------------------------------------------------------------
|
---|
| 24 | */
|
---|
| 25 | #include "OgreStableHeaders.h"
|
---|
| 26 | #include "OgreGpuProgram.h"
|
---|
| 27 | #include "OgreHighLevelGpuProgram.h"
|
---|
| 28 | #include "OgreGpuProgramManager.h"
|
---|
| 29 | #include "OgreVector3.h"
|
---|
| 30 | #include "OgreVector4.h"
|
---|
| 31 | #include "OgreAutoParamDataSource.h"
|
---|
| 32 | #include "OgreLight.h"
|
---|
| 33 | #include "OgreRoot.h"
|
---|
| 34 | #include "OgreRenderSystem.h"
|
---|
| 35 | #include "OgreRenderSystemCapabilities.h"
|
---|
| 36 | #include "OgreStringConverter.h"
|
---|
| 37 |
|
---|
| 38 | namespace Ogre
|
---|
| 39 | {
|
---|
| 40 | //-----------------------------------------------------------------------------
|
---|
| 41 | GpuProgram::CmdType GpuProgram::msTypeCmd;
|
---|
| 42 | GpuProgram::CmdSyntax GpuProgram::msSyntaxCmd;
|
---|
| 43 | GpuProgram::CmdSkeletal GpuProgram::msSkeletalCmd;
|
---|
| 44 |
|
---|
| 45 | //-----------------------------------------------------------------------------
|
---|
| 46 | GpuProgram::GpuProgram(ResourceManager* creator, const String& name, ResourceHandle handle,
|
---|
| 47 | const String& group, bool isManual, ManualResourceLoader* loader)
|
---|
| 48 | :Resource(creator, name, handle, group, isManual, loader),
|
---|
| 49 | mType(GPT_VERTEX_PROGRAM), mLoadFromFile(true), mSkeletalAnimation(false),
|
---|
| 50 | mPassSurfaceAndLightStates(false)
|
---|
| 51 | {
|
---|
| 52 | }
|
---|
| 53 | //-----------------------------------------------------------------------------
|
---|
| 54 | void GpuProgram::setType(GpuProgramType t)
|
---|
| 55 | {
|
---|
| 56 | mType = t;
|
---|
| 57 | }
|
---|
| 58 | //-----------------------------------------------------------------------------
|
---|
| 59 | void GpuProgram::setSyntaxCode(const String& syntax)
|
---|
| 60 | {
|
---|
| 61 | mSyntaxCode = syntax;
|
---|
| 62 | }
|
---|
| 63 | //-----------------------------------------------------------------------------
|
---|
| 64 | void GpuProgram::setSourceFile(const String& filename)
|
---|
| 65 | {
|
---|
| 66 | mFilename = filename;
|
---|
| 67 | mSource = "";
|
---|
| 68 | mLoadFromFile = true;
|
---|
| 69 | }
|
---|
| 70 | //-----------------------------------------------------------------------------
|
---|
| 71 | void GpuProgram::setSource(const String& source)
|
---|
| 72 | {
|
---|
| 73 | mSource = source;
|
---|
| 74 | mFilename = "";
|
---|
| 75 | mLoadFromFile = false;
|
---|
| 76 | }
|
---|
| 77 |
|
---|
| 78 | //-----------------------------------------------------------------------------
|
---|
| 79 | void GpuProgram::loadImpl(void)
|
---|
| 80 | {
|
---|
| 81 | if (mLoadFromFile)
|
---|
| 82 | {
|
---|
| 83 | // find & load source code
|
---|
| 84 | DataStreamPtr stream =
|
---|
| 85 | ResourceGroupManager::getSingleton().openResource(mFilename, mGroup);
|
---|
| 86 | mSource = stream->getAsString();
|
---|
| 87 | }
|
---|
| 88 |
|
---|
| 89 | // Call polymorphic load
|
---|
| 90 | loadFromSource();
|
---|
| 91 |
|
---|
| 92 | }
|
---|
| 93 | //-----------------------------------------------------------------------------
|
---|
| 94 | bool GpuProgram::isSupported(void) const
|
---|
| 95 | {
|
---|
| 96 | // If skeletal animation is being done, we need support for UBYTE4
|
---|
| 97 | if (isSkeletalAnimationIncluded() &&
|
---|
| 98 | !Root::getSingleton().getRenderSystem()->getCapabilities()
|
---|
| 99 | ->hasCapability(RSC_VERTEX_FORMAT_UBYTE4))
|
---|
| 100 | {
|
---|
| 101 | return false;
|
---|
| 102 | }
|
---|
| 103 | return GpuProgramManager::getSingleton().isSyntaxSupported(mSyntaxCode);
|
---|
| 104 | }
|
---|
| 105 | //-----------------------------------------------------------------------------
|
---|
| 106 | GpuProgramParametersSharedPtr GpuProgram::createParameters(void)
|
---|
| 107 | {
|
---|
| 108 | // Default implementation simply returns standard parameters.
|
---|
| 109 | GpuProgramParametersSharedPtr ret =
|
---|
| 110 | GpuProgramManager::getSingleton().createParameters();
|
---|
| 111 | // Copy in default parameters if present
|
---|
| 112 | if (!mDefaultParams.isNull())
|
---|
| 113 | ret->copyConstantsFrom(*(mDefaultParams.get()));
|
---|
| 114 |
|
---|
| 115 | return ret;
|
---|
| 116 | }
|
---|
| 117 | //-----------------------------------------------------------------------------
|
---|
| 118 | GpuProgramParametersSharedPtr GpuProgram::getDefaultParameters(void)
|
---|
| 119 | {
|
---|
| 120 | if (mDefaultParams.isNull())
|
---|
| 121 | {
|
---|
| 122 | mDefaultParams = createParameters();
|
---|
| 123 | }
|
---|
| 124 | return mDefaultParams;
|
---|
| 125 | }
|
---|
| 126 | //-----------------------------------------------------------------------------
|
---|
| 127 | void GpuProgram::setupBaseParamDictionary(void)
|
---|
| 128 | {
|
---|
| 129 | ParamDictionary* dict = getParamDictionary();
|
---|
| 130 |
|
---|
| 131 | dict->addParameter(
|
---|
| 132 | ParameterDef("type", "'vertex_program' or 'fragment_program'",
|
---|
| 133 | PT_STRING), &msTypeCmd);
|
---|
| 134 | dict->addParameter(
|
---|
| 135 | ParameterDef("syntax", "Syntax code, e.g. vs_1_1", PT_STRING), &msSyntaxCmd);
|
---|
| 136 | dict->addParameter(
|
---|
| 137 | ParameterDef("includes_skeletal_animation",
|
---|
| 138 | "Whether this vertex program includes skeletal animation", PT_BOOL),
|
---|
| 139 | &msSkeletalCmd);
|
---|
| 140 | }
|
---|
| 141 |
|
---|
| 142 | //-----------------------------------------------------------------------------
|
---|
| 143 | //-----------------------------------------------------------------------------
|
---|
| 144 | //-----------------------------------------------------------------------------
|
---|
| 145 | GpuProgramParameters::GpuProgramParameters()
|
---|
| 146 | : mTransposeMatrices(false), mAutoAddParamName(false)
|
---|
| 147 | {
|
---|
| 148 | }
|
---|
| 149 | //-----------------------------------------------------------------------------
|
---|
| 150 | void GpuProgramParameters::setConstant(size_t index, const Vector4& vec)
|
---|
| 151 | {
|
---|
| 152 | setConstant(index, vec.val, 1);
|
---|
| 153 | }
|
---|
| 154 | //-----------------------------------------------------------------------------
|
---|
| 155 | void GpuProgramParameters::setConstant(size_t index, Real val)
|
---|
| 156 | {
|
---|
| 157 | setConstant(index, Vector4(val, 0.0f, 0.0f, 0.0f));
|
---|
| 158 | }
|
---|
| 159 | //-----------------------------------------------------------------------------
|
---|
| 160 | void GpuProgramParameters::setConstant(size_t index, const Vector3& vec)
|
---|
| 161 | {
|
---|
| 162 | setConstant(index, Vector4(vec.x, vec.y, vec.z, 1.0f));
|
---|
| 163 | }
|
---|
| 164 | //-----------------------------------------------------------------------------
|
---|
| 165 | void GpuProgramParameters::setConstant(size_t index, const Matrix4& m)
|
---|
| 166 | {
|
---|
| 167 | // set as 4x 4-element floats
|
---|
| 168 | if (mTransposeMatrices)
|
---|
| 169 | {
|
---|
| 170 | Matrix4 t = m.transpose();
|
---|
| 171 | GpuProgramParameters::setConstant(index++, t[0], 1);
|
---|
| 172 | GpuProgramParameters::setConstant(index++, t[1], 1);
|
---|
| 173 | GpuProgramParameters::setConstant(index++, t[2], 1);
|
---|
| 174 | GpuProgramParameters::setConstant(index, t[3], 1);
|
---|
| 175 | }
|
---|
| 176 | else
|
---|
| 177 | {
|
---|
| 178 | GpuProgramParameters::setConstant(index++, m[0], 1);
|
---|
| 179 | GpuProgramParameters::setConstant(index++, m[1], 1);
|
---|
| 180 | GpuProgramParameters::setConstant(index++, m[2], 1);
|
---|
| 181 | GpuProgramParameters::setConstant(index, m[3], 1);
|
---|
| 182 | }
|
---|
| 183 | }
|
---|
| 184 | //-----------------------------------------------------------------------------
|
---|
| 185 | void GpuProgramParameters::setConstant(size_t index, const Matrix4* pMatrix,
|
---|
| 186 | size_t numEntries)
|
---|
| 187 | {
|
---|
| 188 | for (size_t i = 0; i < numEntries; ++i)
|
---|
| 189 | {
|
---|
| 190 | const Matrix4& m = pMatrix[i];
|
---|
| 191 |
|
---|
| 192 | if (mTransposeMatrices)
|
---|
| 193 | {
|
---|
| 194 | Matrix4 t = m.transpose();
|
---|
| 195 | GpuProgramParameters::setConstant(index++, t[0], 1);
|
---|
| 196 | GpuProgramParameters::setConstant(index++, t[1], 1);
|
---|
| 197 | GpuProgramParameters::setConstant(index++, t[2], 1);
|
---|
| 198 | GpuProgramParameters::setConstant(index++, t[3], 1);
|
---|
| 199 | }
|
---|
| 200 | else
|
---|
| 201 | {
|
---|
| 202 | GpuProgramParameters::setConstant(index++, m[0], 1);
|
---|
| 203 | GpuProgramParameters::setConstant(index++, m[1], 1);
|
---|
| 204 | GpuProgramParameters::setConstant(index++, m[2], 1);
|
---|
| 205 | GpuProgramParameters::setConstant(index++, m[3], 1);
|
---|
| 206 | }
|
---|
| 207 | }
|
---|
| 208 | }
|
---|
| 209 | //-----------------------------------------------------------------------------
|
---|
| 210 | void GpuProgramParameters::setConstant(size_t index, const ColourValue& colour)
|
---|
| 211 | {
|
---|
| 212 | setConstant(index, colour.val, 1);
|
---|
| 213 | }
|
---|
| 214 | //-----------------------------------------------------------------------------
|
---|
| 215 | void GpuProgramParameters::setConstant(size_t index, const float *val, size_t count)
|
---|
| 216 | {
|
---|
| 217 | // Expand if required
|
---|
| 218 | if (mRealConstants.size() < index + count)
|
---|
| 219 | mRealConstants.resize(index + count);
|
---|
| 220 |
|
---|
| 221 | // Copy in chunks of 4
|
---|
| 222 | while (count--)
|
---|
| 223 | {
|
---|
| 224 | RealConstantEntry* e = &(mRealConstants[index++]);
|
---|
| 225 | e->isSet = true;
|
---|
| 226 | memcpy(e->val, val, sizeof(float) * 4);
|
---|
| 227 | val += 4;
|
---|
| 228 | }
|
---|
| 229 |
|
---|
| 230 | }
|
---|
| 231 | //-----------------------------------------------------------------------------
|
---|
| 232 | void GpuProgramParameters::setConstant(size_t index, const double *val, size_t count)
|
---|
| 233 | {
|
---|
| 234 | // Expand if required
|
---|
| 235 | if (mRealConstants.size() < index + count)
|
---|
| 236 | mRealConstants.resize(index + count);
|
---|
| 237 |
|
---|
| 238 | // Copy, casting to float
|
---|
| 239 | while (count--)
|
---|
| 240 | {
|
---|
| 241 | RealConstantEntry* e = &(mRealConstants[index++]);
|
---|
| 242 | e->isSet = true;
|
---|
| 243 | e->val[0] = static_cast<float>(val[0]);
|
---|
| 244 | e->val[1] = static_cast<float>(val[1]);
|
---|
| 245 | e->val[2] = static_cast<float>(val[2]);
|
---|
| 246 | e->val[3] = static_cast<float>(val[3]);
|
---|
| 247 | val += 4;
|
---|
| 248 | }
|
---|
| 249 |
|
---|
| 250 | }
|
---|
| 251 | //-----------------------------------------------------------------------------
|
---|
| 252 | void GpuProgramParameters::setConstant(size_t index, const int *val, size_t count)
|
---|
| 253 | {
|
---|
| 254 | // Expand if required
|
---|
| 255 | if (mIntConstants.size() < index + count)
|
---|
| 256 | mIntConstants.resize(index + count);
|
---|
| 257 |
|
---|
| 258 | // Copy in chunks of 4
|
---|
| 259 | while (count--)
|
---|
| 260 | {
|
---|
| 261 | IntConstantEntry* e = &(mIntConstants[index++]);
|
---|
| 262 | e->isSet = true;
|
---|
| 263 | memcpy(e->val, val, sizeof(int) * 4);
|
---|
| 264 | val += 4;
|
---|
| 265 | }
|
---|
| 266 | }
|
---|
| 267 | //-----------------------------------------------------------------------------
|
---|
| 268 | void GpuProgramParameters::setAutoConstant(size_t index, AutoConstantType acType, size_t extraInfo)
|
---|
| 269 | {
|
---|
| 270 | mAutoConstants.push_back(AutoConstantEntry(acType, index, extraInfo));
|
---|
| 271 | }
|
---|
| 272 | //-----------------------------------------------------------------------------
|
---|
| 273 | void GpuProgramParameters::clearAutoConstants(void)
|
---|
| 274 | {
|
---|
| 275 | mAutoConstants.clear();
|
---|
| 276 | }
|
---|
| 277 | //-----------------------------------------------------------------------------
|
---|
| 278 | GpuProgramParameters::AutoConstantIterator GpuProgramParameters::getAutoConstantIterator(void) const
|
---|
| 279 | {
|
---|
| 280 | return AutoConstantIterator(mAutoConstants.begin(), mAutoConstants.end());
|
---|
| 281 | }
|
---|
| 282 | //-----------------------------------------------------------------------------
|
---|
| 283 | void GpuProgramParameters::setAutoConstantReal(size_t index, AutoConstantType acType, Real rData)
|
---|
| 284 | {
|
---|
| 285 | mAutoConstants.push_back(AutoConstantEntry(acType, index, rData));
|
---|
| 286 | }
|
---|
| 287 | //-----------------------------------------------------------------------------
|
---|
| 288 |
|
---|
| 289 | //-----------------------------------------------------------------------------
|
---|
| 290 | void GpuProgramParameters::_updateAutoParamsNoLights(const AutoParamDataSource& source)
|
---|
| 291 | {
|
---|
| 292 | if (!hasAutoConstants()) return; // abort early if no autos
|
---|
| 293 | Vector3 vec3;
|
---|
| 294 | Vector4 vec4;
|
---|
| 295 | size_t index;
|
---|
| 296 | size_t numMatrices;
|
---|
| 297 | const Matrix4* pMatrix;
|
---|
| 298 | size_t m;
|
---|
| 299 |
|
---|
| 300 | AutoConstantList::const_iterator i, iend;
|
---|
| 301 | iend = mAutoConstants.end();
|
---|
| 302 | for (i = mAutoConstants.begin(); i != iend; ++i)
|
---|
| 303 | {
|
---|
| 304 | switch(i->paramType)
|
---|
| 305 | {
|
---|
| 306 | case ACT_WORLD_MATRIX:
|
---|
| 307 | setConstant(i->index, source.getWorldMatrix());
|
---|
| 308 | break;
|
---|
| 309 | case ACT_TIME:
|
---|
| 310 | setConstant(i->index, Vector4(source.getTime() * i->fData, 0.f, 0.f, 0.f));
|
---|
| 311 | break;
|
---|
| 312 | case ACT_TIME_0_X:
|
---|
| 313 | setConstant(i->index, Vector4(source.getTime_0_X(i->fData), 0.f, 0.f, 0.f));
|
---|
| 314 | break;
|
---|
| 315 | case ACT_COSTIME_0_X:
|
---|
| 316 | setConstant(i->index, Vector4(source.getCosTime_0_X(i->fData), 0.f, 0.f, 0.f));
|
---|
| 317 | break;
|
---|
| 318 | case ACT_SINTIME_0_X:
|
---|
| 319 | setConstant(i->index, Vector4(source.getSinTime_0_X(i->fData), 0.f, 0.f, 0.f));
|
---|
| 320 | break;
|
---|
| 321 | case ACT_TANTIME_0_X:
|
---|
| 322 | setConstant(i->index, Vector4(source.getTanTime_0_X(i->fData), 0.f, 0.f, 0.f));
|
---|
| 323 | break;
|
---|
| 324 | case ACT_TIME_0_X_PACKED:
|
---|
| 325 | setConstant(i->index, source.getTime_0_X_packed(i->fData));
|
---|
| 326 | break;
|
---|
| 327 | case ACT_TIME_0_1:
|
---|
| 328 | setConstant(i->index, Vector4(source.getTime_0_1(i->fData), 0.f, 0.f, 0.f));
|
---|
| 329 | break;
|
---|
| 330 | case ACT_COSTIME_0_1:
|
---|
| 331 | setConstant(i->index, Vector4(source.getCosTime_0_1(i->fData), 0.f, 0.f, 0.f));
|
---|
| 332 | break;
|
---|
| 333 | case ACT_SINTIME_0_1:
|
---|
| 334 | setConstant(i->index, Vector4(source.getSinTime_0_1(i->fData), 0.f, 0.f, 0.f));
|
---|
| 335 | break;
|
---|
| 336 | case ACT_TANTIME_0_1:
|
---|
| 337 | setConstant(i->index, Vector4(source.getTanTime_0_1(i->fData), 0.f, 0.f, 0.f));
|
---|
| 338 | break;
|
---|
| 339 | case ACT_TIME_0_1_PACKED:
|
---|
| 340 | setConstant(i->index, source.getTime_0_1_packed(i->fData));
|
---|
| 341 | break;
|
---|
| 342 | case ACT_TIME_0_2PI:
|
---|
| 343 | setConstant(i->index, Vector4(source.getTime_0_2Pi(i->fData), 0.f, 0.f, 0.f));
|
---|
| 344 | break;
|
---|
| 345 | case ACT_COSTIME_0_2PI:
|
---|
| 346 | setConstant(i->index, Vector4(source.getCosTime_0_2Pi(i->fData), 0.f, 0.f, 0.f));
|
---|
| 347 | break;
|
---|
| 348 | case ACT_SINTIME_0_2PI:
|
---|
| 349 | setConstant(i->index, Vector4(source.getSinTime_0_2Pi(i->fData), 0.f, 0.f, 0.f));
|
---|
| 350 | break;
|
---|
| 351 | case ACT_TANTIME_0_2PI:
|
---|
| 352 | setConstant(i->index, Vector4(source.getTanTime_0_2Pi(i->fData), 0.f, 0.f, 0.f));
|
---|
| 353 | break;
|
---|
| 354 | case ACT_TIME_0_2PI_PACKED:
|
---|
| 355 | setConstant(i->index, source.getTime_0_2Pi_packed(i->fData));
|
---|
| 356 | break;
|
---|
| 357 | case ACT_FPS:
|
---|
| 358 | setConstant(i->index, source.getFPS());
|
---|
| 359 | break;
|
---|
| 360 | case ACT_VIEWPORT_WIDTH:
|
---|
| 361 | setConstant(i->index, source.getViewportWidth());
|
---|
| 362 | break;
|
---|
| 363 | case ACT_VIEWPORT_HEIGHT:
|
---|
| 364 | setConstant(i->index, source.getViewportHeight());
|
---|
| 365 | break;
|
---|
| 366 | case ACT_INVERSE_VIEWPORT_WIDTH:
|
---|
| 367 | setConstant(i->index, source.getInverseViewportWidth());
|
---|
| 368 | break;
|
---|
| 369 | case ACT_INVERSE_VIEWPORT_HEIGHT:
|
---|
| 370 | setConstant(i->index, source.getInverseViewportHeight());
|
---|
| 371 | break;
|
---|
| 372 | case ACT_VIEW_DIRECTION:
|
---|
| 373 | setConstant(i->index, source.getViewDirection());
|
---|
| 374 | break;
|
---|
| 375 | case ACT_VIEW_SIDE_VECTOR:
|
---|
| 376 | setConstant(i->index, source.getViewSideVector());
|
---|
| 377 | break;
|
---|
| 378 | case ACT_VIEW_UP_VECTOR:
|
---|
| 379 | setConstant(i->index, source.getViewUpVector());
|
---|
| 380 | break;
|
---|
| 381 | case ACT_FOV:
|
---|
| 382 | setConstant(i->index, source.getFOV());
|
---|
| 383 | break;
|
---|
| 384 | case ACT_NEAR_CLIP_DISTANCE:
|
---|
| 385 | setConstant(i->index, source.getNearClipDistance());
|
---|
| 386 | break;
|
---|
| 387 | case ACT_FAR_CLIP_DISTANCE:
|
---|
| 388 | setConstant(i->index, source.getFarClipDistance());
|
---|
| 389 | break;
|
---|
| 390 | case ACT_INVERSE_VIEWPROJ_MATRIX:
|
---|
| 391 | setConstant(i->index, source.getInverseViewProjMatrix());
|
---|
| 392 | break;
|
---|
| 393 | case ACT_INVERSETRANSPOSE_VIEWPROJ_MATRIX:
|
---|
| 394 | setConstant(i->index, source.getInverseTransposeViewProjMatrix());
|
---|
| 395 | break;
|
---|
| 396 | case ACT_TRANSPOSE_VIEWPROJ_MATRIX:
|
---|
| 397 | setConstant(i->index, source.getTransposeViewProjMatrix());
|
---|
| 398 | break;
|
---|
| 399 | case ACT_TRANSPOSE_VIEW_MATRIX:
|
---|
| 400 | setConstant(i->index, source.getTransposeViewMatrix());
|
---|
| 401 | break;
|
---|
| 402 | case ACT_INVERSE_VIEW_MATRIX:
|
---|
| 403 | setConstant(i->index, source.getInverseViewMatrix());
|
---|
| 404 | break;
|
---|
| 405 | case ACT_INVERSETRANSPOSE_VIEW_MATRIX:
|
---|
| 406 | setConstant(i->index, source.getTransposeViewMatrix());
|
---|
| 407 | break;
|
---|
| 408 | case ACT_TRANSPOSE_PROJECTION_MATRIX:
|
---|
| 409 | setConstant(i->index, source.getTransposeProjectionMatrix());
|
---|
| 410 | break;
|
---|
| 411 | case ACT_INVERSE_PROJECTION_MATRIX:
|
---|
| 412 | setConstant(i->index, source.getInverseProjectionMatrix());
|
---|
| 413 | break;
|
---|
| 414 | case ACT_INVERSETRANSPOSE_PROJECTION_MATRIX:
|
---|
| 415 | setConstant(i->index, source.getInverseTransposeProjectionMatrix());
|
---|
| 416 | break;
|
---|
| 417 | case ACT_TRANSPOSE_WORLDVIEWPROJ_MATRIX:
|
---|
| 418 | setConstant(i->index, source.getTransposeWorldViewProjMatrix());
|
---|
| 419 | break;
|
---|
| 420 | case ACT_INVERSE_WORLDVIEWPROJ_MATRIX:
|
---|
| 421 | setConstant(i->index, source.getInverseWorldViewProjMatrix());
|
---|
| 422 | break;
|
---|
| 423 | case ACT_INVERSETRANSPOSE_WORLDVIEWPROJ_MATRIX:
|
---|
| 424 | setConstant(i->index, source.getInverseTransposeWorldViewProjMatrix());
|
---|
| 425 | break;
|
---|
| 426 | case ACT_TRANSPOSE_WORLDVIEW_MATRIX:
|
---|
| 427 | setConstant(i->index, source.getTransposeWorldViewMatrix());
|
---|
| 428 | break;
|
---|
| 429 | case ACT_INVERSETRANSPOSE_WORLDVIEW_MATRIX:
|
---|
| 430 | setConstant(i->index, source.getInverseTransposeWorldViewMatrix());
|
---|
| 431 | break;
|
---|
| 432 | case ACT_TRANSPOSE_WORLD_MATRIX:
|
---|
| 433 | setConstant(i->index, source.getTransposeWorldMatrix());
|
---|
| 434 | break;
|
---|
| 435 | case ACT_INVERSETRANSPOSE_WORLD_MATRIX:
|
---|
| 436 | setConstant(i->index, source.getInverseTransposeWorldMatrix());
|
---|
| 437 | break;
|
---|
| 438 | case ACT_WORLD_MATRIX_ARRAY:
|
---|
| 439 | setConstant(i->index, source.getWorldMatrixArray(),
|
---|
| 440 | source.getWorldMatrixCount());
|
---|
| 441 | break;
|
---|
| 442 | case ACT_WORLD_MATRIX_ARRAY_3x4:
|
---|
| 443 | // Loop over matrices
|
---|
| 444 | pMatrix = source.getWorldMatrixArray();
|
---|
| 445 | numMatrices = source.getWorldMatrixCount();
|
---|
| 446 | index = i->index;
|
---|
| 447 | for (m = 0; m < numMatrices; ++m)
|
---|
| 448 | {
|
---|
| 449 | GpuProgramParameters::setConstant(index++, (*pMatrix)[0], 1);
|
---|
| 450 | GpuProgramParameters::setConstant(index++, (*pMatrix)[1], 1);
|
---|
| 451 | GpuProgramParameters::setConstant(index++, (*pMatrix)[2], 1);
|
---|
| 452 | ++pMatrix;
|
---|
| 453 | }
|
---|
| 454 |
|
---|
| 455 | break;
|
---|
| 456 | case ACT_VIEW_MATRIX:
|
---|
| 457 | setConstant(i->index, source.getViewMatrix());
|
---|
| 458 | break;
|
---|
| 459 | case ACT_PROJECTION_MATRIX:
|
---|
| 460 | setConstant(i->index, source.getProjectionMatrix());
|
---|
| 461 | break;
|
---|
| 462 | case ACT_WORLDVIEW_MATRIX:
|
---|
| 463 | setConstant(i->index, source.getWorldViewMatrix());
|
---|
| 464 | break;
|
---|
| 465 | case ACT_VIEWPROJ_MATRIX:
|
---|
| 466 | setConstant(i->index, source.getViewProjectionMatrix());
|
---|
| 467 | break;
|
---|
| 468 | case ACT_WORLDVIEWPROJ_MATRIX:
|
---|
| 469 | setConstant(i->index, source.getWorldViewProjMatrix());
|
---|
| 470 | break;
|
---|
| 471 | case ACT_INVERSE_WORLD_MATRIX:
|
---|
| 472 | setConstant(i->index, source.getInverseWorldMatrix());
|
---|
| 473 | break;
|
---|
| 474 | case ACT_INVERSE_WORLDVIEW_MATRIX:
|
---|
| 475 | setConstant(i->index, source.getInverseWorldViewMatrix());
|
---|
| 476 | break;
|
---|
| 477 | case ACT_CAMERA_POSITION:
|
---|
| 478 | setConstant(i->index, source.getCameraPosition());
|
---|
| 479 | break;
|
---|
| 480 | case ACT_CAMERA_POSITION_OBJECT_SPACE:
|
---|
| 481 | setConstant(i->index, source.getCameraPositionObjectSpace());
|
---|
| 482 | break;
|
---|
| 483 | // NB ambient light still here because it's not related to a specific light
|
---|
| 484 | case ACT_AMBIENT_LIGHT_COLOUR:
|
---|
| 485 | setConstant(i->index, source.getAmbientLightColour());
|
---|
| 486 | break;
|
---|
| 487 | case ACT_TEXTURE_VIEWPROJ_MATRIX:
|
---|
| 488 | setConstant(i->index, source.getTextureViewProjMatrix());
|
---|
| 489 | break;
|
---|
| 490 | case ACT_CUSTOM:
|
---|
| 491 | source.getCurrentRenderable()->_updateCustomGpuParameter(*i, this);
|
---|
| 492 | break;
|
---|
| 493 | default:
|
---|
| 494 | break;
|
---|
| 495 | }
|
---|
| 496 | }
|
---|
| 497 | }
|
---|
| 498 | //-----------------------------------------------------------------------------
|
---|
| 499 | void GpuProgramParameters::_updateAutoParamsLightsOnly(const AutoParamDataSource& source)
|
---|
| 500 | {
|
---|
| 501 | if (!hasAutoConstants()) return; // abort early if no autos
|
---|
| 502 | Vector3 vec3;
|
---|
| 503 | Vector4 vec4;
|
---|
| 504 |
|
---|
| 505 | AutoConstantList::const_iterator i, iend;
|
---|
| 506 | iend = mAutoConstants.end();
|
---|
| 507 | for (i = mAutoConstants.begin(); i != iend; ++i)
|
---|
| 508 | {
|
---|
| 509 | switch(i->paramType)
|
---|
| 510 | {
|
---|
| 511 | case ACT_LIGHT_DIFFUSE_COLOUR:
|
---|
| 512 | setConstant(i->index, source.getLight(i->data).getDiffuseColour());
|
---|
| 513 | break;
|
---|
| 514 | case ACT_LIGHT_SPECULAR_COLOUR:
|
---|
| 515 | setConstant(i->index, source.getLight(i->data).getSpecularColour());
|
---|
| 516 | break;
|
---|
| 517 | case ACT_LIGHT_POSITION:
|
---|
| 518 | // Get as 4D vector, works for directional lights too
|
---|
| 519 | setConstant(i->index,
|
---|
| 520 | source.getLight(i->data).getAs4DVector());
|
---|
| 521 | break;
|
---|
| 522 | case ACT_LIGHT_DIRECTION:
|
---|
| 523 | vec3 = source.getLight(i->data).getDerivedDirection();
|
---|
| 524 | // Set as 4D vector for compatibility
|
---|
| 525 | setConstant(i->index, Vector4(vec3.x, vec3.y, vec3.z, 1.0f));
|
---|
| 526 | break;
|
---|
| 527 | case ACT_LIGHT_POSITION_OBJECT_SPACE:
|
---|
| 528 | setConstant(i->index,
|
---|
| 529 | source.getInverseWorldMatrix() * source.getLight(i->data).getAs4DVector());
|
---|
| 530 | break;
|
---|
| 531 | case ACT_LIGHT_DIRECTION_OBJECT_SPACE:
|
---|
| 532 | vec3 = source.getInverseWorldMatrix() *
|
---|
| 533 | source.getLight(i->data).getDerivedDirection();
|
---|
| 534 | vec3.normalise();
|
---|
| 535 | // Set as 4D vector for compatibility
|
---|
| 536 | setConstant(i->index, Vector4(vec3.x, vec3.y, vec3.z, 1.0f));
|
---|
| 537 | break;
|
---|
| 538 | case ACT_LIGHT_DISTANCE_OBJECT_SPACE:
|
---|
| 539 | vec3 = source.getInverseWorldMatrix() * source.getLight(i->data).getDerivedPosition();
|
---|
| 540 | setConstant(i->index, vec3.length());
|
---|
| 541 | break;
|
---|
| 542 | case ACT_SHADOW_EXTRUSION_DISTANCE:
|
---|
| 543 | setConstant(i->index, source.getShadowExtrusionDistance());
|
---|
| 544 | break;
|
---|
| 545 | case ACT_LIGHT_ATTENUATION:
|
---|
| 546 | // range, const, linear, quad
|
---|
| 547 | const Light& l = source.getLight(i->data);
|
---|
| 548 | vec4.x = l.getAttenuationRange();
|
---|
| 549 | vec4.y = l.getAttenuationConstant();
|
---|
| 550 | vec4.z = l.getAttenuationLinear();
|
---|
| 551 | vec4.w = l.getAttenuationQuadric();
|
---|
| 552 | setConstant(i->index, vec4);
|
---|
| 553 | break;
|
---|
| 554 | }
|
---|
| 555 | }
|
---|
| 556 | }
|
---|
| 557 | //---------------------------------------------------------------------------
|
---|
| 558 | void GpuProgramParameters::_mapParameterNameToIndex(const String& name,
|
---|
| 559 | size_t index)
|
---|
| 560 | {
|
---|
| 561 | mParamNameMap[name] = index;
|
---|
| 562 | }
|
---|
| 563 | //---------------------------------------------------------------------------
|
---|
| 564 | size_t GpuProgramParameters::getParamIndex(const String& name)
|
---|
| 565 | {
|
---|
| 566 | ParamNameMap::const_iterator i = mParamNameMap.find(name);
|
---|
| 567 | if (i == mParamNameMap.end())
|
---|
| 568 | {
|
---|
| 569 | // name not found in map, should it be added to the map?
|
---|
| 570 | if(mAutoAddParamName)
|
---|
| 571 | {
|
---|
| 572 | // determine index
|
---|
| 573 | // don't know which Constants list the name is for
|
---|
| 574 | // so pick the largest index
|
---|
| 575 | size_t index = (mRealConstants.size() > mIntConstants.size()) ?
|
---|
| 576 | mRealConstants.size() : mIntConstants.size();
|
---|
| 577 | // allow for at least one Vector4
|
---|
| 578 | mRealConstants.resize(index + 1);
|
---|
| 579 | mIntConstants.resize(index + 1);
|
---|
| 580 | _mapParameterNameToIndex(name, index);
|
---|
| 581 | return index;
|
---|
| 582 | }
|
---|
| 583 | else
|
---|
| 584 | {
|
---|
| 585 | OGRE_EXCEPT(Exception::ERR_ITEM_NOT_FOUND, "Cannot find a parameter named " + name,
|
---|
| 586 | "GpuProgramParameters::getParamIndex");
|
---|
| 587 | }
|
---|
| 588 | }
|
---|
| 589 | return i->second;
|
---|
| 590 | }
|
---|
| 591 | //---------------------------------------------------------------------------
|
---|
| 592 | void GpuProgramParameters::setNamedConstant(const String& name, Real val)
|
---|
| 593 | {
|
---|
| 594 | setConstant(getParamIndex(name), val);
|
---|
| 595 | }
|
---|
| 596 | //---------------------------------------------------------------------------
|
---|
| 597 | void GpuProgramParameters::setNamedConstant(const String& name, int val)
|
---|
| 598 | {
|
---|
| 599 | setConstant(getParamIndex(name), val);
|
---|
| 600 | }
|
---|
| 601 | //---------------------------------------------------------------------------
|
---|
| 602 | void GpuProgramParameters::setNamedConstant(const String& name, const Vector4& vec)
|
---|
| 603 | {
|
---|
| 604 | setConstant(getParamIndex(name), vec);
|
---|
| 605 | }
|
---|
| 606 | //---------------------------------------------------------------------------
|
---|
| 607 | void GpuProgramParameters::setNamedConstant(const String& name, const Vector3& vec)
|
---|
| 608 | {
|
---|
| 609 | setConstant(getParamIndex(name), vec);
|
---|
| 610 | }
|
---|
| 611 | //---------------------------------------------------------------------------
|
---|
| 612 | void GpuProgramParameters::setNamedConstant(const String& name, const Matrix4& m)
|
---|
| 613 | {
|
---|
| 614 | setConstant(getParamIndex(name), m);
|
---|
| 615 | }
|
---|
| 616 | //---------------------------------------------------------------------------
|
---|
| 617 | void GpuProgramParameters::setNamedConstant(const String& name, const Matrix4* m,
|
---|
| 618 | size_t numEntries)
|
---|
| 619 | {
|
---|
| 620 | setConstant(getParamIndex(name), m, numEntries);
|
---|
| 621 | }
|
---|
| 622 | //---------------------------------------------------------------------------
|
---|
| 623 | void GpuProgramParameters::setNamedConstant(const String& name, const float *val, size_t count)
|
---|
| 624 | {
|
---|
| 625 | setConstant(getParamIndex(name), val, count);
|
---|
| 626 | }
|
---|
| 627 | //---------------------------------------------------------------------------
|
---|
| 628 | void GpuProgramParameters::setNamedConstant(const String& name, const double *val, size_t count)
|
---|
| 629 | {
|
---|
| 630 | setConstant(getParamIndex(name), val, count);
|
---|
| 631 | }
|
---|
| 632 | //---------------------------------------------------------------------------
|
---|
| 633 | void GpuProgramParameters::setNamedConstant(const String& name, const ColourValue& colour)
|
---|
| 634 | {
|
---|
| 635 | setConstant(getParamIndex(name), colour);
|
---|
| 636 | }
|
---|
| 637 | //---------------------------------------------------------------------------
|
---|
| 638 | void GpuProgramParameters::setNamedConstant(const String& name, const int *val, size_t count)
|
---|
| 639 | {
|
---|
| 640 | setConstant(getParamIndex(name), val, count);
|
---|
| 641 | }
|
---|
| 642 | //---------------------------------------------------------------------------
|
---|
| 643 | void GpuProgramParameters::setNamedAutoConstant(const String& name, AutoConstantType acType, size_t extraInfo)
|
---|
| 644 | {
|
---|
| 645 | setAutoConstant(getParamIndex(name), acType, extraInfo);
|
---|
| 646 | }
|
---|
| 647 | //---------------------------------------------------------------------------
|
---|
| 648 | void GpuProgramParameters::setConstantFromTime(size_t index, Real factor)
|
---|
| 649 | {
|
---|
| 650 | setAutoConstantReal(index, ACT_TIME, factor);
|
---|
| 651 | }
|
---|
| 652 | //---------------------------------------------------------------------------
|
---|
| 653 | void GpuProgramParameters::setNamedConstantFromTime(const String& name, Real factor)
|
---|
| 654 | {
|
---|
| 655 | setConstantFromTime(getParamIndex(name), factor);
|
---|
| 656 | }
|
---|
| 657 | //---------------------------------------------------------------------------
|
---|
| 658 | GpuProgramParameters::RealConstantIterator GpuProgramParameters::getRealConstantIterator(void) const
|
---|
| 659 | {
|
---|
| 660 | return RealConstantIterator(mRealConstants.begin(), mRealConstants.end());
|
---|
| 661 | }
|
---|
| 662 | //---------------------------------------------------------------------------
|
---|
| 663 | GpuProgramParameters::IntConstantIterator GpuProgramParameters::getIntConstantIterator(void) const
|
---|
| 664 | {
|
---|
| 665 | return IntConstantIterator(mIntConstants.begin(), mIntConstants.end());
|
---|
| 666 | }
|
---|
| 667 |
|
---|
| 668 | //---------------------------------------------------------------------------
|
---|
| 669 | GpuProgramParameters::RealConstantEntry* GpuProgramParameters::getRealConstantEntry(const size_t index)
|
---|
| 670 | {
|
---|
| 671 | if (index < mRealConstants.size())
|
---|
| 672 | {
|
---|
| 673 | return &(mRealConstants[index]);
|
---|
| 674 | }
|
---|
| 675 | else
|
---|
| 676 | {
|
---|
| 677 | return NULL;
|
---|
| 678 | }
|
---|
| 679 | }
|
---|
| 680 |
|
---|
| 681 | //---------------------------------------------------------------------------
|
---|
| 682 | GpuProgramParameters::IntConstantEntry* GpuProgramParameters::getIntConstantEntry(const size_t index)
|
---|
| 683 | {
|
---|
| 684 | if (index < mIntConstants.size())
|
---|
| 685 | {
|
---|
| 686 | return &(mIntConstants[index]);
|
---|
| 687 | }
|
---|
| 688 | else
|
---|
| 689 | {
|
---|
| 690 | return NULL;
|
---|
| 691 | }
|
---|
| 692 | }
|
---|
| 693 |
|
---|
| 694 | //---------------------------------------------------------------------------
|
---|
| 695 | GpuProgramParameters::RealConstantEntry* GpuProgramParameters::getNamedRealConstantEntry(const String& name)
|
---|
| 696 | {
|
---|
| 697 | // check if name is found
|
---|
| 698 | ParamNameMap::const_iterator i = mParamNameMap.find(name);
|
---|
| 699 |
|
---|
| 700 | if (i == mParamNameMap.end())
|
---|
| 701 | {
|
---|
| 702 | // no valid name found
|
---|
| 703 | return NULL;
|
---|
| 704 | }
|
---|
| 705 | else
|
---|
| 706 | {
|
---|
| 707 | // name found: return the entry
|
---|
| 708 | return getRealConstantEntry(i->second);
|
---|
| 709 | }
|
---|
| 710 |
|
---|
| 711 | }
|
---|
| 712 |
|
---|
| 713 | //---------------------------------------------------------------------------
|
---|
| 714 | GpuProgramParameters::IntConstantEntry* GpuProgramParameters::getNamedIntConstantEntry(const String& name)
|
---|
| 715 | {
|
---|
| 716 | // check if name is found
|
---|
| 717 | ParamNameMap::const_iterator i = mParamNameMap.find(name);
|
---|
| 718 |
|
---|
| 719 | if (i == mParamNameMap.end())
|
---|
| 720 | {
|
---|
| 721 | // no valid name found
|
---|
| 722 | return NULL;
|
---|
| 723 | }
|
---|
| 724 | else
|
---|
| 725 | {
|
---|
| 726 | // name found: return the entry
|
---|
| 727 | return getIntConstantEntry(i->second);
|
---|
| 728 | }
|
---|
| 729 |
|
---|
| 730 | }
|
---|
| 731 |
|
---|
| 732 | //---------------------------------------------------------------------------
|
---|
| 733 | void GpuProgramParameters::copyConstantsFrom(const GpuProgramParameters& source)
|
---|
| 734 | {
|
---|
| 735 | // Iterate over fixed parameters
|
---|
| 736 | RealConstantIterator ri = source.getRealConstantIterator();
|
---|
| 737 | ushort i = 0;
|
---|
| 738 | while(ri.hasMoreElements())
|
---|
| 739 | {
|
---|
| 740 | RealConstantEntry re = ri.getNext();
|
---|
| 741 | if (re.isSet)
|
---|
| 742 | {
|
---|
| 743 | setConstant(i, re.val, 4);
|
---|
| 744 | }
|
---|
| 745 | ++i;
|
---|
| 746 |
|
---|
| 747 | }
|
---|
| 748 | IntConstantIterator ii = source.getIntConstantIterator();
|
---|
| 749 | i = 0;
|
---|
| 750 | while (ii.hasMoreElements())
|
---|
| 751 | {
|
---|
| 752 | IntConstantEntry ie = ii.getNext();
|
---|
| 753 | if (ie.isSet)
|
---|
| 754 | {
|
---|
| 755 | setConstant(i, ie.val, 4);
|
---|
| 756 | }
|
---|
| 757 | ++i;
|
---|
| 758 | }
|
---|
| 759 |
|
---|
| 760 | // Iterate over auto parameters
|
---|
| 761 | // Clear existing auto constants
|
---|
| 762 | clearAutoConstants();
|
---|
| 763 | AutoConstantIterator ai = source.getAutoConstantIterator();
|
---|
| 764 | while (ai.hasMoreElements())
|
---|
| 765 | {
|
---|
| 766 | AutoConstantEntry ae = ai.getNext();
|
---|
| 767 | setAutoConstant(ae.index, ae.paramType, ae.data);
|
---|
| 768 | }
|
---|
| 769 |
|
---|
| 770 | // need to copy Parameter names from the source
|
---|
| 771 | mParamNameMap = source.mParamNameMap;
|
---|
| 772 |
|
---|
| 773 | }
|
---|
| 774 | //-----------------------------------------------------------------------
|
---|
| 775 | //-----------------------------------------------------------------------
|
---|
| 776 | String GpuProgram::CmdType::doGet(const void* target) const
|
---|
| 777 | {
|
---|
| 778 | const GpuProgram* t = static_cast<const GpuProgram*>(target);
|
---|
| 779 | if (t->getType() == GPT_VERTEX_PROGRAM)
|
---|
| 780 | {
|
---|
| 781 | return "vertex_program";
|
---|
| 782 | }
|
---|
| 783 | else
|
---|
| 784 | {
|
---|
| 785 | return "fragment_program";
|
---|
| 786 | }
|
---|
| 787 | }
|
---|
| 788 | void GpuProgram::CmdType::doSet(void* target, const String& val)
|
---|
| 789 | {
|
---|
| 790 | GpuProgram* t = static_cast<GpuProgram*>(target);
|
---|
| 791 | if (val == "vertex_program")
|
---|
| 792 | {
|
---|
| 793 | t->setType(GPT_VERTEX_PROGRAM);
|
---|
| 794 | }
|
---|
| 795 | else
|
---|
| 796 | {
|
---|
| 797 | t->setType(GPT_FRAGMENT_PROGRAM);
|
---|
| 798 | }
|
---|
| 799 | }
|
---|
| 800 | //-----------------------------------------------------------------------
|
---|
| 801 | String GpuProgram::CmdSyntax::doGet(const void* target) const
|
---|
| 802 | {
|
---|
| 803 | const GpuProgram* t = static_cast<const GpuProgram*>(target);
|
---|
| 804 | return t->getSyntaxCode();
|
---|
| 805 | }
|
---|
| 806 | void GpuProgram::CmdSyntax::doSet(void* target, const String& val)
|
---|
| 807 | {
|
---|
| 808 | GpuProgram* t = static_cast<GpuProgram*>(target);
|
---|
| 809 | t->setSyntaxCode(val);
|
---|
| 810 | }
|
---|
| 811 | //-----------------------------------------------------------------------
|
---|
| 812 | String GpuProgram::CmdSkeletal::doGet(const void* target) const
|
---|
| 813 | {
|
---|
| 814 | const GpuProgram* t = static_cast<const GpuProgram*>(target);
|
---|
| 815 | return StringConverter::toString(t->isSkeletalAnimationIncluded());
|
---|
| 816 | }
|
---|
| 817 | void GpuProgram::CmdSkeletal::doSet(void* target, const String& val)
|
---|
| 818 | {
|
---|
| 819 | GpuProgram* t = static_cast<GpuProgram*>(target);
|
---|
| 820 | t->setSkeletalAnimationIncluded(StringConverter::parseBool(val));
|
---|
| 821 | }
|
---|
| 822 | //-----------------------------------------------------------------------
|
---|
| 823 | GpuProgramPtr& GpuProgramPtr::operator=(const HighLevelGpuProgramPtr& r)
|
---|
| 824 | {
|
---|
| 825 | // Can assign direct
|
---|
| 826 | if (pRep == r.getPointer())
|
---|
| 827 | return *this;
|
---|
| 828 | release();
|
---|
| 829 | // lock & copy other mutex pointer
|
---|
| 830 | OGRE_LOCK_MUTEX(*r.OGRE_AUTO_MUTEX_NAME)
|
---|
| 831 | OGRE_COPY_AUTO_SHARED_MUTEX(r.OGRE_AUTO_MUTEX_NAME)
|
---|
| 832 | pRep = r.getPointer();
|
---|
| 833 | pUseCount = r.useCountPointer();
|
---|
| 834 | if (pUseCount)
|
---|
| 835 | {
|
---|
| 836 | ++(*pUseCount);
|
---|
| 837 | }
|
---|
| 838 | return *this;
|
---|
| 839 | }
|
---|
| 840 |
|
---|
| 841 | }
|
---|