BsGpuParam.h 9.8 KB

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