2
0

BsGpuParam.h 8.1 KB

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