BsMaterial.h 27 KB

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