BsGpuParams.cpp 32 KB

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