BsMaterial.h 25 KB

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