BsGpuParam.h 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. #pragma once
  2. #include "BsCorePrerequisites.h"
  3. #include "BsVector2.h"
  4. #include "BsVector3.h"
  5. #include "BsVector4.h"
  6. #include "BsMatrix3.h"
  7. #include "BsMatrix4.h"
  8. #include "BsMatrixNxM.h"
  9. #include "BsColor.h"
  10. namespace BansheeEngine
  11. {
  12. template<bool Core> struct TGpuParamsPtrType { };
  13. template<> struct TGpuParamsPtrType<false> { typedef SPtr<GpuParams> Type; };
  14. template<> struct TGpuParamsPtrType<true> { typedef SPtr<GpuParamsCore> Type; };
  15. template<bool Core> struct TGpuParamTextureType { };
  16. template<> struct TGpuParamTextureType < false > { typedef HTexture Type; };
  17. template<> struct TGpuParamTextureType < true > { typedef SPtr<TextureCore> Type; };
  18. template<bool Core> struct TGpuParamSamplerStateType { };
  19. template<> struct TGpuParamSamplerStateType < false > { typedef SamplerStatePtr Type; };
  20. template<> struct TGpuParamSamplerStateType < true > { typedef SPtr<SamplerStateCore> Type; };
  21. /**
  22. * @brief A handle that allows you to set a GpuProgram parameter. Internally keeps a reference to the
  23. * GPU parameter buffer and the necessary offsets. You should specialize this type for specific
  24. * parameter types.
  25. *
  26. * Object of this type must be returned by a Material. Setting/Getting parameter values will internally
  27. * access a GPU parameter buffer attached to the Material this parameter was created from. Anything
  28. * rendered with that material will then use those set values.
  29. *
  30. * @note Normally you can set a GpuProgram parameter by calling various set/get methods on a Material.
  31. * This class primarily used an as optimization in performance critical bits of code
  32. * where it is important to locate and set parameters quickly without any lookups
  33. * (Mentioned set/get methods expect a parameter name). You just retrieve the handle
  34. * once and then set the parameter value many times with minimal performance impact.
  35. *
  36. * @see Material
  37. */
  38. template<class T, bool Core>
  39. class BS_CORE_EXPORT TGpuDataParam
  40. {
  41. private:
  42. template<bool Core> struct TGpuParamBufferType { };
  43. template<> struct TGpuParamBufferType < false > { typedef SPtr<GpuParamBlockBuffer> Type; };
  44. template<> struct TGpuParamBufferType < true > { typedef SPtr<GpuParamBlockBufferCore> Type; };
  45. typedef typename TGpuParamBufferType<Core>::Type GpuParamBufferType;
  46. typedef typename TGpuParamsPtrType<Core>::Type GpuParamsType;
  47. /**
  48. * @brief Policy class that allows us to re-use this template class for matrices which might
  49. * need transposing, and other types which do not. Matrix needs to be transposed for
  50. * certain render systems depending on how they store them in memory.
  51. */
  52. template<class Type>
  53. struct TransposePolicy
  54. {
  55. static Type transpose(const Type& value) { return value; }
  56. static bool transposeEnabled(bool enabled) { return false; }
  57. };
  58. /**
  59. * @brief Transpose policy for 3x3 matrix.
  60. */
  61. template<>
  62. struct TransposePolicy<Matrix3>
  63. {
  64. static Matrix3 transpose(const Matrix3& value) { return value.transpose(); }
  65. static bool transposeEnabled(bool enabled) { return enabled; }
  66. };
  67. /**
  68. * @brief Transpose policy for 4x4 matrix.
  69. */
  70. template<>
  71. struct TransposePolicy<Matrix4>
  72. {
  73. static Matrix4 transpose(const Matrix4& value) { return value.transpose(); }
  74. static bool transposeEnabled(bool enabled) { return enabled; }
  75. };
  76. /**
  77. * @brief Transpose policy for NxM matrix.
  78. */
  79. template<int N, int M>
  80. struct TransposePolicy<MatrixNxM<N, M>>
  81. {
  82. static MatrixNxM<N, M> transpose(const MatrixNxM<N, M>& value) { return value.transpose(); }
  83. static bool transposeEnabled(bool enabled) { return enabled; }
  84. };
  85. public:
  86. TGpuDataParam();
  87. TGpuDataParam(GpuParamDataDesc* paramDesc, const GpuParamsType& parent);
  88. /**
  89. * @brief Sets a parameter value at the specified array index. If parameter does not
  90. * contain an array leave the index at 0.
  91. *
  92. * @note Like with all GPU parameters, the actual GPU buffer will not be updated until rendering
  93. * with material this parameter was created from starts on the core thread.
  94. */
  95. void set(const T& value, UINT32 arrayIdx = 0);
  96. /**
  97. * @brief Returns a value of a parameter at the specified array index. If parameter does not
  98. * contain an array leave the index at 0.
  99. *
  100. * @note No GPU reads are done. Data returned was cached when it was written.
  101. */
  102. T get(UINT32 arrayIdx = 0);
  103. /**
  104. * @brief Checks if param is initialized.
  105. */
  106. bool operator==(const nullptr_t &nullval) const
  107. {
  108. return mParamDesc == nullptr;
  109. }
  110. protected:
  111. GpuParamsType mParent;
  112. GpuParamDataDesc* mParamDesc;
  113. };
  114. /**
  115. * @copydoc TGpuDataParam
  116. */
  117. template<bool Core>
  118. class BS_CORE_EXPORT TGpuParamStruct
  119. {
  120. public:
  121. template<bool Core> struct TGpuParamBufferType { };
  122. template<> struct TGpuParamBufferType < false > { typedef SPtr<GpuParamBlockBuffer> Type; };
  123. template<> struct TGpuParamBufferType < true > { typedef SPtr<GpuParamBlockBufferCore> Type; };
  124. typedef typename TGpuParamBufferType<Core>::Type GpuParamBufferType;
  125. typedef typename TGpuParamsPtrType<Core>::Type GpuParamsType;
  126. TGpuParamStruct();
  127. TGpuParamStruct(GpuParamDataDesc* paramDesc, const GpuParamsType& parent);
  128. /**
  129. * @copydoc TGpuDataParam::set
  130. */
  131. void set(const void* value, UINT32 sizeBytes, UINT32 arrayIdx = 0);
  132. /**
  133. * @copydoc TGpuDataParam::get
  134. */
  135. void get(void* value, UINT32 sizeBytes, UINT32 arrayIdx = 0);
  136. /**
  137. * @brief Returns the size of the struct in bytes.
  138. */
  139. UINT32 getElementSize() const;
  140. /**
  141. * @brief Checks if param is initialized.
  142. */
  143. bool operator==(const nullptr_t &nullval) const
  144. {
  145. return mParamDesc == nullptr;
  146. }
  147. protected:
  148. GpuParamsType mParent;
  149. GpuParamDataDesc* mParamDesc;
  150. };
  151. /**
  152. * @copydoc TGpuObjectParam
  153. */
  154. template<bool Core>
  155. class BS_CORE_EXPORT TGpuParamTexture
  156. {
  157. private:
  158. friend class GpuParams;
  159. friend class GpuParamsCore;
  160. typedef typename TGpuParamsPtrType<Core>::Type GpuParamsType;
  161. typedef typename TGpuParamTextureType<Core>::Type TextureType;
  162. public:
  163. TGpuParamTexture();
  164. TGpuParamTexture(GpuParamObjectDesc* paramDesc, const GpuParamsType& parent);
  165. /**
  166. * @copydoc TGpuDataParam::set
  167. */
  168. void set(const TextureType& texture);
  169. /**
  170. * @copydoc TGpuDataParam::get
  171. */
  172. TextureType get();
  173. /**
  174. * @brief Checks if param is initialized.
  175. */
  176. bool operator==(const nullptr_t &nullval) const
  177. {
  178. return mParamDesc == nullptr;
  179. }
  180. protected:
  181. GpuParamsType mParent;
  182. GpuParamObjectDesc* mParamDesc;
  183. };
  184. /**
  185. * @copydoc TGpuObjectParam
  186. */
  187. template<bool Core>
  188. class BS_CORE_EXPORT TGpuParamLoadStoreTexture
  189. {
  190. private:
  191. friend class GpuParams;
  192. friend class GpuParamsCore;
  193. typedef typename TGpuParamsPtrType<Core>::Type GpuParamsType;
  194. typedef typename TGpuParamTextureType<Core>::Type TextureType;
  195. public:
  196. TGpuParamLoadStoreTexture();
  197. TGpuParamLoadStoreTexture(GpuParamObjectDesc* paramDesc, const GpuParamsType& parent);
  198. /**
  199. * @copydoc TGpuDataParam::set
  200. */
  201. void set(const TextureType& texture, const TextureSurface& surface);
  202. /**
  203. * @copydoc TGpuDataParam::get
  204. */
  205. TextureType get();
  206. /**
  207. * @brief Checks if param is initialized.
  208. */
  209. bool operator==(const nullptr_t &nullval) const
  210. {
  211. return mParamDesc == nullptr;
  212. }
  213. protected:
  214. GpuParamsType mParent;
  215. GpuParamObjectDesc* mParamDesc;
  216. };
  217. /**
  218. * @copydoc TGpuObjectParam
  219. */
  220. template<bool Core>
  221. class BS_CORE_EXPORT TGpuParamSampState
  222. {
  223. private:
  224. friend class GpuParams;
  225. friend class GpuParamsCore;
  226. typedef typename TGpuParamsPtrType<Core>::Type GpuParamsType;
  227. typedef typename TGpuParamSamplerStateType<Core>::Type SamplerStateType;
  228. public:
  229. TGpuParamSampState();
  230. TGpuParamSampState(GpuParamObjectDesc* paramDesc, const GpuParamsType& parent);
  231. /**
  232. * @copydoc TGpuDataParam::set
  233. */
  234. void set(const SamplerStateType& samplerState);
  235. /**
  236. * @copydoc TGpuDataParam::get
  237. */
  238. SamplerStateType get();
  239. /**
  240. * @brief Checks if param is initialized.
  241. */
  242. bool operator==(const nullptr_t &nullval) const
  243. {
  244. return mParamDesc == nullptr;
  245. }
  246. protected:
  247. GpuParamsType mParent;
  248. GpuParamObjectDesc* mParamDesc;
  249. };
  250. typedef TGpuDataParam<float, false> GpuParamFloat;
  251. typedef TGpuDataParam<Color, false> GpuParamColor;
  252. typedef TGpuDataParam<Vector2, false> GpuParamVec2;
  253. typedef TGpuDataParam<Vector3, false> GpuParamVec3;
  254. typedef TGpuDataParam<Vector4, false> GpuParamVec4;
  255. typedef TGpuDataParam<Matrix3, false> GpuParamMat3;
  256. typedef TGpuDataParam<Matrix4, false> GpuParamMat4;
  257. typedef TGpuDataParam<float, true> GpuParamFloatCore;
  258. typedef TGpuDataParam<Color, true> GpuParamColorCore;
  259. typedef TGpuDataParam<Vector2, true> GpuParamVec2Core;
  260. typedef TGpuDataParam<Vector3, true> GpuParamVec3Core;
  261. typedef TGpuDataParam<Vector4, true> GpuParamVec4Core;
  262. typedef TGpuDataParam<Matrix3, true> GpuParamMat3Core;
  263. typedef TGpuDataParam<Matrix4, true> GpuParamMat4Core;
  264. typedef TGpuParamStruct<false> GpuParamStruct;
  265. typedef TGpuParamStruct<true> GpuParamStructCore;
  266. typedef TGpuParamTexture<false> GpuParamTexture;
  267. typedef TGpuParamTexture<true> GpuParamTextureCore;
  268. typedef TGpuParamSampState<false> GpuParamSampState;
  269. typedef TGpuParamSampState<true> GpuParamSampStateCore;
  270. typedef TGpuParamLoadStoreTexture<false> GpuParamLoadStoreTexture;
  271. typedef TGpuParamLoadStoreTexture<true> GpuParamLoadStoreTextureCore;
  272. }