BsMaterial.h 32 KB

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