BsMaterial.h 30 KB

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