BsGpuParam.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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::getIsDestroyed() const
  12. {
  13. return mInternalData->mIsDestroyed;
  14. }
  15. GpuParamBlock* 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. GpuParamBlock* 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. GpuParamBlock* 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->mCoreDirtyFlags = 0xFFFFFFFF;
  106. }
  107. HTexture GpuParamTexture::get()
  108. {
  109. if (mInternalData->mIsDestroyed)
  110. BS_EXCEPT(InternalErrorException, "Trying to access a destroyed gpu parameter.");
  111. return mInternalData->mTextures[mParamDesc->slot];
  112. }
  113. /************************************************************************/
  114. /* SAMPLER STATE */
  115. /************************************************************************/
  116. GpuParamSampState::GpuParamSampState()
  117. :mParamDesc(nullptr)
  118. { }
  119. GpuParamSampState::GpuParamSampState(GpuParamObjectDesc* paramDesc, const std::shared_ptr<GpuParamsInternalData>& internalData)
  120. : mParamDesc(nullptr), mInternalData(internalData)
  121. { }
  122. void GpuParamSampState::set(const HSamplerState& samplerState)
  123. {
  124. if (mInternalData->mIsDestroyed)
  125. BS_EXCEPT(InternalErrorException, "Trying to access a destroyed gpu parameter.");
  126. mInternalData->mSamplerStates[mParamDesc->slot] = samplerState;
  127. mInternalData->mCoreDirtyFlags = 0xFFFFFFFF;
  128. }
  129. HSamplerState GpuParamSampState::get()
  130. {
  131. if (mInternalData->mIsDestroyed)
  132. BS_EXCEPT(InternalErrorException, "Trying to access a destroyed gpu parameter.");
  133. return mInternalData->mSamplerStates[mParamDesc->slot];
  134. }
  135. }