BsMaterial.h 22 KB

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