BsGpuParam.h 10 KB

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