BsGpuParams.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571
  1. #include "BsGpuParams.h"
  2. #include "BsGpuParamDesc.h"
  3. #include "BsGpuParamBlockBuffer.h"
  4. #include "BsVector2.h"
  5. #include "BsTexture.h"
  6. #include "BsSamplerState.h"
  7. #include "BsFrameAlloc.h"
  8. #include "BsDebug.h"
  9. #include "BsException.h"
  10. #include "BsVectorNI.h"
  11. #include "BsMatrixNxM.h"
  12. namespace BansheeEngine
  13. {
  14. GpuParamsBase::GpuParamsBase(const GpuParamDescPtr& paramDesc, bool transposeMatrices)
  15. :mParamDesc(paramDesc), mTransposeMatrices(transposeMatrices), mNumParamBlocks(0), mNumSamplerStates(0),
  16. mNumTextures(0), mTextureInfo(nullptr)
  17. {
  18. for (auto& paramBlock : mParamDesc->paramBlocks)
  19. {
  20. if ((paramBlock.second.slot + 1) > mNumParamBlocks)
  21. mNumParamBlocks = paramBlock.second.slot + 1;
  22. }
  23. for (auto& texture : mParamDesc->textures)
  24. {
  25. if ((texture.second.slot + 1) > mNumTextures)
  26. mNumTextures = texture.second.slot + 1;
  27. }
  28. for (auto& sampler : mParamDesc->samplers)
  29. {
  30. if ((sampler.second.slot + 1) > mNumSamplerStates)
  31. mNumSamplerStates = sampler.second.slot + 1;
  32. }
  33. mTextureInfo = bs_newN<BoundTextureInfo>(mNumTextures);
  34. }
  35. GpuParamsBase::~GpuParamsBase()
  36. {
  37. bs_deleteN(mTextureInfo, mNumTextures);
  38. }
  39. UINT32 GpuParamsBase::getDataParamSize(const String& name) const
  40. {
  41. GpuParamDataDesc* desc = getParamDesc(name);
  42. if(desc != nullptr)
  43. return desc->elementSize * 4;
  44. return 0;
  45. }
  46. bool GpuParamsBase::hasParam(const String& name) const
  47. {
  48. return getParamDesc(name) != nullptr;
  49. }
  50. bool GpuParamsBase::hasTexture(const String& name) const
  51. {
  52. auto paramIter = mParamDesc->textures.find(name);
  53. if(paramIter != mParamDesc->textures.end())
  54. return true;
  55. return false;
  56. }
  57. bool GpuParamsBase::hasSamplerState(const String& name) const
  58. {
  59. auto paramIter = mParamDesc->samplers.find(name);
  60. if(paramIter != mParamDesc->samplers.end())
  61. return true;
  62. return false;
  63. }
  64. bool GpuParamsBase::hasParamBlock(const String& name) const
  65. {
  66. auto paramBlockIter = mParamDesc->paramBlocks.find(name);
  67. if(paramBlockIter != mParamDesc->paramBlocks.end())
  68. return true;
  69. return false;
  70. }
  71. GpuParamDataDesc* GpuParamsBase::getParamDesc(const String& name) const
  72. {
  73. auto paramIter = mParamDesc->params.find(name);
  74. if (paramIter != mParamDesc->params.end())
  75. return &paramIter->second;
  76. return nullptr;
  77. }
  78. bool GpuParamsBase::isLoadStoreTexture(UINT32 slot) const
  79. {
  80. if (slot < 0 || slot >= mNumTextures)
  81. {
  82. BS_EXCEPT(InvalidParametersException, "Index out of range: Valid range: 0 .. " +
  83. toString(mNumTextures - 1) + ". Requested: " + toString(slot));
  84. }
  85. return mTextureInfo[slot].isLoadStore;
  86. }
  87. void GpuParamsBase::setIsLoadStoreTexture(UINT32 slot, bool isLoadStore)
  88. {
  89. if (slot < 0 || slot >= mNumTextures)
  90. {
  91. BS_EXCEPT(InvalidParametersException, "Index out of range: Valid range: 0 .. " +
  92. toString(mNumTextures - 1) + ". Requested: " + toString(slot));
  93. }
  94. mTextureInfo[slot].isLoadStore = isLoadStore;
  95. }
  96. const TextureSurface& GpuParamsBase::getLoadStoreSurface(UINT32 slot) const
  97. {
  98. if (slot < 0 || slot >= mNumTextures)
  99. {
  100. BS_EXCEPT(InvalidParametersException, "Index out of range: Valid range: 0 .. " +
  101. toString(mNumTextures - 1) + ". Requested: " + toString(slot));
  102. }
  103. return mTextureInfo[slot].surface;
  104. }
  105. void GpuParamsBase::setLoadStoreSurface(UINT32 slot, const TextureSurface& surface) const
  106. {
  107. if (slot < 0 || slot >= mNumTextures)
  108. {
  109. BS_EXCEPT(InvalidParametersException, "Index out of range: Valid range: 0 .. " +
  110. toString(mNumTextures - 1) + ". Requested: " + toString(slot));
  111. }
  112. mTextureInfo[slot].surface = surface;
  113. }
  114. template<bool Core>
  115. TGpuParams<Core>::TGpuParams(const GpuParamDescPtr& paramDesc, bool transposeMatrices)
  116. :GpuParamsBase(paramDesc, transposeMatrices), mParamBlockBuffers(nullptr), mTextures(nullptr),
  117. mSamplerStates(nullptr)
  118. {
  119. if (mNumParamBlocks > 0)
  120. mParamBlockBuffers = bs_newN<ParamsBufferType>(mNumParamBlocks);
  121. if (mNumTextures > 0)
  122. mTextures = bs_newN<TextureType>(mNumTextures);
  123. if (mNumSamplerStates > 0)
  124. mSamplerStates = bs_newN<SamplerType>(mNumSamplerStates);
  125. }
  126. template<bool Core>
  127. TGpuParams<Core>::~TGpuParams()
  128. {
  129. if (mParamBlockBuffers != nullptr)
  130. bs_deleteN(mParamBlockBuffers, mNumParamBlocks);
  131. if (mTextures != nullptr)
  132. bs_deleteN(mTextures, mNumTextures);
  133. if (mSamplerStates != nullptr)
  134. bs_deleteN(mSamplerStates, mNumSamplerStates);
  135. }
  136. template<bool Core>
  137. void TGpuParams<Core>::setParamBlockBuffer(UINT32 slot, const ParamsBufferType& paramBlockBuffer)
  138. {
  139. if (slot < 0 || slot >= mNumParamBlocks)
  140. {
  141. BS_EXCEPT(InvalidParametersException, "Index out of range: Valid range: 0 .. " +
  142. toString(mNumParamBlocks - 1) + ". Requested: " + toString(slot));
  143. }
  144. mParamBlockBuffers[slot] = paramBlockBuffer;
  145. _markCoreDirty();
  146. }
  147. template<bool Core>
  148. void TGpuParams<Core>::setParamBlockBuffer(const String& name, const ParamsBufferType& paramBlockBuffer)
  149. {
  150. auto iterFind = mParamDesc->paramBlocks.find(name);
  151. if (iterFind == mParamDesc->paramBlocks.end())
  152. {
  153. LOGWRN("Cannot find parameter block with the name: " + name);
  154. return;
  155. }
  156. mParamBlockBuffers[iterFind->second.slot] = paramBlockBuffer;
  157. _markCoreDirty();
  158. }
  159. template<class T> struct TDataParamInfo { };
  160. template<> struct TDataParamInfo < float > { enum { TypeId = GPDT_FLOAT1 }; };
  161. template<> struct TDataParamInfo < Color > { enum { TypeId = GPDT_FLOAT4 }; };
  162. template<> struct TDataParamInfo < Vector2 > { enum { TypeId = GPDT_FLOAT2 }; };
  163. template<> struct TDataParamInfo < Vector3 > { enum { TypeId = GPDT_FLOAT3 }; };
  164. template<> struct TDataParamInfo < Vector4 > { enum { TypeId = GPDT_FLOAT4 }; };
  165. template<> struct TDataParamInfo < Matrix3 > { enum { TypeId = GPDT_MATRIX_3X3 }; };
  166. template<> struct TDataParamInfo < Matrix4 > { enum { TypeId = GPDT_MATRIX_4X4 }; };
  167. template<bool Core>
  168. template<class T>
  169. void TGpuParams<Core>::getParam(const String& name, TGpuDataParam<T, Core>& output) const
  170. {
  171. auto iterFind = mParamDesc->params.find(name);
  172. if (iterFind == mParamDesc->params.end())
  173. {
  174. output = TGpuDataParam<T, Core>(&iterFind->second, nullptr);
  175. LOGWRN("Cannot find parameter with the name '" + name + "'");
  176. }
  177. else
  178. output = TGpuDataParam<T, Core>(&iterFind->second, _getThisPtr());
  179. }
  180. template<bool Core>
  181. void TGpuParams<Core>::getStructParam(const String& name, TGpuParamStruct<Core>& output) const
  182. {
  183. auto iterFind = mParamDesc->params.find(name);
  184. if (iterFind == mParamDesc->params.end() || iterFind->second.type != GPDT_STRUCT)
  185. {
  186. output = TGpuParamStruct<Core>(&iterFind->second, nullptr);
  187. LOGWRN("Cannot find struct parameter with the name '" + name + "'");
  188. }
  189. else
  190. output = TGpuParamStruct<Core>(&iterFind->second, _getThisPtr());
  191. }
  192. template<bool Core>
  193. void TGpuParams<Core>::getTextureParam(const String& name, TGpuParamTexture<Core>& output) const
  194. {
  195. auto iterFind = mParamDesc->textures.find(name);
  196. if (iterFind == mParamDesc->textures.end())
  197. {
  198. output = TGpuParamTexture<Core>(&iterFind->second, nullptr);
  199. LOGWRN("Cannot find texture parameter with the name '" + name + "'");
  200. }
  201. else
  202. output = TGpuParamTexture<Core>(&iterFind->second, _getThisPtr());
  203. }
  204. template<bool Core>
  205. void TGpuParams<Core>::getLoadStoreTextureParam(const String& name, TGpuParamLoadStoreTexture<Core>& output) const
  206. {
  207. auto iterFind = mParamDesc->textures.find(name);
  208. if (iterFind == mParamDesc->textures.end())
  209. {
  210. output = TGpuParamLoadStoreTexture<Core>(&iterFind->second, nullptr);
  211. LOGWRN("Cannot find texture parameter with the name '" + name + "'");
  212. }
  213. else
  214. output = TGpuParamLoadStoreTexture<Core>(&iterFind->second, _getThisPtr());
  215. }
  216. template<bool Core>
  217. void TGpuParams<Core>::getSamplerStateParam(const String& name, TGpuParamSampState<Core>& output) const
  218. {
  219. auto iterFind = mParamDesc->samplers.find(name);
  220. if (iterFind == mParamDesc->samplers.end())
  221. {
  222. output = TGpuParamSampState<Core>(&iterFind->second, nullptr);
  223. LOGWRN("Cannot find sampler state parameter with the name '" + name + "'");
  224. }
  225. else
  226. output = TGpuParamSampState<Core>(&iterFind->second, _getThisPtr());
  227. }
  228. template<bool Core>
  229. typename TGpuParams<Core>::ParamsBufferType TGpuParams<Core>::getParamBlockBuffer(UINT32 slot) const
  230. {
  231. if (slot < 0 || slot >= mNumParamBlocks)
  232. {
  233. BS_EXCEPT(InvalidParametersException, "Index out of range: Valid range: 0 .. " +
  234. toString(mNumParamBlocks - 1) + ". Requested: " + toString(slot));
  235. }
  236. return mParamBlockBuffers[slot];
  237. }
  238. template<bool Core>
  239. typename TGpuParams<Core>::TextureType TGpuParams<Core>::getTexture(UINT32 slot)
  240. {
  241. if (slot < 0 || slot >= mNumTextures)
  242. {
  243. BS_EXCEPT(InvalidParametersException, "Index out of range: Valid range: 0 .. " +
  244. toString(mNumTextures - 1) + ". Requested: " + toString(slot));
  245. }
  246. return mTextures[slot];
  247. }
  248. template<bool Core>
  249. typename TGpuParams<Core>::SamplerType TGpuParams<Core>::getSamplerState(UINT32 slot)
  250. {
  251. if (slot < 0 || slot >= mNumSamplerStates)
  252. {
  253. BS_EXCEPT(InvalidParametersException, "Index out of range: Valid range: 0 .. " +
  254. toString(mNumSamplerStates - 1) + ". Requested: " + toString(slot));
  255. }
  256. return mSamplerStates[slot];
  257. }
  258. template<bool Core>
  259. void TGpuParams<Core>::setTexture(UINT32 slot, const TextureType& texture)
  260. {
  261. if (slot < 0 || slot >= mNumTextures)
  262. {
  263. BS_EXCEPT(InvalidParametersException, "Index out of range: Valid range: 0 .. " +
  264. toString(mNumTextures - 1) + ". Requested: " + toString(slot));
  265. }
  266. mTextures[slot] = texture;
  267. _markResourcesDirty();
  268. _markCoreDirty();
  269. }
  270. template<bool Core>
  271. void TGpuParams<Core>::setSamplerState(UINT32 slot, const SamplerType& sampler)
  272. {
  273. if (slot < 0 || slot >= mNumSamplerStates)
  274. {
  275. BS_EXCEPT(InvalidParametersException, "Index out of range: Valid range: 0 .. " +
  276. toString(mNumSamplerStates - 1) + ". Requested: " + toString(slot));
  277. }
  278. mSamplerStates[slot] = sampler;
  279. _markResourcesDirty();
  280. _markCoreDirty();
  281. }
  282. template class TGpuParams < false > ;
  283. template class TGpuParams < true > ;
  284. template BS_CORE_EXPORT void TGpuParams<false>::getParam<float>(const String&, TGpuDataParam<float, false>&) const;
  285. template BS_CORE_EXPORT void TGpuParams<false>::getParam<int>(const String&, TGpuDataParam<int, false>&) const;
  286. template BS_CORE_EXPORT void TGpuParams<false>::getParam<Color>(const String&, TGpuDataParam<Color, false>&) const;
  287. template BS_CORE_EXPORT void TGpuParams<false>::getParam<Vector2>(const String&, TGpuDataParam<Vector2, false>&) const;
  288. template BS_CORE_EXPORT void TGpuParams<false>::getParam<Vector3>(const String&, TGpuDataParam<Vector3, false>&) const;
  289. template BS_CORE_EXPORT void TGpuParams<false>::getParam<Vector4>(const String&, TGpuDataParam<Vector4, false>&) const;
  290. template BS_CORE_EXPORT void TGpuParams<false>::getParam<Vector2I>(const String&, TGpuDataParam<Vector2I, false>&) const;
  291. template BS_CORE_EXPORT void TGpuParams<false>::getParam<Vector3I>(const String&, TGpuDataParam<Vector3I, false>&) const;
  292. template BS_CORE_EXPORT void TGpuParams<false>::getParam<Vector4I>(const String&, TGpuDataParam<Vector4I, false>&) const;
  293. template BS_CORE_EXPORT void TGpuParams<false>::getParam<Matrix2>(const String&, TGpuDataParam<Matrix2, false>&) const;
  294. template BS_CORE_EXPORT void TGpuParams<false>::getParam<Matrix2x3>(const String&, TGpuDataParam<Matrix2x3, false>&) const;
  295. template BS_CORE_EXPORT void TGpuParams<false>::getParam<Matrix2x4>(const String&, TGpuDataParam<Matrix2x4, false>&) const;
  296. template BS_CORE_EXPORT void TGpuParams<false>::getParam<Matrix3>(const String&, TGpuDataParam<Matrix3, false>&) const;
  297. template BS_CORE_EXPORT void TGpuParams<false>::getParam<Matrix3x2>(const String&, TGpuDataParam<Matrix3x2, false>&) const;
  298. template BS_CORE_EXPORT void TGpuParams<false>::getParam<Matrix3x4>(const String&, TGpuDataParam<Matrix3x4, false>&) const;
  299. template BS_CORE_EXPORT void TGpuParams<false>::getParam<Matrix4>(const String&, TGpuDataParam<Matrix4, false>&) const;
  300. template BS_CORE_EXPORT void TGpuParams<false>::getParam<Matrix4x2>(const String&, TGpuDataParam<Matrix4x2, false>&) const;
  301. template BS_CORE_EXPORT void TGpuParams<false>::getParam<Matrix4x3>(const String&, TGpuDataParam<Matrix4x3, false>&) const;
  302. template BS_CORE_EXPORT void TGpuParams<true>::getParam<float>(const String&, TGpuDataParam<float, true>&) const;
  303. template BS_CORE_EXPORT void TGpuParams<true>::getParam<int>(const String&, TGpuDataParam<int, true>&) const;
  304. template BS_CORE_EXPORT void TGpuParams<true>::getParam<Color>(const String&, TGpuDataParam<Color, true>&) const;
  305. template BS_CORE_EXPORT void TGpuParams<true>::getParam<Vector2>(const String&, TGpuDataParam<Vector2, true>&) const;
  306. template BS_CORE_EXPORT void TGpuParams<true>::getParam<Vector3>(const String&, TGpuDataParam<Vector3, true>&) const;
  307. template BS_CORE_EXPORT void TGpuParams<true>::getParam<Vector4>(const String&, TGpuDataParam<Vector4, true>&) const;
  308. template BS_CORE_EXPORT void TGpuParams<true>::getParam<Vector2I>(const String&, TGpuDataParam<Vector2I, true>&) const;
  309. template BS_CORE_EXPORT void TGpuParams<true>::getParam<Vector3I>(const String&, TGpuDataParam<Vector3I, true>&) const;
  310. template BS_CORE_EXPORT void TGpuParams<true>::getParam<Vector4I>(const String&, TGpuDataParam<Vector4I, true>&) const;
  311. template BS_CORE_EXPORT void TGpuParams<true>::getParam<Matrix2>(const String&, TGpuDataParam<Matrix2, true>&) const;
  312. template BS_CORE_EXPORT void TGpuParams<true>::getParam<Matrix2x3>(const String&, TGpuDataParam<Matrix2x3, true>&) const;
  313. template BS_CORE_EXPORT void TGpuParams<true>::getParam<Matrix2x4>(const String&, TGpuDataParam<Matrix2x4, true>&) const;
  314. template BS_CORE_EXPORT void TGpuParams<true>::getParam<Matrix3>(const String&, TGpuDataParam<Matrix3, true>&) const;
  315. template BS_CORE_EXPORT void TGpuParams<true>::getParam<Matrix3x2>(const String&, TGpuDataParam<Matrix3x2, true>&) const;
  316. template BS_CORE_EXPORT void TGpuParams<true>::getParam<Matrix3x4>(const String&, TGpuDataParam<Matrix3x4, true>&) const;
  317. template BS_CORE_EXPORT void TGpuParams<true>::getParam<Matrix4>(const String&, TGpuDataParam<Matrix4, true>&) const;
  318. template BS_CORE_EXPORT void TGpuParams<true>::getParam<Matrix4x2>(const String&, TGpuDataParam<Matrix4x2, true>&) const;
  319. template BS_CORE_EXPORT void TGpuParams<true>::getParam<Matrix4x3>(const String&, TGpuDataParam<Matrix4x3, true>&) const;
  320. GpuParamsCore::GpuParamsCore(const GpuParamDescPtr& paramDesc, bool transposeMatrices)
  321. : TGpuParams(paramDesc, transposeMatrices)
  322. {
  323. }
  324. SPtr<GpuParamsCore> GpuParamsCore::_getThisPtr() const
  325. {
  326. return std::static_pointer_cast<GpuParamsCore>(getThisPtr());
  327. }
  328. void GpuParamsCore::updateHardwareBuffers()
  329. {
  330. for (UINT32 i = 0; i < mNumParamBlocks; i++)
  331. {
  332. if (mParamBlockBuffers[i] != nullptr)
  333. {
  334. mParamBlockBuffers[i]->flushToGPU();
  335. }
  336. }
  337. }
  338. void GpuParamsCore::syncToCore(const CoreSyncData& data)
  339. {
  340. UINT32 textureInfoSize = mNumTextures * sizeof(BoundTextureInfo);
  341. UINT32 paramBufferSize = mNumParamBlocks * sizeof(SPtr<GpuParamBlockBufferCore>);
  342. UINT32 textureArraySize = mNumTextures * sizeof(SPtr<TextureCore>);
  343. UINT32 samplerArraySize = mNumSamplerStates * sizeof(SPtr<SamplerStateCore>);
  344. UINT32 totalSize = textureInfoSize + paramBufferSize + textureArraySize + samplerArraySize;
  345. UINT32 textureInfoOffset = 0;
  346. UINT32 paramBufferOffset = textureInfoOffset + textureInfoSize;
  347. UINT32 textureArrayOffset = paramBufferOffset + paramBufferSize;
  348. UINT32 samplerArrayOffset = textureArrayOffset + textureArraySize;
  349. assert(data.getBufferSize() == totalSize);
  350. UINT8* dataPtr = data.getBuffer();
  351. BoundTextureInfo* textureInfos = (BoundTextureInfo*)(dataPtr + textureInfoOffset);
  352. SPtr<GpuParamBlockBufferCore>* paramBuffers = (SPtr<GpuParamBlockBufferCore>*)(dataPtr + paramBufferOffset);
  353. SPtr<TextureCore>* textures = (SPtr<TextureCore>*)(dataPtr + textureArrayOffset);
  354. SPtr<SamplerStateCore>* samplers = (SPtr<SamplerStateCore>*)(dataPtr + samplerArrayOffset);
  355. // Copy & destruct
  356. for (UINT32 i = 0; i < mNumParamBlocks; i++)
  357. {
  358. mParamBlockBuffers[i] = paramBuffers[i];
  359. paramBuffers[i].~SPtr<GpuParamBlockBufferCore>();
  360. }
  361. for (UINT32 i = 0; i < mNumTextures; i++)
  362. {
  363. mTextureInfo[i] = textureInfos[i];
  364. textureInfos[i].~BoundTextureInfo();
  365. mTextures[i] = textures[i];
  366. textures[i].~SPtr<TextureCore>();
  367. }
  368. for (UINT32 i = 0; i < mNumSamplerStates; i++)
  369. {
  370. mSamplerStates[i] = samplers[i];
  371. samplers[i].~SPtr<SamplerStateCore>();
  372. }
  373. }
  374. SPtr<GpuParamsCore> GpuParamsCore::create(const GpuParamDescPtr& paramDesc, bool transposeMatrices)
  375. {
  376. GpuParamsCore* params = new (bs_alloc<GpuParamsCore>()) GpuParamsCore(paramDesc, transposeMatrices);
  377. SPtr<GpuParamsCore> paramsPtr = bs_shared_ptr<GpuParamsCore>(params);
  378. paramsPtr->_setThisPtr(paramsPtr);
  379. return paramsPtr;
  380. }
  381. GpuParams::GpuParams(const GpuParamDescPtr& paramDesc, bool transposeMatrices)
  382. : TGpuParams(paramDesc, transposeMatrices)
  383. {
  384. }
  385. SPtr<GpuParams> GpuParams::_getThisPtr() const
  386. {
  387. return std::static_pointer_cast<GpuParams>(getThisPtr());
  388. }
  389. SPtr<GpuParamsCore> GpuParams::getCore() const
  390. {
  391. return std::static_pointer_cast<GpuParamsCore>(mCoreSpecific);
  392. }
  393. SPtr<CoreObjectCore> GpuParams::createCore() const
  394. {
  395. GpuParamsCore* obj = new (bs_alloc<GpuParamsCore>()) GpuParamsCore(mParamDesc, mTransposeMatrices);
  396. SPtr<CoreObjectCore> coreObj = bs_shared_ptr<GpuParamsCore>(obj);
  397. coreObj->_setThisPtr(coreObj);
  398. return coreObj;
  399. }
  400. void GpuParams::_markCoreDirty()
  401. {
  402. markCoreDirty();
  403. }
  404. void GpuParams::_markResourcesDirty()
  405. {
  406. markListenerResourcesDirty();
  407. }
  408. SPtr<GpuParams> GpuParams::create(const GpuParamDescPtr& paramDesc, bool transposeMatrices)
  409. {
  410. GpuParams* params = new (bs_alloc<GpuParams>()) GpuParams(paramDesc, transposeMatrices);
  411. SPtr<GpuParams> paramsPtr = bs_core_ptr<GpuParams>(params);
  412. paramsPtr->_setThisPtr(paramsPtr);
  413. paramsPtr->initialize();
  414. return paramsPtr;
  415. }
  416. CoreSyncData GpuParams::syncToCore(FrameAlloc* allocator)
  417. {
  418. UINT32 textureInfoSize = mNumTextures * sizeof(BoundTextureInfo);
  419. UINT32 paramBufferSize = mNumParamBlocks * sizeof(SPtr<GpuParamBlockBufferCore>);
  420. UINT32 textureArraySize = mNumTextures * sizeof(SPtr<TextureCore>);
  421. UINT32 samplerArraySize = mNumSamplerStates * sizeof(SPtr<SamplerStateCore>);
  422. UINT32 totalSize = textureInfoSize + paramBufferSize + textureArraySize + samplerArraySize;
  423. UINT32 textureInfoOffset = 0;
  424. UINT32 paramBufferOffset = textureInfoOffset + textureInfoSize;
  425. UINT32 textureArrayOffset = paramBufferOffset + paramBufferSize;
  426. UINT32 samplerArrayOffset = textureArrayOffset + textureArraySize;
  427. UINT8* data = allocator->alloc(totalSize);
  428. BoundTextureInfo* textureInfos = (BoundTextureInfo*)(data + textureInfoOffset);
  429. SPtr<GpuParamBlockBufferCore>* paramBuffers = (SPtr<GpuParamBlockBufferCore>*)(data + paramBufferOffset);
  430. SPtr<TextureCore>* textures = (SPtr<TextureCore>*)(data + textureArrayOffset);
  431. SPtr<SamplerStateCore>* samplers = (SPtr<SamplerStateCore>*)(data + samplerArrayOffset);
  432. // Construct & copy
  433. for (UINT32 i = 0; i < mNumParamBlocks; i++)
  434. {
  435. new (&paramBuffers[i]) SPtr<GpuParamBlockBufferCore>();
  436. if (mParamBlockBuffers[i] != nullptr)
  437. paramBuffers[i] = mParamBlockBuffers[i]->getCore();
  438. }
  439. for (UINT32 i = 0; i < mNumTextures; i++)
  440. {
  441. new (&textureInfos[i]) BoundTextureInfo();
  442. textureInfos[i] = mTextureInfo[i];
  443. new (&textures[i]) SPtr<TextureCore>();
  444. if (mTextures[i].isLoaded())
  445. textures[i] = mTextures[i]->getCore();
  446. else
  447. textures[i] = nullptr;
  448. }
  449. for (UINT32 i = 0; i < mNumSamplerStates; i++)
  450. {
  451. new (&samplers[i]) SPtr<SamplerStateCore>();
  452. if (mSamplerStates[i] != nullptr)
  453. samplers[i] = mSamplerStates[i]->getCore();
  454. else
  455. samplers[i] = nullptr;
  456. }
  457. return CoreSyncData(data, totalSize);
  458. }
  459. void GpuParams::getListenerResources(Vector<HResource>& resources)
  460. {
  461. for (UINT32 i = 0; i < mNumTextures; i++)
  462. {
  463. if (mTextures[i] != nullptr)
  464. resources.push_back(mTextures[i]);
  465. }
  466. }
  467. }