BsMaterial.h 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723
  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 = 1 << 0,
  39. ResourceChanged = 2 << 1
  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. /** Attempts to find a technique matching the provided variation. Returns an index of the technique, or -1 if not found. */
  106. UINT32 findTechnique(const ShaderVariation& variation) const;
  107. /** Finds the index of the default (primary) technique to use. */
  108. UINT32 getDefaultTechnique() const;
  109. /**
  110. * Returns the number of passes that are used by the technique at the specified index.
  111. *
  112. * @param[in] techniqueIdx Index of the technique to retrieve the number of passes for. 0 is always guaranteed
  113. * to be the default technique.
  114. * @return Number of passes used by the technique.
  115. */
  116. UINT32 getNumPasses(UINT32 techniqueIdx = 0) const;
  117. /**
  118. * Retrieves a specific shader pass from the provided technique.
  119. *
  120. * @param[in] passIdx Sequential index of the pass to retrieve.
  121. * @param[in] techniqueIdx Index of the technique to retrieve the pass for. 0 is always guaranteed to be
  122. * the default technique.
  123. * @return Pass if found, null otherwise.
  124. */
  125. SPtr<PassType> getPass(UINT32 passIdx = 0, UINT32 techniqueIdx = 0) const;
  126. /**
  127. * Creates a set of GpuParams that may be used for binding material parameters to the GPU. The expected behaviour
  128. * is to create a set of GpuParams per-technique once, and then before binding them to the GPU call
  129. * updateParamsSet() to ensure any dirty parameters are transfered from the material to GpuParams. You may also
  130. * use the parameter set to manually modify parameters on a per-program basis, in which case no further updates from
  131. * the material are necessary.
  132. */
  133. SPtr<GpuParamsSetType> createParamsSet(UINT32 techniqueIdx = 0);
  134. /**
  135. * Copies internal material parameter data to the provided params set.
  136. *
  137. * @param[in] paramsSet Parameter set to update.
  138. * @param[in] updateAll Normally the system will track dirty parameters since the last call to this method
  139. * (on a per-set basis), and only update the dirty ones. Set this to true if you want
  140. * to force all parameters to update, regardless of their dirty state.
  141. */
  142. void updateParamsSet(const SPtr<GpuParamsSetType>& paramsSet, bool updateAll = false);
  143. /**
  144. * Assigns a float value to the shader parameter with the specified name.
  145. *
  146. * Optionally if the parameter is an array you may provide an array index to assign the value to.
  147. */
  148. BS_SCRIPT_EXPORT(n:SetFloat)
  149. void setFloat(const String& name, float value, UINT32 arrayIdx = 0) { return getParamFloat(name).set(value, arrayIdx); }
  150. /**
  151. * Assigns a color to the shader parameter with the specified name.
  152. *
  153. * Optionally if the parameter is an array you may provide an array index to assign the value to.
  154. */
  155. BS_SCRIPT_EXPORT(n:SetColor)
  156. void setColor(const String& name, const Color& value, UINT32 arrayIdx = 0) { return getParamColor(name).set(value, arrayIdx); }
  157. /**
  158. * Assigns a 2D vector to the shader parameter with the specified name.
  159. *
  160. * Optionally if the parameter is an array you may provide an array index to assign the value to.
  161. */
  162. BS_SCRIPT_EXPORT(n:SetVector2)
  163. void setVec2(const String& name, const Vector2& value, UINT32 arrayIdx = 0) { return getParamVec2(name).set(value, arrayIdx); }
  164. /**
  165. * Assigns a 3D vector to the shader parameter with the specified name.
  166. *
  167. * Optionally if the parameter is an array you may provide an array index to assign the value to.
  168. */
  169. BS_SCRIPT_EXPORT(n:SetVector3)
  170. void setVec3(const String& name, const Vector3& value, UINT32 arrayIdx = 0) { return getParamVec3(name).set(value, arrayIdx); }
  171. /**
  172. * Assigns a 4D vector to the shader parameter with the specified name.
  173. *
  174. * Optionally if the parameter is an array you may provide an array index to assign the value to.
  175. */
  176. BS_SCRIPT_EXPORT(n:SetVector4)
  177. void setVec4(const String& name, const Vector4& value, UINT32 arrayIdx = 0) { return getParamVec4(name).set(value, arrayIdx); }
  178. /**
  179. * Assigns a 3x3 matrix to the shader parameter with the specified name.
  180. *
  181. * Optionally if the parameter is an array you may provide an array index to assign the value to.
  182. */
  183. BS_SCRIPT_EXPORT(n:SetMatrix3)
  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. BS_SCRIPT_EXPORT(n:SetMatrix4)
  191. void setMat4(const String& name, const Matrix4& value, UINT32 arrayIdx = 0) { return getParamMat4(name).set(value, arrayIdx); }
  192. /**
  193. * Assigns a structure to the shader parameter with the specified name.
  194. *
  195. * Structure is provided as a raw buffer and caller must ensure structure in buffer matches what the shader expects.
  196. *
  197. * Optionally if the parameter is an array you may provide an array index to assign the value to.
  198. */
  199. void setStructData(const String& name, void* value, UINT32 size, UINT32 arrayIdx = 0) { return getParamStruct(name).set(value, size, arrayIdx); }
  200. /** Assigns a texture to the shader parameter with the specified name. */
  201. void setTexture(const String& name, const TextureType& value, const TextureSurface& surface = TextureSurface::COMPLETE)
  202. {
  203. return getParamTexture(name).set(value, surface);
  204. }
  205. /** Assigns a texture to be used for random load/store operations to the shader parameter with the specified name. */
  206. void setLoadStoreTexture(const String& name, const TextureType& value, const TextureSurface& surface)
  207. {
  208. return getParamLoadStoreTexture(name).set(value, surface);
  209. }
  210. /** Assigns a buffer to the shader parameter with the specified name. */
  211. void setBuffer(const String& name, const BufferType& value) { return getParamBuffer(name).set(value); }
  212. /** Assigns a sampler state to the shader parameter with the specified name. */
  213. void setSamplerState(const String& name, const SamplerStateType& value) { return getParamSamplerState(name).set(value); }
  214. /**
  215. * Returns a float value assigned with the parameter with the specified name.
  216. *
  217. * Optionally if the parameter is an array you may provide an array index you which to retrieve.
  218. */
  219. BS_SCRIPT_EXPORT(n:GetFloat)
  220. float getFloat(const String& name, UINT32 arrayIdx = 0) const { return getParamFloat(name).get(arrayIdx); }
  221. /**
  222. * Returns a color assigned with the parameter with the specified name.
  223. *
  224. * Optionally if the parameter is an array you may provide an array index you which to retrieve.
  225. */
  226. BS_SCRIPT_EXPORT(n:GetColor)
  227. Color getColor(const String& name, UINT32 arrayIdx = 0) const { return getParamColor(name).get(arrayIdx); }
  228. /**
  229. * Returns a 2D 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. BS_SCRIPT_EXPORT(n:GetVector2)
  234. Vector2 getVec2(const String& name, UINT32 arrayIdx = 0) const { return getParamVec2(name).get(arrayIdx); }
  235. /**
  236. * Returns a 3D vector assigned with the parameter with the specified name.
  237. *
  238. * Optionally if the parameter is an array you may provide an array index you which to retrieve.
  239. */
  240. BS_SCRIPT_EXPORT(n:GetVector3)
  241. Vector3 getVec3(const String& name, UINT32 arrayIdx = 0) const { return getParamVec3(name).get(arrayIdx); }
  242. /**
  243. * Returns a 4D vector assigned with the parameter with the specified name.
  244. *
  245. * Optionally if the parameter is an array you may provide an array index you which to retrieve.
  246. */
  247. BS_SCRIPT_EXPORT(n:GetVector4)
  248. Vector4 getVec4(const String& name, UINT32 arrayIdx = 0) const { return getParamVec4(name).get(arrayIdx); }
  249. /**
  250. * Returns a 3x3 matrix assigned with the parameter with the specified name.
  251. *
  252. * Optionally if the parameter is an array you may provide an array index you which to retrieve.
  253. */
  254. BS_SCRIPT_EXPORT(n:GetMatrix3)
  255. Matrix3 getMat3(const String& name, UINT32 arrayIdx = 0) const { return getParamMat3(name).get(arrayIdx); }
  256. /**
  257. * Returns a 4x4 matrix assigned with 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. BS_SCRIPT_EXPORT(n:GetMatrix4)
  262. Matrix4 getMat4(const String& name, UINT32 arrayIdx = 0) const { return getParamMat4(name).get(arrayIdx); }
  263. /** Returns a texture assigned with the parameter with the specified name. */
  264. TextureType getTexture(const String& name) const { return getParamTexture(name).get(); }
  265. /** Returns a sampler state assigned with the parameter with the specified name. */
  266. SamplerStateType getSamplerState(const String& name) const { return getParamSamplerState(name).get(); }
  267. /**
  268. * Returns a buffer representing a structure assigned to the parameter with the specified name.
  269. *
  270. * Optionally if the parameter is an array you may provide an array index you which to retrieve.
  271. */
  272. MaterialBase::StructData getStructData(const String& name, UINT32 arrayIdx = 0) const
  273. {
  274. TMaterialParamStruct<Core> structParam = getParamStruct(name);
  275. MaterialBase::StructData data(structParam.getElementSize());
  276. structParam.get(data.data.get(), structParam.getElementSize(), arrayIdx);
  277. return data;
  278. }
  279. /**
  280. * Returns a float GPU parameter. This parameter may be used for more efficiently getting/setting GPU parameter
  281. * values than calling Material::get* / Material::set* methods.
  282. *
  283. * @note
  284. * Expected behavior is that you would retrieve this parameter when initially constructing the material, and then
  285. * use it throughout material lifetime to assign and retrieve parameter values.
  286. * @note
  287. * If material shader changes this handle will be invalidated.
  288. */
  289. TMaterialDataParam<float, Core> getParamFloat(const String& name) const
  290. {
  291. TMaterialDataParam<float, Core> gpuParam;
  292. getParam(name, gpuParam);
  293. return gpuParam;
  294. }
  295. /**
  296. * Returns a color GPU parameter. This parameter may be used for more efficiently getting/setting GPU parameter
  297. * values than calling Material::get* / Material::set* methods.
  298. *
  299. * @note
  300. * Expected behavior is that you would retrieve this parameter when initially constructing the material,
  301. * and then use it throughout material lifetime to assign and retrieve parameter values.
  302. * @note
  303. * If material shader changes this handle will be invalidated.
  304. */
  305. TMaterialDataParam<Color, Core> getParamColor(const String& name) const
  306. {
  307. TMaterialDataParam<Color, Core> gpuParam;
  308. getParam(name, gpuParam);
  309. return gpuParam;
  310. }
  311. /**
  312. * Returns a 2D vector GPU parameter. This parameter may be used for more efficiently getting/setting GPU parameter
  313. * values than calling Material::get* / Material::set* methods.
  314. *
  315. * @note
  316. * Expected behavior is that you would retrieve this parameter when initially constructing the material, and then
  317. * use it throughout material lifetime to assign and retrieve parameter values.
  318. * @note
  319. * If material shader changes this handle will be invalidated.
  320. */
  321. TMaterialDataParam<Vector2, Core> getParamVec2(const String& name) const
  322. {
  323. TMaterialDataParam<Vector2, Core> gpuParam;
  324. getParam(name, gpuParam);
  325. return gpuParam;
  326. }
  327. /**
  328. * Returns a 3D vector GPU parameter. This parameter may be used for more efficiently getting/setting GPU parameter
  329. * values than calling Material::get* / Material::set* methods.
  330. *
  331. * @note
  332. * Expected behavior is that you would retrieve this parameter when initially constructing the material, and then
  333. * use it throughout material lifetime to assign and retrieve parameter values.
  334. * @note
  335. * If material shader changes this handle will be invalidated.
  336. */
  337. TMaterialDataParam<Vector3, Core> getParamVec3(const String& name) const
  338. {
  339. TMaterialDataParam<Vector3, Core> gpuParam;
  340. getParam(name, gpuParam);
  341. return gpuParam;
  342. }
  343. /**
  344. * Returns a 4D vector GPU parameter. This parameter may be used for more efficiently getting/setting GPU parameter
  345. * values than calling Material::get* / Material::set* methods.
  346. *
  347. * @note
  348. * Expected behavior is that you would retrieve this parameter when initially constructing the material, and then
  349. * use it throughout material lifetime to assign and retrieve parameter values.
  350. * @note
  351. * If material shader changes this handle will be invalidated.
  352. */
  353. TMaterialDataParam<Vector4, Core> getParamVec4(const String& name) const
  354. {
  355. TMaterialDataParam<Vector4, Core> gpuParam;
  356. getParam(name, gpuParam);
  357. return gpuParam;
  358. }
  359. /**
  360. * Returns a 3x3 matrix GPU parameter. This parameter may be used for more efficiently getting/setting GPU
  361. * parameter values than calling Material::get* / Material::set* methods.
  362. *
  363. * @note
  364. * Expected behavior is that you would retrieve this parameter when initially constructing the material, and then
  365. * use it throughout material lifetime to assign and retrieve parameter values.
  366. * @note
  367. * If material shader changes this handle will be invalidated.
  368. */
  369. TMaterialDataParam<Matrix3, Core> getParamMat3(const String& name) const
  370. {
  371. TMaterialDataParam<Matrix3, Core> gpuParam;
  372. getParam(name, gpuParam);
  373. return gpuParam;
  374. }
  375. /**
  376. * Returns a 4x4 matrix GPU parameter. This parameter may be used for more efficiently getting/setting GPU parameter
  377. * values than calling Material::get* / Material::set* methods.
  378. *
  379. * @note
  380. * Expected behavior is that you would retrieve this parameter when initially constructing the material, and then
  381. * use it throughout material lifetime to assign and retrieve parameter values.
  382. * @note
  383. * If material shader changes this handle will be invalidated.
  384. */
  385. TMaterialDataParam<Matrix4, Core> getParamMat4(const String& name) const
  386. {
  387. TMaterialDataParam<Matrix4, Core> gpuParam;
  388. getParam(name, gpuParam);
  389. return gpuParam;
  390. }
  391. /**
  392. * Returns a structure 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. TMaterialParamStruct<Core> getParamStruct(const String& name) const;
  402. /**
  403. * Returns a texture GPU parameter. This parameter may be used for more efficiently getting/setting GPU parameter
  404. * 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. TMaterialParamTexture<Core> getParamTexture(const String& name) const;
  413. /**
  414. * Returns a GPU parameter for binding a load/store texture. This parameter may be used for more efficiently
  415. * getting/setting GPU parameter 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. TMaterialParamLoadStoreTexture<Core> getParamLoadStoreTexture(const String& name) const;
  424. /**
  425. * Returns a buffer GPU parameter. This parameter may be used for more efficiently getting/setting GPU parameter
  426. * 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. TMaterialParamBuffer<Core> getParamBuffer(const String& name) const;
  435. /**
  436. * Returns a sampler state GPU parameter. This parameter may be used for more efficiently getting/setting GPU
  437. * parameter values than calling Material::get* / Material::set* methods.
  438. *
  439. * @note
  440. * Expected behavior is that you would retrieve this parameter when initially constructing the material, and then
  441. * use it throughout material lifetime to assign and retrieve parameter values.
  442. * @note
  443. * If material shader changes this handle will be invalidated.
  444. */
  445. TMaterialParamSampState<Core> getParamSamplerState(const String& name) const;
  446. /**
  447. * Allows you to retrieve a handle to a parameter that you can then use for quickly setting and retrieving parameter
  448. * data. This allows you to set/get parameter data without all the cost of extra lookups otherwise required.
  449. *
  450. * @note
  451. * All of these handles will be invalidated if material shader ever changes. It is up to the caller to keep track
  452. * of that.
  453. */
  454. template <typename T>
  455. void getParam(const String& name, TMaterialDataParam<T, Core>& output) const;
  456. /**
  457. * @name Internal
  458. * @{
  459. */
  460. /**
  461. * Returns an object containg all of material's parameters. Allows the caller to manipulate the parameters more
  462. * directly.
  463. */
  464. SPtr<MaterialParamsType> _getInternalParams() const { return mParams; }
  465. /** @} */
  466. protected:
  467. /**
  468. * Assigns a value from a raw buffer to the parameter with the specified name. Buffer must be of sizeof(T) *
  469. * numElements size and initialized.
  470. *
  471. * @note Provided parameter must exist, no checking is done.
  472. */
  473. template <typename T>
  474. void setParamValue(const String& name, UINT8* buffer, UINT32 numElements);
  475. /**
  476. * Initializes the material by using the compatible techniques from the currently set shader. Shader must contain
  477. * the techniques that matches the current renderer and render system. Only the techniques matching the provided
  478. * variation will be used, unless @p allVariations is set to true, in which case @p variation parameter is ignored.
  479. */
  480. void initializeTechniques(bool allVariations = true, const ShaderVariation& variation = ShaderVariation());
  481. /** Assigns all the default parameters specified in the shader to the material. */
  482. void initDefaultParameters();
  483. /** Throw an exception if no shader is set, or no acceptable technique was found. */
  484. void throwIfNotInitialized() const;
  485. ShaderType mShader;
  486. SPtr<MaterialParamsType> mParams;
  487. Vector<SPtr<TechniqueType>> mTechniques;
  488. };
  489. /** @} */
  490. /** @addtogroup Material
  491. * @{
  492. */
  493. /** @copydoc MaterialBase */
  494. class BS_CORE_EXPORT BS_SCRIPT_EXPORT(m:Rendering) Material: public Resource, public TMaterial<false>, public IResourceListener
  495. {
  496. public:
  497. ~Material() { }
  498. /**
  499. * Sets a shader that will be used by the material. Material will be initialized using all compatible techniques
  500. * from the shader. Shader must be set before doing any other operations with the material.
  501. */
  502. BS_SCRIPT_EXPORT(n:Shader,pr:setter)
  503. void setShader(const HShader& shader);
  504. /** Retrieves an implementation of a material usable only from the core thread. */
  505. SPtr<ct::Material> getCore() const;
  506. /** @copydoc CoreObject::initialize */
  507. void initialize() override;
  508. /** Creates a deep copy of the material and returns the new object. */
  509. BS_SCRIPT_EXPORT(n:Clone)
  510. HMaterial clone();
  511. /**
  512. * Creates a new empty material.
  513. *
  514. * @note Make sure you call Material::setShader before using it.
  515. */
  516. BS_SCRIPT_EXPORT(ec:Material)
  517. static HMaterial create();
  518. /** Creates a new material with the specified shader. */
  519. BS_SCRIPT_EXPORT(ec:Material)
  520. static HMaterial create(const HShader& shader);
  521. /** @name Internal
  522. * @{
  523. */
  524. /**
  525. * Marks the core data as dirty. This causes the syncToCore() method to trigger the next time objects are synced
  526. * between core and sim threads.
  527. */
  528. void _markCoreDirty(MaterialDirtyFlags flags = MaterialDirtyFlags::Normal) override;
  529. /** @copydoc CoreObject::markDependenciesDirty */
  530. void _markDependenciesDirty() override;
  531. /** @copydoc IResourceListener::markListenerResourcesDirty */
  532. void _markResourcesDirty() override;
  533. /** @} */
  534. private:
  535. friend class MaterialManager;
  536. Material();
  537. Material(const HShader& shader);
  538. /** @copydoc CoreObject::createCore */
  539. SPtr<ct::CoreObject> createCore() const override;
  540. /** @copydoc CoreObject::syncToCore */
  541. CoreSyncData syncToCore(FrameAlloc* allocator) override;
  542. /** @copydoc CoreObject::getCoreDependencies */
  543. void getCoreDependencies(Vector<CoreObject*>& dependencies) override;
  544. /** @copydoc IResourceListener::getListenerResources */
  545. void getListenerResources(Vector<HResource>& resources) override;
  546. /** @copydoc IResourceListener::notifyResourceLoaded */
  547. void notifyResourceLoaded(const HResource& resource) override;
  548. /** @copydoc IResourceListener::notifyResourceChanged */
  549. void notifyResourceChanged(const HResource& resource) override;
  550. /** @copydoc Resource::getResourceDependencies */
  551. void getResourceDependencies(FrameVector<HResource>& dependencies) const override;
  552. /** Performs material initialization when all resources are ready. */
  553. void initializeIfLoaded();
  554. /**
  555. * Uses the provided list of parameters to try to set every parameter in this material. Parameter whose name, type
  556. * or size don't match are ignored and will not be set.
  557. */
  558. void setParams(const SPtr<MaterialParams>& params);
  559. UINT32 mLoadFlags;
  560. /************************************************************************/
  561. /* RTTI */
  562. /************************************************************************/
  563. public:
  564. friend class MaterialRTTI;
  565. static RTTITypeBase* getRTTIStatic();
  566. RTTITypeBase* getRTTI() const override;
  567. };
  568. /** @} */
  569. namespace ct
  570. {
  571. /** @addtogroup Material-Internal
  572. * @{
  573. */
  574. /** @copydoc MaterialBase */
  575. class BS_CORE_EXPORT Material : public CoreObject, public TMaterial<true>
  576. {
  577. public:
  578. ~Material() { }
  579. /** @copydoc bs::Material::setShader */
  580. void setShader(const SPtr<Shader>& shader);
  581. /**
  582. * Sets a shader that will be used by the material. Material will be initialized using a subset of compatible
  583. * techniques matching the provided variation. Shader must be set before doing any other operations with the
  584. * material.
  585. */
  586. void setShader(const SPtr<Shader>& shader, const ShaderVariation& variation);
  587. /**
  588. * Creates a new material with the specified shader. If the shader has multiple variations all of them are
  589. * initialized and can then be retrieved through findTechnique(const ShaderVariation&).
  590. */
  591. static SPtr<Material> create(const SPtr<Shader>& shader);
  592. /** Creates a new material with the specified shader, using only the provided shader variation. */
  593. static SPtr<Material> create(const SPtr<Shader>& shader, const ShaderVariation& variation);
  594. private:
  595. friend class bs::Material;
  596. Material() { }
  597. Material(const SPtr<Shader>& shader);
  598. Material(const SPtr<Shader>& shader, const ShaderVariation& variation);
  599. Material(const SPtr<Shader>& shader, const Vector<SPtr<Technique>>& techniques,
  600. const SPtr<MaterialParams>& materialParams);
  601. /** @copydoc CoreObject::syncToCore */
  602. void syncToCore(const CoreSyncData& data) override;
  603. };
  604. /** @} */
  605. }
  606. }