BsMaterial.h 20 KB

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