BsGpuParam.cpp 11 KB

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