BsGpuParam.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  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/BsVector2I.h"
  11. namespace bs
  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->paramBlockSet, 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. bool transposeMatrices = ct::RenderAPI::instance().getAPIInfo().isFlagSet(RenderAPIFeatureFlag::ColumnMajorMatrices);
  39. if (TransposePolicy<T>::transposeEnabled(transposeMatrices))
  40. {
  41. auto transposed = TransposePolicy<T>::transpose(value);
  42. paramBlock->write((mParamDesc->cpuMemOffset + arrayIdx * mParamDesc->arrayElementStride) * sizeof(UINT32), &transposed, sizeBytes);
  43. }
  44. else
  45. paramBlock->write((mParamDesc->cpuMemOffset + arrayIdx * mParamDesc->arrayElementStride) * sizeof(UINT32), &value, sizeBytes);
  46. // Set unused bytes to 0
  47. if (sizeBytes < elementSizeBytes)
  48. {
  49. UINT32 diffSize = elementSizeBytes - sizeBytes;
  50. paramBlock->zeroOut((mParamDesc->cpuMemOffset + arrayIdx * mParamDesc->arrayElementStride) * sizeof(UINT32) + sizeBytes, diffSize);
  51. }
  52. mParent->_markCoreDirty();
  53. }
  54. template<class T, bool Core>
  55. T TGpuDataParam<T, Core>::get(UINT32 arrayIdx) const
  56. {
  57. if (mParent == nullptr)
  58. return T();
  59. GpuParamBufferType paramBlock = mParent->getParamBlockBuffer(mParamDesc->paramBlockSet, mParamDesc->paramBlockSlot);
  60. if (paramBlock == nullptr)
  61. return T();
  62. #if BS_DEBUG_MODE
  63. if (arrayIdx >= mParamDesc->arraySize)
  64. {
  65. BS_EXCEPT(InvalidParametersException, "Array index out of range. Array size: " +
  66. toString(mParamDesc->arraySize) + ". Requested size: " + toString(arrayIdx));
  67. }
  68. #endif
  69. UINT32 elementSizeBytes = mParamDesc->elementSize * sizeof(UINT32);
  70. UINT32 sizeBytes = std::min(elementSizeBytes, (UINT32)sizeof(T));
  71. T value;
  72. paramBlock->read((mParamDesc->cpuMemOffset + arrayIdx * mParamDesc->arrayElementStride) * sizeof(UINT32), &value, sizeBytes);
  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. :mParent(parent), mParamDesc(paramDesc)
  82. { }
  83. template<bool Core>
  84. void TGpuParamStruct<Core>::set(const void* value, UINT32 sizeBytes, UINT32 arrayIdx) const
  85. {
  86. if (mParent == nullptr)
  87. return;
  88. GpuParamBufferType paramBlock = mParent->getParamBlockBuffer(mParamDesc->paramBlockSet, 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) const
  116. {
  117. if (mParent == nullptr)
  118. return;
  119. GpuParamBufferType paramBlock = mParent->getParamBlockBuffer(mParamDesc->paramBlockSet, 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. :mParent(parent), mParamDesc(paramDesc)
  152. { }
  153. template<bool Core>
  154. void TGpuParamTexture<Core>::set(const TextureType& texture, const TextureSurface& surface) const
  155. {
  156. if (mParent == nullptr)
  157. return;
  158. mParent->setTexture(mParamDesc->set, mParamDesc->slot, texture, surface);
  159. mParent->_markResourcesDirty();
  160. mParent->_markCoreDirty();
  161. }
  162. template<bool Core>
  163. typename TGpuParamTexture<Core>::TextureType TGpuParamTexture<Core>::get() const
  164. {
  165. if (mParent == nullptr)
  166. return TextureType();
  167. return mParent->getTexture(mParamDesc->set, mParamDesc->slot);
  168. }
  169. template<bool Core>
  170. TGpuParamBuffer<Core>::TGpuParamBuffer()
  171. :mParamDesc(nullptr)
  172. { }
  173. template<bool Core>
  174. TGpuParamBuffer<Core>::TGpuParamBuffer(GpuParamObjectDesc* paramDesc, const GpuParamsType& parent)
  175. : mParent(parent), mParamDesc(paramDesc)
  176. { }
  177. template<bool Core>
  178. void TGpuParamBuffer<Core>::set(const BufferType& buffer) const
  179. {
  180. if (mParent == nullptr)
  181. return;
  182. mParent->setBuffer(mParamDesc->set, mParamDesc->slot, buffer);
  183. mParent->_markResourcesDirty();
  184. mParent->_markCoreDirty();
  185. }
  186. template<bool Core>
  187. typename TGpuParamBuffer<Core>::BufferType TGpuParamBuffer<Core>::get() const
  188. {
  189. if (mParent == nullptr)
  190. return BufferType();
  191. return mParent->getBuffer(mParamDesc->set, mParamDesc->slot);
  192. }
  193. template<bool Core>
  194. TGpuParamLoadStoreTexture<Core>::TGpuParamLoadStoreTexture()
  195. :mParamDesc(nullptr)
  196. { }
  197. template<bool Core>
  198. TGpuParamLoadStoreTexture<Core>::TGpuParamLoadStoreTexture(GpuParamObjectDesc* paramDesc, const GpuParamsType& parent)
  199. :mParent(parent), mParamDesc(paramDesc)
  200. { }
  201. template<bool Core>
  202. void TGpuParamLoadStoreTexture<Core>::set(const TextureType& texture, const TextureSurface& surface) const
  203. {
  204. if (mParent == nullptr)
  205. return;
  206. mParent->setLoadStoreTexture(mParamDesc->set, mParamDesc->slot, texture, surface);
  207. mParent->_markResourcesDirty();
  208. mParent->_markCoreDirty();
  209. }
  210. template<bool Core>
  211. typename TGpuParamLoadStoreTexture<Core>::TextureType TGpuParamLoadStoreTexture<Core>::get() const
  212. {
  213. if (mParent == nullptr)
  214. return TextureType();
  215. return mParent->getTexture(mParamDesc->set, mParamDesc->slot);
  216. }
  217. template<bool Core>
  218. TGpuParamSampState<Core>::TGpuParamSampState()
  219. :mParamDesc(nullptr)
  220. { }
  221. template<bool Core>
  222. TGpuParamSampState<Core>::TGpuParamSampState(GpuParamObjectDesc* paramDesc, const GpuParamsType& parent)
  223. :mParent(parent), mParamDesc(paramDesc)
  224. { }
  225. template<bool Core>
  226. void TGpuParamSampState<Core>::set(const SamplerStateType& samplerState) const
  227. {
  228. if (mParent == nullptr)
  229. return;
  230. mParent->setSamplerState(mParamDesc->set, mParamDesc->slot, samplerState);
  231. mParent->_markResourcesDirty();
  232. mParent->_markCoreDirty();
  233. }
  234. template<bool Core>
  235. typename TGpuParamSampState<Core>::SamplerStateType TGpuParamSampState<Core>::get() const
  236. {
  237. if (mParent == nullptr)
  238. return SamplerStateType();
  239. return mParent->getSamplerState(mParamDesc->set, mParamDesc->slot);
  240. }
  241. template class TGpuDataParam < float, false > ;
  242. template class TGpuDataParam < int, false >;
  243. template class TGpuDataParam < Color, false > ;
  244. template class TGpuDataParam < Vector2, false > ;
  245. template class TGpuDataParam < Vector3, false > ;
  246. template class TGpuDataParam < Vector4, false > ;
  247. template class TGpuDataParam < Vector2I, false > ;
  248. template class TGpuDataParam < Vector3I, false > ;
  249. template class TGpuDataParam < Vector4I, false > ;
  250. template class TGpuDataParam < Matrix2, false >;
  251. template class TGpuDataParam < Matrix2x3, false >;
  252. template class TGpuDataParam < Matrix2x4, false >;
  253. template class TGpuDataParam < Matrix3, false > ;
  254. template class TGpuDataParam < Matrix3x2, false > ;
  255. template class TGpuDataParam < Matrix3x4, false > ;
  256. template class TGpuDataParam < Matrix4, false > ;
  257. template class TGpuDataParam < Matrix4x2, false >;
  258. template class TGpuDataParam < Matrix4x3, false >;
  259. template class TGpuDataParam < float, true > ;
  260. template class TGpuDataParam < int, true >;
  261. template class TGpuDataParam < Color, true > ;
  262. template class TGpuDataParam < Vector2, true > ;
  263. template class TGpuDataParam < Vector3, true > ;
  264. template class TGpuDataParam < Vector4, true > ;
  265. template class TGpuDataParam < Vector2I, true > ;
  266. template class TGpuDataParam < Vector3I, true > ;
  267. template class TGpuDataParam < Vector4I, true > ;
  268. template class TGpuDataParam < Matrix2, true >;
  269. template class TGpuDataParam < Matrix2x3, true >;
  270. template class TGpuDataParam < Matrix2x4, true >;
  271. template class TGpuDataParam < Matrix3, true > ;
  272. template class TGpuDataParam < Matrix3x2, true >;
  273. template class TGpuDataParam < Matrix3x4, true >;
  274. template class TGpuDataParam < Matrix4, true > ;
  275. template class TGpuDataParam < Matrix4x2, true >;
  276. template class TGpuDataParam < Matrix4x3, true >;
  277. template class TGpuParamStruct < false > ;
  278. template class TGpuParamStruct < true > ;
  279. template class TGpuParamTexture < false > ;
  280. template class TGpuParamTexture < true > ;
  281. template class TGpuParamBuffer < false >;
  282. template class TGpuParamBuffer < true >;
  283. template class TGpuParamSampState < false > ;
  284. template class TGpuParamSampState < true > ;
  285. template class TGpuParamLoadStoreTexture < false > ;
  286. template class TGpuParamLoadStoreTexture < true > ;
  287. }