BsGpuParam.cpp 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. #include "BsGpuParam.h"
  2. #include "BsGpuParams.h"
  3. #include "BsGpuParamBlockBuffer.h"
  4. #include "BsGpuParamDesc.h"
  5. #include "BsDebug.h"
  6. #include "BsException.h"
  7. namespace BansheeEngine
  8. {
  9. template<class T, bool Core>
  10. TGpuDataParam<T, Core>::TGpuDataParam()
  11. :mParamDesc(nullptr)
  12. { }
  13. template<class T, bool Core>
  14. TGpuDataParam<T, Core>::TGpuDataParam(GpuParamDataDesc* paramDesc, const GpuParamsType& parent)
  15. :mParamDesc(paramDesc), mParent(parent)
  16. { }
  17. template<class T, bool Core>
  18. void TGpuDataParam<T, Core>::set(const T& value, UINT32 arrayIdx)
  19. {
  20. if (mParent == nullptr)
  21. return;
  22. GpuParamBufferType paramBlock = mParent->getParamBlockBuffer(mParamDesc->paramBlockSlot);
  23. if (paramBlock == nullptr)
  24. return;
  25. #if BS_DEBUG_MODE
  26. if (arrayIdx >= mParamDesc->arraySize)
  27. {
  28. BS_EXCEPT(InvalidParametersException, "Array index out of range. Array size: " +
  29. toString(mParamDesc->arraySize) + ". Requested size: " + toString(arrayIdx));
  30. }
  31. #endif
  32. UINT32 elementSizeBytes = mParamDesc->elementSize * sizeof(UINT32);
  33. UINT32 sizeBytes = std::min(elementSizeBytes, (UINT32)sizeof(T)); // Truncate if it doesn't fit within parameter size
  34. if (TransposePolicy<T>::transposeEnabled(mParent->getTransposeMatrices()))
  35. {
  36. T transposed = TransposePolicy<T>::transpose(value);
  37. paramBlock->write((mParamDesc->cpuMemOffset + arrayIdx * mParamDesc->arrayElementStride) * sizeof(UINT32), &transposed, sizeBytes);
  38. }
  39. else
  40. paramBlock->write((mParamDesc->cpuMemOffset + arrayIdx * mParamDesc->arrayElementStride) * sizeof(UINT32), &value, sizeBytes);
  41. // Set unused bytes to 0
  42. if (sizeBytes < elementSizeBytes)
  43. {
  44. UINT32 diffSize = elementSizeBytes - sizeBytes;
  45. paramBlock->zeroOut((mParamDesc->cpuMemOffset + arrayIdx * mParamDesc->arrayElementStride) * sizeof(UINT32) + sizeBytes, diffSize);
  46. }
  47. mParent->_markCoreDirty();
  48. }
  49. template<class T, bool Core>
  50. T TGpuDataParam<T, Core>::get(UINT32 arrayIdx)
  51. {
  52. if (mParent == nullptr)
  53. return T();
  54. GpuParamBufferType paramBlock = mParent->getParamBlockBuffer(mParamDesc->paramBlockSlot);
  55. if (paramBlock == nullptr)
  56. return T();
  57. #if BS_DEBUG_MODE
  58. if (arrayIdx >= mParamDesc->arraySize)
  59. {
  60. BS_EXCEPT(InvalidParametersException, "Array index out of range. Array size: " +
  61. toString(mParamDesc->arraySize) + ". Requested size: " + toString(arrayIdx));
  62. }
  63. #endif
  64. UINT32 elementSizeBytes = mParamDesc->elementSize * sizeof(UINT32);
  65. UINT32 sizeBytes = std::min(elementSizeBytes, (UINT32)sizeof(T));
  66. T value;
  67. paramBlock->read((mParamDesc->cpuMemOffset + arrayIdx * mParamDesc->arrayElementStride) * sizeof(UINT32), &value, sizeBytes);
  68. if (TransposePolicy<T>::transposeEnabled(mParent->getTransposeMatrices()))
  69. return TransposePolicy<T>::transpose(value);
  70. else
  71. return value;
  72. }
  73. template<bool Core>
  74. TGpuParamStruct<Core>::TGpuParamStruct()
  75. :mParamDesc(nullptr)
  76. { }
  77. template<bool Core>
  78. TGpuParamStruct<Core>::TGpuParamStruct(GpuParamDataDesc* paramDesc, const GpuParamsType& parent)
  79. :mParamDesc(paramDesc), mParent(parent)
  80. { }
  81. template<bool Core>
  82. void TGpuParamStruct<Core>::set(const void* value, UINT32 sizeBytes, UINT32 arrayIdx)
  83. {
  84. if (mParent == nullptr)
  85. return;
  86. GpuParamBufferType paramBlock = mParent->getParamBlockBuffer(mParamDesc->paramBlockSlot);
  87. if (paramBlock == nullptr)
  88. return;
  89. UINT32 elementSizeBytes = mParamDesc->elementSize * sizeof(UINT32);
  90. #if BS_DEBUG_MODE
  91. if (sizeBytes > elementSizeBytes)
  92. {
  93. LOGWRN("Provided element size larger than maximum element size. Maximum size: " +
  94. toString(elementSizeBytes) + ". Supplied size: " + toString(sizeBytes));
  95. }
  96. if (arrayIdx >= mParamDesc->arraySize)
  97. {
  98. BS_EXCEPT(InvalidParametersException, "Array index out of range. Array size: " +
  99. toString(mParamDesc->arraySize) + ". Requested size: " + toString(arrayIdx));
  100. }
  101. #endif
  102. sizeBytes = std::min(elementSizeBytes, sizeBytes);
  103. paramBlock->write((mParamDesc->cpuMemOffset + arrayIdx * mParamDesc->arrayElementStride) * sizeof(UINT32), value, sizeBytes);
  104. // Set unused bytes to 0
  105. if (sizeBytes < elementSizeBytes)
  106. {
  107. UINT32 diffSize = elementSizeBytes - sizeBytes;
  108. paramBlock->zeroOut((mParamDesc->cpuMemOffset + arrayIdx * mParamDesc->arrayElementStride) * sizeof(UINT32) + sizeBytes, diffSize);
  109. }
  110. mParent->_markCoreDirty();
  111. }
  112. template<bool Core>
  113. void TGpuParamStruct<Core>::get(void* value, UINT32 sizeBytes, UINT32 arrayIdx)
  114. {
  115. if (mParent == nullptr)
  116. return;
  117. GpuParamBufferType paramBlock = mParent->getParamBlockBuffer(mParamDesc->paramBlockSlot);
  118. if (paramBlock == nullptr)
  119. return;
  120. UINT32 elementSizeBytes = mParamDesc->elementSize * sizeof(UINT32);
  121. #if BS_DEBUG_MODE
  122. if (sizeBytes > elementSizeBytes)
  123. {
  124. LOGWRN("Provided element size larger than maximum element size. Maximum size: " +
  125. toString(elementSizeBytes) + ". Supplied size: " + toString(sizeBytes));
  126. }
  127. if (arrayIdx >= mParamDesc->arraySize)
  128. {
  129. BS_EXCEPT(InvalidParametersException, "Array index out of range. Array size: " +
  130. toString(mParamDesc->arraySize) + ". Requested size: " + toString(arrayIdx));
  131. }
  132. #endif
  133. sizeBytes = std::min(elementSizeBytes, sizeBytes);
  134. paramBlock->read((mParamDesc->cpuMemOffset + arrayIdx * mParamDesc->arrayElementStride) * sizeof(UINT32), value, sizeBytes);
  135. }
  136. template<bool Core>
  137. UINT32 TGpuParamStruct<Core>::getElementSize() const
  138. {
  139. if (mParent == nullptr)
  140. return 0;
  141. return mParamDesc->elementSize * sizeof(UINT32);
  142. }
  143. template<bool Core>
  144. TGpuParamTexture<Core>::TGpuParamTexture()
  145. :mParamDesc(nullptr)
  146. { }
  147. template<bool Core>
  148. TGpuParamTexture<Core>::TGpuParamTexture(GpuParamObjectDesc* paramDesc, const GpuParamsType& parent)
  149. :mParamDesc(paramDesc), mParent(parent)
  150. { }
  151. template<bool Core>
  152. void TGpuParamTexture<Core>::set(const TextureType& texture)
  153. {
  154. if (mParent == nullptr)
  155. return;
  156. mParent->setTexture(mParamDesc->slot, texture);
  157. mParent->setIsLoadStoreTexture(mParamDesc->slot, false);
  158. mParent->_markResourcesDirty();
  159. mParent->_markCoreDirty();
  160. }
  161. template<bool Core>
  162. typename TGpuParamTexture<Core>::TextureType TGpuParamTexture<Core>::get()
  163. {
  164. if (mParent == nullptr)
  165. return TextureType();
  166. return mParent->getTexture(mParamDesc->slot);
  167. }
  168. template<bool Core>
  169. TGpuParamLoadStoreTexture<Core>::TGpuParamLoadStoreTexture()
  170. :mParamDesc(nullptr)
  171. { }
  172. template<bool Core>
  173. TGpuParamLoadStoreTexture<Core>::TGpuParamLoadStoreTexture(GpuParamObjectDesc* paramDesc, const GpuParamsType& parent)
  174. :mParamDesc(paramDesc), mParent(parent)
  175. { }
  176. template<bool Core>
  177. void TGpuParamLoadStoreTexture<Core>::set(const TextureType& texture, const TextureSurface& surface)
  178. {
  179. if (mParent == nullptr)
  180. return;
  181. mParent->setTexture(mParamDesc->slot, texture);
  182. mParent->setIsLoadStoreTexture(mParamDesc->slot, true);
  183. mParent->setLoadStoreSurface(mParamDesc->slot, surface);
  184. mParent->_markResourcesDirty();
  185. mParent->_markCoreDirty();
  186. }
  187. template<bool Core>
  188. typename TGpuParamLoadStoreTexture<Core>::TextureType TGpuParamLoadStoreTexture<Core>::get()
  189. {
  190. if (mParent == nullptr)
  191. return TextureType();
  192. return mParent->getTexture(mParamDesc->slot);
  193. }
  194. template<bool Core>
  195. TGpuParamSampState<Core>::TGpuParamSampState()
  196. :mParamDesc(nullptr)
  197. { }
  198. template<bool Core>
  199. TGpuParamSampState<Core>::TGpuParamSampState(GpuParamObjectDesc* paramDesc, const GpuParamsType& parent)
  200. :mParamDesc(paramDesc), mParent(parent)
  201. { }
  202. template<bool Core>
  203. void TGpuParamSampState<Core>::set(const SamplerStateType& samplerState)
  204. {
  205. if (mParent == nullptr)
  206. return;
  207. mParent->setSamplerState(mParamDesc->slot, samplerState);
  208. mParent->_markResourcesDirty();
  209. mParent->_markCoreDirty();
  210. }
  211. template<bool Core>
  212. typename TGpuParamSampState<Core>::SamplerStateType TGpuParamSampState<Core>::get()
  213. {
  214. if (mParent == nullptr)
  215. return SamplerStateType();
  216. return mParent->getSamplerState(mParamDesc->slot);
  217. }
  218. template class TGpuDataParam < float, false > ;
  219. template class TGpuDataParam < Color, false > ;
  220. template class TGpuDataParam < Vector2, false > ;
  221. template class TGpuDataParam < Vector3, false > ;
  222. template class TGpuDataParam < Vector4, false > ;
  223. template class TGpuDataParam < Matrix3, false > ;
  224. template class TGpuDataParam < Matrix4, false > ;
  225. template class TGpuDataParam < float, true > ;
  226. template class TGpuDataParam < Color, true > ;
  227. template class TGpuDataParam < Vector2, true > ;
  228. template class TGpuDataParam < Vector3, true > ;
  229. template class TGpuDataParam < Vector4, true > ;
  230. template class TGpuDataParam < Matrix3, true > ;
  231. template class TGpuDataParam < Matrix4, true > ;
  232. template class TGpuParamStruct < false > ;
  233. template class TGpuParamStruct < true > ;
  234. template class TGpuParamTexture < false > ;
  235. template class TGpuParamTexture < true > ;
  236. template class TGpuParamSampState < false > ;
  237. template class TGpuParamSampState < true > ;
  238. template class TGpuParamLoadStoreTexture < false > ;
  239. template class TGpuParamLoadStoreTexture < true > ;
  240. }