2
0

BsGpuParam.h 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  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 "BsColor.h"
  10. namespace BansheeEngine
  11. {
  12. /** @addtogroup Implementation
  13. * @{
  14. */
  15. template<bool Core> struct TGpuParamsPtrType { };
  16. template<> struct TGpuParamsPtrType<false> { typedef SPtr<GpuParams> Type; };
  17. template<> struct TGpuParamsPtrType<true> { typedef SPtr<GpuParamsCore> Type; };
  18. template<bool Core> struct TGpuParamTextureType { };
  19. template<> struct TGpuParamTextureType < false > { typedef HTexture Type; };
  20. template<> struct TGpuParamTextureType < true > { typedef SPtr<TextureCore> Type; };
  21. template<bool Core> struct TGpuParamSamplerStateType { };
  22. template<> struct TGpuParamSamplerStateType < false > { typedef SamplerStatePtr Type; };
  23. template<> struct TGpuParamSamplerStateType < true > { typedef SPtr<SamplerStateCore> Type; };
  24. /**
  25. * A handle that allows you to set a GpuProgram parameter. Internally keeps a reference to the GPU parameter buffer and
  26. * the necessary offsets. You should specialize this type for specific parameter types.
  27. *
  28. * Object of this type must be returned by a Material. Setting/Getting parameter values will internally access a GPU
  29. * parameter buffer attached to the Material this parameter was created from. Anything rendered with that material will
  30. * then use those set values.
  31. *
  32. * @note
  33. * Normally you can set a GpuProgram parameter by calling various set/get methods on a Material. This class primarily
  34. * used an as optimization in performance critical bits of code where it is important to locate and set parameters
  35. * quickly without any lookups (Mentioned set/get methods expect a parameter name). You just retrieve the handle once
  36. * and then set the parameter value many times with minimal performance impact.
  37. *
  38. * @see Material
  39. */
  40. template<class T, bool Core>
  41. class BS_CORE_EXPORT TGpuDataParam
  42. {
  43. private:
  44. template<bool Core> struct TGpuParamBufferType { };
  45. template<> struct TGpuParamBufferType < false > { typedef SPtr<GpuParamBlockBuffer> Type; };
  46. template<> struct TGpuParamBufferType < true > { typedef SPtr<GpuParamBlockBufferCore> Type; };
  47. typedef typename TGpuParamBufferType<Core>::Type GpuParamBufferType;
  48. typedef typename TGpuParamsPtrType<Core>::Type GpuParamsType;
  49. /**
  50. * Policy class that allows us to re-use this template class for matrices which might need transposing, and other
  51. * types which do not. Matrix needs to be transposed for certain render systems depending on how they store them
  52. * in memory.
  53. */
  54. template<class Type>
  55. struct TransposePolicy
  56. {
  57. static Type transpose(const Type& value) { return value; }
  58. static bool transposeEnabled(bool enabled) { return false; }
  59. };
  60. /** Transpose policy for 3x3 matrix. */
  61. template<>
  62. struct TransposePolicy<Matrix3>
  63. {
  64. static Matrix3 transpose(const Matrix3& value) { return value.transpose(); }
  65. static bool transposeEnabled(bool enabled) { return enabled; }
  66. };
  67. /** Transpose policy for 4x4 matrix. */
  68. template<>
  69. struct TransposePolicy<Matrix4>
  70. {
  71. static Matrix4 transpose(const Matrix4& value) { return value.transpose(); }
  72. static bool transposeEnabled(bool enabled) { return enabled; }
  73. };
  74. /** Transpose policy for NxM matrix. */
  75. template<int N, int M>
  76. struct TransposePolicy<MatrixNxM<N, M>>
  77. {
  78. static MatrixNxM<N, M> transpose(const MatrixNxM<N, M>& value) { return value.transpose(); }
  79. static bool transposeEnabled(bool enabled) { return enabled; }
  80. };
  81. public:
  82. TGpuDataParam();
  83. TGpuDataParam(GpuParamDataDesc* paramDesc, const GpuParamsType& parent);
  84. /**
  85. * Sets a parameter value at the specified array index. If parameter does not contain an array leave the index at 0.
  86. *
  87. * @note
  88. * Like with all GPU parameters, the actual GPU buffer will not be updated until rendering with material this
  89. * parameter was created from starts on the core thread.
  90. */
  91. void set(const T& value, UINT32 arrayIdx = 0);
  92. /**
  93. * Returns a value of a parameter at the specified array index. If parameter does not contain an array leave the
  94. * index at 0.
  95. *
  96. * @note No GPU reads are done. Data returned was cached when it was written.
  97. */
  98. T get(UINT32 arrayIdx = 0);
  99. /** Checks if param is initialized. */
  100. bool operator==(const nullptr_t &nullval) const
  101. {
  102. return mParamDesc == nullptr;
  103. }
  104. protected:
  105. GpuParamsType mParent;
  106. GpuParamDataDesc* mParamDesc;
  107. };
  108. /** @copydoc TGpuDataParam */
  109. template<bool Core>
  110. class BS_CORE_EXPORT TGpuParamStruct
  111. {
  112. public:
  113. template<bool Core> struct TGpuParamBufferType { };
  114. template<> struct TGpuParamBufferType < false > { typedef SPtr<GpuParamBlockBuffer> Type; };
  115. template<> struct TGpuParamBufferType < true > { typedef SPtr<GpuParamBlockBufferCore> Type; };
  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 TGpuObjectParam */
  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 TGpuObjectParam */
  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 TGpuObjectParam */
  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<Color, false> GpuParamColor;
  216. typedef TGpuDataParam<Vector2, false> GpuParamVec2;
  217. typedef TGpuDataParam<Vector3, false> GpuParamVec3;
  218. typedef TGpuDataParam<Vector4, false> GpuParamVec4;
  219. typedef TGpuDataParam<Matrix3, false> GpuParamMat3;
  220. typedef TGpuDataParam<Matrix4, false> GpuParamMat4;
  221. typedef TGpuDataParam<float, true> GpuParamFloatCore;
  222. typedef TGpuDataParam<Color, true> GpuParamColorCore;
  223. typedef TGpuDataParam<Vector2, true> GpuParamVec2Core;
  224. typedef TGpuDataParam<Vector3, true> GpuParamVec3Core;
  225. typedef TGpuDataParam<Vector4, true> GpuParamVec4Core;
  226. typedef TGpuDataParam<Matrix3, true> GpuParamMat3Core;
  227. typedef TGpuDataParam<Matrix4, true> GpuParamMat4Core;
  228. typedef TGpuParamStruct<false> GpuParamStruct;
  229. typedef TGpuParamStruct<true> GpuParamStructCore;
  230. typedef TGpuParamTexture<false> GpuParamTexture;
  231. typedef TGpuParamTexture<true> GpuParamTextureCore;
  232. typedef TGpuParamSampState<false> GpuParamSampState;
  233. typedef TGpuParamSampState<true> GpuParamSampStateCore;
  234. typedef TGpuParamLoadStoreTexture<false> GpuParamLoadStoreTexture;
  235. typedef TGpuParamLoadStoreTexture<true> GpuParamLoadStoreTextureCore;
  236. /** @} */
  237. }