CmGpuProgramParams.cpp 33 KB

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