BsMaterial.h 27 KB

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