BsGpuParam.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  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. template<bool Core> struct TGpuBufferType { };
  31. template<> struct TGpuBufferType < false > { typedef SPtr<GpuBuffer> Type; };
  32. template<> struct TGpuBufferType < true > { typedef SPtr<GpuBufferCore> Type; };
  33. /**
  34. * Policy class that allows us to re-use this template class for matrices which might need transposing, and other
  35. * types which do not. Matrix needs to be transposed for certain render systems depending on how they store them
  36. * in memory.
  37. */
  38. template<class Type>
  39. struct TransposePolicy
  40. {
  41. static Type transpose(const Type& value) { return value; }
  42. static bool transposeEnabled(bool enabled) { return false; }
  43. };
  44. /** Transpose policy for 3x3 matrix. */
  45. template<>
  46. struct TransposePolicy<Matrix3>
  47. {
  48. static Matrix3 transpose(const Matrix3& value) { return value.transpose(); }
  49. static bool transposeEnabled(bool enabled) { return enabled; }
  50. };
  51. /** Transpose policy for 4x4 matrix. */
  52. template<>
  53. struct TransposePolicy<Matrix4>
  54. {
  55. static Matrix4 transpose(const Matrix4& value) { return value.transpose(); }
  56. static bool transposeEnabled(bool enabled) { return enabled; }
  57. };
  58. /** Transpose policy for NxM matrix. */
  59. template<int N, int M>
  60. struct TransposePolicy<MatrixNxM<N, M>>
  61. {
  62. static MatrixNxM<N, M> transpose(const MatrixNxM<N, M>& value) { return value.transpose(); }
  63. static bool transposeEnabled(bool enabled) { return enabled; }
  64. };
  65. /**
  66. * A handle that allows you to set a GpuProgram parameter. Internally keeps a reference to the GPU parameter buffer and
  67. * the necessary offsets. You should specialize this type for specific parameter types.
  68. *
  69. * Object of this type must be returned by a Material. Setting/Getting parameter values will internally access a GPU
  70. * parameter buffer attached to the Material this parameter was created from. Anything rendered with that material will
  71. * then use those set values.
  72. *
  73. * @note
  74. * Normally you can set a GpuProgram parameter by calling various set/get methods on a Material. This class primarily
  75. * used an as optimization in performance critical bits of code where it is important to locate and set parameters
  76. * quickly without any lookups (Mentioned set/get methods expect a parameter name). You just retrieve the handle once
  77. * and then set the parameter value many times with minimal performance impact.
  78. *
  79. * @see Material
  80. */
  81. template<class T, bool Core>
  82. class BS_CORE_EXPORT TGpuDataParam
  83. {
  84. private:
  85. typedef typename TGpuParamBufferType<Core>::Type GpuParamBufferType;
  86. typedef typename TGpuParamsPtrType<Core>::Type GpuParamsType;
  87. public:
  88. TGpuDataParam();
  89. TGpuDataParam(GpuParamDataDesc* paramDesc, const GpuParamsType& parent);
  90. /**
  91. * Sets a parameter value at the specified array index. If parameter does not contain an array leave the index at 0.
  92. *
  93. * @note
  94. * Like with all GPU parameters, the actual GPU buffer will not be updated until rendering with material this
  95. * parameter was created from starts on the core thread.
  96. */
  97. void set(const T& value, UINT32 arrayIdx = 0);
  98. /**
  99. * Returns a value of a parameter at the specified array index. If parameter does not contain an array leave the
  100. * index at 0.
  101. *
  102. * @note No GPU reads are done. Data returned was cached when it was written.
  103. */
  104. T get(UINT32 arrayIdx = 0);
  105. /** Checks if param is initialized. */
  106. bool operator==(const nullptr_t &nullval) const
  107. {
  108. return mParamDesc == nullptr;
  109. }
  110. protected:
  111. GpuParamsType mParent;
  112. GpuParamDataDesc* mParamDesc;
  113. };
  114. /** @copydoc TGpuDataParam */
  115. template<bool Core>
  116. class BS_CORE_EXPORT TGpuParamStruct
  117. {
  118. public:
  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 TGpuParamBuffer
  191. {
  192. private:
  193. friend class GpuParams;
  194. friend class GpuParamsCore;
  195. typedef typename TGpuParamsPtrType<Core>::Type GpuParamsType;
  196. typedef typename TGpuBufferType<Core>::Type BufferType;
  197. public:
  198. TGpuParamBuffer();
  199. TGpuParamBuffer(GpuParamObjectDesc* paramDesc, const GpuParamsType& parent);
  200. /** @copydoc TGpuDataParam::set */
  201. void set(const BufferType& texture);
  202. /** @copydoc TGpuDataParam::get */
  203. BufferType 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. /** @copydoc TGpuDataParam */
  214. template<bool Core>
  215. class BS_CORE_EXPORT TGpuParamSampState
  216. {
  217. private:
  218. friend class GpuParams;
  219. friend class GpuParamsCore;
  220. typedef typename TGpuParamsPtrType<Core>::Type GpuParamsType;
  221. typedef typename TGpuParamSamplerStateType<Core>::Type SamplerStateType;
  222. public:
  223. TGpuParamSampState();
  224. TGpuParamSampState(GpuParamObjectDesc* paramDesc, const GpuParamsType& parent);
  225. /** @copydoc TGpuDataParam::set */
  226. void set(const SamplerStateType& samplerState);
  227. /** @copydoc TGpuDataParam::get */
  228. SamplerStateType get();
  229. /** Checks if param is initialized. */
  230. bool operator==(const nullptr_t &nullval) const
  231. {
  232. return mParamDesc == nullptr;
  233. }
  234. protected:
  235. GpuParamsType mParent;
  236. GpuParamObjectDesc* mParamDesc;
  237. };
  238. /** @} */
  239. /** @addtogroup RenderAPI
  240. * @{
  241. */
  242. typedef TGpuDataParam<float, false> GpuParamFloat;
  243. typedef TGpuDataParam<Vector2, false> GpuParamVec2;
  244. typedef TGpuDataParam<Vector3, false> GpuParamVec3;
  245. typedef TGpuDataParam<Vector4, false> GpuParamVec4;
  246. typedef TGpuDataParam<int, false> GpuParamInt;
  247. typedef TGpuDataParam<Vector2I, false> GpuParamVec2I;
  248. typedef TGpuDataParam<Vector3I, false> GpuParamVec3I;
  249. typedef TGpuDataParam<Vector4I, false> GpuParamVec4I;
  250. typedef TGpuDataParam<Matrix3, false> GpuParamMat3;
  251. typedef TGpuDataParam<Matrix4, false> GpuParamMat4;
  252. typedef TGpuDataParam<Color, false> GpuParamColor;
  253. typedef TGpuDataParam<float, true> GpuParamFloatCore;
  254. typedef TGpuDataParam<Vector2, true> GpuParamVec2Core;
  255. typedef TGpuDataParam<Vector3, true> GpuParamVec3Core;
  256. typedef TGpuDataParam<Vector4, true> GpuParamVec4Core;
  257. typedef TGpuDataParam<int, true> GpuParamIntCore;
  258. typedef TGpuDataParam<Vector2I, true> GpuParamVec2ICore;
  259. typedef TGpuDataParam<Vector3I, true> GpuParamVec3ICore;
  260. typedef TGpuDataParam<Vector4I, true> GpuParamVec4ICore;
  261. typedef TGpuDataParam<Matrix3, true> GpuParamMat3Core;
  262. typedef TGpuDataParam<Matrix4, true> GpuParamMat4Core;
  263. typedef TGpuDataParam<Color, true> GpuParamColorCore;
  264. typedef TGpuParamStruct<false> GpuParamStruct;
  265. typedef TGpuParamStruct<true> GpuParamStructCore;
  266. typedef TGpuParamTexture<false> GpuParamTexture;
  267. typedef TGpuParamTexture<true> GpuParamTextureCore;
  268. typedef TGpuParamBuffer<false> GpuParamBuffer;
  269. typedef TGpuParamBuffer<true> GpuParamBufferCore;
  270. typedef TGpuParamSampState<false> GpuParamSampState;
  271. typedef TGpuParamSampState<true> GpuParamSampStateCore;
  272. typedef TGpuParamLoadStoreTexture<false> GpuParamLoadStoreTexture;
  273. typedef TGpuParamLoadStoreTexture<true> GpuParamLoadStoreTextureCore;
  274. /** @} */
  275. }