BsGpuParam.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. #include "BsGpuParam.h"
  2. namespace BansheeEngine
  3. {
  4. /************************************************************************/
  5. /* STRUCT */
  6. /************************************************************************/
  7. GpuParamStruct::InternalData::InternalData(GpuParamDataDesc* paramDesc, GpuParamBlock** paramBlocks)
  8. :paramDesc(paramDesc), paramBlocks(paramBlocks), isDestroyed(false)
  9. { }
  10. GpuParamStruct::InternalData::InternalData()
  11. :paramDesc(nullptr), paramBlocks(nullptr), isDestroyed(true)
  12. { }
  13. GpuParamStruct::InternalData::~InternalData()
  14. { }
  15. GpuParamStruct::GpuParamStruct()
  16. :mData(bs_shared_ptr<InternalData>())
  17. { }
  18. GpuParamStruct::GpuParamStruct(GpuParamDataDesc* paramDesc, GpuParamBlock** paramBlocks)
  19. :mData(bs_shared_ptr<InternalData>(paramDesc, paramBlocks))
  20. { }
  21. void GpuParamStruct::set(const void* value, UINT32 sizeBytes, UINT32 arrayIdx)
  22. {
  23. if(mData->isDestroyed)
  24. BS_EXCEPT(InternalErrorException, "Trying to access a destroyed gpu parameter.");
  25. GpuParamDataDesc* paramDesc = mData->paramDesc;
  26. UINT32 elementSizeBytes = paramDesc->elementSize * sizeof(UINT32);
  27. #if BS_DEBUG_MODE
  28. if(sizeBytes > elementSizeBytes)
  29. {
  30. LOGWRN("Provided element size larger than maximum element size. Maximum size: " +
  31. toString(elementSizeBytes) + ". Supplied size: " + toString(sizeBytes));
  32. }
  33. if(arrayIdx >= paramDesc->arraySize)
  34. {
  35. BS_EXCEPT(InvalidParametersException, "Array index out of range. Array size: " +
  36. toString(paramDesc->arraySize) + ". Requested size: " + toString(arrayIdx));
  37. }
  38. #endif
  39. sizeBytes = std::min(elementSizeBytes, sizeBytes);
  40. GpuParamBlock* paramBlock = mData->paramBlocks[paramDesc->paramBlockSlot];
  41. paramBlock->write((paramDesc->cpuMemOffset + arrayIdx * paramDesc->arrayElementStride) * sizeof(UINT32), value, sizeBytes);
  42. // Set unused bytes to 0
  43. if(sizeBytes < elementSizeBytes)
  44. {
  45. UINT32 diffSize = elementSizeBytes - sizeBytes;
  46. paramBlock->zeroOut((paramDesc->cpuMemOffset + arrayIdx * paramDesc->arrayElementStride) * sizeof(UINT32) + sizeBytes, diffSize);
  47. }
  48. }
  49. void GpuParamStruct::get(void* value, UINT32 sizeBytes, UINT32 arrayIdx)
  50. {
  51. if(mData->isDestroyed)
  52. BS_EXCEPT(InternalErrorException, "Trying to access a destroyed gpu parameter.");
  53. GpuParamDataDesc* paramDesc = mData->paramDesc;
  54. UINT32 elementSizeBytes = paramDesc->elementSize * sizeof(UINT32);
  55. #if BS_DEBUG_MODE
  56. if(sizeBytes > elementSizeBytes)
  57. {
  58. LOGWRN("Provided element size larger than maximum element size. Maximum size: " +
  59. toString(elementSizeBytes) + ". Supplied size: " + toString(sizeBytes));
  60. }
  61. if(arrayIdx >= paramDesc->arraySize)
  62. {
  63. BS_EXCEPT(InvalidParametersException, "Array index out of range. Array size: " +
  64. toString(paramDesc->arraySize) + ". Requested size: " + toString(arrayIdx));
  65. }
  66. #endif
  67. sizeBytes = std::min(elementSizeBytes, sizeBytes);
  68. GpuParamBlock* paramBlock = mData->paramBlocks[paramDesc->paramBlockSlot];
  69. paramBlock->read((paramDesc->cpuMemOffset + arrayIdx * paramDesc->arrayElementStride) * sizeof(UINT32), value, sizeBytes);
  70. }
  71. UINT32 GpuParamStruct::getElementSize() const
  72. {
  73. if(mData->isDestroyed)
  74. BS_EXCEPT(InternalErrorException, "Trying to access a destroyed gpu parameter.");
  75. GpuParamDataDesc* paramDesc = mData->paramDesc;
  76. return paramDesc->elementSize * sizeof(UINT32);
  77. }
  78. void GpuParamStruct::_destroy()
  79. {
  80. mData->isDestroyed = true;
  81. }
  82. /************************************************************************/
  83. /* TEXTURE */
  84. /************************************************************************/
  85. GpuParamTexture::InternalData::InternalData(GpuParamObjectDesc* paramDesc, HTexture* textures)
  86. :paramDesc(paramDesc), textures(textures), isDestroyed(false)
  87. { }
  88. GpuParamTexture::InternalData::InternalData()
  89. :paramDesc(nullptr), textures(nullptr), isDestroyed(true)
  90. { }
  91. GpuParamTexture::InternalData::~InternalData()
  92. { }
  93. GpuParamTexture::GpuParamTexture()
  94. :mData(bs_shared_ptr<InternalData>())
  95. { }
  96. GpuParamTexture::GpuParamTexture(GpuParamObjectDesc* paramDesc, HTexture* textures)
  97. :mData(bs_shared_ptr<InternalData>(paramDesc, textures))
  98. { }
  99. void GpuParamTexture::set(const HTexture& texture)
  100. {
  101. if(mData->isDestroyed)
  102. BS_EXCEPT(InternalErrorException, "Trying to access a destroyed gpu parameter.");
  103. mData->textures[mData->paramDesc->slot] = texture;
  104. }
  105. HTexture GpuParamTexture::get()
  106. {
  107. if(mData->isDestroyed)
  108. BS_EXCEPT(InternalErrorException, "Trying to access a destroyed gpu parameter.");
  109. return mData->textures[mData->paramDesc->slot];
  110. }
  111. void GpuParamTexture::_destroy()
  112. {
  113. mData->isDestroyed = true;
  114. }
  115. /************************************************************************/
  116. /* SAMPLER STATE */
  117. /************************************************************************/
  118. GpuParamSampState::InternalData::InternalData(GpuParamObjectDesc* paramDesc, HSamplerState* samplerStates)
  119. :paramDesc(paramDesc), samplerStates(samplerStates), isDestroyed(false)
  120. { }
  121. GpuParamSampState::InternalData::InternalData()
  122. :paramDesc(nullptr), samplerStates(nullptr), isDestroyed(true)
  123. { }
  124. GpuParamSampState::InternalData::~InternalData()
  125. { }
  126. GpuParamSampState::GpuParamSampState()
  127. :mData(bs_shared_ptr<InternalData>())
  128. { }
  129. GpuParamSampState::GpuParamSampState(GpuParamObjectDesc* paramDesc, HSamplerState* samplerStates)
  130. :mData(bs_shared_ptr<InternalData>(paramDesc, samplerStates))
  131. { }
  132. void GpuParamSampState::set(const HSamplerState& samplerState)
  133. {
  134. if(mData->isDestroyed)
  135. BS_EXCEPT(InternalErrorException, "Trying to access a destroyed gpu parameter.");
  136. mData->samplerStates[mData->paramDesc->slot] = samplerState;
  137. }
  138. HSamplerState GpuParamSampState::get()
  139. {
  140. if(mData->isDestroyed)
  141. BS_EXCEPT(InternalErrorException, "Trying to access a destroyed gpu parameter.");
  142. return mData->samplerStates[mData->paramDesc->slot];
  143. }
  144. void GpuParamSampState::_destroy()
  145. {
  146. mData->isDestroyed = true;
  147. }
  148. }