CmGpuProgramParams.cpp 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249
  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 "CmRenderSystemCapabilities.h"
  29. #include "CmException.h"
  30. namespace CamelotEngine
  31. {
  32. bool GpuNamedConstants::msGenerateAllConstantDefinitionArrayEntries = false;
  33. //---------------------------------------------------------------------
  34. void GpuNamedConstants::generateConstantDefinitionArrayEntries(
  35. const String& paramName, const GpuConstantDefinition& baseDef)
  36. {
  37. // Copy definition for use with arrays
  38. GpuConstantDefinition arrayDef = baseDef;
  39. arrayDef.arraySize = 1;
  40. String arrayName;
  41. // Add parameters for array accessors
  42. // [0] will refer to the same location, [1+] will increment
  43. // only populate others individually up to 16 array slots so as not to get out of hand,
  44. // unless the system has been explicitly configured to allow all the parameters to be added
  45. // paramName[0] version will always exist
  46. size_t maxArrayIndex = 1;
  47. if (baseDef.arraySize <= 16 || msGenerateAllConstantDefinitionArrayEntries)
  48. maxArrayIndex = baseDef.arraySize;
  49. for (size_t i = 0; i < maxArrayIndex; i++)
  50. {
  51. arrayName = paramName + "[" + toString(i) + "]";
  52. map.insert(GpuConstantDefinitionMap::value_type(arrayName, arrayDef));
  53. // increment location
  54. arrayDef.physicalIndex += arrayDef.elementSize;
  55. }
  56. // note no increment of buffer sizes since this is shared with main array def
  57. }
  58. //---------------------------------------------------------------------
  59. bool GpuNamedConstants::getGenerateAllConstantDefinitionArrayEntries()
  60. {
  61. return msGenerateAllConstantDefinitionArrayEntries;
  62. }
  63. //---------------------------------------------------------------------
  64. void GpuNamedConstants::setGenerateAllConstantDefinitionArrayEntries(bool generateAll)
  65. {
  66. msGenerateAllConstantDefinitionArrayEntries = generateAll;
  67. }
  68. //-----------------------------------------------------------------------------
  69. // GpuSharedParameters Methods
  70. //-----------------------------------------------------------------------------
  71. GpuSharedParameters::GpuSharedParameters(const String& name)
  72. :mName(name)
  73. // TODO PORT - Commented out because I don't have root in port. But I dont think this will be needed in final version
  74. /*, mFrameLastUpdated(Root::getSingleton().getNextFrameNumber())*/
  75. , mFrameLastUpdated(0)
  76. , mVersion(0)
  77. {
  78. }
  79. //---------------------------------------------------------------------
  80. GpuSharedParameters::~GpuSharedParameters()
  81. {
  82. }
  83. //---------------------------------------------------------------------
  84. void GpuSharedParameters::addConstantDefinition(const String& name, GpuConstantType constType, size_t arraySize)
  85. {
  86. if (mNamedConstants.map.find(name) != mNamedConstants.map.end())
  87. {
  88. CM_EXCEPT(InvalidParametersException,
  89. "Constant entry with name '" + name + "' already exists. ");
  90. }
  91. GpuConstantDefinition def;
  92. def.arraySize = arraySize;
  93. def.constType = constType;
  94. // for compatibility we do not pad values to multiples of 4
  95. // when it comes to arrays, user is responsible for creating matching defs
  96. def.elementSize = GpuConstantDefinition::getElementSize(constType, false);
  97. // not used
  98. def.logicalIndex = 0;
  99. def.variability = (UINT16)GPV_GLOBAL;
  100. if (def.isFloat())
  101. {
  102. def.physicalIndex = mFloatConstants.size();
  103. mFloatConstants.resize(mFloatConstants.size() + def.arraySize * def.elementSize);
  104. }
  105. else
  106. {
  107. def.physicalIndex = mIntConstants.size();
  108. mIntConstants.resize(mIntConstants.size() + def.arraySize * def.elementSize);
  109. }
  110. mNamedConstants.map[name] = def;
  111. ++mVersion;
  112. }
  113. //---------------------------------------------------------------------
  114. void GpuSharedParameters::removeConstantDefinition(const String& name)
  115. {
  116. GpuConstantDefinitionMap::iterator i = mNamedConstants.map.find(name);
  117. if (i != mNamedConstants.map.end())
  118. {
  119. GpuConstantDefinition& def = i->second;
  120. bool isFloat = def.isFloat();
  121. size_t numElems = def.elementSize * def.arraySize;
  122. for (GpuConstantDefinitionMap::iterator j = mNamedConstants.map.begin();
  123. j != mNamedConstants.map.end(); ++j)
  124. {
  125. GpuConstantDefinition& otherDef = j->second;
  126. bool otherIsFloat = otherDef.isFloat();
  127. // same type, and comes after in the buffer
  128. if ( ((isFloat && otherIsFloat) || (!isFloat && !otherIsFloat)) &&
  129. otherDef.physicalIndex > def.physicalIndex)
  130. {
  131. // adjust index
  132. otherDef.physicalIndex -= numElems;
  133. }
  134. }
  135. // remove floats and reduce buffer
  136. if (isFloat)
  137. {
  138. mNamedConstants.floatBufferSize -= numElems;
  139. FloatConstantList::iterator beg = mFloatConstants.begin();
  140. std::advance(beg, def.physicalIndex);
  141. FloatConstantList::iterator en = beg;
  142. std::advance(en, numElems);
  143. mFloatConstants.erase(beg, en);
  144. }
  145. else
  146. {
  147. mNamedConstants.intBufferSize -= numElems;
  148. IntConstantList::iterator beg = mIntConstants.begin();
  149. std::advance(beg, def.physicalIndex);
  150. IntConstantList::iterator en = beg;
  151. std::advance(en, numElems);
  152. mIntConstants.erase(beg, en);
  153. }
  154. ++mVersion;
  155. }
  156. }
  157. //---------------------------------------------------------------------
  158. void GpuSharedParameters::removeAllConstantDefinitions()
  159. {
  160. mNamedConstants.map.clear();
  161. mNamedConstants.floatBufferSize = 0;
  162. mNamedConstants.intBufferSize = 0;
  163. mFloatConstants.clear();
  164. mIntConstants.clear();
  165. }
  166. //---------------------------------------------------------------------
  167. GpuConstantDefinitionIterator GpuSharedParameters::getConstantDefinitionIterator(void) const
  168. {
  169. return mNamedConstants.map.begin();
  170. }
  171. //---------------------------------------------------------------------
  172. const GpuConstantDefinition& GpuSharedParameters::getConstantDefinition(const String& name) const
  173. {
  174. GpuConstantDefinitionMap::const_iterator i = mNamedConstants.map.find(name);
  175. if (i == mNamedConstants.map.end())
  176. {
  177. CM_EXCEPT(InvalidParametersException,
  178. "Constant entry with name '" + name + "' does not exist. ");
  179. }
  180. return i->second;
  181. }
  182. //---------------------------------------------------------------------
  183. const GpuNamedConstants& GpuSharedParameters::getConstantDefinitions() const
  184. {
  185. return mNamedConstants;
  186. }
  187. //---------------------------------------------------------------------
  188. void GpuSharedParameters::setNamedConstant(const String& name, float val)
  189. {
  190. setNamedConstant(name, &val, 1);
  191. }
  192. //---------------------------------------------------------------------
  193. void GpuSharedParameters::setNamedConstant(const String& name, int val)
  194. {
  195. setNamedConstant(name, &val, 1);
  196. }
  197. //---------------------------------------------------------------------
  198. void GpuSharedParameters::setNamedConstant(const String& name, const Vector4& vec)
  199. {
  200. setNamedConstant(name, vec.ptr(), 4);
  201. }
  202. //---------------------------------------------------------------------
  203. void GpuSharedParameters::setNamedConstant(const String& name, const Vector3& vec)
  204. {
  205. setNamedConstant(name, vec.ptr(), 3);
  206. }
  207. //---------------------------------------------------------------------
  208. void GpuSharedParameters::setNamedConstant(const String& name, const Matrix4& m)
  209. {
  210. setNamedConstant(name, m[0], 16);
  211. }
  212. //---------------------------------------------------------------------
  213. void GpuSharedParameters::setNamedConstant(const String& name, const Matrix4* m, size_t numEntries)
  214. {
  215. setNamedConstant(name, m[0][0], 16 * numEntries);
  216. }
  217. //---------------------------------------------------------------------
  218. void GpuSharedParameters::setNamedConstant(const String& name, const float *val, size_t count)
  219. {
  220. GpuConstantDefinitionMap::const_iterator i = mNamedConstants.map.find(name);
  221. if (i != mNamedConstants.map.end())
  222. {
  223. const GpuConstantDefinition& def = i->second;
  224. memcpy(&mFloatConstants[def.physicalIndex], val,
  225. sizeof(float) * std::min(count, def.elementSize * def.arraySize));
  226. }
  227. _markDirty();
  228. }
  229. //---------------------------------------------------------------------
  230. void GpuSharedParameters::setNamedConstant(const String& name, const double *val, size_t count)
  231. {
  232. GpuConstantDefinitionMap::const_iterator i = mNamedConstants.map.find(name);
  233. if (i != mNamedConstants.map.end())
  234. {
  235. const GpuConstantDefinition& def = i->second;
  236. count = std::min(count, def.elementSize * def.arraySize);
  237. const double* src = val;
  238. float* dst = &mFloatConstants[def.physicalIndex];
  239. for (size_t v = 0; v < count; ++v)
  240. {
  241. *dst++ = static_cast<float>(*src++);
  242. }
  243. }
  244. _markDirty();
  245. }
  246. //---------------------------------------------------------------------
  247. void GpuSharedParameters::setNamedConstant(const String& name, const Color& colour)
  248. {
  249. setNamedConstant(name, colour.ptr(), 4);
  250. }
  251. //---------------------------------------------------------------------
  252. void GpuSharedParameters::setNamedConstant(const String& name, const int *val, size_t count)
  253. {
  254. GpuConstantDefinitionMap::const_iterator i = mNamedConstants.map.find(name);
  255. if (i != mNamedConstants.map.end())
  256. {
  257. const GpuConstantDefinition& def = i->second;
  258. memcpy(&mIntConstants[def.physicalIndex], val,
  259. sizeof(int) * std::min(count, def.elementSize * def.arraySize));
  260. }
  261. _markDirty();
  262. }
  263. //---------------------------------------------------------------------
  264. void GpuSharedParameters::_markDirty()
  265. {
  266. // TODO PORT - Don't have access to Root in port. Set it to 0 for now. Will probably be able to remove this completely
  267. mFrameLastUpdated = 0;
  268. /*mFrameLastUpdated = Root::getSingleton().getNextFrameNumber();*/
  269. }
  270. //-----------------------------------------------------------------------------
  271. // GpuSharedParametersUsage Methods
  272. //-----------------------------------------------------------------------------
  273. GpuSharedParametersUsage::GpuSharedParametersUsage(GpuSharedParametersPtr sharedParams,
  274. GpuProgramParameters* params)
  275. : mSharedParams(sharedParams)
  276. , mParams(params)
  277. {
  278. initCopyData();
  279. }
  280. //---------------------------------------------------------------------
  281. void GpuSharedParametersUsage::initCopyData()
  282. {
  283. mCopyDataList.clear();
  284. const GpuConstantDefinitionMap& sharedmap = mSharedParams->getConstantDefinitions().map;
  285. for (GpuConstantDefinitionMap::const_iterator i = sharedmap.begin(); i != sharedmap.end(); ++i)
  286. {
  287. const String& pName = i->first;
  288. const GpuConstantDefinition& shareddef = i->second;
  289. const GpuConstantDefinition* instdef = mParams->_findNamedConstantDefinition(pName, false);
  290. if (instdef)
  291. {
  292. // Check that the definitions are the same
  293. if (instdef->constType == shareddef.constType &&
  294. instdef->arraySize == shareddef.arraySize)
  295. {
  296. CopyDataEntry e;
  297. e.srcDefinition = &shareddef;
  298. e.dstDefinition = instdef;
  299. mCopyDataList.push_back(e);
  300. }
  301. }
  302. }
  303. mCopyDataVersion = mSharedParams->getVersion();
  304. }
  305. //---------------------------------------------------------------------
  306. void GpuSharedParametersUsage::_copySharedParamsToTargetParams()
  307. {
  308. // check copy data version
  309. if (mCopyDataVersion != mSharedParams->getVersion())
  310. initCopyData();
  311. for (CopyDataList::iterator i = mCopyDataList.begin(); i != mCopyDataList.end(); ++i)
  312. {
  313. CopyDataEntry& e = *i;
  314. if (e.dstDefinition->isFloat())
  315. {
  316. const float* pSrc = mSharedParams->getFloatPointer(e.srcDefinition->physicalIndex);
  317. float* pDst = mParams->getFloatPointer(e.dstDefinition->physicalIndex);
  318. // Deal with matrix transposition here!!!
  319. // transposition is specific to the dest param set, shared params don't do it
  320. if (mParams->getTransposeMatrices() && e.dstDefinition->constType == GCT_MATRIX_4X4)
  321. {
  322. for (int row = 0; row < 4; ++row)
  323. for (int col = 0; col < 4; ++col)
  324. pDst[row * 4 + col] = pSrc[col * 4 + row];
  325. }
  326. else
  327. {
  328. if (e.dstDefinition->elementSize == e.srcDefinition->elementSize)
  329. {
  330. // simple copy
  331. memcpy(pDst, pSrc, sizeof(float) * e.dstDefinition->elementSize * e.dstDefinition->arraySize);
  332. }
  333. else
  334. {
  335. // target params may be padded to 4 elements, shared params are packed
  336. assert(e.dstDefinition->elementSize % 4 == 0);
  337. size_t iterations = e.dstDefinition->elementSize / 4
  338. * e.dstDefinition->arraySize;
  339. size_t valsPerIteration = e.srcDefinition->elementSize / iterations;
  340. for (size_t l = 0; l < iterations; ++l)
  341. {
  342. memcpy(pDst, pSrc, sizeof(float) * valsPerIteration);
  343. pSrc += valsPerIteration;
  344. pDst += 4;
  345. }
  346. }
  347. }
  348. }
  349. else
  350. {
  351. const int* pSrc = mSharedParams->getIntPointer(e.srcDefinition->physicalIndex);
  352. int* pDst = mParams->getIntPointer(e.dstDefinition->physicalIndex);
  353. if (e.dstDefinition->elementSize == e.srcDefinition->elementSize)
  354. {
  355. // simple copy
  356. memcpy(pDst, pSrc, sizeof(int) * e.dstDefinition->elementSize * e.dstDefinition->arraySize);
  357. }
  358. else
  359. {
  360. // target params may be padded to 4 elements, shared params are packed
  361. assert(e.dstDefinition->elementSize % 4 == 0);
  362. size_t iterations = (e.dstDefinition->elementSize / 4)
  363. * e.dstDefinition->arraySize;
  364. size_t valsPerIteration = e.srcDefinition->elementSize / iterations;
  365. for (size_t l = 0; l < iterations; ++l)
  366. {
  367. memcpy(pDst, pSrc, sizeof(int) * valsPerIteration);
  368. pSrc += valsPerIteration;
  369. pDst += 4;
  370. }
  371. }
  372. }
  373. }
  374. }
  375. //-----------------------------------------------------------------------------
  376. // GpuProgramParameters Methods
  377. //-----------------------------------------------------------------------------
  378. GpuProgramParameters::GpuProgramParameters() :
  379. mCombinedVariability(GPV_GLOBAL)
  380. , mTransposeMatrices(false)
  381. , mIgnoreMissingParams(false)
  382. , mActivePassIterationIndex(std::numeric_limits<size_t>::max())
  383. {
  384. }
  385. //-----------------------------------------------------------------------------
  386. GpuProgramParameters::GpuProgramParameters(const GpuProgramParameters& oth)
  387. {
  388. *this = oth;
  389. }
  390. //-----------------------------------------------------------------------------
  391. GpuProgramParameters& GpuProgramParameters::operator=(const GpuProgramParameters& oth)
  392. {
  393. // let compiler perform shallow copies of structures
  394. // AutoConstantEntry, RealConstantEntry, IntConstantEntry
  395. mFloatConstants = oth.mFloatConstants;
  396. mIntConstants = oth.mIntConstants;
  397. mFloatLogicalToPhysical = oth.mFloatLogicalToPhysical;
  398. mIntLogicalToPhysical = oth.mIntLogicalToPhysical;
  399. mNamedConstants = oth.mNamedConstants;
  400. copySharedParamSetUsage(oth.mSharedParamSets);
  401. mCombinedVariability = oth.mCombinedVariability;
  402. mTransposeMatrices = oth.mTransposeMatrices;
  403. mIgnoreMissingParams = oth.mIgnoreMissingParams;
  404. mActivePassIterationIndex = oth.mActivePassIterationIndex;
  405. return *this;
  406. }
  407. //---------------------------------------------------------------------
  408. void GpuProgramParameters::copySharedParamSetUsage(const GpuSharedParamUsageList& srcList)
  409. {
  410. mSharedParamSets.clear();
  411. for (GpuSharedParamUsageList::const_iterator i = srcList.begin(); i != srcList.end(); ++i)
  412. {
  413. mSharedParamSets.push_back(GpuSharedParametersUsage(i->getSharedParams(), this));
  414. }
  415. }
  416. //---------------------------------------------------------------------
  417. void GpuProgramParameters::_setNamedConstants(
  418. const GpuNamedConstantsPtr& namedConstants)
  419. {
  420. mNamedConstants = namedConstants;
  421. // Determine any extension to local buffers
  422. // Size and reset buffer (fill with zero to make comparison later ok)
  423. if (namedConstants->floatBufferSize > mFloatConstants.size())
  424. {
  425. mFloatConstants.insert(mFloatConstants.end(),
  426. namedConstants->floatBufferSize - mFloatConstants.size(), 0.0f);
  427. }
  428. if (namedConstants->intBufferSize > mIntConstants.size())
  429. {
  430. mIntConstants.insert(mIntConstants.end(),
  431. namedConstants->intBufferSize - mIntConstants.size(), 0);
  432. }
  433. }
  434. //---------------------------------------------------------------------
  435. void GpuProgramParameters::_setLogicalIndexes(
  436. const GpuLogicalBufferStructPtr& floatIndexMap,
  437. const GpuLogicalBufferStructPtr& intIndexMap)
  438. {
  439. mFloatLogicalToPhysical = floatIndexMap;
  440. mIntLogicalToPhysical = intIndexMap;
  441. // resize the internal buffers
  442. // Note that these will only contain something after the first parameter
  443. // set has set some parameters
  444. // Size and reset buffer (fill with zero to make comparison later ok)
  445. if ((floatIndexMap != nullptr) && floatIndexMap->bufferSize > mFloatConstants.size())
  446. {
  447. mFloatConstants.insert(mFloatConstants.end(),
  448. floatIndexMap->bufferSize - mFloatConstants.size(), 0.0f);
  449. }
  450. if ((intIndexMap != nullptr) && intIndexMap->bufferSize > mIntConstants.size())
  451. {
  452. mIntConstants.insert(mIntConstants.end(),
  453. intIndexMap->bufferSize - mIntConstants.size(), 0);
  454. }
  455. }
  456. //---------------------------------------------------------------------()
  457. void GpuProgramParameters::setConstant(size_t index, const Vector4& vec)
  458. {
  459. setConstant(index, vec.ptr(), 1);
  460. }
  461. //-----------------------------------------------------------------------------
  462. void GpuProgramParameters::setConstant(size_t index, float val)
  463. {
  464. setConstant(index, Vector4(val, 0.0f, 0.0f, 0.0f));
  465. }
  466. //-----------------------------------------------------------------------------
  467. void GpuProgramParameters::setConstant(size_t index, const Vector3& vec)
  468. {
  469. setConstant(index, Vector4(vec.x, vec.y, vec.z, 1.0f));
  470. }
  471. //-----------------------------------------------------------------------------
  472. void GpuProgramParameters::setConstant(size_t index, const Matrix4& m)
  473. {
  474. // set as 4x 4-element floats
  475. if (mTransposeMatrices)
  476. {
  477. Matrix4 t = m.transpose();
  478. GpuProgramParameters::setConstant(index, t[0], 4);
  479. }
  480. else
  481. {
  482. GpuProgramParameters::setConstant(index, m[0], 4);
  483. }
  484. }
  485. //-----------------------------------------------------------------------------
  486. void GpuProgramParameters::setConstant(size_t index, const Matrix4* pMatrix,
  487. size_t numEntries)
  488. {
  489. if (mTransposeMatrices)
  490. {
  491. for (size_t i = 0; i < numEntries; ++i)
  492. {
  493. Matrix4 t = pMatrix[i].transpose();
  494. GpuProgramParameters::setConstant(index, t[0], 4);
  495. index += 4;
  496. }
  497. }
  498. else
  499. {
  500. GpuProgramParameters::setConstant(index, pMatrix[0][0], 4 * numEntries);
  501. }
  502. }
  503. //-----------------------------------------------------------------------------
  504. void GpuProgramParameters::setConstant(size_t index, const Color& colour)
  505. {
  506. setConstant(index, colour.ptr(), 1);
  507. }
  508. //-----------------------------------------------------------------------------
  509. void GpuProgramParameters::setConstant(size_t index, const float *val, size_t count)
  510. {
  511. // Raw buffer size is 4x count
  512. size_t rawCount = count * 4;
  513. // get physical index
  514. assert((mFloatLogicalToPhysical != nullptr) && "GpuProgram hasn't set up the logical -> physical map!");
  515. size_t physicalIndex = _getFloatConstantPhysicalIndex(index, rawCount, GPV_GLOBAL);
  516. // Copy
  517. _writeRawConstants(physicalIndex, val, rawCount);
  518. }
  519. //-----------------------------------------------------------------------------
  520. void GpuProgramParameters::setConstant(size_t index, const double *val, size_t count)
  521. {
  522. // Raw buffer size is 4x count
  523. size_t rawCount = count * 4;
  524. // get physical index
  525. assert((mFloatLogicalToPhysical != nullptr) && "GpuProgram hasn't set up the logical -> physical map!");
  526. size_t physicalIndex = _getFloatConstantPhysicalIndex(index, rawCount, GPV_GLOBAL);
  527. assert(physicalIndex + rawCount <= mFloatConstants.size());
  528. // Copy manually since cast required
  529. for (size_t i = 0; i < rawCount; ++i)
  530. {
  531. mFloatConstants[physicalIndex + i] =
  532. static_cast<float>(val[i]);
  533. }
  534. }
  535. //-----------------------------------------------------------------------------
  536. void GpuProgramParameters::setConstant(size_t index, const int *val, size_t count)
  537. {
  538. // Raw buffer size is 4x count
  539. size_t rawCount = count * 4;
  540. // get physical index
  541. assert((mIntLogicalToPhysical != nullptr) && "GpuProgram hasn't set up the logical -> physical map!");
  542. size_t physicalIndex = _getIntConstantPhysicalIndex(index, rawCount, GPV_GLOBAL);
  543. // Copy
  544. _writeRawConstants(physicalIndex, val, rawCount);
  545. }
  546. //-----------------------------------------------------------------------------
  547. void GpuProgramParameters::_writeRawConstant(size_t physicalIndex, const Vector4& vec,
  548. size_t count)
  549. {
  550. // remember, raw content access uses raw float count rather than float4
  551. // write either the number requested (for packed types) or up to 4
  552. _writeRawConstants(physicalIndex, vec.ptr(), std::min(count, (size_t)4));
  553. }
  554. //-----------------------------------------------------------------------------
  555. void GpuProgramParameters::_writeRawConstant(size_t physicalIndex, float val)
  556. {
  557. _writeRawConstants(physicalIndex, &val, 1);
  558. }
  559. //-----------------------------------------------------------------------------
  560. void GpuProgramParameters::_writeRawConstant(size_t physicalIndex, int val)
  561. {
  562. _writeRawConstants(physicalIndex, &val, 1);
  563. }
  564. //-----------------------------------------------------------------------------
  565. void GpuProgramParameters::_writeRawConstant(size_t physicalIndex, const Vector3& vec)
  566. {
  567. _writeRawConstants(physicalIndex, vec.ptr(), 3);
  568. }
  569. //-----------------------------------------------------------------------------
  570. void GpuProgramParameters::_writeRawConstant(size_t physicalIndex, const Matrix4& m,size_t elementCount)
  571. {
  572. // remember, raw content access uses raw float count rather than float4
  573. if (mTransposeMatrices)
  574. {
  575. Matrix4 t = m.transpose();
  576. _writeRawConstants(physicalIndex, t[0], elementCount>16?16:elementCount);
  577. }
  578. else
  579. {
  580. _writeRawConstants(physicalIndex, m[0], elementCount>16?16:elementCount);
  581. }
  582. }
  583. //-----------------------------------------------------------------------------
  584. void GpuProgramParameters::_writeRawConstant(size_t physicalIndex, const Matrix4* pMatrix, size_t numEntries)
  585. {
  586. // remember, raw content access uses raw float count rather than float4
  587. if (mTransposeMatrices)
  588. {
  589. for (size_t i = 0; i < numEntries; ++i)
  590. {
  591. Matrix4 t = pMatrix[i].transpose();
  592. _writeRawConstants(physicalIndex, t[0], 16);
  593. physicalIndex += 16;
  594. }
  595. }
  596. else
  597. {
  598. _writeRawConstants(physicalIndex, pMatrix[0][0], 16 * numEntries);
  599. }
  600. }
  601. //-----------------------------------------------------------------------------
  602. void GpuProgramParameters::_writeRawConstant(size_t physicalIndex,
  603. const Color& colour, size_t count)
  604. {
  605. // write either the number requested (for packed types) or up to 4
  606. _writeRawConstants(physicalIndex, colour.ptr(), std::min(count, (size_t)4));
  607. }
  608. //-----------------------------------------------------------------------------
  609. void GpuProgramParameters::_writeRawConstants(size_t physicalIndex, const double* val, size_t count)
  610. {
  611. assert(physicalIndex + count <= mFloatConstants.size());
  612. for (size_t i = 0; i < count; ++i)
  613. {
  614. mFloatConstants[physicalIndex+i] = static_cast<float>(val[i]);
  615. }
  616. }
  617. //-----------------------------------------------------------------------------
  618. void GpuProgramParameters::_writeRawConstants(size_t physicalIndex, const float* val, size_t count)
  619. {
  620. assert(physicalIndex + count <= mFloatConstants.size());
  621. memcpy(&mFloatConstants[physicalIndex], val, sizeof(float) * count);
  622. }
  623. //-----------------------------------------------------------------------------
  624. void GpuProgramParameters::_writeRawConstants(size_t physicalIndex, const int* val, size_t count)
  625. {
  626. assert(physicalIndex + count <= mIntConstants.size());
  627. memcpy(&mIntConstants[physicalIndex], val, sizeof(int) * count);
  628. }
  629. //-----------------------------------------------------------------------------
  630. void GpuProgramParameters::_readRawConstants(size_t physicalIndex, size_t count, float* dest)
  631. {
  632. assert(physicalIndex + count <= mFloatConstants.size());
  633. memcpy(dest, &mFloatConstants[physicalIndex], sizeof(float) * count);
  634. }
  635. //-----------------------------------------------------------------------------
  636. void GpuProgramParameters::_readRawConstants(size_t physicalIndex, size_t count, int* dest)
  637. {
  638. assert(physicalIndex + count <= mIntConstants.size());
  639. memcpy(dest, &mIntConstants[physicalIndex], sizeof(int) * count);
  640. }
  641. //---------------------------------------------------------------------
  642. GpuLogicalIndexUse* GpuProgramParameters::_getFloatConstantLogicalIndexUse(
  643. size_t logicalIndex, size_t requestedSize, UINT16 variability)
  644. {
  645. if (mFloatLogicalToPhysical == nullptr)
  646. return 0;
  647. GpuLogicalIndexUse* indexUse = 0;
  648. CM_LOCK_MUTEX(mFloatLogicalToPhysical->mutex)
  649. GpuLogicalIndexUseMap::iterator logi = mFloatLogicalToPhysical->map.find(logicalIndex);
  650. if (logi == mFloatLogicalToPhysical->map.end())
  651. {
  652. if (requestedSize)
  653. {
  654. size_t physicalIndex = mFloatConstants.size();
  655. // Expand at buffer end
  656. mFloatConstants.insert(mFloatConstants.end(), requestedSize, 0.0f);
  657. // Record extended size for future GPU params re-using this information
  658. mFloatLogicalToPhysical->bufferSize = mFloatConstants.size();
  659. // low-level programs will not know about mapping ahead of time, so
  660. // populate it. Other params objects will be able to just use this
  661. // accepted mapping since the constant structure will be the same
  662. // Set up a mapping for all items in the count
  663. size_t currPhys = physicalIndex;
  664. size_t count = requestedSize / 4;
  665. GpuLogicalIndexUseMap::iterator insertedIterator;
  666. for (size_t logicalNum = 0; logicalNum < count; ++logicalNum)
  667. {
  668. GpuLogicalIndexUseMap::iterator it =
  669. mFloatLogicalToPhysical->map.insert(
  670. GpuLogicalIndexUseMap::value_type(
  671. logicalIndex + logicalNum,
  672. GpuLogicalIndexUse(currPhys, requestedSize, variability))).first;
  673. currPhys += 4;
  674. if (logicalNum == 0)
  675. insertedIterator = it;
  676. }
  677. indexUse = &(insertedIterator->second);
  678. }
  679. else
  680. {
  681. // no match & ignore
  682. return 0;
  683. }
  684. }
  685. else
  686. {
  687. size_t physicalIndex = logi->second.physicalIndex;
  688. indexUse = &(logi->second);
  689. // check size
  690. if (logi->second.currentSize < requestedSize)
  691. {
  692. // init buffer entry wasn't big enough; could be a mistake on the part
  693. // of the original use, or perhaps a variable length we can't predict
  694. // until first actual runtime use e.g. world matrix array
  695. size_t insertCount = requestedSize - logi->second.currentSize;
  696. FloatConstantList::iterator insertPos = mFloatConstants.begin();
  697. std::advance(insertPos, physicalIndex);
  698. mFloatConstants.insert(insertPos, insertCount, 0.0f);
  699. // shift all physical positions after this one
  700. for (GpuLogicalIndexUseMap::iterator i = mFloatLogicalToPhysical->map.begin();
  701. i != mFloatLogicalToPhysical->map.end(); ++i)
  702. {
  703. if (i->second.physicalIndex > physicalIndex)
  704. i->second.physicalIndex += insertCount;
  705. }
  706. mFloatLogicalToPhysical->bufferSize += insertCount;
  707. if (mNamedConstants != nullptr)
  708. {
  709. for (GpuConstantDefinitionMap::iterator i = mNamedConstants->map.begin();
  710. i != mNamedConstants->map.end(); ++i)
  711. {
  712. if (i->second.isFloat() && i->second.physicalIndex > physicalIndex)
  713. i->second.physicalIndex += insertCount;
  714. }
  715. mNamedConstants->floatBufferSize += insertCount;
  716. }
  717. logi->second.currentSize += insertCount;
  718. }
  719. }
  720. if (indexUse)
  721. indexUse->variability = variability;
  722. return indexUse;
  723. }
  724. //---------------------------------------------------------------------()
  725. GpuLogicalIndexUse* GpuProgramParameters::_getIntConstantLogicalIndexUse(size_t logicalIndex, size_t requestedSize, UINT16 variability)
  726. {
  727. if (mIntLogicalToPhysical == nullptr)
  728. {
  729. CM_EXCEPT(InvalidParametersException,
  730. "This is not a low-level parameter parameter object");
  731. }
  732. GpuLogicalIndexUse* indexUse = 0;
  733. CM_LOCK_MUTEX(mIntLogicalToPhysical->mutex)
  734. GpuLogicalIndexUseMap::iterator logi = mIntLogicalToPhysical->map.find(logicalIndex);
  735. if (logi == mIntLogicalToPhysical->map.end())
  736. {
  737. if (requestedSize)
  738. {
  739. size_t physicalIndex = mIntConstants.size();
  740. // Expand at buffer end
  741. mIntConstants.insert(mIntConstants.end(), requestedSize, 0);
  742. // Record extended size for future GPU params re-using this information
  743. mIntLogicalToPhysical->bufferSize = mIntConstants.size();
  744. // low-level programs will not know about mapping ahead of time, so
  745. // populate it. Other params objects will be able to just use this
  746. // accepted mapping since the constant structure will be the same
  747. // Set up a mapping for all items in the count
  748. size_t currPhys = physicalIndex;
  749. size_t count = requestedSize / 4;
  750. GpuLogicalIndexUseMap::iterator insertedIterator;
  751. for (size_t logicalNum = 0; logicalNum < count; ++logicalNum)
  752. {
  753. GpuLogicalIndexUseMap::iterator it =
  754. mIntLogicalToPhysical->map.insert(
  755. GpuLogicalIndexUseMap::value_type(
  756. logicalIndex + logicalNum,
  757. GpuLogicalIndexUse(currPhys, requestedSize, variability))).first;
  758. if (logicalNum == 0)
  759. insertedIterator = it;
  760. currPhys += 4;
  761. }
  762. indexUse = &(insertedIterator->second);
  763. }
  764. else
  765. {
  766. // no match
  767. return 0;
  768. }
  769. }
  770. else
  771. {
  772. size_t physicalIndex = logi->second.physicalIndex;
  773. indexUse = &(logi->second);
  774. // check size
  775. if (logi->second.currentSize < requestedSize)
  776. {
  777. // init buffer entry wasn't big enough; could be a mistake on the part
  778. // of the original use, or perhaps a variable length we can't predict
  779. // until first actual runtime use e.g. world matrix array
  780. size_t insertCount = requestedSize - logi->second.currentSize;
  781. IntConstantList::iterator insertPos = mIntConstants.begin();
  782. std::advance(insertPos, physicalIndex);
  783. mIntConstants.insert(insertPos, insertCount, 0);
  784. // shift all physical positions after this one
  785. for (GpuLogicalIndexUseMap::iterator i = mIntLogicalToPhysical->map.begin();
  786. i != mIntLogicalToPhysical->map.end(); ++i)
  787. {
  788. if (i->second.physicalIndex > physicalIndex)
  789. i->second.physicalIndex += insertCount;
  790. }
  791. mIntLogicalToPhysical->bufferSize += insertCount;
  792. if (mNamedConstants != nullptr)
  793. {
  794. for (GpuConstantDefinitionMap::iterator i = mNamedConstants->map.begin();
  795. i != mNamedConstants->map.end(); ++i)
  796. {
  797. if (!i->second.isFloat() && i->second.physicalIndex > physicalIndex)
  798. i->second.physicalIndex += insertCount;
  799. }
  800. mNamedConstants->intBufferSize += insertCount;
  801. }
  802. logi->second.currentSize += insertCount;
  803. }
  804. }
  805. if (indexUse)
  806. indexUse->variability = variability;
  807. return indexUse;
  808. }
  809. //-----------------------------------------------------------------------------
  810. size_t GpuProgramParameters::_getFloatConstantPhysicalIndex(
  811. size_t logicalIndex, size_t requestedSize, UINT16 variability)
  812. {
  813. GpuLogicalIndexUse* indexUse = _getFloatConstantLogicalIndexUse(logicalIndex, requestedSize, variability);
  814. return indexUse ? indexUse->physicalIndex : 0;
  815. }
  816. //-----------------------------------------------------------------------------
  817. size_t GpuProgramParameters::_getIntConstantPhysicalIndex(
  818. size_t logicalIndex, size_t requestedSize, UINT16 variability)
  819. {
  820. GpuLogicalIndexUse* indexUse = _getIntConstantLogicalIndexUse(logicalIndex, requestedSize, variability);
  821. return indexUse ? indexUse->physicalIndex : 0;
  822. }
  823. //-----------------------------------------------------------------------------
  824. size_t GpuProgramParameters::getFloatLogicalIndexForPhysicalIndex(size_t physicalIndex)
  825. {
  826. // perhaps build a reverse map of this sometime (shared in GpuProgram)
  827. for (GpuLogicalIndexUseMap::iterator i = mFloatLogicalToPhysical->map.begin();
  828. i != mFloatLogicalToPhysical->map.end(); ++i)
  829. {
  830. if (i->second.physicalIndex == physicalIndex)
  831. return i->first;
  832. }
  833. return std::numeric_limits<size_t>::max();
  834. }
  835. //-----------------------------------------------------------------------------
  836. size_t GpuProgramParameters::getIntLogicalIndexForPhysicalIndex(size_t physicalIndex)
  837. {
  838. // perhaps build a reverse map of this sometime (shared in GpuProgram)
  839. for (GpuLogicalIndexUseMap::iterator i = mIntLogicalToPhysical->map.begin();
  840. i != mIntLogicalToPhysical->map.end(); ++i)
  841. {
  842. if (i->second.physicalIndex == physicalIndex)
  843. return i->first;
  844. }
  845. return std::numeric_limits<size_t>::max();
  846. }
  847. //-----------------------------------------------------------------------------
  848. GpuConstantDefinitionIterator GpuProgramParameters::getConstantDefinitionIterator(void) const
  849. {
  850. if (mNamedConstants == nullptr)
  851. {
  852. CM_EXCEPT(InvalidParametersException,
  853. "This params object is not based on a program with named parameters.");
  854. }
  855. return mNamedConstants->map.begin();
  856. }
  857. //-----------------------------------------------------------------------------
  858. const GpuNamedConstants& GpuProgramParameters::getConstantDefinitions() const
  859. {
  860. if (mNamedConstants == nullptr)
  861. {
  862. CM_EXCEPT(InvalidParametersException,
  863. "This params object is not based on a program with named parameters.");
  864. }
  865. return *mNamedConstants;
  866. }
  867. //-----------------------------------------------------------------------------
  868. const GpuConstantDefinition& GpuProgramParameters::getConstantDefinition(const String& name) const
  869. {
  870. if (mNamedConstants == nullptr)
  871. {
  872. CM_EXCEPT(InvalidParametersException,
  873. "This params object is not based on a program with named parameters.");
  874. }
  875. // locate, and throw exception if not found
  876. const GpuConstantDefinition* def = _findNamedConstantDefinition(name, true);
  877. return *def;
  878. }
  879. //-----------------------------------------------------------------------------
  880. const GpuConstantDefinition*
  881. GpuProgramParameters::_findNamedConstantDefinition(const String& name,
  882. bool throwExceptionIfNotFound) const
  883. {
  884. if (mNamedConstants == nullptr)
  885. {
  886. if (throwExceptionIfNotFound)
  887. {
  888. CM_EXCEPT(InvalidParametersException,
  889. "Named constants have not been initialised, perhaps a compile error.");
  890. }
  891. return 0;
  892. }
  893. GpuConstantDefinitionMap::const_iterator i = mNamedConstants->map.find(name);
  894. if (i == mNamedConstants->map.end())
  895. {
  896. if (throwExceptionIfNotFound)
  897. {
  898. CM_EXCEPT(InvalidParametersException,
  899. "Parameter called " + name + " does not exist. ");
  900. }
  901. return 0;
  902. }
  903. else
  904. {
  905. return &(i->second);
  906. }
  907. }
  908. //-----------------------------------------------------------------------------
  909. void GpuProgramParameters::setNamedConstant(const String& name, float val)
  910. {
  911. // look up, and throw an exception if we're not ignoring missing
  912. const GpuConstantDefinition* def =
  913. _findNamedConstantDefinition(name, !mIgnoreMissingParams);
  914. if (def)
  915. _writeRawConstant(def->physicalIndex, val);
  916. }
  917. //---------------------------------------------------------------------------
  918. void GpuProgramParameters::setNamedConstant(const String& name, int val)
  919. {
  920. // look up, and throw an exception if we're not ignoring missing
  921. const GpuConstantDefinition* def =
  922. _findNamedConstantDefinition(name, !mIgnoreMissingParams);
  923. if (def)
  924. _writeRawConstant(def->physicalIndex, val);
  925. }
  926. //---------------------------------------------------------------------------
  927. void GpuProgramParameters::setNamedConstant(const String& name, const Vector4& vec)
  928. {
  929. // look up, and throw an exception if we're not ignoring missing
  930. const GpuConstantDefinition* def =
  931. _findNamedConstantDefinition(name, !mIgnoreMissingParams);
  932. if (def)
  933. _writeRawConstant(def->physicalIndex, vec, def->elementSize);
  934. }
  935. //---------------------------------------------------------------------------
  936. void GpuProgramParameters::setNamedConstant(const String& name, const Vector3& vec)
  937. {
  938. // look up, and throw an exception if we're not ignoring missing
  939. const GpuConstantDefinition* def =
  940. _findNamedConstantDefinition(name, !mIgnoreMissingParams);
  941. if (def)
  942. _writeRawConstant(def->physicalIndex, vec);
  943. }
  944. //---------------------------------------------------------------------------
  945. void GpuProgramParameters::setNamedConstant(const String& name, const Matrix4& m)
  946. {
  947. // look up, and throw an exception if we're not ignoring missing
  948. const GpuConstantDefinition* def =
  949. _findNamedConstantDefinition(name, !mIgnoreMissingParams);
  950. if (def)
  951. _writeRawConstant(def->physicalIndex, m, def->elementSize);
  952. }
  953. //---------------------------------------------------------------------------
  954. void GpuProgramParameters::setNamedConstant(const String& name, const Matrix4* m,
  955. size_t numEntries)
  956. {
  957. // look up, and throw an exception if we're not ignoring missing
  958. const GpuConstantDefinition* def =
  959. _findNamedConstantDefinition(name, !mIgnoreMissingParams);
  960. if (def)
  961. _writeRawConstant(def->physicalIndex, m, numEntries);
  962. }
  963. //---------------------------------------------------------------------------
  964. void GpuProgramParameters::setNamedConstant(const String& name,
  965. const float *val, size_t count, size_t multiple)
  966. {
  967. size_t rawCount = count * multiple;
  968. // look up, and throw an exception if we're not ignoring missing
  969. const GpuConstantDefinition* def =
  970. _findNamedConstantDefinition(name, !mIgnoreMissingParams);
  971. if (def)
  972. _writeRawConstants(def->physicalIndex, val, rawCount);
  973. }
  974. //---------------------------------------------------------------------------
  975. void GpuProgramParameters::setNamedConstant(const String& name,
  976. const double *val, size_t count, size_t multiple)
  977. {
  978. size_t rawCount = count * multiple;
  979. // look up, and throw an exception if we're not ignoring missing
  980. const GpuConstantDefinition* def =
  981. _findNamedConstantDefinition(name, !mIgnoreMissingParams);
  982. if (def)
  983. _writeRawConstants(def->physicalIndex, val, rawCount);
  984. }
  985. //---------------------------------------------------------------------------
  986. void GpuProgramParameters::setNamedConstant(const String& name, const Color& colour)
  987. {
  988. // look up, and throw an exception if we're not ignoring missing
  989. const GpuConstantDefinition* def =
  990. _findNamedConstantDefinition(name, !mIgnoreMissingParams);
  991. if (def)
  992. _writeRawConstant(def->physicalIndex, colour, def->elementSize);
  993. }
  994. //---------------------------------------------------------------------------
  995. void GpuProgramParameters::setNamedConstant(const String& name,
  996. const int *val, size_t count, size_t multiple)
  997. {
  998. size_t rawCount = count * multiple;
  999. // look up, and throw an exception if we're not ignoring missing
  1000. const GpuConstantDefinition* def =
  1001. _findNamedConstantDefinition(name, !mIgnoreMissingParams);
  1002. if (def)
  1003. _writeRawConstants(def->physicalIndex, val, rawCount);
  1004. }
  1005. //---------------------------------------------------------------------------
  1006. void GpuProgramParameters::copyConstantsFrom(const GpuProgramParameters& source)
  1007. {
  1008. // Pull buffers & auto constant list over directly
  1009. mFloatConstants = source.getFloatConstantList();
  1010. mIntConstants = source.getIntConstantList();
  1011. mCombinedVariability = source.mCombinedVariability;
  1012. copySharedParamSetUsage(source.mSharedParamSets);
  1013. }
  1014. //---------------------------------------------------------------------
  1015. void GpuProgramParameters::copyMatchingNamedConstantsFrom(const GpuProgramParameters& source)
  1016. {
  1017. if ((mNamedConstants != nullptr) && (source.mNamedConstants != nullptr))
  1018. {
  1019. std::map<size_t, String> srcToDestNamedMap;
  1020. for (GpuConstantDefinitionMap::const_iterator i = source.mNamedConstants->map.begin();
  1021. i != source.mNamedConstants->map.end(); ++i)
  1022. {
  1023. const String& paramName = i->first;
  1024. const GpuConstantDefinition& olddef = i->second;
  1025. const GpuConstantDefinition* newdef = _findNamedConstantDefinition(paramName, false);
  1026. if (newdef)
  1027. {
  1028. // Copy data across, based on smallest common definition size
  1029. size_t srcsz = olddef.elementSize * olddef.arraySize;
  1030. size_t destsz = newdef->elementSize * newdef->arraySize;
  1031. size_t sz = std::min(srcsz, destsz);
  1032. if (newdef->isFloat())
  1033. {
  1034. memcpy(getFloatPointer(newdef->physicalIndex),
  1035. source.getFloatPointer(olddef.physicalIndex),
  1036. sz * sizeof(float));
  1037. }
  1038. else
  1039. {
  1040. memcpy(getIntPointer(newdef->physicalIndex),
  1041. source.getIntPointer(olddef.physicalIndex),
  1042. sz * sizeof(int));
  1043. }
  1044. // we'll use this map to resolve autos later
  1045. // ignore the [0] aliases
  1046. if (!StringUtil::endsWith(paramName, "[0]"))
  1047. srcToDestNamedMap[olddef.physicalIndex] = paramName;
  1048. }
  1049. }
  1050. // Copy shared param sets
  1051. for (GpuSharedParamUsageList::const_iterator i = source.mSharedParamSets.begin();
  1052. i != source.mSharedParamSets.end(); ++i)
  1053. {
  1054. const GpuSharedParametersUsage& usage = *i;
  1055. if (!isUsingSharedParameters(usage.getName()))
  1056. {
  1057. addSharedParameters(usage.getSharedParams());
  1058. }
  1059. }
  1060. }
  1061. }
  1062. //-----------------------------------------------------------------------
  1063. void GpuProgramParameters::incPassIterationNumber(void)
  1064. {
  1065. if (mActivePassIterationIndex != std::numeric_limits<size_t>::max())
  1066. {
  1067. // This is a physical index
  1068. ++mFloatConstants[mActivePassIterationIndex];
  1069. }
  1070. }
  1071. //---------------------------------------------------------------------
  1072. void GpuProgramParameters::addSharedParameters(GpuSharedParametersPtr sharedParams)
  1073. {
  1074. if (!isUsingSharedParameters(sharedParams->getName()))
  1075. {
  1076. mSharedParamSets.push_back(GpuSharedParametersUsage(sharedParams, this));
  1077. }
  1078. }
  1079. //---------------------------------------------------------------------
  1080. void GpuProgramParameters::addSharedParameters(const String& sharedParamsName)
  1081. {
  1082. // TODO PORT - I don't think I'll be needing this. I dont plan on including GpuProgramManager in the port
  1083. //addSharedParameters(GpuProgramManager::getSingleton().getSharedParameters(sharedParamsName));
  1084. }
  1085. //---------------------------------------------------------------------
  1086. bool GpuProgramParameters::isUsingSharedParameters(const String& sharedParamsName) const
  1087. {
  1088. for (GpuSharedParamUsageList::const_iterator i = mSharedParamSets.begin();
  1089. i != mSharedParamSets.end(); ++i)
  1090. {
  1091. if (i->getName() == sharedParamsName)
  1092. return true;
  1093. }
  1094. return false;
  1095. }
  1096. //---------------------------------------------------------------------
  1097. void GpuProgramParameters::removeSharedParameters(const String& sharedParamsName)
  1098. {
  1099. for (GpuSharedParamUsageList::iterator i = mSharedParamSets.begin();
  1100. i != mSharedParamSets.end(); ++i)
  1101. {
  1102. if (i->getName() == sharedParamsName)
  1103. {
  1104. mSharedParamSets.erase(i);
  1105. break;
  1106. }
  1107. }
  1108. }
  1109. //---------------------------------------------------------------------
  1110. void GpuProgramParameters::removeAllSharedParameters()
  1111. {
  1112. mSharedParamSets.clear();
  1113. }
  1114. //---------------------------------------------------------------------
  1115. const GpuProgramParameters::GpuSharedParamUsageList&
  1116. GpuProgramParameters::getSharedParameters() const
  1117. {
  1118. return mSharedParamSets;
  1119. }
  1120. //---------------------------------------------------------------------
  1121. void GpuProgramParameters::_copySharedParams()
  1122. {
  1123. for (GpuSharedParamUsageList::iterator i = mSharedParamSets.begin();
  1124. i != mSharedParamSets.end(); ++i )
  1125. {
  1126. i->_copySharedParamsToTargetParams();
  1127. }
  1128. }
  1129. }