BsGpuParam.h 11 KB

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