CmGpuProgramParams.cpp 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849
  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. GpuLogicalIndexUse* GpuProgramParameters::_getFloatConstantLogicalIndexUse(
  361. size_t logicalIndex, size_t requestedSize, UINT16 variability)
  362. {
  363. if (mFloatLogicalToPhysical == nullptr)
  364. return 0;
  365. GpuLogicalIndexUse* indexUse = 0;
  366. CM_LOCK_MUTEX(mFloatLogicalToPhysical->mutex)
  367. GpuLogicalIndexUseMap::iterator logi = mFloatLogicalToPhysical->map.find(logicalIndex);
  368. if (logi == mFloatLogicalToPhysical->map.end())
  369. {
  370. if (requestedSize)
  371. {
  372. size_t physicalIndex = mFloatConstants.size();
  373. // Expand at buffer end
  374. mFloatConstants.insert(mFloatConstants.end(), requestedSize, 0.0f);
  375. // Record extended size for future GPU params re-using this information
  376. mFloatLogicalToPhysical->bufferSize = mFloatConstants.size();
  377. // low-level programs will not know about mapping ahead of time, so
  378. // populate it. Other params objects will be able to just use this
  379. // accepted mapping since the constant structure will be the same
  380. // Set up a mapping for all items in the count
  381. size_t currPhys = physicalIndex;
  382. size_t count = requestedSize / 4;
  383. GpuLogicalIndexUseMap::iterator insertedIterator;
  384. for (size_t logicalNum = 0; logicalNum < count; ++logicalNum)
  385. {
  386. GpuLogicalIndexUseMap::iterator it =
  387. mFloatLogicalToPhysical->map.insert(
  388. GpuLogicalIndexUseMap::value_type(
  389. logicalIndex + logicalNum,
  390. GpuLogicalIndexUse(currPhys, requestedSize, variability))).first;
  391. currPhys += 4;
  392. if (logicalNum == 0)
  393. insertedIterator = it;
  394. }
  395. indexUse = &(insertedIterator->second);
  396. }
  397. else
  398. {
  399. // no match & ignore
  400. return 0;
  401. }
  402. }
  403. else
  404. {
  405. size_t physicalIndex = logi->second.physicalIndex;
  406. indexUse = &(logi->second);
  407. // check size
  408. if (logi->second.currentSize < requestedSize)
  409. {
  410. // init buffer entry wasn't big enough; could be a mistake on the part
  411. // of the original use, or perhaps a variable length we can't predict
  412. // until first actual runtime use e.g. world matrix array
  413. size_t insertCount = requestedSize - logi->second.currentSize;
  414. FloatConstantList::iterator insertPos = mFloatConstants.begin();
  415. std::advance(insertPos, physicalIndex);
  416. mFloatConstants.insert(insertPos, insertCount, 0.0f);
  417. // shift all physical positions after this one
  418. for (GpuLogicalIndexUseMap::iterator i = mFloatLogicalToPhysical->map.begin();
  419. i != mFloatLogicalToPhysical->map.end(); ++i)
  420. {
  421. if (i->second.physicalIndex > physicalIndex)
  422. i->second.physicalIndex += insertCount;
  423. }
  424. mFloatLogicalToPhysical->bufferSize += insertCount;
  425. if (mNamedConstants != nullptr)
  426. {
  427. for (GpuConstantDefinitionMap::iterator i = mNamedConstants->map.begin();
  428. i != mNamedConstants->map.end(); ++i)
  429. {
  430. if (i->second.isFloat() && i->second.physicalIndex > physicalIndex)
  431. i->second.physicalIndex += insertCount;
  432. }
  433. mNamedConstants->floatBufferSize += insertCount;
  434. }
  435. logi->second.currentSize += insertCount;
  436. }
  437. }
  438. if (indexUse)
  439. indexUse->variability = variability;
  440. return indexUse;
  441. }
  442. //---------------------------------------------------------------------()
  443. GpuLogicalIndexUse* GpuProgramParameters::_getIntConstantLogicalIndexUse(size_t logicalIndex, size_t requestedSize, UINT16 variability)
  444. {
  445. if (mIntLogicalToPhysical == nullptr)
  446. {
  447. CM_EXCEPT(InvalidParametersException,
  448. "This is not a low-level parameter parameter object");
  449. }
  450. GpuLogicalIndexUse* indexUse = 0;
  451. CM_LOCK_MUTEX(mIntLogicalToPhysical->mutex)
  452. GpuLogicalIndexUseMap::iterator logi = mIntLogicalToPhysical->map.find(logicalIndex);
  453. if (logi == mIntLogicalToPhysical->map.end())
  454. {
  455. if (requestedSize)
  456. {
  457. size_t physicalIndex = mIntConstants.size();
  458. // Expand at buffer end
  459. mIntConstants.insert(mIntConstants.end(), requestedSize, 0);
  460. // Record extended size for future GPU params re-using this information
  461. mIntLogicalToPhysical->bufferSize = mIntConstants.size();
  462. // low-level programs will not know about mapping ahead of time, so
  463. // populate it. Other params objects will be able to just use this
  464. // accepted mapping since the constant structure will be the same
  465. // Set up a mapping for all items in the count
  466. size_t currPhys = physicalIndex;
  467. size_t count = requestedSize / 4;
  468. GpuLogicalIndexUseMap::iterator insertedIterator;
  469. for (size_t logicalNum = 0; logicalNum < count; ++logicalNum)
  470. {
  471. GpuLogicalIndexUseMap::iterator it =
  472. mIntLogicalToPhysical->map.insert(
  473. GpuLogicalIndexUseMap::value_type(
  474. logicalIndex + logicalNum,
  475. GpuLogicalIndexUse(currPhys, requestedSize, variability))).first;
  476. if (logicalNum == 0)
  477. insertedIterator = it;
  478. currPhys += 4;
  479. }
  480. indexUse = &(insertedIterator->second);
  481. }
  482. else
  483. {
  484. // no match
  485. return 0;
  486. }
  487. }
  488. else
  489. {
  490. size_t physicalIndex = logi->second.physicalIndex;
  491. indexUse = &(logi->second);
  492. // check size
  493. if (logi->second.currentSize < requestedSize)
  494. {
  495. // init buffer entry wasn't big enough; could be a mistake on the part
  496. // of the original use, or perhaps a variable length we can't predict
  497. // until first actual runtime use e.g. world matrix array
  498. size_t insertCount = requestedSize - logi->second.currentSize;
  499. IntConstantList::iterator insertPos = mIntConstants.begin();
  500. std::advance(insertPos, physicalIndex);
  501. mIntConstants.insert(insertPos, insertCount, 0);
  502. // shift all physical positions after this one
  503. for (GpuLogicalIndexUseMap::iterator i = mIntLogicalToPhysical->map.begin();
  504. i != mIntLogicalToPhysical->map.end(); ++i)
  505. {
  506. if (i->second.physicalIndex > physicalIndex)
  507. i->second.physicalIndex += insertCount;
  508. }
  509. mIntLogicalToPhysical->bufferSize += insertCount;
  510. if (mNamedConstants != nullptr)
  511. {
  512. for (GpuConstantDefinitionMap::iterator i = mNamedConstants->map.begin();
  513. i != mNamedConstants->map.end(); ++i)
  514. {
  515. if (!i->second.isFloat() && i->second.physicalIndex > physicalIndex)
  516. i->second.physicalIndex += insertCount;
  517. }
  518. mNamedConstants->intBufferSize += insertCount;
  519. }
  520. logi->second.currentSize += insertCount;
  521. }
  522. }
  523. if (indexUse)
  524. indexUse->variability = variability;
  525. return indexUse;
  526. }
  527. //-----------------------------------------------------------------------------
  528. size_t GpuProgramParameters::_getFloatConstantPhysicalIndex(
  529. size_t logicalIndex, size_t requestedSize, UINT16 variability)
  530. {
  531. GpuLogicalIndexUse* indexUse = _getFloatConstantLogicalIndexUse(logicalIndex, requestedSize, variability);
  532. return indexUse ? indexUse->physicalIndex : 0;
  533. }
  534. //-----------------------------------------------------------------------------
  535. size_t GpuProgramParameters::_getIntConstantPhysicalIndex(
  536. size_t logicalIndex, size_t requestedSize, UINT16 variability)
  537. {
  538. GpuLogicalIndexUse* indexUse = _getIntConstantLogicalIndexUse(logicalIndex, requestedSize, variability);
  539. return indexUse ? indexUse->physicalIndex : 0;
  540. }
  541. //-----------------------------------------------------------------------------
  542. size_t GpuProgramParameters::getFloatLogicalIndexForPhysicalIndex(size_t physicalIndex)
  543. {
  544. // perhaps build a reverse map of this sometime (shared in GpuProgram)
  545. for (GpuLogicalIndexUseMap::iterator i = mFloatLogicalToPhysical->map.begin();
  546. i != mFloatLogicalToPhysical->map.end(); ++i)
  547. {
  548. if (i->second.physicalIndex == physicalIndex)
  549. return i->first;
  550. }
  551. return std::numeric_limits<size_t>::max();
  552. }
  553. //-----------------------------------------------------------------------------
  554. size_t GpuProgramParameters::getIntLogicalIndexForPhysicalIndex(size_t physicalIndex)
  555. {
  556. // perhaps build a reverse map of this sometime (shared in GpuProgram)
  557. for (GpuLogicalIndexUseMap::iterator i = mIntLogicalToPhysical->map.begin();
  558. i != mIntLogicalToPhysical->map.end(); ++i)
  559. {
  560. if (i->second.physicalIndex == physicalIndex)
  561. return i->first;
  562. }
  563. return std::numeric_limits<size_t>::max();
  564. }
  565. //-----------------------------------------------------------------------------
  566. GpuConstantDefinitionIterator GpuProgramParameters::getConstantDefinitionIterator(void) const
  567. {
  568. if (mNamedConstants == nullptr)
  569. {
  570. CM_EXCEPT(InvalidParametersException,
  571. "This params object is not based on a program with named parameters.");
  572. }
  573. return mNamedConstants->map.begin();
  574. }
  575. //-----------------------------------------------------------------------------
  576. const GpuNamedConstants& GpuProgramParameters::getConstantDefinitions() const
  577. {
  578. if (mNamedConstants == nullptr)
  579. {
  580. CM_EXCEPT(InvalidParametersException,
  581. "This params object is not based on a program with named parameters.");
  582. }
  583. return *mNamedConstants;
  584. }
  585. //-----------------------------------------------------------------------------
  586. const GpuConstantDefinition& GpuProgramParameters::getConstantDefinition(const String& name) const
  587. {
  588. if (mNamedConstants == nullptr)
  589. {
  590. CM_EXCEPT(InvalidParametersException,
  591. "This params object is not based on a program with named parameters.");
  592. }
  593. // locate, and throw exception if not found
  594. const GpuConstantDefinition* def = _findNamedConstantDefinition(name, true);
  595. return *def;
  596. }
  597. //-----------------------------------------------------------------------------
  598. bool GpuProgramParameters::hasNamedConstant(const String& name) const
  599. {
  600. return _findNamedConstantDefinition(name) != nullptr;
  601. }
  602. //-----------------------------------------------------------------------------
  603. const GpuConstantDefinition*
  604. GpuProgramParameters::_findNamedConstantDefinition(const String& name,
  605. bool throwExceptionIfNotFound) const
  606. {
  607. if (mNamedConstants == nullptr)
  608. {
  609. if (throwExceptionIfNotFound)
  610. {
  611. CM_EXCEPT(InvalidParametersException,
  612. "Named constants have not been initialised, perhaps a compile error.");
  613. }
  614. return 0;
  615. }
  616. GpuConstantDefinitionMap::const_iterator i = mNamedConstants->map.find(name);
  617. if (i == mNamedConstants->map.end())
  618. {
  619. if (throwExceptionIfNotFound)
  620. {
  621. CM_EXCEPT(InvalidParametersException,
  622. "Parameter called " + name + " does not exist. ");
  623. }
  624. return 0;
  625. }
  626. else
  627. {
  628. return &(i->second);
  629. }
  630. }
  631. //----------------------------------------------------------------------------
  632. void GpuProgramParameters::setNamedConstant(const String& name, TextureRef val)
  633. {
  634. // look up, and throw an exception if we're not ignoring missing
  635. const GpuConstantDefinition* def =
  636. _findNamedConstantDefinition(name, !mIgnoreMissingParams);
  637. if (def)
  638. mTextures[def->physicalIndex] = val;
  639. }
  640. //-----------------------------------------------------------------------------
  641. void GpuProgramParameters::setNamedConstant(const String& name, float val)
  642. {
  643. // look up, and throw an exception if we're not ignoring missing
  644. const GpuConstantDefinition* def =
  645. _findNamedConstantDefinition(name, !mIgnoreMissingParams);
  646. if (def)
  647. _writeRawConstant(def->physicalIndex, val);
  648. }
  649. //---------------------------------------------------------------------------
  650. void GpuProgramParameters::setNamedConstant(const String& name, int val)
  651. {
  652. // look up, and throw an exception if we're not ignoring missing
  653. const GpuConstantDefinition* def =
  654. _findNamedConstantDefinition(name, !mIgnoreMissingParams);
  655. if (def)
  656. _writeRawConstant(def->physicalIndex, val);
  657. }
  658. //---------------------------------------------------------------------------
  659. void GpuProgramParameters::setNamedConstant(const String& name, const Vector4& vec)
  660. {
  661. // look up, and throw an exception if we're not ignoring missing
  662. const GpuConstantDefinition* def =
  663. _findNamedConstantDefinition(name, !mIgnoreMissingParams);
  664. if (def)
  665. _writeRawConstant(def->physicalIndex, vec, def->elementSize);
  666. }
  667. //---------------------------------------------------------------------------
  668. void GpuProgramParameters::setNamedConstant(const String& name, const Vector3& vec)
  669. {
  670. // look up, and throw an exception if we're not ignoring missing
  671. const GpuConstantDefinition* def =
  672. _findNamedConstantDefinition(name, !mIgnoreMissingParams);
  673. if (def)
  674. _writeRawConstant(def->physicalIndex, vec);
  675. }
  676. //---------------------------------------------------------------------------
  677. void GpuProgramParameters::setNamedConstant(const String& name, const Vector2& vec)
  678. {
  679. // look up, and throw an exception if we're not ignoring missing
  680. const GpuConstantDefinition* def =
  681. _findNamedConstantDefinition(name, !mIgnoreMissingParams);
  682. if (def)
  683. _writeRawConstant(def->physicalIndex, vec);
  684. }
  685. //---------------------------------------------------------------------------
  686. void GpuProgramParameters::setNamedConstant(const String& name, const Matrix4& m)
  687. {
  688. // look up, and throw an exception if we're not ignoring missing
  689. const GpuConstantDefinition* def =
  690. _findNamedConstantDefinition(name, !mIgnoreMissingParams);
  691. if (def)
  692. _writeRawConstant(def->physicalIndex, m, def->elementSize);
  693. }
  694. //---------------------------------------------------------------------------
  695. void GpuProgramParameters::setNamedConstant(const String& name, const Matrix4* m,
  696. size_t numEntries)
  697. {
  698. // look up, and throw an exception if we're not ignoring missing
  699. const GpuConstantDefinition* def =
  700. _findNamedConstantDefinition(name, !mIgnoreMissingParams);
  701. if (def)
  702. _writeRawConstant(def->physicalIndex, m, numEntries);
  703. }
  704. //---------------------------------------------------------------------------
  705. void GpuProgramParameters::setNamedConstant(const String& name, const Matrix3& m)
  706. {
  707. // look up, and throw an exception if we're not ignoring missing
  708. const GpuConstantDefinition* def =
  709. _findNamedConstantDefinition(name, !mIgnoreMissingParams);
  710. if (def)
  711. _writeRawConstant(def->physicalIndex, m, def->elementSize);
  712. }
  713. //---------------------------------------------------------------------------
  714. void GpuProgramParameters::setNamedConstant(const String& name,
  715. const float *val, size_t count, size_t multiple)
  716. {
  717. size_t rawCount = count * multiple;
  718. // look up, and throw an exception if we're not ignoring missing
  719. const GpuConstantDefinition* def =
  720. _findNamedConstantDefinition(name, !mIgnoreMissingParams);
  721. if (def)
  722. _writeRawConstants(def->physicalIndex, val, rawCount);
  723. }
  724. //---------------------------------------------------------------------------
  725. void GpuProgramParameters::setNamedConstant(const String& name,
  726. const double *val, size_t count, size_t multiple)
  727. {
  728. size_t rawCount = count * multiple;
  729. // look up, and throw an exception if we're not ignoring missing
  730. const GpuConstantDefinition* def =
  731. _findNamedConstantDefinition(name, !mIgnoreMissingParams);
  732. if (def)
  733. _writeRawConstants(def->physicalIndex, val, rawCount);
  734. }
  735. //---------------------------------------------------------------------------
  736. void GpuProgramParameters::setNamedConstant(const String& name, const Color& colour)
  737. {
  738. // look up, and throw an exception if we're not ignoring missing
  739. const GpuConstantDefinition* def =
  740. _findNamedConstantDefinition(name, !mIgnoreMissingParams);
  741. if (def)
  742. _writeRawConstant(def->physicalIndex, colour, def->elementSize);
  743. }
  744. //---------------------------------------------------------------------------
  745. void GpuProgramParameters::setNamedConstant(const String& name,
  746. const int *val, size_t count, size_t multiple)
  747. {
  748. size_t rawCount = count * multiple;
  749. // look up, and throw an exception if we're not ignoring missing
  750. const GpuConstantDefinition* def =
  751. _findNamedConstantDefinition(name, !mIgnoreMissingParams);
  752. if (def)
  753. _writeRawConstants(def->physicalIndex, val, rawCount);
  754. }
  755. //---------------------------------------------------------------------------
  756. void GpuProgramParameters::copyConstantsFrom(const GpuProgramParameters& source)
  757. {
  758. // Pull buffers & auto constant list over directly
  759. mFloatConstants = source.getFloatConstantList();
  760. mIntConstants = source.getIntConstantList();
  761. mCombinedVariability = source.mCombinedVariability;
  762. }
  763. //-----------------------------------------------------------------------
  764. void GpuProgramParameters::incPassIterationNumber(void)
  765. {
  766. if (mActivePassIterationIndex != std::numeric_limits<size_t>::max())
  767. {
  768. // This is a physical index
  769. ++mFloatConstants[mActivePassIterationIndex];
  770. }
  771. }
  772. }