BsGpuParams.cpp 32 KB

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