BsGpuParams.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561
  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. mParamBlockBuffers = bs_newN<ParamsBufferType>(mNumParamBlocks);
  120. mTextures = bs_newN<TextureType>(mNumTextures);
  121. mSamplerStates = bs_newN<SamplerType>(mNumSamplerStates);
  122. }
  123. template<bool Core>
  124. TGpuParams<Core>::~TGpuParams()
  125. {
  126. bs_deleteN(mParamBlockBuffers, mNumParamBlocks);
  127. bs_deleteN(mTextures, mNumTextures);
  128. bs_deleteN(mSamplerStates, mNumSamplerStates);
  129. }
  130. template<bool Core>
  131. void TGpuParams<Core>::setParamBlockBuffer(UINT32 slot, const ParamsBufferType& paramBlockBuffer)
  132. {
  133. if (slot < 0 || slot >= mNumParamBlocks)
  134. {
  135. BS_EXCEPT(InvalidParametersException, "Index out of range: Valid range: 0 .. " +
  136. toString(mNumParamBlocks - 1) + ". Requested: " + toString(slot));
  137. }
  138. mParamBlockBuffers[slot] = paramBlockBuffer;
  139. _markCoreDirty();
  140. }
  141. template<bool Core>
  142. void TGpuParams<Core>::setParamBlockBuffer(const String& name, const ParamsBufferType& paramBlockBuffer)
  143. {
  144. auto iterFind = mParamDesc->paramBlocks.find(name);
  145. if (iterFind == mParamDesc->paramBlocks.end())
  146. {
  147. LOGWRN("Cannot find parameter block with the name: " + name);
  148. return;
  149. }
  150. mParamBlockBuffers[iterFind->second.slot] = paramBlockBuffer;
  151. _markCoreDirty();
  152. }
  153. template<class T> struct TDataParamInfo { };
  154. template<> struct TDataParamInfo < float > { enum { TypeId = GPDT_FLOAT1 }; };
  155. template<> struct TDataParamInfo < Color > { enum { TypeId = GPDT_FLOAT4 }; };
  156. template<> struct TDataParamInfo < Vector2 > { enum { TypeId = GPDT_FLOAT2 }; };
  157. template<> struct TDataParamInfo < Vector3 > { enum { TypeId = GPDT_FLOAT3 }; };
  158. template<> struct TDataParamInfo < Vector4 > { enum { TypeId = GPDT_FLOAT4 }; };
  159. template<> struct TDataParamInfo < Matrix3 > { enum { TypeId = GPDT_MATRIX_3X3 }; };
  160. template<> struct TDataParamInfo < Matrix4 > { enum { TypeId = GPDT_MATRIX_4X4 }; };
  161. template<bool Core>
  162. template<class T>
  163. void TGpuParams<Core>::getParam(const String& name, TGpuDataParam<T, Core>& output) const
  164. {
  165. auto iterFind = mParamDesc->params.find(name);
  166. if (iterFind == mParamDesc->params.end())
  167. {
  168. output = TGpuDataParam<T, Core>(&iterFind->second, nullptr);
  169. LOGWRN("Cannot find parameter with the name '" + name + "'");
  170. }
  171. else
  172. output = TGpuDataParam<T, Core>(&iterFind->second, _getThisPtr());
  173. }
  174. template<bool Core>
  175. void TGpuParams<Core>::getStructParam(const String& name, TGpuParamStruct<Core>& output) const
  176. {
  177. auto iterFind = mParamDesc->params.find(name);
  178. if (iterFind == mParamDesc->params.end() || iterFind->second.type != GPDT_STRUCT)
  179. {
  180. output = TGpuParamStruct<Core>(&iterFind->second, nullptr);
  181. LOGWRN("Cannot find struct parameter with the name '" + name + "'");
  182. }
  183. else
  184. output = TGpuParamStruct<Core>(&iterFind->second, _getThisPtr());
  185. }
  186. template<bool Core>
  187. void TGpuParams<Core>::getTextureParam(const String& name, TGpuParamTexture<Core>& output) const
  188. {
  189. auto iterFind = mParamDesc->textures.find(name);
  190. if (iterFind == mParamDesc->textures.end())
  191. {
  192. output = TGpuParamTexture<Core>(&iterFind->second, nullptr);
  193. LOGWRN("Cannot find texture parameter with the name '" + name + "'");
  194. }
  195. else
  196. output = TGpuParamTexture<Core>(&iterFind->second, _getThisPtr());
  197. }
  198. template<bool Core>
  199. void TGpuParams<Core>::getLoadStoreTextureParam(const String& name, TGpuParamLoadStoreTexture<Core>& output) const
  200. {
  201. auto iterFind = mParamDesc->textures.find(name);
  202. if (iterFind == mParamDesc->textures.end())
  203. {
  204. output = TGpuParamLoadStoreTexture<Core>(&iterFind->second, nullptr);
  205. LOGWRN("Cannot find texture parameter with the name '" + name + "'");
  206. }
  207. else
  208. output = TGpuParamLoadStoreTexture<Core>(&iterFind->second, _getThisPtr());
  209. }
  210. template<bool Core>
  211. void TGpuParams<Core>::getSamplerStateParam(const String& name, TGpuParamSampState<Core>& output) const
  212. {
  213. auto iterFind = mParamDesc->samplers.find(name);
  214. if (iterFind == mParamDesc->samplers.end())
  215. {
  216. output = TGpuParamSampState<Core>(&iterFind->second, nullptr);
  217. LOGWRN("Cannot find sampler state parameter with the name '" + name + "'");
  218. }
  219. else
  220. output = TGpuParamSampState<Core>(&iterFind->second, _getThisPtr());
  221. }
  222. template<bool Core>
  223. typename TGpuParams<Core>::ParamsBufferType TGpuParams<Core>::getParamBlockBuffer(UINT32 slot) const
  224. {
  225. if (slot < 0 || slot >= mNumParamBlocks)
  226. {
  227. BS_EXCEPT(InvalidParametersException, "Index out of range: Valid range: 0 .. " +
  228. toString(mNumParamBlocks - 1) + ". Requested: " + toString(slot));
  229. }
  230. return mParamBlockBuffers[slot];
  231. }
  232. template<bool Core>
  233. typename TGpuParams<Core>::TextureType TGpuParams<Core>::getTexture(UINT32 slot)
  234. {
  235. if (slot < 0 || slot >= mNumTextures)
  236. {
  237. BS_EXCEPT(InvalidParametersException, "Index out of range: Valid range: 0 .. " +
  238. toString(mNumTextures - 1) + ". Requested: " + toString(slot));
  239. }
  240. return mTextures[slot];
  241. }
  242. template<bool Core>
  243. typename TGpuParams<Core>::SamplerType TGpuParams<Core>::getSamplerState(UINT32 slot)
  244. {
  245. if (slot < 0 || slot >= mNumSamplerStates)
  246. {
  247. BS_EXCEPT(InvalidParametersException, "Index out of range: Valid range: 0 .. " +
  248. toString(mNumSamplerStates - 1) + ". Requested: " + toString(slot));
  249. }
  250. return mSamplerStates[slot];
  251. }
  252. template<bool Core>
  253. void TGpuParams<Core>::setTexture(UINT32 slot, const TextureType& texture)
  254. {
  255. if (slot < 0 || slot >= mNumTextures)
  256. {
  257. BS_EXCEPT(InvalidParametersException, "Index out of range: Valid range: 0 .. " +
  258. toString(mNumTextures - 1) + ". Requested: " + toString(slot));
  259. }
  260. mTextures[slot] = texture;
  261. _markResourcesDirty();
  262. _markCoreDirty();
  263. }
  264. template<bool Core>
  265. void TGpuParams<Core>::setSamplerState(UINT32 slot, const SamplerType& sampler)
  266. {
  267. if (slot < 0 || slot >= mNumSamplerStates)
  268. {
  269. BS_EXCEPT(InvalidParametersException, "Index out of range: Valid range: 0 .. " +
  270. toString(mNumSamplerStates - 1) + ". Requested: " + toString(slot));
  271. }
  272. mSamplerStates[slot] = sampler;
  273. _markResourcesDirty();
  274. _markCoreDirty();
  275. }
  276. template class TGpuParams < false > ;
  277. template class TGpuParams < true > ;
  278. template BS_CORE_EXPORT void TGpuParams<false>::getParam<float>(const String&, TGpuDataParam<float, false>&) const;
  279. template BS_CORE_EXPORT void TGpuParams<false>::getParam<int>(const String&, TGpuDataParam<int, false>&) const;
  280. template BS_CORE_EXPORT void TGpuParams<false>::getParam<Color>(const String&, TGpuDataParam<Color, false>&) const;
  281. template BS_CORE_EXPORT void TGpuParams<false>::getParam<Vector2>(const String&, TGpuDataParam<Vector2, false>&) const;
  282. template BS_CORE_EXPORT void TGpuParams<false>::getParam<Vector3>(const String&, TGpuDataParam<Vector3, false>&) const;
  283. template BS_CORE_EXPORT void TGpuParams<false>::getParam<Vector4>(const String&, TGpuDataParam<Vector4, false>&) const;
  284. template BS_CORE_EXPORT void TGpuParams<false>::getParam<Vector2I>(const String&, TGpuDataParam<Vector2I, false>&) const;
  285. template BS_CORE_EXPORT void TGpuParams<false>::getParam<Vector3I>(const String&, TGpuDataParam<Vector3I, false>&) const;
  286. template BS_CORE_EXPORT void TGpuParams<false>::getParam<Vector4I>(const String&, TGpuDataParam<Vector4I, false>&) const;
  287. template BS_CORE_EXPORT void TGpuParams<false>::getParam<Matrix2>(const String&, TGpuDataParam<Matrix2, false>&) const;
  288. template BS_CORE_EXPORT void TGpuParams<false>::getParam<Matrix2x3>(const String&, TGpuDataParam<Matrix2x3, false>&) const;
  289. template BS_CORE_EXPORT void TGpuParams<false>::getParam<Matrix2x4>(const String&, TGpuDataParam<Matrix2x4, false>&) const;
  290. template BS_CORE_EXPORT void TGpuParams<false>::getParam<Matrix3>(const String&, TGpuDataParam<Matrix3, false>&) const;
  291. template BS_CORE_EXPORT void TGpuParams<false>::getParam<Matrix3x2>(const String&, TGpuDataParam<Matrix3x2, false>&) const;
  292. template BS_CORE_EXPORT void TGpuParams<false>::getParam<Matrix3x4>(const String&, TGpuDataParam<Matrix3x4, false>&) const;
  293. template BS_CORE_EXPORT void TGpuParams<false>::getParam<Matrix4>(const String&, TGpuDataParam<Matrix4, false>&) const;
  294. template BS_CORE_EXPORT void TGpuParams<false>::getParam<Matrix4x2>(const String&, TGpuDataParam<Matrix4x2, false>&) const;
  295. template BS_CORE_EXPORT void TGpuParams<false>::getParam<Matrix4x3>(const String&, TGpuDataParam<Matrix4x3, false>&) const;
  296. template BS_CORE_EXPORT void TGpuParams<true>::getParam<float>(const String&, TGpuDataParam<float, true>&) const;
  297. template BS_CORE_EXPORT void TGpuParams<true>::getParam<int>(const String&, TGpuDataParam<int, true>&) const;
  298. template BS_CORE_EXPORT void TGpuParams<true>::getParam<Color>(const String&, TGpuDataParam<Color, true>&) const;
  299. template BS_CORE_EXPORT void TGpuParams<true>::getParam<Vector2>(const String&, TGpuDataParam<Vector2, true>&) const;
  300. template BS_CORE_EXPORT void TGpuParams<true>::getParam<Vector3>(const String&, TGpuDataParam<Vector3, true>&) const;
  301. template BS_CORE_EXPORT void TGpuParams<true>::getParam<Vector4>(const String&, TGpuDataParam<Vector4, true>&) const;
  302. template BS_CORE_EXPORT void TGpuParams<true>::getParam<Vector2I>(const String&, TGpuDataParam<Vector2I, true>&) const;
  303. template BS_CORE_EXPORT void TGpuParams<true>::getParam<Vector3I>(const String&, TGpuDataParam<Vector3I, true>&) const;
  304. template BS_CORE_EXPORT void TGpuParams<true>::getParam<Vector4I>(const String&, TGpuDataParam<Vector4I, true>&) const;
  305. template BS_CORE_EXPORT void TGpuParams<true>::getParam<Matrix2>(const String&, TGpuDataParam<Matrix2, true>&) const;
  306. template BS_CORE_EXPORT void TGpuParams<true>::getParam<Matrix2x3>(const String&, TGpuDataParam<Matrix2x3, true>&) const;
  307. template BS_CORE_EXPORT void TGpuParams<true>::getParam<Matrix2x4>(const String&, TGpuDataParam<Matrix2x4, true>&) const;
  308. template BS_CORE_EXPORT void TGpuParams<true>::getParam<Matrix3>(const String&, TGpuDataParam<Matrix3, true>&) const;
  309. template BS_CORE_EXPORT void TGpuParams<true>::getParam<Matrix3x2>(const String&, TGpuDataParam<Matrix3x2, true>&) const;
  310. template BS_CORE_EXPORT void TGpuParams<true>::getParam<Matrix3x4>(const String&, TGpuDataParam<Matrix3x4, true>&) const;
  311. template BS_CORE_EXPORT void TGpuParams<true>::getParam<Matrix4>(const String&, TGpuDataParam<Matrix4, true>&) const;
  312. template BS_CORE_EXPORT void TGpuParams<true>::getParam<Matrix4x2>(const String&, TGpuDataParam<Matrix4x2, true>&) const;
  313. template BS_CORE_EXPORT void TGpuParams<true>::getParam<Matrix4x3>(const String&, TGpuDataParam<Matrix4x3, true>&) const;
  314. GpuParamsCore::GpuParamsCore(const GpuParamDescPtr& paramDesc, bool transposeMatrices)
  315. : TGpuParams(paramDesc, transposeMatrices)
  316. {
  317. }
  318. SPtr<GpuParamsCore> GpuParamsCore::_getThisPtr() const
  319. {
  320. return std::static_pointer_cast<GpuParamsCore>(getThisPtr());
  321. }
  322. void GpuParamsCore::updateHardwareBuffers()
  323. {
  324. for (UINT32 i = 0; i < mNumParamBlocks; i++)
  325. {
  326. if (mParamBlockBuffers[i] != nullptr)
  327. {
  328. mParamBlockBuffers[i]->flushToGPU();
  329. }
  330. }
  331. }
  332. void GpuParamsCore::syncToCore(const CoreSyncData& data)
  333. {
  334. UINT32 textureInfoSize = mNumTextures * sizeof(BoundTextureInfo);
  335. UINT32 paramBufferSize = mNumParamBlocks * sizeof(SPtr<GpuParamBlockBufferCore>);
  336. UINT32 textureArraySize = mNumTextures * sizeof(SPtr<TextureCore>);
  337. UINT32 samplerArraySize = mNumSamplerStates * sizeof(SPtr<SamplerStateCore>);
  338. UINT32 totalSize = textureInfoSize + paramBufferSize + textureArraySize + samplerArraySize;
  339. UINT32 textureInfoOffset = 0;
  340. UINT32 paramBufferOffset = textureInfoOffset + textureInfoSize;
  341. UINT32 textureArrayOffset = paramBufferOffset + paramBufferSize;
  342. UINT32 samplerArrayOffset = textureArrayOffset + textureArraySize;
  343. assert(data.getBufferSize() == totalSize);
  344. UINT8* dataPtr = data.getBuffer();
  345. BoundTextureInfo* textureInfos = (BoundTextureInfo*)(dataPtr + textureInfoOffset);
  346. SPtr<GpuParamBlockBufferCore>* paramBuffers = (SPtr<GpuParamBlockBufferCore>*)(dataPtr + paramBufferOffset);
  347. SPtr<TextureCore>* textures = (SPtr<TextureCore>*)(dataPtr + textureArrayOffset);
  348. SPtr<SamplerStateCore>* samplers = (SPtr<SamplerStateCore>*)(dataPtr + samplerArrayOffset);
  349. // Copy & destruct
  350. for (UINT32 i = 0; i < mNumParamBlocks; i++)
  351. {
  352. mParamBlockBuffers[i] = paramBuffers[i];
  353. paramBuffers[i].~SPtr<GpuParamBlockBufferCore>();
  354. }
  355. for (UINT32 i = 0; i < mNumTextures; i++)
  356. {
  357. mTextureInfo[i] = textureInfos[i];
  358. textureInfos[i].~BoundTextureInfo();
  359. mTextures[i] = textures[i];
  360. textures[i].~SPtr<TextureCore>();
  361. }
  362. for (UINT32 i = 0; i < mNumSamplerStates; i++)
  363. {
  364. mSamplerStates[i] = samplers[i];
  365. samplers[i].~SPtr<SamplerStateCore>();
  366. }
  367. }
  368. SPtr<GpuParamsCore> GpuParamsCore::create(const GpuParamDescPtr& paramDesc, bool transposeMatrices)
  369. {
  370. GpuParamsCore* params = new (bs_alloc<GpuParamsCore>()) GpuParamsCore(paramDesc, transposeMatrices);
  371. SPtr<GpuParamsCore> paramsPtr = bs_shared_ptr<GpuParamsCore, GenAlloc>(params);
  372. paramsPtr->_setThisPtr(paramsPtr);
  373. return paramsPtr;
  374. }
  375. GpuParams::GpuParams(const GpuParamDescPtr& paramDesc, bool transposeMatrices)
  376. : TGpuParams(paramDesc, transposeMatrices)
  377. {
  378. }
  379. SPtr<GpuParams> GpuParams::_getThisPtr() const
  380. {
  381. return std::static_pointer_cast<GpuParams>(getThisPtr());
  382. }
  383. SPtr<GpuParamsCore> GpuParams::getCore() const
  384. {
  385. return std::static_pointer_cast<GpuParamsCore>(mCoreSpecific);
  386. }
  387. SPtr<CoreObjectCore> GpuParams::createCore() const
  388. {
  389. GpuParamsCore* obj = new (bs_alloc<GpuParamsCore>()) GpuParamsCore(mParamDesc, mTransposeMatrices);
  390. SPtr<CoreObjectCore> coreObj = bs_shared_ptr<GpuParamsCore, GenAlloc>(obj);
  391. coreObj->_setThisPtr(coreObj);
  392. return coreObj;
  393. }
  394. void GpuParams::_markCoreDirty()
  395. {
  396. markCoreDirty();
  397. }
  398. void GpuParams::_markResourcesDirty()
  399. {
  400. markListenerResourcesDirty();
  401. }
  402. SPtr<GpuParams> GpuParams::create(const GpuParamDescPtr& paramDesc, bool transposeMatrices)
  403. {
  404. GpuParams* params = new (bs_alloc<GpuParams>()) GpuParams(paramDesc, transposeMatrices);
  405. SPtr<GpuParams> paramsPtr = bs_core_ptr<GpuParams, GenAlloc>(params);
  406. paramsPtr->_setThisPtr(paramsPtr);
  407. paramsPtr->initialize();
  408. return paramsPtr;
  409. }
  410. CoreSyncData GpuParams::syncToCore(FrameAlloc* allocator)
  411. {
  412. UINT32 textureInfoSize = mNumTextures * sizeof(BoundTextureInfo);
  413. UINT32 paramBufferSize = mNumParamBlocks * sizeof(SPtr<GpuParamBlockBufferCore>);
  414. UINT32 textureArraySize = mNumTextures * sizeof(SPtr<TextureCore>);
  415. UINT32 samplerArraySize = mNumSamplerStates * sizeof(SPtr<SamplerStateCore>);
  416. UINT32 totalSize = textureInfoSize + paramBufferSize + textureArraySize + samplerArraySize;
  417. UINT32 textureInfoOffset = 0;
  418. UINT32 paramBufferOffset = textureInfoOffset + textureInfoSize;
  419. UINT32 textureArrayOffset = paramBufferOffset + paramBufferSize;
  420. UINT32 samplerArrayOffset = textureArrayOffset + textureArraySize;
  421. UINT8* data = allocator->alloc(totalSize);
  422. BoundTextureInfo* textureInfos = (BoundTextureInfo*)(data + textureInfoOffset);
  423. SPtr<GpuParamBlockBufferCore>* paramBuffers = (SPtr<GpuParamBlockBufferCore>*)(data + paramBufferOffset);
  424. SPtr<TextureCore>* textures = (SPtr<TextureCore>*)(data + textureArrayOffset);
  425. SPtr<SamplerStateCore>* samplers = (SPtr<SamplerStateCore>*)(data + samplerArrayOffset);
  426. // Construct & copy
  427. for (UINT32 i = 0; i < mNumParamBlocks; i++)
  428. {
  429. new (&paramBuffers[i]) SPtr<GpuParamBlockBufferCore>();
  430. if (mParamBlockBuffers[i] != nullptr)
  431. paramBuffers[i] = mParamBlockBuffers[i]->getCore();
  432. }
  433. for (UINT32 i = 0; i < mNumTextures; i++)
  434. {
  435. new (&textureInfos[i]) BoundTextureInfo();
  436. textureInfos[i] = mTextureInfo[i];
  437. new (&textures[i]) SPtr<TextureCore>();
  438. if (mTextures[i].isLoaded())
  439. textures[i] = mTextures[i]->getCore();
  440. else
  441. textures[i] = nullptr;
  442. }
  443. for (UINT32 i = 0; i < mNumSamplerStates; i++)
  444. {
  445. new (&samplers[i]) SPtr<SamplerStateCore>();
  446. if (mSamplerStates[i] != nullptr)
  447. samplers[i] = mSamplerStates[i]->getCore();
  448. else
  449. samplers[i] = nullptr;
  450. }
  451. return CoreSyncData(data, totalSize);
  452. }
  453. void GpuParams::getListenerResources(Vector<HResource>& resources)
  454. {
  455. for (UINT32 i = 0; i < mNumTextures; i++)
  456. {
  457. if (mTextures[i] != nullptr)
  458. resources.push_back(mTextures[i]);
  459. }
  460. }
  461. }