BsMaterial.h 26 KB

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