BsMaterial.h 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735
  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. class BS_CORE_EXPORT PassParameters : public TPassParameters<false> {};
  58. class BS_CORE_EXPORT PassParametersCore : public TPassParameters<true> {};
  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. template<bool Core> struct TPassParamsType {};
  135. template<> struct TPassParamsType < false > { typedef PassParameters Type; };
  136. template<> struct TPassParamsType < true > { typedef PassParametersCore Type; };
  137. typedef typename TGpuParamsPtrType<Core>::Type GpuParamsType;
  138. typedef typename TGpuParamTextureType<Core>::Type TextureType;
  139. typedef typename TGpuParamSamplerStateType<Core>::Type SamplerStateType;
  140. typedef typename TGpuParamBlockBufferPtrType<Core>::Type ParamBlockPtrType;
  141. typedef typename TGpuParamBlockBufferType<Core>::Type ParamBlockType;
  142. typedef typename TGpuProgramType<Core>::Type GpuProgramType;
  143. typedef typename TPassType<Core>::Type PassType;
  144. typedef typename TTechniqueType<Core>::Type TechniqueType;
  145. typedef typename TShaderType<Core>::Type ShaderType;
  146. typedef typename TPassParamsType<Core>::Type PassParamsType;
  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) { return getParamColor(name).set(value, arrayIdx); }
  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<PassParamsType> 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 Assigns a value from a raw buffer to the parameter with the specified name.
  475. * Buffer must be of sizeof(T) * numElements size and initialized.
  476. *
  477. * @note Provided parameter must exist, no checking is done.
  478. */
  479. template <typename T>
  480. void setParamValue(const String& name, UINT8* buffer, UINT32 numElements);
  481. /**
  482. * @brief Initializes the material by using the best technique from the currently set shader. Shader
  483. * must contain the technique that matches the current renderer and render system.
  484. */
  485. void initBestTechnique();
  486. /**
  487. * @brief Assigns all the default parameters specified in the shader to the material.
  488. */
  489. void initDefaultParameters();
  490. /**
  491. * @brief Throw an exception if no shader is set, or no acceptable
  492. * technique was found.
  493. */
  494. void throwIfNotInitialized() const;
  495. Vector<SPtr<PassParamsType>> mParametersPerPass;
  496. ShaderType mShader;
  497. TechniqueType mBestTechnique;
  498. };
  499. /**
  500. * @copydoc MaterialBase
  501. */
  502. class BS_CORE_EXPORT MaterialCore : public CoreObjectCore, public TMaterial<true>
  503. {
  504. public:
  505. ~MaterialCore() { }
  506. /**
  507. * @copydoc Material::setShader
  508. */
  509. void setShader(const SPtr<ShaderCore>& shader);
  510. /**
  511. * @brief Creates a new material with the specified shader.
  512. */
  513. static SPtr<MaterialCore> create(const SPtr<ShaderCore>& shader);
  514. private:
  515. friend class Material;
  516. MaterialCore() { }
  517. MaterialCore(const SPtr<ShaderCore>& shader);
  518. MaterialCore(const SPtr<ShaderCore>& shader, const SPtr<TechniqueCore>& bestTechnique,
  519. const Set<String>& validShareableParamBlocks, const Map<String, String>& validParams,
  520. const Vector<SPtr<PassParametersCore>>& passParams);
  521. /**
  522. * @copydoc CoreObjectCore::syncToCore
  523. */
  524. void syncToCore(const CoreSyncData& data) override;
  525. };
  526. /**
  527. * @copydoc MaterialBase
  528. */
  529. class BS_CORE_EXPORT Material : public Resource, public TMaterial<false>, public IResourceListener
  530. {
  531. public:
  532. ~Material() { }
  533. /**
  534. * @brief Sets a shader that will be used by the material. Best technique within the
  535. * provided shader will be used for the material.
  536. *
  537. * @note Shader must be set before doing any other operations with the material.
  538. *
  539. * After setting the shader if change any systems that a shader technique is
  540. * dependent upon (render system, renderer, etc), you will need to call this
  541. * method again on all your Materials to make sure technique used is updated.
  542. */
  543. void setShader(const HShader& shader);
  544. /**
  545. * @brief Retrieves an implementation of a material usable only from the
  546. * core thread.
  547. */
  548. SPtr<MaterialCore> getCore() const;
  549. /**
  550. * @copydoc CoreObject::initialize
  551. */
  552. void initialize() override;
  553. /**
  554. * @brief Creates a deep copy of the material and returns the new object.
  555. */
  556. HMaterial Material::clone();
  557. /**
  558. * @brief Creates a new empty material.
  559. *
  560. * @note Make sure you call Material::setShader before using it.
  561. */
  562. static HMaterial create();
  563. /**
  564. * @brief Creates a new material with the specified shader.
  565. */
  566. static HMaterial create(const HShader& shader);
  567. private:
  568. friend class MaterialManager;
  569. Material();
  570. Material(const HShader& shader);
  571. /**
  572. * @copydoc CoreObject::createCore
  573. */
  574. SPtr<CoreObjectCore> createCore() const override;
  575. /**
  576. * @copydoc CoreObject::syncToCore
  577. */
  578. CoreSyncData syncToCore(FrameAlloc* allocator) override;
  579. /**
  580. * @copydoc CoreObject::getCoreDependencies
  581. */
  582. void getCoreDependencies(Vector<SPtr<CoreObject>>& dependencies) override;
  583. /**
  584. * @copydoc CoreObject::markCoreDirty
  585. */
  586. void _markCoreDirty() override;
  587. /**
  588. * @copydoc IResourceListener::markResourcesDirty
  589. */
  590. void _markResourcesDirty() override;
  591. /**
  592. * @copydoc IResourceListener::getListenerResources
  593. */
  594. void getListenerResources(Vector<HResource>& resources) override;
  595. /**
  596. * @copydoc IResourceListener::notifyResourceLoaded
  597. */
  598. void notifyResourceLoaded(const HResource& resource) override;
  599. /**
  600. * @copydoc IResourceListener::notifyResourceDestroyed
  601. */
  602. void notifyResourceDestroyed(const HResource& resource) override;
  603. /**
  604. * @copydoc IResourceListener::notifyResourceChanged
  605. */
  606. void notifyResourceChanged(const HResource& resource) override;
  607. /**
  608. * @copydoc Resource::getResourceDependencies
  609. */
  610. void getResourceDependencies(FrameVector<HResource>& dependencies) const override;
  611. /**
  612. * @brief Performs material initialization when all resources are ready.
  613. */
  614. void initializeIfLoaded();
  615. UINT32 mLoadFlags;
  616. /************************************************************************/
  617. /* RTTI */
  618. /************************************************************************/
  619. public:
  620. friend class MaterialRTTI;
  621. static RTTITypeBase* getRTTIStatic();
  622. virtual RTTITypeBase* getRTTI() const override;
  623. };
  624. }