BsGpuParam.cpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. #include "BsGpuParam.h"
  2. #include "BsGpuParams.h"
  3. namespace BansheeEngine
  4. {
  5. GpuDataParamBase::GpuDataParamBase()
  6. :mParamDesc(nullptr)
  7. { }
  8. GpuDataParamBase::GpuDataParamBase(GpuParamDataDesc* paramDesc, const std::shared_ptr<GpuParamsInternalData>& internalData)
  9. :mParamDesc(paramDesc), mInternalData(internalData)
  10. { }
  11. bool GpuDataParamBase::isDestroyed() const
  12. {
  13. return mInternalData->mIsDestroyed;
  14. }
  15. GpuParamBlockPtr GpuDataParamBase::getParamBlock(UINT32 slotIdx) const
  16. {
  17. return mInternalData->mParamBlocks[slotIdx];
  18. }
  19. bool GpuDataParamBase::getTransposeMatrices() const
  20. {
  21. return mInternalData->mTransposeMatrices;
  22. }
  23. void GpuDataParamBase::markCoreDirty()
  24. {
  25. mInternalData->mCoreDirtyFlags = 0xFFFFFFFF;
  26. }
  27. /************************************************************************/
  28. /* STRUCT */
  29. /************************************************************************/
  30. GpuParamStruct::GpuParamStruct()
  31. :mParamDesc(nullptr)
  32. { }
  33. GpuParamStruct::GpuParamStruct(GpuParamDataDesc* paramDesc, const std::shared_ptr<GpuParamsInternalData>& internalData)
  34. :mParamDesc(paramDesc), mInternalData(internalData)
  35. { }
  36. void GpuParamStruct::set(const void* value, UINT32 sizeBytes, UINT32 arrayIdx)
  37. {
  38. if (mInternalData == nullptr)
  39. return;
  40. if (mInternalData->mIsDestroyed)
  41. BS_EXCEPT(InternalErrorException, "Trying to access a destroyed gpu parameter.");
  42. UINT32 elementSizeBytes = mParamDesc->elementSize * sizeof(UINT32);
  43. #if BS_DEBUG_MODE
  44. if(sizeBytes > elementSizeBytes)
  45. {
  46. LOGWRN("Provided element size larger than maximum element size. Maximum size: " +
  47. toString(elementSizeBytes) + ". Supplied size: " + toString(sizeBytes));
  48. }
  49. if (arrayIdx >= mParamDesc->arraySize)
  50. {
  51. BS_EXCEPT(InvalidParametersException, "Array index out of range. Array size: " +
  52. toString(mParamDesc->arraySize) + ". Requested size: " + toString(arrayIdx));
  53. }
  54. #endif
  55. sizeBytes = std::min(elementSizeBytes, sizeBytes);
  56. GpuParamBlockPtr paramBlock = mInternalData->mParamBlocks[mParamDesc->paramBlockSlot];
  57. paramBlock->write((mParamDesc->cpuMemOffset + arrayIdx * mParamDesc->arrayElementStride) * sizeof(UINT32), value, sizeBytes);
  58. // Set unused bytes to 0
  59. if(sizeBytes < elementSizeBytes)
  60. {
  61. UINT32 diffSize = elementSizeBytes - sizeBytes;
  62. paramBlock->zeroOut((mParamDesc->cpuMemOffset + arrayIdx * mParamDesc->arrayElementStride) * sizeof(UINT32)+sizeBytes, diffSize);
  63. }
  64. mInternalData->mCoreDirtyFlags = 0xFFFFFFFF;
  65. }
  66. void GpuParamStruct::get(void* value, UINT32 sizeBytes, UINT32 arrayIdx)
  67. {
  68. if (mInternalData == nullptr)
  69. return;
  70. if (mInternalData->mIsDestroyed)
  71. BS_EXCEPT(InternalErrorException, "Trying to access a destroyed gpu parameter.");
  72. UINT32 elementSizeBytes = mParamDesc->elementSize * sizeof(UINT32);
  73. #if BS_DEBUG_MODE
  74. if(sizeBytes > elementSizeBytes)
  75. {
  76. LOGWRN("Provided element size larger than maximum element size. Maximum size: " +
  77. toString(elementSizeBytes) + ". Supplied size: " + toString(sizeBytes));
  78. }
  79. if (arrayIdx >= mParamDesc->arraySize)
  80. {
  81. BS_EXCEPT(InvalidParametersException, "Array index out of range. Array size: " +
  82. toString(mParamDesc->arraySize) + ". Requested size: " + toString(arrayIdx));
  83. }
  84. #endif
  85. sizeBytes = std::min(elementSizeBytes, sizeBytes);
  86. GpuParamBlockPtr paramBlock = mInternalData->mParamBlocks[mParamDesc->paramBlockSlot];
  87. paramBlock->read((mParamDesc->cpuMemOffset + arrayIdx * mParamDesc->arrayElementStride) * sizeof(UINT32), value, sizeBytes);
  88. }
  89. UINT32 GpuParamStruct::getElementSize() const
  90. {
  91. if (mInternalData == nullptr)
  92. return 0;
  93. if(mInternalData->mIsDestroyed)
  94. BS_EXCEPT(InternalErrorException, "Trying to access a destroyed gpu parameter.");
  95. return mParamDesc->elementSize * sizeof(UINT32);
  96. }
  97. /************************************************************************/
  98. /* TEXTURE */
  99. /************************************************************************/
  100. GpuParamTexture::GpuParamTexture()
  101. :mParamDesc(nullptr)
  102. { }
  103. GpuParamTexture::GpuParamTexture(GpuParamObjectDesc* paramDesc, const std::shared_ptr<GpuParamsInternalData>& internalData)
  104. : mParamDesc(paramDesc), mInternalData(internalData)
  105. { }
  106. void GpuParamTexture::set(const HTexture& texture)
  107. {
  108. if (mInternalData == nullptr)
  109. return;
  110. if (mInternalData->mIsDestroyed)
  111. BS_EXCEPT(InternalErrorException, "Trying to access a destroyed gpu parameter.");
  112. mInternalData->mTextures[mParamDesc->slot] = texture;
  113. mInternalData->mTextureInfo[mParamDesc->slot].isLoadStore = false;
  114. mInternalData->mCoreDirtyFlags = 0xFFFFFFFF;
  115. }
  116. HTexture GpuParamTexture::get()
  117. {
  118. if (mInternalData == nullptr)
  119. return HTexture();
  120. if (mInternalData->mIsDestroyed)
  121. BS_EXCEPT(InternalErrorException, "Trying to access a destroyed gpu parameter.");
  122. return mInternalData->mTextures[mParamDesc->slot];
  123. }
  124. /************************************************************************/
  125. /* LOAD/STORE TEXTURE */
  126. /************************************************************************/
  127. GpuParamLoadStoreTexture::GpuParamLoadStoreTexture()
  128. :mParamDesc(nullptr)
  129. { }
  130. GpuParamLoadStoreTexture::GpuParamLoadStoreTexture(GpuParamObjectDesc* paramDesc, const std::shared_ptr<GpuParamsInternalData>& internalData)
  131. : mParamDesc(paramDesc), mInternalData(internalData)
  132. { }
  133. void GpuParamLoadStoreTexture::set(const HTexture& texture, const TextureSurface& surface)
  134. {
  135. if (mInternalData == nullptr)
  136. return;
  137. if (mInternalData->mIsDestroyed)
  138. BS_EXCEPT(InternalErrorException, "Trying to access a destroyed gpu parameter.");
  139. mInternalData->mTextures[mParamDesc->slot] = texture;
  140. mInternalData->mTextureInfo[mParamDesc->slot].isLoadStore = true;
  141. mInternalData->mTextureInfo[mParamDesc->slot].surface = surface;
  142. mInternalData->mCoreDirtyFlags = 0xFFFFFFFF;
  143. }
  144. HTexture GpuParamLoadStoreTexture::get()
  145. {
  146. if (mInternalData == nullptr)
  147. return HTexture();
  148. if (mInternalData->mIsDestroyed)
  149. BS_EXCEPT(InternalErrorException, "Trying to access a destroyed gpu parameter.");
  150. return mInternalData->mTextures[mParamDesc->slot];
  151. }
  152. /************************************************************************/
  153. /* SAMPLER STATE */
  154. /************************************************************************/
  155. GpuParamSampState::GpuParamSampState()
  156. :mParamDesc(nullptr)
  157. { }
  158. GpuParamSampState::GpuParamSampState(GpuParamObjectDesc* paramDesc, const std::shared_ptr<GpuParamsInternalData>& internalData)
  159. : mParamDesc(paramDesc), mInternalData(internalData)
  160. { }
  161. void GpuParamSampState::set(const HSamplerState& samplerState)
  162. {
  163. if (mInternalData == nullptr)
  164. return;
  165. if (mInternalData->mIsDestroyed)
  166. BS_EXCEPT(InternalErrorException, "Trying to access a destroyed gpu parameter.");
  167. mInternalData->mSamplerStates[mParamDesc->slot] = samplerState;
  168. mInternalData->mCoreDirtyFlags = 0xFFFFFFFF;
  169. }
  170. HSamplerState GpuParamSampState::get()
  171. {
  172. if (mInternalData == nullptr)
  173. return HSamplerState();
  174. if (mInternalData->mIsDestroyed)
  175. BS_EXCEPT(InternalErrorException, "Trying to access a destroyed gpu parameter.");
  176. return mInternalData->mSamplerStates[mParamDesc->slot];
  177. }
  178. }