BsGpuParams.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #include "BsGpuParams.h"
  4. #include "BsGpuParamDesc.h"
  5. #include "BsGpuParamBlockBuffer.h"
  6. #include "BsVector2.h"
  7. #include "BsTexture.h"
  8. #include "BsSamplerState.h"
  9. #include "BsFrameAlloc.h"
  10. #include "BsDebug.h"
  11. #include "BsException.h"
  12. #include "BsVectorNI.h"
  13. #include "BsMatrixNxM.h"
  14. namespace BansheeEngine
  15. {
  16. GpuParamsBase::GpuParamsBase(const GpuParamDescPtr& paramDesc, bool transposeMatrices)
  17. :mParamDesc(paramDesc), mTransposeMatrices(transposeMatrices), mNumParamBlocks(0), mNumSamplerStates(0),
  18. mNumTextures(0), mTextureInfo(nullptr)
  19. {
  20. for (auto& paramBlock : mParamDesc->paramBlocks)
  21. {
  22. if ((paramBlock.second.slot + 1) > mNumParamBlocks)
  23. mNumParamBlocks = paramBlock.second.slot + 1;
  24. }
  25. for (auto& texture : mParamDesc->textures)
  26. {
  27. if ((texture.second.slot + 1) > mNumTextures)
  28. mNumTextures = texture.second.slot + 1;
  29. }
  30. for (auto& sampler : mParamDesc->samplers)
  31. {
  32. if ((sampler.second.slot + 1) > mNumSamplerStates)
  33. mNumSamplerStates = sampler.second.slot + 1;
  34. }
  35. mTextureInfo = bs_newN<BoundTextureInfo>(mNumTextures);
  36. }
  37. GpuParamsBase::~GpuParamsBase()
  38. {
  39. bs_deleteN(mTextureInfo, mNumTextures);
  40. }
  41. UINT32 GpuParamsBase::getDataParamSize(const String& name) const
  42. {
  43. GpuParamDataDesc* desc = getParamDesc(name);
  44. if(desc != nullptr)
  45. return desc->elementSize * 4;
  46. return 0;
  47. }
  48. bool GpuParamsBase::hasParam(const String& name) const
  49. {
  50. return getParamDesc(name) != nullptr;
  51. }
  52. bool GpuParamsBase::hasTexture(const String& name) const
  53. {
  54. auto paramIter = mParamDesc->textures.find(name);
  55. if(paramIter != mParamDesc->textures.end())
  56. return true;
  57. return false;
  58. }
  59. bool GpuParamsBase::hasSamplerState(const String& name) const
  60. {
  61. auto paramIter = mParamDesc->samplers.find(name);
  62. if(paramIter != mParamDesc->samplers.end())
  63. return true;
  64. return false;
  65. }
  66. bool GpuParamsBase::hasParamBlock(const String& name) const
  67. {
  68. auto paramBlockIter = mParamDesc->paramBlocks.find(name);
  69. if(paramBlockIter != mParamDesc->paramBlocks.end())
  70. return true;
  71. return false;
  72. }
  73. GpuParamDataDesc* GpuParamsBase::getParamDesc(const String& name) const
  74. {
  75. auto paramIter = mParamDesc->params.find(name);
  76. if (paramIter != mParamDesc->params.end())
  77. return &paramIter->second;
  78. return nullptr;
  79. }
  80. bool GpuParamsBase::isLoadStoreTexture(UINT32 slot) const
  81. {
  82. if (slot < 0 || slot >= mNumTextures)
  83. {
  84. BS_EXCEPT(InvalidParametersException, "Index out of range: Valid range: 0 .. " +
  85. toString(mNumTextures - 1) + ". Requested: " + toString(slot));
  86. }
  87. return mTextureInfo[slot].isLoadStore;
  88. }
  89. void GpuParamsBase::setIsLoadStoreTexture(UINT32 slot, bool isLoadStore)
  90. {
  91. if (slot < 0 || slot >= mNumTextures)
  92. {
  93. BS_EXCEPT(InvalidParametersException, "Index out of range: Valid range: 0 .. " +
  94. toString(mNumTextures - 1) + ". Requested: " + toString(slot));
  95. }
  96. mTextureInfo[slot].isLoadStore = isLoadStore;
  97. }
  98. const TextureSurface& GpuParamsBase::getLoadStoreSurface(UINT32 slot) const
  99. {
  100. if (slot < 0 || slot >= mNumTextures)
  101. {
  102. BS_EXCEPT(InvalidParametersException, "Index out of range: Valid range: 0 .. " +
  103. toString(mNumTextures - 1) + ". Requested: " + toString(slot));
  104. }
  105. return mTextureInfo[slot].surface;
  106. }
  107. void GpuParamsBase::setLoadStoreSurface(UINT32 slot, const TextureSurface& surface) const
  108. {
  109. if (slot < 0 || slot >= mNumTextures)
  110. {
  111. BS_EXCEPT(InvalidParametersException, "Index out of range: Valid range: 0 .. " +
  112. toString(mNumTextures - 1) + ". Requested: " + toString(slot));
  113. }
  114. mTextureInfo[slot].surface = surface;
  115. }
  116. template<bool Core>
  117. TGpuParams<Core>::TGpuParams(const GpuParamDescPtr& paramDesc, bool transposeMatrices)
  118. :GpuParamsBase(paramDesc, transposeMatrices), mParamBlockBuffers(nullptr), mTextures(nullptr),
  119. mSamplerStates(nullptr)
  120. {
  121. if (mNumParamBlocks > 0)
  122. mParamBlockBuffers = bs_newN<ParamsBufferType>(mNumParamBlocks);
  123. if (mNumTextures > 0)
  124. mTextures = bs_newN<TextureType>(mNumTextures);
  125. if (mNumSamplerStates > 0)
  126. mSamplerStates = bs_newN<SamplerType>(mNumSamplerStates);
  127. }
  128. template<bool Core>
  129. TGpuParams<Core>::~TGpuParams()
  130. {
  131. if (mParamBlockBuffers != nullptr)
  132. bs_deleteN(mParamBlockBuffers, mNumParamBlocks);
  133. if (mTextures != nullptr)
  134. bs_deleteN(mTextures, mNumTextures);
  135. if (mSamplerStates != nullptr)
  136. bs_deleteN(mSamplerStates, mNumSamplerStates);
  137. }
  138. template<bool Core>
  139. void TGpuParams<Core>::setParamBlockBuffer(UINT32 slot, const ParamsBufferType& paramBlockBuffer)
  140. {
  141. if (slot < 0 || slot >= mNumParamBlocks)
  142. {
  143. BS_EXCEPT(InvalidParametersException, "Index out of range: Valid range: 0 .. " +
  144. toString(mNumParamBlocks - 1) + ". Requested: " + toString(slot));
  145. }
  146. mParamBlockBuffers[slot] = paramBlockBuffer;
  147. _markCoreDirty();
  148. }
  149. template<bool Core>
  150. void TGpuParams<Core>::setParamBlockBuffer(const String& name, const ParamsBufferType& paramBlockBuffer)
  151. {
  152. auto iterFind = mParamDesc->paramBlocks.find(name);
  153. if (iterFind == mParamDesc->paramBlocks.end())
  154. {
  155. LOGWRN("Cannot find parameter block with the name: " + name);
  156. return;
  157. }
  158. mParamBlockBuffers[iterFind->second.slot] = paramBlockBuffer;
  159. _markCoreDirty();
  160. }
  161. template<bool Core>
  162. template<class T>
  163. void TGpuParams<Core>::getParam(const String& name, TGpuDataParam<T, Core>& output) const
  164. {
  165. auto iterFind = mParamDesc->params.find(name);
  166. if (iterFind == mParamDesc->params.end())
  167. {
  168. output = TGpuDataParam<T, Core>(nullptr, nullptr);
  169. LOGWRN("Cannot find parameter with the name '" + name + "'");
  170. }
  171. else
  172. output = TGpuDataParam<T, Core>(&iterFind->second, _getThisPtr());
  173. }
  174. template<bool Core>
  175. void TGpuParams<Core>::getStructParam(const String& name, TGpuParamStruct<Core>& output) const
  176. {
  177. auto iterFind = mParamDesc->params.find(name);
  178. if (iterFind == mParamDesc->params.end() || iterFind->second.type != GPDT_STRUCT)
  179. {
  180. output = TGpuParamStruct<Core>(nullptr, nullptr);
  181. LOGWRN("Cannot find struct parameter with the name '" + name + "'");
  182. }
  183. else
  184. output = TGpuParamStruct<Core>(&iterFind->second, _getThisPtr());
  185. }
  186. template<bool Core>
  187. void TGpuParams<Core>::getTextureParam(const String& name, TGpuParamTexture<Core>& output) const
  188. {
  189. auto iterFind = mParamDesc->textures.find(name);
  190. if (iterFind == mParamDesc->textures.end())
  191. {
  192. output = TGpuParamTexture<Core>(nullptr, nullptr);
  193. LOGWRN("Cannot find texture parameter with the name '" + name + "'");
  194. }
  195. else
  196. output = TGpuParamTexture<Core>(&iterFind->second, _getThisPtr());
  197. }
  198. template<bool Core>
  199. void TGpuParams<Core>::getLoadStoreTextureParam(const String& name, TGpuParamLoadStoreTexture<Core>& output) const
  200. {
  201. auto iterFind = mParamDesc->textures.find(name);
  202. if (iterFind == mParamDesc->textures.end())
  203. {
  204. output = TGpuParamLoadStoreTexture<Core>(nullptr, nullptr);
  205. LOGWRN("Cannot find texture parameter with the name '" + name + "'");
  206. }
  207. else
  208. output = TGpuParamLoadStoreTexture<Core>(&iterFind->second, _getThisPtr());
  209. }
  210. template<bool Core>
  211. void TGpuParams<Core>::getSamplerStateParam(const String& name, TGpuParamSampState<Core>& output) const
  212. {
  213. auto iterFind = mParamDesc->samplers.find(name);
  214. if (iterFind == mParamDesc->samplers.end())
  215. {
  216. output = TGpuParamSampState<Core>(nullptr, nullptr);
  217. LOGWRN("Cannot find sampler state parameter with the name '" + name + "'");
  218. }
  219. else
  220. output = TGpuParamSampState<Core>(&iterFind->second, _getThisPtr());
  221. }
  222. template<bool Core>
  223. typename TGpuParams<Core>::ParamsBufferType TGpuParams<Core>::getParamBlockBuffer(UINT32 slot) const
  224. {
  225. if (slot < 0 || slot >= mNumParamBlocks)
  226. {
  227. BS_EXCEPT(InvalidParametersException, "Index out of range: Valid range: 0 .. " +
  228. toString(mNumParamBlocks - 1) + ". Requested: " + toString(slot));
  229. }
  230. return mParamBlockBuffers[slot];
  231. }
  232. template<bool Core>
  233. typename TGpuParams<Core>::TextureType TGpuParams<Core>::getTexture(UINT32 slot)
  234. {
  235. if (slot < 0 || slot >= mNumTextures)
  236. {
  237. BS_EXCEPT(InvalidParametersException, "Index out of range: Valid range: 0 .. " +
  238. toString(mNumTextures - 1) + ". Requested: " + toString(slot));
  239. }
  240. return mTextures[slot];
  241. }
  242. template<bool Core>
  243. typename TGpuParams<Core>::SamplerType TGpuParams<Core>::getSamplerState(UINT32 slot)
  244. {
  245. if (slot < 0 || slot >= mNumSamplerStates)
  246. {
  247. BS_EXCEPT(InvalidParametersException, "Index out of range: Valid range: 0 .. " +
  248. toString(mNumSamplerStates - 1) + ". Requested: " + toString(slot));
  249. }
  250. return mSamplerStates[slot];
  251. }
  252. template<bool Core>
  253. void TGpuParams<Core>::setTexture(UINT32 slot, const TextureType& texture)
  254. {
  255. if (slot < 0 || slot >= mNumTextures)
  256. {
  257. BS_EXCEPT(InvalidParametersException, "Index out of range: Valid range: 0 .. " +
  258. toString(mNumTextures - 1) + ". Requested: " + toString(slot));
  259. }
  260. mTextures[slot] = texture;
  261. _markResourcesDirty();
  262. _markCoreDirty();
  263. }
  264. template<bool Core>
  265. void TGpuParams<Core>::setSamplerState(UINT32 slot, const SamplerType& sampler)
  266. {
  267. if (slot < 0 || slot >= mNumSamplerStates)
  268. {
  269. BS_EXCEPT(InvalidParametersException, "Index out of range: Valid range: 0 .. " +
  270. toString(mNumSamplerStates - 1) + ". Requested: " + toString(slot));
  271. }
  272. mSamplerStates[slot] = sampler;
  273. _markResourcesDirty();
  274. _markCoreDirty();
  275. }
  276. template class TGpuParams < false > ;
  277. template class TGpuParams < true > ;
  278. template BS_CORE_EXPORT void TGpuParams<false>::getParam<float>(const String&, TGpuDataParam<float, false>&) const;
  279. template BS_CORE_EXPORT void TGpuParams<false>::getParam<int>(const String&, TGpuDataParam<int, false>&) const;
  280. template BS_CORE_EXPORT void TGpuParams<false>::getParam<Color>(const String&, TGpuDataParam<Color, false>&) const;
  281. template BS_CORE_EXPORT void TGpuParams<false>::getParam<Vector2>(const String&, TGpuDataParam<Vector2, false>&) const;
  282. template BS_CORE_EXPORT void TGpuParams<false>::getParam<Vector3>(const String&, TGpuDataParam<Vector3, false>&) const;
  283. template BS_CORE_EXPORT void TGpuParams<false>::getParam<Vector4>(const String&, TGpuDataParam<Vector4, false>&) const;
  284. template BS_CORE_EXPORT void TGpuParams<false>::getParam<Vector2I>(const String&, TGpuDataParam<Vector2I, false>&) const;
  285. template BS_CORE_EXPORT void TGpuParams<false>::getParam<Vector3I>(const String&, TGpuDataParam<Vector3I, false>&) const;
  286. template BS_CORE_EXPORT void TGpuParams<false>::getParam<Vector4I>(const String&, TGpuDataParam<Vector4I, false>&) const;
  287. template BS_CORE_EXPORT void TGpuParams<false>::getParam<Matrix2>(const String&, TGpuDataParam<Matrix2, false>&) const;
  288. template BS_CORE_EXPORT void TGpuParams<false>::getParam<Matrix2x3>(const String&, TGpuDataParam<Matrix2x3, false>&) const;
  289. template BS_CORE_EXPORT void TGpuParams<false>::getParam<Matrix2x4>(const String&, TGpuDataParam<Matrix2x4, false>&) const;
  290. template BS_CORE_EXPORT void TGpuParams<false>::getParam<Matrix3>(const String&, TGpuDataParam<Matrix3, false>&) const;
  291. template BS_CORE_EXPORT void TGpuParams<false>::getParam<Matrix3x2>(const String&, TGpuDataParam<Matrix3x2, false>&) const;
  292. template BS_CORE_EXPORT void TGpuParams<false>::getParam<Matrix3x4>(const String&, TGpuDataParam<Matrix3x4, false>&) const;
  293. template BS_CORE_EXPORT void TGpuParams<false>::getParam<Matrix4>(const String&, TGpuDataParam<Matrix4, false>&) const;
  294. template BS_CORE_EXPORT void TGpuParams<false>::getParam<Matrix4x2>(const String&, TGpuDataParam<Matrix4x2, false>&) const;
  295. template BS_CORE_EXPORT void TGpuParams<false>::getParam<Matrix4x3>(const String&, TGpuDataParam<Matrix4x3, false>&) const;
  296. template BS_CORE_EXPORT void TGpuParams<true>::getParam<float>(const String&, TGpuDataParam<float, true>&) const;
  297. template BS_CORE_EXPORT void TGpuParams<true>::getParam<int>(const String&, TGpuDataParam<int, true>&) const;
  298. template BS_CORE_EXPORT void TGpuParams<true>::getParam<Color>(const String&, TGpuDataParam<Color, true>&) const;
  299. template BS_CORE_EXPORT void TGpuParams<true>::getParam<Vector2>(const String&, TGpuDataParam<Vector2, true>&) const;
  300. template BS_CORE_EXPORT void TGpuParams<true>::getParam<Vector3>(const String&, TGpuDataParam<Vector3, true>&) const;
  301. template BS_CORE_EXPORT void TGpuParams<true>::getParam<Vector4>(const String&, TGpuDataParam<Vector4, true>&) const;
  302. template BS_CORE_EXPORT void TGpuParams<true>::getParam<Vector2I>(const String&, TGpuDataParam<Vector2I, true>&) const;
  303. template BS_CORE_EXPORT void TGpuParams<true>::getParam<Vector3I>(const String&, TGpuDataParam<Vector3I, true>&) const;
  304. template BS_CORE_EXPORT void TGpuParams<true>::getParam<Vector4I>(const String&, TGpuDataParam<Vector4I, true>&) const;
  305. template BS_CORE_EXPORT void TGpuParams<true>::getParam<Matrix2>(const String&, TGpuDataParam<Matrix2, true>&) const;
  306. template BS_CORE_EXPORT void TGpuParams<true>::getParam<Matrix2x3>(const String&, TGpuDataParam<Matrix2x3, true>&) const;
  307. template BS_CORE_EXPORT void TGpuParams<true>::getParam<Matrix2x4>(const String&, TGpuDataParam<Matrix2x4, true>&) const;
  308. template BS_CORE_EXPORT void TGpuParams<true>::getParam<Matrix3>(const String&, TGpuDataParam<Matrix3, true>&) const;
  309. template BS_CORE_EXPORT void TGpuParams<true>::getParam<Matrix3x2>(const String&, TGpuDataParam<Matrix3x2, true>&) const;
  310. template BS_CORE_EXPORT void TGpuParams<true>::getParam<Matrix3x4>(const String&, TGpuDataParam<Matrix3x4, true>&) const;
  311. template BS_CORE_EXPORT void TGpuParams<true>::getParam<Matrix4>(const String&, TGpuDataParam<Matrix4, true>&) const;
  312. template BS_CORE_EXPORT void TGpuParams<true>::getParam<Matrix4x2>(const String&, TGpuDataParam<Matrix4x2, true>&) const;
  313. template BS_CORE_EXPORT void TGpuParams<true>::getParam<Matrix4x3>(const String&, TGpuDataParam<Matrix4x3, true>&) const;
  314. GpuParamsCore::GpuParamsCore(const GpuParamDescPtr& paramDesc, bool transposeMatrices)
  315. : TGpuParams(paramDesc, transposeMatrices)
  316. {
  317. }
  318. SPtr<GpuParamsCore> GpuParamsCore::_getThisPtr() const
  319. {
  320. return std::static_pointer_cast<GpuParamsCore>(getThisPtr());
  321. }
  322. void GpuParamsCore::updateHardwareBuffers()
  323. {
  324. for (UINT32 i = 0; i < mNumParamBlocks; i++)
  325. {
  326. if (mParamBlockBuffers[i] != nullptr)
  327. {
  328. mParamBlockBuffers[i]->flushToGPU();
  329. }
  330. }
  331. }
  332. void GpuParamsCore::syncToCore(const CoreSyncData& data)
  333. {
  334. UINT32 textureInfoSize = mNumTextures * sizeof(BoundTextureInfo);
  335. UINT32 paramBufferSize = mNumParamBlocks * sizeof(SPtr<GpuParamBlockBufferCore>);
  336. UINT32 textureArraySize = mNumTextures * sizeof(SPtr<TextureCore>);
  337. UINT32 samplerArraySize = mNumSamplerStates * sizeof(SPtr<SamplerStateCore>);
  338. UINT32 totalSize = textureInfoSize + paramBufferSize + textureArraySize + samplerArraySize;
  339. UINT32 textureInfoOffset = 0;
  340. UINT32 paramBufferOffset = textureInfoOffset + textureInfoSize;
  341. UINT32 textureArrayOffset = paramBufferOffset + paramBufferSize;
  342. UINT32 samplerArrayOffset = textureArrayOffset + textureArraySize;
  343. assert(data.getBufferSize() == totalSize);
  344. UINT8* dataPtr = data.getBuffer();
  345. BoundTextureInfo* textureInfos = (BoundTextureInfo*)(dataPtr + textureInfoOffset);
  346. SPtr<GpuParamBlockBufferCore>* paramBuffers = (SPtr<GpuParamBlockBufferCore>*)(dataPtr + paramBufferOffset);
  347. SPtr<TextureCore>* textures = (SPtr<TextureCore>*)(dataPtr + textureArrayOffset);
  348. SPtr<SamplerStateCore>* samplers = (SPtr<SamplerStateCore>*)(dataPtr + samplerArrayOffset);
  349. // Copy & destruct
  350. for (UINT32 i = 0; i < mNumParamBlocks; i++)
  351. {
  352. mParamBlockBuffers[i] = paramBuffers[i];
  353. paramBuffers[i].~SPtr<GpuParamBlockBufferCore>();
  354. }
  355. for (UINT32 i = 0; i < mNumTextures; i++)
  356. {
  357. mTextureInfo[i] = textureInfos[i];
  358. textureInfos[i].~BoundTextureInfo();
  359. mTextures[i] = textures[i];
  360. textures[i].~SPtr<TextureCore>();
  361. }
  362. for (UINT32 i = 0; i < mNumSamplerStates; i++)
  363. {
  364. mSamplerStates[i] = samplers[i];
  365. samplers[i].~SPtr<SamplerStateCore>();
  366. }
  367. }
  368. SPtr<GpuParamsCore> GpuParamsCore::create(const GpuParamDescPtr& paramDesc, bool transposeMatrices)
  369. {
  370. GpuParamsCore* params = new (bs_alloc<GpuParamsCore>()) GpuParamsCore(paramDesc, transposeMatrices);
  371. SPtr<GpuParamsCore> paramsPtr = bs_shared_ptr<GpuParamsCore>(params);
  372. paramsPtr->_setThisPtr(paramsPtr);
  373. return paramsPtr;
  374. }
  375. const GpuDataParamInfos GpuParams::PARAM_SIZES;
  376. GpuParams::GpuParams(const GpuParamDescPtr& paramDesc, bool transposeMatrices)
  377. : TGpuParams(paramDesc, transposeMatrices)
  378. {
  379. }
  380. SPtr<GpuParams> GpuParams::_getThisPtr() const
  381. {
  382. return std::static_pointer_cast<GpuParams>(getThisPtr());
  383. }
  384. SPtr<GpuParamsCore> GpuParams::getCore() const
  385. {
  386. return std::static_pointer_cast<GpuParamsCore>(mCoreSpecific);
  387. }
  388. SPtr<CoreObjectCore> GpuParams::createCore() const
  389. {
  390. GpuParamsCore* obj = new (bs_alloc<GpuParamsCore>()) GpuParamsCore(mParamDesc, mTransposeMatrices);
  391. SPtr<CoreObjectCore> coreObj = bs_shared_ptr<GpuParamsCore>(obj);
  392. coreObj->_setThisPtr(coreObj);
  393. return coreObj;
  394. }
  395. void GpuParams::_markCoreDirty()
  396. {
  397. markCoreDirty();
  398. }
  399. void GpuParams::_markResourcesDirty()
  400. {
  401. markListenerResourcesDirty();
  402. }
  403. SPtr<GpuParams> GpuParams::create(const GpuParamDescPtr& paramDesc, bool transposeMatrices)
  404. {
  405. GpuParams* params = new (bs_alloc<GpuParams>()) GpuParams(paramDesc, transposeMatrices);
  406. SPtr<GpuParams> paramsPtr = bs_core_ptr<GpuParams>(params);
  407. paramsPtr->_setThisPtr(paramsPtr);
  408. paramsPtr->initialize();
  409. return paramsPtr;
  410. }
  411. CoreSyncData GpuParams::syncToCore(FrameAlloc* allocator)
  412. {
  413. UINT32 textureInfoSize = mNumTextures * sizeof(BoundTextureInfo);
  414. UINT32 paramBufferSize = mNumParamBlocks * sizeof(SPtr<GpuParamBlockBufferCore>);
  415. UINT32 textureArraySize = mNumTextures * sizeof(SPtr<TextureCore>);
  416. UINT32 samplerArraySize = mNumSamplerStates * sizeof(SPtr<SamplerStateCore>);
  417. UINT32 totalSize = textureInfoSize + paramBufferSize + textureArraySize + samplerArraySize;
  418. UINT32 textureInfoOffset = 0;
  419. UINT32 paramBufferOffset = textureInfoOffset + textureInfoSize;
  420. UINT32 textureArrayOffset = paramBufferOffset + paramBufferSize;
  421. UINT32 samplerArrayOffset = textureArrayOffset + textureArraySize;
  422. UINT8* data = allocator->alloc(totalSize);
  423. BoundTextureInfo* textureInfos = (BoundTextureInfo*)(data + textureInfoOffset);
  424. SPtr<GpuParamBlockBufferCore>* paramBuffers = (SPtr<GpuParamBlockBufferCore>*)(data + paramBufferOffset);
  425. SPtr<TextureCore>* textures = (SPtr<TextureCore>*)(data + textureArrayOffset);
  426. SPtr<SamplerStateCore>* samplers = (SPtr<SamplerStateCore>*)(data + samplerArrayOffset);
  427. // Construct & copy
  428. for (UINT32 i = 0; i < mNumParamBlocks; i++)
  429. {
  430. new (&paramBuffers[i]) SPtr<GpuParamBlockBufferCore>();
  431. if (mParamBlockBuffers[i] != nullptr)
  432. paramBuffers[i] = mParamBlockBuffers[i]->getCore();
  433. }
  434. for (UINT32 i = 0; i < mNumTextures; i++)
  435. {
  436. new (&textureInfos[i]) BoundTextureInfo();
  437. textureInfos[i] = mTextureInfo[i];
  438. new (&textures[i]) SPtr<TextureCore>();
  439. if (mTextures[i].isLoaded())
  440. textures[i] = mTextures[i]->getCore();
  441. else
  442. textures[i] = nullptr;
  443. }
  444. for (UINT32 i = 0; i < mNumSamplerStates; i++)
  445. {
  446. new (&samplers[i]) SPtr<SamplerStateCore>();
  447. if (mSamplerStates[i] != nullptr)
  448. samplers[i] = mSamplerStates[i]->getCore();
  449. else
  450. samplers[i] = nullptr;
  451. }
  452. return CoreSyncData(data, totalSize);
  453. }
  454. void GpuParams::getListenerResources(Vector<HResource>& resources)
  455. {
  456. for (UINT32 i = 0; i < mNumTextures; i++)
  457. {
  458. if (mTextures[i] != nullptr)
  459. resources.push_back(mTextures[i]);
  460. }
  461. }
  462. }