BsMaterial.h 25 KB

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