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