BsMaterial.h 24 KB

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