BsMaterial.h 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #pragma once
  4. #include "BsCorePrerequisites.h"
  5. #include "BsResource.h"
  6. #include "BsIResourceListener.h"
  7. #include "BsMaterialParam.h"
  8. #include "BsMaterialParams.h"
  9. #include "BsTechnique.h"
  10. #include "BsVector2.h"
  11. #include "BsVector3.h"
  12. #include "BsVector4.h"
  13. #include "BsMatrix3.h"
  14. #include "BsMatrix4.h"
  15. namespace BansheeEngine
  16. {
  17. /** @addtogroup Implementation
  18. * @{
  19. */
  20. /** Helper class containing parameters for all types of GPU programs used in a pass. */
  21. template<bool Core>
  22. class TPassParameters
  23. {
  24. public:
  25. typedef typename TGpuParamsPtrType<Core>::Type GpuParamsType;
  26. /**
  27. * Returns a set of GPU parameters based on an index.
  28. *
  29. * @note Useful when needing to iterate over all sets of GPU parameters.
  30. */
  31. GpuParamsType& getParamByIdx(UINT32 idx)
  32. {
  33. GpuParamsType* paramArray[] = { &mVertParams, &mFragParams, &mGeomParams, &mHullParams, &mDomainParams, &mComputeParams };
  34. return *paramArray[idx];
  35. }
  36. /**
  37. * Sets GPU parameters based on an index.
  38. *
  39. * @note Useful when needing to iterate over all sets of GPU parameters.
  40. */
  41. void setParamByIdx(UINT32 idx, const GpuParamsType& params)
  42. {
  43. GpuParamsType* paramArray[] = { &mVertParams, &mFragParams, &mGeomParams, &mHullParams, &mDomainParams, &mComputeParams };
  44. (*paramArray[idx]) = params;
  45. }
  46. GpuParamsType mVertParams;
  47. GpuParamsType mFragParams;
  48. GpuParamsType mGeomParams;
  49. GpuParamsType mHullParams;
  50. GpuParamsType mDomainParams;
  51. GpuParamsType mComputeParams;
  52. };
  53. /** Contains sim thread pass parameters for each shader stage. */
  54. class BS_CORE_EXPORT PassParameters : public TPassParameters<false>
  55. {
  56. public:
  57. static const UINT32 NUM_PARAMS;
  58. };
  59. /** Contains core thread pass parameters for each shader stage. */
  60. class BS_CORE_EXPORT PassParametersCore : public TPassParameters<true>
  61. {
  62. public:
  63. static const UINT32 NUM_PARAMS;
  64. };
  65. template<bool Core> struct TGpuParamBlockBufferPtrType { };
  66. template<> struct TGpuParamBlockBufferPtrType<false> { typedef SPtr<GpuParamBlockBuffer> Type; };
  67. template<> struct TGpuParamBlockBufferPtrType<true> { typedef SPtr<GpuParamBlockBufferCore> Type; };
  68. template<bool Core> struct TGpuProgramType { };
  69. template<> struct TGpuProgramType<false> { typedef SPtr<GpuProgram> Type; };
  70. template<> struct TGpuProgramType<true> { typedef SPtr<GpuProgramCore> Type; };
  71. template<bool Core> struct TShaderType {};
  72. template<> struct TShaderType < false > { typedef HShader Type; };
  73. template<> struct TShaderType < true > { typedef SPtr<ShaderCore> Type; };
  74. template<bool Core> struct TGpuParamBlockBufferType {};
  75. template<> struct TGpuParamBlockBufferType < false > { typedef GpuParamBlockBuffer Type; };
  76. template<> struct TGpuParamBlockBufferType < true > { typedef GpuParamBlockBufferCore Type; };
  77. template<bool Core> struct TPassParamsType {};
  78. template<> struct TPassParamsType < false > { typedef PassParameters Type; };
  79. template<> struct TPassParamsType < true > { typedef PassParametersCore Type; };
  80. /**
  81. * Material that controls how objects are rendered. It is represented by a shader and parameters used to set up that
  82. * shader. It provides a simple interface for manipulating the parameters.
  83. */
  84. class BS_CORE_EXPORT MaterialBase
  85. {
  86. public:
  87. /** Data used to describe a structure defined within a shader. */
  88. struct StructData
  89. {
  90. StructData()
  91. :data(nullptr), size(0)
  92. { }
  93. StructData(UINT32 _size)
  94. :size(_size)
  95. {
  96. data = std::shared_ptr<void>(bs_alloc(_size), (void(*)(void*))&bs_free);
  97. }
  98. /**
  99. * Writes the specified data to the internal buffer. Caller must ensure size of the provided buffer is correct.
  100. */
  101. void write(void* _data)
  102. {
  103. memcpy(data.get(), _data, size);
  104. }
  105. SPtr<void> data;
  106. UINT32 size;
  107. };
  108. MaterialBase() { }
  109. virtual ~MaterialBase() { }
  110. protected:
  111. /** Marks the contents of the sim thread object as dirty, causing it to sync with its core thread counterpart. */
  112. virtual void _markCoreDirty() { }
  113. /** @copydoc CoreObject::markDependenciesDirty */
  114. virtual void _markDependenciesDirty() { }
  115. /** @copydoc IResourceListener::markListenerResourcesDirty */
  116. virtual void _markResourcesDirty() { }
  117. /** Returns all GPU parameter descriptions in the specified technique. */
  118. static Vector<SPtr<GpuParamDesc>> getAllParamDescs(const SPtr<Technique>& technique);
  119. /** Returns all GPU parameter descriptions in the specified technique. */
  120. static Vector<SPtr<GpuParamDesc>> getAllParamDescs(const SPtr<TechniqueCore>& technique);
  121. Set<String> mValidShareableParamBlocks;
  122. Map<String, String> mValidParams; // Also maps Shader param name -> gpu variable name
  123. };
  124. /** @copydoc MaterialBase */
  125. template<bool Core>
  126. class BS_CORE_EXPORT TMaterial : public MaterialBase
  127. {
  128. public:
  129. typedef typename TGpuParamsPtrType<Core>::Type GpuParamsType;
  130. typedef typename TGpuParamTextureType<Core>::Type TextureType;
  131. typedef typename TGpuBufferType<Core>::Type BufferType;
  132. typedef typename TGpuParamSamplerStateType<Core>::Type SamplerStateType;
  133. typedef typename TGpuParamBlockBufferPtrType<Core>::Type ParamBlockPtrType;
  134. typedef typename TGpuParamBlockBufferType<Core>::Type ParamBlockType;
  135. typedef typename TGpuProgramType<Core>::Type GpuProgramType;
  136. typedef typename TPassType<Core>::Type PassType;
  137. typedef typename TTechniqueType<Core>::Type TechniqueType;
  138. typedef typename TShaderType<Core>::Type ShaderType;
  139. typedef typename TPassParamsType<Core>::Type PassParamsType;
  140. typedef typename TMaterialParamsType<Core>::Type MaterialParamsType;
  141. TMaterial() { }
  142. virtual ~TMaterial() { }
  143. /** Returns the currently active shader. */
  144. ShaderType getShader() const { return mShader; }
  145. /** Returns the number of passes that are used by the shader used in the material. */
  146. UINT32 getNumPasses() const;
  147. /** Retrieves a specific shader pass. */
  148. SPtr<PassType> getPass(UINT32 passIdx) const;
  149. /**
  150. * Assigns a float value to the shader parameter with the specified name.
  151. *
  152. * Optionally if the parameter is an array you may provide an array index to assign the value to.
  153. */
  154. void setFloat(const String& name, float value, UINT32 arrayIdx = 0) { return getParamFloat(name).set(value, arrayIdx); }
  155. /**
  156. * Assigns a color to the shader parameter with the specified name.
  157. *
  158. * Optionally if the parameter is an array you may provide an array index to assign the value to.
  159. */
  160. void setColor(const String& name, const Color& value, UINT32 arrayIdx = 0) { return getParamColor(name).set(value, arrayIdx); }
  161. /**
  162. * Assigns a 2D vector 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 setVec2(const String& name, const Vector2& value, UINT32 arrayIdx = 0) { return getParamVec2(name).set(value, arrayIdx); }
  167. /**
  168. * Assigns a 3D vector 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 setVec3(const String& name, const Vector3& value, UINT32 arrayIdx = 0) { return getParamVec3(name).set(value, arrayIdx); }
  173. /**
  174. * Assigns a 4D 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 setVec4(const String& name, const Vector4& value, UINT32 arrayIdx = 0) { return getParamVec4(name).set(value, arrayIdx); }
  179. /**
  180. * Assigns a 3x3 matrix 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 setMat3(const String& name, const Matrix3& value, UINT32 arrayIdx = 0) { return getParamMat3(name).set(value, arrayIdx); }
  185. /**
  186. * Assigns a 4x4 matrix 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 setMat4(const String& name, const Matrix4& value, UINT32 arrayIdx = 0) { return getParamMat4(name).set(value, arrayIdx); }
  191. /**
  192. * Assigns a structure to the shader parameter with the specified name.
  193. *
  194. * Structure is provided as a raw buffer and caller must ensure structure in buffer matches what the shader expects.
  195. *
  196. * Optionally if the parameter is an array you may provide an array index to assign the value to.
  197. */
  198. void setStructData(const String& name, void* value, UINT32 size, UINT32 arrayIdx = 0) { return getParamStruct(name).set(value, size, arrayIdx); }
  199. /** Assigns a texture to the shader parameter with the specified name. */
  200. void setTexture(const String& name, const TextureType& value) { return getParamTexture(name).set(value); }
  201. /** Assigns a texture to be used for random load/store operations to the shader parameter with the specified name. */
  202. void setLoadStoreTexture(const String& name, const TextureType& value, const TextureSurface& surface)
  203. {
  204. return getParamLoadStoreTexture(name).set(value, surface);
  205. }
  206. /** Assigns a buffer to the shader parameter with the specified name. */
  207. void setBuffer(const String& name, const BufferType& value) { return getParamBuffer(name).set(value); }
  208. /** Assigns a sampler state to the shader parameter with the specified name. */
  209. void setSamplerState(const String& name, const SamplerStateType& value) { return getParamSamplerState(name).set(value); }
  210. /**
  211. * Returns a float value assigned with the parameter with the specified name.
  212. *
  213. * Optionally if the parameter is an array you may provide an array index you which to retrieve.
  214. */
  215. float getFloat(const String& name, UINT32 arrayIdx = 0) const { return getParamFloat(name).get(arrayIdx); }
  216. /**
  217. * Returns a color assigned with the parameter with the specified name.
  218. *
  219. * Optionally if the parameter is an array you may provide an array index you which to retrieve.
  220. */
  221. Color getColor(const String& name, UINT32 arrayIdx = 0) const { return getParamColor(name).get(arrayIdx); }
  222. /**
  223. * Returns a 2D vector assigned with the parameter with the specified name.
  224. *
  225. * Optionally if the parameter is an array you may provide an array index you which to retrieve.
  226. */
  227. Vector2 getVec2(const String& name, UINT32 arrayIdx = 0) const { return getParamVec2(name).get(arrayIdx); }
  228. /**
  229. * Returns a 3D vector assigned with the parameter with the specified name.
  230. *
  231. * Optionally if the parameter is an array you may provide an array index you which to retrieve.
  232. */
  233. Vector3 getVec3(const String& name, UINT32 arrayIdx = 0) const { return getParamVec3(name).get(arrayIdx); }
  234. /**
  235. * Returns a 4D vector 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. Vector4 getVec4(const String& name, UINT32 arrayIdx = 0) const { return getParamVec4(name).get(arrayIdx); }
  240. /**
  241. * Returns a 3x3 matrix 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. Matrix3 getMat3(const String& name, UINT32 arrayIdx = 0) const { return getParamMat3(name).get(arrayIdx); }
  246. /**
  247. * Returns a 4x4 matrix 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. Matrix4 getMat4(const String& name, UINT32 arrayIdx = 0) const { return getParamMat4(name).get(arrayIdx); }
  252. /** Returns a texture assigned with the parameter with the specified name. */
  253. TextureType getTexture(const String& name) const { return getParamTexture(name).get(); }
  254. /** Returns a sampler state assigned with the parameter with the specified name. */
  255. SamplerStateType getSamplerState(const String& name) const { return getParamSamplerState(name).get(); }
  256. /**
  257. * Returns a buffer representing a structure assigned to the parameter with the specified name.
  258. *
  259. * Optionally if the parameter is an array you may provide an array index you which to retrieve.
  260. */
  261. MaterialBase::StructData getStructData(const String& name, UINT32 arrayIdx = 0) const
  262. {
  263. TMaterialParamStruct<Core> structParam = getParamStruct(name);
  264. MaterialBase::StructData data(structParam.getElementSize());
  265. structParam.get(data.data.get(), structParam.getElementSize(), arrayIdx);
  266. return data;
  267. }
  268. /**
  269. * Returns a float GPU parameter. This parameter may be used for more efficiently getting/setting GPU parameter
  270. * values than calling Material::get* / Material::set* methods.
  271. *
  272. * @note
  273. * Expected behavior is that you would retrieve this parameter when initially constructing the material, and then
  274. * use it throughout material lifetime to assign and retrieve parameter values.
  275. * @note
  276. * If material shader changes this handle will be invalidated.
  277. */
  278. TMaterialDataParam<float, Core> getParamFloat(const String& name) const
  279. {
  280. TMaterialDataParam<float, Core> gpuParam;
  281. getParam(name, gpuParam);
  282. return gpuParam;
  283. }
  284. /**
  285. * Returns a color GPU parameter. This parameter may be used for more efficiently getting/setting GPU parameter
  286. * values than calling Material::get* / Material::set* methods.
  287. *
  288. * @note
  289. * Expected behavior is that you would retrieve this parameter when initially constructing the material,
  290. * and then use it throughout material lifetime to assign and retrieve parameter values.
  291. * @note
  292. * If material shader changes this handle will be invalidated.
  293. */
  294. TMaterialDataParam<Color, Core> getParamColor(const String& name) const
  295. {
  296. TMaterialDataParam<Color, Core> gpuParam;
  297. getParam(name, gpuParam);
  298. return gpuParam;
  299. }
  300. /**
  301. * Returns a 2D vector GPU parameter. This parameter may be used for more efficiently getting/setting GPU parameter
  302. * values than calling Material::get* / Material::set* methods.
  303. *
  304. * @note
  305. * Expected behavior is that you would retrieve this parameter when initially constructing the material, and then
  306. * use it throughout material lifetime to assign and retrieve parameter values.
  307. * @note
  308. * If material shader changes this handle will be invalidated.
  309. */
  310. TMaterialDataParam<Vector2, Core> getParamVec2(const String& name) const
  311. {
  312. TMaterialDataParam<Vector2, Core> gpuParam;
  313. getParam(name, gpuParam);
  314. return gpuParam;
  315. }
  316. /**
  317. * Returns a 3D vector GPU parameter. This parameter may be used for more efficiently getting/setting GPU parameter
  318. * values than calling Material::get* / Material::set* methods.
  319. *
  320. * @note
  321. * Expected behavior is that you would retrieve this parameter when initially constructing the material, and then
  322. * use it throughout material lifetime to assign and retrieve parameter values.
  323. * @note
  324. * If material shader changes this handle will be invalidated.
  325. */
  326. TMaterialDataParam<Vector3, Core> getParamVec3(const String& name) const
  327. {
  328. TMaterialDataParam<Vector3, Core> gpuParam;
  329. getParam(name, gpuParam);
  330. return gpuParam;
  331. }
  332. /**
  333. * Returns a 4D vector GPU parameter. This parameter may be used for more efficiently getting/setting GPU parameter
  334. * values than calling Material::get* / Material::set* methods.
  335. *
  336. * @note
  337. * Expected behavior is that you would retrieve this parameter when initially constructing the material, and then
  338. * use it throughout material lifetime to assign and retrieve parameter values.
  339. * @note
  340. * If material shader changes this handle will be invalidated.
  341. */
  342. TMaterialDataParam<Vector4, Core> getParamVec4(const String& name) const
  343. {
  344. TMaterialDataParam<Vector4, Core> gpuParam;
  345. getParam(name, gpuParam);
  346. return gpuParam;
  347. }
  348. /**
  349. * Returns a 3x3 matrix GPU parameter. This parameter may be used for more efficiently getting/setting GPU
  350. * parameter values than calling Material::get* / Material::set* methods.
  351. *
  352. * @note
  353. * Expected behavior is that you would retrieve this parameter when initially constructing the material, and then
  354. * use it throughout material lifetime to assign and retrieve parameter values.
  355. * @note
  356. * If material shader changes this handle will be invalidated.
  357. */
  358. TMaterialDataParam<Matrix3, Core> getParamMat3(const String& name) const
  359. {
  360. TMaterialDataParam<Matrix3, Core> gpuParam;
  361. getParam(name, gpuParam);
  362. return gpuParam;
  363. }
  364. /**
  365. * Returns a 4x4 matrix GPU parameter. This parameter may be used for more efficiently getting/setting GPU parameter
  366. * values than calling Material::get* / Material::set* methods.
  367. *
  368. * @note
  369. * Expected behavior is that you would retrieve this parameter when initially constructing the material, and then
  370. * use it throughout material lifetime to assign and retrieve parameter values.
  371. * @note
  372. * If material shader changes this handle will be invalidated.
  373. */
  374. TMaterialDataParam<Matrix4, Core> getParamMat4(const String& name) const
  375. {
  376. TMaterialDataParam<Matrix4, Core> gpuParam;
  377. getParam(name, gpuParam);
  378. return gpuParam;
  379. }
  380. /**
  381. * Returns a structure GPU parameter. This parameter may be used for more efficiently getting/setting GPU parameter
  382. * values than calling Material::get* / Material::set* methods.
  383. *
  384. * @note
  385. * Expected behavior is that you would retrieve this parameter when initially constructing the material, and then
  386. * use it throughout material lifetime to assign and retrieve parameter values.
  387. * @note
  388. * If material shader changes this handle will be invalidated.
  389. */
  390. TMaterialParamStruct<Core> getParamStruct(const String& name) const;
  391. /**
  392. * Returns a texture GPU parameter. This parameter may be used for more efficiently getting/setting GPU parameter
  393. * values than calling Material::get* / Material::set* methods.
  394. *
  395. * @note
  396. * Expected behavior is that you would retrieve this parameter when initially constructing the material, and then
  397. * use it throughout material lifetime to assign and retrieve parameter values.
  398. * @note
  399. * If material shader changes this handle will be invalidated.
  400. */
  401. TMaterialParamTexture<Core> getParamTexture(const String& name) const;
  402. /**
  403. * Returns a GPU parameter for binding a load/store texture. This parameter may be used for more efficiently
  404. * getting/setting GPU parameter values than calling Material::get* / Material::set* methods.
  405. *
  406. * @note
  407. * Expected behavior is that you would retrieve this parameter when initially constructing the material, and then
  408. * use it throughout material lifetime to assign and retrieve parameter values.
  409. * @note
  410. * If material shader changes this handle will be invalidated.
  411. */
  412. TMaterialParamLoadStoreTexture<Core> getParamLoadStoreTexture(const String& name) const;
  413. /**
  414. * Returns a buffer GPU parameter. This parameter may be used for more efficiently getting/setting GPU parameter
  415. * values than calling Material::get* / Material::set* methods.
  416. *
  417. * @note
  418. * Expected behavior is that you would retrieve this parameter when initially constructing the material, and then
  419. * use it throughout material lifetime to assign and retrieve parameter values.
  420. * @note
  421. * If material shader changes this handle will be invalidated.
  422. */
  423. TMaterialParamBuffer<Core> getParamBuffer(const String& name) const;
  424. /**
  425. * Returns a sampler state GPU parameter. This parameter may be used for more efficiently getting/setting GPU
  426. * parameter values than calling Material::get* / Material::set* methods.
  427. *
  428. * @note
  429. * Expected behavior is that you would retrieve this parameter when initially constructing the material, and then
  430. * use it throughout material lifetime to assign and retrieve parameter values.
  431. * @note
  432. * If material shader changes this handle will be invalidated.
  433. */
  434. TMaterialParamSampState<Core> getParamSamplerState(const String& name) const;
  435. /** Returns a set of parameters for all GPU programs in the specified shader pass. */
  436. SPtr<PassParamsType> getPassParameters(UINT32 passIdx) const { return mParametersPerPass[passIdx]; }
  437. /**
  438. * Returns a set of GPU parameters for an individual GPU program of the specified pass.
  439. *
  440. * @param[in] passIdx Pass in which to look the GPU program for in.
  441. * @param[in] type Type of the program to retrieve parameters for.
  442. * @return GPU parameters object that can be used for setting parameters of a GPU program
  443. * individually. Returns null if program or pass doesn't exist.
  444. */
  445. GpuParamsType getGpuParams(UINT32 passIdx, BansheeEngine::GpuProgramType type);
  446. /**
  447. * Assign a parameter block buffer with the specified name.
  448. *
  449. * @note
  450. * Parameter block buffers can be used as quick way of setting multiple parameters on a material at once, or
  451. * potentially sharing parameters between multiple materials. This reduces driver overhead as the parameters
  452. * in the buffers need only be set once and then reused multiple times.
  453. */
  454. void setParamBlockBuffer(const String& name, const ParamBlockPtrType& paramBlock);
  455. /**
  456. * Allows you to retrieve a handle to a parameter that you can then use for quickly setting and retrieving parameter
  457. * data. This allows you to set/get parameter data without all the cost of extra lookups otherwise required.
  458. *
  459. * @note
  460. * All of these handles will be invalidated if material shader ever changes. It is up to the caller to keep track
  461. * of that.
  462. */
  463. template <typename T>
  464. void getParam(const String& name, TMaterialDataParam<T, Core>& output) const;
  465. protected:
  466. /**
  467. * Assigns a value from a raw buffer to the parameter with the specified name. Buffer must be of sizeof(T) *
  468. * numElements size and initialized.
  469. *
  470. * @note Provided parameter must exist, no checking is done.
  471. */
  472. template <typename T>
  473. void setParamValue(const String& name, UINT8* buffer, UINT32 numElements);
  474. /**
  475. * Initializes the material by using the best technique from the currently set shader. Shader must contain the
  476. * technique that matches the current renderer and render system.
  477. */
  478. void initBestTechnique();
  479. /** Assigns all the default parameters specified in the shader to the material. */
  480. void initDefaultParameters();
  481. /** Throw an exception if no shader is set, or no acceptable technique was found. */
  482. void throwIfNotInitialized() const;
  483. ShaderType mShader;
  484. SPtr<MaterialParamsType> mParams;
  485. Vector<SPtr<PassParamsType>> mParametersPerPass;
  486. SPtr<TechniqueType> mBestTechnique;
  487. };
  488. /** @} */
  489. /** @addtogroup Material-Internal
  490. * @{
  491. */
  492. /** @copydoc MaterialBase */
  493. class BS_CORE_EXPORT MaterialCore : public CoreObjectCore, public TMaterial<true>
  494. {
  495. public:
  496. ~MaterialCore() { }
  497. /** @copydoc Material::setShader */
  498. void setShader(const SPtr<ShaderCore>& shader);
  499. /** Creates a new material with the specified shader. */
  500. static SPtr<MaterialCore> create(const SPtr<ShaderCore>& shader);
  501. private:
  502. friend class Material;
  503. MaterialCore() { }
  504. MaterialCore(const SPtr<ShaderCore>& shader);
  505. MaterialCore(const SPtr<ShaderCore>& shader, const SPtr<TechniqueCore>& bestTechnique,
  506. const Set<String>& validShareableParamBlocks, const Map<String, String>& validParams,
  507. const SPtr<MaterialParamsCore>& materialParams,
  508. const Vector<SPtr<PassParametersCore>>& passParams);
  509. /** @copydoc CoreObjectCore::syncToCore */
  510. void syncToCore(const CoreSyncData& data) override;
  511. };
  512. /** @} */
  513. /** @addtogroup Material
  514. * @{
  515. */
  516. /** @copydoc MaterialBase */
  517. class BS_CORE_EXPORT Material : public Resource, public TMaterial<false>, public IResourceListener
  518. {
  519. public:
  520. ~Material() { }
  521. /**
  522. * Sets a shader that will be used by the material. Best technique within the provided shader will be used for the
  523. * material.
  524. *
  525. * @note
  526. * Shader must be set before doing any other operations with the material.
  527. * @note
  528. * After setting the shader if you change the implementation of systems that a shader technique is dependent upon
  529. * (render system, renderer, etc), you will need to call this method again on all your Materials to make sure
  530. * technique used is updated.
  531. */
  532. void setShader(const HShader& shader);
  533. /** Retrieves an implementation of a material usable only from the core thread. */
  534. SPtr<MaterialCore> getCore() const;
  535. /** @copydoc CoreObject::initialize */
  536. void initialize() override;
  537. /** Creates a deep copy of the material and returns the new object. */
  538. HMaterial clone();
  539. /**
  540. * Creates a new empty material.
  541. *
  542. * @note Make sure you call Material::setShader before using it.
  543. */
  544. static HMaterial create();
  545. /** Creates a new material with the specified shader. */
  546. static HMaterial create(const HShader& shader);
  547. private:
  548. friend class MaterialManager;
  549. Material();
  550. Material(const HShader& shader);
  551. /** @copydoc CoreObject::createCore */
  552. SPtr<CoreObjectCore> createCore() const override;
  553. /** @copydoc CoreObject::syncToCore */
  554. CoreSyncData syncToCore(FrameAlloc* allocator) override;
  555. /** @copydoc CoreObject::getCoreDependencies */
  556. void getCoreDependencies(Vector<CoreObject*>& dependencies) override;
  557. /** @copydoc CoreObject::markCoreDirty */
  558. void _markCoreDirty() override;
  559. /** @copydoc CoreObject::markDependenciesDirty */
  560. void _markDependenciesDirty() override;
  561. /** @copydoc IResourceListener::markResourcesDirty */
  562. void _markResourcesDirty() override;
  563. /** @copydoc IResourceListener::getListenerResources */
  564. void getListenerResources(Vector<HResource>& resources) override;
  565. /** @copydoc IResourceListener::notifyResourceLoaded */
  566. void notifyResourceLoaded(const HResource& resource) override;
  567. /** @copydoc IResourceListener::notifyResourceChanged */
  568. void notifyResourceChanged(const HResource& resource) override;
  569. /** @copydoc Resource::getResourceDependencies */
  570. void getResourceDependencies(FrameVector<HResource>& dependencies) const override;
  571. /** Performs material initialization when all resources are ready. */
  572. void initializeIfLoaded();
  573. /**
  574. * Uses the provided list of parameters to try to set every parameter in this material. Parameter whose name, type
  575. * or size don't match are ignored and will not be set.
  576. */
  577. void setParams(const SPtr<MaterialParams>& params);
  578. UINT32 mLoadFlags;
  579. /************************************************************************/
  580. /* RTTI */
  581. /************************************************************************/
  582. public:
  583. friend class MaterialRTTI;
  584. static RTTITypeBase* getRTTIStatic();
  585. RTTITypeBase* getRTTI() const override;
  586. };
  587. /** @} */
  588. }