BsMaterialParam.h 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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 "BsGpuParam.h"
  6. namespace BansheeEngine
  7. {
  8. /** @addtogroup Implementation
  9. * @{
  10. */
  11. class MaterialParams;
  12. class MaterialParamsCore;
  13. template<bool Core> struct TMaterialParamsType { };
  14. template<> struct TMaterialParamsType<false> { typedef MaterialParams Type; };
  15. template<> struct TMaterialParamsType<true> { typedef MaterialParamsCore Type; };
  16. /**
  17. * A handle that allows you to set a Material parameter. Internally keeps a reference to the material parameters so that
  18. * possibly expensive lookup of parameter name can be avoided each time the parameter is accessed, and instead the
  19. * handle can be cached.
  20. *
  21. * @note
  22. * This is pretty much identical to GPU parameter version (for example TGpuDataParam), except that this will get/set
  23. * parameter values on all GPU programs attached to the material, while TGpuDataParam works only for single GPU
  24. * program's parameters. Also, additional parameters that might be optimized out in the GPU program will still exist
  25. * here as long as they're defined in the shader used by the material, which is not the case with TGpuDataParam.
  26. * @note
  27. * For core-thread version of this class no shader-based caching is done, and instead this represents just a wrapper
  28. * for multiple GPU parameters.
  29. *
  30. * @see Material
  31. */
  32. template<class T, bool Core>
  33. class BS_CORE_EXPORT TMaterialDataParam
  34. {
  35. typedef typename TMaterialParamsType<Core>::Type MaterialParamsType;
  36. public:
  37. TMaterialDataParam(const String& name, const SPtr<MaterialParamsType>& params,
  38. const SPtr<Vector<TGpuDataParam<T, Core>>>& gpuParams);
  39. TMaterialDataParam() { }
  40. /** @copydoc TGpuDataParam::set */
  41. void set(const T& value, UINT32 arrayIdx = 0) const;
  42. /** @copydoc TGpuDataParam::get */
  43. T get(UINT32 arrayIdx = 0) const;
  44. /** Checks if param is initialized. */
  45. bool operator==(const nullptr_t& nullval) const
  46. {
  47. return mGPUParams == nullptr;
  48. }
  49. protected:
  50. UINT32 mParamIndex;
  51. UINT32 mArraySize;
  52. SPtr<MaterialParamsType> mMaterialParams;
  53. SPtr<Vector<TGpuDataParam<T, Core>>> mGPUParams;
  54. };
  55. /** @copydoc TMaterialDataParam */
  56. template<bool Core>
  57. class BS_CORE_EXPORT TMaterialParamStruct
  58. {
  59. typedef typename TMaterialParamsType<Core>::Type MaterialParamsType;
  60. public:
  61. TMaterialParamStruct(const String& name, const SPtr<MaterialParamsType>& params,
  62. const SPtr<Vector<TGpuParamStruct<Core>>>& gpuParams);
  63. TMaterialParamStruct() { }
  64. /** @copydoc TGpuParamStruct::set */
  65. void set(const void* value, UINT32 sizeBytes, UINT32 arrayIdx = 0) const;
  66. /** @copydoc TGpuParamStruct::get */
  67. void get(void* value, UINT32 sizeBytes, UINT32 arrayIdx = 0) const;
  68. /** @copydoc TGpuParamStruct::getElementSize */
  69. UINT32 getElementSize() const;
  70. /** Checks if param is initialized. */
  71. bool operator==(const nullptr_t& nullval) const
  72. {
  73. return mGPUParams == nullptr;
  74. }
  75. protected:
  76. UINT32 mParamIndex;
  77. UINT32 mArraySize;
  78. SPtr<MaterialParamsType> mMaterialParams;
  79. SPtr<Vector<TGpuParamStruct<Core>>> mGPUParams;
  80. };
  81. /** @copydoc TMaterialDataParam */
  82. template<bool Core>
  83. class BS_CORE_EXPORT TMaterialParamTexture
  84. {
  85. typedef typename TMaterialParamsType<Core>::Type MaterialParamsType;
  86. typedef typename TGpuParamTextureType<Core>::Type TextureType;
  87. public:
  88. TMaterialParamTexture(const String& name, const SPtr<MaterialParamsType>& params,
  89. const SPtr<Vector<TGpuParamTexture<Core>>>& gpuParams);
  90. TMaterialParamTexture() { }
  91. /** @copydoc GpuParamTexture::set */
  92. void set(const TextureType& texture) const;
  93. /** @copydoc GpuParamTexture::get */
  94. TextureType get() const;
  95. /** Checks if param is initialized. */
  96. bool operator==(const nullptr_t& nullval) const
  97. {
  98. return mGPUParams == nullptr;
  99. }
  100. protected:
  101. UINT32 mParamIndex;
  102. SPtr<MaterialParamsType> mMaterialParams;
  103. SPtr<Vector<TGpuParamTexture<Core>>> mGPUParams;
  104. };
  105. /** @copydoc TMaterialDataParam */
  106. template<bool Core>
  107. class BS_CORE_EXPORT TMaterialParamLoadStoreTexture
  108. {
  109. typedef typename TMaterialParamsType<Core>::Type MaterialParamsType;
  110. typedef typename TGpuParamTextureType<Core>::Type TextureType;
  111. public:
  112. TMaterialParamLoadStoreTexture(const String& name, const SPtr<MaterialParamsType>& params,
  113. const SPtr<Vector<TGpuParamLoadStoreTexture<Core>>>& gpuParams);
  114. TMaterialParamLoadStoreTexture() { }
  115. /** @copydoc GpuParamLoadStoreTexture::set */
  116. void set(const TextureType& texture, const TextureSurface& surface = TextureSurface()) const;
  117. /** @copydoc GpuParamLoadStoreTexture::get */
  118. TextureType get() const;
  119. /** Checks if param is initialized. */
  120. bool operator==(const nullptr_t& nullval) const
  121. {
  122. return mGPUParams == nullptr;
  123. }
  124. protected:
  125. UINT32 mParamIndex;
  126. SPtr<MaterialParamsType> mMaterialParams;
  127. SPtr<Vector<TGpuParamLoadStoreTexture<Core>>> mGPUParams;
  128. };
  129. /** @copydoc TMaterialDataParam */
  130. template<bool Core>
  131. class BS_CORE_EXPORT TMaterialParamBuffer
  132. {
  133. typedef typename TMaterialParamsType<Core>::Type MaterialParamsType;
  134. typedef typename TGpuBufferType<Core>::Type BufferType;
  135. public:
  136. TMaterialParamBuffer(const String& name, const SPtr<MaterialParamsType>& params,
  137. const SPtr<Vector<TGpuParamBuffer<Core>>>& gpuParams);
  138. TMaterialParamBuffer() { }
  139. /** @copydoc GpuParamBuffer::set */
  140. void set(const BufferType& buffer) const;
  141. /** @copydoc GpuParamBuffer::get */
  142. BufferType get() const;
  143. /** Checks if param is initialized. */
  144. bool operator==(const nullptr_t& nullval) const
  145. {
  146. return mGPUParams == nullptr;
  147. }
  148. protected:
  149. UINT32 mParamIndex;
  150. SPtr<MaterialParamsType> mMaterialParams;
  151. SPtr<Vector<TGpuParamBuffer<Core>>> mGPUParams;
  152. };
  153. /** @copydoc TMaterialDataParam */
  154. template<bool Core>
  155. class BS_CORE_EXPORT TMaterialParamSampState
  156. {
  157. typedef typename TMaterialParamsType<Core>::Type MaterialParamsType;
  158. typedef typename TGpuParamSamplerStateType<Core>::Type SamplerStateType;
  159. public:
  160. TMaterialParamSampState(const String& name, const SPtr<MaterialParamsType>& params,
  161. const SPtr<Vector<TGpuParamSampState<Core>>>& gpuParams);
  162. TMaterialParamSampState() { }
  163. /** @copydoc GpuParamSampState::set */
  164. void set(const SamplerStateType& sampState) const;
  165. /** @copydoc GpuParamSampState::get */
  166. SamplerStateType get() const;
  167. /** Checks if param is initialized. */
  168. bool operator==(const nullptr_t& nullval) const
  169. {
  170. return mGPUParams == nullptr;
  171. }
  172. protected:
  173. UINT32 mParamIndex;
  174. SPtr<MaterialParamsType> mMaterialParams;
  175. SPtr<Vector<TGpuParamSampState<Core>>> mGPUParams;
  176. };
  177. /** @} */
  178. /** @addtogroup Material
  179. * @{
  180. */
  181. typedef TMaterialDataParam<float, false> MaterialParamFloat;
  182. typedef TMaterialDataParam<Vector2, false> MaterialParamVec2;
  183. typedef TMaterialDataParam<Vector3, false> MaterialParamVec3;
  184. typedef TMaterialDataParam<Vector4, false> MaterialParamVec4;
  185. typedef TMaterialDataParam<int, false> MaterialParamInt;
  186. typedef TMaterialDataParam<Vector2I, false> MaterialParamVec2I;
  187. typedef TMaterialDataParam<Vector3I, false> MaterialParamVec3I;
  188. typedef TMaterialDataParam<Vector4I, false> MaterialParamVec4I;
  189. typedef TMaterialDataParam<Matrix3, false> MaterialParamMat3;
  190. typedef TMaterialDataParam<Matrix4, false> MaterialParamMat4;
  191. typedef TMaterialDataParam<Color, false> MaterialParamColor;
  192. typedef TMaterialDataParam<float, true> MaterialParamFloatCore;
  193. typedef TMaterialDataParam<Vector2, true> MaterialParamVec2Core;
  194. typedef TMaterialDataParam<Vector3, true> MaterialParamVec3Core;
  195. typedef TMaterialDataParam<Vector4, true> MaterialParamVec4Core;
  196. typedef TMaterialDataParam<int, true> MaterialParamIntCore;
  197. typedef TMaterialDataParam<Vector2I, true> MaterialParamVec2ICore;
  198. typedef TMaterialDataParam<Vector3I, true> MaterialParamVec3ICore;
  199. typedef TMaterialDataParam<Vector4I, true> MaterialParamVec4ICore;
  200. typedef TMaterialDataParam<Matrix3, true> MaterialParamMat3Core;
  201. typedef TMaterialDataParam<Matrix4, true> MaterialParamMat4Core;
  202. typedef TMaterialDataParam<Color, true> MaterialParamColorCore;
  203. typedef TMaterialParamStruct<false> MaterialParamStruct;
  204. typedef TMaterialParamStruct<true> MaterialParamStructCore;
  205. typedef TMaterialParamTexture<false> MaterialParamTexture;
  206. typedef TMaterialParamTexture<true> MaterialParamTextureCore;
  207. typedef TMaterialParamLoadStoreTexture<false> MaterialParamLoadStoreTexture;
  208. typedef TMaterialParamLoadStoreTexture<true> MaterialParamLoadStoreTextureCore;
  209. typedef TMaterialParamBuffer<false> MaterialParamBuffer;
  210. typedef TMaterialParamBuffer<true> MaterialParamBufferCore;
  211. typedef TMaterialParamSampState<false> MaterialParamSampState;
  212. typedef TMaterialParamSampState<true> MaterialParamSampStateCore;
  213. /** @} */
  214. }