BsMaterial.h 25 KB

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