BsMaterial.h 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801
  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 "BsResource.h"
  6. #include "BsIResourceListener.h"
  7. #include "BsMaterialParam.h"
  8. #include "BsTechnique.h"
  9. #include "BsVector2.h"
  10. #include "BsVector3.h"
  11. #include "BsVector4.h"
  12. #include "BsMatrix3.h"
  13. #include "BsMatrix4.h"
  14. namespace BansheeEngine
  15. {
  16. /** @addtogroup Implementation
  17. * @{
  18. */
  19. class MaterialParams;
  20. /** Helper class containing parameters for all types of GPU programs used in a pass. */
  21. template<bool Core>
  22. class TPassParameters
  23. {
  24. public:
  25. typedef typename TGpuParamsPtrType<Core>::Type GpuParamsType;
  26. /**
  27. * Returns a set of GPU parameters based on an index.
  28. *
  29. * @note Useful when needing to iterate over all sets of GPU parameters.
  30. */
  31. GpuParamsType& getParamByIdx(UINT32 idx)
  32. {
  33. GpuParamsType* paramArray[] = { &mVertParams, &mFragParams, &mGeomParams, &mHullParams, &mDomainParams, &mComputeParams };
  34. return *paramArray[idx];
  35. }
  36. /**
  37. * Sets GPU parameters based on an index.
  38. *
  39. * @note Useful when needing to iterate over all sets of GPU parameters.
  40. */
  41. void setParamByIdx(UINT32 idx, const GpuParamsType& params)
  42. {
  43. GpuParamsType* paramArray[] = { &mVertParams, &mFragParams, &mGeomParams, &mHullParams, &mDomainParams, &mComputeParams };
  44. (*paramArray[idx]) = params;
  45. }
  46. GpuParamsType mVertParams;
  47. GpuParamsType mFragParams;
  48. GpuParamsType mGeomParams;
  49. GpuParamsType mHullParams;
  50. GpuParamsType mDomainParams;
  51. GpuParamsType mComputeParams;
  52. };
  53. /** Contains sim thread pass parameters for each shader stage. */
  54. class BS_CORE_EXPORT PassParameters : public TPassParameters<false>
  55. {
  56. public:
  57. static const UINT32 NUM_PARAMS;
  58. };
  59. /** Contains core thread pass parameters for each shader stage. */
  60. class BS_CORE_EXPORT PassParametersCore : public TPassParameters<true>
  61. {
  62. public:
  63. static const UINT32 NUM_PARAMS;
  64. };
  65. template<bool Core> struct TGpuParamBlockBufferPtrType { };
  66. template<> struct TGpuParamBlockBufferPtrType<false> { typedef SPtr<GpuParamBlockBuffer> Type; };
  67. template<> struct TGpuParamBlockBufferPtrType<true> { typedef SPtr<GpuParamBlockBufferCore> Type; };
  68. template<bool Core> struct TGpuProgramType { };
  69. template<> struct TGpuProgramType<false> { typedef SPtr<GpuProgram> Type; };
  70. template<> struct TGpuProgramType<true> { typedef SPtr<GpuProgramCore> Type; };
  71. template<bool Core> struct TShaderType {};
  72. template<> struct TShaderType < false > { typedef HShader Type; };
  73. template<> struct TShaderType < true > { typedef SPtr<ShaderCore> Type; };
  74. template<bool Core> struct TGpuParamBlockBufferType {};
  75. template<> struct TGpuParamBlockBufferType < false > { typedef GpuParamBlockBuffer Type; };
  76. template<> struct TGpuParamBlockBufferType < true > { typedef GpuParamBlockBufferCore Type; };
  77. template<bool Core> struct TPassParamsType {};
  78. template<> struct TPassParamsType < false > { typedef PassParameters Type; };
  79. template<> struct TPassParamsType < true > { typedef PassParametersCore Type; };
  80. /**
  81. * Material that controls how objects are rendered. It is represented by a shader and parameters used to set up that
  82. * shader. It provides a simple interface for manipulating the parameters.
  83. */
  84. class BS_CORE_EXPORT MaterialBase
  85. {
  86. public:
  87. /** Data used to describe a structure defined within a shader. */
  88. struct StructData
  89. {
  90. StructData()
  91. :data(nullptr), size(0)
  92. { }
  93. StructData(UINT32 _size)
  94. :size(_size)
  95. {
  96. data = std::shared_ptr<void>(bs_alloc(_size), (void(*)(void*))&bs_free);
  97. }
  98. /**
  99. * Writes the specified data to the internal buffer. Caller must ensure size of the provided buffer is correct.
  100. */
  101. void write(void* _data)
  102. {
  103. memcpy(data.get(), _data, size);
  104. }
  105. SPtr<void> data;
  106. UINT32 size;
  107. };
  108. MaterialBase() { }
  109. virtual ~MaterialBase() { }
  110. protected:
  111. /** Retrieves a list of all shader GPU parameters, and the GPU program variable names they map to. */
  112. const Map<String, String>& getValidParamNames() const { return mValidParams; }
  113. /** Marks the contents of the sim thread object as dirty, causing it to sync with its core thread counterpart. */
  114. virtual void _markCoreDirty() { }
  115. /** @copydoc CoreObject::markDependenciesDirty */
  116. virtual void _markDependenciesDirty() { }
  117. /** @copydoc IResourceListener::markListenerResourcesDirty */
  118. virtual void _markResourcesDirty() { }
  119. /** Returns all GPU parameter descriptions in the specified technique. */
  120. static Vector<SPtr<GpuParamDesc>> getAllParamDescs(const SPtr<Technique>& technique);
  121. /** Returns all GPU parameter descriptions in the specified technique. */
  122. static Vector<SPtr<GpuParamDesc>> getAllParamDescs(const SPtr<TechniqueCore>& technique);
  123. Set<String> mValidShareableParamBlocks;
  124. Map<String, String> mValidParams; // Also maps Shader param name -> gpu variable name
  125. };
  126. /** @copydoc MaterialBase */
  127. template<bool Core>
  128. class BS_CORE_EXPORT TMaterial : public MaterialBase
  129. {
  130. public:
  131. typedef typename TGpuParamsPtrType<Core>::Type GpuParamsType;
  132. typedef typename TGpuParamTextureType<Core>::Type TextureType;
  133. typedef typename TGpuParamSamplerStateType<Core>::Type SamplerStateType;
  134. typedef typename TGpuParamBlockBufferPtrType<Core>::Type ParamBlockPtrType;
  135. typedef typename TGpuParamBlockBufferType<Core>::Type ParamBlockType;
  136. typedef typename TGpuProgramType<Core>::Type GpuProgramType;
  137. typedef typename TPassType<Core>::Type PassType;
  138. typedef typename TTechniqueType<Core>::Type TechniqueType;
  139. typedef typename TShaderType<Core>::Type ShaderType;
  140. typedef typename TPassParamsType<Core>::Type PassParamsType;
  141. TMaterial() { }
  142. virtual ~TMaterial() { }
  143. /** Returns the currently active shader. */
  144. ShaderType getShader() const { return mShader; }
  145. /** Returns the number of passes that are used by the shader used in the material. */
  146. UINT32 getNumPasses() const;
  147. /** Retrieves a specific shader pass. */
  148. SPtr<PassType> getPass(UINT32 passIdx) const;
  149. /**
  150. * Assigns a float value to the shader parameter with the specified name.
  151. *
  152. * Optionally if the parameter is an array you may provide an array index to assign the value to.
  153. */
  154. void setFloat(const String& name, float value, UINT32 arrayIdx = 0) { return getParamFloat(name).set(value, arrayIdx); }
  155. /**
  156. * Assigns a color to the shader parameter with the specified name.
  157. *
  158. * Optionally if the parameter is an array you may provide an array index to assign the value to.
  159. */
  160. void setColor(const String& name, const Color& value, UINT32 arrayIdx = 0) { return getParamColor(name).set(value, arrayIdx); }
  161. /**
  162. * Assigns a 2D vector to the shader parameter with the specified name.
  163. *
  164. * Optionally if the parameter is an array you may provide an array index to assign the value to.
  165. */
  166. void setVec2(const String& name, const Vector2& value, UINT32 arrayIdx = 0) { return getParamVec2(name).set(value, arrayIdx); }
  167. /**
  168. * Assigns a 3D vector to the shader parameter with the specified name.
  169. *
  170. * Optionally if the parameter is an array you may provide an array index to assign the value to.
  171. */
  172. void setVec3(const String& name, const Vector3& value, UINT32 arrayIdx = 0) { return getParamVec3(name).set(value, arrayIdx); }
  173. /**
  174. * Assigns a 4D vector to the shader parameter with the specified name.
  175. *
  176. * Optionally if the parameter is an array you may provide an array index to assign the value to.
  177. */
  178. void setVec4(const String& name, const Vector4& value, UINT32 arrayIdx = 0) { return getParamVec4(name).set(value, arrayIdx); }
  179. /**
  180. * Assigns a 3x3 matrix to the shader parameter with the specified name.
  181. *
  182. * Optionally if the parameter is an array you may provide an array index to assign the value to.
  183. */
  184. void setMat3(const String& name, const Matrix3& value, UINT32 arrayIdx = 0) { return getParamMat3(name).set(value, arrayIdx); }
  185. /**
  186. * Assigns a 4x4 matrix to the shader parameter with the specified name.
  187. *
  188. * Optionally if the parameter is an array you may provide an array index to assign the value to.
  189. */
  190. void setMat4(const String& name, const Matrix4& value, UINT32 arrayIdx = 0) { return getParamMat4(name).set(value, arrayIdx); }
  191. /**
  192. * Assigns a structure to the shader parameter with the specified name.
  193. *
  194. * Structure is provided as a raw buffer and caller must ensure structure in buffer matches what the shader expects.
  195. *
  196. * Optionally if the parameter is an array you may provide an array index to assign the value to.
  197. */
  198. void setStructData(const String& name, void* value, UINT32 size, UINT32 arrayIdx = 0) { return getParamStruct(name).set(value, size, arrayIdx); }
  199. /** Assigns a texture to the shader parameter with the specified name. */
  200. void setTexture(const String& name, const TextureType& value) { return getParamTexture(name).set(value); }
  201. /** Assigns a texture to be used for random load/store operations to the shader parameter with the specified name. */
  202. void setLoadStoreTexture(const String& name, const TextureType& value, const TextureSurface& surface)
  203. {
  204. return getParamLoadStoreTexture(name).set(value, surface);
  205. }
  206. /** Assigns a sampler state to the shader parameter with the specified name. */
  207. void setSamplerState(const String& name, const SamplerStateType& value) { return getParamSamplerState(name).set(value); }
  208. /**
  209. * Returns a float value assigned with the parameter with the specified name.
  210. *
  211. * Optionally if the parameter is an array you may provide an array index you which to retrieve.
  212. */
  213. float getFloat(const String& name, UINT32 arrayIdx = 0) const { return getParamFloat(name).get(arrayIdx); }
  214. /**
  215. * Returns a color assigned with the parameter with the specified name.
  216. *
  217. * Optionally if the parameter is an array you may provide an array index you which to retrieve.
  218. */
  219. Color getColor(const String& name, UINT32 arrayIdx = 0) const { return getParamColor(name).get(arrayIdx); }
  220. /**
  221. * Returns a 2D vector assigned with the parameter with the specified name.
  222. *
  223. * Optionally if the parameter is an array you may provide an array index you which to retrieve.
  224. */
  225. Vector2 getVec2(const String& name, UINT32 arrayIdx = 0) const { return getParamVec2(name).get(arrayIdx); }
  226. /**
  227. * Returns a 3D vector assigned with the parameter with the specified name.
  228. *
  229. * Optionally if the parameter is an array you may provide an array index you which to retrieve.
  230. */
  231. Vector3 getVec3(const String& name, UINT32 arrayIdx = 0) const { return getParamVec3(name).get(arrayIdx); }
  232. /**
  233. * Returns a 4D vector assigned with the parameter with the specified name.
  234. *
  235. * Optionally if the parameter is an array you may provide an array index you which to retrieve.
  236. */
  237. Vector4 getVec4(const String& name, UINT32 arrayIdx = 0) const { return getParamVec4(name).get(arrayIdx); }
  238. /**
  239. * Returns a 3x3 matrix assigned with the parameter with the specified name.
  240. *
  241. * Optionally if the parameter is an array you may provide an array index you which to retrieve.
  242. */
  243. Matrix3 getMat3(const String& name, UINT32 arrayIdx = 0) const { return getParamMat3(name).get(arrayIdx); }
  244. /**
  245. * Returns a 4x4 matrix assigned with the parameter with the specified name.
  246. *
  247. * Optionally if the parameter is an array you may provide an array index you which to retrieve.
  248. */
  249. Matrix4 getMat4(const String& name, UINT32 arrayIdx = 0) const { return getParamMat4(name).get(arrayIdx); }
  250. /** Returns a texture assigned with the parameter with the specified name. */
  251. TextureType getTexture(const String& name) const { return getParamTexture(name).get(); }
  252. /** Returns a sampler state assigned with the parameter with the specified name. */
  253. SamplerStateType getSamplerState(const String& name) const { return getParamSamplerState(name).get(); }
  254. /**
  255. * Returns a buffer representing a structure assigned to the parameter with the specified name.
  256. *
  257. * Optionally if the parameter is an array you may provide an array index you which to retrieve.
  258. */
  259. MaterialBase::StructData getStructData(const String& name, UINT32 arrayIdx = 0) const
  260. {
  261. TMaterialParamStruct<Core> structParam = getParamStruct(name);
  262. MaterialBase::StructData data(structParam.getElementSize());
  263. structParam.get(data.data.get(), structParam.getElementSize(), arrayIdx);
  264. return data;
  265. }
  266. /**
  267. * Returns a float GPU parameter. This parameter may be used for more efficiently getting/setting GPU parameter
  268. * values than calling Material::get* / Material::set* methods.
  269. *
  270. * @note
  271. * Expected behavior is that you would retrieve this parameter when initially constructing the material, and then
  272. * use it throughout material lifetime to assign and retrieve parameter values.
  273. * @note
  274. * If material shader changes this handle will be invalidated.
  275. */
  276. TMaterialDataParam<float, Core> getParamFloat(const String& name) const
  277. {
  278. TMaterialDataParam<float, Core> gpuParam;
  279. getParam(name, gpuParam);
  280. return gpuParam;
  281. }
  282. /**
  283. * Returns a color GPU parameter. This parameter may be used for more efficiently getting/setting GPU parameter
  284. * values than calling Material::get* / Material::set* methods.
  285. *
  286. * @note
  287. * Expected behavior is that you would retrieve this parameter when initially constructing the material,
  288. * and then use it throughout material lifetime to assign and retrieve parameter values.
  289. * @note
  290. * If material shader changes this handle will be invalidated.
  291. */
  292. TMaterialDataParam<Color, Core> getParamColor(const String& name) const
  293. {
  294. TMaterialDataParam<Color, Core> gpuParam;
  295. getParam(name, gpuParam);
  296. return gpuParam;
  297. }
  298. /**
  299. * Returns a 2D vector GPU parameter. This parameter may be used for more efficiently getting/setting GPU parameter
  300. * values than calling Material::get* / Material::set* methods.
  301. *
  302. * @note
  303. * Expected behavior is that you would retrieve this parameter when initially constructing the material, and then
  304. * use it throughout material lifetime to assign and retrieve parameter values.
  305. * @note
  306. * If material shader changes this handle will be invalidated.
  307. */
  308. TMaterialDataParam<Vector2, Core> getParamVec2(const String& name) const
  309. {
  310. TMaterialDataParam<Vector2, Core> gpuParam;
  311. getParam(name, gpuParam);
  312. return gpuParam;
  313. }
  314. /**
  315. * Returns a 3D vector GPU parameter. This parameter may be used for more efficiently getting/setting GPU parameter
  316. * values than calling Material::get* / Material::set* methods.
  317. *
  318. * @note
  319. * Expected behavior is that you would retrieve this parameter when initially constructing the material, and then
  320. * use it throughout material lifetime to assign and retrieve parameter values.
  321. * @note
  322. * If material shader changes this handle will be invalidated.
  323. */
  324. TMaterialDataParam<Vector3, Core> getParamVec3(const String& name) const
  325. {
  326. TMaterialDataParam<Vector3, Core> gpuParam;
  327. getParam(name, gpuParam);
  328. return gpuParam;
  329. }
  330. /**
  331. * Returns a 4D vector GPU parameter. This parameter may be used for more efficiently getting/setting GPU parameter
  332. * values than calling Material::get* / Material::set* methods.
  333. *
  334. * @note
  335. * Expected behavior is that you would retrieve this parameter when initially constructing the material, and then
  336. * use it throughout material lifetime to assign and retrieve parameter values.
  337. * @note
  338. * If material shader changes this handle will be invalidated.
  339. */
  340. TMaterialDataParam<Vector4, Core> getParamVec4(const String& name) const
  341. {
  342. TMaterialDataParam<Vector4, Core> gpuParam;
  343. getParam(name, gpuParam);
  344. return gpuParam;
  345. }
  346. /**
  347. * Returns a 3x3 matrix GPU parameter. This parameter may be used for more efficiently getting/setting GPU
  348. * parameter values than calling Material::get* / Material::set* methods.
  349. *
  350. * @note
  351. * Expected behavior is that you would retrieve this parameter when initially constructing the material, and then
  352. * use it throughout material lifetime to assign and retrieve parameter values.
  353. * @note
  354. * If material shader changes this handle will be invalidated.
  355. */
  356. TMaterialDataParam<Matrix3, Core> getParamMat3(const String& name) const
  357. {
  358. TMaterialDataParam<Matrix3, Core> gpuParam;
  359. getParam(name, gpuParam);
  360. return gpuParam;
  361. }
  362. /**
  363. * Returns a 4x4 matrix GPU parameter. This parameter may be used for more efficiently getting/setting GPU parameter
  364. * values than calling Material::get* / Material::set* methods.
  365. *
  366. * @note
  367. * Expected behavior is that you would retrieve this parameter when initially constructing the material, and then
  368. * use it throughout material lifetime to assign and retrieve parameter values.
  369. * @note
  370. * If material shader changes this handle will be invalidated.
  371. */
  372. TMaterialDataParam<Matrix4, Core> getParamMat4(const String& name) const
  373. {
  374. TMaterialDataParam<Matrix4, Core> gpuParam;
  375. getParam(name, gpuParam);
  376. return gpuParam;
  377. }
  378. /**
  379. * Returns a structure GPU parameter. This parameter may be used for more efficiently getting/setting GPU parameter
  380. * values than calling Material::get* / Material::set* methods.
  381. *
  382. * @note
  383. * Expected behavior is that you would retrieve this parameter when initially constructing the material, and then
  384. * use it throughout material lifetime to assign and retrieve parameter values.
  385. * @note
  386. * If material shader changes this handle will be invalidated.
  387. */
  388. TMaterialParamStruct<Core> getParamStruct(const String& name) const;
  389. /**
  390. * Returns a texture GPU parameter. This parameter may be used for more efficiently getting/setting GPU parameter
  391. * values than calling Material::get* / Material::set* methods.
  392. *
  393. * @note
  394. * Expected behavior is that you would retrieve this parameter when initially constructing the material, and then
  395. * use it throughout material lifetime to assign and retrieve parameter values.
  396. * @note
  397. * If material shader changes this handle will be invalidated.
  398. */
  399. TMaterialParamTexture<Core> getParamTexture(const String& name) const;
  400. /**
  401. * Returns a GPU parameter for binding a load/store texture. This parameter may be used for more efficiently
  402. * getting/setting GPU parameter values than calling Material::get* / Material::set* methods.
  403. *
  404. * @note
  405. * Expected behavior is that you would retrieve this parameter when initially constructing the material, and then
  406. * use it throughout material lifetime to assign and retrieve parameter values.
  407. * @note
  408. * If material shader changes this handle will be invalidated.
  409. */
  410. TMaterialParamLoadStoreTexture<Core> getParamLoadStoreTexture(const String& name) const;
  411. /**
  412. * Returns a sampler state GPU parameter. This parameter may be used for more efficiently getting/setting GPU
  413. * parameter values than calling Material::get* / Material::set* methods.
  414. *
  415. * @note
  416. * Expected behavior is that you would retrieve this parameter when initially constructing the material, and then
  417. * use it throughout material lifetime to assign and retrieve parameter values.
  418. * @note
  419. * If material shader changes this handle will be invalidated.
  420. */
  421. TMaterialParamSampState<Core> getParamSamplerState(const String& name) const;
  422. /** Returns a set of parameters for all GPU programs in the specified shader pass. */
  423. SPtr<PassParamsType> getPassParameters(UINT32 passIdx) const { return mParametersPerPass[passIdx]; }
  424. /**
  425. * Assign a parameter block buffer with the specified name.
  426. *
  427. * @note
  428. * Parameter block buffers can be used as quick way of setting multiple parameters on a material at once, or
  429. * potentially sharing parameters between multiple materials. This reduces driver overhead as the parameters
  430. * in the buffers need only be set once and then reused multiple times.
  431. */
  432. void setParamBlockBuffer(const String& name, const ParamBlockPtrType& paramBlock);
  433. /**
  434. * Allows you to retrieve a handle to a parameter that you can then use for quickly setting and retrieving parameter
  435. * data. This allows you to set/get parameter data without all the cost of extra lookups otherwise required.
  436. *
  437. * @note
  438. * All of these handles will be invalidated if material shader ever changes. It is up to the caller to keep track
  439. * of that.
  440. */
  441. template <typename T>
  442. void getParam(const String& name, TMaterialDataParam<T, Core>& output) const;
  443. protected:
  444. /**
  445. * Creates a material param out of multiple GPU params. Caller must ensure all GPU params reference the same
  446. * parameter.
  447. */
  448. template <typename T>
  449. TMaterialDataParam<T, false> createDataParam(const String& name,
  450. const SPtr<Vector<TGpuDataParam<T, false>>>& gpuParams) const
  451. {
  452. return TMaterialDataParam<T, false>(name, getCachedParams(), gpuParams);
  453. }
  454. /**
  455. * Creates a material param out of multiple GPU params. Caller must ensure all GPU params reference the same
  456. * parameter.
  457. */
  458. template <typename T>
  459. TMaterialDataParam<T, true> createDataParam(const String& name,
  460. const SPtr<Vector<TGpuDataParam<T, true>>>& gpuParams) const
  461. {
  462. return TMaterialDataParam<T, true>(gpuParams);
  463. }
  464. /**
  465. * Creates a material param out of multiple GPU params. Caller must ensure all GPU params reference the same
  466. * parameter.
  467. */
  468. TMaterialParamStruct<false> createStructParam(const String& name,
  469. const SPtr<Vector<TGpuParamStruct<false>>>& gpuParams) const
  470. {
  471. return TMaterialParamStruct<false>(name, getCachedParams(), gpuParams);
  472. }
  473. /**
  474. * Creates a material param out of multiple GPU params. Caller must ensure all GPU params reference the same
  475. * parameter.
  476. */
  477. TMaterialParamStruct<true> createStructParam(const String& name,
  478. const SPtr<Vector<TGpuParamStruct<true>>>& gpuParams) const
  479. {
  480. return TMaterialParamStruct<true>(gpuParams);
  481. }
  482. /**
  483. * Creates a material param out of multiple GPU params. Caller must ensure all GPU params reference the same
  484. * parameter.
  485. */
  486. TMaterialParamTexture<false> createTextureParam(const String& name,
  487. const SPtr<Vector<TGpuParamTexture<false>>>& gpuParams) const
  488. {
  489. return TMaterialParamTexture<false>(name, getCachedParams(), gpuParams);
  490. }
  491. /**
  492. * Creates a material param out of multiple GPU params. Caller must ensure all GPU params reference the same
  493. * parameter.
  494. */
  495. TMaterialParamTexture<true> createTextureParam(const String& name,
  496. const SPtr<Vector<TGpuParamTexture<true>>>& gpuParams) const
  497. {
  498. return TMaterialParamTexture<true>(gpuParams);
  499. }
  500. /**
  501. * Creates a material param out of multiple GPU params. Caller must ensure all GPU params reference the same
  502. * parameter.
  503. */
  504. TMaterialParamLoadStoreTexture<false> createLoadStoreTextureParam(const String& name,
  505. const SPtr<Vector<TGpuParamLoadStoreTexture<false>>>& gpuParams) const
  506. {
  507. return TMaterialParamLoadStoreTexture<false>(name, getCachedParams(), gpuParams);
  508. }
  509. /**
  510. * Creates a material param out of multiple GPU params. Caller must ensure all GPU params reference the same
  511. * parameter.
  512. */
  513. TMaterialParamLoadStoreTexture<true> createLoadStoreTextureParam(const String& name,
  514. const SPtr<Vector<TGpuParamLoadStoreTexture<true>>>& gpuParams) const
  515. {
  516. return TMaterialParamLoadStoreTexture<true>(gpuParams);
  517. }
  518. /**
  519. * Creates a material param out of multiple GPU params. Caller must ensure all GPU params reference the same
  520. * parameter.
  521. */
  522. TMaterialParamSampState<false> createSamplerStateParam(const String& name,
  523. const SPtr<Vector<TGpuParamSampState<false>>>& gpuParams) const
  524. {
  525. return TMaterialParamSampState<false>(name, getCachedParams(), gpuParams);
  526. }
  527. /**
  528. * Creates a material param out of multiple GPU params. Caller must ensure all GPU params reference the same
  529. * parameter.
  530. */
  531. TMaterialParamSampState<true> createSamplerStateParam(const String& name,
  532. const SPtr<Vector<TGpuParamSampState<true>>>& gpuParams) const
  533. {
  534. return TMaterialParamSampState<true>(gpuParams);
  535. }
  536. /**
  537. * Assigns a value from a raw buffer to the parameter with the specified name. Buffer must be of sizeof(T) *
  538. * numElements size and initialized.
  539. *
  540. * @note Provided parameter must exist, no checking is done.
  541. */
  542. template <typename T>
  543. void setParamValue(const String& name, UINT8* buffer, UINT32 numElements);
  544. /** Called during initialization, creates enough space to cache all parameters (not used on core thread). */
  545. void createCachedParams(const SPtr<ShaderCore>& shader) { /* Do nothing */}
  546. /** Called during initialization, creates enough space to cache all parameters. */
  547. virtual void createCachedParams(const HShader& shader) { }
  548. /** Returns a list of all values assigned to material parameters. */
  549. virtual SPtr<MaterialParams> getCachedParams() const { return nullptr; }
  550. /**
  551. * Initializes the material by using the best technique from the currently set shader. Shader must contain the
  552. * technique that matches the current renderer and render system.
  553. */
  554. void initBestTechnique();
  555. /** Assigns all the default parameters specified in the shader to the material. */
  556. void initDefaultParameters();
  557. /** Throw an exception if no shader is set, or no acceptable technique was found. */
  558. void throwIfNotInitialized() const;
  559. Vector<SPtr<PassParamsType>> mParametersPerPass;
  560. ShaderType mShader;
  561. SPtr<TechniqueType> mBestTechnique;
  562. };
  563. /** @} */
  564. /** @addtogroup Material-Internal
  565. * @{
  566. */
  567. /** @copydoc MaterialBase */
  568. class BS_CORE_EXPORT MaterialCore : public CoreObjectCore, public TMaterial<true>
  569. {
  570. public:
  571. ~MaterialCore() { }
  572. /** @copydoc Material::setShader */
  573. void setShader(const SPtr<ShaderCore>& shader);
  574. /** Creates a new material with the specified shader. */
  575. static SPtr<MaterialCore> create(const SPtr<ShaderCore>& shader);
  576. private:
  577. friend class Material;
  578. MaterialCore() { }
  579. MaterialCore(const SPtr<ShaderCore>& shader);
  580. MaterialCore(const SPtr<ShaderCore>& shader, const SPtr<TechniqueCore>& bestTechnique,
  581. const Set<String>& validShareableParamBlocks, const Map<String, String>& validParams,
  582. const Vector<SPtr<PassParametersCore>>& passParams);
  583. /** @copydoc CoreObjectCore::syncToCore */
  584. void syncToCore(const CoreSyncData& data) override;
  585. };
  586. /** @} */
  587. /** @addtogroup Material
  588. * @{
  589. */
  590. /** @copydoc MaterialBase */
  591. class BS_CORE_EXPORT Material : public Resource, public TMaterial<false>, public IResourceListener
  592. {
  593. public:
  594. ~Material() { }
  595. /**
  596. * Sets a shader that will be used by the material. Best technique within the provided shader will be used for the
  597. * material.
  598. *
  599. * @note
  600. * Shader must be set before doing any other operations with the material.
  601. * @note
  602. * After setting the shader if you change the implementation of systems that a shader technique is dependent upon
  603. * (render system, renderer, etc), you will need to call this method again on all your Materials to make sure
  604. * technique used is updated.
  605. */
  606. void setShader(const HShader& shader);
  607. /** Retrieves an implementation of a material usable only from the core thread. */
  608. SPtr<MaterialCore> getCore() const;
  609. /** @copydoc CoreObject::initialize */
  610. void initialize() override;
  611. /** Creates a deep copy of the material and returns the new object. */
  612. HMaterial clone();
  613. /**
  614. * Creates a new empty material.
  615. *
  616. * @note Make sure you call Material::setShader before using it.
  617. */
  618. static HMaterial create();
  619. /** Creates a new material with the specified shader. */
  620. static HMaterial create(const HShader& shader);
  621. private:
  622. friend class MaterialManager;
  623. Material();
  624. Material(const HShader& shader);
  625. /** @copydoc CoreObject::createCore */
  626. SPtr<CoreObjectCore> createCore() const override;
  627. /** @copydoc CoreObject::syncToCore */
  628. CoreSyncData syncToCore(FrameAlloc* allocator) override;
  629. /** @copydoc CoreObject::getCoreDependencies */
  630. void getCoreDependencies(Vector<CoreObject*>& dependencies) override;
  631. /** @copydoc CoreObject::markCoreDirty */
  632. void _markCoreDirty() override;
  633. /** @copydoc CoreObject::markDependenciesDirty */
  634. void _markDependenciesDirty() override;
  635. /** @copydoc IResourceListener::markResourcesDirty */
  636. void _markResourcesDirty() override;
  637. /** @copydoc IResourceListener::getListenerResources */
  638. void getListenerResources(Vector<HResource>& resources) override;
  639. /** @copydoc IResourceListener::notifyResourceLoaded */
  640. void notifyResourceLoaded(const HResource& resource) override;
  641. /** @copydoc IResourceListener::notifyResourceChanged */
  642. void notifyResourceChanged(const HResource& resource) override;
  643. /** @copydoc Resource::getResourceDependencies */
  644. void getResourceDependencies(FrameVector<HResource>& dependencies) const override;
  645. /** Performs material initialization when all resources are ready. */
  646. void initializeIfLoaded();
  647. /** @copydoc Material::createCachedParams */
  648. void createCachedParams(const HShader& shader) override;
  649. /** @copydoc Material::getCachedParams */
  650. SPtr<MaterialParams> getCachedParams() const override { return mCachedParams; }
  651. /**
  652. * Uses the provided list of parameters to try to set every parameter in this material. Parameter whose name, type
  653. * or size don't match are ignored and will not be set.
  654. */
  655. void setParams(const SPtr<MaterialParams>& params);
  656. UINT32 mLoadFlags;
  657. SPtr<MaterialParams> mCachedParams;
  658. /************************************************************************/
  659. /* RTTI */
  660. /************************************************************************/
  661. public:
  662. friend class MaterialRTTI;
  663. static RTTITypeBase* getRTTIStatic();
  664. virtual RTTITypeBase* getRTTI() const override;
  665. };
  666. /** @} */
  667. }