BsGpuParam.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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. /************************************************************************/
  24. /* STRUCT */
  25. /************************************************************************/
  26. GpuParamStruct::GpuParamStruct()
  27. :mParamDesc(nullptr)
  28. { }
  29. GpuParamStruct::GpuParamStruct(GpuParamDataDesc* paramDesc, const std::shared_ptr<GpuParamsInternalData>& internalData)
  30. :mParamDesc(paramDesc), mInternalData(internalData)
  31. { }
  32. void GpuParamStruct::set(const void* value, UINT32 sizeBytes, UINT32 arrayIdx)
  33. {
  34. if (mInternalData->mIsDestroyed)
  35. BS_EXCEPT(InternalErrorException, "Trying to access a destroyed gpu parameter.");
  36. UINT32 elementSizeBytes = mParamDesc->elementSize * sizeof(UINT32);
  37. #if BS_DEBUG_MODE
  38. if(sizeBytes > elementSizeBytes)
  39. {
  40. LOGWRN("Provided element size larger than maximum element size. Maximum size: " +
  41. toString(elementSizeBytes) + ". Supplied size: " + toString(sizeBytes));
  42. }
  43. if (arrayIdx >= mParamDesc->arraySize)
  44. {
  45. BS_EXCEPT(InvalidParametersException, "Array index out of range. Array size: " +
  46. toString(mParamDesc->arraySize) + ". Requested size: " + toString(arrayIdx));
  47. }
  48. #endif
  49. sizeBytes = std::min(elementSizeBytes, sizeBytes);
  50. GpuParamBlock* paramBlock = mInternalData->mParamBlocks[mParamDesc->paramBlockSlot];
  51. paramBlock->write((mParamDesc->cpuMemOffset + arrayIdx * mParamDesc->arrayElementStride) * sizeof(UINT32), value, sizeBytes);
  52. // Set unused bytes to 0
  53. if(sizeBytes < elementSizeBytes)
  54. {
  55. UINT32 diffSize = elementSizeBytes - sizeBytes;
  56. paramBlock->zeroOut((mParamDesc->cpuMemOffset + arrayIdx * mParamDesc->arrayElementStride) * sizeof(UINT32)+sizeBytes, diffSize);
  57. }
  58. }
  59. void GpuParamStruct::get(void* value, UINT32 sizeBytes, UINT32 arrayIdx)
  60. {
  61. if (mInternalData->mIsDestroyed)
  62. BS_EXCEPT(InternalErrorException, "Trying to access a destroyed gpu parameter.");
  63. UINT32 elementSizeBytes = mParamDesc->elementSize * sizeof(UINT32);
  64. #if BS_DEBUG_MODE
  65. if(sizeBytes > elementSizeBytes)
  66. {
  67. LOGWRN("Provided element size larger than maximum element size. Maximum size: " +
  68. toString(elementSizeBytes) + ". Supplied size: " + toString(sizeBytes));
  69. }
  70. if (arrayIdx >= mParamDesc->arraySize)
  71. {
  72. BS_EXCEPT(InvalidParametersException, "Array index out of range. Array size: " +
  73. toString(mParamDesc->arraySize) + ". Requested size: " + toString(arrayIdx));
  74. }
  75. #endif
  76. sizeBytes = std::min(elementSizeBytes, sizeBytes);
  77. GpuParamBlock* paramBlock = mInternalData->mParamBlocks[mParamDesc->paramBlockSlot];
  78. paramBlock->read((mParamDesc->cpuMemOffset + arrayIdx * mParamDesc->arrayElementStride) * sizeof(UINT32), value, sizeBytes);
  79. }
  80. UINT32 GpuParamStruct::getElementSize() const
  81. {
  82. if(mInternalData->mIsDestroyed)
  83. BS_EXCEPT(InternalErrorException, "Trying to access a destroyed gpu parameter.");
  84. return mParamDesc->elementSize * sizeof(UINT32);
  85. }
  86. /************************************************************************/
  87. /* TEXTURE */
  88. /************************************************************************/
  89. GpuParamTexture::GpuParamTexture()
  90. :mParamDesc(nullptr)
  91. { }
  92. GpuParamTexture::GpuParamTexture(GpuParamObjectDesc* paramDesc, const std::shared_ptr<GpuParamsInternalData>& internalData)
  93. : mParamDesc(paramDesc), mInternalData(internalData)
  94. { }
  95. void GpuParamTexture::set(const HTexture& texture)
  96. {
  97. if (mInternalData->mIsDestroyed)
  98. BS_EXCEPT(InternalErrorException, "Trying to access a destroyed gpu parameter.");
  99. mInternalData->mTextures[mParamDesc->slot] = texture;
  100. }
  101. HTexture GpuParamTexture::get()
  102. {
  103. if (mInternalData->mIsDestroyed)
  104. BS_EXCEPT(InternalErrorException, "Trying to access a destroyed gpu parameter.");
  105. return mInternalData->mTextures[mParamDesc->slot];
  106. }
  107. /************************************************************************/
  108. /* SAMPLER STATE */
  109. /************************************************************************/
  110. GpuParamSampState::GpuParamSampState()
  111. :mParamDesc(nullptr)
  112. { }
  113. GpuParamSampState::GpuParamSampState(GpuParamObjectDesc* paramDesc, const std::shared_ptr<GpuParamsInternalData>& internalData)
  114. : mParamDesc(nullptr), mInternalData(internalData)
  115. { }
  116. void GpuParamSampState::set(const HSamplerState& samplerState)
  117. {
  118. if (mInternalData->mIsDestroyed)
  119. BS_EXCEPT(InternalErrorException, "Trying to access a destroyed gpu parameter.");
  120. mInternalData->mSamplerStates[mParamDesc->slot] = samplerState;
  121. }
  122. HSamplerState GpuParamSampState::get()
  123. {
  124. if (mInternalData->mIsDestroyed)
  125. BS_EXCEPT(InternalErrorException, "Trying to access a destroyed gpu parameter.");
  126. return mInternalData->mSamplerStates[mParamDesc->slot];
  127. }
  128. }