BsGpuParams.cpp 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803
  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. const GpuDataParamInfos GpuParams::PARAM_SIZES;
  454. GpuParams::GpuParams(const SPtr<GpuPipelineParamInfo>& paramInfo)
  455. : TGpuParams(paramInfo)
  456. {
  457. }
  458. SPtr<GpuParams> GpuParams::_getThisPtr() const
  459. {
  460. return std::static_pointer_cast<GpuParams>(getThisPtr());
  461. }
  462. SPtr<ct::GpuParams> GpuParams::getCore() const
  463. {
  464. return std::static_pointer_cast<ct::GpuParams>(mCoreSpecific);
  465. }
  466. SPtr<ct::CoreObject> GpuParams::createCore() const
  467. {
  468. SPtr<GpuPipelineParamInfo> paramInfo = std::static_pointer_cast<GpuPipelineParamInfo>(mParamInfo);
  469. return ct::HardwareBufferManager::instance().createGpuParams(paramInfo->getCore());
  470. }
  471. void GpuParams::_markCoreDirty()
  472. {
  473. markCoreDirty();
  474. }
  475. void GpuParams::_markResourcesDirty()
  476. {
  477. markListenerResourcesDirty();
  478. }
  479. SPtr<GpuParams> GpuParams::create(const SPtr<GraphicsPipelineState>& pipelineState)
  480. {
  481. return HardwareBufferManager::instance().createGpuParams(pipelineState->getParamInfo());
  482. }
  483. SPtr<GpuParams> GpuParams::create(const SPtr<ComputePipelineState>& pipelineState)
  484. {
  485. return HardwareBufferManager::instance().createGpuParams(pipelineState->getParamInfo());
  486. }
  487. SPtr<GpuParams> GpuParams::create(const SPtr<GpuPipelineParamInfo>& paramInfo)
  488. {
  489. return HardwareBufferManager::instance().createGpuParams(paramInfo);
  490. }
  491. CoreSyncData GpuParams::syncToCore(FrameAlloc* allocator)
  492. {
  493. UINT32 numParamBlocks = mParamInfo->getNumElements(GpuPipelineParamInfo::ParamType::ParamBlock);
  494. UINT32 numTextures = mParamInfo->getNumElements(GpuPipelineParamInfo::ParamType::Texture);
  495. UINT32 numStorageTextures = mParamInfo->getNumElements(GpuPipelineParamInfo::ParamType::LoadStoreTexture);
  496. UINT32 numBuffers = mParamInfo->getNumElements(GpuPipelineParamInfo::ParamType::Buffer);
  497. UINT32 numSamplers = mParamInfo->getNumElements(GpuPipelineParamInfo::ParamType::SamplerState);
  498. UINT32 loadStoreSurfacesSize = numStorageTextures * sizeof(TextureSurface);
  499. UINT32 paramBufferSize = numParamBlocks * sizeof(SPtr<ct::GpuParamBlockBuffer>);
  500. UINT32 textureArraySize = numTextures * sizeof(SPtr<ct::Texture>);
  501. UINT32 loadStoreTextureArraySize = numStorageTextures * sizeof(SPtr<ct::Texture>);
  502. UINT32 bufferArraySize = numBuffers * sizeof(SPtr<ct::GpuBuffer>);
  503. UINT32 samplerArraySize = numSamplers * sizeof(SPtr<ct::SamplerState>);
  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<ct::GpuParamBlockBuffer>* paramBuffers = (SPtr<ct::GpuParamBlockBuffer>*)(data + paramBufferOffset);
  515. SPtr<ct::Texture>* textures = (SPtr<ct::Texture>*)(data + textureArrayOffset);
  516. SPtr<ct::Texture>* loadStoreTextures = (SPtr<ct::Texture>*)(data + loadStoreTextureArrayOffset);
  517. SPtr<ct::GpuBuffer>* buffers = (SPtr<ct::GpuBuffer>*)(data + bufferArrayOffset);
  518. SPtr<ct::SamplerState>* samplers = (SPtr<ct::SamplerState>*)(data + samplerArrayOffset);
  519. // Construct & copy
  520. for (UINT32 i = 0; i < numParamBlocks; i++)
  521. {
  522. new (&paramBuffers[i]) SPtr<ct::GpuParamBlockBuffer>();
  523. if (mParamBlockBuffers[i] != nullptr)
  524. paramBuffers[i] = mParamBlockBuffers[i]->getCore();
  525. }
  526. for (UINT32 i = 0; i < numTextures; i++)
  527. {
  528. new (&textures[i]) SPtr<ct::Texture>();
  529. if (mTextures[i].isLoaded())
  530. textures[i] = mTextures[i]->getCore();
  531. else
  532. textures[i] = nullptr;
  533. }
  534. for (UINT32 i = 0; i < numStorageTextures; i++)
  535. {
  536. new (&loadStoreSurfaces[i]) TextureSurface();
  537. loadStoreSurfaces[i] = mLoadStoreSurfaces[i];
  538. new (&loadStoreTextures[i]) SPtr<ct::Texture>();
  539. if (mLoadStoreTextures[i].isLoaded())
  540. loadStoreTextures[i] = mLoadStoreTextures[i]->getCore();
  541. else
  542. loadStoreTextures[i] = nullptr;
  543. }
  544. for (UINT32 i = 0; i < numBuffers; i++)
  545. {
  546. new (&buffers[i]) SPtr<ct::GpuBuffer>();
  547. if (mBuffers[i] != nullptr)
  548. buffers[i] = mBuffers[i]->getCore();
  549. else
  550. buffers[i] = nullptr;
  551. }
  552. for (UINT32 i = 0; i < numSamplers; i++)
  553. {
  554. new (&samplers[i]) SPtr<ct::SamplerState>();
  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. UINT32 numTextures = mParamInfo->getNumElements(GpuPipelineParamInfo::ParamType::Texture);
  565. UINT32 numStorageTextures = mParamInfo->getNumElements(GpuPipelineParamInfo::ParamType::LoadStoreTexture);
  566. for (UINT32 i = 0; i < numTextures; i++)
  567. {
  568. if (mTextures[i] != nullptr)
  569. resources.push_back(mTextures[i]);
  570. }
  571. for (UINT32 i = 0; i < numStorageTextures; i++)
  572. {
  573. if (mLoadStoreTextures[i] != nullptr)
  574. resources.push_back(mLoadStoreTextures[i]);
  575. }
  576. }
  577. namespace ct
  578. {
  579. GpuParams::GpuParams(const SPtr<GpuPipelineParamInfo>& paramInfo, GpuDeviceFlags deviceMask)
  580. : TGpuParams(paramInfo)
  581. {
  582. }
  583. SPtr<GpuParams> GpuParams::_getThisPtr() const
  584. {
  585. return std::static_pointer_cast<GpuParams>(getThisPtr());
  586. }
  587. void GpuParams::syncToCore(const CoreSyncData& data)
  588. {
  589. UINT32 numParamBlocks = mParamInfo->getNumElements(GpuPipelineParamInfo::ParamType::ParamBlock);
  590. UINT32 numTextures = mParamInfo->getNumElements(GpuPipelineParamInfo::ParamType::Texture);
  591. UINT32 numStorageTextures = mParamInfo->getNumElements(GpuPipelineParamInfo::ParamType::LoadStoreTexture);
  592. UINT32 numBuffers = mParamInfo->getNumElements(GpuPipelineParamInfo::ParamType::Buffer);
  593. UINT32 numSamplers = mParamInfo->getNumElements(GpuPipelineParamInfo::ParamType::SamplerState);
  594. UINT32 loadStoreSurfacesSize = numStorageTextures * sizeof(TextureSurface);
  595. UINT32 paramBufferSize = numParamBlocks * sizeof(SPtr<GpuParamBlockBuffer>);
  596. UINT32 textureArraySize = numTextures * sizeof(SPtr<Texture>);
  597. UINT32 loadStoreTextureArraySize = numStorageTextures * sizeof(SPtr<Texture>);
  598. UINT32 bufferArraySize = numBuffers * sizeof(SPtr<GpuBuffer>);
  599. UINT32 samplerArraySize = numSamplers * sizeof(SPtr<SamplerState>);
  600. UINT32 totalSize = loadStoreSurfacesSize + paramBufferSize + textureArraySize + loadStoreTextureArraySize
  601. + bufferArraySize + samplerArraySize;
  602. UINT32 textureInfoOffset = 0;
  603. UINT32 paramBufferOffset = textureInfoOffset + loadStoreSurfacesSize;
  604. UINT32 textureArrayOffset = paramBufferOffset + paramBufferSize;
  605. UINT32 loadStoreTextureArrayOffset = textureArrayOffset + textureArraySize;
  606. UINT32 bufferArrayOffset = loadStoreTextureArrayOffset + loadStoreTextureArraySize;
  607. UINT32 samplerArrayOffset = bufferArrayOffset + bufferArraySize;
  608. assert(data.getBufferSize() == totalSize);
  609. UINT8* dataPtr = data.getBuffer();
  610. TextureSurface* loadStoreSurfaces = (TextureSurface*)(dataPtr + textureInfoOffset);
  611. SPtr<GpuParamBlockBuffer>* paramBuffers = (SPtr<GpuParamBlockBuffer>*)(dataPtr + paramBufferOffset);
  612. SPtr<Texture>* textures = (SPtr<Texture>*)(dataPtr + textureArrayOffset);
  613. SPtr<Texture>* loadStoreTextures = (SPtr<Texture>*)(dataPtr + loadStoreTextureArrayOffset);
  614. SPtr<GpuBuffer>* buffers = (SPtr<GpuBuffer>*)(dataPtr + bufferArrayOffset);
  615. SPtr<SamplerState>* samplers = (SPtr<SamplerState>*)(dataPtr + samplerArrayOffset);
  616. // Copy & destruct
  617. for (UINT32 i = 0; i < numParamBlocks; i++)
  618. {
  619. mParamBlockBuffers[i] = paramBuffers[i];
  620. paramBuffers[i].~SPtr<GpuParamBlockBuffer>();
  621. }
  622. for (UINT32 i = 0; i < numTextures; i++)
  623. {
  624. mTextures[i] = textures[i];
  625. textures[i].~SPtr<Texture>();
  626. }
  627. for (UINT32 i = 0; i < numStorageTextures; i++)
  628. {
  629. mLoadStoreSurfaces[i] = loadStoreSurfaces[i];
  630. loadStoreSurfaces[i].~TextureSurface();
  631. mLoadStoreTextures[i] = loadStoreTextures[i];
  632. loadStoreTextures[i].~SPtr<Texture>();
  633. }
  634. for (UINT32 i = 0; i < numBuffers; i++)
  635. {
  636. mBuffers[i] = buffers[i];
  637. buffers[i].~SPtr<GpuBuffer>();
  638. }
  639. for (UINT32 i = 0; i < numSamplers; i++)
  640. {
  641. mSamplerStates[i] = samplers[i];
  642. samplers[i].~SPtr<SamplerState>();
  643. }
  644. }
  645. SPtr<GpuParams> GpuParams::create(const SPtr<GraphicsPipelineState>& pipelineState, GpuDeviceFlags deviceMask)
  646. {
  647. return HardwareBufferManager::instance().createGpuParams(pipelineState->getParamInfo(), deviceMask);
  648. }
  649. SPtr<GpuParams> GpuParams::create(const SPtr<ComputePipelineState>& pipelineState, GpuDeviceFlags deviceMask)
  650. {
  651. return HardwareBufferManager::instance().createGpuParams(pipelineState->getParamInfo(), deviceMask);
  652. }
  653. SPtr<GpuParams> GpuParams::create(const SPtr<GpuPipelineParamInfo>& paramInfo, GpuDeviceFlags deviceMask)
  654. {
  655. return HardwareBufferManager::instance().createGpuParams(paramInfo, deviceMask);
  656. }
  657. }
  658. }