BsGpuParams.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. #include "BsGpuParams.h"
  2. #include "BsGpuParamDesc.h"
  3. #include "BsGpuParamBlockBuffer.h"
  4. #include "BsVector2.h"
  5. #include "BsFrameAlloc.h"
  6. #include "BsDebug.h"
  7. #include "BsException.h"
  8. namespace BansheeEngine
  9. {
  10. GpuParamsBase::GpuParamsBase(const GpuParamDescPtr& paramDesc, bool transposeMatrices)
  11. :mParamDesc(paramDesc), mTransposeMatrices(transposeMatrices), mNumParamBlocks(0), mNumSamplerStates(0),
  12. mNumTextures(0), mTextureInfo(nullptr)
  13. {
  14. for (auto& paramBlock : mParamDesc->paramBlocks)
  15. {
  16. if ((paramBlock.second.slot + 1) > mNumParamBlocks)
  17. mNumParamBlocks = paramBlock.second.slot + 1;
  18. }
  19. for (auto& texture : mParamDesc->textures)
  20. {
  21. if ((texture.second.slot + 1) > mNumTextures)
  22. mNumTextures = texture.second.slot + 1;
  23. }
  24. for (auto& sampler : mParamDesc->samplers)
  25. {
  26. if ((sampler.second.slot + 1) > mNumSamplerStates)
  27. mNumSamplerStates = sampler.second.slot + 1;
  28. }
  29. mTextureInfo = bs_newN<BoundTextureInfo>(mNumTextures);
  30. }
  31. GpuParamsBase::~GpuParamsBase()
  32. {
  33. bs_deleteN(mTextureInfo, mNumTextures);
  34. }
  35. UINT32 GpuParamsBase::getDataParamSize(const String& name) const
  36. {
  37. GpuParamDataDesc* desc = getParamDesc(name);
  38. if(desc != nullptr)
  39. return desc->elementSize * 4;
  40. return 0;
  41. }
  42. bool GpuParamsBase::hasParam(const String& name) const
  43. {
  44. return getParamDesc(name) != nullptr;
  45. }
  46. bool GpuParamsBase::hasTexture(const String& name) const
  47. {
  48. auto paramIter = mParamDesc->textures.find(name);
  49. if(paramIter != mParamDesc->textures.end())
  50. return true;
  51. return false;
  52. }
  53. bool GpuParamsBase::hasSamplerState(const String& name) const
  54. {
  55. auto paramIter = mParamDesc->samplers.find(name);
  56. if(paramIter != mParamDesc->samplers.end())
  57. return true;
  58. return false;
  59. }
  60. bool GpuParamsBase::hasParamBlock(const String& name) const
  61. {
  62. auto paramBlockIter = mParamDesc->paramBlocks.find(name);
  63. if(paramBlockIter != mParamDesc->paramBlocks.end())
  64. return true;
  65. return false;
  66. }
  67. GpuParamDataDesc* GpuParamsBase::getParamDesc(const String& name) const
  68. {
  69. auto paramIter = mParamDesc->params.find(name);
  70. if (paramIter != mParamDesc->params.end())
  71. return &paramIter->second;
  72. return nullptr;
  73. }
  74. bool GpuParamsBase::isLoadStoreTexture(UINT32 slot) const
  75. {
  76. if (slot < 0 || slot >= mNumTextures)
  77. {
  78. BS_EXCEPT(InvalidParametersException, "Index out of range: Valid range: 0 .. " +
  79. toString(mNumTextures - 1) + ". Requested: " + toString(slot));
  80. }
  81. return mTextureInfo[slot].isLoadStore;
  82. }
  83. void GpuParamsBase::setIsLoadStoreTexture(UINT32 slot, bool isLoadStore)
  84. {
  85. if (slot < 0 || slot >= mNumTextures)
  86. {
  87. BS_EXCEPT(InvalidParametersException, "Index out of range: Valid range: 0 .. " +
  88. toString(mNumTextures - 1) + ". Requested: " + toString(slot));
  89. }
  90. mTextureInfo[slot].isLoadStore = isLoadStore;
  91. }
  92. const TextureSurface& GpuParamsBase::getLoadStoreSurface(UINT32 slot) const
  93. {
  94. if (slot < 0 || slot >= mNumTextures)
  95. {
  96. BS_EXCEPT(InvalidParametersException, "Index out of range: Valid range: 0 .. " +
  97. toString(mNumTextures - 1) + ". Requested: " + toString(slot));
  98. }
  99. return mTextureInfo[slot].surface;
  100. }
  101. void GpuParamsBase::setLoadStoreSurface(UINT32 slot, const TextureSurface& surface) const
  102. {
  103. if (slot < 0 || slot >= mNumTextures)
  104. {
  105. BS_EXCEPT(InvalidParametersException, "Index out of range: Valid range: 0 .. " +
  106. toString(mNumTextures - 1) + ". Requested: " + toString(slot));
  107. }
  108. mTextureInfo[slot].surface = surface;
  109. }
  110. GpuParamsCore::GpuParamsCore(const GpuParamDescPtr& paramDesc, bool transposeMatrices)
  111. : TGpuParams(paramDesc, transposeMatrices)
  112. {
  113. }
  114. void GpuParamsCore::updateHardwareBuffers()
  115. {
  116. for (UINT32 i = 0; i < mNumParamBlocks; i++)
  117. {
  118. if (mParamBlockBuffers[i] != nullptr)
  119. {
  120. mParamBlockBuffers[i]->flushToGPU();
  121. }
  122. }
  123. }
  124. SPtr<GpuParamsCore> GpuParamsCore::create(const GpuParamDescPtr& paramDesc, bool transposeMatrices)
  125. {
  126. GpuParamsCore* params = new (bs_alloc<GpuParamsCore>()) GpuParamsCore(paramDesc, transposeMatrices);
  127. SPtr<GpuParamsCore> paramsPtr = bs_shared_ptr<GpuParamsCore, GenAlloc>(params);
  128. paramsPtr->_setThisPtr(paramsPtr);
  129. return paramsPtr;
  130. }
  131. GpuParams::GpuParams(const GpuParamDescPtr& paramDesc, bool transposeMatrices)
  132. : TGpuParams(paramDesc, transposeMatrices)
  133. {
  134. }
  135. SPtr<GpuParamsCore> GpuParams::getCore() const
  136. {
  137. return std::static_pointer_cast<GpuParamsCore>(mCoreSpecific);
  138. }
  139. SPtr<CoreObjectCore> GpuParams::createCore() const
  140. {
  141. GpuParamsCore* obj = new (bs_alloc<GpuParamsCore>()) GpuParamsCore(mParamDesc, mTransposeMatrices);
  142. SPtr<CoreObjectCore> coreObj = bs_shared_ptr<GpuParamsCore, GenAlloc>(obj);
  143. coreObj->_setThisPtr(coreObj);
  144. return coreObj;
  145. }
  146. void GpuParams::_markCoreDirty()
  147. {
  148. markCoreDirty();
  149. }
  150. SPtr<GpuParams> GpuParams::create(const GpuParamDescPtr& paramDesc, bool transposeMatrices)
  151. {
  152. GpuParams* params = new (bs_alloc<GpuParams>()) GpuParams(paramDesc, transposeMatrices);
  153. SPtr<GpuParams> paramsPtr = bs_core_ptr<GpuParams, GenAlloc>(params);
  154. paramsPtr->_setThisPtr(paramsPtr);
  155. paramsPtr->initialize();
  156. return paramsPtr;
  157. }
  158. }