BsGpuParam.h 8.2 KB

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