CmGpuProgramParams.cpp 30 KB

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