BsGpuParam.cpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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->mIsDestroyed)
  39. BS_EXCEPT(InternalErrorException, "Trying to access a destroyed gpu parameter.");
  40. UINT32 elementSizeBytes = mParamDesc->elementSize * sizeof(UINT32);
  41. #if BS_DEBUG_MODE
  42. if(sizeBytes > elementSizeBytes)
  43. {
  44. LOGWRN("Provided element size larger than maximum element size. Maximum size: " +
  45. toString(elementSizeBytes) + ". Supplied size: " + toString(sizeBytes));
  46. }
  47. if (arrayIdx >= mParamDesc->arraySize)
  48. {
  49. BS_EXCEPT(InvalidParametersException, "Array index out of range. Array size: " +
  50. toString(mParamDesc->arraySize) + ". Requested size: " + toString(arrayIdx));
  51. }
  52. #endif
  53. sizeBytes = std::min(elementSizeBytes, sizeBytes);
  54. GpuParamBlockPtr paramBlock = mInternalData->mParamBlocks[mParamDesc->paramBlockSlot];
  55. paramBlock->write((mParamDesc->cpuMemOffset + arrayIdx * mParamDesc->arrayElementStride) * sizeof(UINT32), value, sizeBytes);
  56. // Set unused bytes to 0
  57. if(sizeBytes < elementSizeBytes)
  58. {
  59. UINT32 diffSize = elementSizeBytes - sizeBytes;
  60. paramBlock->zeroOut((mParamDesc->cpuMemOffset + arrayIdx * mParamDesc->arrayElementStride) * sizeof(UINT32)+sizeBytes, diffSize);
  61. }
  62. mInternalData->mCoreDirtyFlags = 0xFFFFFFFF;
  63. }
  64. void GpuParamStruct::get(void* value, UINT32 sizeBytes, UINT32 arrayIdx)
  65. {
  66. if (mInternalData->mIsDestroyed)
  67. BS_EXCEPT(InternalErrorException, "Trying to access a destroyed gpu parameter.");
  68. UINT32 elementSizeBytes = mParamDesc->elementSize * sizeof(UINT32);
  69. #if BS_DEBUG_MODE
  70. if(sizeBytes > elementSizeBytes)
  71. {
  72. LOGWRN("Provided element size larger than maximum element size. Maximum size: " +
  73. toString(elementSizeBytes) + ". Supplied size: " + toString(sizeBytes));
  74. }
  75. if (arrayIdx >= mParamDesc->arraySize)
  76. {
  77. BS_EXCEPT(InvalidParametersException, "Array index out of range. Array size: " +
  78. toString(mParamDesc->arraySize) + ". Requested size: " + toString(arrayIdx));
  79. }
  80. #endif
  81. sizeBytes = std::min(elementSizeBytes, sizeBytes);
  82. GpuParamBlockPtr paramBlock = mInternalData->mParamBlocks[mParamDesc->paramBlockSlot];
  83. paramBlock->read((mParamDesc->cpuMemOffset + arrayIdx * mParamDesc->arrayElementStride) * sizeof(UINT32), value, sizeBytes);
  84. }
  85. UINT32 GpuParamStruct::getElementSize() const
  86. {
  87. if(mInternalData->mIsDestroyed)
  88. BS_EXCEPT(InternalErrorException, "Trying to access a destroyed gpu parameter.");
  89. return mParamDesc->elementSize * sizeof(UINT32);
  90. }
  91. /************************************************************************/
  92. /* TEXTURE */
  93. /************************************************************************/
  94. GpuParamTexture::GpuParamTexture()
  95. :mParamDesc(nullptr)
  96. { }
  97. GpuParamTexture::GpuParamTexture(GpuParamObjectDesc* paramDesc, const std::shared_ptr<GpuParamsInternalData>& internalData)
  98. : mParamDesc(paramDesc), mInternalData(internalData)
  99. { }
  100. void GpuParamTexture::set(const HTexture& texture)
  101. {
  102. if (mInternalData->mIsDestroyed)
  103. BS_EXCEPT(InternalErrorException, "Trying to access a destroyed gpu parameter.");
  104. mInternalData->mTextures[mParamDesc->slot] = texture;
  105. mInternalData->mTextureInfo[mParamDesc->slot].isLoadStore = false;
  106. mInternalData->mCoreDirtyFlags = 0xFFFFFFFF;
  107. }
  108. HTexture GpuParamTexture::get()
  109. {
  110. if (mInternalData->mIsDestroyed)
  111. BS_EXCEPT(InternalErrorException, "Trying to access a destroyed gpu parameter.");
  112. return mInternalData->mTextures[mParamDesc->slot];
  113. }
  114. /************************************************************************/
  115. /* LOAD/STORE TEXTURE */
  116. /************************************************************************/
  117. GpuParamLoadStoreTexture::GpuParamLoadStoreTexture()
  118. :mParamDesc(nullptr)
  119. { }
  120. GpuParamLoadStoreTexture::GpuParamLoadStoreTexture(GpuParamObjectDesc* paramDesc, const std::shared_ptr<GpuParamsInternalData>& internalData)
  121. : mParamDesc(paramDesc), mInternalData(internalData)
  122. { }
  123. void GpuParamLoadStoreTexture::set(const HTexture& texture, const TextureSurface& surface)
  124. {
  125. if (mInternalData->mIsDestroyed)
  126. BS_EXCEPT(InternalErrorException, "Trying to access a destroyed gpu parameter.");
  127. mInternalData->mTextures[mParamDesc->slot] = texture;
  128. mInternalData->mTextureInfo[mParamDesc->slot].isLoadStore = true;
  129. mInternalData->mTextureInfo[mParamDesc->slot].surface = surface;
  130. mInternalData->mCoreDirtyFlags = 0xFFFFFFFF;
  131. }
  132. HTexture GpuParamLoadStoreTexture::get()
  133. {
  134. if (mInternalData->mIsDestroyed)
  135. BS_EXCEPT(InternalErrorException, "Trying to access a destroyed gpu parameter.");
  136. return mInternalData->mTextures[mParamDesc->slot];
  137. }
  138. /************************************************************************/
  139. /* SAMPLER STATE */
  140. /************************************************************************/
  141. GpuParamSampState::GpuParamSampState()
  142. :mParamDesc(nullptr)
  143. { }
  144. GpuParamSampState::GpuParamSampState(GpuParamObjectDesc* paramDesc, const std::shared_ptr<GpuParamsInternalData>& internalData)
  145. : mParamDesc(paramDesc), mInternalData(internalData)
  146. { }
  147. void GpuParamSampState::set(const HSamplerState& samplerState)
  148. {
  149. if (mInternalData->mIsDestroyed)
  150. BS_EXCEPT(InternalErrorException, "Trying to access a destroyed gpu parameter.");
  151. mInternalData->mSamplerStates[mParamDesc->slot] = samplerState;
  152. mInternalData->mCoreDirtyFlags = 0xFFFFFFFF;
  153. }
  154. HSamplerState GpuParamSampState::get()
  155. {
  156. if (mInternalData->mIsDestroyed)
  157. BS_EXCEPT(InternalErrorException, "Trying to access a destroyed gpu parameter.");
  158. return mInternalData->mSamplerStates[mParamDesc->slot];
  159. }
  160. }