2
0

BsGpuParam.cpp 10.0 KB

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