BsMaterialParam.h 8.8 KB

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