BsMaterial.h 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576
  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 2D vector 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. Vector2 getVec2(const String& name, UINT32 arrayIdx = 0) const { return getParamVec2(name).get(arrayIdx); }
  188. /**
  189. * @brief Returns a 3D 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. Vector3 getVec3(const String& name, UINT32 arrayIdx = 0) const { return getParamVec3(name).get(arrayIdx); }
  194. /**
  195. * @brief Returns a 4D 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. Vector4 getVec4(const String& name, UINT32 arrayIdx = 0) const { return getParamVec4(name).get(arrayIdx); }
  200. /**
  201. * @brief Returns a 3x3 matrix 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. Matrix3 getMat3(const String& name, UINT32 arrayIdx = 0) const { return getParamMat3(name).get(arrayIdx); }
  206. /**
  207. * @brief Returns a 4x4 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. Matrix4 getMat4(const String& name, UINT32 arrayIdx = 0) const { return getParamMat4(name).get(arrayIdx); }
  212. /**
  213. * @brief Returns a buffer representing a structure assigned to 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. StructData getStructData(const String& name, UINT32 arrayIdx = 0) const;
  218. /**
  219. * @brief Returns a float GPU parameter. This parameter may be used for
  220. * more efficiently getting/setting GPU parameter values than calling
  221. * Material::get* / Material::set* methods.
  222. *
  223. * @note Expected behavior is that you would retrieve this parameter when
  224. * initially constructing the material, and then use it throughout material
  225. * lifetime to assign and retrieve parameter values.
  226. *
  227. * If material shader changes this handle will be invalidated.
  228. */
  229. GpuParamFloat getParamFloat(const String& name) const;
  230. /**
  231. * @brief Returns a 2D vector GPU parameter. This parameter may be used for
  232. * more efficiently getting/setting GPU parameter values than calling
  233. * Material::get* / Material::set* methods.
  234. *
  235. * @note Expected behavior is that you would retrieve this parameter when
  236. * initially constructing the material, and then use it throughout material
  237. * lifetime to assign and retrieve parameter values.
  238. *
  239. * If material shader changes this handle will be invalidated.
  240. */
  241. GpuParamVec2 getParamVec2(const String& name) const;
  242. /**
  243. * @brief Returns a 3D vector GPU parameter. This parameter may be used for
  244. * more efficiently getting/setting GPU parameter values than calling
  245. * Material::get* / Material::set* methods.
  246. *
  247. * @note Expected behavior is that you would retrieve this parameter when
  248. * initially constructing the material, and then use it throughout material
  249. * lifetime to assign and retrieve parameter values.
  250. *
  251. * If material shader changes this handle will be invalidated.
  252. */
  253. GpuParamVec3 getParamVec3(const String& name) const;
  254. /**
  255. * @brief Returns a 4D vector GPU parameter. This parameter may be used for
  256. * more efficiently getting/setting GPU parameter values than calling
  257. * Material::get* / Material::set* methods.
  258. *
  259. * @note Expected behavior is that you would retrieve this parameter when
  260. * initially constructing the material, and then use it throughout material
  261. * lifetime to assign and retrieve parameter values.
  262. *
  263. * If material shader changes this handle will be invalidated.
  264. */
  265. GpuParamVec4 getParamVec4(const String& name) const;
  266. /**
  267. * @brief Returns a 3x3 matrix GPU parameter. This parameter may be used for
  268. * more efficiently getting/setting GPU parameter values than calling
  269. * Material::get* / Material::set* methods.
  270. *
  271. * @note Expected behavior is that you would retrieve this parameter when
  272. * initially constructing the material, and then use it throughout material
  273. * lifetime to assign and retrieve parameter values.
  274. *
  275. * If material shader changes this handle will be invalidated.
  276. */
  277. GpuParamMat3 getParamMat3(const String& name) const;
  278. /**
  279. * @brief Returns a 4x4 matrix GPU parameter. This parameter may be used for
  280. * more efficiently getting/setting GPU parameter values than calling
  281. * Material::get* / Material::set* methods.
  282. *
  283. * @note Expected behavior is that you would retrieve this parameter when
  284. * initially constructing the material, and then use it throughout material
  285. * lifetime to assign and retrieve parameter values.
  286. *
  287. * If material shader changes this handle will be invalidated.
  288. */
  289. GpuParamMat4 getParamMat4(const String& name) const;
  290. /**
  291. * @brief Returns a structure GPU parameter. This parameter may be used for
  292. * more efficiently getting/setting GPU parameter values than calling
  293. * Material::get* / Material::set* methods.
  294. *
  295. * @note Expected behavior is that you would retrieve this parameter when
  296. * initially constructing the material, and then use it throughout material
  297. * lifetime to assign and retrieve parameter values.
  298. *
  299. * If material shader changes this handle will be invalidated.
  300. */
  301. GpuParamStruct getParamStruct(const String& name) const;
  302. /**
  303. * @brief Returns a texture GPU parameter. This parameter may be used for
  304. * more efficiently getting/setting GPU parameter values than calling
  305. * Material::get* / Material::set* methods.
  306. *
  307. * @note Expected behavior is that you would retrieve this parameter when
  308. * initially constructing the material, and then use it throughout material
  309. * lifetime to assign and retrieve parameter values.
  310. *
  311. * If material shader changes this handle will be invalidated.
  312. */
  313. GpuParamTexture getParamTexture(const String& name) const;
  314. /**
  315. * @brief Returns a sampler state GPU parameter. This parameter may be used for
  316. * more efficiently getting/setting GPU parameter values than calling
  317. * Material::get* / Material::set* methods.
  318. *
  319. * @note Expected behavior is that you would retrieve this parameter when
  320. * initially constructing the material, and then use it throughout material
  321. * lifetime to assign and retrieve parameter values.
  322. *
  323. * If material shader changes this handle will be invalidated.
  324. */
  325. GpuParamSampState getParamSamplerState(const String& name) const;
  326. /**
  327. * @brief Returns the number of passes that are used
  328. * by the shader contained in the material.
  329. */
  330. UINT32 getNumPasses() const;
  331. /**
  332. * @brief Retrieves a specific shader pass.
  333. */
  334. PassPtr getPass(UINT32 passIdx) const;
  335. /**
  336. * @brief Returns a set of parameters for all GPU programs
  337. * in the specified shader pass.
  338. */
  339. PassParametersPtr getPassParameters(UINT32 passIdx) const;
  340. /**
  341. * @brief Creates a new empty material.
  342. *
  343. * @note Make sure you call Material::setShader before using it.
  344. */
  345. static HMaterial create();
  346. /**
  347. * @brief Creates a new material with the specified shader.
  348. */
  349. static HMaterial create(ShaderPtr shader);
  350. /************************************************************************/
  351. /* CORE PROXY */
  352. /************************************************************************/
  353. /**
  354. * @brief Checks is the core dirty flag set. This is used by external systems
  355. * to know when internal data has changed and core thread potentially needs to be notified.
  356. *
  357. * @note Sim thread only.
  358. */
  359. bool _isCoreDirty(MaterialDirtyFlag flag) const;
  360. /**
  361. * @brief Marks the core dirty flag as clean.
  362. *
  363. * @note Sim thread only.
  364. */
  365. void _markCoreClean(MaterialDirtyFlag flag);
  366. /**
  367. * @brief Gets the currently active proxy of this material.
  368. */
  369. MaterialProxyPtr _getActiveProxy() const { return mActiveProxy; }
  370. /**
  371. * @brief Sets an active proxy for this material.
  372. */
  373. void _setActiveProxy(const MaterialProxyPtr& proxy) { mActiveProxy = proxy; }
  374. /**
  375. * @brief Returns updated GPU parameters since the last time the parameters were marked clean.
  376. */
  377. Vector<MaterialProxy::ParamsBindInfo> _getDirtyProxyParams();
  378. /**
  379. * @brief Creates a new core proxy from the currently set material data. Core proxies ensure
  380. * that the core thread has all the necessary material data, while avoiding the need
  381. * to manage Material itself on the core thread.
  382. *
  383. * @note Sim thread only.
  384. * You generally need to update the core thread with a new proxy whenever core
  385. * dirty flag is set.
  386. */
  387. MaterialProxyPtr _createProxy();
  388. protected:
  389. /**
  390. * @copydoc Resource::destroy_internal
  391. */
  392. void destroy_internal();
  393. /**
  394. * @brief Allows you to retrieve a handle to a parameter that you can then use for quickly
  395. * setting and retrieving parameter data. This allows you to set/get parameter data
  396. * without all the cost of extra lookups otherwise required.
  397. *
  398. * @note All of these handles will be invalidated if material shader ever changes. It is up to the
  399. * caller to keep track of that.
  400. */
  401. template <typename T>
  402. void getParam(const String& name, TGpuDataParam<T>& output) const
  403. {
  404. throwIfNotInitialized();
  405. auto iterFind = mValidParams.find(name);
  406. if(iterFind == mValidParams.end())
  407. {
  408. LOGWRN("Material doesn't have a parameter named " + name);
  409. return;
  410. }
  411. const String& gpuVarName = iterFind->second;
  412. GpuParamsPtr params = findParamsWithName(gpuVarName);
  413. params->getParam<T>(gpuVarName, output);
  414. }
  415. private:
  416. friend class MaterialManager;
  417. ShaderPtr mShader;
  418. TechniquePtr mBestTechnique;
  419. INT32 mCoreDirtyFlags;
  420. Set<String> mValidShareableParamBlocks;
  421. Map<String, String> mValidParams; // Also maps Shader param name -> gpu variable name
  422. Vector<PassParametersPtr> mParametersPerPass;
  423. Vector<GpuParamBlockBufferPtr> mParamBuffers;
  424. MaterialProxyPtr mActiveProxy;
  425. Material();
  426. /**
  427. * @brief Throw an exception if no shader is set, or no acceptable
  428. * technique was found.
  429. */
  430. void throwIfNotInitialized() const;
  431. /**
  432. * @brief Marks the core data as dirty.
  433. */
  434. void markCoreDirty() { mCoreDirtyFlags = 0xFFFFFFFF; }
  435. /**
  436. * @brief Retrieves a list of all shader GPU parameters, and the GPU program variable names they map to.
  437. */
  438. const Map<String, String>& getValidParamNames() const { return mValidParams; }
  439. /**
  440. * @brief Initializes the material by using the best technique from the currently set shader. Shader
  441. * must contain the technique that matches the current renderer and render system.
  442. */
  443. void initBestTechnique();
  444. /**
  445. * @brief Constructs a map containing all data parameters (e.g. float, vector3, color).
  446. * Map contains parameter names and descriptions.
  447. */
  448. Map<String, const GpuParamDataDesc*> determineValidDataParameters(const Vector<GpuParamDescPtr>& paramDescs) const;
  449. /**
  450. * @brief Constructs a list containing all object parameter (e.g. texture, sampler state) names.
  451. */
  452. Set<String> determineValidObjectParameters(const Vector<GpuParamDescPtr>& paramDescs) const;
  453. /**
  454. * @brief Constructs a list containing all shareable parameter block names. Shareable blocks may be shared between
  455. * different GPU programs, passes or even materials.
  456. */
  457. Set<String> determineValidShareableParamBlocks(const Vector<GpuParamDescPtr>& paramDescs) const;
  458. /**
  459. * @brief Constructs a map that maps parameter names to a parameter block.
  460. */
  461. Map<String, String> determineParameterToBlockMapping(const Vector<GpuParamDescPtr>& paramDescs);
  462. /**
  463. * @brief Checks are the specified two parameters equal
  464. *
  465. * @param paramA The parameter a to compare.
  466. * @param paramB The parameter b to compare.
  467. * @param ignoreBufferOffsets (optional) If true, parameter offsets into the parameter buffer will be ignored
  468. * when comparing.
  469. */
  470. bool areParamsEqual(const GpuParamDataDesc& paramA, const GpuParamDataDesc& paramB, bool ignoreBufferOffsets = false) const;
  471. /**
  472. * @brief Frees all parameter block buffers.
  473. */
  474. void freeParamBuffers();
  475. /**
  476. * @brief Finds a set of GPU parameters containing a data (e.g. float, vector2) parameter with the provided name.
  477. */
  478. GpuParamsPtr findParamsWithName(const String& name) const;
  479. /**
  480. * @brief Finds a set of GPU parameters containing a texture parameter with the provided name.
  481. */
  482. GpuParamsPtr findTexWithName(const String& name) const;
  483. /**
  484. * @brief Finds a set of GPU parameters containing a sampler state parameter with the provided name.
  485. */
  486. GpuParamsPtr findSamplerStateWithName(const String& name) const;
  487. /************************************************************************/
  488. /* RTTI */
  489. /************************************************************************/
  490. public:
  491. friend class MaterialRTTI;
  492. static RTTITypeBase* getRTTIStatic();
  493. virtual RTTITypeBase* getRTTI() const;
  494. };
  495. }