BsMaterial.h 28 KB

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