BsMaterial.h 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684
  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 "BsMaterialParams.h"
  9. #include "BsTechnique.h"
  10. #include "BsVector2.h"
  11. #include "BsVector3.h"
  12. #include "BsVector4.h"
  13. #include "BsMatrix3.h"
  14. #include "BsMatrix4.h"
  15. namespace BansheeEngine
  16. {
  17. /** @addtogroup Implementation
  18. * @{
  19. */
  20. template<bool Core> struct TGpuParamBlockBufferPtrType { };
  21. template<> struct TGpuParamBlockBufferPtrType<false> { typedef SPtr<GpuParamBlockBuffer> Type; };
  22. template<> struct TGpuParamBlockBufferPtrType<true> { typedef SPtr<GpuParamBlockBufferCore> Type; };
  23. template<bool Core> struct TGpuProgramType { };
  24. template<> struct TGpuProgramType<false> { typedef SPtr<GpuProgram> Type; };
  25. template<> struct TGpuProgramType<true> { typedef SPtr<GpuProgramCore> Type; };
  26. template<bool Core> struct TShaderType {};
  27. template<> struct TShaderType < false > { typedef HShader Type; };
  28. template<> struct TShaderType < true > { typedef SPtr<ShaderCore> Type; };
  29. template<bool Core> struct TGpuParamBlockBufferType {};
  30. template<> struct TGpuParamBlockBufferType < false > { typedef GpuParamBlockBuffer Type; };
  31. template<> struct TGpuParamBlockBufferType < true > { typedef GpuParamBlockBufferCore Type; };
  32. template<bool Core> struct TGpuParamsSetType {};
  33. template<> struct TGpuParamsSetType < false > { typedef GpuParamsSet Type; };
  34. template<> struct TGpuParamsSetType < true > { typedef GpuParamsSetCore Type; };
  35. /**
  36. * Material that controls how objects are rendered. It is represented by a shader and parameters used to set up that
  37. * shader. It provides a simple interface for manipulating the parameters.
  38. */
  39. class BS_CORE_EXPORT MaterialBase
  40. {
  41. public:
  42. /** Data used to describe a structure defined within a shader. */
  43. struct StructData
  44. {
  45. StructData()
  46. :data(nullptr), size(0)
  47. { }
  48. StructData(UINT32 _size)
  49. :size(_size)
  50. {
  51. data = std::shared_ptr<void>(bs_alloc(_size), (void(*)(void*))&bs_free);
  52. }
  53. /**
  54. * Writes the specified data to the internal buffer. Caller must ensure size of the provided buffer is correct.
  55. */
  56. void write(void* _data)
  57. {
  58. memcpy(data.get(), _data, size);
  59. }
  60. SPtr<void> data;
  61. UINT32 size;
  62. };
  63. MaterialBase() { }
  64. virtual ~MaterialBase() { }
  65. protected:
  66. /** Marks the contents of the sim thread object as dirty, causing it to sync with its core thread counterpart. */
  67. virtual void _markCoreDirty() { }
  68. /** @copydoc CoreObject::markDependenciesDirty */
  69. virtual void _markDependenciesDirty() { }
  70. /** @copydoc IResourceListener::markListenerResourcesDirty */
  71. virtual void _markResourcesDirty() { }
  72. };
  73. /** @copydoc MaterialBase */
  74. template<bool Core>
  75. class BS_CORE_EXPORT TMaterial : public MaterialBase
  76. {
  77. public:
  78. typedef typename TGpuParamTextureType<Core>::Type TextureType;
  79. typedef typename TGpuBufferType<Core>::Type BufferType;
  80. typedef typename TGpuParamSamplerStateType<Core>::Type SamplerStateType;
  81. typedef typename TGpuProgramType<Core>::Type GpuProgramType;
  82. typedef typename TPassType<Core>::Type PassType;
  83. typedef typename TTechniqueType<Core>::Type TechniqueType;
  84. typedef typename TShaderType<Core>::Type ShaderType;
  85. typedef typename TGpuParamsSetType<Core>::Type GpuParamsSetType;
  86. typedef typename TMaterialParamsType<Core>::Type MaterialParamsType;
  87. TMaterial() { }
  88. virtual ~TMaterial() { }
  89. /** Returns the currently active shader. */
  90. ShaderType getShader() const { return mShader; }
  91. /** Returns the total number of techniques supported by this material. */
  92. UINT32 getNumTechniques() const { return (UINT32)mTechniques.size(); }
  93. /** Attempts to find a technique with the supported tag. Returns an index of the technique, or -1 if not found. */
  94. UINT32 findTechnique(const StringID& tag);
  95. /**
  96. * Returns the number of passes that are used by the technique at the specified index.
  97. *
  98. * @param[in] techniqueIdx Index of the technique to retrieve the number of passes for. 0 is always guaranteed
  99. * to be the default technique.
  100. * @return Number of passes used by the technique.
  101. */
  102. UINT32 getNumPasses(UINT32 techniqueIdx = 0) const;
  103. /**
  104. * Retrieves a specific shader pass from the provided technique.
  105. *
  106. * @param[in] passIdx Sequential index of the pass to retrieve.
  107. * @param[in] techniqueIdx Index of the technique to retrieve the pass for. 0 is always guaranteed to be
  108. * the default technique.
  109. * @return Pass if found, null otherwise.
  110. */
  111. SPtr<PassType> getPass(UINT32 passIdx = 0, UINT32 techniqueIdx = 0) const;
  112. /**
  113. * Creates a set of GpuParams that may be used for binding material parameters to the GPU. The expected behaviour
  114. * is to create a set of GpuParams per-technique once, and then before binding them to the GPU call
  115. * updateParamsSet() to ensure any dirty parameters are transfered from the material to GpuParams. You may also
  116. * use the parameter set to manually modify parameters on a per-program basis, in which case no further updates from
  117. * the material are necessary.
  118. */
  119. SPtr<GpuParamsSetType> createParamsSet(UINT32 techniqueIdx = 0);
  120. /**
  121. * Updates the provided parameter set by recording in it any changes that were made since the last call. It is
  122. * assumed only a single parameter set exists per-material per-technique. If there are multiple the material
  123. * will not be able to track which parameters were updated since the last call.
  124. *
  125. * @param[in] paramsSet Parameter set to update.
  126. * @param[in] forceRefresh If true all material parameters will be assigned to the params set, regardless if
  127. * they are marked dirty or not.
  128. */
  129. void updateParamsSet(const SPtr<GpuParamsSetType>& paramsSet, bool forceRefresh = false);
  130. /**
  131. * Assigns a float value to the shader parameter with the specified name.
  132. *
  133. * Optionally if the parameter is an array you may provide an array index to assign the value to.
  134. */
  135. void setFloat(const String& name, float value, UINT32 arrayIdx = 0) { return getParamFloat(name).set(value, arrayIdx); }
  136. /**
  137. * Assigns a color to the shader parameter with the specified name.
  138. *
  139. * Optionally if the parameter is an array you may provide an array index to assign the value to.
  140. */
  141. void setColor(const String& name, const Color& value, UINT32 arrayIdx = 0) { return getParamColor(name).set(value, arrayIdx); }
  142. /**
  143. * Assigns a 2D vector to the shader parameter with the specified name.
  144. *
  145. * Optionally if the parameter is an array you may provide an array index to assign the value to.
  146. */
  147. void setVec2(const String& name, const Vector2& value, UINT32 arrayIdx = 0) { return getParamVec2(name).set(value, arrayIdx); }
  148. /**
  149. * Assigns a 3D vector to the shader parameter with the specified name.
  150. *
  151. * Optionally if the parameter is an array you may provide an array index to assign the value to.
  152. */
  153. void setVec3(const String& name, const Vector3& value, UINT32 arrayIdx = 0) { return getParamVec3(name).set(value, arrayIdx); }
  154. /**
  155. * Assigns a 4D vector to the shader parameter with the specified name.
  156. *
  157. * Optionally if the parameter is an array you may provide an array index to assign the value to.
  158. */
  159. void setVec4(const String& name, const Vector4& value, UINT32 arrayIdx = 0) { return getParamVec4(name).set(value, arrayIdx); }
  160. /**
  161. * Assigns a 3x3 matrix to the shader parameter with the specified name.
  162. *
  163. * Optionally if the parameter is an array you may provide an array index to assign the value to.
  164. */
  165. void setMat3(const String& name, const Matrix3& value, UINT32 arrayIdx = 0) { return getParamMat3(name).set(value, arrayIdx); }
  166. /**
  167. * Assigns a 4x4 matrix to the shader parameter with the specified name.
  168. *
  169. * Optionally if the parameter is an array you may provide an array index to assign the value to.
  170. */
  171. void setMat4(const String& name, const Matrix4& value, UINT32 arrayIdx = 0) { return getParamMat4(name).set(value, arrayIdx); }
  172. /**
  173. * Assigns a structure to the shader parameter with the specified name.
  174. *
  175. * Structure is provided as a raw buffer and caller must ensure structure in buffer matches what the shader expects.
  176. *
  177. * Optionally if the parameter is an array you may provide an array index to assign the value to.
  178. */
  179. void setStructData(const String& name, void* value, UINT32 size, UINT32 arrayIdx = 0) { return getParamStruct(name).set(value, size, arrayIdx); }
  180. /** Assigns a texture to the shader parameter with the specified name. */
  181. void setTexture(const String& name, const TextureType& value) { return getParamTexture(name).set(value); }
  182. /** Assigns a texture to be used for random load/store operations to the shader parameter with the specified name. */
  183. void setLoadStoreTexture(const String& name, const TextureType& value, const TextureSurface& surface)
  184. {
  185. return getParamLoadStoreTexture(name).set(value, surface);
  186. }
  187. /** Assigns a buffer to the shader parameter with the specified name. */
  188. void setBuffer(const String& name, const BufferType& value) { return getParamBuffer(name).set(value); }
  189. /** Assigns a sampler state to the shader parameter with the specified name. */
  190. void setSamplerState(const String& name, const SamplerStateType& value) { return getParamSamplerState(name).set(value); }
  191. /**
  192. * Returns a float value assigned with the parameter with the specified name.
  193. *
  194. * Optionally if the parameter is an array you may provide an array index you which to retrieve.
  195. */
  196. float getFloat(const String& name, UINT32 arrayIdx = 0) const { return getParamFloat(name).get(arrayIdx); }
  197. /**
  198. * Returns a color assigned with the parameter with the specified name.
  199. *
  200. * Optionally if the parameter is an array you may provide an array index you which to retrieve.
  201. */
  202. Color getColor(const String& name, UINT32 arrayIdx = 0) const { return getParamColor(name).get(arrayIdx); }
  203. /**
  204. * Returns a 2D vector assigned with the parameter with the specified name.
  205. *
  206. * Optionally if the parameter is an array you may provide an array index you which to retrieve.
  207. */
  208. Vector2 getVec2(const String& name, UINT32 arrayIdx = 0) const { return getParamVec2(name).get(arrayIdx); }
  209. /**
  210. * Returns a 3D vector assigned with the parameter with the specified name.
  211. *
  212. * Optionally if the parameter is an array you may provide an array index you which to retrieve.
  213. */
  214. Vector3 getVec3(const String& name, UINT32 arrayIdx = 0) const { return getParamVec3(name).get(arrayIdx); }
  215. /**
  216. * Returns a 4D vector assigned with the parameter with the specified name.
  217. *
  218. * Optionally if the parameter is an array you may provide an array index you which to retrieve.
  219. */
  220. Vector4 getVec4(const String& name, UINT32 arrayIdx = 0) const { return getParamVec4(name).get(arrayIdx); }
  221. /**
  222. * Returns a 3x3 matrix assigned with the parameter with the specified name.
  223. *
  224. * Optionally if the parameter is an array you may provide an array index you which to retrieve.
  225. */
  226. Matrix3 getMat3(const String& name, UINT32 arrayIdx = 0) const { return getParamMat3(name).get(arrayIdx); }
  227. /**
  228. * Returns a 4x4 matrix assigned with the parameter with the specified name.
  229. *
  230. * Optionally if the parameter is an array you may provide an array index you which to retrieve.
  231. */
  232. Matrix4 getMat4(const String& name, UINT32 arrayIdx = 0) const { return getParamMat4(name).get(arrayIdx); }
  233. /** Returns a texture assigned with the parameter with the specified name. */
  234. TextureType getTexture(const String& name) const { return getParamTexture(name).get(); }
  235. /** Returns a sampler state assigned with the parameter with the specified name. */
  236. SamplerStateType getSamplerState(const String& name) const { return getParamSamplerState(name).get(); }
  237. /**
  238. * Returns a buffer representing a structure assigned to the parameter with the specified name.
  239. *
  240. * Optionally if the parameter is an array you may provide an array index you which to retrieve.
  241. */
  242. MaterialBase::StructData getStructData(const String& name, UINT32 arrayIdx = 0) const
  243. {
  244. TMaterialParamStruct<Core> structParam = getParamStruct(name);
  245. MaterialBase::StructData data(structParam.getElementSize());
  246. structParam.get(data.data.get(), structParam.getElementSize(), arrayIdx);
  247. return data;
  248. }
  249. /**
  250. * Returns a float GPU parameter. This parameter may be used for more efficiently getting/setting GPU parameter
  251. * values than calling Material::get* / Material::set* methods.
  252. *
  253. * @note
  254. * Expected behavior is that you would retrieve this parameter when initially constructing the material, and then
  255. * use it throughout material lifetime to assign and retrieve parameter values.
  256. * @note
  257. * If material shader changes this handle will be invalidated.
  258. */
  259. TMaterialDataParam<float, Core> getParamFloat(const String& name) const
  260. {
  261. TMaterialDataParam<float, Core> gpuParam;
  262. getParam(name, gpuParam);
  263. return gpuParam;
  264. }
  265. /**
  266. * Returns a color GPU parameter. This parameter may be used for more efficiently getting/setting GPU parameter
  267. * values than calling Material::get* / Material::set* methods.
  268. *
  269. * @note
  270. * Expected behavior is that you would retrieve this parameter when initially constructing the material,
  271. * and then use it throughout material lifetime to assign and retrieve parameter values.
  272. * @note
  273. * If material shader changes this handle will be invalidated.
  274. */
  275. TMaterialDataParam<Color, Core> getParamColor(const String& name) const
  276. {
  277. TMaterialDataParam<Color, Core> gpuParam;
  278. getParam(name, gpuParam);
  279. return gpuParam;
  280. }
  281. /**
  282. * Returns a 2D vector GPU parameter. This parameter may be used for more efficiently getting/setting GPU parameter
  283. * values than calling Material::get* / Material::set* methods.
  284. *
  285. * @note
  286. * Expected behavior is that you would retrieve this parameter when initially constructing the material, and then
  287. * use it throughout material lifetime to assign and retrieve parameter values.
  288. * @note
  289. * If material shader changes this handle will be invalidated.
  290. */
  291. TMaterialDataParam<Vector2, Core> getParamVec2(const String& name) const
  292. {
  293. TMaterialDataParam<Vector2, Core> gpuParam;
  294. getParam(name, gpuParam);
  295. return gpuParam;
  296. }
  297. /**
  298. * Returns a 3D vector GPU parameter. This parameter may be used for more efficiently getting/setting GPU parameter
  299. * values than calling Material::get* / Material::set* methods.
  300. *
  301. * @note
  302. * Expected behavior is that you would retrieve this parameter when initially constructing the material, and then
  303. * use it throughout material lifetime to assign and retrieve parameter values.
  304. * @note
  305. * If material shader changes this handle will be invalidated.
  306. */
  307. TMaterialDataParam<Vector3, Core> getParamVec3(const String& name) const
  308. {
  309. TMaterialDataParam<Vector3, Core> gpuParam;
  310. getParam(name, gpuParam);
  311. return gpuParam;
  312. }
  313. /**
  314. * Returns a 4D vector GPU parameter. This parameter may be used for more efficiently getting/setting GPU parameter
  315. * values than calling Material::get* / Material::set* methods.
  316. *
  317. * @note
  318. * Expected behavior is that you would retrieve this parameter when initially constructing the material, and then
  319. * use it throughout material lifetime to assign and retrieve parameter values.
  320. * @note
  321. * If material shader changes this handle will be invalidated.
  322. */
  323. TMaterialDataParam<Vector4, Core> getParamVec4(const String& name) const
  324. {
  325. TMaterialDataParam<Vector4, Core> gpuParam;
  326. getParam(name, gpuParam);
  327. return gpuParam;
  328. }
  329. /**
  330. * Returns a 3x3 matrix GPU parameter. This parameter may be used for more efficiently getting/setting GPU
  331. * parameter values than calling Material::get* / Material::set* methods.
  332. *
  333. * @note
  334. * Expected behavior is that you would retrieve this parameter when initially constructing the material, and then
  335. * use it throughout material lifetime to assign and retrieve parameter values.
  336. * @note
  337. * If material shader changes this handle will be invalidated.
  338. */
  339. TMaterialDataParam<Matrix3, Core> getParamMat3(const String& name) const
  340. {
  341. TMaterialDataParam<Matrix3, Core> gpuParam;
  342. getParam(name, gpuParam);
  343. return gpuParam;
  344. }
  345. /**
  346. * Returns a 4x4 matrix GPU parameter. This parameter may be used for more efficiently getting/setting GPU parameter
  347. * values than calling Material::get* / Material::set* methods.
  348. *
  349. * @note
  350. * Expected behavior is that you would retrieve this parameter when initially constructing the material, and then
  351. * use it throughout material lifetime to assign and retrieve parameter values.
  352. * @note
  353. * If material shader changes this handle will be invalidated.
  354. */
  355. TMaterialDataParam<Matrix4, Core> getParamMat4(const String& name) const
  356. {
  357. TMaterialDataParam<Matrix4, Core> gpuParam;
  358. getParam(name, gpuParam);
  359. return gpuParam;
  360. }
  361. /**
  362. * Returns a structure GPU parameter. This parameter may be used for more efficiently getting/setting GPU parameter
  363. * values than calling Material::get* / Material::set* methods.
  364. *
  365. * @note
  366. * Expected behavior is that you would retrieve this parameter when initially constructing the material, and then
  367. * use it throughout material lifetime to assign and retrieve parameter values.
  368. * @note
  369. * If material shader changes this handle will be invalidated.
  370. */
  371. TMaterialParamStruct<Core> getParamStruct(const String& name) const;
  372. /**
  373. * Returns a texture GPU parameter. This parameter may be used for more efficiently getting/setting GPU parameter
  374. * values than calling Material::get* / Material::set* methods.
  375. *
  376. * @note
  377. * Expected behavior is that you would retrieve this parameter when initially constructing the material, and then
  378. * use it throughout material lifetime to assign and retrieve parameter values.
  379. * @note
  380. * If material shader changes this handle will be invalidated.
  381. */
  382. TMaterialParamTexture<Core> getParamTexture(const String& name) const;
  383. /**
  384. * Returns a GPU parameter for binding a load/store texture. This parameter may be used for more efficiently
  385. * getting/setting GPU parameter values than calling Material::get* / Material::set* methods.
  386. *
  387. * @note
  388. * Expected behavior is that you would retrieve this parameter when initially constructing the material, and then
  389. * use it throughout material lifetime to assign and retrieve parameter values.
  390. * @note
  391. * If material shader changes this handle will be invalidated.
  392. */
  393. TMaterialParamLoadStoreTexture<Core> getParamLoadStoreTexture(const String& name) const;
  394. /**
  395. * Returns a buffer GPU parameter. This parameter may be used for more efficiently getting/setting GPU parameter
  396. * values than calling Material::get* / Material::set* methods.
  397. *
  398. * @note
  399. * Expected behavior is that you would retrieve this parameter when initially constructing the material, and then
  400. * use it throughout material lifetime to assign and retrieve parameter values.
  401. * @note
  402. * If material shader changes this handle will be invalidated.
  403. */
  404. TMaterialParamBuffer<Core> getParamBuffer(const String& name) const;
  405. /**
  406. * Returns a sampler state GPU parameter. This parameter may be used for more efficiently getting/setting GPU
  407. * parameter values than calling Material::get* / Material::set* methods.
  408. *
  409. * @note
  410. * Expected behavior is that you would retrieve this parameter when initially constructing the material, and then
  411. * use it throughout material lifetime to assign and retrieve parameter values.
  412. * @note
  413. * If material shader changes this handle will be invalidated.
  414. */
  415. TMaterialParamSampState<Core> getParamSamplerState(const String& name) const;
  416. /**
  417. * Allows you to retrieve a handle to a parameter that you can then use for quickly setting and retrieving parameter
  418. * data. This allows you to set/get parameter data without all the cost of extra lookups otherwise required.
  419. *
  420. * @note
  421. * All of these handles will be invalidated if material shader ever changes. It is up to the caller to keep track
  422. * of that.
  423. */
  424. template <typename T>
  425. void getParam(const String& name, TMaterialDataParam<T, Core>& output) const;
  426. /**
  427. * @name Internal
  428. * @{
  429. */
  430. /**
  431. * Returns an object containg all of material's parameters. Allows the caller to manipulate the parameters more
  432. * directly.
  433. */
  434. SPtr<MaterialParamsType> _getInternalParams() const { return mParams; }
  435. /** @} */
  436. protected:
  437. /**
  438. * Assigns a value from a raw buffer to the parameter with the specified name. Buffer must be of sizeof(T) *
  439. * numElements size and initialized.
  440. *
  441. * @note Provided parameter must exist, no checking is done.
  442. */
  443. template <typename T>
  444. void setParamValue(const String& name, UINT8* buffer, UINT32 numElements);
  445. /**
  446. * Initializes the material by using the compatible techniques from the currently set shader. Shader must contain
  447. * the techniques that matches the current renderer and render system.
  448. */
  449. void initializeTechniques();
  450. /** Assigns all the default parameters specified in the shader to the material. */
  451. void initDefaultParameters();
  452. /** Throw an exception if no shader is set, or no acceptable technique was found. */
  453. void throwIfNotInitialized() const;
  454. ShaderType mShader;
  455. SPtr<MaterialParamsType> mParams;
  456. Vector<SPtr<TechniqueType>> mTechniques;
  457. };
  458. /** @} */
  459. /** @addtogroup Material-Internal
  460. * @{
  461. */
  462. /** @copydoc MaterialBase */
  463. class BS_CORE_EXPORT MaterialCore : public CoreObjectCore, public TMaterial<true>
  464. {
  465. public:
  466. ~MaterialCore() { }
  467. /** @copydoc Material::setShader */
  468. void setShader(const SPtr<ShaderCore>& shader);
  469. /** Returns any custom data set by the renderer. */
  470. const Any& getRendererData() const { return mRendererData; }
  471. /**
  472. * Sets renderer-specific data on the material. This can by anything the renderer requires, as its not used by the
  473. * material directly.
  474. */
  475. void setRendererData(const Any& data) { mRendererData = data; }
  476. /**
  477. * Returns a version number that increments whenever the material's shader changes. Allows external systems to know
  478. * when they need to rebuild relevant cached data.
  479. */
  480. UINT32 getVersion() const { return mVersion; }
  481. /** Creates a new material with the specified shader. */
  482. static SPtr<MaterialCore> create(const SPtr<ShaderCore>& shader);
  483. private:
  484. friend class Material;
  485. MaterialCore() { }
  486. MaterialCore(const SPtr<ShaderCore>& shader);
  487. MaterialCore(const SPtr<ShaderCore>& shader, const Vector<SPtr<TechniqueCore>>& techniques,
  488. const SPtr<MaterialParamsCore>& materialParams);
  489. /** @copydoc CoreObjectCore::syncToCore */
  490. void syncToCore(const CoreSyncData& data) override;
  491. Any mRendererData;
  492. UINT32 mVersion;
  493. };
  494. /** @} */
  495. /** @addtogroup Material
  496. * @{
  497. */
  498. /** @copydoc MaterialBase */
  499. class BS_CORE_EXPORT Material : public Resource, public TMaterial<false>, public IResourceListener
  500. {
  501. public:
  502. ~Material() { }
  503. /**
  504. * Sets a shader that will be used by the material. Best technique within the provided shader will be used for the
  505. * material.
  506. *
  507. * @note
  508. * Shader must be set before doing any other operations with the material.
  509. * @note
  510. * After setting the shader if you change the implementation of systems that a shader technique is dependent upon
  511. * (render system, renderer, etc), you will need to call this method again on all your Materials to make sure
  512. * technique used is updated.
  513. */
  514. void setShader(const HShader& shader);
  515. /** Retrieves an implementation of a material usable only from the core thread. */
  516. SPtr<MaterialCore> getCore() const;
  517. /** @copydoc CoreObject::initialize */
  518. void initialize() override;
  519. /** Creates a deep copy of the material and returns the new object. */
  520. HMaterial clone();
  521. /**
  522. * Creates a new empty material.
  523. *
  524. * @note Make sure you call Material::setShader before using it.
  525. */
  526. static HMaterial create();
  527. /** Creates a new material with the specified shader. */
  528. static HMaterial create(const HShader& shader);
  529. private:
  530. friend class MaterialManager;
  531. Material();
  532. Material(const HShader& shader);
  533. /** @copydoc CoreObject::createCore */
  534. SPtr<CoreObjectCore> createCore() const override;
  535. /** @copydoc CoreObject::syncToCore */
  536. CoreSyncData syncToCore(FrameAlloc* allocator) override;
  537. /** @copydoc CoreObject::getCoreDependencies */
  538. void getCoreDependencies(Vector<CoreObject*>& dependencies) override;
  539. /** @copydoc CoreObject::markCoreDirty */
  540. void _markCoreDirty() override;
  541. /** @copydoc CoreObject::markDependenciesDirty */
  542. void _markDependenciesDirty() override;
  543. /** @copydoc IResourceListener::markResourcesDirty */
  544. void _markResourcesDirty() override;
  545. /** @copydoc IResourceListener::getListenerResources */
  546. void getListenerResources(Vector<HResource>& resources) override;
  547. /** @copydoc IResourceListener::notifyResourceLoaded */
  548. void notifyResourceLoaded(const HResource& resource) override;
  549. /** @copydoc IResourceListener::notifyResourceChanged */
  550. void notifyResourceChanged(const HResource& resource) override;
  551. /** @copydoc Resource::getResourceDependencies */
  552. void getResourceDependencies(FrameVector<HResource>& dependencies) const override;
  553. /** Performs material initialization when all resources are ready. */
  554. void initializeIfLoaded();
  555. /**
  556. * Uses the provided list of parameters to try to set every parameter in this material. Parameter whose name, type
  557. * or size don't match are ignored and will not be set.
  558. */
  559. void setParams(const SPtr<MaterialParams>& params);
  560. UINT32 mLoadFlags;
  561. /************************************************************************/
  562. /* RTTI */
  563. /************************************************************************/
  564. public:
  565. friend class MaterialRTTI;
  566. static RTTITypeBase* getRTTIStatic();
  567. RTTITypeBase* getRTTI() const override;
  568. };
  569. /** @} */
  570. }