BsMaterial.h 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713
  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 "Resources/BsResource.h"
  6. #include "Resources/BsIResourceListener.h"
  7. #include "Material/BsMaterialParam.h"
  8. #include "Material/BsMaterialParams.h"
  9. #include "Material/BsTechnique.h"
  10. #include "Math/BsVector2.h"
  11. #include "Math/BsVector3.h"
  12. #include "Math/BsVector4.h"
  13. #include "Math/BsMatrix3.h"
  14. #include "Math/BsMatrix4.h"
  15. namespace bs
  16. {
  17. /** @addtogroup Implementation
  18. * @{
  19. */
  20. template<bool Core> struct TGpuParamBlockBufferPtrType { };
  21. template<> struct TGpuParamBlockBufferPtrType<false> { typedef SPtr<GpuParamBlockBuffer> Type; };
  22. template<> struct TGpuParamBlockBufferPtrType<true> { typedef SPtr<ct::GpuParamBlockBuffer> Type; };
  23. template<bool Core> struct TGpuProgramType { };
  24. template<> struct TGpuProgramType<false> { typedef SPtr<GpuProgram> Type; };
  25. template<> struct TGpuProgramType<true> { typedef SPtr<ct::GpuProgram> Type; };
  26. template<bool Core> struct TShaderType {};
  27. template<> struct TShaderType < false > { typedef HShader Type; };
  28. template<> struct TShaderType < true > { typedef SPtr<ct::Shader> Type; };
  29. template<bool Core> struct TGpuParamBlockBufferType {};
  30. template<> struct TGpuParamBlockBufferType < false > { typedef GpuParamBlockBuffer Type; };
  31. template<> struct TGpuParamBlockBufferType < true > { typedef ct::GpuParamBlockBuffer Type; };
  32. template<bool Core> struct TGpuParamsSetType {};
  33. template<> struct TGpuParamsSetType < false > { typedef GpuParamsSet Type; };
  34. template<> struct TGpuParamsSetType < true > { typedef ct::GpuParamsSet Type; };
  35. /** Flags that signal in what way did the Material change. */
  36. enum class MaterialDirtyFlags
  37. {
  38. Normal,
  39. ResourceChanged
  40. };
  41. /**
  42. * Material that controls how objects are rendered. It is represented by a shader and parameters used to set up that
  43. * shader. It provides a simple interface for manipulating the parameters.
  44. */
  45. class BS_CORE_EXPORT MaterialBase
  46. {
  47. public:
  48. /** Data used to describe a structure defined within a shader. */
  49. struct StructData
  50. {
  51. StructData()
  52. :data(nullptr), size(0)
  53. { }
  54. StructData(UINT32 _size)
  55. :size(_size)
  56. {
  57. data = std::shared_ptr<void>(bs_alloc(_size), (void(*)(void*))&bs_free);
  58. }
  59. /**
  60. * Writes the specified data to the internal buffer. Caller must ensure size of the provided buffer is correct.
  61. */
  62. void write(void* _data)
  63. {
  64. memcpy(data.get(), _data, size);
  65. }
  66. SPtr<void> data;
  67. UINT32 size;
  68. };
  69. MaterialBase() { }
  70. virtual ~MaterialBase() { }
  71. /** @name Internal
  72. * @{
  73. */
  74. /** Marks the contents of the sim thread object as dirty, causing it to sync with its core thread counterpart. */
  75. virtual void _markCoreDirty(MaterialDirtyFlags flags = MaterialDirtyFlags::Normal) { }
  76. /** @copydoc CoreObject::markDependenciesDirty */
  77. virtual void _markDependenciesDirty() { }
  78. /** @copydoc IResourceListener::markListenerResourcesDirty */
  79. virtual void _markResourcesDirty() { }
  80. /** @} */
  81. };
  82. /** @copydoc MaterialBase */
  83. template<bool Core>
  84. class BS_CORE_EXPORT TMaterial : public MaterialBase
  85. {
  86. public:
  87. typedef typename TGpuParamTextureType<Core>::Type TextureType;
  88. typedef typename TGpuBufferType<Core>::Type BufferType;
  89. typedef typename TGpuParamSamplerStateType<Core>::Type SamplerStateType;
  90. typedef typename TGpuProgramType<Core>::Type GpuProgramType;
  91. typedef typename TPassType<Core>::Type PassType;
  92. typedef typename TTechniqueType<Core>::Type TechniqueType;
  93. typedef typename TShaderType<Core>::Type ShaderType;
  94. typedef typename TGpuParamsSetType<Core>::Type GpuParamsSetType;
  95. typedef typename TMaterialParamsType<Core>::Type MaterialParamsType;
  96. TMaterial() { }
  97. virtual ~TMaterial() { }
  98. /** Returns the currently active shader. */
  99. BS_SCRIPT_EXPORT(n:Shader,pr:getter)
  100. ShaderType getShader() const { return mShader; }
  101. /** Returns the total number of techniques supported by this material. */
  102. UINT32 getNumTechniques() const { return (UINT32)mTechniques.size(); }
  103. /** Attempts to find a technique with the supported tag. Returns an index of the technique, or -1 if not found. */
  104. UINT32 findTechnique(const StringID& tag) const;
  105. /** Finds the index of the default (primary) technique to use. */
  106. UINT32 getDefaultTechnique() const;
  107. /**
  108. * Returns the number of passes that are used by the technique at the specified index.
  109. *
  110. * @param[in] techniqueIdx Index of the technique to retrieve the number of passes for. 0 is always guaranteed
  111. * to be the default technique.
  112. * @return Number of passes used by the technique.
  113. */
  114. UINT32 getNumPasses(UINT32 techniqueIdx = 0) const;
  115. /**
  116. * Retrieves a specific shader pass from the provided technique.
  117. *
  118. * @param[in] passIdx Sequential index of the pass to retrieve.
  119. * @param[in] techniqueIdx Index of the technique to retrieve the pass for. 0 is always guaranteed to be
  120. * the default technique.
  121. * @return Pass if found, null otherwise.
  122. */
  123. SPtr<PassType> getPass(UINT32 passIdx = 0, UINT32 techniqueIdx = 0) const;
  124. /**
  125. * Creates a set of GpuParams that may be used for binding material parameters to the GPU. The expected behaviour
  126. * is to create a set of GpuParams per-technique once, and then before binding them to the GPU call
  127. * updateParamsSet() to ensure any dirty parameters are transfered from the material to GpuParams. You may also
  128. * use the parameter set to manually modify parameters on a per-program basis, in which case no further updates from
  129. * the material are necessary.
  130. */
  131. SPtr<GpuParamsSetType> createParamsSet(UINT32 techniqueIdx = 0);
  132. /**
  133. * Copies internal material parameter data to the provided params set.
  134. *
  135. * @param[in] paramsSet Parameter set to update.
  136. * @param[in] updateAll Normally the system will track dirty parameters since the last call to this method
  137. * (on a per-set basis), and only update the dirty ones. Set this to true if you want
  138. * to force all parameters to update, regardless of their dirty state.
  139. */
  140. void updateParamsSet(const SPtr<GpuParamsSetType>& paramsSet, bool updateAll = false);
  141. /**
  142. * Assigns a float value to the shader parameter with the specified name.
  143. *
  144. * Optionally if the parameter is an array you may provide an array index to assign the value to.
  145. */
  146. BS_SCRIPT_EXPORT(n:SetFloat)
  147. void setFloat(const String& name, float value, UINT32 arrayIdx = 0) { return getParamFloat(name).set(value, arrayIdx); }
  148. /**
  149. * Assigns a color to the shader parameter with the specified name.
  150. *
  151. * Optionally if the parameter is an array you may provide an array index to assign the value to.
  152. */
  153. BS_SCRIPT_EXPORT(n:SetColor)
  154. void setColor(const String& name, const Color& value, UINT32 arrayIdx = 0) { return getParamColor(name).set(value, arrayIdx); }
  155. /**
  156. * Assigns a 2D vector 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. BS_SCRIPT_EXPORT(n:SetVector2)
  161. void setVec2(const String& name, const Vector2& value, UINT32 arrayIdx = 0) { return getParamVec2(name).set(value, arrayIdx); }
  162. /**
  163. * Assigns a 3D vector to the shader parameter with the specified name.
  164. *
  165. * Optionally if the parameter is an array you may provide an array index to assign the value to.
  166. */
  167. BS_SCRIPT_EXPORT(n:SetVector3)
  168. void setVec3(const String& name, const Vector3& value, UINT32 arrayIdx = 0) { return getParamVec3(name).set(value, arrayIdx); }
  169. /**
  170. * Assigns a 4D vector to the shader parameter with the specified name.
  171. *
  172. * Optionally if the parameter is an array you may provide an array index to assign the value to.
  173. */
  174. BS_SCRIPT_EXPORT(n:SetVector4)
  175. void setVec4(const String& name, const Vector4& value, UINT32 arrayIdx = 0) { return getParamVec4(name).set(value, arrayIdx); }
  176. /**
  177. * Assigns a 3x3 matrix to the shader parameter with the specified name.
  178. *
  179. * Optionally if the parameter is an array you may provide an array index to assign the value to.
  180. */
  181. BS_SCRIPT_EXPORT(n:SetMatrix3)
  182. void setMat3(const String& name, const Matrix3& value, UINT32 arrayIdx = 0) { return getParamMat3(name).set(value, arrayIdx); }
  183. /**
  184. * Assigns a 4x4 matrix 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. BS_SCRIPT_EXPORT(n:SetMatrix4)
  189. void setMat4(const String& name, const Matrix4& value, UINT32 arrayIdx = 0) { return getParamMat4(name).set(value, arrayIdx); }
  190. /**
  191. * Assigns a structure to the shader parameter with the specified name.
  192. *
  193. * Structure is provided as a raw buffer and caller must ensure structure in buffer matches what the shader expects.
  194. *
  195. * Optionally if the parameter is an array you may provide an array index to assign the value to.
  196. */
  197. void setStructData(const String& name, void* value, UINT32 size, UINT32 arrayIdx = 0) { return getParamStruct(name).set(value, size, arrayIdx); }
  198. /** Assigns a texture to the shader parameter with the specified name. */
  199. void setTexture(const String& name, const TextureType& value, const TextureSurface& surface = TextureSurface::COMPLETE)
  200. {
  201. return getParamTexture(name).set(value, surface);
  202. }
  203. /** Assigns a texture to be used for random load/store operations to the shader parameter with the specified name. */
  204. void setLoadStoreTexture(const String& name, const TextureType& value, const TextureSurface& surface)
  205. {
  206. return getParamLoadStoreTexture(name).set(value, surface);
  207. }
  208. /** Assigns a buffer to the shader parameter with the specified name. */
  209. void setBuffer(const String& name, const BufferType& value) { return getParamBuffer(name).set(value); }
  210. /** Assigns a sampler state to the shader parameter with the specified name. */
  211. void setSamplerState(const String& name, const SamplerStateType& value) { return getParamSamplerState(name).set(value); }
  212. /**
  213. * Returns a float value assigned with the parameter with the specified name.
  214. *
  215. * Optionally if the parameter is an array you may provide an array index you which to retrieve.
  216. */
  217. BS_SCRIPT_EXPORT(n:GetFloat)
  218. float getFloat(const String& name, UINT32 arrayIdx = 0) const { return getParamFloat(name).get(arrayIdx); }
  219. /**
  220. * Returns a color assigned with the parameter with the specified name.
  221. *
  222. * Optionally if the parameter is an array you may provide an array index you which to retrieve.
  223. */
  224. BS_SCRIPT_EXPORT(n:GetColor)
  225. Color getColor(const String& name, UINT32 arrayIdx = 0) const { return getParamColor(name).get(arrayIdx); }
  226. /**
  227. * Returns a 2D vector assigned with the parameter with the specified name.
  228. *
  229. * Optionally if the parameter is an array you may provide an array index you which to retrieve.
  230. */
  231. BS_SCRIPT_EXPORT(n:GetVector2)
  232. Vector2 getVec2(const String& name, UINT32 arrayIdx = 0) const { return getParamVec2(name).get(arrayIdx); }
  233. /**
  234. * Returns a 3D vector assigned with the parameter with the specified name.
  235. *
  236. * Optionally if the parameter is an array you may provide an array index you which to retrieve.
  237. */
  238. BS_SCRIPT_EXPORT(n:GetVector3)
  239. Vector3 getVec3(const String& name, UINT32 arrayIdx = 0) const { return getParamVec3(name).get(arrayIdx); }
  240. /**
  241. * Returns a 4D vector 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. BS_SCRIPT_EXPORT(n:GetVector4)
  246. Vector4 getVec4(const String& name, UINT32 arrayIdx = 0) const { return getParamVec4(name).get(arrayIdx); }
  247. /**
  248. * Returns a 3x3 matrix assigned with the parameter with the specified name.
  249. *
  250. * Optionally if the parameter is an array you may provide an array index you which to retrieve.
  251. */
  252. BS_SCRIPT_EXPORT(n:GetMatrix3)
  253. Matrix3 getMat3(const String& name, UINT32 arrayIdx = 0) const { return getParamMat3(name).get(arrayIdx); }
  254. /**
  255. * Returns a 4x4 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. BS_SCRIPT_EXPORT(n:GetMatrix4)
  260. Matrix4 getMat4(const String& name, UINT32 arrayIdx = 0) const { return getParamMat4(name).get(arrayIdx); }
  261. /** Returns a texture assigned with the parameter with the specified name. */
  262. TextureType getTexture(const String& name) const { return getParamTexture(name).get(); }
  263. /** Returns a sampler state assigned with the parameter with the specified name. */
  264. SamplerStateType getSamplerState(const String& name) const { return getParamSamplerState(name).get(); }
  265. /**
  266. * Returns a buffer representing a structure assigned to the parameter with the specified name.
  267. *
  268. * Optionally if the parameter is an array you may provide an array index you which to retrieve.
  269. */
  270. MaterialBase::StructData getStructData(const String& name, UINT32 arrayIdx = 0) const
  271. {
  272. TMaterialParamStruct<Core> structParam = getParamStruct(name);
  273. MaterialBase::StructData data(structParam.getElementSize());
  274. structParam.get(data.data.get(), structParam.getElementSize(), arrayIdx);
  275. return data;
  276. }
  277. /**
  278. * Returns a float GPU parameter. This parameter may be used for more efficiently getting/setting GPU parameter
  279. * values than calling Material::get* / Material::set* methods.
  280. *
  281. * @note
  282. * Expected behavior is that you would retrieve this parameter when initially constructing the material, and then
  283. * use it throughout material lifetime to assign and retrieve parameter values.
  284. * @note
  285. * If material shader changes this handle will be invalidated.
  286. */
  287. TMaterialDataParam<float, Core> getParamFloat(const String& name) const
  288. {
  289. TMaterialDataParam<float, Core> gpuParam;
  290. getParam(name, gpuParam);
  291. return gpuParam;
  292. }
  293. /**
  294. * Returns a color GPU parameter. This parameter may be used for more efficiently getting/setting GPU parameter
  295. * values than calling Material::get* / Material::set* methods.
  296. *
  297. * @note
  298. * Expected behavior is that you would retrieve this parameter when initially constructing the material,
  299. * and then use it throughout material lifetime to assign and retrieve parameter values.
  300. * @note
  301. * If material shader changes this handle will be invalidated.
  302. */
  303. TMaterialDataParam<Color, Core> getParamColor(const String& name) const
  304. {
  305. TMaterialDataParam<Color, Core> gpuParam;
  306. getParam(name, gpuParam);
  307. return gpuParam;
  308. }
  309. /**
  310. * Returns a 2D vector GPU parameter. This parameter may be used for more efficiently getting/setting GPU parameter
  311. * values than calling Material::get* / Material::set* methods.
  312. *
  313. * @note
  314. * Expected behavior is that you would retrieve this parameter when initially constructing the material, and then
  315. * use it throughout material lifetime to assign and retrieve parameter values.
  316. * @note
  317. * If material shader changes this handle will be invalidated.
  318. */
  319. TMaterialDataParam<Vector2, Core> getParamVec2(const String& name) const
  320. {
  321. TMaterialDataParam<Vector2, Core> gpuParam;
  322. getParam(name, gpuParam);
  323. return gpuParam;
  324. }
  325. /**
  326. * Returns a 3D vector GPU parameter. This parameter may be used for more efficiently getting/setting GPU parameter
  327. * values than calling Material::get* / Material::set* methods.
  328. *
  329. * @note
  330. * Expected behavior is that you would retrieve this parameter when initially constructing the material, and then
  331. * use it throughout material lifetime to assign and retrieve parameter values.
  332. * @note
  333. * If material shader changes this handle will be invalidated.
  334. */
  335. TMaterialDataParam<Vector3, Core> getParamVec3(const String& name) const
  336. {
  337. TMaterialDataParam<Vector3, Core> gpuParam;
  338. getParam(name, gpuParam);
  339. return gpuParam;
  340. }
  341. /**
  342. * Returns a 4D vector GPU parameter. This parameter may be used for more efficiently getting/setting GPU parameter
  343. * values than calling Material::get* / Material::set* methods.
  344. *
  345. * @note
  346. * Expected behavior is that you would retrieve this parameter when initially constructing the material, and then
  347. * use it throughout material lifetime to assign and retrieve parameter values.
  348. * @note
  349. * If material shader changes this handle will be invalidated.
  350. */
  351. TMaterialDataParam<Vector4, Core> getParamVec4(const String& name) const
  352. {
  353. TMaterialDataParam<Vector4, Core> gpuParam;
  354. getParam(name, gpuParam);
  355. return gpuParam;
  356. }
  357. /**
  358. * Returns a 3x3 matrix GPU parameter. This parameter may be used for more efficiently getting/setting GPU
  359. * parameter values than calling Material::get* / Material::set* methods.
  360. *
  361. * @note
  362. * Expected behavior is that you would retrieve this parameter when initially constructing the material, and then
  363. * use it throughout material lifetime to assign and retrieve parameter values.
  364. * @note
  365. * If material shader changes this handle will be invalidated.
  366. */
  367. TMaterialDataParam<Matrix3, Core> getParamMat3(const String& name) const
  368. {
  369. TMaterialDataParam<Matrix3, Core> gpuParam;
  370. getParam(name, gpuParam);
  371. return gpuParam;
  372. }
  373. /**
  374. * Returns a 4x4 matrix GPU parameter. This parameter may be used for more efficiently getting/setting GPU parameter
  375. * values than calling Material::get* / Material::set* methods.
  376. *
  377. * @note
  378. * Expected behavior is that you would retrieve this parameter when initially constructing the material, and then
  379. * use it throughout material lifetime to assign and retrieve parameter values.
  380. * @note
  381. * If material shader changes this handle will be invalidated.
  382. */
  383. TMaterialDataParam<Matrix4, Core> getParamMat4(const String& name) const
  384. {
  385. TMaterialDataParam<Matrix4, Core> gpuParam;
  386. getParam(name, gpuParam);
  387. return gpuParam;
  388. }
  389. /**
  390. * Returns a structure GPU parameter. This parameter may be used for more efficiently getting/setting GPU parameter
  391. * values than calling Material::get* / Material::set* methods.
  392. *
  393. * @note
  394. * Expected behavior is that you would retrieve this parameter when initially constructing the material, and then
  395. * use it throughout material lifetime to assign and retrieve parameter values.
  396. * @note
  397. * If material shader changes this handle will be invalidated.
  398. */
  399. TMaterialParamStruct<Core> getParamStruct(const String& name) const;
  400. /**
  401. * Returns a texture GPU parameter. This parameter may be used for more efficiently getting/setting GPU parameter
  402. * values than calling Material::get* / Material::set* methods.
  403. *
  404. * @note
  405. * Expected behavior is that you would retrieve this parameter when initially constructing the material, and then
  406. * use it throughout material lifetime to assign and retrieve parameter values.
  407. * @note
  408. * If material shader changes this handle will be invalidated.
  409. */
  410. TMaterialParamTexture<Core> getParamTexture(const String& name) const;
  411. /**
  412. * Returns a GPU parameter for binding a load/store texture. This parameter may be used for more efficiently
  413. * getting/setting GPU parameter values than calling Material::get* / Material::set* methods.
  414. *
  415. * @note
  416. * Expected behavior is that you would retrieve this parameter when initially constructing the material, and then
  417. * use it throughout material lifetime to assign and retrieve parameter values.
  418. * @note
  419. * If material shader changes this handle will be invalidated.
  420. */
  421. TMaterialParamLoadStoreTexture<Core> getParamLoadStoreTexture(const String& name) const;
  422. /**
  423. * Returns a buffer GPU parameter. This parameter may be used for more efficiently getting/setting GPU parameter
  424. * values than calling Material::get* / Material::set* methods.
  425. *
  426. * @note
  427. * Expected behavior is that you would retrieve this parameter when initially constructing the material, and then
  428. * use it throughout material lifetime to assign and retrieve parameter values.
  429. * @note
  430. * If material shader changes this handle will be invalidated.
  431. */
  432. TMaterialParamBuffer<Core> getParamBuffer(const String& name) const;
  433. /**
  434. * Returns a sampler state GPU parameter. This parameter may be used for more efficiently getting/setting GPU
  435. * parameter values than calling Material::get* / Material::set* methods.
  436. *
  437. * @note
  438. * Expected behavior is that you would retrieve this parameter when initially constructing the material, and then
  439. * use it throughout material lifetime to assign and retrieve parameter values.
  440. * @note
  441. * If material shader changes this handle will be invalidated.
  442. */
  443. TMaterialParamSampState<Core> getParamSamplerState(const String& name) const;
  444. /**
  445. * Allows you to retrieve a handle to a parameter that you can then use for quickly setting and retrieving parameter
  446. * data. This allows you to set/get parameter data without all the cost of extra lookups otherwise required.
  447. *
  448. * @note
  449. * All of these handles will be invalidated if material shader ever changes. It is up to the caller to keep track
  450. * of that.
  451. */
  452. template <typename T>
  453. void getParam(const String& name, TMaterialDataParam<T, Core>& output) const;
  454. /**
  455. * @name Internal
  456. * @{
  457. */
  458. /**
  459. * Returns an object containg all of material's parameters. Allows the caller to manipulate the parameters more
  460. * directly.
  461. */
  462. SPtr<MaterialParamsType> _getInternalParams() const { return mParams; }
  463. /** @} */
  464. protected:
  465. /**
  466. * Assigns a value from a raw buffer to the parameter with the specified name. Buffer must be of sizeof(T) *
  467. * numElements size and initialized.
  468. *
  469. * @note Provided parameter must exist, no checking is done.
  470. */
  471. template <typename T>
  472. void setParamValue(const String& name, UINT8* buffer, UINT32 numElements);
  473. /**
  474. * Initializes the material by using the compatible techniques from the currently set shader. Shader must contain
  475. * the techniques that matches the current renderer and render system.
  476. */
  477. void initializeTechniques();
  478. /** Assigns all the default parameters specified in the shader to the material. */
  479. void initDefaultParameters();
  480. /** Throw an exception if no shader is set, or no acceptable technique was found. */
  481. void throwIfNotInitialized() const;
  482. ShaderType mShader;
  483. SPtr<MaterialParamsType> mParams;
  484. Vector<SPtr<TechniqueType>> mTechniques;
  485. };
  486. /** @} */
  487. /** @addtogroup Material
  488. * @{
  489. */
  490. /** @copydoc MaterialBase */
  491. class BS_CORE_EXPORT BS_SCRIPT_EXPORT(m:Rendering) Material: public Resource, public TMaterial<false>, public IResourceListener
  492. {
  493. public:
  494. ~Material() { }
  495. /**
  496. * Sets a shader that will be used by the material. Best technique within the provided shader will be used for the
  497. * material.
  498. *
  499. * @note
  500. * Shader must be set before doing any other operations with the material.
  501. * @note
  502. * After setting the shader if you change the implementation of systems that a shader technique is dependent upon
  503. * (render system, renderer, etc), you will need to call this method again on all your Materials to make sure
  504. * technique used is updated.
  505. */
  506. BS_SCRIPT_EXPORT(n:Shader,pr:setter)
  507. void setShader(const HShader& shader);
  508. /** Retrieves an implementation of a material usable only from the core thread. */
  509. SPtr<ct::Material> getCore() const;
  510. /** @copydoc CoreObject::initialize */
  511. void initialize() override;
  512. /** Creates a deep copy of the material and returns the new object. */
  513. BS_SCRIPT_EXPORT(n:Clone)
  514. HMaterial clone();
  515. /**
  516. * Creates a new empty material.
  517. *
  518. * @note Make sure you call Material::setShader before using it.
  519. */
  520. BS_SCRIPT_EXPORT(ec:Material)
  521. static HMaterial create();
  522. /** Creates a new material with the specified shader. */
  523. BS_SCRIPT_EXPORT(ec:Material)
  524. static HMaterial create(const HShader& shader);
  525. /** @name Internal
  526. * @{
  527. */
  528. /**
  529. * Marks the core data as dirty. This causes the syncToCore() method to trigger the next time objects are synced
  530. * between core and sim threads.
  531. */
  532. void _markCoreDirty(MaterialDirtyFlags flags = MaterialDirtyFlags::Normal) override;
  533. /** @copydoc CoreObject::markDependenciesDirty */
  534. void _markDependenciesDirty() override;
  535. /** @copydoc IResourceListener::markListenerResourcesDirty */
  536. void _markResourcesDirty() override;
  537. /** @} */
  538. private:
  539. friend class MaterialManager;
  540. Material();
  541. Material(const HShader& shader);
  542. /** @copydoc CoreObject::createCore */
  543. SPtr<ct::CoreObject> createCore() const override;
  544. /** @copydoc CoreObject::syncToCore */
  545. CoreSyncData syncToCore(FrameAlloc* allocator) override;
  546. /** @copydoc CoreObject::getCoreDependencies */
  547. void getCoreDependencies(Vector<CoreObject*>& dependencies) override;
  548. /** @copydoc IResourceListener::getListenerResources */
  549. void getListenerResources(Vector<HResource>& resources) override;
  550. /** @copydoc IResourceListener::notifyResourceLoaded */
  551. void notifyResourceLoaded(const HResource& resource) override;
  552. /** @copydoc IResourceListener::notifyResourceChanged */
  553. void notifyResourceChanged(const HResource& resource) override;
  554. /** @copydoc Resource::getResourceDependencies */
  555. void getResourceDependencies(FrameVector<HResource>& dependencies) const override;
  556. /** Performs material initialization when all resources are ready. */
  557. void initializeIfLoaded();
  558. /**
  559. * Uses the provided list of parameters to try to set every parameter in this material. Parameter whose name, type
  560. * or size don't match are ignored and will not be set.
  561. */
  562. void setParams(const SPtr<MaterialParams>& params);
  563. UINT32 mLoadFlags;
  564. /************************************************************************/
  565. /* RTTI */
  566. /************************************************************************/
  567. public:
  568. friend class MaterialRTTI;
  569. static RTTITypeBase* getRTTIStatic();
  570. RTTITypeBase* getRTTI() const override;
  571. };
  572. /** @} */
  573. namespace ct
  574. {
  575. /** @addtogroup Material-Internal
  576. * @{
  577. */
  578. /** @copydoc MaterialBase */
  579. class BS_CORE_EXPORT Material : public CoreObject, public TMaterial<true>
  580. {
  581. public:
  582. ~Material() { }
  583. /** @copydoc bs::Material::setShader */
  584. void setShader(const SPtr<Shader>& shader);
  585. /** Creates a new material with the specified shader. */
  586. static SPtr<Material> create(const SPtr<Shader>& shader);
  587. private:
  588. friend class bs::Material;
  589. Material() { }
  590. Material(const SPtr<Shader>& shader);
  591. Material(const SPtr<Shader>& shader, const Vector<SPtr<Technique>>& techniques,
  592. const SPtr<MaterialParams>& materialParams);
  593. /** @copydoc CoreObject::syncToCore */
  594. void syncToCore(const CoreSyncData& data) override;
  595. };
  596. /** @} */
  597. }
  598. }