BsGpuParam.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. //__________________________ Banshee Project - A modern game development toolkit _________________________________//
  2. //_____________________________________ www.banshee-project.com __________________________________________________//
  3. //________________________ Copyright (c) 2014 Marko Pintera. All rights reserved. ________________________________//
  4. #include "BsGpuParam.h"
  5. #include "BsGpuParams.h"
  6. namespace BansheeEngine
  7. {
  8. GpuDataParamBase::GpuDataParamBase()
  9. :mParamDesc(nullptr)
  10. { }
  11. GpuDataParamBase::GpuDataParamBase(GpuParamDataDesc* paramDesc, const std::shared_ptr<GpuParamsInternalData>& internalData)
  12. :mParamDesc(paramDesc), mInternalData(internalData)
  13. { }
  14. bool GpuDataParamBase::isDestroyed() const
  15. {
  16. return mInternalData->mIsDestroyed;
  17. }
  18. GpuParamBlockPtr GpuDataParamBase::getParamBlock(UINT32 slotIdx) const
  19. {
  20. return mInternalData->mParamBlocks[slotIdx];
  21. }
  22. bool GpuDataParamBase::getTransposeMatrices() const
  23. {
  24. return mInternalData->mTransposeMatrices;
  25. }
  26. void GpuDataParamBase::markCoreDirty()
  27. {
  28. mInternalData->mCoreDirtyFlags = 0xFFFFFFFF;
  29. }
  30. /************************************************************************/
  31. /* STRUCT */
  32. /************************************************************************/
  33. GpuParamStruct::GpuParamStruct()
  34. :mParamDesc(nullptr)
  35. { }
  36. GpuParamStruct::GpuParamStruct(GpuParamDataDesc* paramDesc, const std::shared_ptr<GpuParamsInternalData>& internalData)
  37. :mParamDesc(paramDesc), mInternalData(internalData)
  38. { }
  39. void GpuParamStruct::set(const void* value, UINT32 sizeBytes, UINT32 arrayIdx)
  40. {
  41. if (mInternalData->mIsDestroyed)
  42. BS_EXCEPT(InternalErrorException, "Trying to access a destroyed gpu parameter.");
  43. UINT32 elementSizeBytes = mParamDesc->elementSize * sizeof(UINT32);
  44. #if BS_DEBUG_MODE
  45. if(sizeBytes > elementSizeBytes)
  46. {
  47. LOGWRN("Provided element size larger than maximum element size. Maximum size: " +
  48. toString(elementSizeBytes) + ". Supplied size: " + toString(sizeBytes));
  49. }
  50. if (arrayIdx >= mParamDesc->arraySize)
  51. {
  52. BS_EXCEPT(InvalidParametersException, "Array index out of range. Array size: " +
  53. toString(mParamDesc->arraySize) + ". Requested size: " + toString(arrayIdx));
  54. }
  55. #endif
  56. sizeBytes = std::min(elementSizeBytes, sizeBytes);
  57. GpuParamBlockPtr paramBlock = mInternalData->mParamBlocks[mParamDesc->paramBlockSlot];
  58. paramBlock->write((mParamDesc->cpuMemOffset + arrayIdx * mParamDesc->arrayElementStride) * sizeof(UINT32), value, sizeBytes);
  59. // Set unused bytes to 0
  60. if(sizeBytes < elementSizeBytes)
  61. {
  62. UINT32 diffSize = elementSizeBytes - sizeBytes;
  63. paramBlock->zeroOut((mParamDesc->cpuMemOffset + arrayIdx * mParamDesc->arrayElementStride) * sizeof(UINT32)+sizeBytes, diffSize);
  64. }
  65. mInternalData->mCoreDirtyFlags = 0xFFFFFFFF;
  66. }
  67. void GpuParamStruct::get(void* value, UINT32 sizeBytes, UINT32 arrayIdx)
  68. {
  69. if (mInternalData->mIsDestroyed)
  70. BS_EXCEPT(InternalErrorException, "Trying to access a destroyed gpu parameter.");
  71. UINT32 elementSizeBytes = mParamDesc->elementSize * sizeof(UINT32);
  72. #if BS_DEBUG_MODE
  73. if(sizeBytes > elementSizeBytes)
  74. {
  75. LOGWRN("Provided element size larger than maximum element size. Maximum size: " +
  76. toString(elementSizeBytes) + ". Supplied size: " + toString(sizeBytes));
  77. }
  78. if (arrayIdx >= mParamDesc->arraySize)
  79. {
  80. BS_EXCEPT(InvalidParametersException, "Array index out of range. Array size: " +
  81. toString(mParamDesc->arraySize) + ". Requested size: " + toString(arrayIdx));
  82. }
  83. #endif
  84. sizeBytes = std::min(elementSizeBytes, sizeBytes);
  85. GpuParamBlockPtr paramBlock = mInternalData->mParamBlocks[mParamDesc->paramBlockSlot];
  86. paramBlock->read((mParamDesc->cpuMemOffset + arrayIdx * mParamDesc->arrayElementStride) * sizeof(UINT32), value, sizeBytes);
  87. }
  88. UINT32 GpuParamStruct::getElementSize() const
  89. {
  90. if(mInternalData->mIsDestroyed)
  91. BS_EXCEPT(InternalErrorException, "Trying to access a destroyed gpu parameter.");
  92. return mParamDesc->elementSize * sizeof(UINT32);
  93. }
  94. /************************************************************************/
  95. /* TEXTURE */
  96. /************************************************************************/
  97. GpuParamTexture::GpuParamTexture()
  98. :mParamDesc(nullptr)
  99. { }
  100. GpuParamTexture::GpuParamTexture(GpuParamObjectDesc* paramDesc, const std::shared_ptr<GpuParamsInternalData>& internalData)
  101. : mParamDesc(paramDesc), mInternalData(internalData)
  102. { }
  103. void GpuParamTexture::set(const HTexture& texture)
  104. {
  105. if (mInternalData->mIsDestroyed)
  106. BS_EXCEPT(InternalErrorException, "Trying to access a destroyed gpu parameter.");
  107. mInternalData->mTextures[mParamDesc->slot] = texture;
  108. mInternalData->mCoreDirtyFlags = 0xFFFFFFFF;
  109. }
  110. HTexture GpuParamTexture::get()
  111. {
  112. if (mInternalData->mIsDestroyed)
  113. BS_EXCEPT(InternalErrorException, "Trying to access a destroyed gpu parameter.");
  114. return mInternalData->mTextures[mParamDesc->slot];
  115. }
  116. /************************************************************************/
  117. /* SAMPLER STATE */
  118. /************************************************************************/
  119. GpuParamSampState::GpuParamSampState()
  120. :mParamDesc(nullptr)
  121. { }
  122. GpuParamSampState::GpuParamSampState(GpuParamObjectDesc* paramDesc, const std::shared_ptr<GpuParamsInternalData>& internalData)
  123. : mParamDesc(paramDesc), mInternalData(internalData)
  124. { }
  125. void GpuParamSampState::set(const HSamplerState& samplerState)
  126. {
  127. if (mInternalData->mIsDestroyed)
  128. BS_EXCEPT(InternalErrorException, "Trying to access a destroyed gpu parameter.");
  129. mInternalData->mSamplerStates[mParamDesc->slot] = samplerState;
  130. mInternalData->mCoreDirtyFlags = 0xFFFFFFFF;
  131. }
  132. HSamplerState GpuParamSampState::get()
  133. {
  134. if (mInternalData->mIsDestroyed)
  135. BS_EXCEPT(InternalErrorException, "Trying to access a destroyed gpu parameter.");
  136. return mInternalData->mSamplerStates[mParamDesc->slot];
  137. }
  138. }