BsGpuParam.h 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  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 (TransposePolicy<T>::transposeEnabled(getTransposeMatrices()))
  121. {
  122. T transposed = TransposePolicy<T>::transpose(value);
  123. paramBlock->write((mParamDesc->cpuMemOffset + arrayIdx * mParamDesc->arrayElementStride) * sizeof(UINT32), &transposed, sizeBytes);
  124. }
  125. else
  126. paramBlock->write((mParamDesc->cpuMemOffset + arrayIdx * mParamDesc->arrayElementStride) * sizeof(UINT32), &value, sizeBytes);
  127. // Set unused bytes to 0
  128. if(sizeBytes < elementSizeBytes)
  129. {
  130. UINT32 diffSize = elementSizeBytes - sizeBytes;
  131. paramBlock->zeroOut((mParamDesc->cpuMemOffset + arrayIdx * mParamDesc->arrayElementStride) * sizeof(UINT32)+sizeBytes, diffSize);
  132. }
  133. markCoreDirty();
  134. }
  135. /**
  136. * @brief Returns a value of a parameter at the specified array index. If parameter does not
  137. * contain an array leave the index at 0.
  138. *
  139. * @note No GPU reads are done. Data returned was cached when it was written.
  140. */
  141. T get(UINT32 arrayIdx = 0)
  142. {
  143. if (isDestroyed())
  144. BS_EXCEPT(InternalErrorException, "Trying to access a destroyed gpu parameter.");
  145. #if BS_DEBUG_MODE
  146. if (arrayIdx >= mParamDesc->arraySize)
  147. {
  148. BS_EXCEPT(InvalidParametersException, "Array index out of range. Array size: " +
  149. toString(mParamDesc->arraySize) + ". Requested size: " + toString(arrayIdx));
  150. }
  151. #endif
  152. UINT32 elementSizeBytes = mParamDesc->elementSize * sizeof(UINT32);
  153. UINT32 sizeBytes = std::min(elementSizeBytes, (UINT32)sizeof(T));
  154. GpuParamBlockPtr paramBlock = getParamBlock(mParamDesc->paramBlockSlot);
  155. T value;
  156. paramBlock->read((mParamDesc->cpuMemOffset + arrayIdx * mParamDesc->arrayElementStride) * sizeof(UINT32), &value, sizeBytes);
  157. if (TransposePolicy<T>::transposeEnabled(getTransposeMatrices()))
  158. return TransposePolicy<T>::transpose(value);
  159. else
  160. return value;
  161. }
  162. private:
  163. TGpuDataParam(GpuParamDataDesc* paramDesc, const std::shared_ptr<GpuParamsInternalData>& internalData)
  164. :GpuDataParamBase(paramDesc, internalData)
  165. { }
  166. };
  167. typedef TGpuDataParam<float> GpuParamFloat;
  168. typedef TGpuDataParam<Vector2> GpuParamVec2;
  169. typedef TGpuDataParam<Vector3> GpuParamVec3;
  170. typedef TGpuDataParam<Vector4> GpuParamVec4;
  171. typedef TGpuDataParam<Matrix3> GpuParamMat3;
  172. typedef TGpuDataParam<Matrix4> GpuParamMat4;
  173. /**
  174. * @copydoc GpuDataParamBase
  175. */
  176. class BS_CORE_EXPORT GpuParamStruct
  177. {
  178. private:
  179. friend class GpuParams;
  180. public:
  181. GpuParamStruct();
  182. /**
  183. * @copydoc GpuDataParamBase::set
  184. */
  185. void set(const void* value, UINT32 sizeBytes, UINT32 arrayIdx = 0);
  186. /**
  187. * @copydoc GpuDataParamBase::get
  188. */
  189. void get(void* value, UINT32 sizeBytes, UINT32 arrayIdx = 0);
  190. /**
  191. * @brief Returns the size of the struct in bytes.
  192. */
  193. UINT32 getElementSize() const;
  194. private:
  195. GpuParamStruct(GpuParamDataDesc* paramDesc, const std::shared_ptr<GpuParamsInternalData>& internalData);
  196. private:
  197. GpuParamDataDesc* mParamDesc;
  198. std::shared_ptr<GpuParamsInternalData> mInternalData;
  199. };
  200. /**
  201. * @copydoc GpuDataParamBase
  202. */
  203. class BS_CORE_EXPORT GpuParamTexture
  204. {
  205. private:
  206. friend class GpuParams;
  207. public:
  208. GpuParamTexture();
  209. /**
  210. * @copydoc GpuDataParamBase::set
  211. */
  212. void set(const HTexture& texture);
  213. /**
  214. * @copydoc GpuDataParamBase::get
  215. */
  216. HTexture get();
  217. private:
  218. GpuParamTexture(GpuParamObjectDesc* paramDesc, const std::shared_ptr<GpuParamsInternalData>& internalData);
  219. private:
  220. GpuParamObjectDesc* mParamDesc;
  221. std::shared_ptr<GpuParamsInternalData> mInternalData;
  222. };
  223. /**
  224. * @copydoc GpuDataParamBase
  225. */
  226. class BS_CORE_EXPORT GpuParamSampState
  227. {
  228. private:
  229. friend class GpuParams;
  230. public:
  231. GpuParamSampState();
  232. /**
  233. * @copydoc GpuDataParamBase::set
  234. */
  235. void set(const HSamplerState& texture);
  236. /**
  237. * @copydoc GpuDataParamBase::get
  238. */
  239. HSamplerState get();
  240. private:
  241. GpuParamSampState(GpuParamObjectDesc* paramDesc, const std::shared_ptr<GpuParamsInternalData>& internalData);
  242. private:
  243. GpuParamObjectDesc* mParamDesc;
  244. std::shared_ptr<GpuParamsInternalData> mInternalData;
  245. };
  246. }