BsMaterial.h 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664
  1. #pragma once
  2. #include "BsCorePrerequisites.h"
  3. #include "BsResource.h"
  4. #include "BsIResourceListener.h"
  5. #include "BsMaterialParam.h"
  6. #include "BsVector2.h"
  7. #include "BsVector3.h"
  8. #include "BsVector4.h"
  9. #include "BsMatrix3.h"
  10. #include "BsMatrix4.h"
  11. namespace BansheeEngine
  12. {
  13. /** @addtogroup Material
  14. * @{
  15. */
  16. /** Helper class containing parameters for all types of GPU programs used in a pass. */
  17. template<bool Core>
  18. class TPassParameters
  19. {
  20. public:
  21. typedef typename TGpuParamsPtrType<Core>::Type GpuParamsType;
  22. /**
  23. * Returns a set of GPU parameters based on an index.
  24. *
  25. * @note Useful when needing to iterate over all sets of GPU parameters.
  26. */
  27. GpuParamsType& getParamByIdx(UINT32 idx)
  28. {
  29. GpuParamsType* paramArray[] = { &mVertParams, &mFragParams, &mGeomParams, &mHullParams, &mDomainParams, &mComputeParams };
  30. return *paramArray[idx];
  31. }
  32. /**
  33. * Sets GPU parameters based on an index.
  34. *
  35. * @note Useful when needing to iterate over all sets of GPU parameters.
  36. */
  37. void setParamByIdx(UINT32 idx, const GpuParamsType& params)
  38. {
  39. GpuParamsType* paramArray[] = { &mVertParams, &mFragParams, &mGeomParams, &mHullParams, &mDomainParams, &mComputeParams };
  40. (*paramArray[idx]) = params;
  41. }
  42. GpuParamsType mVertParams;
  43. GpuParamsType mFragParams;
  44. GpuParamsType mGeomParams;
  45. GpuParamsType mHullParams;
  46. GpuParamsType mDomainParams;
  47. GpuParamsType mComputeParams;
  48. };
  49. template<bool Core> struct TGpuParamBlockBufferPtrType { };
  50. template<> struct TGpuParamBlockBufferPtrType<false> { typedef SPtr<GpuParamBlockBuffer> Type; };
  51. template<> struct TGpuParamBlockBufferPtrType<true> { typedef SPtr<GpuParamBlockBufferCore> Type; };
  52. template<bool Core> struct TGpuProgramType { };
  53. template<> struct TGpuProgramType<false> { typedef GpuProgramPtr Type; };
  54. template<> struct TGpuProgramType<true> { typedef SPtr<GpuProgramCore> Type; };
  55. /** Contains sim thread pass parameters for each shader stage. */
  56. class BS_CORE_EXPORT PassParameters : public TPassParameters<false>
  57. {
  58. public:
  59. static const UINT32 NUM_PARAMS;
  60. };
  61. /** Contains core thread pass parameters for each shader stage. */
  62. class BS_CORE_EXPORT PassParametersCore : public TPassParameters<true>
  63. {
  64. public:
  65. static const UINT32 NUM_PARAMS;
  66. };
  67. /**
  68. * Material that controls how objects are rendered. It is represented by a shader and parameters used to set up that
  69. * shader. It provides a simple interface for manipulating the parameters.
  70. */
  71. class BS_CORE_EXPORT MaterialBase
  72. {
  73. public:
  74. /** Data used to described a structure defined within a shader. */
  75. struct StructData
  76. {
  77. StructData()
  78. :size(0), data(nullptr)
  79. { }
  80. StructData(UINT32 _size)
  81. :size(_size)
  82. {
  83. data = std::shared_ptr<void>(bs_alloc(_size), &bs_free);
  84. }
  85. /**
  86. * Writes the specified data to the internal buffer. Caller must ensure size of the provided buffer is correct.
  87. */
  88. void write(void* _data)
  89. {
  90. memcpy(data.get(), _data, size);
  91. }
  92. std::shared_ptr<void> data;
  93. UINT32 size;
  94. };
  95. virtual ~MaterialBase() { }
  96. protected:
  97. /** Retrieves a list of all shader GPU parameters, and the GPU program variable names they map to. */
  98. const Map<String, String>& getValidParamNames() const { return mValidParams; }
  99. /** @copydoc CoreObject::markCoreDirty */
  100. virtual void _markCoreDirty() { }
  101. /** @copydoc CoreObject::markDependenciesDirty */
  102. virtual void _markDependenciesDirty() { }
  103. /** @copydoc IResourceListener::markResourcesDirty */
  104. virtual void _markResourcesDirty() { }
  105. /** Returns all GPU parameter descriptions in the specified technique. */
  106. static Vector<GpuParamDescPtr> getAllParamDescs(const SPtr<Technique>& technique);
  107. /** Returns all GPU parameter descriptions in the specified technique. */
  108. static Vector<GpuParamDescPtr> getAllParamDescs(const SPtr<TechniqueCore>& technique);
  109. Set<String> mValidShareableParamBlocks;
  110. Map<String, String> mValidParams; // Also maps Shader param name -> gpu variable name
  111. };
  112. /** @copydoc MaterialBase */
  113. template<bool Core>
  114. class BS_CORE_EXPORT TMaterial : public MaterialBase
  115. {
  116. public:
  117. template<bool Core> struct TPassType {};
  118. template<> struct TPassType < false > { typedef SPtr<Pass> Type; };
  119. template<> struct TPassType < true > { typedef SPtr<PassCore> Type; };
  120. template<bool Core> struct TTechniqueType {};
  121. template<> struct TTechniqueType < false > { typedef SPtr<Technique> Type; };
  122. template<> struct TTechniqueType < true > { typedef SPtr<TechniqueCore> Type; };
  123. template<bool Core> struct TShaderType {};
  124. template<> struct TShaderType < false > { typedef HShader Type; };
  125. template<> struct TShaderType < true > { typedef SPtr<ShaderCore> Type; };
  126. template<bool Core> struct TGpuParamBlockBufferType {};
  127. template<> struct TGpuParamBlockBufferType < false > { typedef GpuParamBlockBuffer Type; };
  128. template<> struct TGpuParamBlockBufferType < true > { typedef GpuParamBlockBufferCore Type; };
  129. template<bool Core> struct TPassParamsType {};
  130. template<> struct TPassParamsType < false > { typedef PassParameters Type; };
  131. template<> struct TPassParamsType < true > { typedef PassParametersCore Type; };
  132. typedef typename TGpuParamsPtrType<Core>::Type GpuParamsType;
  133. typedef typename TGpuParamTextureType<Core>::Type TextureType;
  134. typedef typename TGpuParamSamplerStateType<Core>::Type SamplerStateType;
  135. typedef typename TGpuParamBlockBufferPtrType<Core>::Type ParamBlockPtrType;
  136. typedef typename TGpuParamBlockBufferType<Core>::Type ParamBlockType;
  137. typedef typename TGpuProgramType<Core>::Type GpuProgramType;
  138. typedef typename TPassType<Core>::Type PassType;
  139. typedef typename TTechniqueType<Core>::Type TechniqueType;
  140. typedef typename TShaderType<Core>::Type ShaderType;
  141. typedef typename TPassParamsType<Core>::Type PassParamsType;
  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. 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. protected:
  434. /**
  435. * Allows you to retrieve a handle to a parameter that you can then use for quickly setting and retrieving parameter
  436. * data. This allows you to set/get parameter data without all the cost of extra lookups otherwise required.
  437. *
  438. * @note
  439. * All of these handles will be invalidated if material shader ever changes. It is up to the caller to keep track
  440. * of that.
  441. */
  442. template <typename T>
  443. void getParam(const String& name, TMaterialDataParam<T, Core>& output) const;
  444. /**
  445. * Assigns a value from a raw buffer to the parameter with the specified name. Buffer must be of sizeof(T) *
  446. * numElements size and initialized.
  447. *
  448. * @note Provided parameter must exist, no checking is done.
  449. */
  450. template <typename T>
  451. void setParamValue(const String& name, UINT8* buffer, UINT32 numElements);
  452. /**
  453. * Initializes the material by using the best technique from the currently set shader. Shader must contain the
  454. * technique that matches the current renderer and render system.
  455. */
  456. void initBestTechnique();
  457. /** Assigns all the default parameters specified in the shader to the material. */
  458. void initDefaultParameters();
  459. /** Throw an exception if no shader is set, or no acceptable technique was found. */
  460. void throwIfNotInitialized() const;
  461. Vector<SPtr<PassParamsType>> mParametersPerPass;
  462. ShaderType mShader;
  463. TechniqueType mBestTechnique;
  464. };
  465. /** @copydoc MaterialBase */
  466. class BS_CORE_EXPORT MaterialCore : public CoreObjectCore, public TMaterial<true>
  467. {
  468. public:
  469. ~MaterialCore() { }
  470. /** @copydoc Material::setShader */
  471. void setShader(const SPtr<ShaderCore>& shader);
  472. /** Creates a new material with the specified shader. */
  473. static SPtr<MaterialCore> create(const SPtr<ShaderCore>& shader);
  474. private:
  475. friend class Material;
  476. MaterialCore() { }
  477. MaterialCore(const SPtr<ShaderCore>& shader);
  478. MaterialCore(const SPtr<ShaderCore>& shader, const SPtr<TechniqueCore>& bestTechnique,
  479. const Set<String>& validShareableParamBlocks, const Map<String, String>& validParams,
  480. const Vector<SPtr<PassParametersCore>>& passParams);
  481. /** @copydoc CoreObjectCore::syncToCore */
  482. void syncToCore(const CoreSyncData& data) override;
  483. };
  484. /** @copydoc MaterialBase */
  485. class BS_CORE_EXPORT Material : public Resource, public TMaterial<false>, public IResourceListener
  486. {
  487. public:
  488. ~Material() { }
  489. /**
  490. * Sets a shader that will be used by the material. Best technique within the provided shader will be used for the
  491. * material.
  492. *
  493. * @note
  494. * Shader must be set before doing any other operations with the material.
  495. * @note
  496. * After setting the shader if change any systems that a shader technique is dependent upon (render system,
  497. * renderer, etc), you will need to call this method again on all your Materials to make sure technique used is
  498. * updated.
  499. */
  500. void setShader(const HShader& shader);
  501. /** Retrieves an implementation of a material usable only from the core thread. */
  502. SPtr<MaterialCore> getCore() const;
  503. /** @copydoc CoreObject::initialize */
  504. void initialize() override;
  505. /** Creates a deep copy of the material and returns the new object. */
  506. HMaterial Material::clone();
  507. /**
  508. * Creates a new empty material.
  509. *
  510. * @note Make sure you call Material::setShader before using it.
  511. */
  512. static HMaterial create();
  513. /** Creates a new material with the specified shader. */
  514. static HMaterial create(const HShader& shader);
  515. private:
  516. friend class MaterialManager;
  517. Material();
  518. Material(const HShader& shader);
  519. /** @copydoc CoreObject::createCore */
  520. SPtr<CoreObjectCore> createCore() const override;
  521. /** @copydoc CoreObject::syncToCore */
  522. CoreSyncData syncToCore(FrameAlloc* allocator) override;
  523. /** @copydoc CoreObject::getCoreDependencies */
  524. void getCoreDependencies(Vector<CoreObject*>& dependencies) override;
  525. /** @copydoc CoreObject::markCoreDirty */
  526. void _markCoreDirty() override;
  527. /** @copydoc CoreObject::markDependenciesDirty */
  528. void _markDependenciesDirty() override;
  529. /** @copydoc IResourceListener::markResourcesDirty */
  530. void _markResourcesDirty() override;
  531. /** @copydoc IResourceListener::getListenerResources */
  532. void getListenerResources(Vector<HResource>& resources) override;
  533. /** @copydoc IResourceListener::notifyResourceLoaded */
  534. void notifyResourceLoaded(const HResource& resource) override;
  535. /** @copydoc IResourceListener::notifyResourceChanged */
  536. void notifyResourceChanged(const HResource& resource) override;
  537. /** @copydoc Resource::getResourceDependencies */
  538. void getResourceDependencies(FrameVector<HResource>& dependencies) const override;
  539. /** Performs material initialization when all resources are ready. */
  540. void initializeIfLoaded();
  541. UINT32 mLoadFlags;
  542. /************************************************************************/
  543. /* RTTI */
  544. /************************************************************************/
  545. public:
  546. friend class MaterialRTTI;
  547. static RTTITypeBase* getRTTIStatic();
  548. virtual RTTITypeBase* getRTTI() const override;
  549. };
  550. /** @} */
  551. }