BsMaterialParam.h 8.5 KB

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