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