BsGpuParams.cpp 30 KB

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