BsMaterial.h 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599
  1. #pragma once
  2. #include "BsCorePrerequisites.h"
  3. #include "BsResource.h"
  4. #include "BsGpuParam.h"
  5. #include "BsMaterialProxy.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. /**
  14. * @brief Type of material dirty flags
  15. */
  16. enum class MaterialDirtyFlag
  17. {
  18. Material = 0x01, /**< Internal material data is dirty. */
  19. Proxy = 0x02, /**< Active proxy needs to be updated. */
  20. Params = 0x04 /**< Parameters are dirty. */
  21. };
  22. /**
  23. * @brief Helper class containing parameters for all types
  24. * of GPU programs used in a pass.
  25. */
  26. class BS_CORE_EXPORT PassParameters
  27. {
  28. public:
  29. GpuParamsPtr mVertParams;
  30. GpuParamsPtr mFragParams;
  31. GpuParamsPtr mGeomParams;
  32. GpuParamsPtr mHullParams;
  33. GpuParamsPtr mDomainParams;
  34. GpuParamsPtr mComputeParams;
  35. /**
  36. * @brief Returns a set of GPU parameters based on an index.
  37. *
  38. * @note Useful when needing to iterate over all sets of GPU parameters.
  39. */
  40. GpuParamsPtr& getParamByIdx(UINT32 idx)
  41. {
  42. GpuParamsPtr* paramArray[] = {&mVertParams, &mFragParams, &mGeomParams, &mHullParams, &mDomainParams, &mComputeParams};
  43. return *paramArray[idx];
  44. }
  45. /**
  46. * @brief Sets GPU parameters based on an index.
  47. *
  48. * @note Useful when needing to iterate over all sets of GPU parameters.
  49. */
  50. void setParamByIdx(UINT32 idx, const GpuParamsPtr& params)
  51. {
  52. GpuParamsPtr* paramArray[] = {&mVertParams, &mFragParams, &mGeomParams, &mHullParams, &mDomainParams, &mComputeParams};
  53. (*paramArray[idx]) = params;
  54. }
  55. /**
  56. * @brief Returns the total number of stored sets of
  57. * GPU parameters in this object.
  58. */
  59. UINT32 getNumParams() const { return 6; }
  60. };
  61. /**
  62. * @brief Material represents a shader and parameters used to set up
  63. * that shader. It provides a simple interface for manipulating the
  64. * parameters.
  65. */
  66. class BS_CORE_EXPORT Material : public Resource
  67. {
  68. public:
  69. /**
  70. * @brief Data used to described a structure defined within a shader.
  71. */
  72. struct StructData
  73. {
  74. StructData()
  75. :size(0), data(nullptr)
  76. { }
  77. StructData(UINT32 _size)
  78. :size(_size)
  79. {
  80. data = std::shared_ptr<void>(bs_alloc<ScratchAlloc>(_size), &bs_free<ScratchAlloc>);
  81. }
  82. /**
  83. * @brief Writes the specified data to the internal buffer. Caller
  84. * must ensure size of the provided buffer is correct.
  85. */
  86. void write(void* _data)
  87. {
  88. memcpy(data.get(), _data, size);
  89. }
  90. std::shared_ptr<void> data;
  91. UINT32 size;
  92. };
  93. ~Material();
  94. /**
  95. * @brief Sets a shader that will be used by the material. Best technique within the
  96. * provided shader will be used for the material.
  97. *
  98. * @note Shader must be set before doing any other operations with the material.
  99. *
  100. * After setting the shader if change any systems that a shader technique is
  101. * dependent upon (render system, renderer, etc), you will need to call this
  102. * method again on all your Materials to make sure technique used is updated.
  103. */
  104. void setShader(ShaderPtr shader);
  105. /**
  106. * @brief Returns the currently active shader.
  107. */
  108. ShaderPtr getShader() const { return mShader; }
  109. /** @brief Assigns a texture to the shader parameter with the specified name. */
  110. void setTexture(const String& name, const HTexture& value) { return getParamTexture(name).set(value); }
  111. /** @brief Assigns a sampler state to the shader parameter with the specified name. */
  112. void setSamplerState(const String& name, const HSamplerState& value) { return getParamSamplerState(name).set(value); }
  113. /**
  114. * @brief Assigns a float value to the shader parameter with the specified name.
  115. *
  116. * Optionally if the parameter is an array you may provide an array index to assign the value to.
  117. */
  118. void setFloat(const String& name, float value, UINT32 arrayIdx = 0) { return getParamFloat(name).set(value, arrayIdx); }
  119. /**
  120. * @brief Assigns a color to the shader parameter with the specified name.
  121. *
  122. * Optionally if the parameter is an array you may provide an array index to assign the value to.
  123. */
  124. void setColor(const String& name, const Color& value, UINT32 arrayIdx = 0);
  125. /**
  126. * @brief Assigns a 2D vector to the shader parameter with the specified name.
  127. *
  128. * Optionally if the parameter is an array you may provide an array index to assign the value to.
  129. */
  130. void setVec2(const String& name, const Vector2& value, UINT32 arrayIdx = 0) { return getParamVec2(name).set(value, arrayIdx); }
  131. /**
  132. * @brief Assigns a 3D vector to the shader parameter with the specified name.
  133. *
  134. * Optionally if the parameter is an array you may provide an array index to assign the value to.
  135. */
  136. void setVec3(const String& name, const Vector3& value, UINT32 arrayIdx = 0) { return getParamVec3(name).set(value, arrayIdx); }
  137. /**
  138. * @brief Assigns a 4D vector to the shader parameter with the specified name.
  139. *
  140. * Optionally if the parameter is an array you may provide an array index to assign the value to.
  141. */
  142. void setVec4(const String& name, const Vector4& value, UINT32 arrayIdx = 0) { return getParamVec4(name).set(value, arrayIdx); }
  143. /**
  144. * @brief Assigns a 3x3 matrix to the shader parameter with the specified name.
  145. *
  146. * Optionally if the parameter is an array you may provide an array index to assign the value to.
  147. */
  148. void setMat3(const String& name, const Matrix3& value, UINT32 arrayIdx = 0) { return getParamMat3(name).set(value, arrayIdx); }
  149. /**
  150. * @brief Assigns a 4x4 matrix to the shader parameter with the specified name.
  151. *
  152. * Optionally if the parameter is an array you may provide an array index to assign the value to.
  153. */
  154. void setMat4(const String& name, const Matrix4& value, UINT32 arrayIdx = 0) { return getParamMat4(name).set(value, arrayIdx); }
  155. /**
  156. * @brief Assigns a structure to the shader parameter with the specified name.
  157. *
  158. * Structure is provided as a raw buffer and caller must ensure structure in buffer
  159. * matches what the shader expects.
  160. *
  161. * Optionally if the parameter is an array you may provide an array index to assign the value to.
  162. */
  163. void setStructData(const String& name, void* value, UINT32 size, UINT32 arrayIdx = 0) { return getParamStruct(name).set(value, size, arrayIdx); }
  164. /**
  165. * @brief Assign a parameter block buffer with the specified name.
  166. *
  167. * @note Parameter block buffers can be used as quick way of setting multiple parameters on a material at once, or
  168. * potentially sharing parameters between multiple materials. This reduces driver overhead as the parameters
  169. * in the buffers need only be set once and then reused multiple times.
  170. */
  171. void setParamBlockBuffer(const String& name, const GpuParamBlockBufferPtr& paramBlock);
  172. /** @brief Returns a texture assigned with the parameter with the specified name. */
  173. HTexture getTexture(const String& name) const { return getParamTexture(name).get(); }
  174. /** @brief Returns a sampler state assigned with the parameter with the specified name. */
  175. HSamplerState getSamplerState(const String& name) const { return getParamSamplerState(name).get(); }
  176. /**
  177. * @brief Returns a float value assigned with the parameter with the specified name.
  178. *
  179. * Optionally if the parameter is an array you may provide an array index you which to retrieve.
  180. */
  181. float getFloat(const String& name, UINT32 arrayIdx = 0) const { return getParamFloat(name).get(arrayIdx); }
  182. /**
  183. * @brief Returns a color assigned with the parameter with the specified name.
  184. *
  185. * Optionally if the parameter is an array you may provide an array index you which to retrieve.
  186. */
  187. Color getColor(const String& name, UINT32 arrayIdx = 0) const { return getParamColor(name).get(arrayIdx); }
  188. /**
  189. * @brief Returns a 2D vector assigned with the parameter with the specified name.
  190. *
  191. * Optionally if the parameter is an array you may provide an array index you which to retrieve.
  192. */
  193. Vector2 getVec2(const String& name, UINT32 arrayIdx = 0) const { return getParamVec2(name).get(arrayIdx); }
  194. /**
  195. * @brief Returns a 3D vector assigned with the parameter with the specified name.
  196. *
  197. * Optionally if the parameter is an array you may provide an array index you which to retrieve.
  198. */
  199. Vector3 getVec3(const String& name, UINT32 arrayIdx = 0) const { return getParamVec3(name).get(arrayIdx); }
  200. /**
  201. * @brief Returns a 4D vector assigned with the parameter with the specified name.
  202. *
  203. * Optionally if the parameter is an array you may provide an array index you which to retrieve.
  204. */
  205. Vector4 getVec4(const String& name, UINT32 arrayIdx = 0) const { return getParamVec4(name).get(arrayIdx); }
  206. /**
  207. * @brief Returns a 3x3 matrix assigned with the parameter with the specified name.
  208. *
  209. * Optionally if the parameter is an array you may provide an array index you which to retrieve.
  210. */
  211. Matrix3 getMat3(const String& name, UINT32 arrayIdx = 0) const { return getParamMat3(name).get(arrayIdx); }
  212. /**
  213. * @brief Returns a 4x4 matrix assigned with the parameter with the specified name.
  214. *
  215. * Optionally if the parameter is an array you may provide an array index you which to retrieve.
  216. */
  217. Matrix4 getMat4(const String& name, UINT32 arrayIdx = 0) const { return getParamMat4(name).get(arrayIdx); }
  218. /**
  219. * @brief Returns a buffer representing a structure assigned to the parameter with the specified name.
  220. *
  221. * Optionally if the parameter is an array you may provide an array index you which to retrieve.
  222. */
  223. StructData getStructData(const String& name, UINT32 arrayIdx = 0) const;
  224. /**
  225. * @brief Returns a float GPU parameter. This parameter may be used for
  226. * more efficiently getting/setting GPU parameter values than calling
  227. * Material::get* / Material::set* methods.
  228. *
  229. * @note Expected behavior is that you would retrieve this parameter when
  230. * initially constructing the material, and then use it throughout material
  231. * lifetime to assign and retrieve parameter values.
  232. *
  233. * If material shader changes this handle will be invalidated.
  234. */
  235. GpuParamFloat getParamFloat(const String& name) const;
  236. /**
  237. * @brief Returns a color GPU parameter. This parameter may be used for
  238. * more efficiently getting/setting GPU parameter values than calling
  239. * Material::get* / Material::set* methods.
  240. *
  241. * @note Expected behavior is that you would retrieve this parameter when
  242. * initially constructing the material, and then use it throughout material
  243. * lifetime to assign and retrieve parameter values.
  244. *
  245. * If material shader changes this handle will be invalidated.
  246. */
  247. GpuParamColor getParamColor(const String& name) const;
  248. /**
  249. * @brief Returns a 2D vector GPU parameter. This parameter may be used for
  250. * more efficiently getting/setting GPU parameter values than calling
  251. * Material::get* / Material::set* methods.
  252. *
  253. * @note Expected behavior is that you would retrieve this parameter when
  254. * initially constructing the material, and then use it throughout material
  255. * lifetime to assign and retrieve parameter values.
  256. *
  257. * If material shader changes this handle will be invalidated.
  258. */
  259. GpuParamVec2 getParamVec2(const String& name) const;
  260. /**
  261. * @brief Returns a 3D vector GPU parameter. This parameter may be used for
  262. * more efficiently getting/setting GPU parameter values than calling
  263. * Material::get* / Material::set* methods.
  264. *
  265. * @note Expected behavior is that you would retrieve this parameter when
  266. * initially constructing the material, and then use it throughout material
  267. * lifetime to assign and retrieve parameter values.
  268. *
  269. * If material shader changes this handle will be invalidated.
  270. */
  271. GpuParamVec3 getParamVec3(const String& name) const;
  272. /**
  273. * @brief Returns a 4D vector GPU parameter. This parameter may be used for
  274. * more efficiently getting/setting GPU parameter values than calling
  275. * Material::get* / Material::set* methods.
  276. *
  277. * @note Expected behavior is that you would retrieve this parameter when
  278. * initially constructing the material, and then use it throughout material
  279. * lifetime to assign and retrieve parameter values.
  280. *
  281. * If material shader changes this handle will be invalidated.
  282. */
  283. GpuParamVec4 getParamVec4(const String& name) const;
  284. /**
  285. * @brief Returns a 3x3 matrix GPU parameter. This parameter may be used for
  286. * more efficiently getting/setting GPU parameter values than calling
  287. * Material::get* / Material::set* methods.
  288. *
  289. * @note Expected behavior is that you would retrieve this parameter when
  290. * initially constructing the material, and then use it throughout material
  291. * lifetime to assign and retrieve parameter values.
  292. *
  293. * If material shader changes this handle will be invalidated.
  294. */
  295. GpuParamMat3 getParamMat3(const String& name) const;
  296. /**
  297. * @brief Returns a 4x4 matrix GPU parameter. This parameter may be used for
  298. * more efficiently getting/setting GPU parameter values than calling
  299. * Material::get* / Material::set* methods.
  300. *
  301. * @note Expected behavior is that you would retrieve this parameter when
  302. * initially constructing the material, and then use it throughout material
  303. * lifetime to assign and retrieve parameter values.
  304. *
  305. * If material shader changes this handle will be invalidated.
  306. */
  307. GpuParamMat4 getParamMat4(const String& name) const;
  308. /**
  309. * @brief Returns a structure GPU parameter. This parameter may be used for
  310. * more efficiently getting/setting GPU parameter values than calling
  311. * Material::get* / Material::set* methods.
  312. *
  313. * @note Expected behavior is that you would retrieve this parameter when
  314. * initially constructing the material, and then use it throughout material
  315. * lifetime to assign and retrieve parameter values.
  316. *
  317. * If material shader changes this handle will be invalidated.
  318. */
  319. GpuParamStruct getParamStruct(const String& name) const;
  320. /**
  321. * @brief Returns a texture GPU parameter. This parameter may be used for
  322. * more efficiently getting/setting GPU parameter values than calling
  323. * Material::get* / Material::set* methods.
  324. *
  325. * @note Expected behavior is that you would retrieve this parameter when
  326. * initially constructing the material, and then use it throughout material
  327. * lifetime to assign and retrieve parameter values.
  328. *
  329. * If material shader changes this handle will be invalidated.
  330. */
  331. GpuParamTexture getParamTexture(const String& name) const;
  332. /**
  333. * @brief Returns a sampler state GPU parameter. This parameter may be used for
  334. * more efficiently getting/setting GPU parameter values than calling
  335. * Material::get* / Material::set* methods.
  336. *
  337. * @note Expected behavior is that you would retrieve this parameter when
  338. * initially constructing the material, and then use it throughout material
  339. * lifetime to assign and retrieve parameter values.
  340. *
  341. * If material shader changes this handle will be invalidated.
  342. */
  343. GpuParamSampState getParamSamplerState(const String& name) const;
  344. /**
  345. * @brief Returns the number of passes that are used
  346. * by the shader contained in the material.
  347. */
  348. UINT32 getNumPasses() const;
  349. /**
  350. * @brief Retrieves a specific shader pass.
  351. */
  352. PassPtr getPass(UINT32 passIdx) const;
  353. /**
  354. * @brief Returns a set of parameters for all GPU programs
  355. * in the specified shader pass.
  356. */
  357. PassParametersPtr getPassParameters(UINT32 passIdx) const;
  358. /**
  359. * @brief Creates a new empty material.
  360. *
  361. * @note Make sure you call Material::setShader before using it.
  362. */
  363. static HMaterial create();
  364. /**
  365. * @brief Creates a new material with the specified shader.
  366. */
  367. static HMaterial create(ShaderPtr shader);
  368. /************************************************************************/
  369. /* CORE PROXY */
  370. /************************************************************************/
  371. /**
  372. * @brief Checks is the core dirty flag set. This is used by external systems
  373. * to know when internal data has changed and core thread potentially needs to be notified.
  374. *
  375. * @note Sim thread only.
  376. */
  377. bool _isCoreDirty(MaterialDirtyFlag flag) const;
  378. /**
  379. * @brief Marks the core dirty flag as clean.
  380. *
  381. * @note Sim thread only.
  382. */
  383. void _markCoreClean(MaterialDirtyFlag flag);
  384. /**
  385. * @brief Gets the currently active proxy of this material.
  386. */
  387. MaterialProxyPtr _getActiveProxy() const { return mActiveProxy; }
  388. /**
  389. * @brief Sets an active proxy for this material.
  390. */
  391. void _setActiveProxy(const MaterialProxyPtr& proxy) { mActiveProxy = proxy; }
  392. /**
  393. * @brief Returns updated GPU parameters since the last time the parameters were marked clean.
  394. *
  395. * @note Returned data will be allocated with a frame allocator and must be released during the
  396. * same frame it was allocated.
  397. */
  398. MaterialProxy::DirtyParamsInfo* _getDirtyProxyParams(FrameAlloc* frameAlloc);
  399. /**
  400. * @brief Creates a new core proxy from the currently set material data. Core proxies ensure
  401. * that the core thread has all the necessary material data, while avoiding the need
  402. * to manage Material itself on the core thread.
  403. *
  404. * @note Sim thread only.
  405. * You generally need to update the core thread with a new proxy whenever core
  406. * dirty flag is set.
  407. */
  408. MaterialProxyPtr _createProxy();
  409. protected:
  410. /**
  411. * @copydoc Resource::destroy_internal
  412. */
  413. void destroy_internal();
  414. /**
  415. * @brief Allows you to retrieve a handle to a parameter that you can then use for quickly
  416. * setting and retrieving parameter data. This allows you to set/get parameter data
  417. * without all the cost of extra lookups otherwise required.
  418. *
  419. * @note All of these handles will be invalidated if material shader ever changes. It is up to the
  420. * caller to keep track of that.
  421. */
  422. template <typename T>
  423. void getParam(const String& name, TGpuDataParam<T>& output) const
  424. {
  425. throwIfNotInitialized();
  426. auto iterFind = mValidParams.find(name);
  427. if(iterFind == mValidParams.end())
  428. {
  429. LOGWRN("Material doesn't have a parameter named " + name);
  430. return;
  431. }
  432. const String& gpuVarName = iterFind->second;
  433. GpuParamsPtr params = findParamsWithName(gpuVarName);
  434. params->getParam<T>(gpuVarName, output);
  435. }
  436. private:
  437. friend class MaterialManager;
  438. ShaderPtr mShader;
  439. TechniquePtr mBestTechnique;
  440. INT32 mCoreDirtyFlags;
  441. Set<String> mValidShareableParamBlocks;
  442. Map<String, String> mValidParams; // Also maps Shader param name -> gpu variable name
  443. Vector<PassParametersPtr> mParametersPerPass;
  444. Vector<GpuParamBlockBufferPtr> mParamBuffers;
  445. MaterialProxyPtr mActiveProxy;
  446. Material();
  447. /**
  448. * @brief Throw an exception if no shader is set, or no acceptable
  449. * technique was found.
  450. */
  451. void throwIfNotInitialized() const;
  452. /**
  453. * @brief Marks the core data as dirty.
  454. */
  455. void markCoreDirty() { mCoreDirtyFlags = 0xFFFFFFFF; }
  456. /**
  457. * @brief Retrieves a list of all shader GPU parameters, and the GPU program variable names they map to.
  458. */
  459. const Map<String, String>& getValidParamNames() const { return mValidParams; }
  460. /**
  461. * @brief Initializes the material by using the best technique from the currently set shader. Shader
  462. * must contain the technique that matches the current renderer and render system.
  463. */
  464. void initBestTechnique();
  465. /**
  466. * @brief Constructs a map containing all data parameters (e.g. float, vector3, color).
  467. * Map contains parameter names and descriptions.
  468. */
  469. Map<String, const GpuParamDataDesc*> determineValidDataParameters(const Vector<GpuParamDescPtr>& paramDescs) const;
  470. /**
  471. * @brief Constructs a list containing all object parameter (e.g. texture, sampler state) names.
  472. */
  473. Vector<const GpuParamObjectDesc*> determineValidObjectParameters(const Vector<GpuParamDescPtr>& paramDescs) const;
  474. /**
  475. * @brief Constructs a list containing all shareable parameter block names. Shareable blocks may be shared between
  476. * different GPU programs, passes or even materials.
  477. */
  478. Set<String> determineValidShareableParamBlocks(const Vector<GpuParamDescPtr>& paramDescs) const;
  479. /**
  480. * @brief Constructs a map that maps parameter names to a parameter block.
  481. */
  482. Map<String, String> determineParameterToBlockMapping(const Vector<GpuParamDescPtr>& paramDescs);
  483. /**
  484. * @brief Checks are the specified two parameters equal
  485. *
  486. * @param paramA The parameter a to compare.
  487. * @param paramB The parameter b to compare.
  488. * @param ignoreBufferOffsets (optional) If true, parameter offsets into the parameter buffer will be ignored
  489. * when comparing.
  490. */
  491. bool areParamsEqual(const GpuParamDataDesc& paramA, const GpuParamDataDesc& paramB, bool ignoreBufferOffsets = false) const;
  492. /**
  493. * @brief Frees all parameter block buffers.
  494. */
  495. void freeParamBuffers();
  496. /**
  497. * @brief Finds a set of GPU parameters containing a data (e.g. float, vector2) parameter with the provided name.
  498. */
  499. GpuParamsPtr findParamsWithName(const String& name) const;
  500. /**
  501. * @brief Finds a set of GPU parameters containing a texture parameter with the provided name.
  502. */
  503. GpuParamsPtr findTexWithName(const String& name) const;
  504. /**
  505. * @brief Finds a set of GPU parameters containing a sampler state parameter with the provided name.
  506. */
  507. GpuParamsPtr findSamplerStateWithName(const String& name) const;
  508. /************************************************************************/
  509. /* RTTI */
  510. /************************************************************************/
  511. public:
  512. friend class MaterialRTTI;
  513. static RTTITypeBase* getRTTIStatic();
  514. virtual RTTITypeBase* getRTTI() const;
  515. };
  516. }