BsGpuParam.h 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  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 SPtr<SamplerState> Type; };
  26. template<> struct TGpuParamSamplerStateType < true > { typedef SPtr<SamplerStateCore> Type; };
  27. template<bool Core> struct TGpuParamBufferType { };
  28. template<> struct TGpuParamBufferType < false > { typedef SPtr<GpuParamBlockBuffer> Type; };
  29. template<> struct TGpuParamBufferType < true > { typedef SPtr<GpuParamBlockBufferCore> Type; };
  30. /**
  31. * Policy class that allows us to re-use this template class for matrices which might need transposing, and other
  32. * types which do not. Matrix needs to be transposed for certain render systems depending on how they store them
  33. * in memory.
  34. */
  35. template<class Type>
  36. struct TransposePolicy
  37. {
  38. static Type transpose(const Type& value) { return value; }
  39. static bool transposeEnabled(bool enabled) { return false; }
  40. };
  41. /** Transpose policy for 3x3 matrix. */
  42. template<>
  43. struct TransposePolicy<Matrix3>
  44. {
  45. static Matrix3 transpose(const Matrix3& value) { return value.transpose(); }
  46. static bool transposeEnabled(bool enabled) { return enabled; }
  47. };
  48. /** Transpose policy for 4x4 matrix. */
  49. template<>
  50. struct TransposePolicy<Matrix4>
  51. {
  52. static Matrix4 transpose(const Matrix4& value) { return value.transpose(); }
  53. static bool transposeEnabled(bool enabled) { return enabled; }
  54. };
  55. /** Transpose policy for NxM matrix. */
  56. template<int N, int M>
  57. struct TransposePolicy<MatrixNxM<N, M>>
  58. {
  59. static MatrixNxM<N, M> transpose(const MatrixNxM<N, M>& value) { return value.transpose(); }
  60. static bool transposeEnabled(bool enabled) { return enabled; }
  61. };
  62. /**
  63. * A handle that allows you to set a GpuProgram parameter. Internally keeps a reference to the GPU parameter buffer and
  64. * the necessary offsets. You should specialize this type for specific parameter types.
  65. *
  66. * Object of this type must be returned by a Material. Setting/Getting parameter values will internally access a GPU
  67. * parameter buffer attached to the Material this parameter was created from. Anything rendered with that material will
  68. * then use those set values.
  69. *
  70. * @note
  71. * Normally you can set a GpuProgram parameter by calling various set/get methods on a Material. This class primarily
  72. * used an as optimization in performance critical bits of code where it is important to locate and set parameters
  73. * quickly without any lookups (Mentioned set/get methods expect a parameter name). You just retrieve the handle once
  74. * and then set the parameter value many times with minimal performance impact.
  75. *
  76. * @see Material
  77. */
  78. template<class T, bool Core>
  79. class BS_CORE_EXPORT TGpuDataParam
  80. {
  81. private:
  82. typedef typename TGpuParamBufferType<Core>::Type GpuParamBufferType;
  83. typedef typename TGpuParamsPtrType<Core>::Type GpuParamsType;
  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. typedef typename TGpuParamBufferType<Core>::Type GpuParamBufferType;
  117. typedef typename TGpuParamsPtrType<Core>::Type GpuParamsType;
  118. TGpuParamStruct();
  119. TGpuParamStruct(GpuParamDataDesc* paramDesc, const GpuParamsType& parent);
  120. /** @copydoc TGpuDataParam::set */
  121. void set(const void* value, UINT32 sizeBytes, UINT32 arrayIdx = 0);
  122. /** @copydoc TGpuDataParam::get */
  123. void get(void* value, UINT32 sizeBytes, UINT32 arrayIdx = 0);
  124. /** Returns the size of the struct in bytes. */
  125. UINT32 getElementSize() const;
  126. /** Checks if param is initialized. */
  127. bool operator==(const nullptr_t &nullval) const
  128. {
  129. return mParamDesc == nullptr;
  130. }
  131. protected:
  132. GpuParamsType mParent;
  133. GpuParamDataDesc* mParamDesc;
  134. };
  135. /** @copydoc TGpuDataParam */
  136. template<bool Core>
  137. class BS_CORE_EXPORT TGpuParamTexture
  138. {
  139. private:
  140. friend class GpuParams;
  141. friend class GpuParamsCore;
  142. typedef typename TGpuParamsPtrType<Core>::Type GpuParamsType;
  143. typedef typename TGpuParamTextureType<Core>::Type TextureType;
  144. public:
  145. TGpuParamTexture();
  146. TGpuParamTexture(GpuParamObjectDesc* paramDesc, const GpuParamsType& parent);
  147. /** @copydoc TGpuDataParam::set */
  148. void set(const TextureType& texture);
  149. /** @copydoc TGpuDataParam::get */
  150. TextureType get();
  151. /** Checks if param is initialized. */
  152. bool operator==(const nullptr_t &nullval) const
  153. {
  154. return mParamDesc == nullptr;
  155. }
  156. protected:
  157. GpuParamsType mParent;
  158. GpuParamObjectDesc* mParamDesc;
  159. };
  160. /** @copydoc TGpuDataParam */
  161. template<bool Core>
  162. class BS_CORE_EXPORT TGpuParamLoadStoreTexture
  163. {
  164. private:
  165. friend class GpuParams;
  166. friend class GpuParamsCore;
  167. typedef typename TGpuParamsPtrType<Core>::Type GpuParamsType;
  168. typedef typename TGpuParamTextureType<Core>::Type TextureType;
  169. public:
  170. TGpuParamLoadStoreTexture();
  171. TGpuParamLoadStoreTexture(GpuParamObjectDesc* paramDesc, const GpuParamsType& parent);
  172. /** @copydoc TGpuDataParam::set */
  173. void set(const TextureType& texture, const TextureSurface& surface);
  174. /** @copydoc TGpuDataParam::get */
  175. TextureType get();
  176. /** Checks if param is initialized. */
  177. bool operator==(const nullptr_t &nullval) const
  178. {
  179. return mParamDesc == nullptr;
  180. }
  181. protected:
  182. GpuParamsType mParent;
  183. GpuParamObjectDesc* mParamDesc;
  184. };
  185. /** @copydoc TGpuDataParam */
  186. template<bool Core>
  187. class BS_CORE_EXPORT TGpuParamSampState
  188. {
  189. private:
  190. friend class GpuParams;
  191. friend class GpuParamsCore;
  192. typedef typename TGpuParamsPtrType<Core>::Type GpuParamsType;
  193. typedef typename TGpuParamSamplerStateType<Core>::Type SamplerStateType;
  194. public:
  195. TGpuParamSampState();
  196. TGpuParamSampState(GpuParamObjectDesc* paramDesc, const GpuParamsType& parent);
  197. /** @copydoc TGpuDataParam::set */
  198. void set(const SamplerStateType& samplerState);
  199. /** @copydoc TGpuDataParam::get */
  200. SamplerStateType get();
  201. /** Checks if param is initialized. */
  202. bool operator==(const nullptr_t &nullval) const
  203. {
  204. return mParamDesc == nullptr;
  205. }
  206. protected:
  207. GpuParamsType mParent;
  208. GpuParamObjectDesc* mParamDesc;
  209. };
  210. /** @} */
  211. /** @addtogroup RenderAPI
  212. * @{
  213. */
  214. typedef TGpuDataParam<float, false> GpuParamFloat;
  215. typedef TGpuDataParam<Vector2, false> GpuParamVec2;
  216. typedef TGpuDataParam<Vector3, false> GpuParamVec3;
  217. typedef TGpuDataParam<Vector4, false> GpuParamVec4;
  218. typedef TGpuDataParam<int, false> GpuParamInt;
  219. typedef TGpuDataParam<Vector2I, false> GpuParamVec2I;
  220. typedef TGpuDataParam<Vector3I, false> GpuParamVec3I;
  221. typedef TGpuDataParam<Vector4I, false> GpuParamVec4I;
  222. typedef TGpuDataParam<Matrix3, false> GpuParamMat3;
  223. typedef TGpuDataParam<Matrix4, false> GpuParamMat4;
  224. typedef TGpuDataParam<Color, false> GpuParamColor;
  225. typedef TGpuDataParam<float, true> GpuParamFloatCore;
  226. typedef TGpuDataParam<Vector2, true> GpuParamVec2Core;
  227. typedef TGpuDataParam<Vector3, true> GpuParamVec3Core;
  228. typedef TGpuDataParam<Vector4, true> GpuParamVec4Core;
  229. typedef TGpuDataParam<int, true> GpuParamIntCore;
  230. typedef TGpuDataParam<Vector2I, true> GpuParamVec2ICore;
  231. typedef TGpuDataParam<Vector3I, true> GpuParamVec3ICore;
  232. typedef TGpuDataParam<Vector4I, true> GpuParamVec4ICore;
  233. typedef TGpuDataParam<Matrix3, true> GpuParamMat3Core;
  234. typedef TGpuDataParam<Matrix4, true> GpuParamMat4Core;
  235. typedef TGpuDataParam<Color, true> GpuParamColorCore;
  236. typedef TGpuParamStruct<false> GpuParamStruct;
  237. typedef TGpuParamStruct<true> GpuParamStructCore;
  238. typedef TGpuParamTexture<false> GpuParamTexture;
  239. typedef TGpuParamTexture<true> GpuParamTextureCore;
  240. typedef TGpuParamSampState<false> GpuParamSampState;
  241. typedef TGpuParamSampState<true> GpuParamSampStateCore;
  242. typedef TGpuParamLoadStoreTexture<false> GpuParamLoadStoreTexture;
  243. typedef TGpuParamLoadStoreTexture<true> GpuParamLoadStoreTextureCore;
  244. /** @} */
  245. }