BsGpuParam.h 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  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. protected:
  104. GpuParamsType mParent;
  105. GpuParamDataDesc* mParamDesc;
  106. };
  107. /**
  108. * @copydoc TGpuDataParam
  109. */
  110. template<bool Core>
  111. class BS_CORE_EXPORT TGpuParamStruct
  112. {
  113. public:
  114. template<bool Core> struct TGpuParamBufferType { };
  115. template<> struct TGpuParamBufferType < false > { typedef SPtr<GpuParamBlockBuffer> Type; };
  116. template<> struct TGpuParamBufferType < true > { typedef SPtr<GpuParamBlockBufferCore> Type; };
  117. typedef typename TGpuParamBufferType<Core>::Type GpuParamBufferType;
  118. typedef typename TGpuParamsPtrType<Core>::Type GpuParamsType;
  119. TGpuParamStruct();
  120. TGpuParamStruct(GpuParamDataDesc* paramDesc, const GpuParamsType& parent);
  121. /**
  122. * @copydoc TGpuDataParam::set
  123. */
  124. void set(const void* value, UINT32 sizeBytes, UINT32 arrayIdx = 0);
  125. /**
  126. * @copydoc TGpuDataParam::get
  127. */
  128. void get(void* value, UINT32 sizeBytes, UINT32 arrayIdx = 0);
  129. /**
  130. * @brief Returns the size of the struct in bytes.
  131. */
  132. UINT32 getElementSize() const;
  133. protected:
  134. GpuParamsType mParent;
  135. GpuParamDataDesc* mParamDesc;
  136. };
  137. /**
  138. * @copydoc TGpuObjectParam
  139. */
  140. template<bool Core>
  141. class BS_CORE_EXPORT TGpuParamTexture
  142. {
  143. private:
  144. friend class GpuParams;
  145. friend class GpuParamsCore;
  146. typedef typename TGpuParamsPtrType<Core>::Type GpuParamsType;
  147. typedef typename TGpuParamTextureType<Core>::Type TextureType;
  148. public:
  149. TGpuParamTexture();
  150. TGpuParamTexture(GpuParamObjectDesc* paramDesc, const GpuParamsType& parent);
  151. /**
  152. * @copydoc TGpuDataParam::set
  153. */
  154. void set(const TextureType& texture);
  155. /**
  156. * @copydoc TGpuDataParam::get
  157. */
  158. TextureType get();
  159. protected:
  160. GpuParamsType mParent;
  161. GpuParamObjectDesc* mParamDesc;
  162. };
  163. /**
  164. * @copydoc TGpuObjectParam
  165. */
  166. template<bool Core>
  167. class BS_CORE_EXPORT TGpuParamLoadStoreTexture
  168. {
  169. private:
  170. friend class GpuParams;
  171. friend class GpuParamsCore;
  172. typedef typename TGpuParamsPtrType<Core>::Type GpuParamsType;
  173. typedef typename TGpuParamTextureType<Core>::Type TextureType;
  174. public:
  175. TGpuParamLoadStoreTexture();
  176. TGpuParamLoadStoreTexture(GpuParamObjectDesc* paramDesc, const GpuParamsType& parent);
  177. /**
  178. * @copydoc TGpuDataParam::set
  179. */
  180. void set(const TextureType& texture, const TextureSurface& surface);
  181. /**
  182. * @copydoc TGpuDataParam::get
  183. */
  184. TextureType get();
  185. protected:
  186. GpuParamsType mParent;
  187. GpuParamObjectDesc* mParamDesc;
  188. };
  189. /**
  190. * @copydoc TGpuObjectParam
  191. */
  192. template<bool Core>
  193. class BS_CORE_EXPORT TGpuParamSampState
  194. {
  195. private:
  196. friend class GpuParams;
  197. friend class GpuParamsCore;
  198. typedef typename TGpuParamsPtrType<Core>::Type GpuParamsType;
  199. typedef typename TGpuParamSamplerStateType<Core>::Type SamplerStateType;
  200. public:
  201. TGpuParamSampState();
  202. TGpuParamSampState(GpuParamObjectDesc* paramDesc, const GpuParamsType& parent);
  203. /**
  204. * @copydoc TGpuDataParam::set
  205. */
  206. void set(const SamplerStateType& samplerState);
  207. /**
  208. * @copydoc TGpuDataParam::get
  209. */
  210. SamplerStateType get();
  211. protected:
  212. GpuParamsType mParent;
  213. GpuParamObjectDesc* mParamDesc;
  214. };
  215. typedef TGpuDataParam<float, false> GpuParamFloat;
  216. typedef TGpuDataParam<Color, false> GpuParamColor;
  217. typedef TGpuDataParam<Vector2, false> GpuParamVec2;
  218. typedef TGpuDataParam<Vector3, false> GpuParamVec3;
  219. typedef TGpuDataParam<Vector4, false> GpuParamVec4;
  220. typedef TGpuDataParam<Matrix3, false> GpuParamMat3;
  221. typedef TGpuDataParam<Matrix4, false> GpuParamMat4;
  222. typedef TGpuDataParam<float, true> GpuParamFloatCore;
  223. typedef TGpuDataParam<Color, true> GpuParamColorCore;
  224. typedef TGpuDataParam<Vector2, true> GpuParamVec2Core;
  225. typedef TGpuDataParam<Vector3, true> GpuParamVec3Core;
  226. typedef TGpuDataParam<Vector4, true> GpuParamVec4Core;
  227. typedef TGpuDataParam<Matrix3, true> GpuParamMat3Core;
  228. typedef TGpuDataParam<Matrix4, true> GpuParamMat4Core;
  229. typedef TGpuParamStruct<false> GpuParamStruct;
  230. typedef TGpuParamStruct<true> GpuParamStructCore;
  231. typedef TGpuParamTexture<false> GpuParamTexture;
  232. typedef TGpuParamTexture<true> GpuParamTextureCore;
  233. typedef TGpuParamSampState<false> GpuParamSampState;
  234. typedef TGpuParamSampState<true> GpuParamSampStateCore;
  235. typedef TGpuParamLoadStoreTexture<false> GpuParamLoadStoreTexture;
  236. typedef TGpuParamLoadStoreTexture<true> GpuParamLoadStoreTextureCore;
  237. }