CmGpuProgramParams.cpp 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855
  1. /*
  2. -----------------------------------------------------------------------------
  3. This source file is part of OGRE
  4. (Object-oriented Graphics Rendering Engine)
  5. For the latest info, see http://www.ogre3d.org
  6. Copyright (c) 2000-2011 Torus Knot Software Ltd
  7. Permission is hereby granted, free of charge, to any person obtaining a copy
  8. of this software and associated documentation files (the "Software"), to deal
  9. in the Software without restriction, including without limitation the rights
  10. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. copies of the Software, and to permit persons to whom the Software is
  12. furnished to do so, subject to the following conditions:
  13. The above copyright notice and this permission notice shall be included in
  14. all copies or substantial portions of the Software.
  15. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. THE SOFTWARE.
  22. -----------------------------------------------------------------------------
  23. */
  24. #include "CmGpuProgramParams.h"
  25. #include "CmMatrix4.h"
  26. #include "CmMatrix3.h"
  27. #include "CmVector2.h"
  28. #include "CmVector3.h"
  29. #include "CmVector4.h"
  30. #include "CmTexture.h"
  31. #include "CmRenderSystemCapabilities.h"
  32. #include "CmException.h"
  33. namespace CamelotEngine
  34. {
  35. bool GpuNamedConstants::msGenerateAllConstantDefinitionArrayEntries = false;
  36. //---------------------------------------------------------------------
  37. void GpuNamedConstants::generateConstantDefinitionArrayEntries(
  38. const String& paramName, const GpuConstantDefinition& baseDef)
  39. {
  40. // Copy definition for use with arrays
  41. GpuConstantDefinition arrayDef = baseDef;
  42. arrayDef.arraySize = 1;
  43. String arrayName;
  44. // Add parameters for array accessors
  45. // [0] will refer to the same location, [1+] will increment
  46. // only populate others individually up to 16 array slots so as not to get out of hand,
  47. // unless the system has been explicitly configured to allow all the parameters to be added
  48. // paramName[0] version will always exist
  49. size_t maxArrayIndex = 1;
  50. if (baseDef.arraySize <= 16 || msGenerateAllConstantDefinitionArrayEntries)
  51. maxArrayIndex = baseDef.arraySize;
  52. for (size_t i = 0; i < maxArrayIndex; i++)
  53. {
  54. arrayName = paramName + "[" + toString(i) + "]";
  55. map.insert(GpuConstantDefinitionMap::value_type(arrayName, arrayDef));
  56. // increment location
  57. arrayDef.physicalIndex += arrayDef.elementSize;
  58. }
  59. // note no increment of buffer sizes since this is shared with main array def
  60. }
  61. //---------------------------------------------------------------------
  62. bool GpuNamedConstants::getGenerateAllConstantDefinitionArrayEntries()
  63. {
  64. return msGenerateAllConstantDefinitionArrayEntries;
  65. }
  66. //---------------------------------------------------------------------
  67. void GpuNamedConstants::setGenerateAllConstantDefinitionArrayEntries(bool generateAll)
  68. {
  69. msGenerateAllConstantDefinitionArrayEntries = generateAll;
  70. }
  71. //-----------------------------------------------------------------------------
  72. // GpuProgramParameters Methods
  73. //-----------------------------------------------------------------------------
  74. GpuProgramParameters::GpuProgramParameters() :
  75. mCombinedVariability(GPV_GLOBAL)
  76. , mTransposeMatrices(false)
  77. , mIgnoreMissingParams(false)
  78. , mActivePassIterationIndex(std::numeric_limits<size_t>::max())
  79. {
  80. }
  81. //-----------------------------------------------------------------------------
  82. GpuProgramParameters::GpuProgramParameters(const GpuProgramParameters& oth)
  83. {
  84. *this = oth;
  85. }
  86. //-----------------------------------------------------------------------------
  87. GpuProgramParameters& GpuProgramParameters::operator=(const GpuProgramParameters& oth)
  88. {
  89. // let compiler perform shallow copies of structures
  90. // RealConstantEntry, IntConstantEntry
  91. mFloatConstants = oth.mFloatConstants;
  92. mIntConstants = oth.mIntConstants;
  93. mFloatLogicalToPhysical = oth.mFloatLogicalToPhysical;
  94. mIntLogicalToPhysical = oth.mIntLogicalToPhysical;
  95. mSamplerLogicalToPhysical = oth.mSamplerLogicalToPhysical;
  96. mNamedConstants = oth.mNamedConstants;
  97. mCombinedVariability = oth.mCombinedVariability;
  98. mTransposeMatrices = oth.mTransposeMatrices;
  99. mIgnoreMissingParams = oth.mIgnoreMissingParams;
  100. mActivePassIterationIndex = oth.mActivePassIterationIndex;
  101. return *this;
  102. }
  103. //---------------------------------------------------------------------
  104. void GpuProgramParameters::_setNamedConstants(
  105. const GpuNamedConstantsPtr& namedConstants)
  106. {
  107. mNamedConstants = namedConstants;
  108. // Determine any extension to local buffers
  109. // Size and reset buffer (fill with zero to make comparison later ok)
  110. if (namedConstants->floatBufferSize > mFloatConstants.size())
  111. {
  112. mFloatConstants.insert(mFloatConstants.end(),
  113. namedConstants->floatBufferSize - mFloatConstants.size(), 0.0f);
  114. }
  115. if (namedConstants->intBufferSize > mIntConstants.size())
  116. {
  117. mIntConstants.insert(mIntConstants.end(),
  118. namedConstants->intBufferSize - mIntConstants.size(), 0);
  119. }
  120. if (namedConstants->samplerCount > mTextures.size())
  121. {
  122. mTextures.insert(mTextures.end(),
  123. namedConstants->samplerCount - mTextures.size(), nullptr);
  124. }
  125. }
  126. //---------------------------------------------------------------------
  127. void GpuProgramParameters::_setLogicalIndexes(
  128. const GpuLogicalBufferStructPtr& floatIndexMap,
  129. const GpuLogicalBufferStructPtr& intIndexMap,
  130. const GpuLogicalBufferStructPtr& samplerIndexMap)
  131. {
  132. mFloatLogicalToPhysical = floatIndexMap;
  133. mIntLogicalToPhysical = intIndexMap;
  134. mSamplerLogicalToPhysical = samplerIndexMap;
  135. // resize the internal buffers
  136. // Note that these will only contain something after the first parameter
  137. // set has set some parameters
  138. // Size and reset buffer (fill with zero to make comparison later ok)
  139. if ((floatIndexMap != nullptr) && floatIndexMap->bufferSize > mFloatConstants.size())
  140. {
  141. mFloatConstants.insert(mFloatConstants.end(),
  142. floatIndexMap->bufferSize - mFloatConstants.size(), 0.0f);
  143. }
  144. if ((intIndexMap != nullptr) && intIndexMap->bufferSize > mIntConstants.size())
  145. {
  146. mIntConstants.insert(mIntConstants.end(),
  147. intIndexMap->bufferSize - mIntConstants.size(), 0);
  148. }
  149. if ((samplerIndexMap != nullptr) && samplerIndexMap->bufferSize > mTextures.size())
  150. {
  151. mTextures.insert(mTextures.end(),
  152. samplerIndexMap->bufferSize - mTextures.size(), nullptr);
  153. }
  154. }
  155. //---------------------------------------------------------------------()
  156. void GpuProgramParameters::setConstant(size_t index, const Vector4& vec)
  157. {
  158. setConstant(index, vec.ptr(), 1);
  159. }
  160. //-----------------------------------------------------------------------------
  161. void GpuProgramParameters::setConstant(size_t index, float val)
  162. {
  163. setConstant(index, Vector4(val, 0.0f, 0.0f, 0.0f));
  164. }
  165. //-----------------------------------------------------------------------------
  166. void GpuProgramParameters::setConstant(size_t index, const Vector3& vec)
  167. {
  168. setConstant(index, Vector4(vec.x, vec.y, vec.z, 1.0f));
  169. }
  170. //-----------------------------------------------------------------------------
  171. void GpuProgramParameters::setConstant(size_t index, const Matrix4& m)
  172. {
  173. // set as 4x 4-element floats
  174. if (mTransposeMatrices)
  175. {
  176. Matrix4 t = m.transpose();
  177. GpuProgramParameters::setConstant(index, t[0], 4);
  178. }
  179. else
  180. {
  181. GpuProgramParameters::setConstant(index, m[0], 4);
  182. }
  183. }
  184. //-----------------------------------------------------------------------------
  185. void GpuProgramParameters::setConstant(size_t index, const Matrix4* pMatrix,
  186. size_t numEntries)
  187. {
  188. if (mTransposeMatrices)
  189. {
  190. for (size_t i = 0; i < numEntries; ++i)
  191. {
  192. Matrix4 t = pMatrix[i].transpose();
  193. GpuProgramParameters::setConstant(index, t[0], 4);
  194. index += 4;
  195. }
  196. }
  197. else
  198. {
  199. GpuProgramParameters::setConstant(index, pMatrix[0][0], 4 * numEntries);
  200. }
  201. }
  202. //-----------------------------------------------------------------------------
  203. void GpuProgramParameters::setConstant(size_t index, const Color& colour)
  204. {
  205. setConstant(index, colour.ptr(), 1);
  206. }
  207. //-----------------------------------------------------------------------------
  208. void GpuProgramParameters::setConstant(size_t index, const float *val, size_t count)
  209. {
  210. // Raw buffer size is 4x count
  211. size_t rawCount = count * 4;
  212. // get physical index
  213. assert((mFloatLogicalToPhysical != nullptr) && "GpuProgram hasn't set up the logical -> physical map!");
  214. size_t physicalIndex = _getFloatConstantPhysicalIndex(index, rawCount, GPV_GLOBAL);
  215. // Copy
  216. _writeRawConstants(physicalIndex, val, rawCount);
  217. }
  218. //-----------------------------------------------------------------------------
  219. void GpuProgramParameters::setConstant(size_t index, const double *val, size_t count)
  220. {
  221. // Raw buffer size is 4x count
  222. size_t rawCount = count * 4;
  223. // get physical index
  224. assert((mFloatLogicalToPhysical != nullptr) && "GpuProgram hasn't set up the logical -> physical map!");
  225. size_t physicalIndex = _getFloatConstantPhysicalIndex(index, rawCount, GPV_GLOBAL);
  226. assert(physicalIndex + rawCount <= mFloatConstants.size());
  227. // Copy manually since cast required
  228. for (size_t i = 0; i < rawCount; ++i)
  229. {
  230. mFloatConstants[physicalIndex + i] =
  231. static_cast<float>(val[i]);
  232. }
  233. }
  234. //-----------------------------------------------------------------------------
  235. void GpuProgramParameters::setConstant(size_t index, const int *val, size_t count)
  236. {
  237. // Raw buffer size is 4x count
  238. size_t rawCount = count * 4;
  239. // get physical index
  240. assert((mIntLogicalToPhysical != nullptr) && "GpuProgram hasn't set up the logical -> physical map!");
  241. size_t physicalIndex = _getIntConstantPhysicalIndex(index, rawCount, GPV_GLOBAL);
  242. // Copy
  243. _writeRawConstants(physicalIndex, val, rawCount);
  244. }
  245. //-----------------------------------------------------------------------------
  246. void GpuProgramParameters::_writeRawConstant(size_t physicalIndex, const Vector4& vec,
  247. size_t count)
  248. {
  249. // remember, raw content access uses raw float count rather than float4
  250. // write either the number requested (for packed types) or up to 4
  251. _writeRawConstants(physicalIndex, vec.ptr(), std::min(count, (size_t)4));
  252. }
  253. //-----------------------------------------------------------------------------
  254. void GpuProgramParameters::_writeRawConstant(size_t physicalIndex, float val)
  255. {
  256. _writeRawConstants(physicalIndex, &val, 1);
  257. }
  258. //-----------------------------------------------------------------------------
  259. void GpuProgramParameters::_writeRawConstant(size_t physicalIndex, int val)
  260. {
  261. _writeRawConstants(physicalIndex, &val, 1);
  262. }
  263. //-----------------------------------------------------------------------------
  264. void GpuProgramParameters::_writeRawConstant(size_t physicalIndex, const Vector3& vec)
  265. {
  266. _writeRawConstants(physicalIndex, vec.ptr(), 3);
  267. }
  268. //-----------------------------------------------------------------------------
  269. void GpuProgramParameters::_writeRawConstant(size_t physicalIndex, const Vector2& vec)
  270. {
  271. _writeRawConstants(physicalIndex, vec.ptr(), 2);
  272. }
  273. //-----------------------------------------------------------------------------
  274. void GpuProgramParameters::_writeRawConstant(size_t physicalIndex, const Matrix4& m,size_t elementCount)
  275. {
  276. // remember, raw content access uses raw float count rather than float4
  277. if (mTransposeMatrices)
  278. {
  279. Matrix4 t = m.transpose();
  280. _writeRawConstants(physicalIndex, t[0], elementCount>16?16:elementCount);
  281. }
  282. else
  283. {
  284. _writeRawConstants(physicalIndex, m[0], elementCount>16?16:elementCount);
  285. }
  286. }
  287. //-----------------------------------------------------------------------------
  288. void GpuProgramParameters::_writeRawConstant(size_t physicalIndex, const Matrix4* pMatrix, size_t numEntries)
  289. {
  290. // remember, raw content access uses raw float count rather than float4
  291. if (mTransposeMatrices)
  292. {
  293. for (size_t i = 0; i < numEntries; ++i)
  294. {
  295. Matrix4 t = pMatrix[i].transpose();
  296. _writeRawConstants(physicalIndex, t[0], 16);
  297. physicalIndex += 16;
  298. }
  299. }
  300. else
  301. {
  302. _writeRawConstants(physicalIndex, pMatrix[0][0], 16 * numEntries);
  303. }
  304. }
  305. //-----------------------------------------------------------------------------
  306. void GpuProgramParameters::_writeRawConstant(size_t physicalIndex, const Matrix3& m,size_t elementCount)
  307. {
  308. // remember, raw content access uses raw float count rather than float4
  309. if (mTransposeMatrices)
  310. {
  311. Matrix3 t = m.Transpose();
  312. _writeRawConstants(physicalIndex, t[0], elementCount>9?9:elementCount);
  313. }
  314. else
  315. {
  316. _writeRawConstants(physicalIndex, m[0], elementCount>9?9:elementCount);
  317. }
  318. }
  319. //-----------------------------------------------------------------------------
  320. void GpuProgramParameters::_writeRawConstant(size_t physicalIndex,
  321. const Color& colour, size_t count)
  322. {
  323. // write either the number requested (for packed types) or up to 4
  324. _writeRawConstants(physicalIndex, colour.ptr(), std::min(count, (size_t)4));
  325. }
  326. //-----------------------------------------------------------------------------
  327. void GpuProgramParameters::_writeRawConstants(size_t physicalIndex, const double* val, size_t count)
  328. {
  329. assert(physicalIndex + count <= mFloatConstants.size());
  330. for (size_t i = 0; i < count; ++i)
  331. {
  332. mFloatConstants[physicalIndex+i] = static_cast<float>(val[i]);
  333. }
  334. }
  335. //-----------------------------------------------------------------------------
  336. void GpuProgramParameters::_writeRawConstants(size_t physicalIndex, const float* val, size_t count)
  337. {
  338. assert(physicalIndex + count <= mFloatConstants.size());
  339. memcpy(&mFloatConstants[physicalIndex], val, sizeof(float) * count);
  340. }
  341. //-----------------------------------------------------------------------------
  342. void GpuProgramParameters::_writeRawConstants(size_t physicalIndex, const int* val, size_t count)
  343. {
  344. assert(physicalIndex + count <= mIntConstants.size());
  345. memcpy(&mIntConstants[physicalIndex], val, sizeof(int) * count);
  346. }
  347. //-----------------------------------------------------------------------------
  348. void GpuProgramParameters::_readRawConstants(size_t physicalIndex, size_t count, float* dest)
  349. {
  350. assert(physicalIndex + count <= mFloatConstants.size());
  351. memcpy(dest, &mFloatConstants[physicalIndex], sizeof(float) * count);
  352. }
  353. //-----------------------------------------------------------------------------
  354. void GpuProgramParameters::_readRawConstants(size_t physicalIndex, size_t count, int* dest)
  355. {
  356. assert(physicalIndex + count <= mIntConstants.size());
  357. memcpy(dest, &mIntConstants[physicalIndex], sizeof(int) * count);
  358. }
  359. //-----------------------------------------------------------------------------
  360. void GpuProgramParameters::_readTexture(size_t physicalIndex, TextureRef& dest)
  361. {
  362. assert(physicalIndex < mTextures.size());
  363. dest = mTextures[physicalIndex];
  364. }
  365. //---------------------------------------------------------------------
  366. GpuLogicalIndexUse* GpuProgramParameters::_getFloatConstantLogicalIndexUse(
  367. size_t logicalIndex, size_t requestedSize, UINT16 variability)
  368. {
  369. if (mFloatLogicalToPhysical == nullptr)
  370. return 0;
  371. GpuLogicalIndexUse* indexUse = 0;
  372. CM_LOCK_MUTEX(mFloatLogicalToPhysical->mutex)
  373. GpuLogicalIndexUseMap::iterator logi = mFloatLogicalToPhysical->map.find(logicalIndex);
  374. if (logi == mFloatLogicalToPhysical->map.end())
  375. {
  376. if (requestedSize)
  377. {
  378. size_t physicalIndex = mFloatConstants.size();
  379. // Expand at buffer end
  380. mFloatConstants.insert(mFloatConstants.end(), requestedSize, 0.0f);
  381. // Record extended size for future GPU params re-using this information
  382. mFloatLogicalToPhysical->bufferSize = mFloatConstants.size();
  383. // low-level programs will not know about mapping ahead of time, so
  384. // populate it. Other params objects will be able to just use this
  385. // accepted mapping since the constant structure will be the same
  386. // Set up a mapping for all items in the count
  387. size_t currPhys = physicalIndex;
  388. size_t count = requestedSize / 4;
  389. GpuLogicalIndexUseMap::iterator insertedIterator;
  390. for (size_t logicalNum = 0; logicalNum < count; ++logicalNum)
  391. {
  392. GpuLogicalIndexUseMap::iterator it =
  393. mFloatLogicalToPhysical->map.insert(
  394. GpuLogicalIndexUseMap::value_type(
  395. logicalIndex + logicalNum,
  396. GpuLogicalIndexUse(currPhys, requestedSize, variability))).first;
  397. currPhys += 4;
  398. if (logicalNum == 0)
  399. insertedIterator = it;
  400. }
  401. indexUse = &(insertedIterator->second);
  402. }
  403. else
  404. {
  405. // no match & ignore
  406. return 0;
  407. }
  408. }
  409. else
  410. {
  411. size_t physicalIndex = logi->second.physicalIndex;
  412. indexUse = &(logi->second);
  413. // check size
  414. if (logi->second.currentSize < requestedSize)
  415. {
  416. // init buffer entry wasn't big enough; could be a mistake on the part
  417. // of the original use, or perhaps a variable length we can't predict
  418. // until first actual runtime use e.g. world matrix array
  419. size_t insertCount = requestedSize - logi->second.currentSize;
  420. FloatConstantList::iterator insertPos = mFloatConstants.begin();
  421. std::advance(insertPos, physicalIndex);
  422. mFloatConstants.insert(insertPos, insertCount, 0.0f);
  423. // shift all physical positions after this one
  424. for (GpuLogicalIndexUseMap::iterator i = mFloatLogicalToPhysical->map.begin();
  425. i != mFloatLogicalToPhysical->map.end(); ++i)
  426. {
  427. if (i->second.physicalIndex > physicalIndex)
  428. i->second.physicalIndex += insertCount;
  429. }
  430. mFloatLogicalToPhysical->bufferSize += insertCount;
  431. if (mNamedConstants != nullptr)
  432. {
  433. for (GpuConstantDefinitionMap::iterator i = mNamedConstants->map.begin();
  434. i != mNamedConstants->map.end(); ++i)
  435. {
  436. if (i->second.isFloat() && i->second.physicalIndex > physicalIndex)
  437. i->second.physicalIndex += insertCount;
  438. }
  439. mNamedConstants->floatBufferSize += insertCount;
  440. }
  441. logi->second.currentSize += insertCount;
  442. }
  443. }
  444. if (indexUse)
  445. indexUse->variability = variability;
  446. return indexUse;
  447. }
  448. //---------------------------------------------------------------------()
  449. GpuLogicalIndexUse* GpuProgramParameters::_getIntConstantLogicalIndexUse(size_t logicalIndex, size_t requestedSize, UINT16 variability)
  450. {
  451. if (mIntLogicalToPhysical == nullptr)
  452. {
  453. CM_EXCEPT(InvalidParametersException,
  454. "This is not a low-level parameter parameter object");
  455. }
  456. GpuLogicalIndexUse* indexUse = 0;
  457. CM_LOCK_MUTEX(mIntLogicalToPhysical->mutex)
  458. GpuLogicalIndexUseMap::iterator logi = mIntLogicalToPhysical->map.find(logicalIndex);
  459. if (logi == mIntLogicalToPhysical->map.end())
  460. {
  461. if (requestedSize)
  462. {
  463. size_t physicalIndex = mIntConstants.size();
  464. // Expand at buffer end
  465. mIntConstants.insert(mIntConstants.end(), requestedSize, 0);
  466. // Record extended size for future GPU params re-using this information
  467. mIntLogicalToPhysical->bufferSize = mIntConstants.size();
  468. // low-level programs will not know about mapping ahead of time, so
  469. // populate it. Other params objects will be able to just use this
  470. // accepted mapping since the constant structure will be the same
  471. // Set up a mapping for all items in the count
  472. size_t currPhys = physicalIndex;
  473. size_t count = requestedSize / 4;
  474. GpuLogicalIndexUseMap::iterator insertedIterator;
  475. for (size_t logicalNum = 0; logicalNum < count; ++logicalNum)
  476. {
  477. GpuLogicalIndexUseMap::iterator it =
  478. mIntLogicalToPhysical->map.insert(
  479. GpuLogicalIndexUseMap::value_type(
  480. logicalIndex + logicalNum,
  481. GpuLogicalIndexUse(currPhys, requestedSize, variability))).first;
  482. if (logicalNum == 0)
  483. insertedIterator = it;
  484. currPhys += 4;
  485. }
  486. indexUse = &(insertedIterator->second);
  487. }
  488. else
  489. {
  490. // no match
  491. return 0;
  492. }
  493. }
  494. else
  495. {
  496. size_t physicalIndex = logi->second.physicalIndex;
  497. indexUse = &(logi->second);
  498. // check size
  499. if (logi->second.currentSize < requestedSize)
  500. {
  501. // init buffer entry wasn't big enough; could be a mistake on the part
  502. // of the original use, or perhaps a variable length we can't predict
  503. // until first actual runtime use e.g. world matrix array
  504. size_t insertCount = requestedSize - logi->second.currentSize;
  505. IntConstantList::iterator insertPos = mIntConstants.begin();
  506. std::advance(insertPos, physicalIndex);
  507. mIntConstants.insert(insertPos, insertCount, 0);
  508. // shift all physical positions after this one
  509. for (GpuLogicalIndexUseMap::iterator i = mIntLogicalToPhysical->map.begin();
  510. i != mIntLogicalToPhysical->map.end(); ++i)
  511. {
  512. if (i->second.physicalIndex > physicalIndex)
  513. i->second.physicalIndex += insertCount;
  514. }
  515. mIntLogicalToPhysical->bufferSize += insertCount;
  516. if (mNamedConstants != nullptr)
  517. {
  518. for (GpuConstantDefinitionMap::iterator i = mNamedConstants->map.begin();
  519. i != mNamedConstants->map.end(); ++i)
  520. {
  521. if (!i->second.isFloat() && i->second.physicalIndex > physicalIndex)
  522. i->second.physicalIndex += insertCount;
  523. }
  524. mNamedConstants->intBufferSize += insertCount;
  525. }
  526. logi->second.currentSize += insertCount;
  527. }
  528. }
  529. if (indexUse)
  530. indexUse->variability = variability;
  531. return indexUse;
  532. }
  533. //-----------------------------------------------------------------------------
  534. size_t GpuProgramParameters::_getFloatConstantPhysicalIndex(
  535. size_t logicalIndex, size_t requestedSize, UINT16 variability)
  536. {
  537. GpuLogicalIndexUse* indexUse = _getFloatConstantLogicalIndexUse(logicalIndex, requestedSize, variability);
  538. return indexUse ? indexUse->physicalIndex : 0;
  539. }
  540. //-----------------------------------------------------------------------------
  541. size_t GpuProgramParameters::_getIntConstantPhysicalIndex(
  542. size_t logicalIndex, size_t requestedSize, UINT16 variability)
  543. {
  544. GpuLogicalIndexUse* indexUse = _getIntConstantLogicalIndexUse(logicalIndex, requestedSize, variability);
  545. return indexUse ? indexUse->physicalIndex : 0;
  546. }
  547. //-----------------------------------------------------------------------------
  548. size_t GpuProgramParameters::getFloatLogicalIndexForPhysicalIndex(size_t physicalIndex)
  549. {
  550. // perhaps build a reverse map of this sometime (shared in GpuProgram)
  551. for (GpuLogicalIndexUseMap::iterator i = mFloatLogicalToPhysical->map.begin();
  552. i != mFloatLogicalToPhysical->map.end(); ++i)
  553. {
  554. if (i->second.physicalIndex == physicalIndex)
  555. return i->first;
  556. }
  557. return std::numeric_limits<size_t>::max();
  558. }
  559. //-----------------------------------------------------------------------------
  560. size_t GpuProgramParameters::getIntLogicalIndexForPhysicalIndex(size_t physicalIndex)
  561. {
  562. // perhaps build a reverse map of this sometime (shared in GpuProgram)
  563. for (GpuLogicalIndexUseMap::iterator i = mIntLogicalToPhysical->map.begin();
  564. i != mIntLogicalToPhysical->map.end(); ++i)
  565. {
  566. if (i->second.physicalIndex == physicalIndex)
  567. return i->first;
  568. }
  569. return std::numeric_limits<size_t>::max();
  570. }
  571. //-----------------------------------------------------------------------------
  572. GpuConstantDefinitionIterator GpuProgramParameters::getConstantDefinitionIterator(void) const
  573. {
  574. if (mNamedConstants == nullptr)
  575. {
  576. CM_EXCEPT(InvalidParametersException,
  577. "This params object is not based on a program with named parameters.");
  578. }
  579. return mNamedConstants->map.begin();
  580. }
  581. //-----------------------------------------------------------------------------
  582. const GpuNamedConstants& GpuProgramParameters::getConstantDefinitions() const
  583. {
  584. if (mNamedConstants == nullptr)
  585. {
  586. CM_EXCEPT(InvalidParametersException,
  587. "This params object is not based on a program with named parameters.");
  588. }
  589. return *mNamedConstants;
  590. }
  591. //-----------------------------------------------------------------------------
  592. const GpuConstantDefinition& GpuProgramParameters::getConstantDefinition(const String& name) const
  593. {
  594. if (mNamedConstants == nullptr)
  595. {
  596. CM_EXCEPT(InvalidParametersException,
  597. "This params object is not based on a program with named parameters.");
  598. }
  599. // locate, and throw exception if not found
  600. const GpuConstantDefinition* def = _findNamedConstantDefinition(name, true);
  601. return *def;
  602. }
  603. //-----------------------------------------------------------------------------
  604. bool GpuProgramParameters::hasNamedConstant(const String& name) const
  605. {
  606. return _findNamedConstantDefinition(name) != nullptr;
  607. }
  608. //-----------------------------------------------------------------------------
  609. const GpuConstantDefinition*
  610. GpuProgramParameters::_findNamedConstantDefinition(const String& name,
  611. bool throwExceptionIfNotFound) const
  612. {
  613. if (mNamedConstants == nullptr)
  614. {
  615. if (throwExceptionIfNotFound)
  616. {
  617. CM_EXCEPT(InvalidParametersException,
  618. "Named constants have not been initialised, perhaps a compile error.");
  619. }
  620. return 0;
  621. }
  622. GpuConstantDefinitionMap::const_iterator i = mNamedConstants->map.find(name);
  623. if (i == mNamedConstants->map.end())
  624. {
  625. if (throwExceptionIfNotFound)
  626. {
  627. CM_EXCEPT(InvalidParametersException,
  628. "Parameter called " + name + " does not exist. ");
  629. }
  630. return 0;
  631. }
  632. else
  633. {
  634. return &(i->second);
  635. }
  636. }
  637. //----------------------------------------------------------------------------
  638. void GpuProgramParameters::setNamedConstant(const String& name, TextureRef val)
  639. {
  640. // look up, and throw an exception if we're not ignoring missing
  641. const GpuConstantDefinition* def =
  642. _findNamedConstantDefinition(name, !mIgnoreMissingParams);
  643. if (def)
  644. mTextures[def->physicalIndex] = val;
  645. }
  646. //-----------------------------------------------------------------------------
  647. void GpuProgramParameters::setNamedConstant(const String& name, float val)
  648. {
  649. // look up, and throw an exception if we're not ignoring missing
  650. const GpuConstantDefinition* def =
  651. _findNamedConstantDefinition(name, !mIgnoreMissingParams);
  652. if (def)
  653. _writeRawConstant(def->physicalIndex, val);
  654. }
  655. //---------------------------------------------------------------------------
  656. void GpuProgramParameters::setNamedConstant(const String& name, int val)
  657. {
  658. // look up, and throw an exception if we're not ignoring missing
  659. const GpuConstantDefinition* def =
  660. _findNamedConstantDefinition(name, !mIgnoreMissingParams);
  661. if (def)
  662. _writeRawConstant(def->physicalIndex, val);
  663. }
  664. //---------------------------------------------------------------------------
  665. void GpuProgramParameters::setNamedConstant(const String& name, const Vector4& vec)
  666. {
  667. // look up, and throw an exception if we're not ignoring missing
  668. const GpuConstantDefinition* def =
  669. _findNamedConstantDefinition(name, !mIgnoreMissingParams);
  670. if (def)
  671. _writeRawConstant(def->physicalIndex, vec, def->elementSize);
  672. }
  673. //---------------------------------------------------------------------------
  674. void GpuProgramParameters::setNamedConstant(const String& name, const Vector3& vec)
  675. {
  676. // look up, and throw an exception if we're not ignoring missing
  677. const GpuConstantDefinition* def =
  678. _findNamedConstantDefinition(name, !mIgnoreMissingParams);
  679. if (def)
  680. _writeRawConstant(def->physicalIndex, vec);
  681. }
  682. //---------------------------------------------------------------------------
  683. void GpuProgramParameters::setNamedConstant(const String& name, const Vector2& vec)
  684. {
  685. // look up, and throw an exception if we're not ignoring missing
  686. const GpuConstantDefinition* def =
  687. _findNamedConstantDefinition(name, !mIgnoreMissingParams);
  688. if (def)
  689. _writeRawConstant(def->physicalIndex, vec);
  690. }
  691. //---------------------------------------------------------------------------
  692. void GpuProgramParameters::setNamedConstant(const String& name, const Matrix4& m)
  693. {
  694. // look up, and throw an exception if we're not ignoring missing
  695. const GpuConstantDefinition* def =
  696. _findNamedConstantDefinition(name, !mIgnoreMissingParams);
  697. if (def)
  698. _writeRawConstant(def->physicalIndex, m, def->elementSize);
  699. }
  700. //---------------------------------------------------------------------------
  701. void GpuProgramParameters::setNamedConstant(const String& name, const Matrix4* m,
  702. size_t numEntries)
  703. {
  704. // look up, and throw an exception if we're not ignoring missing
  705. const GpuConstantDefinition* def =
  706. _findNamedConstantDefinition(name, !mIgnoreMissingParams);
  707. if (def)
  708. _writeRawConstant(def->physicalIndex, m, numEntries);
  709. }
  710. //---------------------------------------------------------------------------
  711. void GpuProgramParameters::setNamedConstant(const String& name, const Matrix3& m)
  712. {
  713. // look up, and throw an exception if we're not ignoring missing
  714. const GpuConstantDefinition* def =
  715. _findNamedConstantDefinition(name, !mIgnoreMissingParams);
  716. if (def)
  717. _writeRawConstant(def->physicalIndex, m, def->elementSize);
  718. }
  719. //---------------------------------------------------------------------------
  720. void GpuProgramParameters::setNamedConstant(const String& name,
  721. const float *val, size_t count, size_t multiple)
  722. {
  723. size_t rawCount = count * multiple;
  724. // look up, and throw an exception if we're not ignoring missing
  725. const GpuConstantDefinition* def =
  726. _findNamedConstantDefinition(name, !mIgnoreMissingParams);
  727. if (def)
  728. _writeRawConstants(def->physicalIndex, val, rawCount);
  729. }
  730. //---------------------------------------------------------------------------
  731. void GpuProgramParameters::setNamedConstant(const String& name,
  732. const double *val, size_t count, size_t multiple)
  733. {
  734. size_t rawCount = count * multiple;
  735. // look up, and throw an exception if we're not ignoring missing
  736. const GpuConstantDefinition* def =
  737. _findNamedConstantDefinition(name, !mIgnoreMissingParams);
  738. if (def)
  739. _writeRawConstants(def->physicalIndex, val, rawCount);
  740. }
  741. //---------------------------------------------------------------------------
  742. void GpuProgramParameters::setNamedConstant(const String& name, const Color& colour)
  743. {
  744. // look up, and throw an exception if we're not ignoring missing
  745. const GpuConstantDefinition* def =
  746. _findNamedConstantDefinition(name, !mIgnoreMissingParams);
  747. if (def)
  748. _writeRawConstant(def->physicalIndex, colour, def->elementSize);
  749. }
  750. //---------------------------------------------------------------------------
  751. void GpuProgramParameters::setNamedConstant(const String& name,
  752. const int *val, size_t count, size_t multiple)
  753. {
  754. size_t rawCount = count * multiple;
  755. // look up, and throw an exception if we're not ignoring missing
  756. const GpuConstantDefinition* def =
  757. _findNamedConstantDefinition(name, !mIgnoreMissingParams);
  758. if (def)
  759. _writeRawConstants(def->physicalIndex, val, rawCount);
  760. }
  761. //---------------------------------------------------------------------------
  762. void GpuProgramParameters::copyConstantsFrom(const GpuProgramParameters& source)
  763. {
  764. // Pull buffers & auto constant list over directly
  765. mFloatConstants = source.getFloatConstantList();
  766. mIntConstants = source.getIntConstantList();
  767. mCombinedVariability = source.mCombinedVariability;
  768. }
  769. //-----------------------------------------------------------------------
  770. void GpuProgramParameters::incPassIterationNumber(void)
  771. {
  772. if (mActivePassIterationIndex != std::numeric_limits<size_t>::max())
  773. {
  774. // This is a physical index
  775. ++mFloatConstants[mActivePassIterationIndex];
  776. }
  777. }
  778. }