BsGpuParams.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622
  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 SPtr<GpuParamDesc>& paramDesc, bool transposeMatrices)
  17. : mParamDesc(paramDesc), mNumParamBlocks(0), mNumTextures(0), mNumLoadStoreTextures(0), mNumSamplerStates(0)
  18. , mLoadStoreSurfaces(nullptr), mTransposeMatrices(transposeMatrices)
  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& texture : mParamDesc->loadStoreTextures)
  31. {
  32. if ((texture.second.slot + 1) > mNumLoadStoreTextures)
  33. mNumLoadStoreTextures = texture.second.slot + 1;
  34. }
  35. for (auto& sampler : mParamDesc->samplers)
  36. {
  37. if ((sampler.second.slot + 1) > mNumSamplerStates)
  38. mNumSamplerStates = sampler.second.slot + 1;
  39. }
  40. mLoadStoreSurfaces = bs_newN<TextureSurface>(mNumLoadStoreTextures);
  41. }
  42. GpuParamsBase::~GpuParamsBase()
  43. {
  44. bs_deleteN(mLoadStoreSurfaces, mNumLoadStoreTextures);
  45. }
  46. UINT32 GpuParamsBase::getDataParamSize(const String& name) const
  47. {
  48. GpuParamDataDesc* desc = getParamDesc(name);
  49. if(desc != nullptr)
  50. return desc->elementSize * 4;
  51. return 0;
  52. }
  53. bool GpuParamsBase::hasParam(const String& name) const
  54. {
  55. return getParamDesc(name) != nullptr;
  56. }
  57. bool GpuParamsBase::hasTexture(const String& name) const
  58. {
  59. auto paramIter = mParamDesc->textures.find(name);
  60. if(paramIter != mParamDesc->textures.end())
  61. return true;
  62. return false;
  63. }
  64. bool GpuParamsBase::hasLoadStoreTexture(const String& name) const
  65. {
  66. auto paramIter = mParamDesc->loadStoreTextures.find(name);
  67. if (paramIter != mParamDesc->loadStoreTextures.end())
  68. return true;
  69. return false;
  70. }
  71. bool GpuParamsBase::hasSamplerState(const String& name) const
  72. {
  73. auto paramIter = mParamDesc->samplers.find(name);
  74. if(paramIter != mParamDesc->samplers.end())
  75. return true;
  76. return false;
  77. }
  78. bool GpuParamsBase::hasParamBlock(const String& name) const
  79. {
  80. auto paramBlockIter = mParamDesc->paramBlocks.find(name);
  81. if(paramBlockIter != mParamDesc->paramBlocks.end())
  82. return true;
  83. return false;
  84. }
  85. GpuParamDataDesc* GpuParamsBase::getParamDesc(const String& name) const
  86. {
  87. auto paramIter = mParamDesc->params.find(name);
  88. if (paramIter != mParamDesc->params.end())
  89. return &paramIter->second;
  90. return nullptr;
  91. }
  92. const TextureSurface& GpuParamsBase::getLoadStoreSurface(UINT32 slot) const
  93. {
  94. if (slot >= mNumLoadStoreTextures)
  95. {
  96. BS_EXCEPT(InvalidParametersException, "Index out of range: Valid range: 0 .. " +
  97. toString(mNumLoadStoreTextures - 1) + ". Requested: " + toString(slot));
  98. }
  99. return mLoadStoreSurfaces[slot];
  100. }
  101. void GpuParamsBase::setLoadStoreSurface(UINT32 slot, const TextureSurface& surface) const
  102. {
  103. if (slot >= mNumLoadStoreTextures)
  104. {
  105. BS_EXCEPT(InvalidParametersException, "Index out of range: Valid range: 0 .. " +
  106. toString(mNumLoadStoreTextures - 1) + ". Requested: " + toString(slot));
  107. }
  108. mLoadStoreSurfaces[slot] = surface;
  109. }
  110. template<bool Core>
  111. TGpuParams<Core>::TGpuParams(const SPtr<GpuParamDesc>& paramDesc, bool transposeMatrices)
  112. : GpuParamsBase(paramDesc, transposeMatrices), mParamBlockBuffers(nullptr), mTextures(nullptr)
  113. , mLoadStoreTextures(nullptr), mSamplerStates(nullptr)
  114. {
  115. if (mNumParamBlocks > 0)
  116. mParamBlockBuffers = bs_newN<ParamsBufferType>(mNumParamBlocks);
  117. if (mNumTextures > 0)
  118. mTextures = bs_newN<TextureType>(mNumTextures);
  119. if (mNumLoadStoreTextures > 0)
  120. mLoadStoreTextures = bs_newN<TextureType>(mNumLoadStoreTextures);
  121. if (mNumSamplerStates > 0)
  122. mSamplerStates = bs_newN<SamplerType>(mNumSamplerStates);
  123. }
  124. template<bool Core>
  125. TGpuParams<Core>::~TGpuParams()
  126. {
  127. if (mParamBlockBuffers != nullptr)
  128. bs_deleteN(mParamBlockBuffers, mNumParamBlocks);
  129. if (mTextures != nullptr)
  130. bs_deleteN(mTextures, mNumTextures);
  131. if (mLoadStoreTextures != nullptr)
  132. bs_deleteN(mLoadStoreTextures, mNumLoadStoreTextures);
  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>(nullptr, 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>(nullptr, 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>(nullptr, 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->loadStoreTextures.find(name);
  200. if (iterFind == mParamDesc->loadStoreTextures.end())
  201. {
  202. output = TGpuParamLoadStoreTexture<Core>(nullptr, 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>(nullptr, 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>::TextureType TGpuParams<Core>::getLoadStoreTexture(UINT32 slot)
  242. {
  243. if (slot < 0 || slot >= mNumLoadStoreTextures)
  244. {
  245. BS_EXCEPT(InvalidParametersException, "Index out of range: Valid range: 0 .. " +
  246. toString(mNumLoadStoreTextures - 1) + ". Requested: " + toString(slot));
  247. }
  248. return mLoadStoreTextures[slot];
  249. }
  250. template<bool Core>
  251. typename TGpuParams<Core>::SamplerType TGpuParams<Core>::getSamplerState(UINT32 slot)
  252. {
  253. if (slot < 0 || slot >= mNumSamplerStates)
  254. {
  255. BS_EXCEPT(InvalidParametersException, "Index out of range: Valid range: 0 .. " +
  256. toString(mNumSamplerStates - 1) + ". Requested: " + toString(slot));
  257. }
  258. return mSamplerStates[slot];
  259. }
  260. template<bool Core>
  261. void TGpuParams<Core>::setTexture(UINT32 slot, const TextureType& texture)
  262. {
  263. if (slot < 0 || slot >= mNumTextures)
  264. {
  265. BS_EXCEPT(InvalidParametersException, "Index out of range: Valid range: 0 .. " +
  266. toString(mNumTextures - 1) + ". Requested: " + toString(slot));
  267. }
  268. mTextures[slot] = texture;
  269. _markResourcesDirty();
  270. _markCoreDirty();
  271. }
  272. template<bool Core>
  273. void TGpuParams<Core>::setLoadStoreTexture(UINT32 slot, const TextureType& texture, const TextureSurface& surface)
  274. {
  275. if (slot < 0 || slot >= mNumLoadStoreTextures)
  276. {
  277. BS_EXCEPT(InvalidParametersException, "Index out of range: Valid range: 0 .. " +
  278. toString(mNumLoadStoreTextures - 1) + ". Requested: " + toString(slot));
  279. }
  280. mLoadStoreTextures[slot] = texture;
  281. _markResourcesDirty();
  282. _markCoreDirty();
  283. }
  284. template<bool Core>
  285. void TGpuParams<Core>::setSamplerState(UINT32 slot, const SamplerType& sampler)
  286. {
  287. if (slot < 0 || slot >= mNumSamplerStates)
  288. {
  289. BS_EXCEPT(InvalidParametersException, "Index out of range: Valid range: 0 .. " +
  290. toString(mNumSamplerStates - 1) + ". Requested: " + toString(slot));
  291. }
  292. mSamplerStates[slot] = sampler;
  293. _markResourcesDirty();
  294. _markCoreDirty();
  295. }
  296. template class TGpuParams < false > ;
  297. template class TGpuParams < true > ;
  298. template BS_CORE_EXPORT void TGpuParams<false>::getParam<float>(const String&, TGpuDataParam<float, false>&) const;
  299. template BS_CORE_EXPORT void TGpuParams<false>::getParam<int>(const String&, TGpuDataParam<int, false>&) const;
  300. template BS_CORE_EXPORT void TGpuParams<false>::getParam<Color>(const String&, TGpuDataParam<Color, false>&) const;
  301. template BS_CORE_EXPORT void TGpuParams<false>::getParam<Vector2>(const String&, TGpuDataParam<Vector2, false>&) const;
  302. template BS_CORE_EXPORT void TGpuParams<false>::getParam<Vector3>(const String&, TGpuDataParam<Vector3, false>&) const;
  303. template BS_CORE_EXPORT void TGpuParams<false>::getParam<Vector4>(const String&, TGpuDataParam<Vector4, false>&) const;
  304. template BS_CORE_EXPORT void TGpuParams<false>::getParam<Vector2I>(const String&, TGpuDataParam<Vector2I, false>&) const;
  305. template BS_CORE_EXPORT void TGpuParams<false>::getParam<Vector3I>(const String&, TGpuDataParam<Vector3I, false>&) const;
  306. template BS_CORE_EXPORT void TGpuParams<false>::getParam<Vector4I>(const String&, TGpuDataParam<Vector4I, false>&) const;
  307. template BS_CORE_EXPORT void TGpuParams<false>::getParam<Matrix2>(const String&, TGpuDataParam<Matrix2, false>&) const;
  308. template BS_CORE_EXPORT void TGpuParams<false>::getParam<Matrix2x3>(const String&, TGpuDataParam<Matrix2x3, false>&) const;
  309. template BS_CORE_EXPORT void TGpuParams<false>::getParam<Matrix2x4>(const String&, TGpuDataParam<Matrix2x4, false>&) const;
  310. template BS_CORE_EXPORT void TGpuParams<false>::getParam<Matrix3>(const String&, TGpuDataParam<Matrix3, false>&) const;
  311. template BS_CORE_EXPORT void TGpuParams<false>::getParam<Matrix3x2>(const String&, TGpuDataParam<Matrix3x2, false>&) const;
  312. template BS_CORE_EXPORT void TGpuParams<false>::getParam<Matrix3x4>(const String&, TGpuDataParam<Matrix3x4, false>&) const;
  313. template BS_CORE_EXPORT void TGpuParams<false>::getParam<Matrix4>(const String&, TGpuDataParam<Matrix4, false>&) const;
  314. template BS_CORE_EXPORT void TGpuParams<false>::getParam<Matrix4x2>(const String&, TGpuDataParam<Matrix4x2, false>&) const;
  315. template BS_CORE_EXPORT void TGpuParams<false>::getParam<Matrix4x3>(const String&, TGpuDataParam<Matrix4x3, false>&) const;
  316. template BS_CORE_EXPORT void TGpuParams<true>::getParam<float>(const String&, TGpuDataParam<float, true>&) const;
  317. template BS_CORE_EXPORT void TGpuParams<true>::getParam<int>(const String&, TGpuDataParam<int, true>&) const;
  318. template BS_CORE_EXPORT void TGpuParams<true>::getParam<Color>(const String&, TGpuDataParam<Color, true>&) const;
  319. template BS_CORE_EXPORT void TGpuParams<true>::getParam<Vector2>(const String&, TGpuDataParam<Vector2, true>&) const;
  320. template BS_CORE_EXPORT void TGpuParams<true>::getParam<Vector3>(const String&, TGpuDataParam<Vector3, true>&) const;
  321. template BS_CORE_EXPORT void TGpuParams<true>::getParam<Vector4>(const String&, TGpuDataParam<Vector4, true>&) const;
  322. template BS_CORE_EXPORT void TGpuParams<true>::getParam<Vector2I>(const String&, TGpuDataParam<Vector2I, true>&) const;
  323. template BS_CORE_EXPORT void TGpuParams<true>::getParam<Vector3I>(const String&, TGpuDataParam<Vector3I, true>&) const;
  324. template BS_CORE_EXPORT void TGpuParams<true>::getParam<Vector4I>(const String&, TGpuDataParam<Vector4I, true>&) const;
  325. template BS_CORE_EXPORT void TGpuParams<true>::getParam<Matrix2>(const String&, TGpuDataParam<Matrix2, true>&) const;
  326. template BS_CORE_EXPORT void TGpuParams<true>::getParam<Matrix2x3>(const String&, TGpuDataParam<Matrix2x3, true>&) const;
  327. template BS_CORE_EXPORT void TGpuParams<true>::getParam<Matrix2x4>(const String&, TGpuDataParam<Matrix2x4, true>&) const;
  328. template BS_CORE_EXPORT void TGpuParams<true>::getParam<Matrix3>(const String&, TGpuDataParam<Matrix3, true>&) const;
  329. template BS_CORE_EXPORT void TGpuParams<true>::getParam<Matrix3x2>(const String&, TGpuDataParam<Matrix3x2, true>&) const;
  330. template BS_CORE_EXPORT void TGpuParams<true>::getParam<Matrix3x4>(const String&, TGpuDataParam<Matrix3x4, true>&) const;
  331. template BS_CORE_EXPORT void TGpuParams<true>::getParam<Matrix4>(const String&, TGpuDataParam<Matrix4, true>&) const;
  332. template BS_CORE_EXPORT void TGpuParams<true>::getParam<Matrix4x2>(const String&, TGpuDataParam<Matrix4x2, true>&) const;
  333. template BS_CORE_EXPORT void TGpuParams<true>::getParam<Matrix4x3>(const String&, TGpuDataParam<Matrix4x3, true>&) const;
  334. GpuParamsCore::GpuParamsCore(const SPtr<GpuParamDesc>& paramDesc, bool transposeMatrices)
  335. : TGpuParams(paramDesc, transposeMatrices)
  336. {
  337. }
  338. SPtr<GpuParamsCore> GpuParamsCore::_getThisPtr() const
  339. {
  340. return std::static_pointer_cast<GpuParamsCore>(getThisPtr());
  341. }
  342. void GpuParamsCore::updateHardwareBuffers()
  343. {
  344. for (UINT32 i = 0; i < mNumParamBlocks; i++)
  345. {
  346. if (mParamBlockBuffers[i] != nullptr)
  347. {
  348. mParamBlockBuffers[i]->flushToGPU();
  349. }
  350. }
  351. }
  352. void GpuParamsCore::syncToCore(const CoreSyncData& data)
  353. {
  354. UINT32 loadStoreSurfacesSize = mNumLoadStoreTextures * sizeof(TextureSurface);
  355. UINT32 paramBufferSize = mNumParamBlocks * sizeof(SPtr<GpuParamBlockBufferCore>);
  356. UINT32 textureArraySize = mNumTextures * sizeof(SPtr<TextureCore>);
  357. UINT32 loadStoreTextureArraySize = mNumLoadStoreTextures * sizeof(SPtr<TextureCore>);
  358. UINT32 samplerArraySize = mNumSamplerStates * sizeof(SPtr<SamplerStateCore>);
  359. UINT32 totalSize = loadStoreSurfacesSize + paramBufferSize + textureArraySize + loadStoreTextureArraySize
  360. + samplerArraySize;
  361. UINT32 textureInfoOffset = 0;
  362. UINT32 paramBufferOffset = textureInfoOffset + loadStoreSurfacesSize;
  363. UINT32 textureArrayOffset = paramBufferOffset + paramBufferSize;
  364. UINT32 loadStoreTextureArrayOffset = textureArrayOffset + textureArraySize;
  365. UINT32 samplerArrayOffset = loadStoreTextureArrayOffset + loadStoreTextureArraySize;
  366. assert(data.getBufferSize() == totalSize);
  367. UINT8* dataPtr = data.getBuffer();
  368. TextureSurface* loadStoreSurfaces = (TextureSurface*)(dataPtr + textureInfoOffset);
  369. SPtr<GpuParamBlockBufferCore>* paramBuffers = (SPtr<GpuParamBlockBufferCore>*)(dataPtr + paramBufferOffset);
  370. SPtr<TextureCore>* textures = (SPtr<TextureCore>*)(dataPtr + textureArrayOffset);
  371. SPtr<TextureCore>* loadStoreTextures = (SPtr<TextureCore>*)(dataPtr + loadStoreTextureArrayOffset);
  372. SPtr<SamplerStateCore>* samplers = (SPtr<SamplerStateCore>*)(dataPtr + samplerArrayOffset);
  373. // Copy & destruct
  374. for (UINT32 i = 0; i < mNumParamBlocks; i++)
  375. {
  376. mParamBlockBuffers[i] = paramBuffers[i];
  377. paramBuffers[i].~SPtr<GpuParamBlockBufferCore>();
  378. }
  379. for (UINT32 i = 0; i < mNumTextures; i++)
  380. {
  381. mTextures[i] = textures[i];
  382. textures[i].~SPtr<TextureCore>();
  383. }
  384. for (UINT32 i = 0; i < mNumLoadStoreTextures; i++)
  385. {
  386. mLoadStoreSurfaces[i] = loadStoreSurfaces[i];
  387. loadStoreSurfaces[i].~TextureSurface();
  388. mLoadStoreTextures[i] = loadStoreTextures[i];
  389. loadStoreTextures[i].~SPtr<TextureCore>();
  390. }
  391. for (UINT32 i = 0; i < mNumSamplerStates; i++)
  392. {
  393. mSamplerStates[i] = samplers[i];
  394. samplers[i].~SPtr<SamplerStateCore>();
  395. }
  396. }
  397. SPtr<GpuParamsCore> GpuParamsCore::create(const SPtr<GpuParamDesc>& paramDesc, bool transposeMatrices)
  398. {
  399. GpuParamsCore* params = new (bs_alloc<GpuParamsCore>()) GpuParamsCore(paramDesc, transposeMatrices);
  400. SPtr<GpuParamsCore> paramsPtr = bs_shared_ptr<GpuParamsCore>(params);
  401. paramsPtr->_setThisPtr(paramsPtr);
  402. return paramsPtr;
  403. }
  404. const GpuDataParamInfos GpuParams::PARAM_SIZES;
  405. GpuParams::GpuParams(const SPtr<GpuParamDesc>& paramDesc, bool transposeMatrices)
  406. : TGpuParams(paramDesc, transposeMatrices)
  407. {
  408. }
  409. SPtr<GpuParams> GpuParams::_getThisPtr() const
  410. {
  411. return std::static_pointer_cast<GpuParams>(getThisPtr());
  412. }
  413. SPtr<GpuParamsCore> GpuParams::getCore() const
  414. {
  415. return std::static_pointer_cast<GpuParamsCore>(mCoreSpecific);
  416. }
  417. SPtr<CoreObjectCore> GpuParams::createCore() const
  418. {
  419. GpuParamsCore* obj = new (bs_alloc<GpuParamsCore>()) GpuParamsCore(mParamDesc, mTransposeMatrices);
  420. SPtr<CoreObjectCore> coreObj = bs_shared_ptr<GpuParamsCore>(obj);
  421. coreObj->_setThisPtr(coreObj);
  422. return coreObj;
  423. }
  424. void GpuParams::_markCoreDirty()
  425. {
  426. markCoreDirty();
  427. }
  428. void GpuParams::_markResourcesDirty()
  429. {
  430. markListenerResourcesDirty();
  431. }
  432. SPtr<GpuParams> GpuParams::create(const SPtr<GpuParamDesc>& paramDesc, bool transposeMatrices)
  433. {
  434. GpuParams* params = new (bs_alloc<GpuParams>()) GpuParams(paramDesc, transposeMatrices);
  435. SPtr<GpuParams> paramsPtr = bs_core_ptr<GpuParams>(params);
  436. paramsPtr->_setThisPtr(paramsPtr);
  437. paramsPtr->initialize();
  438. return paramsPtr;
  439. }
  440. CoreSyncData GpuParams::syncToCore(FrameAlloc* allocator)
  441. {
  442. UINT32 loadStoreSurfacesSize = mNumLoadStoreTextures * sizeof(TextureSurface);
  443. UINT32 paramBufferSize = mNumParamBlocks * sizeof(SPtr<GpuParamBlockBufferCore>);
  444. UINT32 textureArraySize = mNumTextures * sizeof(SPtr<TextureCore>);
  445. UINT32 loadStoreTextureArraySize = mNumLoadStoreTextures * sizeof(SPtr<TextureCore>);
  446. UINT32 samplerArraySize = mNumSamplerStates * sizeof(SPtr<SamplerStateCore>);
  447. UINT32 totalSize = loadStoreSurfacesSize + paramBufferSize + textureArraySize + loadStoreTextureArraySize
  448. + samplerArraySize;
  449. UINT32 textureInfoOffset = 0;
  450. UINT32 paramBufferOffset = textureInfoOffset + loadStoreSurfacesSize;
  451. UINT32 textureArrayOffset = paramBufferOffset + paramBufferSize;
  452. UINT32 loadStoreTextureArrayOffset = textureArrayOffset + textureArraySize;
  453. UINT32 samplerArrayOffset = loadStoreTextureArrayOffset + loadStoreTextureArraySize;
  454. UINT8* data = allocator->alloc(totalSize);
  455. TextureSurface* loadStoreSurfaces = (TextureSurface*)(data + textureInfoOffset);
  456. SPtr<GpuParamBlockBufferCore>* paramBuffers = (SPtr<GpuParamBlockBufferCore>*)(data + paramBufferOffset);
  457. SPtr<TextureCore>* textures = (SPtr<TextureCore>*)(data + textureArrayOffset);
  458. SPtr<TextureCore>* loadStoreTextures = (SPtr<TextureCore>*)(data + loadStoreTextureArrayOffset);
  459. SPtr<SamplerStateCore>* samplers = (SPtr<SamplerStateCore>*)(data + samplerArrayOffset);
  460. // Construct & copy
  461. for (UINT32 i = 0; i < mNumParamBlocks; i++)
  462. {
  463. new (&paramBuffers[i]) SPtr<GpuParamBlockBufferCore>();
  464. if (mParamBlockBuffers[i] != nullptr)
  465. paramBuffers[i] = mParamBlockBuffers[i]->getCore();
  466. }
  467. for (UINT32 i = 0; i < mNumTextures; i++)
  468. {
  469. new (&textures[i]) SPtr<TextureCore>();
  470. if (mTextures[i].isLoaded())
  471. textures[i] = mTextures[i]->getCore();
  472. else
  473. textures[i] = nullptr;
  474. }
  475. for (UINT32 i = 0; i < mNumLoadStoreTextures; i++)
  476. {
  477. new (&loadStoreSurfaces[i]) TextureSurface();
  478. loadStoreSurfaces[i] = mLoadStoreSurfaces[i];
  479. new (&loadStoreTextures[i]) SPtr<TextureCore>();
  480. if (mLoadStoreTextures[i].isLoaded())
  481. loadStoreTextures[i] = mLoadStoreTextures[i]->getCore();
  482. else
  483. loadStoreTextures[i] = nullptr;
  484. }
  485. for (UINT32 i = 0; i < mNumSamplerStates; i++)
  486. {
  487. new (&samplers[i]) SPtr<SamplerStateCore>();
  488. if (mSamplerStates[i] != nullptr)
  489. samplers[i] = mSamplerStates[i]->getCore();
  490. else
  491. samplers[i] = nullptr;
  492. }
  493. return CoreSyncData(data, totalSize);
  494. }
  495. void GpuParams::getListenerResources(Vector<HResource>& resources)
  496. {
  497. for (UINT32 i = 0; i < mNumTextures; i++)
  498. {
  499. if (mTextures[i] != nullptr)
  500. resources.push_back(mTextures[i]);
  501. }
  502. for (UINT32 i = 0; i < mNumLoadStoreTextures; i++)
  503. {
  504. if (mLoadStoreTextures[i] != nullptr)
  505. resources.push_back(mLoadStoreTextures[i]);
  506. }
  507. }
  508. }