BsGpuParam.cpp 11 KB

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