BsGpuParams.cpp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705
  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 "BsGpuBuffer.h"
  9. #include "BsSamplerState.h"
  10. #include "BsFrameAlloc.h"
  11. #include "BsDebug.h"
  12. #include "BsException.h"
  13. #include "BsVectorNI.h"
  14. #include "BsMatrixNxM.h"
  15. namespace BansheeEngine
  16. {
  17. GpuParamsBase::GpuParamsBase(const SPtr<GpuParamDesc>& paramDesc)
  18. : mParamDesc(paramDesc), mNumParamBlocks(0), mNumTextures(0), mNumLoadStoreTextures(0), mNumBuffers(0)
  19. , mNumSamplerStates(0), mLoadStoreSurfaces(nullptr)
  20. {
  21. for (auto& paramBlock : mParamDesc->paramBlocks)
  22. {
  23. if ((paramBlock.second.slot + 1) > mNumParamBlocks)
  24. mNumParamBlocks = paramBlock.second.slot + 1;
  25. }
  26. for (auto& texture : mParamDesc->textures)
  27. {
  28. if ((texture.second.slot + 1) > mNumTextures)
  29. mNumTextures = texture.second.slot + 1;
  30. }
  31. for (auto& texture : mParamDesc->loadStoreTextures)
  32. {
  33. if ((texture.second.slot + 1) > mNumLoadStoreTextures)
  34. mNumLoadStoreTextures = texture.second.slot + 1;
  35. }
  36. for (auto& buffer : mParamDesc->buffers)
  37. {
  38. if ((buffer.second.slot + 1) > mNumBuffers)
  39. mNumBuffers = buffer.second.slot + 1;
  40. }
  41. for (auto& sampler : mParamDesc->samplers)
  42. {
  43. if ((sampler.second.slot + 1) > mNumSamplerStates)
  44. mNumSamplerStates = sampler.second.slot + 1;
  45. }
  46. mLoadStoreSurfaces = bs_newN<TextureSurface>(mNumLoadStoreTextures);
  47. }
  48. GpuParamsBase::~GpuParamsBase()
  49. {
  50. bs_deleteN(mLoadStoreSurfaces, mNumLoadStoreTextures);
  51. }
  52. UINT32 GpuParamsBase::getDataParamSize(const String& name) const
  53. {
  54. GpuParamDataDesc* desc = getParamDesc(name);
  55. if(desc != nullptr)
  56. return desc->elementSize * 4;
  57. return 0;
  58. }
  59. bool GpuParamsBase::hasParam(const String& name) const
  60. {
  61. return getParamDesc(name) != nullptr;
  62. }
  63. bool GpuParamsBase::hasTexture(const String& name) const
  64. {
  65. auto paramIter = mParamDesc->textures.find(name);
  66. if(paramIter != mParamDesc->textures.end())
  67. return true;
  68. return false;
  69. }
  70. bool GpuParamsBase::hasBuffer(const String& name) const
  71. {
  72. auto paramIter = mParamDesc->buffers.find(name);
  73. if (paramIter != mParamDesc->buffers.end())
  74. return true;
  75. return false;
  76. }
  77. bool GpuParamsBase::hasLoadStoreTexture(const String& name) const
  78. {
  79. auto paramIter = mParamDesc->loadStoreTextures.find(name);
  80. if (paramIter != mParamDesc->loadStoreTextures.end())
  81. return true;
  82. return false;
  83. }
  84. bool GpuParamsBase::hasSamplerState(const String& name) const
  85. {
  86. auto paramIter = mParamDesc->samplers.find(name);
  87. if(paramIter != mParamDesc->samplers.end())
  88. return true;
  89. return false;
  90. }
  91. bool GpuParamsBase::hasParamBlock(const String& name) const
  92. {
  93. auto paramBlockIter = mParamDesc->paramBlocks.find(name);
  94. if(paramBlockIter != mParamDesc->paramBlocks.end())
  95. return true;
  96. return false;
  97. }
  98. GpuParamDataDesc* GpuParamsBase::getParamDesc(const String& name) const
  99. {
  100. auto paramIter = mParamDesc->params.find(name);
  101. if (paramIter != mParamDesc->params.end())
  102. return &paramIter->second;
  103. return nullptr;
  104. }
  105. GpuParamBlockDesc* GpuParamsBase::getParamBlockDesc(const String& name) const
  106. {
  107. auto paramBlockIter = mParamDesc->paramBlocks.find(name);
  108. if (paramBlockIter != mParamDesc->paramBlocks.end())
  109. return &paramBlockIter->second;
  110. return nullptr;
  111. }
  112. const TextureSurface& GpuParamsBase::getLoadStoreSurface(UINT32 slot) const
  113. {
  114. if (slot >= mNumLoadStoreTextures)
  115. {
  116. BS_EXCEPT(InvalidParametersException, "Index out of range: Valid range: 0 .. " +
  117. toString(mNumLoadStoreTextures - 1) + ". Requested: " + toString(slot));
  118. }
  119. return mLoadStoreSurfaces[slot];
  120. }
  121. void GpuParamsBase::setLoadStoreSurface(UINT32 slot, const TextureSurface& surface) const
  122. {
  123. if (slot >= mNumLoadStoreTextures)
  124. {
  125. BS_EXCEPT(InvalidParametersException, "Index out of range: Valid range: 0 .. " +
  126. toString(mNumLoadStoreTextures - 1) + ". Requested: " + toString(slot));
  127. }
  128. mLoadStoreSurfaces[slot] = surface;
  129. }
  130. template<bool Core>
  131. TGpuParams<Core>::TGpuParams(const SPtr<GpuParamDesc>& paramDesc)
  132. : GpuParamsBase(paramDesc), mParamBlockBuffers(nullptr), mTextures(nullptr)
  133. , mLoadStoreTextures(nullptr), mBuffers(nullptr), mSamplerStates(nullptr)
  134. {
  135. if (mNumParamBlocks > 0)
  136. mParamBlockBuffers = bs_newN<ParamsBufferType>(mNumParamBlocks);
  137. if (mNumTextures > 0)
  138. mTextures = bs_newN<TextureType>(mNumTextures);
  139. if (mNumLoadStoreTextures > 0)
  140. mLoadStoreTextures = bs_newN<TextureType>(mNumLoadStoreTextures);
  141. if(mNumBuffers > 0)
  142. mBuffers = bs_newN<BufferType>(mNumBuffers);
  143. if (mNumSamplerStates > 0)
  144. mSamplerStates = bs_newN<SamplerType>(mNumSamplerStates);
  145. }
  146. template<bool Core>
  147. TGpuParams<Core>::~TGpuParams()
  148. {
  149. if (mParamBlockBuffers != nullptr)
  150. bs_deleteN(mParamBlockBuffers, mNumParamBlocks);
  151. if (mTextures != nullptr)
  152. bs_deleteN(mTextures, mNumTextures);
  153. if (mLoadStoreTextures != nullptr)
  154. bs_deleteN(mLoadStoreTextures, mNumLoadStoreTextures);
  155. if (mBuffers != nullptr)
  156. bs_deleteN(mBuffers, mNumBuffers);
  157. if (mSamplerStates != nullptr)
  158. bs_deleteN(mSamplerStates, mNumSamplerStates);
  159. }
  160. template<bool Core>
  161. void TGpuParams<Core>::setParamBlockBuffer(UINT32 slot, const ParamsBufferType& paramBlockBuffer)
  162. {
  163. if (slot < 0 || slot >= mNumParamBlocks)
  164. {
  165. BS_EXCEPT(InvalidParametersException, "Index out of range: Valid range: 0 .. " +
  166. toString(mNumParamBlocks - 1) + ". Requested: " + toString(slot));
  167. }
  168. mParamBlockBuffers[slot] = paramBlockBuffer;
  169. _markCoreDirty();
  170. }
  171. template<bool Core>
  172. void TGpuParams<Core>::setParamBlockBuffer(const String& name, const ParamsBufferType& paramBlockBuffer)
  173. {
  174. auto iterFind = mParamDesc->paramBlocks.find(name);
  175. if (iterFind == mParamDesc->paramBlocks.end())
  176. {
  177. LOGWRN("Cannot find parameter block with the name: " + name);
  178. return;
  179. }
  180. mParamBlockBuffers[iterFind->second.slot] = paramBlockBuffer;
  181. _markCoreDirty();
  182. }
  183. template<bool Core>
  184. template<class T>
  185. void TGpuParams<Core>::getParam(const String& name, TGpuDataParam<T, Core>& output) const
  186. {
  187. auto iterFind = mParamDesc->params.find(name);
  188. if (iterFind == mParamDesc->params.end())
  189. {
  190. output = TGpuDataParam<T, Core>(nullptr, nullptr);
  191. LOGWRN("Cannot find parameter with the name '" + name + "'");
  192. }
  193. else
  194. output = TGpuDataParam<T, Core>(&iterFind->second, _getThisPtr());
  195. }
  196. template<bool Core>
  197. void TGpuParams<Core>::getStructParam(const String& name, TGpuParamStruct<Core>& output) const
  198. {
  199. auto iterFind = mParamDesc->params.find(name);
  200. if (iterFind == mParamDesc->params.end() || iterFind->second.type != GPDT_STRUCT)
  201. {
  202. output = TGpuParamStruct<Core>(nullptr, nullptr);
  203. LOGWRN("Cannot find struct parameter with the name '" + name + "'");
  204. }
  205. else
  206. output = TGpuParamStruct<Core>(&iterFind->second, _getThisPtr());
  207. }
  208. template<bool Core>
  209. void TGpuParams<Core>::getTextureParam(const String& name, TGpuParamTexture<Core>& output) const
  210. {
  211. auto iterFind = mParamDesc->textures.find(name);
  212. if (iterFind == mParamDesc->textures.end())
  213. {
  214. output = TGpuParamTexture<Core>(nullptr, nullptr);
  215. LOGWRN("Cannot find texture parameter with the name '" + name + "'");
  216. }
  217. else
  218. output = TGpuParamTexture<Core>(&iterFind->second, _getThisPtr());
  219. }
  220. template<bool Core>
  221. void TGpuParams<Core>::getLoadStoreTextureParam(const String& name, TGpuParamLoadStoreTexture<Core>& output) const
  222. {
  223. auto iterFind = mParamDesc->loadStoreTextures.find(name);
  224. if (iterFind == mParamDesc->loadStoreTextures.end())
  225. {
  226. output = TGpuParamLoadStoreTexture<Core>(nullptr, nullptr);
  227. LOGWRN("Cannot find texture parameter with the name '" + name + "'");
  228. }
  229. else
  230. output = TGpuParamLoadStoreTexture<Core>(&iterFind->second, _getThisPtr());
  231. }
  232. template<bool Core>
  233. void TGpuParams<Core>::getBufferParam(const String& name, TGpuParamBuffer<Core>& output) const
  234. {
  235. auto iterFind = mParamDesc->buffers.find(name);
  236. if (iterFind == mParamDesc->buffers.end())
  237. {
  238. output = TGpuParamBuffer<Core>(nullptr, nullptr);
  239. LOGWRN("Cannot find buffer parameter with the name '" + name + "'");
  240. }
  241. else
  242. output = TGpuParamBuffer<Core>(&iterFind->second, _getThisPtr());
  243. }
  244. template<bool Core>
  245. void TGpuParams<Core>::getSamplerStateParam(const String& name, TGpuParamSampState<Core>& output) const
  246. {
  247. auto iterFind = mParamDesc->samplers.find(name);
  248. if (iterFind == mParamDesc->samplers.end())
  249. {
  250. output = TGpuParamSampState<Core>(nullptr, nullptr);
  251. LOGWRN("Cannot find sampler state parameter with the name '" + name + "'");
  252. }
  253. else
  254. output = TGpuParamSampState<Core>(&iterFind->second, _getThisPtr());
  255. }
  256. template<bool Core>
  257. typename TGpuParams<Core>::ParamsBufferType TGpuParams<Core>::getParamBlockBuffer(UINT32 slot) const
  258. {
  259. if (slot < 0 || slot >= mNumParamBlocks)
  260. {
  261. BS_EXCEPT(InvalidParametersException, "Index out of range: Valid range: 0 .. " +
  262. toString(mNumParamBlocks - 1) + ". Requested: " + toString(slot));
  263. }
  264. return mParamBlockBuffers[slot];
  265. }
  266. template<bool Core>
  267. typename TGpuParams<Core>::TextureType TGpuParams<Core>::getTexture(UINT32 slot)
  268. {
  269. if (slot < 0 || slot >= mNumTextures)
  270. {
  271. BS_EXCEPT(InvalidParametersException, "Index out of range: Valid range: 0 .. " +
  272. toString(mNumTextures - 1) + ". Requested: " + toString(slot));
  273. }
  274. return mTextures[slot];
  275. }
  276. template<bool Core>
  277. typename TGpuParams<Core>::TextureType TGpuParams<Core>::getLoadStoreTexture(UINT32 slot)
  278. {
  279. if (slot < 0 || slot >= mNumLoadStoreTextures)
  280. {
  281. BS_EXCEPT(InvalidParametersException, "Index out of range: Valid range: 0 .. " +
  282. toString(mNumLoadStoreTextures - 1) + ". Requested: " + toString(slot));
  283. }
  284. return mLoadStoreTextures[slot];
  285. }
  286. template<bool Core>
  287. typename TGpuParams<Core>::BufferType TGpuParams<Core>::getBuffer(UINT32 slot)
  288. {
  289. if (slot < 0 || slot >= mNumBuffers)
  290. {
  291. BS_EXCEPT(InvalidParametersException, "Index out of range: Valid range: 0 .. " +
  292. toString(mNumBuffers - 1) + ". Requested: " + toString(slot));
  293. }
  294. return mBuffers[slot];
  295. }
  296. template<bool Core>
  297. typename TGpuParams<Core>::SamplerType TGpuParams<Core>::getSamplerState(UINT32 slot)
  298. {
  299. if (slot < 0 || slot >= mNumSamplerStates)
  300. {
  301. BS_EXCEPT(InvalidParametersException, "Index out of range: Valid range: 0 .. " +
  302. toString(mNumSamplerStates - 1) + ". Requested: " + toString(slot));
  303. }
  304. return mSamplerStates[slot];
  305. }
  306. template<bool Core>
  307. void TGpuParams<Core>::setTexture(UINT32 slot, const TextureType& texture)
  308. {
  309. if (slot < 0 || slot >= mNumTextures)
  310. {
  311. BS_EXCEPT(InvalidParametersException, "Index out of range: Valid range: 0 .. " +
  312. toString(mNumTextures - 1) + ". Requested: " + toString(slot));
  313. }
  314. mTextures[slot] = texture;
  315. _markResourcesDirty();
  316. _markCoreDirty();
  317. }
  318. template<bool Core>
  319. void TGpuParams<Core>::setLoadStoreTexture(UINT32 slot, const TextureType& texture, const TextureSurface& surface)
  320. {
  321. if (slot < 0 || slot >= mNumLoadStoreTextures)
  322. {
  323. BS_EXCEPT(InvalidParametersException, "Index out of range: Valid range: 0 .. " +
  324. toString(mNumLoadStoreTextures - 1) + ". Requested: " + toString(slot));
  325. }
  326. mLoadStoreTextures[slot] = texture;
  327. _markResourcesDirty();
  328. _markCoreDirty();
  329. }
  330. template<bool Core>
  331. void TGpuParams<Core>::setBuffer(UINT32 slot, const BufferType& buffer)
  332. {
  333. if (slot < 0 || slot >= mNumBuffers)
  334. {
  335. BS_EXCEPT(InvalidParametersException, "Index out of range: Valid range: 0 .. " +
  336. toString(mNumBuffers - 1) + ". Requested: " + toString(slot));
  337. }
  338. mBuffers[slot] = buffer;
  339. _markResourcesDirty();
  340. _markCoreDirty();
  341. }
  342. template<bool Core>
  343. void TGpuParams<Core>::setSamplerState(UINT32 slot, const SamplerType& sampler)
  344. {
  345. if (slot < 0 || slot >= mNumSamplerStates)
  346. {
  347. BS_EXCEPT(InvalidParametersException, "Index out of range: Valid range: 0 .. " +
  348. toString(mNumSamplerStates - 1) + ". Requested: " + toString(slot));
  349. }
  350. mSamplerStates[slot] = sampler;
  351. _markResourcesDirty();
  352. _markCoreDirty();
  353. }
  354. template class TGpuParams < false > ;
  355. template class TGpuParams < true > ;
  356. template BS_CORE_EXPORT void TGpuParams<false>::getParam<float>(const String&, TGpuDataParam<float, false>&) const;
  357. template BS_CORE_EXPORT void TGpuParams<false>::getParam<int>(const String&, TGpuDataParam<int, false>&) const;
  358. template BS_CORE_EXPORT void TGpuParams<false>::getParam<Color>(const String&, TGpuDataParam<Color, false>&) const;
  359. template BS_CORE_EXPORT void TGpuParams<false>::getParam<Vector2>(const String&, TGpuDataParam<Vector2, false>&) const;
  360. template BS_CORE_EXPORT void TGpuParams<false>::getParam<Vector3>(const String&, TGpuDataParam<Vector3, false>&) const;
  361. template BS_CORE_EXPORT void TGpuParams<false>::getParam<Vector4>(const String&, TGpuDataParam<Vector4, false>&) const;
  362. template BS_CORE_EXPORT void TGpuParams<false>::getParam<Vector2I>(const String&, TGpuDataParam<Vector2I, false>&) const;
  363. template BS_CORE_EXPORT void TGpuParams<false>::getParam<Vector3I>(const String&, TGpuDataParam<Vector3I, false>&) const;
  364. template BS_CORE_EXPORT void TGpuParams<false>::getParam<Vector4I>(const String&, TGpuDataParam<Vector4I, false>&) const;
  365. template BS_CORE_EXPORT void TGpuParams<false>::getParam<Matrix2>(const String&, TGpuDataParam<Matrix2, false>&) const;
  366. template BS_CORE_EXPORT void TGpuParams<false>::getParam<Matrix2x3>(const String&, TGpuDataParam<Matrix2x3, false>&) const;
  367. template BS_CORE_EXPORT void TGpuParams<false>::getParam<Matrix2x4>(const String&, TGpuDataParam<Matrix2x4, false>&) const;
  368. template BS_CORE_EXPORT void TGpuParams<false>::getParam<Matrix3>(const String&, TGpuDataParam<Matrix3, false>&) const;
  369. template BS_CORE_EXPORT void TGpuParams<false>::getParam<Matrix3x2>(const String&, TGpuDataParam<Matrix3x2, false>&) const;
  370. template BS_CORE_EXPORT void TGpuParams<false>::getParam<Matrix3x4>(const String&, TGpuDataParam<Matrix3x4, false>&) const;
  371. template BS_CORE_EXPORT void TGpuParams<false>::getParam<Matrix4>(const String&, TGpuDataParam<Matrix4, false>&) const;
  372. template BS_CORE_EXPORT void TGpuParams<false>::getParam<Matrix4x2>(const String&, TGpuDataParam<Matrix4x2, false>&) const;
  373. template BS_CORE_EXPORT void TGpuParams<false>::getParam<Matrix4x3>(const String&, TGpuDataParam<Matrix4x3, false>&) const;
  374. template BS_CORE_EXPORT void TGpuParams<true>::getParam<float>(const String&, TGpuDataParam<float, true>&) const;
  375. template BS_CORE_EXPORT void TGpuParams<true>::getParam<int>(const String&, TGpuDataParam<int, true>&) const;
  376. template BS_CORE_EXPORT void TGpuParams<true>::getParam<Color>(const String&, TGpuDataParam<Color, true>&) const;
  377. template BS_CORE_EXPORT void TGpuParams<true>::getParam<Vector2>(const String&, TGpuDataParam<Vector2, true>&) const;
  378. template BS_CORE_EXPORT void TGpuParams<true>::getParam<Vector3>(const String&, TGpuDataParam<Vector3, true>&) const;
  379. template BS_CORE_EXPORT void TGpuParams<true>::getParam<Vector4>(const String&, TGpuDataParam<Vector4, true>&) const;
  380. template BS_CORE_EXPORT void TGpuParams<true>::getParam<Vector2I>(const String&, TGpuDataParam<Vector2I, true>&) const;
  381. template BS_CORE_EXPORT void TGpuParams<true>::getParam<Vector3I>(const String&, TGpuDataParam<Vector3I, true>&) const;
  382. template BS_CORE_EXPORT void TGpuParams<true>::getParam<Vector4I>(const String&, TGpuDataParam<Vector4I, true>&) const;
  383. template BS_CORE_EXPORT void TGpuParams<true>::getParam<Matrix2>(const String&, TGpuDataParam<Matrix2, true>&) const;
  384. template BS_CORE_EXPORT void TGpuParams<true>::getParam<Matrix2x3>(const String&, TGpuDataParam<Matrix2x3, true>&) const;
  385. template BS_CORE_EXPORT void TGpuParams<true>::getParam<Matrix2x4>(const String&, TGpuDataParam<Matrix2x4, true>&) const;
  386. template BS_CORE_EXPORT void TGpuParams<true>::getParam<Matrix3>(const String&, TGpuDataParam<Matrix3, true>&) const;
  387. template BS_CORE_EXPORT void TGpuParams<true>::getParam<Matrix3x2>(const String&, TGpuDataParam<Matrix3x2, true>&) const;
  388. template BS_CORE_EXPORT void TGpuParams<true>::getParam<Matrix3x4>(const String&, TGpuDataParam<Matrix3x4, true>&) const;
  389. template BS_CORE_EXPORT void TGpuParams<true>::getParam<Matrix4>(const String&, TGpuDataParam<Matrix4, true>&) const;
  390. template BS_CORE_EXPORT void TGpuParams<true>::getParam<Matrix4x2>(const String&, TGpuDataParam<Matrix4x2, true>&) const;
  391. template BS_CORE_EXPORT void TGpuParams<true>::getParam<Matrix4x3>(const String&, TGpuDataParam<Matrix4x3, true>&) const;
  392. GpuParamsCore::GpuParamsCore(const SPtr<GpuParamDesc>& paramDesc)
  393. : TGpuParams(paramDesc)
  394. {
  395. }
  396. SPtr<GpuParamsCore> GpuParamsCore::_getThisPtr() const
  397. {
  398. return std::static_pointer_cast<GpuParamsCore>(getThisPtr());
  399. }
  400. void GpuParamsCore::syncToCore(const CoreSyncData& data)
  401. {
  402. UINT32 loadStoreSurfacesSize = mNumLoadStoreTextures * sizeof(TextureSurface);
  403. UINT32 paramBufferSize = mNumParamBlocks * sizeof(SPtr<GpuParamBlockBufferCore>);
  404. UINT32 textureArraySize = mNumTextures * sizeof(SPtr<TextureCore>);
  405. UINT32 loadStoreTextureArraySize = mNumLoadStoreTextures * sizeof(SPtr<TextureCore>);
  406. UINT32 bufferArraySize = mNumBuffers * sizeof(SPtr<GpuBufferCore>);
  407. UINT32 samplerArraySize = mNumSamplerStates * sizeof(SPtr<SamplerStateCore>);
  408. UINT32 totalSize = loadStoreSurfacesSize + paramBufferSize + textureArraySize + loadStoreTextureArraySize
  409. + bufferArraySize + samplerArraySize;
  410. UINT32 textureInfoOffset = 0;
  411. UINT32 paramBufferOffset = textureInfoOffset + loadStoreSurfacesSize;
  412. UINT32 textureArrayOffset = paramBufferOffset + paramBufferSize;
  413. UINT32 loadStoreTextureArrayOffset = textureArrayOffset + textureArraySize;
  414. UINT32 bufferArrayOffset = loadStoreTextureArrayOffset + loadStoreTextureArraySize;
  415. UINT32 samplerArrayOffset = bufferArrayOffset + bufferArraySize;
  416. assert(data.getBufferSize() == totalSize);
  417. UINT8* dataPtr = data.getBuffer();
  418. TextureSurface* loadStoreSurfaces = (TextureSurface*)(dataPtr + textureInfoOffset);
  419. SPtr<GpuParamBlockBufferCore>* paramBuffers = (SPtr<GpuParamBlockBufferCore>*)(dataPtr + paramBufferOffset);
  420. SPtr<TextureCore>* textures = (SPtr<TextureCore>*)(dataPtr + textureArrayOffset);
  421. SPtr<TextureCore>* loadStoreTextures = (SPtr<TextureCore>*)(dataPtr + loadStoreTextureArrayOffset);
  422. SPtr<GpuBufferCore>* buffers = (SPtr<GpuBufferCore>*)(dataPtr + bufferArrayOffset);
  423. SPtr<SamplerStateCore>* samplers = (SPtr<SamplerStateCore>*)(dataPtr + samplerArrayOffset);
  424. // Copy & destruct
  425. for (UINT32 i = 0; i < mNumParamBlocks; i++)
  426. {
  427. mParamBlockBuffers[i] = paramBuffers[i];
  428. paramBuffers[i].~SPtr<GpuParamBlockBufferCore>();
  429. }
  430. for (UINT32 i = 0; i < mNumTextures; i++)
  431. {
  432. mTextures[i] = textures[i];
  433. textures[i].~SPtr<TextureCore>();
  434. }
  435. for (UINT32 i = 0; i < mNumLoadStoreTextures; i++)
  436. {
  437. mLoadStoreSurfaces[i] = loadStoreSurfaces[i];
  438. loadStoreSurfaces[i].~TextureSurface();
  439. mLoadStoreTextures[i] = loadStoreTextures[i];
  440. loadStoreTextures[i].~SPtr<TextureCore>();
  441. }
  442. for (UINT32 i = 0; i < mNumBuffers; i++)
  443. {
  444. mBuffers[i] = buffers[i];
  445. buffers[i].~SPtr<GpuBufferCore>();
  446. }
  447. for (UINT32 i = 0; i < mNumSamplerStates; i++)
  448. {
  449. mSamplerStates[i] = samplers[i];
  450. samplers[i].~SPtr<SamplerStateCore>();
  451. }
  452. }
  453. SPtr<GpuParamsCore> GpuParamsCore::create(const SPtr<GpuParamDesc>& paramDesc)
  454. {
  455. GpuParamsCore* params = new (bs_alloc<GpuParamsCore>()) GpuParamsCore(paramDesc);
  456. SPtr<GpuParamsCore> paramsPtr = bs_shared_ptr<GpuParamsCore>(params);
  457. paramsPtr->_setThisPtr(paramsPtr);
  458. return paramsPtr;
  459. }
  460. const GpuDataParamInfos GpuParams::PARAM_SIZES;
  461. GpuParams::GpuParams(const SPtr<GpuParamDesc>& paramDesc)
  462. : TGpuParams(paramDesc)
  463. {
  464. }
  465. SPtr<GpuParams> GpuParams::_getThisPtr() const
  466. {
  467. return std::static_pointer_cast<GpuParams>(getThisPtr());
  468. }
  469. SPtr<GpuParamsCore> GpuParams::getCore() const
  470. {
  471. return std::static_pointer_cast<GpuParamsCore>(mCoreSpecific);
  472. }
  473. SPtr<CoreObjectCore> GpuParams::createCore() const
  474. {
  475. GpuParamsCore* obj = new (bs_alloc<GpuParamsCore>()) GpuParamsCore(mParamDesc);
  476. SPtr<CoreObjectCore> coreObj = bs_shared_ptr<GpuParamsCore>(obj);
  477. coreObj->_setThisPtr(coreObj);
  478. return coreObj;
  479. }
  480. void GpuParams::_markCoreDirty()
  481. {
  482. markCoreDirty();
  483. }
  484. void GpuParams::_markResourcesDirty()
  485. {
  486. markListenerResourcesDirty();
  487. }
  488. SPtr<GpuParams> GpuParams::create(const SPtr<GpuParamDesc>& paramDesc)
  489. {
  490. GpuParams* params = new (bs_alloc<GpuParams>()) GpuParams(paramDesc);
  491. SPtr<GpuParams> paramsPtr = bs_core_ptr<GpuParams>(params);
  492. paramsPtr->_setThisPtr(paramsPtr);
  493. paramsPtr->initialize();
  494. return paramsPtr;
  495. }
  496. CoreSyncData GpuParams::syncToCore(FrameAlloc* allocator)
  497. {
  498. UINT32 loadStoreSurfacesSize = mNumLoadStoreTextures * sizeof(TextureSurface);
  499. UINT32 paramBufferSize = mNumParamBlocks * sizeof(SPtr<GpuParamBlockBufferCore>);
  500. UINT32 textureArraySize = mNumTextures * sizeof(SPtr<TextureCore>);
  501. UINT32 loadStoreTextureArraySize = mNumLoadStoreTextures * sizeof(SPtr<TextureCore>);
  502. UINT32 bufferArraySize = mNumBuffers * sizeof(SPtr<GpuBufferCore>);
  503. UINT32 samplerArraySize = mNumSamplerStates * sizeof(SPtr<SamplerStateCore>);
  504. UINT32 totalSize = loadStoreSurfacesSize + paramBufferSize + textureArraySize + loadStoreTextureArraySize
  505. + bufferArraySize + samplerArraySize;
  506. UINT32 textureInfoOffset = 0;
  507. UINT32 paramBufferOffset = textureInfoOffset + loadStoreSurfacesSize;
  508. UINT32 textureArrayOffset = paramBufferOffset + paramBufferSize;
  509. UINT32 loadStoreTextureArrayOffset = textureArrayOffset + textureArraySize;
  510. UINT32 bufferArrayOffset = loadStoreTextureArrayOffset + loadStoreTextureArraySize;
  511. UINT32 samplerArrayOffset = bufferArrayOffset + bufferArraySize;
  512. UINT8* data = allocator->alloc(totalSize);
  513. TextureSurface* loadStoreSurfaces = (TextureSurface*)(data + textureInfoOffset);
  514. SPtr<GpuParamBlockBufferCore>* paramBuffers = (SPtr<GpuParamBlockBufferCore>*)(data + paramBufferOffset);
  515. SPtr<TextureCore>* textures = (SPtr<TextureCore>*)(data + textureArrayOffset);
  516. SPtr<TextureCore>* loadStoreTextures = (SPtr<TextureCore>*)(data + loadStoreTextureArrayOffset);
  517. SPtr<GpuBufferCore>* buffers = (SPtr<GpuBufferCore>*)(data + bufferArrayOffset);
  518. SPtr<SamplerStateCore>* samplers = (SPtr<SamplerStateCore>*)(data + samplerArrayOffset);
  519. // Construct & copy
  520. for (UINT32 i = 0; i < mNumParamBlocks; i++)
  521. {
  522. new (&paramBuffers[i]) SPtr<GpuParamBlockBufferCore>();
  523. if (mParamBlockBuffers[i] != nullptr)
  524. paramBuffers[i] = mParamBlockBuffers[i]->getCore();
  525. }
  526. for (UINT32 i = 0; i < mNumTextures; i++)
  527. {
  528. new (&textures[i]) SPtr<TextureCore>();
  529. if (mTextures[i].isLoaded())
  530. textures[i] = mTextures[i]->getCore();
  531. else
  532. textures[i] = nullptr;
  533. }
  534. for (UINT32 i = 0; i < mNumLoadStoreTextures; i++)
  535. {
  536. new (&loadStoreSurfaces[i]) TextureSurface();
  537. loadStoreSurfaces[i] = mLoadStoreSurfaces[i];
  538. new (&loadStoreTextures[i]) SPtr<TextureCore>();
  539. if (mLoadStoreTextures[i].isLoaded())
  540. loadStoreTextures[i] = mLoadStoreTextures[i]->getCore();
  541. else
  542. loadStoreTextures[i] = nullptr;
  543. }
  544. for (UINT32 i = 0; i < mNumBuffers; i++)
  545. {
  546. new (&buffers[i]) SPtr<GpuBufferCore>();
  547. if (mBuffers[i] != nullptr)
  548. buffers[i] = mBuffers[i]->getCore();
  549. else
  550. buffers[i] = nullptr;
  551. }
  552. for (UINT32 i = 0; i < mNumSamplerStates; i++)
  553. {
  554. new (&samplers[i]) SPtr<SamplerStateCore>();
  555. if (mSamplerStates[i] != nullptr)
  556. samplers[i] = mSamplerStates[i]->getCore();
  557. else
  558. samplers[i] = nullptr;
  559. }
  560. return CoreSyncData(data, totalSize);
  561. }
  562. void GpuParams::getListenerResources(Vector<HResource>& resources)
  563. {
  564. for (UINT32 i = 0; i < mNumTextures; i++)
  565. {
  566. if (mTextures[i] != nullptr)
  567. resources.push_back(mTextures[i]);
  568. }
  569. for (UINT32 i = 0; i < mNumLoadStoreTextures; i++)
  570. {
  571. if (mLoadStoreTextures[i] != nullptr)
  572. resources.push_back(mLoadStoreTextures[i]);
  573. }
  574. }
  575. }