BsGpuParam.h 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. #pragma once
  2. #include "BsCorePrerequisites.h"
  3. #include "BsGpuParamDesc.h"
  4. #include "BsGpuParamBlock.h"
  5. #include "BsDebug.h"
  6. #include "BsException.h"
  7. #include "BsVector2.h"
  8. #include "BsVector3.h"
  9. #include "BsVector4.h"
  10. #include "BsMatrix3.h"
  11. #include "BsMatrix4.h"
  12. #include "BsColor.h"
  13. namespace BansheeEngine
  14. {
  15. struct GpuParamsInternalData;
  16. /**
  17. * @brief Base class containing some non-templated methods for
  18. * GpuDataParam.
  19. *
  20. * @see TGpuDataParam
  21. */
  22. class BS_CORE_EXPORT GpuDataParamBase
  23. {
  24. protected:
  25. GpuDataParamBase();
  26. GpuDataParamBase(GpuParamDataDesc* paramDesc, const std::shared_ptr<GpuParamsInternalData>& internalData);
  27. /**
  28. * @brief Checks if the gpu param is still valid or were the parent GpuParams destroyed.
  29. */
  30. bool isDestroyed() const;
  31. /**
  32. * @brief Gets a parameter block at the specified slot index.
  33. */
  34. GpuParamBlockPtr getParamBlock(UINT32 slotIdx) const;
  35. /**
  36. * @brief Returns true if matrices need to be transposed when reading/writing them.
  37. */
  38. bool getTransposeMatrices() const;
  39. /**
  40. * @brief Marks the core data as dirty, signifying the core thread it should update it.
  41. */
  42. void markCoreDirty();
  43. protected:
  44. GpuParamDataDesc* mParamDesc;
  45. std::shared_ptr<GpuParamsInternalData> mInternalData;
  46. };
  47. /**
  48. * @brief A handle that allows you to set a GpuProgram parameter. Internally keeps a reference to the
  49. * GPU parameter buffer and the necessary offsets. You should specialize this type for specific
  50. * parameter types.
  51. *
  52. * Object of this type must be returned by a Material. Setting/Getting parameter values will internally
  53. * access a GPU parameter buffer attached to the Material this parameter was created from. Anything
  54. * rendered with that material will then use those set values.
  55. *
  56. * @note Normally you can set a GpuProgram parameter by calling various set/get methods on a Material.
  57. * This class primarily used an as optimization in performance critical bits of code
  58. * where it is important to locate and set parameters quickly without any lookups
  59. * (Mentioned set/get methods expect a parameter name). You just retrieve the handle
  60. * once and then set the parameter value many times with minimal performance impact.
  61. *
  62. * @see Material
  63. *
  64. * @note Sim thread only.
  65. */
  66. template<class T>
  67. class BS_CORE_EXPORT TGpuDataParam : public GpuDataParamBase
  68. {
  69. private:
  70. friend class GpuParams;
  71. /**
  72. * @brief Policy class that allows us to re-use this template class for matrices which might
  73. * need transposing, and other types which do not. Matrix needs to be transposed for
  74. * certain render systems depending on how they store them in memory.
  75. */
  76. template<class Type>
  77. struct TransposePolicy
  78. {
  79. static Type transpose(const Type& value) { return value; }
  80. static bool transposeEnabled(bool enabled) { return false; }
  81. };
  82. /**
  83. * @brief Transpose policy for 3x3 matrix.
  84. */
  85. template<>
  86. struct TransposePolicy<Matrix3>
  87. {
  88. static Matrix3 transpose(const Matrix3& value) { return value.transpose(); }
  89. static bool transposeEnabled(bool enabled) { return enabled; }
  90. };
  91. /**
  92. * @brief Transpose policy for 4x4 matrix.
  93. */
  94. template<>
  95. struct TransposePolicy<Matrix4>
  96. {
  97. static Matrix4 transpose(const Matrix4& value) { return value.transpose(); }
  98. static bool transposeEnabled(bool enabled) { return enabled; }
  99. };
  100. public:
  101. TGpuDataParam()
  102. { }
  103. /**
  104. * @brief Sets a parameter value at the specified array index. If parameter does not
  105. * contain an array leave the index at 0.
  106. *
  107. * @note Like with all GPU parameters, the actual GPU buffer will not be updated until rendering
  108. * with material this parameter was created from starts on the core thread.
  109. */
  110. void set(const T& value, UINT32 arrayIdx = 0)
  111. {
  112. if (isDestroyed())
  113. BS_EXCEPT(InternalErrorException, "Trying to access a destroyed gpu parameter.");
  114. #if BS_DEBUG_MODE
  115. if (arrayIdx >= mParamDesc->arraySize)
  116. {
  117. BS_EXCEPT(InvalidParametersException, "Array index out of range. Array size: " +
  118. toString(mParamDesc->arraySize) + ". Requested size: " + toString(arrayIdx));
  119. }
  120. #endif
  121. UINT32 elementSizeBytes = mParamDesc->elementSize * sizeof(UINT32);
  122. UINT32 sizeBytes = std::min(elementSizeBytes, (UINT32)sizeof(T)); // Truncate if it doesn't fit within parameter size
  123. GpuParamBlockPtr paramBlock = getParamBlock(mParamDesc->paramBlockSlot);
  124. if (paramBlock == nullptr)
  125. return;
  126. if (TransposePolicy<T>::transposeEnabled(getTransposeMatrices()))
  127. {
  128. T transposed = TransposePolicy<T>::transpose(value);
  129. paramBlock->write((mParamDesc->cpuMemOffset + arrayIdx * mParamDesc->arrayElementStride) * sizeof(UINT32), &transposed, sizeBytes);
  130. }
  131. else
  132. paramBlock->write((mParamDesc->cpuMemOffset + arrayIdx * mParamDesc->arrayElementStride) * sizeof(UINT32), &value, sizeBytes);
  133. // Set unused bytes to 0
  134. if(sizeBytes < elementSizeBytes)
  135. {
  136. UINT32 diffSize = elementSizeBytes - sizeBytes;
  137. paramBlock->zeroOut((mParamDesc->cpuMemOffset + arrayIdx * mParamDesc->arrayElementStride) * sizeof(UINT32)+sizeBytes, diffSize);
  138. }
  139. markCoreDirty();
  140. }
  141. /**
  142. * @brief Returns a value of a parameter at the specified array index. If parameter does not
  143. * contain an array leave the index at 0.
  144. *
  145. * @note No GPU reads are done. Data returned was cached when it was written.
  146. */
  147. T get(UINT32 arrayIdx = 0)
  148. {
  149. if (isDestroyed())
  150. BS_EXCEPT(InternalErrorException, "Trying to access a destroyed gpu parameter.");
  151. #if BS_DEBUG_MODE
  152. if (arrayIdx >= mParamDesc->arraySize)
  153. {
  154. BS_EXCEPT(InvalidParametersException, "Array index out of range. Array size: " +
  155. toString(mParamDesc->arraySize) + ". Requested size: " + toString(arrayIdx));
  156. }
  157. #endif
  158. UINT32 elementSizeBytes = mParamDesc->elementSize * sizeof(UINT32);
  159. UINT32 sizeBytes = std::min(elementSizeBytes, (UINT32)sizeof(T));
  160. GpuParamBlockPtr paramBlock = getParamBlock(mParamDesc->paramBlockSlot);
  161. if (paramBlock == nullptr)
  162. return T();
  163. T value;
  164. paramBlock->read((mParamDesc->cpuMemOffset + arrayIdx * mParamDesc->arrayElementStride) * sizeof(UINT32), &value, sizeBytes);
  165. if (TransposePolicy<T>::transposeEnabled(getTransposeMatrices()))
  166. return TransposePolicy<T>::transpose(value);
  167. else
  168. return value;
  169. }
  170. private:
  171. TGpuDataParam(GpuParamDataDesc* paramDesc, const std::shared_ptr<GpuParamsInternalData>& internalData)
  172. :GpuDataParamBase(paramDesc, internalData)
  173. { }
  174. };
  175. typedef TGpuDataParam<float> GpuParamFloat;
  176. typedef TGpuDataParam<Color> GpuParamColor;
  177. typedef TGpuDataParam<Vector2> GpuParamVec2;
  178. typedef TGpuDataParam<Vector3> GpuParamVec3;
  179. typedef TGpuDataParam<Vector4> GpuParamVec4;
  180. typedef TGpuDataParam<Matrix3> GpuParamMat3;
  181. typedef TGpuDataParam<Matrix4> GpuParamMat4;
  182. template class TGpuDataParam<float>;
  183. template class TGpuDataParam<Color>;
  184. template class TGpuDataParam<Vector2>;
  185. template class TGpuDataParam<Vector3>;
  186. template class TGpuDataParam<Vector4>;
  187. template class TGpuDataParam<Matrix3>;
  188. template class TGpuDataParam<Matrix4>;
  189. /**
  190. * @copydoc GpuDataParamBase
  191. */
  192. class BS_CORE_EXPORT GpuParamStruct
  193. {
  194. private:
  195. friend class GpuParams;
  196. public:
  197. GpuParamStruct();
  198. /**
  199. * @copydoc GpuDataParamBase::set
  200. */
  201. void set(const void* value, UINT32 sizeBytes, UINT32 arrayIdx = 0);
  202. /**
  203. * @copydoc GpuDataParamBase::get
  204. */
  205. void get(void* value, UINT32 sizeBytes, UINT32 arrayIdx = 0);
  206. /**
  207. * @brief Returns the size of the struct in bytes.
  208. */
  209. UINT32 getElementSize() const;
  210. private:
  211. GpuParamStruct(GpuParamDataDesc* paramDesc, const std::shared_ptr<GpuParamsInternalData>& internalData);
  212. private:
  213. GpuParamDataDesc* mParamDesc;
  214. std::shared_ptr<GpuParamsInternalData> mInternalData;
  215. };
  216. /**
  217. * @copydoc GpuDataParamBase
  218. */
  219. class BS_CORE_EXPORT GpuParamTexture
  220. {
  221. private:
  222. friend class GpuParams;
  223. public:
  224. GpuParamTexture();
  225. /**
  226. * @copydoc GpuDataParamBase::set
  227. */
  228. void set(const HTexture& texture);
  229. /**
  230. * @copydoc GpuDataParamBase::get
  231. */
  232. HTexture get();
  233. private:
  234. GpuParamTexture(GpuParamObjectDesc* paramDesc, const std::shared_ptr<GpuParamsInternalData>& internalData);
  235. private:
  236. GpuParamObjectDesc* mParamDesc;
  237. std::shared_ptr<GpuParamsInternalData> mInternalData;
  238. };
  239. /**
  240. * @copydoc GpuDataParamBase
  241. */
  242. class BS_CORE_EXPORT GpuParamSampState
  243. {
  244. private:
  245. friend class GpuParams;
  246. public:
  247. GpuParamSampState();
  248. /**
  249. * @copydoc GpuDataParamBase::set
  250. */
  251. void set(const HSamplerState& texture);
  252. /**
  253. * @copydoc GpuDataParamBase::get
  254. */
  255. HSamplerState get();
  256. private:
  257. GpuParamSampState(GpuParamObjectDesc* paramDesc, const std::shared_ptr<GpuParamsInternalData>& internalData);
  258. private:
  259. GpuParamObjectDesc* mParamDesc;
  260. std::shared_ptr<GpuParamsInternalData> mInternalData;
  261. };
  262. }