BsGpuParam.h 8.3 KB

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