BsGpuParams.cpp 19 KB

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