BsGpuParam.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  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/BsVectorNI.h"
  12. #include "Image/BsColor.h"
  13. namespace bs
  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<ct::GpuParams> Type; };
  21. template<bool Core> struct TGpuParamTextureType { };
  22. template<> struct TGpuParamTextureType < false > { typedef HTexture Type; };
  23. template<> struct TGpuParamTextureType < true > { typedef SPtr<ct::Texture> Type; };
  24. template<bool Core> struct TGpuParamSamplerStateType { };
  25. template<> struct TGpuParamSamplerStateType < false > { typedef SPtr<SamplerState> Type; };
  26. template<> struct TGpuParamSamplerStateType < true > { typedef SPtr<ct::SamplerState> Type; };
  27. template<bool Core> struct TGpuParamBufferType { };
  28. template<> struct TGpuParamBufferType < false > { typedef SPtr<GpuParamBlockBuffer> Type; };
  29. template<> struct TGpuParamBufferType < true > { typedef SPtr<ct::GpuParamBlockBuffer> Type; };
  30. template<bool Core> struct TGpuBufferType { };
  31. template<> struct TGpuBufferType < false > { typedef SPtr<GpuBuffer> Type; };
  32. template<> struct TGpuBufferType < true > { typedef SPtr<ct::GpuBuffer> 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) const;
  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) const;
  105. /** Returns meta-data about the parameter. */
  106. const GpuParamDataDesc& getDesc() const { return *mParamDesc; }
  107. /** Checks if param is initialized. */
  108. bool operator==(const std::nullptr_t& nullval) const
  109. {
  110. return mParamDesc == nullptr;
  111. }
  112. protected:
  113. GpuParamsType mParent;
  114. GpuParamDataDesc* mParamDesc;
  115. };
  116. /** @copydoc TGpuDataParam */
  117. template<bool Core>
  118. class BS_CORE_EXPORT TGpuParamStruct
  119. {
  120. public:
  121. typedef typename TGpuParamBufferType<Core>::Type GpuParamBufferType;
  122. typedef typename TGpuParamsPtrType<Core>::Type GpuParamsType;
  123. TGpuParamStruct();
  124. TGpuParamStruct(GpuParamDataDesc* paramDesc, const GpuParamsType& parent);
  125. /** @copydoc TGpuDataParam::set */
  126. void set(const void* value, UINT32 sizeBytes, UINT32 arrayIdx = 0) const;
  127. /** @copydoc TGpuDataParam::get */
  128. void get(void* value, UINT32 sizeBytes, UINT32 arrayIdx = 0) const;
  129. /** Returns the size of the struct in bytes. */
  130. UINT32 getElementSize() const;
  131. /** Returns meta-data about the parameter. */
  132. const GpuParamDataDesc& getDesc() const { return *mParamDesc; }
  133. /** Checks if param is initialized. */
  134. bool operator==(const std::nullptr_t& nullval) const
  135. {
  136. return mParamDesc == nullptr;
  137. }
  138. protected:
  139. GpuParamsType mParent;
  140. GpuParamDataDesc* mParamDesc;
  141. };
  142. /** @copydoc TGpuDataParam */
  143. template<bool Core>
  144. class BS_CORE_EXPORT TGpuParamTexture
  145. {
  146. private:
  147. friend class GpuParams;
  148. friend class ct::GpuParams;
  149. typedef typename TGpuParamsPtrType<Core>::Type GpuParamsType;
  150. typedef typename TGpuParamTextureType<Core>::Type TextureType;
  151. public:
  152. TGpuParamTexture();
  153. TGpuParamTexture(GpuParamObjectDesc* paramDesc, const GpuParamsType& parent);
  154. /** @copydoc TGpuDataParam::set */
  155. void set(const TextureType& texture, const TextureSurface& surface = TextureSurface::COMPLETE) const;
  156. /** @copydoc TGpuDataParam::get */
  157. TextureType get() const;
  158. /** @copydoc TGpuDataParam::getDesc */
  159. const GpuParamObjectDesc& getDesc() const { return *mParamDesc; }
  160. /** Checks if param is initialized. */
  161. bool operator==(const std::nullptr_t& nullval) const
  162. {
  163. return mParamDesc == nullptr;
  164. }
  165. protected:
  166. GpuParamsType mParent;
  167. GpuParamObjectDesc* mParamDesc;
  168. };
  169. /** @copydoc TGpuDataParam */
  170. template<bool Core>
  171. class BS_CORE_EXPORT TGpuParamLoadStoreTexture
  172. {
  173. private:
  174. friend class GpuParams;
  175. friend class ct::GpuParams;
  176. typedef typename TGpuParamsPtrType<Core>::Type GpuParamsType;
  177. typedef typename TGpuParamTextureType<Core>::Type TextureType;
  178. public:
  179. TGpuParamLoadStoreTexture();
  180. TGpuParamLoadStoreTexture(GpuParamObjectDesc* paramDesc, const GpuParamsType& parent);
  181. /** @copydoc TGpuDataParam::set */
  182. void set(const TextureType& texture, const TextureSurface& surface = TextureSurface()) const;
  183. /** @copydoc TGpuDataParam::get */
  184. TextureType get() const;
  185. /** @copydoc TGpuDataParam::getDesc */
  186. const GpuParamObjectDesc& getDesc() const { return *mParamDesc; }
  187. /** Checks if param is initialized. */
  188. bool operator==(const std::nullptr_t& nullval) const
  189. {
  190. return mParamDesc == nullptr;
  191. }
  192. protected:
  193. GpuParamsType mParent;
  194. GpuParamObjectDesc* mParamDesc;
  195. };
  196. /** @copydoc TGpuDataParam */
  197. template<bool Core>
  198. class BS_CORE_EXPORT TGpuParamBuffer
  199. {
  200. private:
  201. friend class GpuParams;
  202. friend class ct::GpuParams;
  203. typedef typename TGpuParamsPtrType<Core>::Type GpuParamsType;
  204. typedef typename TGpuBufferType<Core>::Type BufferType;
  205. public:
  206. TGpuParamBuffer();
  207. TGpuParamBuffer(GpuParamObjectDesc* paramDesc, const GpuParamsType& parent);
  208. /** @copydoc TGpuDataParam::set */
  209. void set(const BufferType& buffer) const;
  210. /** @copydoc TGpuDataParam::get */
  211. BufferType get() const;
  212. /** @copydoc TGpuDataParam::getDesc */
  213. const GpuParamObjectDesc& getDesc() const { return *mParamDesc; }
  214. /** Checks if param is initialized. */
  215. bool operator==(const std::nullptr_t& nullval) const
  216. {
  217. return mParamDesc == nullptr;
  218. }
  219. protected:
  220. GpuParamsType mParent;
  221. GpuParamObjectDesc* mParamDesc;
  222. };
  223. /** @copydoc TGpuDataParam */
  224. template<bool Core>
  225. class BS_CORE_EXPORT TGpuParamSampState
  226. {
  227. private:
  228. friend class GpuParams;
  229. friend class ct::GpuParams;
  230. typedef typename TGpuParamsPtrType<Core>::Type GpuParamsType;
  231. typedef typename TGpuParamSamplerStateType<Core>::Type SamplerStateType;
  232. public:
  233. TGpuParamSampState();
  234. TGpuParamSampState(GpuParamObjectDesc* paramDesc, const GpuParamsType& parent);
  235. /** @copydoc TGpuDataParam::set */
  236. void set(const SamplerStateType& samplerState) const;
  237. /** @copydoc TGpuDataParam::get */
  238. SamplerStateType get() const;
  239. /** @copydoc TGpuDataParam::getDesc */
  240. const GpuParamObjectDesc& getDesc() const { return *mParamDesc; }
  241. /** Checks if param is initialized. */
  242. bool operator==(const std::nullptr_t& nullval) const
  243. {
  244. return mParamDesc == nullptr;
  245. }
  246. protected:
  247. GpuParamsType mParent;
  248. GpuParamObjectDesc* mParamDesc;
  249. };
  250. /** @} */
  251. /** @addtogroup RenderAPI
  252. * @{
  253. */
  254. typedef TGpuDataParam<float, false> GpuParamFloat;
  255. typedef TGpuDataParam<Vector2, false> GpuParamVec2;
  256. typedef TGpuDataParam<Vector3, false> GpuParamVec3;
  257. typedef TGpuDataParam<Vector4, false> GpuParamVec4;
  258. typedef TGpuDataParam<int, false> GpuParamInt;
  259. typedef TGpuDataParam<Vector2I, false> GpuParamVec2I;
  260. typedef TGpuDataParam<Vector3I, false> GpuParamVec3I;
  261. typedef TGpuDataParam<Vector4I, false> GpuParamVec4I;
  262. typedef TGpuDataParam<Matrix3, false> GpuParamMat3;
  263. typedef TGpuDataParam<Matrix4, false> GpuParamMat4;
  264. typedef TGpuDataParam<Color, false> GpuParamColor;
  265. typedef TGpuParamStruct<false> GpuParamStruct;
  266. typedef TGpuParamTexture<false> GpuParamTexture;
  267. typedef TGpuParamBuffer<false> GpuParamBuffer;
  268. typedef TGpuParamSampState<false> GpuParamSampState;
  269. typedef TGpuParamLoadStoreTexture<false> GpuParamLoadStoreTexture;
  270. namespace ct
  271. {
  272. typedef TGpuDataParam<float, true> GpuParamFloat;
  273. typedef TGpuDataParam<Vector2, true> GpuParamVec2;
  274. typedef TGpuDataParam<Vector3, true> GpuParamVec3;
  275. typedef TGpuDataParam<Vector4, true> GpuParamVec4;
  276. typedef TGpuDataParam<int, true> GpuParamInt;
  277. typedef TGpuDataParam<Vector2I, true> GpuParamVec2I;
  278. typedef TGpuDataParam<Vector3I, true> GpuParamVec3I;
  279. typedef TGpuDataParam<Vector4I, true> GpuParamVec4I;
  280. typedef TGpuDataParam<Matrix3, true> GpuParamMat3;
  281. typedef TGpuDataParam<Matrix4, true> GpuParamMat4;
  282. typedef TGpuDataParam<Color, true> GpuParamColor;
  283. typedef TGpuParamStruct<true> GpuParamStruct;
  284. typedef TGpuParamTexture<true> GpuParamTexture;
  285. typedef TGpuParamBuffer<true> GpuParamBuffer;
  286. typedef TGpuParamSampState<true> GpuParamSampState;
  287. typedef TGpuParamLoadStoreTexture<true> GpuParamLoadStoreTexture;
  288. }
  289. /** @} */
  290. }